sbsa-ref.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * ARM SBSA Reference Platform emulation
  3. *
  4. * Copyright (c) 2018 Linaro Limited
  5. * Written by Hongbo Zhang <hongbo.zhang@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2 or later, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu-common.h"
  21. #include "qapi/error.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/units.h"
  24. #include "sysemu/device_tree.h"
  25. #include "sysemu/numa.h"
  26. #include "sysemu/runstate.h"
  27. #include "sysemu/sysemu.h"
  28. #include "exec/address-spaces.h"
  29. #include "exec/hwaddr.h"
  30. #include "kvm_arm.h"
  31. #include "hw/arm/boot.h"
  32. #include "hw/block/flash.h"
  33. #include "hw/boards.h"
  34. #include "hw/ide/internal.h"
  35. #include "hw/ide/ahci_internal.h"
  36. #include "hw/intc/arm_gicv3_common.h"
  37. #include "hw/loader.h"
  38. #include "hw/pci-host/gpex.h"
  39. #include "hw/qdev-properties.h"
  40. #include "hw/usb.h"
  41. #include "net/net.h"
  42. #define RAMLIMIT_GB 8192
  43. #define RAMLIMIT_BYTES (RAMLIMIT_GB * GiB)
  44. #define NUM_IRQS 256
  45. #define NUM_SMMU_IRQS 4
  46. #define NUM_SATA_PORTS 6
  47. #define VIRTUAL_PMU_IRQ 7
  48. #define ARCH_GIC_MAINT_IRQ 9
  49. #define ARCH_TIMER_VIRT_IRQ 11
  50. #define ARCH_TIMER_S_EL1_IRQ 13
  51. #define ARCH_TIMER_NS_EL1_IRQ 14
  52. #define ARCH_TIMER_NS_EL2_IRQ 10
  53. enum {
  54. SBSA_FLASH,
  55. SBSA_MEM,
  56. SBSA_CPUPERIPHS,
  57. SBSA_GIC_DIST,
  58. SBSA_GIC_REDIST,
  59. SBSA_SMMU,
  60. SBSA_UART,
  61. SBSA_RTC,
  62. SBSA_PCIE,
  63. SBSA_PCIE_MMIO,
  64. SBSA_PCIE_MMIO_HIGH,
  65. SBSA_PCIE_PIO,
  66. SBSA_PCIE_ECAM,
  67. SBSA_GPIO,
  68. SBSA_SECURE_UART,
  69. SBSA_SECURE_UART_MM,
  70. SBSA_SECURE_MEM,
  71. SBSA_AHCI,
  72. SBSA_EHCI,
  73. };
  74. typedef struct MemMapEntry {
  75. hwaddr base;
  76. hwaddr size;
  77. } MemMapEntry;
  78. typedef struct {
  79. MachineState parent;
  80. struct arm_boot_info bootinfo;
  81. int smp_cpus;
  82. void *fdt;
  83. int fdt_size;
  84. int psci_conduit;
  85. PFlashCFI01 *flash[2];
  86. } SBSAMachineState;
  87. #define TYPE_SBSA_MACHINE MACHINE_TYPE_NAME("sbsa-ref")
  88. #define SBSA_MACHINE(obj) \
  89. OBJECT_CHECK(SBSAMachineState, (obj), TYPE_SBSA_MACHINE)
  90. static const MemMapEntry sbsa_ref_memmap[] = {
  91. /* 512M boot ROM */
  92. [SBSA_FLASH] = { 0, 0x20000000 },
  93. /* 512M secure memory */
  94. [SBSA_SECURE_MEM] = { 0x20000000, 0x20000000 },
  95. /* Space reserved for CPU peripheral devices */
  96. [SBSA_CPUPERIPHS] = { 0x40000000, 0x00040000 },
  97. [SBSA_GIC_DIST] = { 0x40060000, 0x00010000 },
  98. [SBSA_GIC_REDIST] = { 0x40080000, 0x04000000 },
  99. [SBSA_UART] = { 0x60000000, 0x00001000 },
  100. [SBSA_RTC] = { 0x60010000, 0x00001000 },
  101. [SBSA_GPIO] = { 0x60020000, 0x00001000 },
  102. [SBSA_SECURE_UART] = { 0x60030000, 0x00001000 },
  103. [SBSA_SECURE_UART_MM] = { 0x60040000, 0x00001000 },
  104. [SBSA_SMMU] = { 0x60050000, 0x00020000 },
  105. /* Space here reserved for more SMMUs */
  106. [SBSA_AHCI] = { 0x60100000, 0x00010000 },
  107. [SBSA_EHCI] = { 0x60110000, 0x00010000 },
  108. /* Space here reserved for other devices */
  109. [SBSA_PCIE_PIO] = { 0x7fff0000, 0x00010000 },
  110. /* 32-bit address PCIE MMIO space */
  111. [SBSA_PCIE_MMIO] = { 0x80000000, 0x70000000 },
  112. /* 256M PCIE ECAM space */
  113. [SBSA_PCIE_ECAM] = { 0xf0000000, 0x10000000 },
  114. /* ~1TB PCIE MMIO space (4GB to 1024GB boundary) */
  115. [SBSA_PCIE_MMIO_HIGH] = { 0x100000000ULL, 0xFF00000000ULL },
  116. [SBSA_MEM] = { 0x10000000000ULL, RAMLIMIT_BYTES },
  117. };
  118. static const int sbsa_ref_irqmap[] = {
  119. [SBSA_UART] = 1,
  120. [SBSA_RTC] = 2,
  121. [SBSA_PCIE] = 3, /* ... to 6 */
  122. [SBSA_GPIO] = 7,
  123. [SBSA_SECURE_UART] = 8,
  124. [SBSA_SECURE_UART_MM] = 9,
  125. [SBSA_AHCI] = 10,
  126. [SBSA_EHCI] = 11,
  127. };
  128. /*
  129. * Firmware on this machine only uses ACPI table to load OS, these limited
  130. * device tree nodes are just to let firmware know the info which varies from
  131. * command line parameters, so it is not necessary to be fully compatible
  132. * with the kernel CPU and NUMA binding rules.
  133. */
  134. static void create_fdt(SBSAMachineState *sms)
  135. {
  136. void *fdt = create_device_tree(&sms->fdt_size);
  137. const MachineState *ms = MACHINE(sms);
  138. int nb_numa_nodes = ms->numa_state->num_nodes;
  139. int cpu;
  140. if (!fdt) {
  141. error_report("create_device_tree() failed");
  142. exit(1);
  143. }
  144. sms->fdt = fdt;
  145. qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,sbsa-ref");
  146. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  147. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  148. if (ms->numa_state->have_numa_distance) {
  149. int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
  150. uint32_t *matrix = g_malloc0(size);
  151. int idx, i, j;
  152. for (i = 0; i < nb_numa_nodes; i++) {
  153. for (j = 0; j < nb_numa_nodes; j++) {
  154. idx = (i * nb_numa_nodes + j) * 3;
  155. matrix[idx + 0] = cpu_to_be32(i);
  156. matrix[idx + 1] = cpu_to_be32(j);
  157. matrix[idx + 2] =
  158. cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
  159. }
  160. }
  161. qemu_fdt_add_subnode(fdt, "/distance-map");
  162. qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
  163. matrix, size);
  164. g_free(matrix);
  165. }
  166. qemu_fdt_add_subnode(sms->fdt, "/cpus");
  167. for (cpu = sms->smp_cpus - 1; cpu >= 0; cpu--) {
  168. char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
  169. ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
  170. CPUState *cs = CPU(armcpu);
  171. qemu_fdt_add_subnode(sms->fdt, nodename);
  172. if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
  173. qemu_fdt_setprop_cell(sms->fdt, nodename, "numa-node-id",
  174. ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
  175. }
  176. g_free(nodename);
  177. }
  178. }
  179. #define SBSA_FLASH_SECTOR_SIZE (256 * KiB)
  180. static PFlashCFI01 *sbsa_flash_create1(SBSAMachineState *sms,
  181. const char *name,
  182. const char *alias_prop_name)
  183. {
  184. /*
  185. * Create a single flash device. We use the same parameters as
  186. * the flash devices on the Versatile Express board.
  187. */
  188. DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01);
  189. qdev_prop_set_uint64(dev, "sector-length", SBSA_FLASH_SECTOR_SIZE);
  190. qdev_prop_set_uint8(dev, "width", 4);
  191. qdev_prop_set_uint8(dev, "device-width", 2);
  192. qdev_prop_set_bit(dev, "big-endian", false);
  193. qdev_prop_set_uint16(dev, "id0", 0x89);
  194. qdev_prop_set_uint16(dev, "id1", 0x18);
  195. qdev_prop_set_uint16(dev, "id2", 0x00);
  196. qdev_prop_set_uint16(dev, "id3", 0x00);
  197. qdev_prop_set_string(dev, "name", name);
  198. object_property_add_child(OBJECT(sms), name, OBJECT(dev),
  199. &error_abort);
  200. object_property_add_alias(OBJECT(sms), alias_prop_name,
  201. OBJECT(dev), "drive", &error_abort);
  202. return PFLASH_CFI01(dev);
  203. }
  204. static void sbsa_flash_create(SBSAMachineState *sms)
  205. {
  206. sms->flash[0] = sbsa_flash_create1(sms, "sbsa.flash0", "pflash0");
  207. sms->flash[1] = sbsa_flash_create1(sms, "sbsa.flash1", "pflash1");
  208. }
  209. static void sbsa_flash_map1(PFlashCFI01 *flash,
  210. hwaddr base, hwaddr size,
  211. MemoryRegion *sysmem)
  212. {
  213. DeviceState *dev = DEVICE(flash);
  214. assert(size % SBSA_FLASH_SECTOR_SIZE == 0);
  215. assert(size / SBSA_FLASH_SECTOR_SIZE <= UINT32_MAX);
  216. qdev_prop_set_uint32(dev, "num-blocks", size / SBSA_FLASH_SECTOR_SIZE);
  217. qdev_init_nofail(dev);
  218. memory_region_add_subregion(sysmem, base,
  219. sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
  220. 0));
  221. }
  222. static void sbsa_flash_map(SBSAMachineState *sms,
  223. MemoryRegion *sysmem,
  224. MemoryRegion *secure_sysmem)
  225. {
  226. /*
  227. * Map two flash devices to fill the SBSA_FLASH space in the memmap.
  228. * sysmem is the system memory space. secure_sysmem is the secure view
  229. * of the system, and the first flash device should be made visible only
  230. * there. The second flash device is visible to both secure and nonsecure.
  231. */
  232. hwaddr flashsize = sbsa_ref_memmap[SBSA_FLASH].size / 2;
  233. hwaddr flashbase = sbsa_ref_memmap[SBSA_FLASH].base;
  234. sbsa_flash_map1(sms->flash[0], flashbase, flashsize,
  235. secure_sysmem);
  236. sbsa_flash_map1(sms->flash[1], flashbase + flashsize, flashsize,
  237. sysmem);
  238. }
  239. static bool sbsa_firmware_init(SBSAMachineState *sms,
  240. MemoryRegion *sysmem,
  241. MemoryRegion *secure_sysmem)
  242. {
  243. int i;
  244. BlockBackend *pflash_blk0;
  245. /* Map legacy -drive if=pflash to machine properties */
  246. for (i = 0; i < ARRAY_SIZE(sms->flash); i++) {
  247. pflash_cfi01_legacy_drive(sms->flash[i],
  248. drive_get(IF_PFLASH, 0, i));
  249. }
  250. sbsa_flash_map(sms, sysmem, secure_sysmem);
  251. pflash_blk0 = pflash_cfi01_get_blk(sms->flash[0]);
  252. if (bios_name) {
  253. char *fname;
  254. MemoryRegion *mr;
  255. int image_size;
  256. if (pflash_blk0) {
  257. error_report("The contents of the first flash device may be "
  258. "specified with -bios or with -drive if=pflash... "
  259. "but you cannot use both options at once");
  260. exit(1);
  261. }
  262. /* Fall back to -bios */
  263. fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  264. if (!fname) {
  265. error_report("Could not find ROM image '%s'", bios_name);
  266. exit(1);
  267. }
  268. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(sms->flash[0]), 0);
  269. image_size = load_image_mr(fname, mr);
  270. g_free(fname);
  271. if (image_size < 0) {
  272. error_report("Could not load ROM image '%s'", bios_name);
  273. exit(1);
  274. }
  275. }
  276. return pflash_blk0 || bios_name;
  277. }
  278. static void create_secure_ram(SBSAMachineState *sms,
  279. MemoryRegion *secure_sysmem)
  280. {
  281. MemoryRegion *secram = g_new(MemoryRegion, 1);
  282. hwaddr base = sbsa_ref_memmap[SBSA_SECURE_MEM].base;
  283. hwaddr size = sbsa_ref_memmap[SBSA_SECURE_MEM].size;
  284. memory_region_init_ram(secram, NULL, "sbsa-ref.secure-ram", size,
  285. &error_fatal);
  286. memory_region_add_subregion(secure_sysmem, base, secram);
  287. }
  288. static void create_gic(SBSAMachineState *sms, qemu_irq *pic)
  289. {
  290. unsigned int smp_cpus = MACHINE(sms)->smp.cpus;
  291. DeviceState *gicdev;
  292. SysBusDevice *gicbusdev;
  293. const char *gictype;
  294. uint32_t redist0_capacity, redist0_count;
  295. int i;
  296. gictype = gicv3_class_name();
  297. gicdev = qdev_create(NULL, gictype);
  298. qdev_prop_set_uint32(gicdev, "revision", 3);
  299. qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
  300. /*
  301. * Note that the num-irq property counts both internal and external
  302. * interrupts; there are always 32 of the former (mandated by GIC spec).
  303. */
  304. qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
  305. qdev_prop_set_bit(gicdev, "has-security-extensions", true);
  306. redist0_capacity =
  307. sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
  308. redist0_count = MIN(smp_cpus, redist0_capacity);
  309. qdev_prop_set_uint32(gicdev, "len-redist-region-count", 1);
  310. qdev_prop_set_uint32(gicdev, "redist-region-count[0]", redist0_count);
  311. qdev_init_nofail(gicdev);
  312. gicbusdev = SYS_BUS_DEVICE(gicdev);
  313. sysbus_mmio_map(gicbusdev, 0, sbsa_ref_memmap[SBSA_GIC_DIST].base);
  314. sysbus_mmio_map(gicbusdev, 1, sbsa_ref_memmap[SBSA_GIC_REDIST].base);
  315. /*
  316. * Wire the outputs from each CPU's generic timer and the GICv3
  317. * maintenance interrupt signal to the appropriate GIC PPI inputs,
  318. * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
  319. */
  320. for (i = 0; i < smp_cpus; i++) {
  321. DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
  322. int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
  323. int irq;
  324. /*
  325. * Mapping from the output timer irq lines from the CPU to the
  326. * GIC PPI inputs used for this board.
  327. */
  328. const int timer_irq[] = {
  329. [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
  330. [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
  331. [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
  332. [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
  333. };
  334. for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
  335. qdev_connect_gpio_out(cpudev, irq,
  336. qdev_get_gpio_in(gicdev,
  337. ppibase + timer_irq[irq]));
  338. }
  339. qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt", 0,
  340. qdev_get_gpio_in(gicdev, ppibase
  341. + ARCH_GIC_MAINT_IRQ));
  342. qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
  343. qdev_get_gpio_in(gicdev, ppibase
  344. + VIRTUAL_PMU_IRQ));
  345. sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
  346. sysbus_connect_irq(gicbusdev, i + smp_cpus,
  347. qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
  348. sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
  349. qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
  350. sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
  351. qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
  352. }
  353. for (i = 0; i < NUM_IRQS; i++) {
  354. pic[i] = qdev_get_gpio_in(gicdev, i);
  355. }
  356. }
  357. static void create_uart(const SBSAMachineState *sms, qemu_irq *pic, int uart,
  358. MemoryRegion *mem, Chardev *chr)
  359. {
  360. hwaddr base = sbsa_ref_memmap[uart].base;
  361. int irq = sbsa_ref_irqmap[uart];
  362. DeviceState *dev = qdev_create(NULL, "pl011");
  363. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  364. qdev_prop_set_chr(dev, "chardev", chr);
  365. qdev_init_nofail(dev);
  366. memory_region_add_subregion(mem, base,
  367. sysbus_mmio_get_region(s, 0));
  368. sysbus_connect_irq(s, 0, pic[irq]);
  369. }
  370. static void create_rtc(const SBSAMachineState *sms, qemu_irq *pic)
  371. {
  372. hwaddr base = sbsa_ref_memmap[SBSA_RTC].base;
  373. int irq = sbsa_ref_irqmap[SBSA_RTC];
  374. sysbus_create_simple("pl031", base, pic[irq]);
  375. }
  376. static DeviceState *gpio_key_dev;
  377. static void sbsa_ref_powerdown_req(Notifier *n, void *opaque)
  378. {
  379. /* use gpio Pin 3 for power button event */
  380. qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
  381. }
  382. static Notifier sbsa_ref_powerdown_notifier = {
  383. .notify = sbsa_ref_powerdown_req
  384. };
  385. static void create_gpio(const SBSAMachineState *sms, qemu_irq *pic)
  386. {
  387. DeviceState *pl061_dev;
  388. hwaddr base = sbsa_ref_memmap[SBSA_GPIO].base;
  389. int irq = sbsa_ref_irqmap[SBSA_GPIO];
  390. pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
  391. gpio_key_dev = sysbus_create_simple("gpio-key", -1,
  392. qdev_get_gpio_in(pl061_dev, 3));
  393. /* connect powerdown request */
  394. qemu_register_powerdown_notifier(&sbsa_ref_powerdown_notifier);
  395. }
  396. static void create_ahci(const SBSAMachineState *sms, qemu_irq *pic)
  397. {
  398. hwaddr base = sbsa_ref_memmap[SBSA_AHCI].base;
  399. int irq = sbsa_ref_irqmap[SBSA_AHCI];
  400. DeviceState *dev;
  401. DriveInfo *hd[NUM_SATA_PORTS];
  402. SysbusAHCIState *sysahci;
  403. AHCIState *ahci;
  404. int i;
  405. dev = qdev_create(NULL, "sysbus-ahci");
  406. qdev_prop_set_uint32(dev, "num-ports", NUM_SATA_PORTS);
  407. qdev_init_nofail(dev);
  408. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  409. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[irq]);
  410. sysahci = SYSBUS_AHCI(dev);
  411. ahci = &sysahci->ahci;
  412. ide_drive_get(hd, ARRAY_SIZE(hd));
  413. for (i = 0; i < ahci->ports; i++) {
  414. if (hd[i] == NULL) {
  415. continue;
  416. }
  417. ide_create_drive(&ahci->dev[i].port, 0, hd[i]);
  418. }
  419. }
  420. static void create_ehci(const SBSAMachineState *sms, qemu_irq *pic)
  421. {
  422. hwaddr base = sbsa_ref_memmap[SBSA_EHCI].base;
  423. int irq = sbsa_ref_irqmap[SBSA_EHCI];
  424. sysbus_create_simple("platform-ehci-usb", base, pic[irq]);
  425. }
  426. static void create_smmu(const SBSAMachineState *sms, qemu_irq *pic,
  427. PCIBus *bus)
  428. {
  429. hwaddr base = sbsa_ref_memmap[SBSA_SMMU].base;
  430. int irq = sbsa_ref_irqmap[SBSA_SMMU];
  431. DeviceState *dev;
  432. int i;
  433. dev = qdev_create(NULL, "arm-smmuv3");
  434. object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
  435. &error_abort);
  436. qdev_init_nofail(dev);
  437. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  438. for (i = 0; i < NUM_SMMU_IRQS; i++) {
  439. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
  440. }
  441. }
  442. static void create_pcie(SBSAMachineState *sms, qemu_irq *pic)
  443. {
  444. hwaddr base_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].base;
  445. hwaddr size_ecam = sbsa_ref_memmap[SBSA_PCIE_ECAM].size;
  446. hwaddr base_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].base;
  447. hwaddr size_mmio = sbsa_ref_memmap[SBSA_PCIE_MMIO].size;
  448. hwaddr base_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].base;
  449. hwaddr size_mmio_high = sbsa_ref_memmap[SBSA_PCIE_MMIO_HIGH].size;
  450. hwaddr base_pio = sbsa_ref_memmap[SBSA_PCIE_PIO].base;
  451. int irq = sbsa_ref_irqmap[SBSA_PCIE];
  452. MemoryRegion *mmio_alias, *mmio_alias_high, *mmio_reg;
  453. MemoryRegion *ecam_alias, *ecam_reg;
  454. DeviceState *dev;
  455. PCIHostState *pci;
  456. int i;
  457. dev = qdev_create(NULL, TYPE_GPEX_HOST);
  458. qdev_init_nofail(dev);
  459. /* Map ECAM space */
  460. ecam_alias = g_new0(MemoryRegion, 1);
  461. ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  462. memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
  463. ecam_reg, 0, size_ecam);
  464. memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
  465. /* Map the MMIO space */
  466. mmio_alias = g_new0(MemoryRegion, 1);
  467. mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  468. memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
  469. mmio_reg, base_mmio, size_mmio);
  470. memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
  471. /* Map the MMIO_HIGH space */
  472. mmio_alias_high = g_new0(MemoryRegion, 1);
  473. memory_region_init_alias(mmio_alias_high, OBJECT(dev), "pcie-mmio-high",
  474. mmio_reg, base_mmio_high, size_mmio_high);
  475. memory_region_add_subregion(get_system_memory(), base_mmio_high,
  476. mmio_alias_high);
  477. /* Map IO port space */
  478. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
  479. for (i = 0; i < GPEX_NUM_IRQS; i++) {
  480. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
  481. gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
  482. }
  483. pci = PCI_HOST_BRIDGE(dev);
  484. if (pci->bus) {
  485. for (i = 0; i < nb_nics; i++) {
  486. NICInfo *nd = &nd_table[i];
  487. if (!nd->model) {
  488. nd->model = g_strdup("e1000e");
  489. }
  490. pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
  491. }
  492. }
  493. pci_create_simple(pci->bus, -1, "VGA");
  494. create_smmu(sms, pic, pci->bus);
  495. }
  496. static void *sbsa_ref_dtb(const struct arm_boot_info *binfo, int *fdt_size)
  497. {
  498. const SBSAMachineState *board = container_of(binfo, SBSAMachineState,
  499. bootinfo);
  500. *fdt_size = board->fdt_size;
  501. return board->fdt;
  502. }
  503. static void sbsa_ref_init(MachineState *machine)
  504. {
  505. unsigned int smp_cpus = machine->smp.cpus;
  506. unsigned int max_cpus = machine->smp.max_cpus;
  507. SBSAMachineState *sms = SBSA_MACHINE(machine);
  508. MachineClass *mc = MACHINE_GET_CLASS(machine);
  509. MemoryRegion *sysmem = get_system_memory();
  510. MemoryRegion *secure_sysmem = g_new(MemoryRegion, 1);
  511. MemoryRegion *ram = g_new(MemoryRegion, 1);
  512. bool firmware_loaded;
  513. const CPUArchIdList *possible_cpus;
  514. int n, sbsa_max_cpus;
  515. qemu_irq pic[NUM_IRQS];
  516. if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a57"))) {
  517. error_report("sbsa-ref: CPU type other than the built-in "
  518. "cortex-a57 not supported");
  519. exit(1);
  520. }
  521. if (kvm_enabled()) {
  522. error_report("sbsa-ref: KVM is not supported for this machine");
  523. exit(1);
  524. }
  525. /*
  526. * The Secure view of the world is the same as the NonSecure,
  527. * but with a few extra devices. Create it as a container region
  528. * containing the system memory at low priority; any secure-only
  529. * devices go in at higher priority and take precedence.
  530. */
  531. memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
  532. UINT64_MAX);
  533. memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
  534. firmware_loaded = sbsa_firmware_init(sms, sysmem, secure_sysmem);
  535. if (machine->kernel_filename && firmware_loaded) {
  536. error_report("sbsa-ref: No fw_cfg device on this machine, "
  537. "so -kernel option is not supported when firmware loaded, "
  538. "please load OS from hard disk instead");
  539. exit(1);
  540. }
  541. /*
  542. * This machine has EL3 enabled, external firmware should supply PSCI
  543. * implementation, so the QEMU's internal PSCI is disabled.
  544. */
  545. sms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
  546. sbsa_max_cpus = sbsa_ref_memmap[SBSA_GIC_REDIST].size / GICV3_REDIST_SIZE;
  547. if (max_cpus > sbsa_max_cpus) {
  548. error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
  549. "supported by machine 'sbsa-ref' (%d)",
  550. max_cpus, sbsa_max_cpus);
  551. exit(1);
  552. }
  553. sms->smp_cpus = smp_cpus;
  554. if (machine->ram_size > sbsa_ref_memmap[SBSA_MEM].size) {
  555. error_report("sbsa-ref: cannot model more than %dGB RAM", RAMLIMIT_GB);
  556. exit(1);
  557. }
  558. possible_cpus = mc->possible_cpu_arch_ids(machine);
  559. for (n = 0; n < possible_cpus->len; n++) {
  560. Object *cpuobj;
  561. CPUState *cs;
  562. if (n >= smp_cpus) {
  563. break;
  564. }
  565. cpuobj = object_new(possible_cpus->cpus[n].type);
  566. object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
  567. "mp-affinity", NULL);
  568. cs = CPU(cpuobj);
  569. cs->cpu_index = n;
  570. numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
  571. &error_fatal);
  572. if (object_property_find(cpuobj, "reset-cbar", NULL)) {
  573. object_property_set_int(cpuobj,
  574. sbsa_ref_memmap[SBSA_CPUPERIPHS].base,
  575. "reset-cbar", &error_abort);
  576. }
  577. object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
  578. &error_abort);
  579. object_property_set_link(cpuobj, OBJECT(secure_sysmem),
  580. "secure-memory", &error_abort);
  581. object_property_set_bool(cpuobj, true, "realized", &error_fatal);
  582. object_unref(cpuobj);
  583. }
  584. memory_region_allocate_system_memory(ram, NULL, "sbsa-ref.ram",
  585. machine->ram_size);
  586. memory_region_add_subregion(sysmem, sbsa_ref_memmap[SBSA_MEM].base, ram);
  587. create_fdt(sms);
  588. create_secure_ram(sms, secure_sysmem);
  589. create_gic(sms, pic);
  590. create_uart(sms, pic, SBSA_UART, sysmem, serial_hd(0));
  591. create_uart(sms, pic, SBSA_SECURE_UART, secure_sysmem, serial_hd(1));
  592. /* Second secure UART for RAS and MM from EL0 */
  593. create_uart(sms, pic, SBSA_SECURE_UART_MM, secure_sysmem, serial_hd(2));
  594. create_rtc(sms, pic);
  595. create_gpio(sms, pic);
  596. create_ahci(sms, pic);
  597. create_ehci(sms, pic);
  598. create_pcie(sms, pic);
  599. sms->bootinfo.ram_size = machine->ram_size;
  600. sms->bootinfo.nb_cpus = smp_cpus;
  601. sms->bootinfo.board_id = -1;
  602. sms->bootinfo.loader_start = sbsa_ref_memmap[SBSA_MEM].base;
  603. sms->bootinfo.get_dtb = sbsa_ref_dtb;
  604. sms->bootinfo.firmware_loaded = firmware_loaded;
  605. arm_load_kernel(ARM_CPU(first_cpu), machine, &sms->bootinfo);
  606. }
  607. static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
  608. {
  609. uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
  610. return arm_cpu_mp_affinity(idx, clustersz);
  611. }
  612. static const CPUArchIdList *sbsa_ref_possible_cpu_arch_ids(MachineState *ms)
  613. {
  614. unsigned int max_cpus = ms->smp.max_cpus;
  615. SBSAMachineState *sms = SBSA_MACHINE(ms);
  616. int n;
  617. if (ms->possible_cpus) {
  618. assert(ms->possible_cpus->len == max_cpus);
  619. return ms->possible_cpus;
  620. }
  621. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  622. sizeof(CPUArchId) * max_cpus);
  623. ms->possible_cpus->len = max_cpus;
  624. for (n = 0; n < ms->possible_cpus->len; n++) {
  625. ms->possible_cpus->cpus[n].type = ms->cpu_type;
  626. ms->possible_cpus->cpus[n].arch_id =
  627. sbsa_ref_cpu_mp_affinity(sms, n);
  628. ms->possible_cpus->cpus[n].props.has_thread_id = true;
  629. ms->possible_cpus->cpus[n].props.thread_id = n;
  630. }
  631. return ms->possible_cpus;
  632. }
  633. static CpuInstanceProperties
  634. sbsa_ref_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  635. {
  636. MachineClass *mc = MACHINE_GET_CLASS(ms);
  637. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  638. assert(cpu_index < possible_cpus->len);
  639. return possible_cpus->cpus[cpu_index].props;
  640. }
  641. static int64_t
  642. sbsa_ref_get_default_cpu_node_id(const MachineState *ms, int idx)
  643. {
  644. return idx % ms->numa_state->num_nodes;
  645. }
  646. static void sbsa_ref_instance_init(Object *obj)
  647. {
  648. SBSAMachineState *sms = SBSA_MACHINE(obj);
  649. sbsa_flash_create(sms);
  650. }
  651. static void sbsa_ref_class_init(ObjectClass *oc, void *data)
  652. {
  653. MachineClass *mc = MACHINE_CLASS(oc);
  654. mc->init = sbsa_ref_init;
  655. mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine";
  656. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a57");
  657. mc->max_cpus = 512;
  658. mc->pci_allow_0_address = true;
  659. mc->minimum_page_bits = 12;
  660. mc->block_default_type = IF_IDE;
  661. mc->no_cdrom = 1;
  662. mc->default_ram_size = 1 * GiB;
  663. mc->default_cpus = 4;
  664. mc->possible_cpu_arch_ids = sbsa_ref_possible_cpu_arch_ids;
  665. mc->cpu_index_to_instance_props = sbsa_ref_cpu_index_to_props;
  666. mc->get_default_cpu_node_id = sbsa_ref_get_default_cpu_node_id;
  667. mc->numa_mem_supported = true;
  668. }
  669. static const TypeInfo sbsa_ref_info = {
  670. .name = TYPE_SBSA_MACHINE,
  671. .parent = TYPE_MACHINE,
  672. .instance_init = sbsa_ref_instance_init,
  673. .class_init = sbsa_ref_class_init,
  674. .instance_size = sizeof(SBSAMachineState),
  675. };
  676. static void sbsa_ref_machine_init(void)
  677. {
  678. type_register_static(&sbsa_ref_info);
  679. }
  680. type_init(sbsa_ref_machine_init);