virt.c 97 KB

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