sbsa-ref.c 32 KB

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