virt.c 79 KB

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