2
0

virt.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. * ARM mach-virt emulation
  3. *
  4. * Copyright (c) 2013 Linaro Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2 or later, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Emulate a virtual board which works by passing Linux all the information
  19. * it needs about what devices are present via the device tree.
  20. * There are some restrictions about what we can do here:
  21. * + we can only present devices whose Linux drivers will work based
  22. * purely on the device tree with no platform data at all
  23. * + we want to present a very stripped-down minimalist platform,
  24. * both because this reduces the security attack surface from the guest
  25. * and also because it reduces our exposure to being broken when
  26. * the kernel updates its device tree bindings and requires further
  27. * information in a device binding that we aren't providing.
  28. * This is essentially the same approach kvmtool uses.
  29. */
  30. #include "qemu/osdep.h"
  31. #include "qemu-common.h"
  32. #include "qemu/units.h"
  33. #include "qemu/option.h"
  34. #include "qapi/error.h"
  35. #include "hw/sysbus.h"
  36. #include "hw/boards.h"
  37. #include "hw/arm/boot.h"
  38. #include "hw/arm/primecell.h"
  39. #include "hw/arm/virt.h"
  40. #include "hw/block/flash.h"
  41. #include "hw/vfio/vfio-calxeda-xgmac.h"
  42. #include "hw/vfio/vfio-amd-xgbe.h"
  43. #include "hw/display/ramfb.h"
  44. #include "net/net.h"
  45. #include "sysemu/device_tree.h"
  46. #include "sysemu/numa.h"
  47. #include "sysemu/runstate.h"
  48. #include "sysemu/sysemu.h"
  49. #include "sysemu/kvm.h"
  50. #include "hw/loader.h"
  51. #include "exec/address-spaces.h"
  52. #include "qemu/bitops.h"
  53. #include "qemu/error-report.h"
  54. #include "qemu/module.h"
  55. #include "hw/pci-host/gpex.h"
  56. #include "hw/arm/sysbus-fdt.h"
  57. #include "hw/platform-bus.h"
  58. #include "hw/qdev-properties.h"
  59. #include "hw/arm/fdt.h"
  60. #include "hw/intc/arm_gic.h"
  61. #include "hw/intc/arm_gicv3_common.h"
  62. #include "hw/irq.h"
  63. #include "kvm_arm.h"
  64. #include "hw/firmware/smbios.h"
  65. #include "qapi/visitor.h"
  66. #include "standard-headers/linux/input.h"
  67. #include "hw/arm/smmuv3.h"
  68. #include "hw/acpi/acpi.h"
  69. #include "target/arm/internals.h"
  70. #include "hw/mem/pc-dimm.h"
  71. #include "hw/mem/nvdimm.h"
  72. #include "hw/acpi/generic_event_device.h"
  73. #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
  74. static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
  75. void *data) \
  76. { \
  77. MachineClass *mc = MACHINE_CLASS(oc); \
  78. virt_machine_##major##_##minor##_options(mc); \
  79. mc->desc = "QEMU " # major "." # minor " ARM Virtual Machine"; \
  80. if (latest) { \
  81. mc->alias = "virt"; \
  82. } \
  83. } \
  84. static const TypeInfo machvirt_##major##_##minor##_info = { \
  85. .name = MACHINE_TYPE_NAME("virt-" # major "." # minor), \
  86. .parent = TYPE_VIRT_MACHINE, \
  87. .class_init = virt_##major##_##minor##_class_init, \
  88. }; \
  89. static void machvirt_machine_##major##_##minor##_init(void) \
  90. { \
  91. type_register_static(&machvirt_##major##_##minor##_info); \
  92. } \
  93. type_init(machvirt_machine_##major##_##minor##_init);
  94. #define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \
  95. DEFINE_VIRT_MACHINE_LATEST(major, minor, true)
  96. #define DEFINE_VIRT_MACHINE(major, minor) \
  97. DEFINE_VIRT_MACHINE_LATEST(major, minor, false)
  98. /* Number of external interrupt lines to configure the GIC with */
  99. #define NUM_IRQS 256
  100. #define PLATFORM_BUS_NUM_IRQS 64
  101. /* Legacy RAM limit in GB (< version 4.0) */
  102. #define LEGACY_RAMLIMIT_GB 255
  103. #define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB)
  104. /* Addresses and sizes of our components.
  105. * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
  106. * 128MB..256MB is used for miscellaneous device I/O.
  107. * 256MB..1GB is reserved for possible future PCI support (ie where the
  108. * PCI memory window will go if we add a PCI host controller).
  109. * 1GB and up is RAM (which may happily spill over into the
  110. * high memory region beyond 4GB).
  111. * This represents a compromise between how much RAM can be given to
  112. * a 32 bit VM and leaving space for expansion and in particular for PCI.
  113. * Note that devices should generally be placed at multiples of 0x10000,
  114. * to accommodate guests using 64K pages.
  115. */
  116. static const MemMapEntry base_memmap[] = {
  117. /* Space up to 0x8000000 is reserved for a boot ROM */
  118. [VIRT_FLASH] = { 0, 0x08000000 },
  119. [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
  120. /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
  121. [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
  122. [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
  123. [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
  124. [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 },
  125. [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 },
  126. /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
  127. [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 },
  128. /* This redistributor space allows up to 2*64kB*123 CPUs */
  129. [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 },
  130. [VIRT_UART] = { 0x09000000, 0x00001000 },
  131. [VIRT_RTC] = { 0x09010000, 0x00001000 },
  132. [VIRT_FW_CFG] = { 0x09020000, 0x00000018 },
  133. [VIRT_GPIO] = { 0x09030000, 0x00001000 },
  134. [VIRT_SECURE_UART] = { 0x09040000, 0x00001000 },
  135. [VIRT_SMMU] = { 0x09050000, 0x00020000 },
  136. [VIRT_PCDIMM_ACPI] = { 0x09070000, MEMORY_HOTPLUG_IO_LEN },
  137. [VIRT_ACPI_GED] = { 0x09080000, ACPI_GED_EVT_SEL_LEN },
  138. [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
  139. /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
  140. [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
  141. [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 },
  142. [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
  143. [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
  144. [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
  145. /* Actual RAM size depends on initial RAM and device memory settings */
  146. [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES },
  147. };
  148. /*
  149. * Highmem IO Regions: This memory map is floating, located after the RAM.
  150. * Each MemMapEntry base (GPA) will be dynamically computed, depending on the
  151. * top of the RAM, so that its base get the same alignment as the size,
  152. * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is
  153. * less than 256GiB of RAM, the floating area starts at the 256GiB mark.
  154. * Note the extended_memmap is sized so that it eventually also includes the
  155. * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last
  156. * index of base_memmap).
  157. */
  158. static MemMapEntry extended_memmap[] = {
  159. /* Additional 64 MB redist region (can contain up to 512 redistributors) */
  160. [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB },
  161. [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB },
  162. /* Second PCIe window */
  163. [VIRT_HIGH_PCIE_MMIO] = { 0x0, 512 * GiB },
  164. };
  165. static const int a15irqmap[] = {
  166. [VIRT_UART] = 1,
  167. [VIRT_RTC] = 2,
  168. [VIRT_PCIE] = 3, /* ... to 6 */
  169. [VIRT_GPIO] = 7,
  170. [VIRT_SECURE_UART] = 8,
  171. [VIRT_ACPI_GED] = 9,
  172. [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
  173. [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
  174. [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
  175. [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
  176. };
  177. static const char *valid_cpus[] = {
  178. ARM_CPU_TYPE_NAME("cortex-a7"),
  179. ARM_CPU_TYPE_NAME("cortex-a15"),
  180. ARM_CPU_TYPE_NAME("cortex-a53"),
  181. ARM_CPU_TYPE_NAME("cortex-a57"),
  182. ARM_CPU_TYPE_NAME("cortex-a72"),
  183. ARM_CPU_TYPE_NAME("host"),
  184. ARM_CPU_TYPE_NAME("max"),
  185. };
  186. static bool cpu_type_valid(const char *cpu)
  187. {
  188. int i;
  189. for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
  190. if (strcmp(cpu, valid_cpus[i]) == 0) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. static void create_fdt(VirtMachineState *vms)
  197. {
  198. MachineState *ms = MACHINE(vms);
  199. int nb_numa_nodes = ms->numa_state->num_nodes;
  200. void *fdt = create_device_tree(&vms->fdt_size);
  201. if (!fdt) {
  202. error_report("create_device_tree() failed");
  203. exit(1);
  204. }
  205. vms->fdt = fdt;
  206. /* Header */
  207. qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
  208. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  209. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  210. /* /chosen must exist for load_dtb to fill in necessary properties later */
  211. qemu_fdt_add_subnode(fdt, "/chosen");
  212. /* Clock node, for the benefit of the UART. The kernel device tree
  213. * binding documentation claims the PL011 node clock properties are
  214. * optional but in practice if you omit them the kernel refuses to
  215. * probe for the device.
  216. */
  217. vms->clock_phandle = qemu_fdt_alloc_phandle(fdt);
  218. qemu_fdt_add_subnode(fdt, "/apb-pclk");
  219. qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
  220. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
  221. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
  222. qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
  223. "clk24mhz");
  224. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle);
  225. if (nb_numa_nodes > 0 && ms->numa_state->have_numa_distance) {
  226. int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
  227. uint32_t *matrix = g_malloc0(size);
  228. int idx, i, j;
  229. for (i = 0; i < nb_numa_nodes; i++) {
  230. for (j = 0; j < nb_numa_nodes; j++) {
  231. idx = (i * nb_numa_nodes + j) * 3;
  232. matrix[idx + 0] = cpu_to_be32(i);
  233. matrix[idx + 1] = cpu_to_be32(j);
  234. matrix[idx + 2] =
  235. cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
  236. }
  237. }
  238. qemu_fdt_add_subnode(fdt, "/distance-map");
  239. qemu_fdt_setprop_string(fdt, "/distance-map", "compatible",
  240. "numa-distance-map-v1");
  241. qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
  242. matrix, size);
  243. g_free(matrix);
  244. }
  245. }
  246. static void fdt_add_timer_nodes(const VirtMachineState *vms)
  247. {
  248. /* On real hardware these interrupts are level-triggered.
  249. * On KVM they were edge-triggered before host kernel version 4.4,
  250. * and level-triggered afterwards.
  251. * On emulated QEMU they are level-triggered.
  252. *
  253. * Getting the DTB info about them wrong is awkward for some
  254. * guest kernels:
  255. * pre-4.8 ignore the DT and leave the interrupt configured
  256. * with whatever the GIC reset value (or the bootloader) left it at
  257. * 4.8 before rc6 honour the incorrect data by programming it back
  258. * into the GIC, causing problems
  259. * 4.8rc6 and later ignore the DT and always write "level triggered"
  260. * into the GIC
  261. *
  262. * For backwards-compatibility, virt-2.8 and earlier will continue
  263. * to say these are edge-triggered, but later machines will report
  264. * the correct information.
  265. */
  266. ARMCPU *armcpu;
  267. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  268. uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  269. if (vmc->claim_edge_triggered_timers) {
  270. irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
  271. }
  272. if (vms->gic_version == 2) {
  273. irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
  274. GIC_FDT_IRQ_PPI_CPU_WIDTH,
  275. (1 << vms->smp_cpus) - 1);
  276. }
  277. qemu_fdt_add_subnode(vms->fdt, "/timer");
  278. armcpu = ARM_CPU(qemu_get_cpu(0));
  279. if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
  280. const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
  281. qemu_fdt_setprop(vms->fdt, "/timer", "compatible",
  282. compat, sizeof(compat));
  283. } else {
  284. qemu_fdt_setprop_string(vms->fdt, "/timer", "compatible",
  285. "arm,armv7-timer");
  286. }
  287. qemu_fdt_setprop(vms->fdt, "/timer", "always-on", NULL, 0);
  288. qemu_fdt_setprop_cells(vms->fdt, "/timer", "interrupts",
  289. GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_S_EL1_IRQ, irqflags,
  290. GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL1_IRQ, irqflags,
  291. GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_VIRT_IRQ, irqflags,
  292. GIC_FDT_IRQ_TYPE_PPI, ARCH_TIMER_NS_EL2_IRQ, irqflags);
  293. }
  294. static void fdt_add_cpu_nodes(const VirtMachineState *vms)
  295. {
  296. int cpu;
  297. int addr_cells = 1;
  298. const MachineState *ms = MACHINE(vms);
  299. /*
  300. * From Documentation/devicetree/bindings/arm/cpus.txt
  301. * On ARM v8 64-bit systems value should be set to 2,
  302. * that corresponds to the MPIDR_EL1 register size.
  303. * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
  304. * in the system, #address-cells can be set to 1, since
  305. * MPIDR_EL1[63:32] bits are not used for CPUs
  306. * identification.
  307. *
  308. * Here we actually don't know whether our system is 32- or 64-bit one.
  309. * The simplest way to go is to examine affinity IDs of all our CPUs. If
  310. * at least one of them has Aff3 populated, we set #address-cells to 2.
  311. */
  312. for (cpu = 0; cpu < vms->smp_cpus; cpu++) {
  313. ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
  314. if (armcpu->mp_affinity & ARM_AFF3_MASK) {
  315. addr_cells = 2;
  316. break;
  317. }
  318. }
  319. qemu_fdt_add_subnode(vms->fdt, "/cpus");
  320. qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#address-cells", addr_cells);
  321. qemu_fdt_setprop_cell(vms->fdt, "/cpus", "#size-cells", 0x0);
  322. for (cpu = vms->smp_cpus - 1; cpu >= 0; cpu--) {
  323. char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
  324. ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
  325. CPUState *cs = CPU(armcpu);
  326. qemu_fdt_add_subnode(vms->fdt, nodename);
  327. qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "cpu");
  328. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
  329. armcpu->dtb_compatible);
  330. if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED
  331. && vms->smp_cpus > 1) {
  332. qemu_fdt_setprop_string(vms->fdt, nodename,
  333. "enable-method", "psci");
  334. }
  335. if (addr_cells == 2) {
  336. qemu_fdt_setprop_u64(vms->fdt, nodename, "reg",
  337. armcpu->mp_affinity);
  338. } else {
  339. qemu_fdt_setprop_cell(vms->fdt, nodename, "reg",
  340. armcpu->mp_affinity);
  341. }
  342. if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
  343. qemu_fdt_setprop_cell(vms->fdt, nodename, "numa-node-id",
  344. ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
  345. }
  346. g_free(nodename);
  347. }
  348. }
  349. static void fdt_add_its_gic_node(VirtMachineState *vms)
  350. {
  351. char *nodename;
  352. vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
  353. nodename = g_strdup_printf("/intc/its@%" PRIx64,
  354. vms->memmap[VIRT_GIC_ITS].base);
  355. qemu_fdt_add_subnode(vms->fdt, nodename);
  356. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
  357. "arm,gic-v3-its");
  358. qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
  359. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  360. 2, vms->memmap[VIRT_GIC_ITS].base,
  361. 2, vms->memmap[VIRT_GIC_ITS].size);
  362. qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
  363. g_free(nodename);
  364. }
  365. static void fdt_add_v2m_gic_node(VirtMachineState *vms)
  366. {
  367. char *nodename;
  368. nodename = g_strdup_printf("/intc/v2m@%" PRIx64,
  369. vms->memmap[VIRT_GIC_V2M].base);
  370. vms->msi_phandle = qemu_fdt_alloc_phandle(vms->fdt);
  371. qemu_fdt_add_subnode(vms->fdt, nodename);
  372. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
  373. "arm,gic-v2m-frame");
  374. qemu_fdt_setprop(vms->fdt, nodename, "msi-controller", NULL, 0);
  375. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  376. 2, vms->memmap[VIRT_GIC_V2M].base,
  377. 2, vms->memmap[VIRT_GIC_V2M].size);
  378. qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->msi_phandle);
  379. g_free(nodename);
  380. }
  381. static void fdt_add_gic_node(VirtMachineState *vms)
  382. {
  383. char *nodename;
  384. vms->gic_phandle = qemu_fdt_alloc_phandle(vms->fdt);
  385. qemu_fdt_setprop_cell(vms->fdt, "/", "interrupt-parent", vms->gic_phandle);
  386. nodename = g_strdup_printf("/intc@%" PRIx64,
  387. vms->memmap[VIRT_GIC_DIST].base);
  388. qemu_fdt_add_subnode(vms->fdt, nodename);
  389. qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 3);
  390. qemu_fdt_setprop(vms->fdt, nodename, "interrupt-controller", NULL, 0);
  391. qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 0x2);
  392. qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 0x2);
  393. qemu_fdt_setprop(vms->fdt, nodename, "ranges", NULL, 0);
  394. if (vms->gic_version == 3) {
  395. int nb_redist_regions = virt_gicv3_redist_region_count(vms);
  396. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
  397. "arm,gic-v3");
  398. qemu_fdt_setprop_cell(vms->fdt, nodename,
  399. "#redistributor-regions", nb_redist_regions);
  400. if (nb_redist_regions == 1) {
  401. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  402. 2, vms->memmap[VIRT_GIC_DIST].base,
  403. 2, vms->memmap[VIRT_GIC_DIST].size,
  404. 2, vms->memmap[VIRT_GIC_REDIST].base,
  405. 2, vms->memmap[VIRT_GIC_REDIST].size);
  406. } else {
  407. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  408. 2, vms->memmap[VIRT_GIC_DIST].base,
  409. 2, vms->memmap[VIRT_GIC_DIST].size,
  410. 2, vms->memmap[VIRT_GIC_REDIST].base,
  411. 2, vms->memmap[VIRT_GIC_REDIST].size,
  412. 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base,
  413. 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size);
  414. }
  415. if (vms->virt) {
  416. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  417. GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
  418. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  419. }
  420. } else {
  421. /* 'cortex-a15-gic' means 'GIC v2' */
  422. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible",
  423. "arm,cortex-a15-gic");
  424. if (!vms->virt) {
  425. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  426. 2, vms->memmap[VIRT_GIC_DIST].base,
  427. 2, vms->memmap[VIRT_GIC_DIST].size,
  428. 2, vms->memmap[VIRT_GIC_CPU].base,
  429. 2, vms->memmap[VIRT_GIC_CPU].size);
  430. } else {
  431. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  432. 2, vms->memmap[VIRT_GIC_DIST].base,
  433. 2, vms->memmap[VIRT_GIC_DIST].size,
  434. 2, vms->memmap[VIRT_GIC_CPU].base,
  435. 2, vms->memmap[VIRT_GIC_CPU].size,
  436. 2, vms->memmap[VIRT_GIC_HYP].base,
  437. 2, vms->memmap[VIRT_GIC_HYP].size,
  438. 2, vms->memmap[VIRT_GIC_VCPU].base,
  439. 2, vms->memmap[VIRT_GIC_VCPU].size);
  440. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  441. GIC_FDT_IRQ_TYPE_PPI, ARCH_GIC_MAINT_IRQ,
  442. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  443. }
  444. }
  445. qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", vms->gic_phandle);
  446. g_free(nodename);
  447. }
  448. static void fdt_add_pmu_nodes(const VirtMachineState *vms)
  449. {
  450. CPUState *cpu;
  451. ARMCPU *armcpu;
  452. uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  453. CPU_FOREACH(cpu) {
  454. armcpu = ARM_CPU(cpu);
  455. if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
  456. return;
  457. }
  458. if (kvm_enabled()) {
  459. if (kvm_irqchip_in_kernel()) {
  460. kvm_arm_pmu_set_irq(cpu, PPI(VIRTUAL_PMU_IRQ));
  461. }
  462. kvm_arm_pmu_init(cpu);
  463. }
  464. }
  465. if (vms->gic_version == 2) {
  466. irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
  467. GIC_FDT_IRQ_PPI_CPU_WIDTH,
  468. (1 << vms->smp_cpus) - 1);
  469. }
  470. armcpu = ARM_CPU(qemu_get_cpu(0));
  471. qemu_fdt_add_subnode(vms->fdt, "/pmu");
  472. if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
  473. const char compat[] = "arm,armv8-pmuv3";
  474. qemu_fdt_setprop(vms->fdt, "/pmu", "compatible",
  475. compat, sizeof(compat));
  476. qemu_fdt_setprop_cells(vms->fdt, "/pmu", "interrupts",
  477. GIC_FDT_IRQ_TYPE_PPI, VIRTUAL_PMU_IRQ, irqflags);
  478. }
  479. }
  480. static inline DeviceState *create_acpi_ged(VirtMachineState *vms, qemu_irq *pic)
  481. {
  482. DeviceState *dev;
  483. MachineState *ms = MACHINE(vms);
  484. int irq = vms->irqmap[VIRT_ACPI_GED];
  485. uint32_t event = ACPI_GED_PWR_DOWN_EVT;
  486. if (ms->ram_slots) {
  487. event |= ACPI_GED_MEM_HOTPLUG_EVT;
  488. }
  489. dev = qdev_create(NULL, TYPE_ACPI_GED);
  490. qdev_prop_set_uint32(dev, "ged-event", event);
  491. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base);
  492. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base);
  493. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, pic[irq]);
  494. qdev_init_nofail(dev);
  495. return dev;
  496. }
  497. static void create_its(VirtMachineState *vms, DeviceState *gicdev)
  498. {
  499. const char *itsclass = its_class_name();
  500. DeviceState *dev;
  501. if (!itsclass) {
  502. /* Do nothing if not supported */
  503. return;
  504. }
  505. dev = qdev_create(NULL, itsclass);
  506. object_property_set_link(OBJECT(dev), OBJECT(gicdev), "parent-gicv3",
  507. &error_abort);
  508. qdev_init_nofail(dev);
  509. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base);
  510. fdt_add_its_gic_node(vms);
  511. }
  512. static void create_v2m(VirtMachineState *vms, qemu_irq *pic)
  513. {
  514. int i;
  515. int irq = vms->irqmap[VIRT_GIC_V2M];
  516. DeviceState *dev;
  517. dev = qdev_create(NULL, "arm-gicv2m");
  518. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
  519. qdev_prop_set_uint32(dev, "base-spi", irq);
  520. qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
  521. qdev_init_nofail(dev);
  522. for (i = 0; i < NUM_GICV2M_SPIS; i++) {
  523. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
  524. }
  525. fdt_add_v2m_gic_node(vms);
  526. }
  527. static void create_gic(VirtMachineState *vms, qemu_irq *pic)
  528. {
  529. MachineState *ms = MACHINE(vms);
  530. /* We create a standalone GIC */
  531. DeviceState *gicdev;
  532. SysBusDevice *gicbusdev;
  533. const char *gictype;
  534. int type = vms->gic_version, i;
  535. unsigned int smp_cpus = ms->smp.cpus;
  536. uint32_t nb_redist_regions = 0;
  537. gictype = (type == 3) ? gicv3_class_name() : gic_class_name();
  538. gicdev = qdev_create(NULL, gictype);
  539. qdev_prop_set_uint32(gicdev, "revision", type);
  540. qdev_prop_set_uint32(gicdev, "num-cpu", smp_cpus);
  541. /* Note that the num-irq property counts both internal and external
  542. * interrupts; there are always 32 of the former (mandated by GIC spec).
  543. */
  544. qdev_prop_set_uint32(gicdev, "num-irq", NUM_IRQS + 32);
  545. if (!kvm_irqchip_in_kernel()) {
  546. qdev_prop_set_bit(gicdev, "has-security-extensions", vms->secure);
  547. }
  548. if (type == 3) {
  549. uint32_t redist0_capacity =
  550. vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
  551. uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
  552. nb_redist_regions = virt_gicv3_redist_region_count(vms);
  553. qdev_prop_set_uint32(gicdev, "len-redist-region-count",
  554. nb_redist_regions);
  555. qdev_prop_set_uint32(gicdev, "redist-region-count[0]", redist0_count);
  556. if (nb_redist_regions == 2) {
  557. uint32_t redist1_capacity =
  558. vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
  559. qdev_prop_set_uint32(gicdev, "redist-region-count[1]",
  560. MIN(smp_cpus - redist0_count, redist1_capacity));
  561. }
  562. } else {
  563. if (!kvm_irqchip_in_kernel()) {
  564. qdev_prop_set_bit(gicdev, "has-virtualization-extensions",
  565. vms->virt);
  566. }
  567. }
  568. qdev_init_nofail(gicdev);
  569. gicbusdev = SYS_BUS_DEVICE(gicdev);
  570. sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base);
  571. if (type == 3) {
  572. sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base);
  573. if (nb_redist_regions == 2) {
  574. sysbus_mmio_map(gicbusdev, 2,
  575. vms->memmap[VIRT_HIGH_GIC_REDIST2].base);
  576. }
  577. } else {
  578. sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base);
  579. if (vms->virt) {
  580. sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base);
  581. sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base);
  582. }
  583. }
  584. /* Wire the outputs from each CPU's generic timer and the GICv3
  585. * maintenance interrupt signal to the appropriate GIC PPI inputs,
  586. * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
  587. */
  588. for (i = 0; i < smp_cpus; i++) {
  589. DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
  590. int ppibase = NUM_IRQS + i * GIC_INTERNAL + GIC_NR_SGIS;
  591. int irq;
  592. /* Mapping from the output timer irq lines from the CPU to the
  593. * GIC PPI inputs we use for the virt board.
  594. */
  595. const int timer_irq[] = {
  596. [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
  597. [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
  598. [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
  599. [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
  600. };
  601. for (irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
  602. qdev_connect_gpio_out(cpudev, irq,
  603. qdev_get_gpio_in(gicdev,
  604. ppibase + timer_irq[irq]));
  605. }
  606. if (type == 3) {
  607. qemu_irq irq = qdev_get_gpio_in(gicdev,
  608. ppibase + ARCH_GIC_MAINT_IRQ);
  609. qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
  610. 0, irq);
  611. } else if (vms->virt) {
  612. qemu_irq irq = qdev_get_gpio_in(gicdev,
  613. ppibase + ARCH_GIC_MAINT_IRQ);
  614. sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
  615. }
  616. qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
  617. qdev_get_gpio_in(gicdev, ppibase
  618. + VIRTUAL_PMU_IRQ));
  619. sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
  620. sysbus_connect_irq(gicbusdev, i + smp_cpus,
  621. qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
  622. sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
  623. qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
  624. sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
  625. qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
  626. }
  627. for (i = 0; i < NUM_IRQS; i++) {
  628. pic[i] = qdev_get_gpio_in(gicdev, i);
  629. }
  630. fdt_add_gic_node(vms);
  631. if (type == 3 && vms->its) {
  632. create_its(vms, gicdev);
  633. } else if (type == 2) {
  634. create_v2m(vms, pic);
  635. }
  636. }
  637. static void create_uart(const VirtMachineState *vms, qemu_irq *pic, int uart,
  638. MemoryRegion *mem, Chardev *chr)
  639. {
  640. char *nodename;
  641. hwaddr base = vms->memmap[uart].base;
  642. hwaddr size = vms->memmap[uart].size;
  643. int irq = vms->irqmap[uart];
  644. const char compat[] = "arm,pl011\0arm,primecell";
  645. const char clocknames[] = "uartclk\0apb_pclk";
  646. DeviceState *dev = qdev_create(NULL, "pl011");
  647. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  648. qdev_prop_set_chr(dev, "chardev", chr);
  649. qdev_init_nofail(dev);
  650. memory_region_add_subregion(mem, base,
  651. sysbus_mmio_get_region(s, 0));
  652. sysbus_connect_irq(s, 0, pic[irq]);
  653. nodename = g_strdup_printf("/pl011@%" PRIx64, base);
  654. qemu_fdt_add_subnode(vms->fdt, nodename);
  655. /* Note that we can't use setprop_string because of the embedded NUL */
  656. qemu_fdt_setprop(vms->fdt, nodename, "compatible",
  657. compat, sizeof(compat));
  658. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  659. 2, base, 2, size);
  660. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  661. GIC_FDT_IRQ_TYPE_SPI, irq,
  662. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  663. qemu_fdt_setprop_cells(vms->fdt, nodename, "clocks",
  664. vms->clock_phandle, vms->clock_phandle);
  665. qemu_fdt_setprop(vms->fdt, nodename, "clock-names",
  666. clocknames, sizeof(clocknames));
  667. if (uart == VIRT_UART) {
  668. qemu_fdt_setprop_string(vms->fdt, "/chosen", "stdout-path", nodename);
  669. } else {
  670. /* Mark as not usable by the normal world */
  671. qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
  672. qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
  673. qemu_fdt_add_subnode(vms->fdt, "/secure-chosen");
  674. qemu_fdt_setprop_string(vms->fdt, "/secure-chosen", "stdout-path",
  675. nodename);
  676. }
  677. g_free(nodename);
  678. }
  679. static void create_rtc(const VirtMachineState *vms, qemu_irq *pic)
  680. {
  681. char *nodename;
  682. hwaddr base = vms->memmap[VIRT_RTC].base;
  683. hwaddr size = vms->memmap[VIRT_RTC].size;
  684. int irq = vms->irqmap[VIRT_RTC];
  685. const char compat[] = "arm,pl031\0arm,primecell";
  686. sysbus_create_simple("pl031", base, pic[irq]);
  687. nodename = g_strdup_printf("/pl031@%" PRIx64, base);
  688. qemu_fdt_add_subnode(vms->fdt, nodename);
  689. qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
  690. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  691. 2, base, 2, size);
  692. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  693. GIC_FDT_IRQ_TYPE_SPI, irq,
  694. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  695. qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
  696. qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
  697. g_free(nodename);
  698. }
  699. static DeviceState *gpio_key_dev;
  700. static void virt_powerdown_req(Notifier *n, void *opaque)
  701. {
  702. VirtMachineState *s = container_of(n, VirtMachineState, powerdown_notifier);
  703. if (s->acpi_dev) {
  704. acpi_send_event(s->acpi_dev, ACPI_POWER_DOWN_STATUS);
  705. } else {
  706. /* use gpio Pin 3 for power button event */
  707. qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
  708. }
  709. }
  710. static void create_gpio(const VirtMachineState *vms, qemu_irq *pic)
  711. {
  712. char *nodename;
  713. DeviceState *pl061_dev;
  714. hwaddr base = vms->memmap[VIRT_GPIO].base;
  715. hwaddr size = vms->memmap[VIRT_GPIO].size;
  716. int irq = vms->irqmap[VIRT_GPIO];
  717. const char compat[] = "arm,pl061\0arm,primecell";
  718. pl061_dev = sysbus_create_simple("pl061", base, pic[irq]);
  719. uint32_t phandle = qemu_fdt_alloc_phandle(vms->fdt);
  720. nodename = g_strdup_printf("/pl061@%" PRIx64, base);
  721. qemu_fdt_add_subnode(vms->fdt, nodename);
  722. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  723. 2, base, 2, size);
  724. qemu_fdt_setprop(vms->fdt, nodename, "compatible", compat, sizeof(compat));
  725. qemu_fdt_setprop_cell(vms->fdt, nodename, "#gpio-cells", 2);
  726. qemu_fdt_setprop(vms->fdt, nodename, "gpio-controller", NULL, 0);
  727. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  728. GIC_FDT_IRQ_TYPE_SPI, irq,
  729. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  730. qemu_fdt_setprop_cell(vms->fdt, nodename, "clocks", vms->clock_phandle);
  731. qemu_fdt_setprop_string(vms->fdt, nodename, "clock-names", "apb_pclk");
  732. qemu_fdt_setprop_cell(vms->fdt, nodename, "phandle", phandle);
  733. gpio_key_dev = sysbus_create_simple("gpio-key", -1,
  734. qdev_get_gpio_in(pl061_dev, 3));
  735. qemu_fdt_add_subnode(vms->fdt, "/gpio-keys");
  736. qemu_fdt_setprop_string(vms->fdt, "/gpio-keys", "compatible", "gpio-keys");
  737. qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#size-cells", 0);
  738. qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys", "#address-cells", 1);
  739. qemu_fdt_add_subnode(vms->fdt, "/gpio-keys/poweroff");
  740. qemu_fdt_setprop_string(vms->fdt, "/gpio-keys/poweroff",
  741. "label", "GPIO Key Poweroff");
  742. qemu_fdt_setprop_cell(vms->fdt, "/gpio-keys/poweroff", "linux,code",
  743. KEY_POWER);
  744. qemu_fdt_setprop_cells(vms->fdt, "/gpio-keys/poweroff",
  745. "gpios", phandle, 3, 0);
  746. g_free(nodename);
  747. }
  748. static void create_virtio_devices(const VirtMachineState *vms, qemu_irq *pic)
  749. {
  750. int i;
  751. hwaddr size = vms->memmap[VIRT_MMIO].size;
  752. /* We create the transports in forwards order. Since qbus_realize()
  753. * prepends (not appends) new child buses, the incrementing loop below will
  754. * create a list of virtio-mmio buses with decreasing base addresses.
  755. *
  756. * When a -device option is processed from the command line,
  757. * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
  758. * order. The upshot is that -device options in increasing command line
  759. * order are mapped to virtio-mmio buses with decreasing base addresses.
  760. *
  761. * When this code was originally written, that arrangement ensured that the
  762. * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
  763. * the first -device on the command line. (The end-to-end order is a
  764. * function of this loop, qbus_realize(), qbus_find_recursive(), and the
  765. * guest kernel's name-to-address assignment strategy.)
  766. *
  767. * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
  768. * the message, if not necessarily the code, of commit 70161ff336.
  769. * Therefore the loop now establishes the inverse of the original intent.
  770. *
  771. * Unfortunately, we can't counteract the kernel change by reversing the
  772. * loop; it would break existing command lines.
  773. *
  774. * In any case, the kernel makes no guarantee about the stability of
  775. * enumeration order of virtio devices (as demonstrated by it changing
  776. * between kernel versions). For reliable and stable identification
  777. * of disks users must use UUIDs or similar mechanisms.
  778. */
  779. for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
  780. int irq = vms->irqmap[VIRT_MMIO] + i;
  781. hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
  782. sysbus_create_simple("virtio-mmio", base, pic[irq]);
  783. }
  784. /* We add dtb nodes in reverse order so that they appear in the finished
  785. * device tree lowest address first.
  786. *
  787. * Note that this mapping is independent of the loop above. The previous
  788. * loop influences virtio device to virtio transport assignment, whereas
  789. * this loop controls how virtio transports are laid out in the dtb.
  790. */
  791. for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
  792. char *nodename;
  793. int irq = vms->irqmap[VIRT_MMIO] + i;
  794. hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
  795. nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
  796. qemu_fdt_add_subnode(vms->fdt, nodename);
  797. qemu_fdt_setprop_string(vms->fdt, nodename,
  798. "compatible", "virtio,mmio");
  799. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  800. 2, base, 2, size);
  801. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupts",
  802. GIC_FDT_IRQ_TYPE_SPI, irq,
  803. GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
  804. qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
  805. g_free(nodename);
  806. }
  807. }
  808. #define VIRT_FLASH_SECTOR_SIZE (256 * KiB)
  809. static PFlashCFI01 *virt_flash_create1(VirtMachineState *vms,
  810. const char *name,
  811. const char *alias_prop_name)
  812. {
  813. /*
  814. * Create a single flash device. We use the same parameters as
  815. * the flash devices on the Versatile Express board.
  816. */
  817. DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01);
  818. qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE);
  819. qdev_prop_set_uint8(dev, "width", 4);
  820. qdev_prop_set_uint8(dev, "device-width", 2);
  821. qdev_prop_set_bit(dev, "big-endian", false);
  822. qdev_prop_set_uint16(dev, "id0", 0x89);
  823. qdev_prop_set_uint16(dev, "id1", 0x18);
  824. qdev_prop_set_uint16(dev, "id2", 0x00);
  825. qdev_prop_set_uint16(dev, "id3", 0x00);
  826. qdev_prop_set_string(dev, "name", name);
  827. object_property_add_child(OBJECT(vms), name, OBJECT(dev),
  828. &error_abort);
  829. object_property_add_alias(OBJECT(vms), alias_prop_name,
  830. OBJECT(dev), "drive", &error_abort);
  831. return PFLASH_CFI01(dev);
  832. }
  833. static void virt_flash_create(VirtMachineState *vms)
  834. {
  835. vms->flash[0] = virt_flash_create1(vms, "virt.flash0", "pflash0");
  836. vms->flash[1] = virt_flash_create1(vms, "virt.flash1", "pflash1");
  837. }
  838. static void virt_flash_map1(PFlashCFI01 *flash,
  839. hwaddr base, hwaddr size,
  840. MemoryRegion *sysmem)
  841. {
  842. DeviceState *dev = DEVICE(flash);
  843. assert(size % VIRT_FLASH_SECTOR_SIZE == 0);
  844. assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
  845. qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE);
  846. qdev_init_nofail(dev);
  847. memory_region_add_subregion(sysmem, base,
  848. sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
  849. 0));
  850. }
  851. static void virt_flash_map(VirtMachineState *vms,
  852. MemoryRegion *sysmem,
  853. MemoryRegion *secure_sysmem)
  854. {
  855. /*
  856. * Map two flash devices to fill the VIRT_FLASH space in the memmap.
  857. * sysmem is the system memory space. secure_sysmem is the secure view
  858. * of the system, and the first flash device should be made visible only
  859. * there. The second flash device is visible to both secure and nonsecure.
  860. * If sysmem == secure_sysmem this means there is no separate Secure
  861. * address space and both flash devices are generally visible.
  862. */
  863. hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
  864. hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
  865. virt_flash_map1(vms->flash[0], flashbase, flashsize,
  866. secure_sysmem);
  867. virt_flash_map1(vms->flash[1], flashbase + flashsize, flashsize,
  868. sysmem);
  869. }
  870. static void virt_flash_fdt(VirtMachineState *vms,
  871. MemoryRegion *sysmem,
  872. MemoryRegion *secure_sysmem)
  873. {
  874. hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
  875. hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
  876. char *nodename;
  877. if (sysmem == secure_sysmem) {
  878. /* Report both flash devices as a single node in the DT */
  879. nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
  880. qemu_fdt_add_subnode(vms->fdt, nodename);
  881. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
  882. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  883. 2, flashbase, 2, flashsize,
  884. 2, flashbase + flashsize, 2, flashsize);
  885. qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
  886. g_free(nodename);
  887. } else {
  888. /*
  889. * Report the devices as separate nodes so we can mark one as
  890. * only visible to the secure world.
  891. */
  892. nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase);
  893. qemu_fdt_add_subnode(vms->fdt, nodename);
  894. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
  895. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  896. 2, flashbase, 2, flashsize);
  897. qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
  898. qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
  899. qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
  900. g_free(nodename);
  901. nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
  902. qemu_fdt_add_subnode(vms->fdt, nodename);
  903. qemu_fdt_setprop_string(vms->fdt, nodename, "compatible", "cfi-flash");
  904. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  905. 2, flashbase + flashsize, 2, flashsize);
  906. qemu_fdt_setprop_cell(vms->fdt, nodename, "bank-width", 4);
  907. g_free(nodename);
  908. }
  909. }
  910. static bool virt_firmware_init(VirtMachineState *vms,
  911. MemoryRegion *sysmem,
  912. MemoryRegion *secure_sysmem)
  913. {
  914. int i;
  915. BlockBackend *pflash_blk0;
  916. /* Map legacy -drive if=pflash to machine properties */
  917. for (i = 0; i < ARRAY_SIZE(vms->flash); i++) {
  918. pflash_cfi01_legacy_drive(vms->flash[i],
  919. drive_get(IF_PFLASH, 0, i));
  920. }
  921. virt_flash_map(vms, sysmem, secure_sysmem);
  922. pflash_blk0 = pflash_cfi01_get_blk(vms->flash[0]);
  923. if (bios_name) {
  924. char *fname;
  925. MemoryRegion *mr;
  926. int image_size;
  927. if (pflash_blk0) {
  928. error_report("The contents of the first flash device may be "
  929. "specified with -bios or with -drive if=pflash... "
  930. "but you cannot use both options at once");
  931. exit(1);
  932. }
  933. /* Fall back to -bios */
  934. fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  935. if (!fname) {
  936. error_report("Could not find ROM image '%s'", bios_name);
  937. exit(1);
  938. }
  939. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(vms->flash[0]), 0);
  940. image_size = load_image_mr(fname, mr);
  941. g_free(fname);
  942. if (image_size < 0) {
  943. error_report("Could not load ROM image '%s'", bios_name);
  944. exit(1);
  945. }
  946. }
  947. return pflash_blk0 || bios_name;
  948. }
  949. static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as)
  950. {
  951. MachineState *ms = MACHINE(vms);
  952. hwaddr base = vms->memmap[VIRT_FW_CFG].base;
  953. hwaddr size = vms->memmap[VIRT_FW_CFG].size;
  954. FWCfgState *fw_cfg;
  955. char *nodename;
  956. fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
  957. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)ms->smp.cpus);
  958. nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
  959. qemu_fdt_add_subnode(vms->fdt, nodename);
  960. qemu_fdt_setprop_string(vms->fdt, nodename,
  961. "compatible", "qemu,fw-cfg-mmio");
  962. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  963. 2, base, 2, size);
  964. qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
  965. g_free(nodename);
  966. return fw_cfg;
  967. }
  968. static void create_pcie_irq_map(const VirtMachineState *vms,
  969. uint32_t gic_phandle,
  970. int first_irq, const char *nodename)
  971. {
  972. int devfn, pin;
  973. uint32_t full_irq_map[4 * 4 * 10] = { 0 };
  974. uint32_t *irq_map = full_irq_map;
  975. for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
  976. for (pin = 0; pin < 4; pin++) {
  977. int irq_type = GIC_FDT_IRQ_TYPE_SPI;
  978. int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
  979. int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  980. int i;
  981. uint32_t map[] = {
  982. devfn << 8, 0, 0, /* devfn */
  983. pin + 1, /* PCI pin */
  984. gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
  985. /* Convert map to big endian */
  986. for (i = 0; i < 10; i++) {
  987. irq_map[i] = cpu_to_be32(map[i]);
  988. }
  989. irq_map += 10;
  990. }
  991. }
  992. qemu_fdt_setprop(vms->fdt, nodename, "interrupt-map",
  993. full_irq_map, sizeof(full_irq_map));
  994. qemu_fdt_setprop_cells(vms->fdt, nodename, "interrupt-map-mask",
  995. 0x1800, 0, 0, /* devfn (PCI_SLOT(3)) */
  996. 0x7 /* PCI irq */);
  997. }
  998. static void create_smmu(const VirtMachineState *vms, qemu_irq *pic,
  999. PCIBus *bus)
  1000. {
  1001. char *node;
  1002. const char compat[] = "arm,smmu-v3";
  1003. int irq = vms->irqmap[VIRT_SMMU];
  1004. int i;
  1005. hwaddr base = vms->memmap[VIRT_SMMU].base;
  1006. hwaddr size = vms->memmap[VIRT_SMMU].size;
  1007. const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror";
  1008. DeviceState *dev;
  1009. if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) {
  1010. return;
  1011. }
  1012. dev = qdev_create(NULL, "arm-smmuv3");
  1013. object_property_set_link(OBJECT(dev), OBJECT(bus), "primary-bus",
  1014. &error_abort);
  1015. qdev_init_nofail(dev);
  1016. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  1017. for (i = 0; i < NUM_SMMU_IRQS; i++) {
  1018. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
  1019. }
  1020. node = g_strdup_printf("/smmuv3@%" PRIx64, base);
  1021. qemu_fdt_add_subnode(vms->fdt, node);
  1022. qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
  1023. qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg", 2, base, 2, size);
  1024. qemu_fdt_setprop_cells(vms->fdt, node, "interrupts",
  1025. GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1026. GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1027. GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1028. GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
  1029. qemu_fdt_setprop(vms->fdt, node, "interrupt-names", irq_names,
  1030. sizeof(irq_names));
  1031. qemu_fdt_setprop_cell(vms->fdt, node, "clocks", vms->clock_phandle);
  1032. qemu_fdt_setprop_string(vms->fdt, node, "clock-names", "apb_pclk");
  1033. qemu_fdt_setprop(vms->fdt, node, "dma-coherent", NULL, 0);
  1034. qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
  1035. qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
  1036. g_free(node);
  1037. }
  1038. static void create_pcie(VirtMachineState *vms, qemu_irq *pic)
  1039. {
  1040. hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
  1041. hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size;
  1042. hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base;
  1043. hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size;
  1044. hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base;
  1045. hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size;
  1046. hwaddr base_ecam, size_ecam;
  1047. hwaddr base = base_mmio;
  1048. int nr_pcie_buses;
  1049. int irq = vms->irqmap[VIRT_PCIE];
  1050. MemoryRegion *mmio_alias;
  1051. MemoryRegion *mmio_reg;
  1052. MemoryRegion *ecam_alias;
  1053. MemoryRegion *ecam_reg;
  1054. DeviceState *dev;
  1055. char *nodename;
  1056. int i, ecam_id;
  1057. PCIHostState *pci;
  1058. dev = qdev_create(NULL, TYPE_GPEX_HOST);
  1059. qdev_init_nofail(dev);
  1060. ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
  1061. base_ecam = vms->memmap[ecam_id].base;
  1062. size_ecam = vms->memmap[ecam_id].size;
  1063. nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
  1064. /* Map only the first size_ecam bytes of ECAM space */
  1065. ecam_alias = g_new0(MemoryRegion, 1);
  1066. ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  1067. memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
  1068. ecam_reg, 0, size_ecam);
  1069. memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
  1070. /* Map the MMIO window into system address space so as to expose
  1071. * the section of PCI MMIO space which starts at the same base address
  1072. * (ie 1:1 mapping for that part of PCI MMIO space visible through
  1073. * the window).
  1074. */
  1075. mmio_alias = g_new0(MemoryRegion, 1);
  1076. mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  1077. memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
  1078. mmio_reg, base_mmio, size_mmio);
  1079. memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
  1080. if (vms->highmem) {
  1081. /* Map high MMIO space */
  1082. MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
  1083. memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
  1084. mmio_reg, base_mmio_high, size_mmio_high);
  1085. memory_region_add_subregion(get_system_memory(), base_mmio_high,
  1086. high_mmio_alias);
  1087. }
  1088. /* Map IO port space */
  1089. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
  1090. for (i = 0; i < GPEX_NUM_IRQS; i++) {
  1091. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pic[irq + i]);
  1092. gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
  1093. }
  1094. pci = PCI_HOST_BRIDGE(dev);
  1095. if (pci->bus) {
  1096. for (i = 0; i < nb_nics; i++) {
  1097. NICInfo *nd = &nd_table[i];
  1098. if (!nd->model) {
  1099. nd->model = g_strdup("virtio");
  1100. }
  1101. pci_nic_init_nofail(nd, pci->bus, nd->model, NULL);
  1102. }
  1103. }
  1104. nodename = g_strdup_printf("/pcie@%" PRIx64, base);
  1105. qemu_fdt_add_subnode(vms->fdt, nodename);
  1106. qemu_fdt_setprop_string(vms->fdt, nodename,
  1107. "compatible", "pci-host-ecam-generic");
  1108. qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "pci");
  1109. qemu_fdt_setprop_cell(vms->fdt, nodename, "#address-cells", 3);
  1110. qemu_fdt_setprop_cell(vms->fdt, nodename, "#size-cells", 2);
  1111. qemu_fdt_setprop_cell(vms->fdt, nodename, "linux,pci-domain", 0);
  1112. qemu_fdt_setprop_cells(vms->fdt, nodename, "bus-range", 0,
  1113. nr_pcie_buses - 1);
  1114. qemu_fdt_setprop(vms->fdt, nodename, "dma-coherent", NULL, 0);
  1115. if (vms->msi_phandle) {
  1116. qemu_fdt_setprop_cells(vms->fdt, nodename, "msi-parent",
  1117. vms->msi_phandle);
  1118. }
  1119. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg",
  1120. 2, base_ecam, 2, size_ecam);
  1121. if (vms->highmem) {
  1122. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
  1123. 1, FDT_PCI_RANGE_IOPORT, 2, 0,
  1124. 2, base_pio, 2, size_pio,
  1125. 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
  1126. 2, base_mmio, 2, size_mmio,
  1127. 1, FDT_PCI_RANGE_MMIO_64BIT,
  1128. 2, base_mmio_high,
  1129. 2, base_mmio_high, 2, size_mmio_high);
  1130. } else {
  1131. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "ranges",
  1132. 1, FDT_PCI_RANGE_IOPORT, 2, 0,
  1133. 2, base_pio, 2, size_pio,
  1134. 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
  1135. 2, base_mmio, 2, size_mmio);
  1136. }
  1137. qemu_fdt_setprop_cell(vms->fdt, nodename, "#interrupt-cells", 1);
  1138. create_pcie_irq_map(vms, vms->gic_phandle, irq, nodename);
  1139. if (vms->iommu) {
  1140. vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
  1141. create_smmu(vms, pic, pci->bus);
  1142. qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
  1143. 0x0, vms->iommu_phandle, 0x0, 0x10000);
  1144. }
  1145. g_free(nodename);
  1146. }
  1147. static void create_platform_bus(VirtMachineState *vms, qemu_irq *pic)
  1148. {
  1149. DeviceState *dev;
  1150. SysBusDevice *s;
  1151. int i;
  1152. MemoryRegion *sysmem = get_system_memory();
  1153. dev = qdev_create(NULL, TYPE_PLATFORM_BUS_DEVICE);
  1154. dev->id = TYPE_PLATFORM_BUS_DEVICE;
  1155. qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS);
  1156. qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size);
  1157. qdev_init_nofail(dev);
  1158. vms->platform_bus_dev = dev;
  1159. s = SYS_BUS_DEVICE(dev);
  1160. for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) {
  1161. int irqn = vms->irqmap[VIRT_PLATFORM_BUS] + i;
  1162. sysbus_connect_irq(s, i, pic[irqn]);
  1163. }
  1164. memory_region_add_subregion(sysmem,
  1165. vms->memmap[VIRT_PLATFORM_BUS].base,
  1166. sysbus_mmio_get_region(s, 0));
  1167. }
  1168. static void create_secure_ram(VirtMachineState *vms,
  1169. MemoryRegion *secure_sysmem)
  1170. {
  1171. MemoryRegion *secram = g_new(MemoryRegion, 1);
  1172. char *nodename;
  1173. hwaddr base = vms->memmap[VIRT_SECURE_MEM].base;
  1174. hwaddr size = vms->memmap[VIRT_SECURE_MEM].size;
  1175. memory_region_init_ram(secram, NULL, "virt.secure-ram", size,
  1176. &error_fatal);
  1177. memory_region_add_subregion(secure_sysmem, base, secram);
  1178. nodename = g_strdup_printf("/secram@%" PRIx64, base);
  1179. qemu_fdt_add_subnode(vms->fdt, nodename);
  1180. qemu_fdt_setprop_string(vms->fdt, nodename, "device_type", "memory");
  1181. qemu_fdt_setprop_sized_cells(vms->fdt, nodename, "reg", 2, base, 2, size);
  1182. qemu_fdt_setprop_string(vms->fdt, nodename, "status", "disabled");
  1183. qemu_fdt_setprop_string(vms->fdt, nodename, "secure-status", "okay");
  1184. g_free(nodename);
  1185. }
  1186. static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
  1187. {
  1188. const VirtMachineState *board = container_of(binfo, VirtMachineState,
  1189. bootinfo);
  1190. *fdt_size = board->fdt_size;
  1191. return board->fdt;
  1192. }
  1193. static void virt_build_smbios(VirtMachineState *vms)
  1194. {
  1195. MachineClass *mc = MACHINE_GET_CLASS(vms);
  1196. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1197. uint8_t *smbios_tables, *smbios_anchor;
  1198. size_t smbios_tables_len, smbios_anchor_len;
  1199. const char *product = "QEMU Virtual Machine";
  1200. if (kvm_enabled()) {
  1201. product = "KVM Virtual Machine";
  1202. }
  1203. smbios_set_defaults("QEMU", product,
  1204. vmc->smbios_old_sys_ver ? "1.0" : mc->name, false,
  1205. true, SMBIOS_ENTRY_POINT_30);
  1206. smbios_get_tables(MACHINE(vms), NULL, 0, &smbios_tables, &smbios_tables_len,
  1207. &smbios_anchor, &smbios_anchor_len);
  1208. if (smbios_anchor) {
  1209. fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables",
  1210. smbios_tables, smbios_tables_len);
  1211. fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor",
  1212. smbios_anchor, smbios_anchor_len);
  1213. }
  1214. }
  1215. static
  1216. void virt_machine_done(Notifier *notifier, void *data)
  1217. {
  1218. VirtMachineState *vms = container_of(notifier, VirtMachineState,
  1219. machine_done);
  1220. MachineState *ms = MACHINE(vms);
  1221. ARMCPU *cpu = ARM_CPU(first_cpu);
  1222. struct arm_boot_info *info = &vms->bootinfo;
  1223. AddressSpace *as = arm_boot_address_space(cpu, info);
  1224. /*
  1225. * If the user provided a dtb, we assume the dynamic sysbus nodes
  1226. * already are integrated there. This corresponds to a use case where
  1227. * the dynamic sysbus nodes are complex and their generation is not yet
  1228. * supported. In that case the user can take charge of the guest dt
  1229. * while qemu takes charge of the qom stuff.
  1230. */
  1231. if (info->dtb_filename == NULL) {
  1232. platform_bus_add_all_fdt_nodes(vms->fdt, "/intc",
  1233. vms->memmap[VIRT_PLATFORM_BUS].base,
  1234. vms->memmap[VIRT_PLATFORM_BUS].size,
  1235. vms->irqmap[VIRT_PLATFORM_BUS]);
  1236. }
  1237. if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) {
  1238. exit(1);
  1239. }
  1240. virt_acpi_setup(vms);
  1241. virt_build_smbios(vms);
  1242. }
  1243. static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
  1244. {
  1245. uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
  1246. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1247. if (!vmc->disallow_affinity_adjustment) {
  1248. /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
  1249. * GIC's target-list limitations. 32-bit KVM hosts currently
  1250. * always create clusters of 4 CPUs, but that is expected to
  1251. * change when they gain support for gicv3. When KVM is enabled
  1252. * it will override the changes we make here, therefore our
  1253. * purposes are to make TCG consistent (with 64-bit KVM hosts)
  1254. * and to improve SGI efficiency.
  1255. */
  1256. if (vms->gic_version == 3) {
  1257. clustersz = GICV3_TARGETLIST_BITS;
  1258. } else {
  1259. clustersz = GIC_TARGETLIST_BITS;
  1260. }
  1261. }
  1262. return arm_cpu_mp_affinity(idx, clustersz);
  1263. }
  1264. static void virt_set_memmap(VirtMachineState *vms)
  1265. {
  1266. MachineState *ms = MACHINE(vms);
  1267. hwaddr base, device_memory_base, device_memory_size;
  1268. int i;
  1269. vms->memmap = extended_memmap;
  1270. for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
  1271. vms->memmap[i] = base_memmap[i];
  1272. }
  1273. if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) {
  1274. error_report("unsupported number of memory slots: %"PRIu64,
  1275. ms->ram_slots);
  1276. exit(EXIT_FAILURE);
  1277. }
  1278. /*
  1279. * We compute the base of the high IO region depending on the
  1280. * amount of initial and device memory. The device memory start/size
  1281. * is aligned on 1GiB. We never put the high IO region below 256GiB
  1282. * so that if maxram_size is < 255GiB we keep the legacy memory map.
  1283. * The device region size assumes 1GiB page max alignment per slot.
  1284. */
  1285. device_memory_base =
  1286. ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB);
  1287. device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB;
  1288. /* Base address of the high IO region */
  1289. base = device_memory_base + ROUND_UP(device_memory_size, GiB);
  1290. if (base < device_memory_base) {
  1291. error_report("maxmem/slots too huge");
  1292. exit(EXIT_FAILURE);
  1293. }
  1294. if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) {
  1295. base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES;
  1296. }
  1297. for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
  1298. hwaddr size = extended_memmap[i].size;
  1299. base = ROUND_UP(base, size);
  1300. vms->memmap[i].base = base;
  1301. vms->memmap[i].size = size;
  1302. base += size;
  1303. }
  1304. vms->highest_gpa = base - 1;
  1305. if (device_memory_size > 0) {
  1306. ms->device_memory = g_malloc0(sizeof(*ms->device_memory));
  1307. ms->device_memory->base = device_memory_base;
  1308. memory_region_init(&ms->device_memory->mr, OBJECT(vms),
  1309. "device-memory", device_memory_size);
  1310. }
  1311. }
  1312. static void machvirt_init(MachineState *machine)
  1313. {
  1314. VirtMachineState *vms = VIRT_MACHINE(machine);
  1315. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
  1316. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1317. const CPUArchIdList *possible_cpus;
  1318. qemu_irq pic[NUM_IRQS];
  1319. MemoryRegion *sysmem = get_system_memory();
  1320. MemoryRegion *secure_sysmem = NULL;
  1321. int n, virt_max_cpus;
  1322. MemoryRegion *ram = g_new(MemoryRegion, 1);
  1323. bool firmware_loaded;
  1324. bool aarch64 = true;
  1325. bool has_ged = !vmc->no_ged;
  1326. unsigned int smp_cpus = machine->smp.cpus;
  1327. unsigned int max_cpus = machine->smp.max_cpus;
  1328. /*
  1329. * In accelerated mode, the memory map is computed earlier in kvm_type()
  1330. * to create a VM with the right number of IPA bits.
  1331. */
  1332. if (!vms->memmap) {
  1333. virt_set_memmap(vms);
  1334. }
  1335. /* We can probe only here because during property set
  1336. * KVM is not available yet
  1337. */
  1338. if (vms->gic_version <= 0) {
  1339. /* "host" or "max" */
  1340. if (!kvm_enabled()) {
  1341. if (vms->gic_version == 0) {
  1342. error_report("gic-version=host requires KVM");
  1343. exit(1);
  1344. } else {
  1345. /* "max": currently means 3 for TCG */
  1346. vms->gic_version = 3;
  1347. }
  1348. } else {
  1349. vms->gic_version = kvm_arm_vgic_probe();
  1350. if (!vms->gic_version) {
  1351. error_report(
  1352. "Unable to determine GIC version supported by host");
  1353. exit(1);
  1354. }
  1355. }
  1356. }
  1357. if (!cpu_type_valid(machine->cpu_type)) {
  1358. error_report("mach-virt: CPU type %s not supported", machine->cpu_type);
  1359. exit(1);
  1360. }
  1361. if (vms->secure) {
  1362. if (kvm_enabled()) {
  1363. error_report("mach-virt: KVM does not support Security extensions");
  1364. exit(1);
  1365. }
  1366. /*
  1367. * The Secure view of the world is the same as the NonSecure,
  1368. * but with a few extra devices. Create it as a container region
  1369. * containing the system memory at low priority; any secure-only
  1370. * devices go in at higher priority and take precedence.
  1371. */
  1372. secure_sysmem = g_new(MemoryRegion, 1);
  1373. memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
  1374. UINT64_MAX);
  1375. memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
  1376. }
  1377. firmware_loaded = virt_firmware_init(vms, sysmem,
  1378. secure_sysmem ?: sysmem);
  1379. /* If we have an EL3 boot ROM then the assumption is that it will
  1380. * implement PSCI itself, so disable QEMU's internal implementation
  1381. * so it doesn't get in the way. Instead of starting secondary
  1382. * CPUs in PSCI powerdown state we will start them all running and
  1383. * let the boot ROM sort them out.
  1384. * The usual case is that we do use QEMU's PSCI implementation;
  1385. * if the guest has EL2 then we will use SMC as the conduit,
  1386. * and otherwise we will use HVC (for backwards compatibility and
  1387. * because if we're using KVM then we must use HVC).
  1388. */
  1389. if (vms->secure && firmware_loaded) {
  1390. vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
  1391. } else if (vms->virt) {
  1392. vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
  1393. } else {
  1394. vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
  1395. }
  1396. /* The maximum number of CPUs depends on the GIC version, or on how
  1397. * many redistributors we can fit into the memory map.
  1398. */
  1399. if (vms->gic_version == 3) {
  1400. virt_max_cpus =
  1401. vms->memmap[VIRT_GIC_REDIST].size / GICV3_REDIST_SIZE;
  1402. virt_max_cpus +=
  1403. vms->memmap[VIRT_HIGH_GIC_REDIST2].size / GICV3_REDIST_SIZE;
  1404. } else {
  1405. virt_max_cpus = GIC_NCPU;
  1406. }
  1407. if (max_cpus > virt_max_cpus) {
  1408. error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
  1409. "supported by machine 'mach-virt' (%d)",
  1410. max_cpus, virt_max_cpus);
  1411. exit(1);
  1412. }
  1413. vms->smp_cpus = smp_cpus;
  1414. if (vms->virt && kvm_enabled()) {
  1415. error_report("mach-virt: KVM does not support providing "
  1416. "Virtualization extensions to the guest CPU");
  1417. exit(1);
  1418. }
  1419. create_fdt(vms);
  1420. possible_cpus = mc->possible_cpu_arch_ids(machine);
  1421. for (n = 0; n < possible_cpus->len; n++) {
  1422. Object *cpuobj;
  1423. CPUState *cs;
  1424. if (n >= smp_cpus) {
  1425. break;
  1426. }
  1427. cpuobj = object_new(possible_cpus->cpus[n].type);
  1428. object_property_set_int(cpuobj, possible_cpus->cpus[n].arch_id,
  1429. "mp-affinity", NULL);
  1430. cs = CPU(cpuobj);
  1431. cs->cpu_index = n;
  1432. numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
  1433. &error_fatal);
  1434. aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
  1435. if (!vms->secure) {
  1436. object_property_set_bool(cpuobj, false, "has_el3", NULL);
  1437. }
  1438. if (!vms->virt && object_property_find(cpuobj, "has_el2", NULL)) {
  1439. object_property_set_bool(cpuobj, false, "has_el2", NULL);
  1440. }
  1441. if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
  1442. object_property_set_int(cpuobj, vms->psci_conduit,
  1443. "psci-conduit", NULL);
  1444. /* Secondary CPUs start in PSCI powered-down state */
  1445. if (n > 0) {
  1446. object_property_set_bool(cpuobj, true,
  1447. "start-powered-off", NULL);
  1448. }
  1449. }
  1450. if (vmc->no_pmu && object_property_find(cpuobj, "pmu", NULL)) {
  1451. object_property_set_bool(cpuobj, false, "pmu", NULL);
  1452. }
  1453. if (object_property_find(cpuobj, "reset-cbar", NULL)) {
  1454. object_property_set_int(cpuobj, vms->memmap[VIRT_CPUPERIPHS].base,
  1455. "reset-cbar", &error_abort);
  1456. }
  1457. object_property_set_link(cpuobj, OBJECT(sysmem), "memory",
  1458. &error_abort);
  1459. if (vms->secure) {
  1460. object_property_set_link(cpuobj, OBJECT(secure_sysmem),
  1461. "secure-memory", &error_abort);
  1462. }
  1463. object_property_set_bool(cpuobj, true, "realized", &error_fatal);
  1464. object_unref(cpuobj);
  1465. }
  1466. fdt_add_timer_nodes(vms);
  1467. fdt_add_cpu_nodes(vms);
  1468. if (!kvm_enabled()) {
  1469. ARMCPU *cpu = ARM_CPU(first_cpu);
  1470. bool aarch64 = object_property_get_bool(OBJECT(cpu), "aarch64", NULL);
  1471. if (aarch64 && vms->highmem) {
  1472. int requested_pa_size, pamax = arm_pamax(cpu);
  1473. requested_pa_size = 64 - clz64(vms->highest_gpa);
  1474. if (pamax < requested_pa_size) {
  1475. error_report("VCPU supports less PA bits (%d) than requested "
  1476. "by the memory map (%d)", pamax, requested_pa_size);
  1477. exit(1);
  1478. }
  1479. }
  1480. }
  1481. memory_region_allocate_system_memory(ram, NULL, "mach-virt.ram",
  1482. machine->ram_size);
  1483. memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base, ram);
  1484. if (machine->device_memory) {
  1485. memory_region_add_subregion(sysmem, machine->device_memory->base,
  1486. &machine->device_memory->mr);
  1487. }
  1488. virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem);
  1489. create_gic(vms, pic);
  1490. fdt_add_pmu_nodes(vms);
  1491. create_uart(vms, pic, VIRT_UART, sysmem, serial_hd(0));
  1492. if (vms->secure) {
  1493. create_secure_ram(vms, secure_sysmem);
  1494. create_uart(vms, pic, VIRT_SECURE_UART, secure_sysmem, serial_hd(1));
  1495. }
  1496. vms->highmem_ecam &= vms->highmem && (!firmware_loaded || aarch64);
  1497. create_rtc(vms, pic);
  1498. create_pcie(vms, pic);
  1499. if (has_ged && aarch64 && firmware_loaded && acpi_enabled) {
  1500. vms->acpi_dev = create_acpi_ged(vms, pic);
  1501. } else {
  1502. create_gpio(vms, pic);
  1503. }
  1504. /* connect powerdown request */
  1505. vms->powerdown_notifier.notify = virt_powerdown_req;
  1506. qemu_register_powerdown_notifier(&vms->powerdown_notifier);
  1507. /* Create mmio transports, so the user can create virtio backends
  1508. * (which will be automatically plugged in to the transports). If
  1509. * no backend is created the transport will just sit harmlessly idle.
  1510. */
  1511. create_virtio_devices(vms, pic);
  1512. vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
  1513. rom_set_fw(vms->fw_cfg);
  1514. create_platform_bus(vms, pic);
  1515. vms->bootinfo.ram_size = machine->ram_size;
  1516. vms->bootinfo.nb_cpus = smp_cpus;
  1517. vms->bootinfo.board_id = -1;
  1518. vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
  1519. vms->bootinfo.get_dtb = machvirt_dtb;
  1520. vms->bootinfo.skip_dtb_autoload = true;
  1521. vms->bootinfo.firmware_loaded = firmware_loaded;
  1522. arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
  1523. vms->machine_done.notify = virt_machine_done;
  1524. qemu_add_machine_init_done_notifier(&vms->machine_done);
  1525. }
  1526. static bool virt_get_secure(Object *obj, Error **errp)
  1527. {
  1528. VirtMachineState *vms = VIRT_MACHINE(obj);
  1529. return vms->secure;
  1530. }
  1531. static void virt_set_secure(Object *obj, bool value, Error **errp)
  1532. {
  1533. VirtMachineState *vms = VIRT_MACHINE(obj);
  1534. vms->secure = value;
  1535. }
  1536. static bool virt_get_virt(Object *obj, Error **errp)
  1537. {
  1538. VirtMachineState *vms = VIRT_MACHINE(obj);
  1539. return vms->virt;
  1540. }
  1541. static void virt_set_virt(Object *obj, bool value, Error **errp)
  1542. {
  1543. VirtMachineState *vms = VIRT_MACHINE(obj);
  1544. vms->virt = value;
  1545. }
  1546. static bool virt_get_highmem(Object *obj, Error **errp)
  1547. {
  1548. VirtMachineState *vms = VIRT_MACHINE(obj);
  1549. return vms->highmem;
  1550. }
  1551. static void virt_set_highmem(Object *obj, bool value, Error **errp)
  1552. {
  1553. VirtMachineState *vms = VIRT_MACHINE(obj);
  1554. vms->highmem = value;
  1555. }
  1556. static bool virt_get_its(Object *obj, Error **errp)
  1557. {
  1558. VirtMachineState *vms = VIRT_MACHINE(obj);
  1559. return vms->its;
  1560. }
  1561. static void virt_set_its(Object *obj, bool value, Error **errp)
  1562. {
  1563. VirtMachineState *vms = VIRT_MACHINE(obj);
  1564. vms->its = value;
  1565. }
  1566. static char *virt_get_gic_version(Object *obj, Error **errp)
  1567. {
  1568. VirtMachineState *vms = VIRT_MACHINE(obj);
  1569. const char *val = vms->gic_version == 3 ? "3" : "2";
  1570. return g_strdup(val);
  1571. }
  1572. static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
  1573. {
  1574. VirtMachineState *vms = VIRT_MACHINE(obj);
  1575. if (!strcmp(value, "3")) {
  1576. vms->gic_version = 3;
  1577. } else if (!strcmp(value, "2")) {
  1578. vms->gic_version = 2;
  1579. } else if (!strcmp(value, "host")) {
  1580. vms->gic_version = 0; /* Will probe later */
  1581. } else if (!strcmp(value, "max")) {
  1582. vms->gic_version = -1; /* Will probe later */
  1583. } else {
  1584. error_setg(errp, "Invalid gic-version value");
  1585. error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
  1586. }
  1587. }
  1588. static char *virt_get_iommu(Object *obj, Error **errp)
  1589. {
  1590. VirtMachineState *vms = VIRT_MACHINE(obj);
  1591. switch (vms->iommu) {
  1592. case VIRT_IOMMU_NONE:
  1593. return g_strdup("none");
  1594. case VIRT_IOMMU_SMMUV3:
  1595. return g_strdup("smmuv3");
  1596. default:
  1597. g_assert_not_reached();
  1598. }
  1599. }
  1600. static void virt_set_iommu(Object *obj, const char *value, Error **errp)
  1601. {
  1602. VirtMachineState *vms = VIRT_MACHINE(obj);
  1603. if (!strcmp(value, "smmuv3")) {
  1604. vms->iommu = VIRT_IOMMU_SMMUV3;
  1605. } else if (!strcmp(value, "none")) {
  1606. vms->iommu = VIRT_IOMMU_NONE;
  1607. } else {
  1608. error_setg(errp, "Invalid iommu value");
  1609. error_append_hint(errp, "Valid values are none, smmuv3.\n");
  1610. }
  1611. }
  1612. static CpuInstanceProperties
  1613. virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  1614. {
  1615. MachineClass *mc = MACHINE_GET_CLASS(ms);
  1616. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  1617. assert(cpu_index < possible_cpus->len);
  1618. return possible_cpus->cpus[cpu_index].props;
  1619. }
  1620. static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
  1621. {
  1622. return idx % ms->numa_state->num_nodes;
  1623. }
  1624. static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
  1625. {
  1626. int n;
  1627. unsigned int max_cpus = ms->smp.max_cpus;
  1628. VirtMachineState *vms = VIRT_MACHINE(ms);
  1629. if (ms->possible_cpus) {
  1630. assert(ms->possible_cpus->len == max_cpus);
  1631. return ms->possible_cpus;
  1632. }
  1633. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  1634. sizeof(CPUArchId) * max_cpus);
  1635. ms->possible_cpus->len = max_cpus;
  1636. for (n = 0; n < ms->possible_cpus->len; n++) {
  1637. ms->possible_cpus->cpus[n].type = ms->cpu_type;
  1638. ms->possible_cpus->cpus[n].arch_id =
  1639. virt_cpu_mp_affinity(vms, n);
  1640. ms->possible_cpus->cpus[n].props.has_thread_id = true;
  1641. ms->possible_cpus->cpus[n].props.thread_id = n;
  1642. }
  1643. return ms->possible_cpus;
  1644. }
  1645. static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  1646. Error **errp)
  1647. {
  1648. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  1649. const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  1650. if (is_nvdimm) {
  1651. error_setg(errp, "nvdimm is not yet supported");
  1652. return;
  1653. }
  1654. if (!vms->acpi_dev) {
  1655. error_setg(errp,
  1656. "memory hotplug is not enabled: missing acpi-ged device");
  1657. return;
  1658. }
  1659. pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), NULL, errp);
  1660. }
  1661. static void virt_memory_plug(HotplugHandler *hotplug_dev,
  1662. DeviceState *dev, Error **errp)
  1663. {
  1664. HotplugHandlerClass *hhc;
  1665. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  1666. Error *local_err = NULL;
  1667. pc_dimm_plug(PC_DIMM(dev), MACHINE(vms), &local_err);
  1668. if (local_err) {
  1669. goto out;
  1670. }
  1671. hhc = HOTPLUG_HANDLER_GET_CLASS(vms->acpi_dev);
  1672. hhc->plug(HOTPLUG_HANDLER(vms->acpi_dev), dev, &error_abort);
  1673. out:
  1674. error_propagate(errp, local_err);
  1675. }
  1676. static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  1677. DeviceState *dev, Error **errp)
  1678. {
  1679. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1680. virt_memory_pre_plug(hotplug_dev, dev, errp);
  1681. }
  1682. }
  1683. static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
  1684. DeviceState *dev, Error **errp)
  1685. {
  1686. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  1687. if (vms->platform_bus_dev) {
  1688. if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE)) {
  1689. platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
  1690. SYS_BUS_DEVICE(dev));
  1691. }
  1692. }
  1693. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1694. virt_memory_plug(hotplug_dev, dev, errp);
  1695. }
  1696. }
  1697. static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  1698. DeviceState *dev, Error **errp)
  1699. {
  1700. error_setg(errp, "device unplug request for unsupported device"
  1701. " type: %s", object_get_typename(OBJECT(dev)));
  1702. }
  1703. static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
  1704. DeviceState *dev)
  1705. {
  1706. if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
  1707. (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
  1708. return HOTPLUG_HANDLER(machine);
  1709. }
  1710. return NULL;
  1711. }
  1712. /*
  1713. * for arm64 kvm_type [7-0] encodes the requested number of bits
  1714. * in the IPA address space
  1715. */
  1716. static int virt_kvm_type(MachineState *ms, const char *type_str)
  1717. {
  1718. VirtMachineState *vms = VIRT_MACHINE(ms);
  1719. int max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms);
  1720. int requested_pa_size;
  1721. /* we freeze the memory map to compute the highest gpa */
  1722. virt_set_memmap(vms);
  1723. requested_pa_size = 64 - clz64(vms->highest_gpa);
  1724. if (requested_pa_size > max_vm_pa_size) {
  1725. error_report("-m and ,maxmem option values "
  1726. "require an IPA range (%d bits) larger than "
  1727. "the one supported by the host (%d bits)",
  1728. requested_pa_size, max_vm_pa_size);
  1729. exit(1);
  1730. }
  1731. /*
  1732. * By default we return 0 which corresponds to an implicit legacy
  1733. * 40b IPA setting. Otherwise we return the actual requested PA
  1734. * logsize
  1735. */
  1736. return requested_pa_size > 40 ? requested_pa_size : 0;
  1737. }
  1738. static void virt_machine_class_init(ObjectClass *oc, void *data)
  1739. {
  1740. MachineClass *mc = MACHINE_CLASS(oc);
  1741. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  1742. mc->init = machvirt_init;
  1743. /* Start with max_cpus set to 512, which is the maximum supported by KVM.
  1744. * The value may be reduced later when we have more information about the
  1745. * configuration of the particular instance.
  1746. */
  1747. mc->max_cpus = 512;
  1748. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
  1749. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
  1750. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
  1751. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
  1752. mc->block_default_type = IF_VIRTIO;
  1753. mc->no_cdrom = 1;
  1754. mc->pci_allow_0_address = true;
  1755. /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
  1756. mc->minimum_page_bits = 12;
  1757. mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
  1758. mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
  1759. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
  1760. mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
  1761. mc->kvm_type = virt_kvm_type;
  1762. assert(!mc->get_hotplug_handler);
  1763. mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
  1764. hc->pre_plug = virt_machine_device_pre_plug_cb;
  1765. hc->plug = virt_machine_device_plug_cb;
  1766. hc->unplug_request = virt_machine_device_unplug_request_cb;
  1767. mc->numa_mem_supported = true;
  1768. mc->auto_enable_numa_with_memhp = true;
  1769. }
  1770. static void virt_instance_init(Object *obj)
  1771. {
  1772. VirtMachineState *vms = VIRT_MACHINE(obj);
  1773. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1774. /* EL3 is disabled by default on virt: this makes us consistent
  1775. * between KVM and TCG for this board, and it also allows us to
  1776. * boot UEFI blobs which assume no TrustZone support.
  1777. */
  1778. vms->secure = false;
  1779. object_property_add_bool(obj, "secure", virt_get_secure,
  1780. virt_set_secure, NULL);
  1781. object_property_set_description(obj, "secure",
  1782. "Set on/off to enable/disable the ARM "
  1783. "Security Extensions (TrustZone)",
  1784. NULL);
  1785. /* EL2 is also disabled by default, for similar reasons */
  1786. vms->virt = false;
  1787. object_property_add_bool(obj, "virtualization", virt_get_virt,
  1788. virt_set_virt, NULL);
  1789. object_property_set_description(obj, "virtualization",
  1790. "Set on/off to enable/disable emulating a "
  1791. "guest CPU which implements the ARM "
  1792. "Virtualization Extensions",
  1793. NULL);
  1794. /* High memory is enabled by default */
  1795. vms->highmem = true;
  1796. object_property_add_bool(obj, "highmem", virt_get_highmem,
  1797. virt_set_highmem, NULL);
  1798. object_property_set_description(obj, "highmem",
  1799. "Set on/off to enable/disable using "
  1800. "physical address space above 32 bits",
  1801. NULL);
  1802. /* Default GIC type is v2 */
  1803. vms->gic_version = 2;
  1804. object_property_add_str(obj, "gic-version", virt_get_gic_version,
  1805. virt_set_gic_version, NULL);
  1806. object_property_set_description(obj, "gic-version",
  1807. "Set GIC version. "
  1808. "Valid values are 2, 3 and host", NULL);
  1809. vms->highmem_ecam = !vmc->no_highmem_ecam;
  1810. if (vmc->no_its) {
  1811. vms->its = false;
  1812. } else {
  1813. /* Default allows ITS instantiation */
  1814. vms->its = true;
  1815. object_property_add_bool(obj, "its", virt_get_its,
  1816. virt_set_its, NULL);
  1817. object_property_set_description(obj, "its",
  1818. "Set on/off to enable/disable "
  1819. "ITS instantiation",
  1820. NULL);
  1821. }
  1822. /* Default disallows iommu instantiation */
  1823. vms->iommu = VIRT_IOMMU_NONE;
  1824. object_property_add_str(obj, "iommu", virt_get_iommu, virt_set_iommu, NULL);
  1825. object_property_set_description(obj, "iommu",
  1826. "Set the IOMMU type. "
  1827. "Valid values are none and smmuv3",
  1828. NULL);
  1829. vms->irqmap = a15irqmap;
  1830. virt_flash_create(vms);
  1831. }
  1832. static const TypeInfo virt_machine_info = {
  1833. .name = TYPE_VIRT_MACHINE,
  1834. .parent = TYPE_MACHINE,
  1835. .abstract = true,
  1836. .instance_size = sizeof(VirtMachineState),
  1837. .class_size = sizeof(VirtMachineClass),
  1838. .class_init = virt_machine_class_init,
  1839. .instance_init = virt_instance_init,
  1840. .interfaces = (InterfaceInfo[]) {
  1841. { TYPE_HOTPLUG_HANDLER },
  1842. { }
  1843. },
  1844. };
  1845. static void machvirt_machine_init(void)
  1846. {
  1847. type_register_static(&virt_machine_info);
  1848. }
  1849. type_init(machvirt_machine_init);
  1850. static void virt_machine_4_2_options(MachineClass *mc)
  1851. {
  1852. }
  1853. DEFINE_VIRT_MACHINE_AS_LATEST(4, 2)
  1854. static void virt_machine_4_1_options(MachineClass *mc)
  1855. {
  1856. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1857. virt_machine_4_2_options(mc);
  1858. compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
  1859. vmc->no_ged = true;
  1860. mc->auto_enable_numa_with_memhp = false;
  1861. }
  1862. DEFINE_VIRT_MACHINE(4, 1)
  1863. static void virt_machine_4_0_options(MachineClass *mc)
  1864. {
  1865. virt_machine_4_1_options(mc);
  1866. compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
  1867. }
  1868. DEFINE_VIRT_MACHINE(4, 0)
  1869. static void virt_machine_3_1_options(MachineClass *mc)
  1870. {
  1871. virt_machine_4_0_options(mc);
  1872. compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
  1873. }
  1874. DEFINE_VIRT_MACHINE(3, 1)
  1875. static void virt_machine_3_0_options(MachineClass *mc)
  1876. {
  1877. virt_machine_3_1_options(mc);
  1878. compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
  1879. }
  1880. DEFINE_VIRT_MACHINE(3, 0)
  1881. static void virt_machine_2_12_options(MachineClass *mc)
  1882. {
  1883. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1884. virt_machine_3_0_options(mc);
  1885. compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
  1886. vmc->no_highmem_ecam = true;
  1887. mc->max_cpus = 255;
  1888. }
  1889. DEFINE_VIRT_MACHINE(2, 12)
  1890. static void virt_machine_2_11_options(MachineClass *mc)
  1891. {
  1892. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1893. virt_machine_2_12_options(mc);
  1894. compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
  1895. vmc->smbios_old_sys_ver = true;
  1896. }
  1897. DEFINE_VIRT_MACHINE(2, 11)
  1898. static void virt_machine_2_10_options(MachineClass *mc)
  1899. {
  1900. virt_machine_2_11_options(mc);
  1901. compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
  1902. /* before 2.11 we never faulted accesses to bad addresses */
  1903. mc->ignore_memory_transaction_failures = true;
  1904. }
  1905. DEFINE_VIRT_MACHINE(2, 10)
  1906. static void virt_machine_2_9_options(MachineClass *mc)
  1907. {
  1908. virt_machine_2_10_options(mc);
  1909. compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
  1910. }
  1911. DEFINE_VIRT_MACHINE(2, 9)
  1912. static void virt_machine_2_8_options(MachineClass *mc)
  1913. {
  1914. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1915. virt_machine_2_9_options(mc);
  1916. compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
  1917. /* For 2.8 and earlier we falsely claimed in the DT that
  1918. * our timers were edge-triggered, not level-triggered.
  1919. */
  1920. vmc->claim_edge_triggered_timers = true;
  1921. }
  1922. DEFINE_VIRT_MACHINE(2, 8)
  1923. static void virt_machine_2_7_options(MachineClass *mc)
  1924. {
  1925. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1926. virt_machine_2_8_options(mc);
  1927. compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
  1928. /* ITS was introduced with 2.8 */
  1929. vmc->no_its = true;
  1930. /* Stick with 1K pages for migration compatibility */
  1931. mc->minimum_page_bits = 0;
  1932. }
  1933. DEFINE_VIRT_MACHINE(2, 7)
  1934. static void virt_machine_2_6_options(MachineClass *mc)
  1935. {
  1936. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  1937. virt_machine_2_7_options(mc);
  1938. compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
  1939. vmc->disallow_affinity_adjustment = true;
  1940. /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
  1941. vmc->no_pmu = true;
  1942. }
  1943. DEFINE_VIRT_MACHINE(2, 6)