vmapple.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * VMApple machine emulation
  3. *
  4. * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. *
  11. * VMApple is the device model that the macOS built-in hypervisor called
  12. * "Virtualization.framework" exposes to Apple Silicon macOS guests. The
  13. * machine model in this file implements the same device model in QEMU, but
  14. * does not use any code from Virtualization.Framework.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qemu/bitops.h"
  18. #include "qemu/datadir.h"
  19. #include "qemu/error-report.h"
  20. #include "qemu/guest-random.h"
  21. #include "qemu/help-texts.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "qemu/option.h"
  25. #include "qemu/units.h"
  26. #include "monitor/qdev.h"
  27. #include "hw/boards.h"
  28. #include "hw/irq.h"
  29. #include "hw/loader.h"
  30. #include "hw/qdev-properties.h"
  31. #include "hw/sysbus.h"
  32. #include "hw/usb.h"
  33. #include "hw/arm/boot.h"
  34. #include "hw/arm/primecell.h"
  35. #include "hw/char/pl011.h"
  36. #include "hw/intc/arm_gic.h"
  37. #include "hw/intc/arm_gicv3_common.h"
  38. #include "hw/misc/pvpanic.h"
  39. #include "hw/pci-host/gpex.h"
  40. #include "hw/usb/hcd-xhci-pci.h"
  41. #include "hw/virtio/virtio-pci.h"
  42. #include "hw/vmapple/vmapple.h"
  43. #include "net/net.h"
  44. #include "qapi/error.h"
  45. #include "qapi/visitor.h"
  46. #include "qapi/qapi-visit-common.h"
  47. #include "qobject/qlist.h"
  48. #include "standard-headers/linux/input.h"
  49. #include "system/hvf.h"
  50. #include "system/reset.h"
  51. #include "system/runstate.h"
  52. #include "system/system.h"
  53. struct VMAppleMachineState {
  54. MachineState parent;
  55. Notifier machine_done;
  56. struct arm_boot_info bootinfo;
  57. const MemMapEntry *memmap;
  58. const int *irqmap;
  59. DeviceState *gic;
  60. DeviceState *cfg;
  61. DeviceState *pvpanic;
  62. Notifier powerdown_notifier;
  63. PCIBus *bus;
  64. MemoryRegion fw_mr;
  65. MemoryRegion ecam_alias;
  66. uint64_t uuid;
  67. };
  68. #define TYPE_VMAPPLE_MACHINE MACHINE_TYPE_NAME("vmapple")
  69. OBJECT_DECLARE_SIMPLE_TYPE(VMAppleMachineState, VMAPPLE_MACHINE)
  70. /* Number of external interrupt lines to configure the GIC with */
  71. #define NUM_IRQS 256
  72. enum {
  73. VMAPPLE_FIRMWARE,
  74. VMAPPLE_CONFIG,
  75. VMAPPLE_MEM,
  76. VMAPPLE_GIC_DIST,
  77. VMAPPLE_GIC_REDIST,
  78. VMAPPLE_UART,
  79. VMAPPLE_RTC,
  80. VMAPPLE_PCIE,
  81. VMAPPLE_PCIE_MMIO,
  82. VMAPPLE_PCIE_ECAM,
  83. VMAPPLE_GPIO,
  84. VMAPPLE_PVPANIC,
  85. VMAPPLE_APV_GFX,
  86. VMAPPLE_APV_IOSFC,
  87. VMAPPLE_AES_1,
  88. VMAPPLE_AES_2,
  89. VMAPPLE_BDOOR,
  90. VMAPPLE_MEMMAP_LAST,
  91. };
  92. static const MemMapEntry memmap[] = {
  93. [VMAPPLE_FIRMWARE] = { 0x00100000, 0x00100000 },
  94. [VMAPPLE_CONFIG] = { 0x00400000, 0x00010000 },
  95. [VMAPPLE_GIC_DIST] = { 0x10000000, 0x00010000 },
  96. [VMAPPLE_GIC_REDIST] = { 0x10010000, 0x00400000 },
  97. [VMAPPLE_UART] = { 0x20010000, 0x00010000 },
  98. [VMAPPLE_RTC] = { 0x20050000, 0x00001000 },
  99. [VMAPPLE_GPIO] = { 0x20060000, 0x00001000 },
  100. [VMAPPLE_PVPANIC] = { 0x20070000, 0x00000002 },
  101. [VMAPPLE_BDOOR] = { 0x30000000, 0x00200000 },
  102. [VMAPPLE_APV_GFX] = { 0x30200000, 0x00010000 },
  103. [VMAPPLE_APV_IOSFC] = { 0x30210000, 0x00010000 },
  104. [VMAPPLE_AES_1] = { 0x30220000, 0x00004000 },
  105. [VMAPPLE_AES_2] = { 0x30230000, 0x00004000 },
  106. [VMAPPLE_PCIE_ECAM] = { 0x40000000, 0x10000000 },
  107. [VMAPPLE_PCIE_MMIO] = { 0x50000000, 0x1fff0000 },
  108. /* Actual RAM size depends on configuration */
  109. [VMAPPLE_MEM] = { 0x70000000ULL, GiB},
  110. };
  111. static const int irqmap[] = {
  112. [VMAPPLE_UART] = 1,
  113. [VMAPPLE_RTC] = 2,
  114. [VMAPPLE_GPIO] = 0x5,
  115. [VMAPPLE_APV_IOSFC] = 0x10,
  116. [VMAPPLE_APV_GFX] = 0x11,
  117. [VMAPPLE_AES_1] = 0x12,
  118. [VMAPPLE_PCIE] = 0x20,
  119. };
  120. #define GPEX_NUM_IRQS 16
  121. static void create_bdif(VMAppleMachineState *vms, MemoryRegion *mem)
  122. {
  123. DeviceState *bdif;
  124. SysBusDevice *bdif_sb;
  125. DriveInfo *di_aux = drive_get(IF_PFLASH, 0, 0);
  126. DriveInfo *di_root = drive_get(IF_PFLASH, 0, 1);
  127. if (!di_aux) {
  128. error_report("No AUX device. Please specify one as pflash drive.");
  129. exit(1);
  130. }
  131. if (!di_root) {
  132. /* Fall back to the first IF_VIRTIO device as root device */
  133. di_root = drive_get(IF_VIRTIO, 0, 0);
  134. }
  135. if (!di_root) {
  136. error_report("No root device. Please specify one as virtio drive.");
  137. exit(1);
  138. }
  139. /* PV backdoor device */
  140. bdif = qdev_new(TYPE_VMAPPLE_BDIF);
  141. bdif_sb = SYS_BUS_DEVICE(bdif);
  142. sysbus_mmio_map(bdif_sb, 0, vms->memmap[VMAPPLE_BDOOR].base);
  143. qdev_prop_set_drive(DEVICE(bdif), "aux", blk_by_legacy_dinfo(di_aux));
  144. qdev_prop_set_drive(DEVICE(bdif), "root", blk_by_legacy_dinfo(di_root));
  145. sysbus_realize_and_unref(bdif_sb, &error_fatal);
  146. }
  147. static void create_pvpanic(VMAppleMachineState *vms, MemoryRegion *mem)
  148. {
  149. SysBusDevice *pvpanic;
  150. vms->pvpanic = qdev_new(TYPE_PVPANIC_MMIO_DEVICE);
  151. pvpanic = SYS_BUS_DEVICE(vms->pvpanic);
  152. sysbus_mmio_map(pvpanic, 0, vms->memmap[VMAPPLE_PVPANIC].base);
  153. sysbus_realize_and_unref(pvpanic, &error_fatal);
  154. }
  155. static bool create_cfg(VMAppleMachineState *vms, MemoryRegion *mem,
  156. Error **errp)
  157. {
  158. ERRP_GUARD();
  159. SysBusDevice *cfg;
  160. MachineState *machine = MACHINE(vms);
  161. uint32_t rnd = 1;
  162. vms->cfg = qdev_new(TYPE_VMAPPLE_CFG);
  163. cfg = SYS_BUS_DEVICE(vms->cfg);
  164. sysbus_mmio_map(cfg, 0, vms->memmap[VMAPPLE_CONFIG].base);
  165. qemu_guest_getrandom_nofail(&rnd, sizeof(rnd));
  166. qdev_prop_set_uint32(vms->cfg, "nr-cpus", machine->smp.cpus);
  167. qdev_prop_set_uint64(vms->cfg, "ecid", vms->uuid);
  168. qdev_prop_set_uint64(vms->cfg, "ram-size", machine->ram_size);
  169. qdev_prop_set_uint32(vms->cfg, "rnd", rnd);
  170. if (!sysbus_realize_and_unref(cfg, errp)) {
  171. error_prepend(errp, "Error creating vmapple cfg device: ");
  172. return false;
  173. }
  174. return true;
  175. }
  176. static void create_gfx(VMAppleMachineState *vms, MemoryRegion *mem)
  177. {
  178. int irq_gfx = vms->irqmap[VMAPPLE_APV_GFX];
  179. int irq_iosfc = vms->irqmap[VMAPPLE_APV_IOSFC];
  180. SysBusDevice *gfx;
  181. gfx = SYS_BUS_DEVICE(qdev_new("apple-gfx-mmio"));
  182. sysbus_mmio_map(gfx, 0, vms->memmap[VMAPPLE_APV_GFX].base);
  183. sysbus_mmio_map(gfx, 1, vms->memmap[VMAPPLE_APV_IOSFC].base);
  184. sysbus_connect_irq(gfx, 0, qdev_get_gpio_in(vms->gic, irq_gfx));
  185. sysbus_connect_irq(gfx, 1, qdev_get_gpio_in(vms->gic, irq_iosfc));
  186. sysbus_realize_and_unref(gfx, &error_fatal);
  187. }
  188. static void create_aes(VMAppleMachineState *vms, MemoryRegion *mem)
  189. {
  190. int irq = vms->irqmap[VMAPPLE_AES_1];
  191. SysBusDevice *aes;
  192. aes = SYS_BUS_DEVICE(qdev_new(TYPE_APPLE_AES));
  193. sysbus_mmio_map(aes, 0, vms->memmap[VMAPPLE_AES_1].base);
  194. sysbus_mmio_map(aes, 1, vms->memmap[VMAPPLE_AES_2].base);
  195. sysbus_connect_irq(aes, 0, qdev_get_gpio_in(vms->gic, irq));
  196. sysbus_realize_and_unref(aes, &error_fatal);
  197. }
  198. static int arm_gic_ppi_index(int cpu_nr, int ppi_index)
  199. {
  200. return NUM_IRQS + cpu_nr * GIC_INTERNAL + ppi_index;
  201. }
  202. static void create_gic(VMAppleMachineState *vms, MemoryRegion *mem)
  203. {
  204. MachineState *ms = MACHINE(vms);
  205. /* We create a standalone GIC */
  206. SysBusDevice *gicbusdev;
  207. QList *redist_region_count;
  208. int i;
  209. unsigned int smp_cpus = ms->smp.cpus;
  210. vms->gic = qdev_new(gicv3_class_name());
  211. qdev_prop_set_uint32(vms->gic, "revision", 3);
  212. qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus);
  213. /*
  214. * Note that the num-irq property counts both internal and external
  215. * interrupts; there are always 32 of the former (mandated by GIC spec).
  216. */
  217. qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32);
  218. uint32_t redist0_capacity =
  219. vms->memmap[VMAPPLE_GIC_REDIST].size / GICV3_REDIST_SIZE;
  220. uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
  221. redist_region_count = qlist_new();
  222. qlist_append_int(redist_region_count, redist0_count);
  223. qdev_prop_set_array(vms->gic, "redist-region-count", redist_region_count);
  224. gicbusdev = SYS_BUS_DEVICE(vms->gic);
  225. sysbus_realize_and_unref(gicbusdev, &error_fatal);
  226. sysbus_mmio_map(gicbusdev, 0, vms->memmap[VMAPPLE_GIC_DIST].base);
  227. sysbus_mmio_map(gicbusdev, 1, vms->memmap[VMAPPLE_GIC_REDIST].base);
  228. /*
  229. * Wire the outputs from each CPU's generic timer and the GICv3
  230. * maintenance interrupt signal to the appropriate GIC PPI inputs,
  231. * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
  232. */
  233. for (i = 0; i < smp_cpus; i++) {
  234. DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
  235. /* Map the virt timer to PPI 27 */
  236. qdev_connect_gpio_out(cpudev, GTIMER_VIRT,
  237. qdev_get_gpio_in(vms->gic,
  238. arm_gic_ppi_index(i, 27)));
  239. /* Map the GIC IRQ and FIQ lines to CPU */
  240. sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
  241. sysbus_connect_irq(gicbusdev, i + smp_cpus,
  242. qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
  243. }
  244. }
  245. static void create_uart(const VMAppleMachineState *vms, int uart,
  246. MemoryRegion *mem, Chardev *chr)
  247. {
  248. hwaddr base = vms->memmap[uart].base;
  249. int irq = vms->irqmap[uart];
  250. DeviceState *dev = qdev_new(TYPE_PL011);
  251. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  252. qdev_prop_set_chr(dev, "chardev", chr);
  253. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  254. memory_region_add_subregion(mem, base,
  255. sysbus_mmio_get_region(s, 0));
  256. sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
  257. }
  258. static void create_rtc(const VMAppleMachineState *vms)
  259. {
  260. hwaddr base = vms->memmap[VMAPPLE_RTC].base;
  261. int irq = vms->irqmap[VMAPPLE_RTC];
  262. sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq));
  263. }
  264. static DeviceState *gpio_key_dev;
  265. static void vmapple_powerdown_req(Notifier *n, void *opaque)
  266. {
  267. /* use gpio Pin 3 for power button event */
  268. qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
  269. }
  270. static void create_gpio_devices(const VMAppleMachineState *vms, int gpio,
  271. MemoryRegion *mem)
  272. {
  273. DeviceState *pl061_dev;
  274. hwaddr base = vms->memmap[gpio].base;
  275. int irq = vms->irqmap[gpio];
  276. SysBusDevice *s;
  277. pl061_dev = qdev_new("pl061");
  278. /* Pull lines down to 0 if not driven by the PL061 */
  279. qdev_prop_set_uint32(pl061_dev, "pullups", 0);
  280. qdev_prop_set_uint32(pl061_dev, "pulldowns", 0xff);
  281. s = SYS_BUS_DEVICE(pl061_dev);
  282. sysbus_realize_and_unref(s, &error_fatal);
  283. memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0));
  284. sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
  285. gpio_key_dev = sysbus_create_simple("gpio-key", -1,
  286. qdev_get_gpio_in(pl061_dev, 3));
  287. }
  288. static void vmapple_firmware_init(VMAppleMachineState *vms,
  289. MemoryRegion *sysmem)
  290. {
  291. hwaddr size = vms->memmap[VMAPPLE_FIRMWARE].size;
  292. hwaddr base = vms->memmap[VMAPPLE_FIRMWARE].base;
  293. const char *bios_name;
  294. int image_size;
  295. char *fname;
  296. bios_name = MACHINE(vms)->firmware;
  297. if (!bios_name) {
  298. error_report("No firmware specified");
  299. exit(1);
  300. }
  301. fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  302. if (!fname) {
  303. error_report("Could not find ROM image '%s'", bios_name);
  304. exit(1);
  305. }
  306. memory_region_init_ram(&vms->fw_mr, NULL, "firmware", size, &error_fatal);
  307. image_size = load_image_mr(fname, &vms->fw_mr);
  308. g_free(fname);
  309. if (image_size < 0) {
  310. error_report("Could not load ROM image '%s'", bios_name);
  311. exit(1);
  312. }
  313. memory_region_add_subregion(get_system_memory(), base, &vms->fw_mr);
  314. }
  315. static void create_pcie(VMAppleMachineState *vms)
  316. {
  317. hwaddr base_mmio = vms->memmap[VMAPPLE_PCIE_MMIO].base;
  318. hwaddr size_mmio = vms->memmap[VMAPPLE_PCIE_MMIO].size;
  319. hwaddr base_ecam = vms->memmap[VMAPPLE_PCIE_ECAM].base;
  320. hwaddr size_ecam = vms->memmap[VMAPPLE_PCIE_ECAM].size;
  321. int irq = vms->irqmap[VMAPPLE_PCIE];
  322. MemoryRegion *mmio_alias;
  323. MemoryRegion *mmio_reg;
  324. MemoryRegion *ecam_reg;
  325. DeviceState *dev;
  326. int i;
  327. PCIHostState *pci;
  328. DeviceState *usb_controller;
  329. USBBus *usb_bus;
  330. dev = qdev_new(TYPE_GPEX_HOST);
  331. qdev_prop_set_uint32(dev, "num-irqs", GPEX_NUM_IRQS);
  332. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  333. /* Map only the first size_ecam bytes of ECAM space */
  334. ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  335. memory_region_init_alias(&vms->ecam_alias, OBJECT(dev), "pcie-ecam",
  336. ecam_reg, 0, size_ecam);
  337. memory_region_add_subregion(get_system_memory(), base_ecam,
  338. &vms->ecam_alias);
  339. /*
  340. * Map the MMIO window from [0x50000000-0x7fff0000] in PCI space into
  341. * system address space at [0x50000000-0x7fff0000].
  342. */
  343. mmio_alias = g_new0(MemoryRegion, 1);
  344. mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  345. memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
  346. mmio_reg, base_mmio, size_mmio);
  347. memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
  348. for (i = 0; i < GPEX_NUM_IRQS; i++) {
  349. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  350. qdev_get_gpio_in(vms->gic, irq + i));
  351. gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
  352. }
  353. pci = PCI_HOST_BRIDGE(dev);
  354. vms->bus = pci->bus;
  355. g_assert(vms->bus);
  356. while ((dev = qemu_create_nic_device("virtio-net-pci", true, NULL))) {
  357. qdev_realize_and_unref(dev, BUS(vms->bus), &error_fatal);
  358. }
  359. if (defaults_enabled()) {
  360. usb_controller = qdev_new(TYPE_QEMU_XHCI);
  361. qdev_realize_and_unref(usb_controller, BUS(pci->bus), &error_fatal);
  362. usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
  363. &error_fatal));
  364. usb_create_simple(usb_bus, "usb-kbd");
  365. usb_create_simple(usb_bus, "usb-tablet");
  366. }
  367. }
  368. static void vmapple_reset(void *opaque)
  369. {
  370. VMAppleMachineState *vms = opaque;
  371. hwaddr base = vms->memmap[VMAPPLE_FIRMWARE].base;
  372. cpu_set_pc(first_cpu, base);
  373. }
  374. static void mach_vmapple_init(MachineState *machine)
  375. {
  376. VMAppleMachineState *vms = VMAPPLE_MACHINE(machine);
  377. MachineClass *mc = MACHINE_GET_CLASS(machine);
  378. const CPUArchIdList *possible_cpus;
  379. MemoryRegion *sysmem = get_system_memory();
  380. int n;
  381. unsigned int smp_cpus = machine->smp.cpus;
  382. unsigned int max_cpus = machine->smp.max_cpus;
  383. vms->memmap = memmap;
  384. machine->usb = true;
  385. possible_cpus = mc->possible_cpu_arch_ids(machine);
  386. assert(possible_cpus->len == max_cpus);
  387. for (n = 0; n < possible_cpus->len; n++) {
  388. Object *cpu;
  389. CPUState *cs;
  390. if (n >= smp_cpus) {
  391. break;
  392. }
  393. cpu = object_new(possible_cpus->cpus[n].type);
  394. object_property_set_int(cpu, "mp-affinity",
  395. possible_cpus->cpus[n].arch_id, &error_fatal);
  396. cs = CPU(cpu);
  397. cs->cpu_index = n;
  398. numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpu),
  399. &error_fatal);
  400. if (object_property_find(cpu, "has_el3")) {
  401. object_property_set_bool(cpu, "has_el3", false, &error_fatal);
  402. }
  403. if (object_property_find(cpu, "has_el2")) {
  404. object_property_set_bool(cpu, "has_el2", false, &error_fatal);
  405. }
  406. object_property_set_int(cpu, "psci-conduit", QEMU_PSCI_CONDUIT_HVC,
  407. &error_fatal);
  408. /* Secondary CPUs start in PSCI powered-down state */
  409. if (n > 0) {
  410. object_property_set_bool(cpu, "start-powered-off", true,
  411. &error_fatal);
  412. }
  413. object_property_set_link(cpu, "memory", OBJECT(sysmem), &error_abort);
  414. qdev_realize(DEVICE(cpu), NULL, &error_fatal);
  415. object_unref(cpu);
  416. }
  417. memory_region_add_subregion(sysmem, vms->memmap[VMAPPLE_MEM].base,
  418. machine->ram);
  419. create_gic(vms, sysmem);
  420. create_bdif(vms, sysmem);
  421. create_pvpanic(vms, sysmem);
  422. create_aes(vms, sysmem);
  423. create_gfx(vms, sysmem);
  424. create_uart(vms, VMAPPLE_UART, sysmem, serial_hd(0));
  425. create_rtc(vms);
  426. create_pcie(vms);
  427. create_gpio_devices(vms, VMAPPLE_GPIO, sysmem);
  428. vmapple_firmware_init(vms, sysmem);
  429. create_cfg(vms, sysmem, &error_fatal);
  430. /* connect powerdown request */
  431. vms->powerdown_notifier.notify = vmapple_powerdown_req;
  432. qemu_register_powerdown_notifier(&vms->powerdown_notifier);
  433. vms->bootinfo.ram_size = machine->ram_size;
  434. vms->bootinfo.board_id = -1;
  435. vms->bootinfo.loader_start = vms->memmap[VMAPPLE_MEM].base;
  436. vms->bootinfo.skip_dtb_autoload = true;
  437. vms->bootinfo.firmware_loaded = true;
  438. arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
  439. qemu_register_reset(vmapple_reset, vms);
  440. }
  441. static CpuInstanceProperties
  442. vmapple_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  443. {
  444. MachineClass *mc = MACHINE_GET_CLASS(ms);
  445. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  446. assert(cpu_index < possible_cpus->len);
  447. return possible_cpus->cpus[cpu_index].props;
  448. }
  449. static int64_t vmapple_get_default_cpu_node_id(const MachineState *ms, int idx)
  450. {
  451. return idx % ms->numa_state->num_nodes;
  452. }
  453. static const CPUArchIdList *vmapple_possible_cpu_arch_ids(MachineState *ms)
  454. {
  455. int n;
  456. unsigned int max_cpus = ms->smp.max_cpus;
  457. if (ms->possible_cpus) {
  458. assert(ms->possible_cpus->len == max_cpus);
  459. return ms->possible_cpus;
  460. }
  461. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  462. sizeof(CPUArchId) * max_cpus);
  463. ms->possible_cpus->len = max_cpus;
  464. for (n = 0; n < ms->possible_cpus->len; n++) {
  465. ms->possible_cpus->cpus[n].type = ms->cpu_type;
  466. ms->possible_cpus->cpus[n].arch_id =
  467. arm_build_mp_affinity(n, GICV3_TARGETLIST_BITS);
  468. ms->possible_cpus->cpus[n].props.has_thread_id = true;
  469. ms->possible_cpus->cpus[n].props.thread_id = n;
  470. }
  471. return ms->possible_cpus;
  472. }
  473. static GlobalProperty vmapple_compat_defaults[] = {
  474. { TYPE_VIRTIO_PCI, "disable-legacy", "on" },
  475. /*
  476. * macOS XHCI driver attempts to schedule events onto even rings 1 & 2
  477. * even when (as here) there is no MSI(-X) support. Disabling interrupter
  478. * mapping in the XHCI controller works around the problem.
  479. */
  480. { TYPE_XHCI_PCI, "conditional-intr-mapping", "on" },
  481. };
  482. static void vmapple_machine_class_init(ObjectClass *oc, void *data)
  483. {
  484. MachineClass *mc = MACHINE_CLASS(oc);
  485. mc->init = mach_vmapple_init;
  486. mc->max_cpus = 32;
  487. mc->block_default_type = IF_VIRTIO;
  488. mc->no_cdrom = 1;
  489. mc->pci_allow_0_address = true;
  490. mc->minimum_page_bits = 12;
  491. mc->possible_cpu_arch_ids = vmapple_possible_cpu_arch_ids;
  492. mc->cpu_index_to_instance_props = vmapple_cpu_index_to_props;
  493. mc->default_cpu_type = ARM_CPU_TYPE_NAME("host");
  494. mc->get_default_cpu_node_id = vmapple_get_default_cpu_node_id;
  495. mc->default_ram_id = "mach-vmapple.ram";
  496. mc->desc = "Apple aarch64 Virtual Machine";
  497. compat_props_add(mc->compat_props, vmapple_compat_defaults,
  498. G_N_ELEMENTS(vmapple_compat_defaults));
  499. }
  500. static void vmapple_instance_init(Object *obj)
  501. {
  502. VMAppleMachineState *vms = VMAPPLE_MACHINE(obj);
  503. vms->irqmap = irqmap;
  504. object_property_add_uint64_ptr(obj, "uuid", &vms->uuid,
  505. OBJ_PROP_FLAG_READWRITE);
  506. object_property_set_description(obj, "uuid", "Machine UUID (SDOM)");
  507. }
  508. static const TypeInfo vmapple_machine_info = {
  509. .name = TYPE_VMAPPLE_MACHINE,
  510. .parent = TYPE_MACHINE,
  511. .instance_size = sizeof(VMAppleMachineState),
  512. .class_init = vmapple_machine_class_init,
  513. .instance_init = vmapple_instance_init,
  514. };
  515. static void machvmapple_machine_init(void)
  516. {
  517. type_register_static(&vmapple_machine_info);
  518. }
  519. type_init(machvmapple_machine_init);