virt.c 79 KB

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