virt.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670
  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/datadir.h"
  32. #include "qemu/units.h"
  33. #include "qemu/option.h"
  34. #include "monitor/qdev.h"
  35. #include "hw/sysbus.h"
  36. #include "hw/arm/boot.h"
  37. #include "hw/arm/primecell.h"
  38. #include "hw/arm/virt.h"
  39. #include "hw/block/flash.h"
  40. #include "hw/vfio/vfio-calxeda-xgmac.h"
  41. #include "hw/vfio/vfio-amd-xgbe.h"
  42. #include "hw/display/ramfb.h"
  43. #include "net/net.h"
  44. #include "system/device_tree.h"
  45. #include "system/numa.h"
  46. #include "system/runstate.h"
  47. #include "system/tpm.h"
  48. #include "system/tcg.h"
  49. #include "system/kvm.h"
  50. #include "system/hvf.h"
  51. #include "system/qtest.h"
  52. #include "hw/loader.h"
  53. #include "qapi/error.h"
  54. #include "qemu/bitops.h"
  55. #include "qemu/cutils.h"
  56. #include "qemu/error-report.h"
  57. #include "qemu/module.h"
  58. #include "hw/pci-host/gpex.h"
  59. #include "hw/virtio/virtio-pci.h"
  60. #include "hw/core/sysbus-fdt.h"
  61. #include "hw/platform-bus.h"
  62. #include "hw/qdev-properties.h"
  63. #include "hw/arm/fdt.h"
  64. #include "hw/intc/arm_gic.h"
  65. #include "hw/intc/arm_gicv3_common.h"
  66. #include "hw/intc/arm_gicv3_its_common.h"
  67. #include "hw/irq.h"
  68. #include "kvm_arm.h"
  69. #include "hvf_arm.h"
  70. #include "hw/firmware/smbios.h"
  71. #include "qapi/visitor.h"
  72. #include "qapi/qapi-visit-common.h"
  73. #include "qobject/qlist.h"
  74. #include "standard-headers/linux/input.h"
  75. #include "hw/arm/smmuv3.h"
  76. #include "hw/acpi/acpi.h"
  77. #include "target/arm/cpu-qom.h"
  78. #include "target/arm/internals.h"
  79. #include "target/arm/multiprocessing.h"
  80. #include "target/arm/gtimer.h"
  81. #include "hw/mem/pc-dimm.h"
  82. #include "hw/mem/nvdimm.h"
  83. #include "hw/acpi/generic_event_device.h"
  84. #include "hw/virtio/virtio-md-pci.h"
  85. #include "hw/virtio/virtio-iommu.h"
  86. #include "hw/char/pl011.h"
  87. #include "qemu/guest-random.h"
  88. static GlobalProperty arm_virt_compat[] = {
  89. { TYPE_VIRTIO_IOMMU_PCI, "aw-bits", "48" },
  90. };
  91. static const size_t arm_virt_compat_len = G_N_ELEMENTS(arm_virt_compat);
  92. /*
  93. * This cannot be called from the virt_machine_class_init() because
  94. * TYPE_VIRT_MACHINE is abstract and mc->compat_props g_ptr_array_new()
  95. * only is called on virt non abstract class init.
  96. */
  97. static void arm_virt_compat_set(MachineClass *mc)
  98. {
  99. compat_props_add(mc->compat_props, arm_virt_compat,
  100. arm_virt_compat_len);
  101. }
  102. #define DEFINE_VIRT_MACHINE_IMPL(latest, ...) \
  103. static void MACHINE_VER_SYM(class_init, virt, __VA_ARGS__)( \
  104. ObjectClass *oc, \
  105. void *data) \
  106. { \
  107. MachineClass *mc = MACHINE_CLASS(oc); \
  108. arm_virt_compat_set(mc); \
  109. MACHINE_VER_SYM(options, virt, __VA_ARGS__)(mc); \
  110. mc->desc = "QEMU " MACHINE_VER_STR(__VA_ARGS__) " ARM Virtual Machine"; \
  111. MACHINE_VER_DEPRECATION(__VA_ARGS__); \
  112. if (latest) { \
  113. mc->alias = "virt"; \
  114. } \
  115. } \
  116. static const TypeInfo MACHINE_VER_SYM(info, virt, __VA_ARGS__) = \
  117. { \
  118. .name = MACHINE_VER_TYPE_NAME("virt", __VA_ARGS__), \
  119. .parent = TYPE_VIRT_MACHINE, \
  120. .class_init = MACHINE_VER_SYM(class_init, virt, __VA_ARGS__), \
  121. }; \
  122. static void MACHINE_VER_SYM(register, virt, __VA_ARGS__)(void) \
  123. { \
  124. MACHINE_VER_DELETION(__VA_ARGS__); \
  125. type_register_static(&MACHINE_VER_SYM(info, virt, __VA_ARGS__)); \
  126. } \
  127. type_init(MACHINE_VER_SYM(register, virt, __VA_ARGS__));
  128. #define DEFINE_VIRT_MACHINE_AS_LATEST(major, minor) \
  129. DEFINE_VIRT_MACHINE_IMPL(true, major, minor)
  130. #define DEFINE_VIRT_MACHINE(major, minor) \
  131. DEFINE_VIRT_MACHINE_IMPL(false, major, minor)
  132. /* Number of external interrupt lines to configure the GIC with */
  133. #define NUM_IRQS 256
  134. #define PLATFORM_BUS_NUM_IRQS 64
  135. /* Legacy RAM limit in GB (< version 4.0) */
  136. #define LEGACY_RAMLIMIT_GB 255
  137. #define LEGACY_RAMLIMIT_BYTES (LEGACY_RAMLIMIT_GB * GiB)
  138. /* Addresses and sizes of our components.
  139. * 0..128MB is space for a flash device so we can run bootrom code such as UEFI.
  140. * 128MB..256MB is used for miscellaneous device I/O.
  141. * 256MB..1GB is reserved for possible future PCI support (ie where the
  142. * PCI memory window will go if we add a PCI host controller).
  143. * 1GB and up is RAM (which may happily spill over into the
  144. * high memory region beyond 4GB).
  145. * This represents a compromise between how much RAM can be given to
  146. * a 32 bit VM and leaving space for expansion and in particular for PCI.
  147. * Note that devices should generally be placed at multiples of 0x10000,
  148. * to accommodate guests using 64K pages.
  149. */
  150. static const MemMapEntry base_memmap[] = {
  151. /* Space up to 0x8000000 is reserved for a boot ROM */
  152. [VIRT_FLASH] = { 0, 0x08000000 },
  153. [VIRT_CPUPERIPHS] = { 0x08000000, 0x00020000 },
  154. /* GIC distributor and CPU interfaces sit inside the CPU peripheral space */
  155. [VIRT_GIC_DIST] = { 0x08000000, 0x00010000 },
  156. [VIRT_GIC_CPU] = { 0x08010000, 0x00010000 },
  157. [VIRT_GIC_V2M] = { 0x08020000, 0x00001000 },
  158. [VIRT_GIC_HYP] = { 0x08030000, 0x00010000 },
  159. [VIRT_GIC_VCPU] = { 0x08040000, 0x00010000 },
  160. /* The space in between here is reserved for GICv3 CPU/vCPU/HYP */
  161. [VIRT_GIC_ITS] = { 0x08080000, 0x00020000 },
  162. /* This redistributor space allows up to 2*64kB*123 CPUs */
  163. [VIRT_GIC_REDIST] = { 0x080A0000, 0x00F60000 },
  164. [VIRT_UART0] = { 0x09000000, 0x00001000 },
  165. [VIRT_RTC] = { 0x09010000, 0x00001000 },
  166. [VIRT_FW_CFG] = { 0x09020000, 0x00000018 },
  167. [VIRT_GPIO] = { 0x09030000, 0x00001000 },
  168. [VIRT_UART1] = { 0x09040000, 0x00001000 },
  169. [VIRT_SMMU] = { 0x09050000, 0x00020000 },
  170. [VIRT_PCDIMM_ACPI] = { 0x09070000, MEMORY_HOTPLUG_IO_LEN },
  171. [VIRT_ACPI_GED] = { 0x09080000, ACPI_GED_EVT_SEL_LEN },
  172. [VIRT_NVDIMM_ACPI] = { 0x09090000, NVDIMM_ACPI_IO_LEN},
  173. [VIRT_PVTIME] = { 0x090a0000, 0x00010000 },
  174. [VIRT_SECURE_GPIO] = { 0x090b0000, 0x00001000 },
  175. [VIRT_MMIO] = { 0x0a000000, 0x00000200 },
  176. /* ...repeating for a total of NUM_VIRTIO_TRANSPORTS, each of that size */
  177. [VIRT_PLATFORM_BUS] = { 0x0c000000, 0x02000000 },
  178. [VIRT_SECURE_MEM] = { 0x0e000000, 0x01000000 },
  179. [VIRT_PCIE_MMIO] = { 0x10000000, 0x2eff0000 },
  180. [VIRT_PCIE_PIO] = { 0x3eff0000, 0x00010000 },
  181. [VIRT_PCIE_ECAM] = { 0x3f000000, 0x01000000 },
  182. /* Actual RAM size depends on initial RAM and device memory settings */
  183. [VIRT_MEM] = { GiB, LEGACY_RAMLIMIT_BYTES },
  184. };
  185. /* Update the docs for highmem-mmio-size when changing this default */
  186. #define DEFAULT_HIGH_PCIE_MMIO_SIZE_GB 512
  187. #define DEFAULT_HIGH_PCIE_MMIO_SIZE (DEFAULT_HIGH_PCIE_MMIO_SIZE_GB * GiB)
  188. /*
  189. * Highmem IO Regions: This memory map is floating, located after the RAM.
  190. * Each MemMapEntry base (GPA) will be dynamically computed, depending on the
  191. * top of the RAM, so that its base get the same alignment as the size,
  192. * ie. a 512GiB entry will be aligned on a 512GiB boundary. If there is
  193. * less than 256GiB of RAM, the floating area starts at the 256GiB mark.
  194. * Note the extended_memmap is sized so that it eventually also includes the
  195. * base_memmap entries (VIRT_HIGH_GIC_REDIST2 index is greater than the last
  196. * index of base_memmap).
  197. *
  198. * The memory map for these Highmem IO Regions can be in legacy or compact
  199. * layout, depending on 'compact-highmem' property. With legacy layout, the
  200. * PA space for one specific region is always reserved, even if the region
  201. * has been disabled or doesn't fit into the PA space. However, the PA space
  202. * for the region won't be reserved in these circumstances with compact layout.
  203. *
  204. * Note that the highmem-mmio-size property will update the high PCIE MMIO size
  205. * field in this array.
  206. */
  207. static MemMapEntry extended_memmap[] = {
  208. /* Additional 64 MB redist region (can contain up to 512 redistributors) */
  209. [VIRT_HIGH_GIC_REDIST2] = { 0x0, 64 * MiB },
  210. [VIRT_HIGH_PCIE_ECAM] = { 0x0, 256 * MiB },
  211. /* Second PCIe window */
  212. [VIRT_HIGH_PCIE_MMIO] = { 0x0, DEFAULT_HIGH_PCIE_MMIO_SIZE },
  213. };
  214. static const int a15irqmap[] = {
  215. [VIRT_UART0] = 1,
  216. [VIRT_RTC] = 2,
  217. [VIRT_PCIE] = 3, /* ... to 6 */
  218. [VIRT_GPIO] = 7,
  219. [VIRT_UART1] = 8,
  220. [VIRT_ACPI_GED] = 9,
  221. [VIRT_MMIO] = 16, /* ...to 16 + NUM_VIRTIO_TRANSPORTS - 1 */
  222. [VIRT_GIC_V2M] = 48, /* ...to 48 + NUM_GICV2M_SPIS - 1 */
  223. [VIRT_SMMU] = 74, /* ...to 74 + NUM_SMMU_IRQS - 1 */
  224. [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
  225. };
  226. static void create_randomness(MachineState *ms, const char *node)
  227. {
  228. struct {
  229. uint64_t kaslr;
  230. uint8_t rng[32];
  231. } seed;
  232. if (qemu_guest_getrandom(&seed, sizeof(seed), NULL)) {
  233. return;
  234. }
  235. qemu_fdt_setprop_u64(ms->fdt, node, "kaslr-seed", seed.kaslr);
  236. qemu_fdt_setprop(ms->fdt, node, "rng-seed", seed.rng, sizeof(seed.rng));
  237. }
  238. /*
  239. * The CPU object always exposes the NS EL2 virt timer IRQ line,
  240. * but we don't want to advertise it to the guest in the dtb or ACPI
  241. * table unless it's really going to do something.
  242. */
  243. static bool ns_el2_virt_timer_present(void)
  244. {
  245. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(0));
  246. CPUARMState *env = &cpu->env;
  247. return arm_feature(env, ARM_FEATURE_AARCH64) &&
  248. arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu);
  249. }
  250. static void create_fdt(VirtMachineState *vms)
  251. {
  252. MachineState *ms = MACHINE(vms);
  253. int nb_numa_nodes = ms->numa_state->num_nodes;
  254. void *fdt = create_device_tree(&vms->fdt_size);
  255. if (!fdt) {
  256. error_report("create_device_tree() failed");
  257. exit(1);
  258. }
  259. ms->fdt = fdt;
  260. /* Header */
  261. qemu_fdt_setprop_string(fdt, "/", "compatible", "linux,dummy-virt");
  262. qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2);
  263. qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2);
  264. qemu_fdt_setprop_string(fdt, "/", "model", "linux,dummy-virt");
  265. /*
  266. * For QEMU, all DMA is coherent. Advertising this in the root node
  267. * has two benefits:
  268. *
  269. * - It avoids potential bugs where we forget to mark a DMA
  270. * capable device as being dma-coherent
  271. * - It avoids spurious warnings from the Linux kernel about
  272. * devices which can't do DMA at all
  273. */
  274. qemu_fdt_setprop(fdt, "/", "dma-coherent", NULL, 0);
  275. /* /chosen must exist for load_dtb to fill in necessary properties later */
  276. qemu_fdt_add_subnode(fdt, "/chosen");
  277. if (vms->dtb_randomness) {
  278. create_randomness(ms, "/chosen");
  279. }
  280. if (vms->secure) {
  281. qemu_fdt_add_subnode(fdt, "/secure-chosen");
  282. if (vms->dtb_randomness) {
  283. create_randomness(ms, "/secure-chosen");
  284. }
  285. }
  286. qemu_fdt_add_subnode(fdt, "/aliases");
  287. /* Clock node, for the benefit of the UART. The kernel device tree
  288. * binding documentation claims the PL011 node clock properties are
  289. * optional but in practice if you omit them the kernel refuses to
  290. * probe for the device.
  291. */
  292. vms->clock_phandle = qemu_fdt_alloc_phandle(fdt);
  293. qemu_fdt_add_subnode(fdt, "/apb-pclk");
  294. qemu_fdt_setprop_string(fdt, "/apb-pclk", "compatible", "fixed-clock");
  295. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "#clock-cells", 0x0);
  296. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "clock-frequency", 24000000);
  297. qemu_fdt_setprop_string(fdt, "/apb-pclk", "clock-output-names",
  298. "clk24mhz");
  299. qemu_fdt_setprop_cell(fdt, "/apb-pclk", "phandle", vms->clock_phandle);
  300. if (nb_numa_nodes > 0 && ms->numa_state->have_numa_distance) {
  301. int size = nb_numa_nodes * nb_numa_nodes * 3 * sizeof(uint32_t);
  302. uint32_t *matrix = g_malloc0(size);
  303. int idx, i, j;
  304. for (i = 0; i < nb_numa_nodes; i++) {
  305. for (j = 0; j < nb_numa_nodes; j++) {
  306. idx = (i * nb_numa_nodes + j) * 3;
  307. matrix[idx + 0] = cpu_to_be32(i);
  308. matrix[idx + 1] = cpu_to_be32(j);
  309. matrix[idx + 2] =
  310. cpu_to_be32(ms->numa_state->nodes[i].distance[j]);
  311. }
  312. }
  313. qemu_fdt_add_subnode(fdt, "/distance-map");
  314. qemu_fdt_setprop_string(fdt, "/distance-map", "compatible",
  315. "numa-distance-map-v1");
  316. qemu_fdt_setprop(fdt, "/distance-map", "distance-matrix",
  317. matrix, size);
  318. g_free(matrix);
  319. }
  320. }
  321. static void fdt_add_timer_nodes(const VirtMachineState *vms)
  322. {
  323. /* On real hardware these interrupts are level-triggered.
  324. * On KVM they were edge-triggered before host kernel version 4.4,
  325. * and level-triggered afterwards.
  326. * On emulated QEMU they are level-triggered.
  327. *
  328. * Getting the DTB info about them wrong is awkward for some
  329. * guest kernels:
  330. * pre-4.8 ignore the DT and leave the interrupt configured
  331. * with whatever the GIC reset value (or the bootloader) left it at
  332. * 4.8 before rc6 honour the incorrect data by programming it back
  333. * into the GIC, causing problems
  334. * 4.8rc6 and later ignore the DT and always write "level triggered"
  335. * into the GIC
  336. *
  337. * For backwards-compatibility, virt-2.8 and earlier will continue
  338. * to say these are edge-triggered, but later machines will report
  339. * the correct information.
  340. */
  341. ARMCPU *armcpu;
  342. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  343. uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  344. MachineState *ms = MACHINE(vms);
  345. if (vmc->claim_edge_triggered_timers) {
  346. irqflags = GIC_FDT_IRQ_FLAGS_EDGE_LO_HI;
  347. }
  348. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  349. irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
  350. GIC_FDT_IRQ_PPI_CPU_WIDTH,
  351. (1 << MACHINE(vms)->smp.cpus) - 1);
  352. }
  353. qemu_fdt_add_subnode(ms->fdt, "/timer");
  354. armcpu = ARM_CPU(qemu_get_cpu(0));
  355. if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
  356. const char compat[] = "arm,armv8-timer\0arm,armv7-timer";
  357. qemu_fdt_setprop(ms->fdt, "/timer", "compatible",
  358. compat, sizeof(compat));
  359. } else {
  360. qemu_fdt_setprop_string(ms->fdt, "/timer", "compatible",
  361. "arm,armv7-timer");
  362. }
  363. qemu_fdt_setprop(ms->fdt, "/timer", "always-on", NULL, 0);
  364. if (vms->ns_el2_virt_timer_irq) {
  365. qemu_fdt_setprop_cells(ms->fdt, "/timer", "interrupts",
  366. GIC_FDT_IRQ_TYPE_PPI,
  367. INTID_TO_PPI(ARCH_TIMER_S_EL1_IRQ), irqflags,
  368. GIC_FDT_IRQ_TYPE_PPI,
  369. INTID_TO_PPI(ARCH_TIMER_NS_EL1_IRQ), irqflags,
  370. GIC_FDT_IRQ_TYPE_PPI,
  371. INTID_TO_PPI(ARCH_TIMER_VIRT_IRQ), irqflags,
  372. GIC_FDT_IRQ_TYPE_PPI,
  373. INTID_TO_PPI(ARCH_TIMER_NS_EL2_IRQ), irqflags,
  374. GIC_FDT_IRQ_TYPE_PPI,
  375. INTID_TO_PPI(ARCH_TIMER_NS_EL2_VIRT_IRQ), irqflags);
  376. } else {
  377. qemu_fdt_setprop_cells(ms->fdt, "/timer", "interrupts",
  378. GIC_FDT_IRQ_TYPE_PPI,
  379. INTID_TO_PPI(ARCH_TIMER_S_EL1_IRQ), irqflags,
  380. GIC_FDT_IRQ_TYPE_PPI,
  381. INTID_TO_PPI(ARCH_TIMER_NS_EL1_IRQ), irqflags,
  382. GIC_FDT_IRQ_TYPE_PPI,
  383. INTID_TO_PPI(ARCH_TIMER_VIRT_IRQ), irqflags,
  384. GIC_FDT_IRQ_TYPE_PPI,
  385. INTID_TO_PPI(ARCH_TIMER_NS_EL2_IRQ), irqflags);
  386. }
  387. }
  388. static void fdt_add_cpu_nodes(const VirtMachineState *vms)
  389. {
  390. int cpu;
  391. int addr_cells = 1;
  392. const MachineState *ms = MACHINE(vms);
  393. const VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  394. int smp_cpus = ms->smp.cpus;
  395. /*
  396. * See Linux Documentation/devicetree/bindings/arm/cpus.yaml
  397. * On ARM v8 64-bit systems value should be set to 2,
  398. * that corresponds to the MPIDR_EL1 register size.
  399. * If MPIDR_EL1[63:32] value is equal to 0 on all CPUs
  400. * in the system, #address-cells can be set to 1, since
  401. * MPIDR_EL1[63:32] bits are not used for CPUs
  402. * identification.
  403. *
  404. * Here we actually don't know whether our system is 32- or 64-bit one.
  405. * The simplest way to go is to examine affinity IDs of all our CPUs. If
  406. * at least one of them has Aff3 populated, we set #address-cells to 2.
  407. */
  408. for (cpu = 0; cpu < smp_cpus; cpu++) {
  409. ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
  410. if (arm_cpu_mp_affinity(armcpu) & ARM_AFF3_MASK) {
  411. addr_cells = 2;
  412. break;
  413. }
  414. }
  415. qemu_fdt_add_subnode(ms->fdt, "/cpus");
  416. qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#address-cells", addr_cells);
  417. qemu_fdt_setprop_cell(ms->fdt, "/cpus", "#size-cells", 0x0);
  418. for (cpu = smp_cpus - 1; cpu >= 0; cpu--) {
  419. char *nodename = g_strdup_printf("/cpus/cpu@%d", cpu);
  420. ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(cpu));
  421. CPUState *cs = CPU(armcpu);
  422. qemu_fdt_add_subnode(ms->fdt, nodename);
  423. qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "cpu");
  424. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
  425. armcpu->dtb_compatible);
  426. if (vms->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED && smp_cpus > 1) {
  427. qemu_fdt_setprop_string(ms->fdt, nodename,
  428. "enable-method", "psci");
  429. }
  430. if (addr_cells == 2) {
  431. qemu_fdt_setprop_u64(ms->fdt, nodename, "reg",
  432. arm_cpu_mp_affinity(armcpu));
  433. } else {
  434. qemu_fdt_setprop_cell(ms->fdt, nodename, "reg",
  435. arm_cpu_mp_affinity(armcpu));
  436. }
  437. if (ms->possible_cpus->cpus[cs->cpu_index].props.has_node_id) {
  438. qemu_fdt_setprop_cell(ms->fdt, nodename, "numa-node-id",
  439. ms->possible_cpus->cpus[cs->cpu_index].props.node_id);
  440. }
  441. if (!vmc->no_cpu_topology) {
  442. qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle",
  443. qemu_fdt_alloc_phandle(ms->fdt));
  444. }
  445. g_free(nodename);
  446. }
  447. if (!vmc->no_cpu_topology) {
  448. /*
  449. * Add vCPU topology description through fdt node cpu-map.
  450. *
  451. * See Linux Documentation/devicetree/bindings/cpu/cpu-topology.txt
  452. * In a SMP system, the hierarchy of CPUs can be defined through
  453. * four entities that are used to describe the layout of CPUs in
  454. * the system: socket/cluster/core/thread.
  455. *
  456. * A socket node represents the boundary of system physical package
  457. * and its child nodes must be one or more cluster nodes. A system
  458. * can contain several layers of clustering within a single physical
  459. * package and cluster nodes can be contained in parent cluster nodes.
  460. *
  461. * Note: currently we only support one layer of clustering within
  462. * each physical package.
  463. */
  464. qemu_fdt_add_subnode(ms->fdt, "/cpus/cpu-map");
  465. for (cpu = smp_cpus - 1; cpu >= 0; cpu--) {
  466. char *cpu_path = g_strdup_printf("/cpus/cpu@%d", cpu);
  467. char *map_path;
  468. if (ms->smp.threads > 1) {
  469. map_path = g_strdup_printf(
  470. "/cpus/cpu-map/socket%d/cluster%d/core%d/thread%d",
  471. cpu / (ms->smp.clusters * ms->smp.cores * ms->smp.threads),
  472. (cpu / (ms->smp.cores * ms->smp.threads)) % ms->smp.clusters,
  473. (cpu / ms->smp.threads) % ms->smp.cores,
  474. cpu % ms->smp.threads);
  475. } else {
  476. map_path = g_strdup_printf(
  477. "/cpus/cpu-map/socket%d/cluster%d/core%d",
  478. cpu / (ms->smp.clusters * ms->smp.cores),
  479. (cpu / ms->smp.cores) % ms->smp.clusters,
  480. cpu % ms->smp.cores);
  481. }
  482. qemu_fdt_add_path(ms->fdt, map_path);
  483. qemu_fdt_setprop_phandle(ms->fdt, map_path, "cpu", cpu_path);
  484. g_free(map_path);
  485. g_free(cpu_path);
  486. }
  487. }
  488. }
  489. static void fdt_add_its_gic_node(VirtMachineState *vms)
  490. {
  491. char *nodename;
  492. MachineState *ms = MACHINE(vms);
  493. vms->msi_phandle = qemu_fdt_alloc_phandle(ms->fdt);
  494. nodename = g_strdup_printf("/intc/its@%" PRIx64,
  495. vms->memmap[VIRT_GIC_ITS].base);
  496. qemu_fdt_add_subnode(ms->fdt, nodename);
  497. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
  498. "arm,gic-v3-its");
  499. qemu_fdt_setprop(ms->fdt, nodename, "msi-controller", NULL, 0);
  500. qemu_fdt_setprop_cell(ms->fdt, nodename, "#msi-cells", 1);
  501. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  502. 2, vms->memmap[VIRT_GIC_ITS].base,
  503. 2, vms->memmap[VIRT_GIC_ITS].size);
  504. qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->msi_phandle);
  505. g_free(nodename);
  506. }
  507. static void fdt_add_v2m_gic_node(VirtMachineState *vms)
  508. {
  509. MachineState *ms = MACHINE(vms);
  510. char *nodename;
  511. nodename = g_strdup_printf("/intc/v2m@%" PRIx64,
  512. vms->memmap[VIRT_GIC_V2M].base);
  513. vms->msi_phandle = qemu_fdt_alloc_phandle(ms->fdt);
  514. qemu_fdt_add_subnode(ms->fdt, nodename);
  515. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
  516. "arm,gic-v2m-frame");
  517. qemu_fdt_setprop(ms->fdt, nodename, "msi-controller", NULL, 0);
  518. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  519. 2, vms->memmap[VIRT_GIC_V2M].base,
  520. 2, vms->memmap[VIRT_GIC_V2M].size);
  521. qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->msi_phandle);
  522. g_free(nodename);
  523. }
  524. static void fdt_add_gic_node(VirtMachineState *vms)
  525. {
  526. MachineState *ms = MACHINE(vms);
  527. char *nodename;
  528. vms->gic_phandle = qemu_fdt_alloc_phandle(ms->fdt);
  529. qemu_fdt_setprop_cell(ms->fdt, "/", "interrupt-parent", vms->gic_phandle);
  530. nodename = g_strdup_printf("/intc@%" PRIx64,
  531. vms->memmap[VIRT_GIC_DIST].base);
  532. qemu_fdt_add_subnode(ms->fdt, nodename);
  533. qemu_fdt_setprop_cell(ms->fdt, nodename, "#interrupt-cells", 3);
  534. qemu_fdt_setprop(ms->fdt, nodename, "interrupt-controller", NULL, 0);
  535. qemu_fdt_setprop_cell(ms->fdt, nodename, "#address-cells", 0x2);
  536. qemu_fdt_setprop_cell(ms->fdt, nodename, "#size-cells", 0x2);
  537. qemu_fdt_setprop(ms->fdt, nodename, "ranges", NULL, 0);
  538. if (vms->gic_version != VIRT_GIC_VERSION_2) {
  539. int nb_redist_regions = virt_gicv3_redist_region_count(vms);
  540. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
  541. "arm,gic-v3");
  542. qemu_fdt_setprop_cell(ms->fdt, nodename,
  543. "#redistributor-regions", nb_redist_regions);
  544. if (nb_redist_regions == 1) {
  545. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  546. 2, vms->memmap[VIRT_GIC_DIST].base,
  547. 2, vms->memmap[VIRT_GIC_DIST].size,
  548. 2, vms->memmap[VIRT_GIC_REDIST].base,
  549. 2, vms->memmap[VIRT_GIC_REDIST].size);
  550. } else {
  551. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  552. 2, vms->memmap[VIRT_GIC_DIST].base,
  553. 2, vms->memmap[VIRT_GIC_DIST].size,
  554. 2, vms->memmap[VIRT_GIC_REDIST].base,
  555. 2, vms->memmap[VIRT_GIC_REDIST].size,
  556. 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].base,
  557. 2, vms->memmap[VIRT_HIGH_GIC_REDIST2].size);
  558. }
  559. if (vms->virt) {
  560. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  561. GIC_FDT_IRQ_TYPE_PPI,
  562. INTID_TO_PPI(ARCH_GIC_MAINT_IRQ),
  563. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  564. }
  565. } else {
  566. /* 'cortex-a15-gic' means 'GIC v2' */
  567. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible",
  568. "arm,cortex-a15-gic");
  569. if (!vms->virt) {
  570. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  571. 2, vms->memmap[VIRT_GIC_DIST].base,
  572. 2, vms->memmap[VIRT_GIC_DIST].size,
  573. 2, vms->memmap[VIRT_GIC_CPU].base,
  574. 2, vms->memmap[VIRT_GIC_CPU].size);
  575. } else {
  576. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  577. 2, vms->memmap[VIRT_GIC_DIST].base,
  578. 2, vms->memmap[VIRT_GIC_DIST].size,
  579. 2, vms->memmap[VIRT_GIC_CPU].base,
  580. 2, vms->memmap[VIRT_GIC_CPU].size,
  581. 2, vms->memmap[VIRT_GIC_HYP].base,
  582. 2, vms->memmap[VIRT_GIC_HYP].size,
  583. 2, vms->memmap[VIRT_GIC_VCPU].base,
  584. 2, vms->memmap[VIRT_GIC_VCPU].size);
  585. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  586. GIC_FDT_IRQ_TYPE_PPI,
  587. INTID_TO_PPI(ARCH_GIC_MAINT_IRQ),
  588. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  589. }
  590. }
  591. qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", vms->gic_phandle);
  592. g_free(nodename);
  593. }
  594. static void fdt_add_pmu_nodes(const VirtMachineState *vms)
  595. {
  596. ARMCPU *armcpu = ARM_CPU(first_cpu);
  597. uint32_t irqflags = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  598. MachineState *ms = MACHINE(vms);
  599. if (!arm_feature(&armcpu->env, ARM_FEATURE_PMU)) {
  600. assert(!object_property_get_bool(OBJECT(armcpu), "pmu", NULL));
  601. return;
  602. }
  603. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  604. irqflags = deposit32(irqflags, GIC_FDT_IRQ_PPI_CPU_START,
  605. GIC_FDT_IRQ_PPI_CPU_WIDTH,
  606. (1 << MACHINE(vms)->smp.cpus) - 1);
  607. }
  608. qemu_fdt_add_subnode(ms->fdt, "/pmu");
  609. if (arm_feature(&armcpu->env, ARM_FEATURE_V8)) {
  610. const char compat[] = "arm,armv8-pmuv3";
  611. qemu_fdt_setprop(ms->fdt, "/pmu", "compatible",
  612. compat, sizeof(compat));
  613. qemu_fdt_setprop_cells(ms->fdt, "/pmu", "interrupts",
  614. GIC_FDT_IRQ_TYPE_PPI,
  615. INTID_TO_PPI(VIRTUAL_PMU_IRQ), irqflags);
  616. }
  617. }
  618. static inline DeviceState *create_acpi_ged(VirtMachineState *vms)
  619. {
  620. DeviceState *dev;
  621. MachineState *ms = MACHINE(vms);
  622. int irq = vms->irqmap[VIRT_ACPI_GED];
  623. uint32_t event = ACPI_GED_PWR_DOWN_EVT;
  624. if (ms->ram_slots) {
  625. event |= ACPI_GED_MEM_HOTPLUG_EVT;
  626. }
  627. if (ms->nvdimms_state->is_enabled) {
  628. event |= ACPI_GED_NVDIMM_HOTPLUG_EVT;
  629. }
  630. dev = qdev_new(TYPE_ACPI_GED);
  631. qdev_prop_set_uint32(dev, "ged-event", event);
  632. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  633. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_ACPI_GED].base);
  634. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, vms->memmap[VIRT_PCDIMM_ACPI].base);
  635. sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, qdev_get_gpio_in(vms->gic, irq));
  636. return dev;
  637. }
  638. static void create_its(VirtMachineState *vms)
  639. {
  640. const char *itsclass = its_class_name();
  641. DeviceState *dev;
  642. if (!strcmp(itsclass, "arm-gicv3-its")) {
  643. if (!vms->tcg_its) {
  644. itsclass = NULL;
  645. }
  646. }
  647. if (!itsclass) {
  648. /* Do nothing if not supported */
  649. return;
  650. }
  651. dev = qdev_new(itsclass);
  652. object_property_set_link(OBJECT(dev), "parent-gicv3", OBJECT(vms->gic),
  653. &error_abort);
  654. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  655. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_ITS].base);
  656. fdt_add_its_gic_node(vms);
  657. vms->msi_controller = VIRT_MSI_CTRL_ITS;
  658. }
  659. static void create_v2m(VirtMachineState *vms)
  660. {
  661. int i;
  662. int irq = vms->irqmap[VIRT_GIC_V2M];
  663. DeviceState *dev;
  664. dev = qdev_new("arm-gicv2m");
  665. qdev_prop_set_uint32(dev, "base-spi", irq);
  666. qdev_prop_set_uint32(dev, "num-spi", NUM_GICV2M_SPIS);
  667. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  668. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, vms->memmap[VIRT_GIC_V2M].base);
  669. for (i = 0; i < NUM_GICV2M_SPIS; i++) {
  670. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  671. qdev_get_gpio_in(vms->gic, irq + i));
  672. }
  673. fdt_add_v2m_gic_node(vms);
  674. vms->msi_controller = VIRT_MSI_CTRL_GICV2M;
  675. }
  676. /*
  677. * If the CPU has FEAT_NMI, then turn on the NMI support in the GICv3 too.
  678. * It's permitted to have a configuration with NMI in the CPU (and thus the
  679. * GICv3 CPU interface) but not in the distributor/redistributors, but it's
  680. * not very useful.
  681. */
  682. static bool gicv3_nmi_present(VirtMachineState *vms)
  683. {
  684. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(0));
  685. return tcg_enabled() && cpu_isar_feature(aa64_nmi, cpu) &&
  686. (vms->gic_version != VIRT_GIC_VERSION_2);
  687. }
  688. static void create_gic(VirtMachineState *vms, MemoryRegion *mem)
  689. {
  690. MachineState *ms = MACHINE(vms);
  691. /* We create a standalone GIC */
  692. SysBusDevice *gicbusdev;
  693. const char *gictype;
  694. int i;
  695. unsigned int smp_cpus = ms->smp.cpus;
  696. uint32_t nb_redist_regions = 0;
  697. int revision;
  698. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  699. gictype = gic_class_name();
  700. } else {
  701. gictype = gicv3_class_name();
  702. }
  703. switch (vms->gic_version) {
  704. case VIRT_GIC_VERSION_2:
  705. revision = 2;
  706. break;
  707. case VIRT_GIC_VERSION_3:
  708. revision = 3;
  709. break;
  710. case VIRT_GIC_VERSION_4:
  711. revision = 4;
  712. break;
  713. default:
  714. g_assert_not_reached();
  715. }
  716. vms->gic = qdev_new(gictype);
  717. qdev_prop_set_uint32(vms->gic, "revision", revision);
  718. qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus);
  719. /* Note that the num-irq property counts both internal and external
  720. * interrupts; there are always 32 of the former (mandated by GIC spec).
  721. */
  722. qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32);
  723. if (!kvm_irqchip_in_kernel()) {
  724. qdev_prop_set_bit(vms->gic, "has-security-extensions", vms->secure);
  725. }
  726. if (vms->gic_version != VIRT_GIC_VERSION_2) {
  727. QList *redist_region_count;
  728. uint32_t redist0_capacity = virt_redist_capacity(vms, VIRT_GIC_REDIST);
  729. uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
  730. nb_redist_regions = virt_gicv3_redist_region_count(vms);
  731. redist_region_count = qlist_new();
  732. qlist_append_int(redist_region_count, redist0_count);
  733. if (nb_redist_regions == 2) {
  734. uint32_t redist1_capacity =
  735. virt_redist_capacity(vms, VIRT_HIGH_GIC_REDIST2);
  736. qlist_append_int(redist_region_count,
  737. MIN(smp_cpus - redist0_count, redist1_capacity));
  738. }
  739. qdev_prop_set_array(vms->gic, "redist-region-count",
  740. redist_region_count);
  741. if (!kvm_irqchip_in_kernel()) {
  742. if (vms->tcg_its) {
  743. object_property_set_link(OBJECT(vms->gic), "sysmem",
  744. OBJECT(mem), &error_fatal);
  745. qdev_prop_set_bit(vms->gic, "has-lpi", true);
  746. }
  747. }
  748. } else {
  749. if (!kvm_irqchip_in_kernel()) {
  750. qdev_prop_set_bit(vms->gic, "has-virtualization-extensions",
  751. vms->virt);
  752. }
  753. }
  754. if (gicv3_nmi_present(vms)) {
  755. qdev_prop_set_bit(vms->gic, "has-nmi", true);
  756. }
  757. gicbusdev = SYS_BUS_DEVICE(vms->gic);
  758. sysbus_realize_and_unref(gicbusdev, &error_fatal);
  759. sysbus_mmio_map(gicbusdev, 0, vms->memmap[VIRT_GIC_DIST].base);
  760. if (vms->gic_version != VIRT_GIC_VERSION_2) {
  761. sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_REDIST].base);
  762. if (nb_redist_regions == 2) {
  763. sysbus_mmio_map(gicbusdev, 2,
  764. vms->memmap[VIRT_HIGH_GIC_REDIST2].base);
  765. }
  766. } else {
  767. sysbus_mmio_map(gicbusdev, 1, vms->memmap[VIRT_GIC_CPU].base);
  768. if (vms->virt) {
  769. sysbus_mmio_map(gicbusdev, 2, vms->memmap[VIRT_GIC_HYP].base);
  770. sysbus_mmio_map(gicbusdev, 3, vms->memmap[VIRT_GIC_VCPU].base);
  771. }
  772. }
  773. /* Wire the outputs from each CPU's generic timer and the GICv3
  774. * maintenance interrupt signal to the appropriate GIC PPI inputs,
  775. * and the GIC's IRQ/FIQ/VIRQ/VFIQ/NMI/VINMI interrupt outputs to the
  776. * CPU's inputs.
  777. */
  778. for (i = 0; i < smp_cpus; i++) {
  779. DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
  780. int intidbase = NUM_IRQS + i * GIC_INTERNAL;
  781. /* Mapping from the output timer irq lines from the CPU to the
  782. * GIC PPI inputs we use for the virt board.
  783. */
  784. const int timer_irq[] = {
  785. [GTIMER_PHYS] = ARCH_TIMER_NS_EL1_IRQ,
  786. [GTIMER_VIRT] = ARCH_TIMER_VIRT_IRQ,
  787. [GTIMER_HYP] = ARCH_TIMER_NS_EL2_IRQ,
  788. [GTIMER_SEC] = ARCH_TIMER_S_EL1_IRQ,
  789. [GTIMER_HYPVIRT] = ARCH_TIMER_NS_EL2_VIRT_IRQ,
  790. };
  791. for (unsigned irq = 0; irq < ARRAY_SIZE(timer_irq); irq++) {
  792. qdev_connect_gpio_out(cpudev, irq,
  793. qdev_get_gpio_in(vms->gic,
  794. intidbase + timer_irq[irq]));
  795. }
  796. if (vms->gic_version != VIRT_GIC_VERSION_2) {
  797. qemu_irq irq = qdev_get_gpio_in(vms->gic,
  798. intidbase + ARCH_GIC_MAINT_IRQ);
  799. qdev_connect_gpio_out_named(cpudev, "gicv3-maintenance-interrupt",
  800. 0, irq);
  801. } else if (vms->virt) {
  802. qemu_irq irq = qdev_get_gpio_in(vms->gic,
  803. intidbase + ARCH_GIC_MAINT_IRQ);
  804. sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus, irq);
  805. }
  806. qdev_connect_gpio_out_named(cpudev, "pmu-interrupt", 0,
  807. qdev_get_gpio_in(vms->gic, intidbase
  808. + VIRTUAL_PMU_IRQ));
  809. sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
  810. sysbus_connect_irq(gicbusdev, i + smp_cpus,
  811. qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
  812. sysbus_connect_irq(gicbusdev, i + 2 * smp_cpus,
  813. qdev_get_gpio_in(cpudev, ARM_CPU_VIRQ));
  814. sysbus_connect_irq(gicbusdev, i + 3 * smp_cpus,
  815. qdev_get_gpio_in(cpudev, ARM_CPU_VFIQ));
  816. if (vms->gic_version != VIRT_GIC_VERSION_2) {
  817. sysbus_connect_irq(gicbusdev, i + 4 * smp_cpus,
  818. qdev_get_gpio_in(cpudev, ARM_CPU_NMI));
  819. sysbus_connect_irq(gicbusdev, i + 5 * smp_cpus,
  820. qdev_get_gpio_in(cpudev, ARM_CPU_VINMI));
  821. }
  822. }
  823. fdt_add_gic_node(vms);
  824. if (vms->gic_version != VIRT_GIC_VERSION_2 && vms->its) {
  825. create_its(vms);
  826. } else if (vms->gic_version == VIRT_GIC_VERSION_2) {
  827. create_v2m(vms);
  828. }
  829. }
  830. static void create_uart(const VirtMachineState *vms, int uart,
  831. MemoryRegion *mem, Chardev *chr, bool secure)
  832. {
  833. char *nodename;
  834. hwaddr base = vms->memmap[uart].base;
  835. hwaddr size = vms->memmap[uart].size;
  836. int irq = vms->irqmap[uart];
  837. const char compat[] = "arm,pl011\0arm,primecell";
  838. const char clocknames[] = "uartclk\0apb_pclk";
  839. DeviceState *dev = qdev_new(TYPE_PL011);
  840. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  841. MachineState *ms = MACHINE(vms);
  842. qdev_prop_set_chr(dev, "chardev", chr);
  843. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  844. memory_region_add_subregion(mem, base,
  845. sysbus_mmio_get_region(s, 0));
  846. sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
  847. nodename = g_strdup_printf("/pl011@%" PRIx64, base);
  848. qemu_fdt_add_subnode(ms->fdt, nodename);
  849. /* Note that we can't use setprop_string because of the embedded NUL */
  850. qemu_fdt_setprop(ms->fdt, nodename, "compatible",
  851. compat, sizeof(compat));
  852. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  853. 2, base, 2, size);
  854. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  855. GIC_FDT_IRQ_TYPE_SPI, irq,
  856. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  857. qemu_fdt_setprop_cells(ms->fdt, nodename, "clocks",
  858. vms->clock_phandle, vms->clock_phandle);
  859. qemu_fdt_setprop(ms->fdt, nodename, "clock-names",
  860. clocknames, sizeof(clocknames));
  861. if (uart == VIRT_UART0) {
  862. qemu_fdt_setprop_string(ms->fdt, "/chosen", "stdout-path", nodename);
  863. qemu_fdt_setprop_string(ms->fdt, "/aliases", "serial0", nodename);
  864. } else {
  865. qemu_fdt_setprop_string(ms->fdt, "/aliases", "serial1", nodename);
  866. }
  867. if (secure) {
  868. /* Mark as not usable by the normal world */
  869. qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled");
  870. qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay");
  871. qemu_fdt_setprop_string(ms->fdt, "/secure-chosen", "stdout-path",
  872. nodename);
  873. }
  874. g_free(nodename);
  875. }
  876. static void create_rtc(const VirtMachineState *vms)
  877. {
  878. char *nodename;
  879. hwaddr base = vms->memmap[VIRT_RTC].base;
  880. hwaddr size = vms->memmap[VIRT_RTC].size;
  881. int irq = vms->irqmap[VIRT_RTC];
  882. const char compat[] = "arm,pl031\0arm,primecell";
  883. MachineState *ms = MACHINE(vms);
  884. sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq));
  885. nodename = g_strdup_printf("/pl031@%" PRIx64, base);
  886. qemu_fdt_add_subnode(ms->fdt, nodename);
  887. qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat));
  888. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  889. 2, base, 2, size);
  890. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  891. GIC_FDT_IRQ_TYPE_SPI, irq,
  892. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  893. qemu_fdt_setprop_cell(ms->fdt, nodename, "clocks", vms->clock_phandle);
  894. qemu_fdt_setprop_string(ms->fdt, nodename, "clock-names", "apb_pclk");
  895. g_free(nodename);
  896. }
  897. static DeviceState *gpio_key_dev;
  898. static void virt_powerdown_req(Notifier *n, void *opaque)
  899. {
  900. VirtMachineState *s = container_of(n, VirtMachineState, powerdown_notifier);
  901. if (s->acpi_dev) {
  902. acpi_send_event(s->acpi_dev, ACPI_POWER_DOWN_STATUS);
  903. } else {
  904. /* use gpio Pin for power button event */
  905. qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
  906. }
  907. }
  908. static void create_gpio_keys(char *fdt, DeviceState *pl061_dev,
  909. uint32_t phandle)
  910. {
  911. gpio_key_dev = sysbus_create_simple("gpio-key", -1,
  912. qdev_get_gpio_in(pl061_dev,
  913. GPIO_PIN_POWER_BUTTON));
  914. qemu_fdt_add_subnode(fdt, "/gpio-keys");
  915. qemu_fdt_setprop_string(fdt, "/gpio-keys", "compatible", "gpio-keys");
  916. qemu_fdt_add_subnode(fdt, "/gpio-keys/poweroff");
  917. qemu_fdt_setprop_string(fdt, "/gpio-keys/poweroff",
  918. "label", "GPIO Key Poweroff");
  919. qemu_fdt_setprop_cell(fdt, "/gpio-keys/poweroff", "linux,code",
  920. KEY_POWER);
  921. qemu_fdt_setprop_cells(fdt, "/gpio-keys/poweroff",
  922. "gpios", phandle, GPIO_PIN_POWER_BUTTON, 0);
  923. }
  924. #define SECURE_GPIO_POWEROFF 0
  925. #define SECURE_GPIO_RESET 1
  926. static void create_secure_gpio_pwr(char *fdt, DeviceState *pl061_dev,
  927. uint32_t phandle)
  928. {
  929. DeviceState *gpio_pwr_dev;
  930. /* gpio-pwr */
  931. gpio_pwr_dev = sysbus_create_simple("gpio-pwr", -1, NULL);
  932. /* connect secure pl061 to gpio-pwr */
  933. qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_RESET,
  934. qdev_get_gpio_in_named(gpio_pwr_dev, "reset", 0));
  935. qdev_connect_gpio_out(pl061_dev, SECURE_GPIO_POWEROFF,
  936. qdev_get_gpio_in_named(gpio_pwr_dev, "shutdown", 0));
  937. qemu_fdt_add_subnode(fdt, "/gpio-poweroff");
  938. qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "compatible",
  939. "gpio-poweroff");
  940. qemu_fdt_setprop_cells(fdt, "/gpio-poweroff",
  941. "gpios", phandle, SECURE_GPIO_POWEROFF, 0);
  942. qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "status", "disabled");
  943. qemu_fdt_setprop_string(fdt, "/gpio-poweroff", "secure-status",
  944. "okay");
  945. qemu_fdt_add_subnode(fdt, "/gpio-restart");
  946. qemu_fdt_setprop_string(fdt, "/gpio-restart", "compatible",
  947. "gpio-restart");
  948. qemu_fdt_setprop_cells(fdt, "/gpio-restart",
  949. "gpios", phandle, SECURE_GPIO_RESET, 0);
  950. qemu_fdt_setprop_string(fdt, "/gpio-restart", "status", "disabled");
  951. qemu_fdt_setprop_string(fdt, "/gpio-restart", "secure-status",
  952. "okay");
  953. }
  954. static void create_gpio_devices(const VirtMachineState *vms, int gpio,
  955. MemoryRegion *mem)
  956. {
  957. char *nodename;
  958. DeviceState *pl061_dev;
  959. hwaddr base = vms->memmap[gpio].base;
  960. hwaddr size = vms->memmap[gpio].size;
  961. int irq = vms->irqmap[gpio];
  962. const char compat[] = "arm,pl061\0arm,primecell";
  963. SysBusDevice *s;
  964. MachineState *ms = MACHINE(vms);
  965. pl061_dev = qdev_new("pl061");
  966. /* Pull lines down to 0 if not driven by the PL061 */
  967. qdev_prop_set_uint32(pl061_dev, "pullups", 0);
  968. qdev_prop_set_uint32(pl061_dev, "pulldowns", 0xff);
  969. s = SYS_BUS_DEVICE(pl061_dev);
  970. sysbus_realize_and_unref(s, &error_fatal);
  971. memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0));
  972. sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
  973. uint32_t phandle = qemu_fdt_alloc_phandle(ms->fdt);
  974. nodename = g_strdup_printf("/pl061@%" PRIx64, base);
  975. qemu_fdt_add_subnode(ms->fdt, nodename);
  976. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  977. 2, base, 2, size);
  978. qemu_fdt_setprop(ms->fdt, nodename, "compatible", compat, sizeof(compat));
  979. qemu_fdt_setprop_cell(ms->fdt, nodename, "#gpio-cells", 2);
  980. qemu_fdt_setprop(ms->fdt, nodename, "gpio-controller", NULL, 0);
  981. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  982. GIC_FDT_IRQ_TYPE_SPI, irq,
  983. GIC_FDT_IRQ_FLAGS_LEVEL_HI);
  984. qemu_fdt_setprop_cell(ms->fdt, nodename, "clocks", vms->clock_phandle);
  985. qemu_fdt_setprop_string(ms->fdt, nodename, "clock-names", "apb_pclk");
  986. qemu_fdt_setprop_cell(ms->fdt, nodename, "phandle", phandle);
  987. if (gpio != VIRT_GPIO) {
  988. /* Mark as not usable by the normal world */
  989. qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled");
  990. qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay");
  991. }
  992. g_free(nodename);
  993. /* Child gpio devices */
  994. if (gpio == VIRT_GPIO) {
  995. create_gpio_keys(ms->fdt, pl061_dev, phandle);
  996. } else {
  997. create_secure_gpio_pwr(ms->fdt, pl061_dev, phandle);
  998. }
  999. }
  1000. static void create_virtio_devices(const VirtMachineState *vms)
  1001. {
  1002. int i;
  1003. hwaddr size = vms->memmap[VIRT_MMIO].size;
  1004. MachineState *ms = MACHINE(vms);
  1005. /* We create the transports in forwards order. Since qbus_realize()
  1006. * prepends (not appends) new child buses, the incrementing loop below will
  1007. * create a list of virtio-mmio buses with decreasing base addresses.
  1008. *
  1009. * When a -device option is processed from the command line,
  1010. * qbus_find_recursive() picks the next free virtio-mmio bus in forwards
  1011. * order. The upshot is that -device options in increasing command line
  1012. * order are mapped to virtio-mmio buses with decreasing base addresses.
  1013. *
  1014. * When this code was originally written, that arrangement ensured that the
  1015. * guest Linux kernel would give the lowest "name" (/dev/vda, eth0, etc) to
  1016. * the first -device on the command line. (The end-to-end order is a
  1017. * function of this loop, qbus_realize(), qbus_find_recursive(), and the
  1018. * guest kernel's name-to-address assignment strategy.)
  1019. *
  1020. * Meanwhile, the kernel's traversal seems to have been reversed; see eg.
  1021. * the message, if not necessarily the code, of commit 70161ff336.
  1022. * Therefore the loop now establishes the inverse of the original intent.
  1023. *
  1024. * Unfortunately, we can't counteract the kernel change by reversing the
  1025. * loop; it would break existing command lines.
  1026. *
  1027. * In any case, the kernel makes no guarantee about the stability of
  1028. * enumeration order of virtio devices (as demonstrated by it changing
  1029. * between kernel versions). For reliable and stable identification
  1030. * of disks users must use UUIDs or similar mechanisms.
  1031. */
  1032. for (i = 0; i < NUM_VIRTIO_TRANSPORTS; i++) {
  1033. int irq = vms->irqmap[VIRT_MMIO] + i;
  1034. hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
  1035. sysbus_create_simple("virtio-mmio", base,
  1036. qdev_get_gpio_in(vms->gic, irq));
  1037. }
  1038. /* We add dtb nodes in reverse order so that they appear in the finished
  1039. * device tree lowest address first.
  1040. *
  1041. * Note that this mapping is independent of the loop above. The previous
  1042. * loop influences virtio device to virtio transport assignment, whereas
  1043. * this loop controls how virtio transports are laid out in the dtb.
  1044. */
  1045. for (i = NUM_VIRTIO_TRANSPORTS - 1; i >= 0; i--) {
  1046. char *nodename;
  1047. int irq = vms->irqmap[VIRT_MMIO] + i;
  1048. hwaddr base = vms->memmap[VIRT_MMIO].base + i * size;
  1049. nodename = g_strdup_printf("/virtio_mmio@%" PRIx64, base);
  1050. qemu_fdt_add_subnode(ms->fdt, nodename);
  1051. qemu_fdt_setprop_string(ms->fdt, nodename,
  1052. "compatible", "virtio,mmio");
  1053. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1054. 2, base, 2, size);
  1055. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupts",
  1056. GIC_FDT_IRQ_TYPE_SPI, irq,
  1057. GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
  1058. qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0);
  1059. g_free(nodename);
  1060. }
  1061. }
  1062. #define VIRT_FLASH_SECTOR_SIZE (256 * KiB)
  1063. static PFlashCFI01 *virt_flash_create1(VirtMachineState *vms,
  1064. const char *name,
  1065. const char *alias_prop_name)
  1066. {
  1067. /*
  1068. * Create a single flash device. We use the same parameters as
  1069. * the flash devices on the Versatile Express board.
  1070. */
  1071. DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
  1072. qdev_prop_set_uint64(dev, "sector-length", VIRT_FLASH_SECTOR_SIZE);
  1073. qdev_prop_set_uint8(dev, "width", 4);
  1074. qdev_prop_set_uint8(dev, "device-width", 2);
  1075. qdev_prop_set_bit(dev, "big-endian", false);
  1076. qdev_prop_set_uint16(dev, "id0", 0x89);
  1077. qdev_prop_set_uint16(dev, "id1", 0x18);
  1078. qdev_prop_set_uint16(dev, "id2", 0x00);
  1079. qdev_prop_set_uint16(dev, "id3", 0x00);
  1080. qdev_prop_set_string(dev, "name", name);
  1081. object_property_add_child(OBJECT(vms), name, OBJECT(dev));
  1082. object_property_add_alias(OBJECT(vms), alias_prop_name,
  1083. OBJECT(dev), "drive");
  1084. return PFLASH_CFI01(dev);
  1085. }
  1086. static void virt_flash_create(VirtMachineState *vms)
  1087. {
  1088. vms->flash[0] = virt_flash_create1(vms, "virt.flash0", "pflash0");
  1089. vms->flash[1] = virt_flash_create1(vms, "virt.flash1", "pflash1");
  1090. }
  1091. static void virt_flash_map1(PFlashCFI01 *flash,
  1092. hwaddr base, hwaddr size,
  1093. MemoryRegion *sysmem)
  1094. {
  1095. DeviceState *dev = DEVICE(flash);
  1096. assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE));
  1097. assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
  1098. qdev_prop_set_uint32(dev, "num-blocks", size / VIRT_FLASH_SECTOR_SIZE);
  1099. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  1100. memory_region_add_subregion(sysmem, base,
  1101. sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
  1102. 0));
  1103. }
  1104. static void virt_flash_map(VirtMachineState *vms,
  1105. MemoryRegion *sysmem,
  1106. MemoryRegion *secure_sysmem)
  1107. {
  1108. /*
  1109. * Map two flash devices to fill the VIRT_FLASH space in the memmap.
  1110. * sysmem is the system memory space. secure_sysmem is the secure view
  1111. * of the system, and the first flash device should be made visible only
  1112. * there. The second flash device is visible to both secure and nonsecure.
  1113. * If sysmem == secure_sysmem this means there is no separate Secure
  1114. * address space and both flash devices are generally visible.
  1115. */
  1116. hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
  1117. hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
  1118. virt_flash_map1(vms->flash[0], flashbase, flashsize,
  1119. secure_sysmem);
  1120. virt_flash_map1(vms->flash[1], flashbase + flashsize, flashsize,
  1121. sysmem);
  1122. }
  1123. static void virt_flash_fdt(VirtMachineState *vms,
  1124. MemoryRegion *sysmem,
  1125. MemoryRegion *secure_sysmem)
  1126. {
  1127. hwaddr flashsize = vms->memmap[VIRT_FLASH].size / 2;
  1128. hwaddr flashbase = vms->memmap[VIRT_FLASH].base;
  1129. MachineState *ms = MACHINE(vms);
  1130. char *nodename;
  1131. if (sysmem == secure_sysmem) {
  1132. /* Report both flash devices as a single node in the DT */
  1133. nodename = g_strdup_printf("/flash@%" PRIx64, flashbase);
  1134. qemu_fdt_add_subnode(ms->fdt, nodename);
  1135. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash");
  1136. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1137. 2, flashbase, 2, flashsize,
  1138. 2, flashbase + flashsize, 2, flashsize);
  1139. qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4);
  1140. g_free(nodename);
  1141. } else {
  1142. /*
  1143. * Report the devices as separate nodes so we can mark one as
  1144. * only visible to the secure world.
  1145. */
  1146. nodename = g_strdup_printf("/secflash@%" PRIx64, flashbase);
  1147. qemu_fdt_add_subnode(ms->fdt, nodename);
  1148. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash");
  1149. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1150. 2, flashbase, 2, flashsize);
  1151. qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4);
  1152. qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled");
  1153. qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay");
  1154. g_free(nodename);
  1155. nodename = g_strdup_printf("/flash@%" PRIx64, flashbase + flashsize);
  1156. qemu_fdt_add_subnode(ms->fdt, nodename);
  1157. qemu_fdt_setprop_string(ms->fdt, nodename, "compatible", "cfi-flash");
  1158. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1159. 2, flashbase + flashsize, 2, flashsize);
  1160. qemu_fdt_setprop_cell(ms->fdt, nodename, "bank-width", 4);
  1161. g_free(nodename);
  1162. }
  1163. }
  1164. static bool virt_firmware_init(VirtMachineState *vms,
  1165. MemoryRegion *sysmem,
  1166. MemoryRegion *secure_sysmem)
  1167. {
  1168. int i;
  1169. const char *bios_name;
  1170. BlockBackend *pflash_blk0;
  1171. /* Map legacy -drive if=pflash to machine properties */
  1172. for (i = 0; i < ARRAY_SIZE(vms->flash); i++) {
  1173. pflash_cfi01_legacy_drive(vms->flash[i],
  1174. drive_get(IF_PFLASH, 0, i));
  1175. }
  1176. virt_flash_map(vms, sysmem, secure_sysmem);
  1177. pflash_blk0 = pflash_cfi01_get_blk(vms->flash[0]);
  1178. bios_name = MACHINE(vms)->firmware;
  1179. if (bios_name) {
  1180. char *fname;
  1181. MemoryRegion *mr;
  1182. int image_size;
  1183. if (pflash_blk0) {
  1184. error_report("The contents of the first flash device may be "
  1185. "specified with -bios or with -drive if=pflash... "
  1186. "but you cannot use both options at once");
  1187. exit(1);
  1188. }
  1189. /* Fall back to -bios */
  1190. fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  1191. if (!fname) {
  1192. error_report("Could not find ROM image '%s'", bios_name);
  1193. exit(1);
  1194. }
  1195. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(vms->flash[0]), 0);
  1196. image_size = load_image_mr(fname, mr);
  1197. g_free(fname);
  1198. if (image_size < 0) {
  1199. error_report("Could not load ROM image '%s'", bios_name);
  1200. exit(1);
  1201. }
  1202. }
  1203. return pflash_blk0 || bios_name;
  1204. }
  1205. static FWCfgState *create_fw_cfg(const VirtMachineState *vms, AddressSpace *as)
  1206. {
  1207. MachineState *ms = MACHINE(vms);
  1208. hwaddr base = vms->memmap[VIRT_FW_CFG].base;
  1209. hwaddr size = vms->memmap[VIRT_FW_CFG].size;
  1210. FWCfgState *fw_cfg;
  1211. char *nodename;
  1212. fw_cfg = fw_cfg_init_mem_wide(base + 8, base, 8, base + 16, as);
  1213. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, (uint16_t)ms->smp.cpus);
  1214. nodename = g_strdup_printf("/fw-cfg@%" PRIx64, base);
  1215. qemu_fdt_add_subnode(ms->fdt, nodename);
  1216. qemu_fdt_setprop_string(ms->fdt, nodename,
  1217. "compatible", "qemu,fw-cfg-mmio");
  1218. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1219. 2, base, 2, size);
  1220. qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0);
  1221. g_free(nodename);
  1222. return fw_cfg;
  1223. }
  1224. static void create_pcie_irq_map(const MachineState *ms,
  1225. uint32_t gic_phandle,
  1226. int first_irq, const char *nodename)
  1227. {
  1228. int devfn, pin;
  1229. uint32_t full_irq_map[4 * 4 * 10] = { 0 };
  1230. uint32_t *irq_map = full_irq_map;
  1231. for (devfn = 0; devfn <= 0x18; devfn += 0x8) {
  1232. for (pin = 0; pin < 4; pin++) {
  1233. int irq_type = GIC_FDT_IRQ_TYPE_SPI;
  1234. int irq_nr = first_irq + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
  1235. int irq_level = GIC_FDT_IRQ_FLAGS_LEVEL_HI;
  1236. int i;
  1237. uint32_t map[] = {
  1238. devfn << 8, 0, 0, /* devfn */
  1239. pin + 1, /* PCI pin */
  1240. gic_phandle, 0, 0, irq_type, irq_nr, irq_level }; /* GIC irq */
  1241. /* Convert map to big endian */
  1242. for (i = 0; i < 10; i++) {
  1243. irq_map[i] = cpu_to_be32(map[i]);
  1244. }
  1245. irq_map += 10;
  1246. }
  1247. }
  1248. qemu_fdt_setprop(ms->fdt, nodename, "interrupt-map",
  1249. full_irq_map, sizeof(full_irq_map));
  1250. qemu_fdt_setprop_cells(ms->fdt, nodename, "interrupt-map-mask",
  1251. cpu_to_be16(PCI_DEVFN(3, 0)), /* Slot 3 */
  1252. 0, 0,
  1253. 0x7 /* PCI irq */);
  1254. }
  1255. static void create_smmu(const VirtMachineState *vms,
  1256. PCIBus *bus)
  1257. {
  1258. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1259. char *node;
  1260. const char compat[] = "arm,smmu-v3";
  1261. int irq = vms->irqmap[VIRT_SMMU];
  1262. int i;
  1263. hwaddr base = vms->memmap[VIRT_SMMU].base;
  1264. hwaddr size = vms->memmap[VIRT_SMMU].size;
  1265. const char irq_names[] = "eventq\0priq\0cmdq-sync\0gerror";
  1266. DeviceState *dev;
  1267. MachineState *ms = MACHINE(vms);
  1268. if (vms->iommu != VIRT_IOMMU_SMMUV3 || !vms->iommu_phandle) {
  1269. return;
  1270. }
  1271. dev = qdev_new(TYPE_ARM_SMMUV3);
  1272. if (!vmc->no_nested_smmu) {
  1273. object_property_set_str(OBJECT(dev), "stage", "nested", &error_fatal);
  1274. }
  1275. object_property_set_link(OBJECT(dev), "primary-bus", OBJECT(bus),
  1276. &error_abort);
  1277. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  1278. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
  1279. for (i = 0; i < NUM_SMMU_IRQS; i++) {
  1280. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  1281. qdev_get_gpio_in(vms->gic, irq + i));
  1282. }
  1283. node = g_strdup_printf("/smmuv3@%" PRIx64, base);
  1284. qemu_fdt_add_subnode(ms->fdt, node);
  1285. qemu_fdt_setprop(ms->fdt, node, "compatible", compat, sizeof(compat));
  1286. qemu_fdt_setprop_sized_cells(ms->fdt, node, "reg", 2, base, 2, size);
  1287. qemu_fdt_setprop_cells(ms->fdt, node, "interrupts",
  1288. GIC_FDT_IRQ_TYPE_SPI, irq , GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1289. GIC_FDT_IRQ_TYPE_SPI, irq + 1, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1290. GIC_FDT_IRQ_TYPE_SPI, irq + 2, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI,
  1291. GIC_FDT_IRQ_TYPE_SPI, irq + 3, GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
  1292. qemu_fdt_setprop(ms->fdt, node, "interrupt-names", irq_names,
  1293. sizeof(irq_names));
  1294. qemu_fdt_setprop(ms->fdt, node, "dma-coherent", NULL, 0);
  1295. qemu_fdt_setprop_cell(ms->fdt, node, "#iommu-cells", 1);
  1296. qemu_fdt_setprop_cell(ms->fdt, node, "phandle", vms->iommu_phandle);
  1297. g_free(node);
  1298. }
  1299. static void create_virtio_iommu_dt_bindings(VirtMachineState *vms)
  1300. {
  1301. const char compat[] = "virtio,pci-iommu\0pci1af4,1057";
  1302. uint16_t bdf = vms->virtio_iommu_bdf;
  1303. MachineState *ms = MACHINE(vms);
  1304. char *node;
  1305. vms->iommu_phandle = qemu_fdt_alloc_phandle(ms->fdt);
  1306. node = g_strdup_printf("%s/virtio_iommu@%x,%x", vms->pciehb_nodename,
  1307. PCI_SLOT(bdf), PCI_FUNC(bdf));
  1308. qemu_fdt_add_subnode(ms->fdt, node);
  1309. qemu_fdt_setprop(ms->fdt, node, "compatible", compat, sizeof(compat));
  1310. qemu_fdt_setprop_sized_cells(ms->fdt, node, "reg",
  1311. 1, bdf << 8, 1, 0, 1, 0,
  1312. 1, 0, 1, 0);
  1313. qemu_fdt_setprop_cell(ms->fdt, node, "#iommu-cells", 1);
  1314. qemu_fdt_setprop_cell(ms->fdt, node, "phandle", vms->iommu_phandle);
  1315. g_free(node);
  1316. qemu_fdt_setprop_cells(ms->fdt, vms->pciehb_nodename, "iommu-map",
  1317. 0x0, vms->iommu_phandle, 0x0, bdf,
  1318. bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf);
  1319. }
  1320. static void create_pcie(VirtMachineState *vms)
  1321. {
  1322. hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
  1323. hwaddr size_mmio = vms->memmap[VIRT_PCIE_MMIO].size;
  1324. hwaddr base_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].base;
  1325. hwaddr size_mmio_high = vms->memmap[VIRT_HIGH_PCIE_MMIO].size;
  1326. hwaddr base_pio = vms->memmap[VIRT_PCIE_PIO].base;
  1327. hwaddr size_pio = vms->memmap[VIRT_PCIE_PIO].size;
  1328. hwaddr base_ecam, size_ecam;
  1329. hwaddr base = base_mmio;
  1330. int nr_pcie_buses;
  1331. int irq = vms->irqmap[VIRT_PCIE];
  1332. MemoryRegion *mmio_alias;
  1333. MemoryRegion *mmio_reg;
  1334. MemoryRegion *ecam_alias;
  1335. MemoryRegion *ecam_reg;
  1336. DeviceState *dev;
  1337. char *nodename;
  1338. int i, ecam_id;
  1339. PCIHostState *pci;
  1340. MachineState *ms = MACHINE(vms);
  1341. MachineClass *mc = MACHINE_GET_CLASS(ms);
  1342. dev = qdev_new(TYPE_GPEX_HOST);
  1343. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  1344. ecam_id = VIRT_ECAM_ID(vms->highmem_ecam);
  1345. base_ecam = vms->memmap[ecam_id].base;
  1346. size_ecam = vms->memmap[ecam_id].size;
  1347. nr_pcie_buses = size_ecam / PCIE_MMCFG_SIZE_MIN;
  1348. /* Map only the first size_ecam bytes of ECAM space */
  1349. ecam_alias = g_new0(MemoryRegion, 1);
  1350. ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
  1351. memory_region_init_alias(ecam_alias, OBJECT(dev), "pcie-ecam",
  1352. ecam_reg, 0, size_ecam);
  1353. memory_region_add_subregion(get_system_memory(), base_ecam, ecam_alias);
  1354. /* Map the MMIO window into system address space so as to expose
  1355. * the section of PCI MMIO space which starts at the same base address
  1356. * (ie 1:1 mapping for that part of PCI MMIO space visible through
  1357. * the window).
  1358. */
  1359. mmio_alias = g_new0(MemoryRegion, 1);
  1360. mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
  1361. memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
  1362. mmio_reg, base_mmio, size_mmio);
  1363. memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
  1364. if (vms->highmem_mmio) {
  1365. /* Map high MMIO space */
  1366. MemoryRegion *high_mmio_alias = g_new0(MemoryRegion, 1);
  1367. memory_region_init_alias(high_mmio_alias, OBJECT(dev), "pcie-mmio-high",
  1368. mmio_reg, base_mmio_high, size_mmio_high);
  1369. memory_region_add_subregion(get_system_memory(), base_mmio_high,
  1370. high_mmio_alias);
  1371. }
  1372. /* Map IO port space */
  1373. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
  1374. for (i = 0; i < PCI_NUM_PINS; i++) {
  1375. sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
  1376. qdev_get_gpio_in(vms->gic, irq + i));
  1377. gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
  1378. }
  1379. pci = PCI_HOST_BRIDGE(dev);
  1380. pci->bypass_iommu = vms->default_bus_bypass_iommu;
  1381. vms->bus = pci->bus;
  1382. if (vms->bus) {
  1383. pci_init_nic_devices(pci->bus, mc->default_nic);
  1384. }
  1385. nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base);
  1386. qemu_fdt_add_subnode(ms->fdt, nodename);
  1387. qemu_fdt_setprop_string(ms->fdt, nodename,
  1388. "compatible", "pci-host-ecam-generic");
  1389. qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "pci");
  1390. qemu_fdt_setprop_cell(ms->fdt, nodename, "#address-cells", 3);
  1391. qemu_fdt_setprop_cell(ms->fdt, nodename, "#size-cells", 2);
  1392. qemu_fdt_setprop_cell(ms->fdt, nodename, "linux,pci-domain", 0);
  1393. qemu_fdt_setprop_cells(ms->fdt, nodename, "bus-range", 0,
  1394. nr_pcie_buses - 1);
  1395. qemu_fdt_setprop(ms->fdt, nodename, "dma-coherent", NULL, 0);
  1396. if (vms->msi_phandle) {
  1397. qemu_fdt_setprop_cells(ms->fdt, nodename, "msi-map",
  1398. 0, vms->msi_phandle, 0, 0x10000);
  1399. }
  1400. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg",
  1401. 2, base_ecam, 2, size_ecam);
  1402. if (vms->highmem_mmio) {
  1403. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "ranges",
  1404. 1, FDT_PCI_RANGE_IOPORT, 2, 0,
  1405. 2, base_pio, 2, size_pio,
  1406. 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
  1407. 2, base_mmio, 2, size_mmio,
  1408. 1, FDT_PCI_RANGE_MMIO_64BIT,
  1409. 2, base_mmio_high,
  1410. 2, base_mmio_high, 2, size_mmio_high);
  1411. } else {
  1412. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "ranges",
  1413. 1, FDT_PCI_RANGE_IOPORT, 2, 0,
  1414. 2, base_pio, 2, size_pio,
  1415. 1, FDT_PCI_RANGE_MMIO, 2, base_mmio,
  1416. 2, base_mmio, 2, size_mmio);
  1417. }
  1418. qemu_fdt_setprop_cell(ms->fdt, nodename, "#interrupt-cells", 1);
  1419. create_pcie_irq_map(ms, vms->gic_phandle, irq, nodename);
  1420. if (vms->iommu) {
  1421. vms->iommu_phandle = qemu_fdt_alloc_phandle(ms->fdt);
  1422. switch (vms->iommu) {
  1423. case VIRT_IOMMU_SMMUV3:
  1424. create_smmu(vms, vms->bus);
  1425. qemu_fdt_setprop_cells(ms->fdt, nodename, "iommu-map",
  1426. 0x0, vms->iommu_phandle, 0x0, 0x10000);
  1427. break;
  1428. default:
  1429. g_assert_not_reached();
  1430. }
  1431. }
  1432. }
  1433. static void create_platform_bus(VirtMachineState *vms)
  1434. {
  1435. DeviceState *dev;
  1436. SysBusDevice *s;
  1437. int i;
  1438. MemoryRegion *sysmem = get_system_memory();
  1439. dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE);
  1440. dev->id = g_strdup(TYPE_PLATFORM_BUS_DEVICE);
  1441. qdev_prop_set_uint32(dev, "num_irqs", PLATFORM_BUS_NUM_IRQS);
  1442. qdev_prop_set_uint32(dev, "mmio_size", vms->memmap[VIRT_PLATFORM_BUS].size);
  1443. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  1444. vms->platform_bus_dev = dev;
  1445. s = SYS_BUS_DEVICE(dev);
  1446. for (i = 0; i < PLATFORM_BUS_NUM_IRQS; i++) {
  1447. int irq = vms->irqmap[VIRT_PLATFORM_BUS] + i;
  1448. sysbus_connect_irq(s, i, qdev_get_gpio_in(vms->gic, irq));
  1449. }
  1450. memory_region_add_subregion(sysmem,
  1451. vms->memmap[VIRT_PLATFORM_BUS].base,
  1452. sysbus_mmio_get_region(s, 0));
  1453. }
  1454. static void create_tag_ram(MemoryRegion *tag_sysmem,
  1455. hwaddr base, hwaddr size,
  1456. const char *name)
  1457. {
  1458. MemoryRegion *tagram = g_new(MemoryRegion, 1);
  1459. memory_region_init_ram(tagram, NULL, name, size / 32, &error_fatal);
  1460. memory_region_add_subregion(tag_sysmem, base / 32, tagram);
  1461. }
  1462. static void create_secure_ram(VirtMachineState *vms,
  1463. MemoryRegion *secure_sysmem,
  1464. MemoryRegion *secure_tag_sysmem)
  1465. {
  1466. MemoryRegion *secram = g_new(MemoryRegion, 1);
  1467. char *nodename;
  1468. hwaddr base = vms->memmap[VIRT_SECURE_MEM].base;
  1469. hwaddr size = vms->memmap[VIRT_SECURE_MEM].size;
  1470. MachineState *ms = MACHINE(vms);
  1471. memory_region_init_ram(secram, NULL, "virt.secure-ram", size,
  1472. &error_fatal);
  1473. memory_region_add_subregion(secure_sysmem, base, secram);
  1474. nodename = g_strdup_printf("/secram@%" PRIx64, base);
  1475. qemu_fdt_add_subnode(ms->fdt, nodename);
  1476. qemu_fdt_setprop_string(ms->fdt, nodename, "device_type", "memory");
  1477. qemu_fdt_setprop_sized_cells(ms->fdt, nodename, "reg", 2, base, 2, size);
  1478. qemu_fdt_setprop_string(ms->fdt, nodename, "status", "disabled");
  1479. qemu_fdt_setprop_string(ms->fdt, nodename, "secure-status", "okay");
  1480. if (secure_tag_sysmem) {
  1481. create_tag_ram(secure_tag_sysmem, base, size, "mach-virt.secure-tag");
  1482. }
  1483. g_free(nodename);
  1484. }
  1485. static void *machvirt_dtb(const struct arm_boot_info *binfo, int *fdt_size)
  1486. {
  1487. const VirtMachineState *board = container_of(binfo, VirtMachineState,
  1488. bootinfo);
  1489. MachineState *ms = MACHINE(board);
  1490. *fdt_size = board->fdt_size;
  1491. return ms->fdt;
  1492. }
  1493. static void virt_build_smbios(VirtMachineState *vms)
  1494. {
  1495. MachineClass *mc = MACHINE_GET_CLASS(vms);
  1496. MachineState *ms = MACHINE(vms);
  1497. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1498. uint8_t *smbios_tables, *smbios_anchor;
  1499. size_t smbios_tables_len, smbios_anchor_len;
  1500. struct smbios_phys_mem_area mem_array;
  1501. const char *product = "QEMU Virtual Machine";
  1502. if (kvm_enabled()) {
  1503. product = "KVM Virtual Machine";
  1504. }
  1505. smbios_set_defaults("QEMU", product,
  1506. vmc->smbios_old_sys_ver ? "1.0" : mc->name);
  1507. /* build the array of physical mem area from base_memmap */
  1508. mem_array.address = vms->memmap[VIRT_MEM].base;
  1509. mem_array.length = ms->ram_size;
  1510. smbios_get_tables(ms, SMBIOS_ENTRY_POINT_TYPE_64, &mem_array, 1,
  1511. &smbios_tables, &smbios_tables_len,
  1512. &smbios_anchor, &smbios_anchor_len,
  1513. &error_fatal);
  1514. if (smbios_anchor) {
  1515. fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-tables",
  1516. smbios_tables, smbios_tables_len);
  1517. fw_cfg_add_file(vms->fw_cfg, "etc/smbios/smbios-anchor",
  1518. smbios_anchor, smbios_anchor_len);
  1519. }
  1520. }
  1521. static
  1522. void virt_machine_done(Notifier *notifier, void *data)
  1523. {
  1524. VirtMachineState *vms = container_of(notifier, VirtMachineState,
  1525. machine_done);
  1526. MachineState *ms = MACHINE(vms);
  1527. ARMCPU *cpu = ARM_CPU(first_cpu);
  1528. struct arm_boot_info *info = &vms->bootinfo;
  1529. AddressSpace *as = arm_boot_address_space(cpu, info);
  1530. /*
  1531. * If the user provided a dtb, we assume the dynamic sysbus nodes
  1532. * already are integrated there. This corresponds to a use case where
  1533. * the dynamic sysbus nodes are complex and their generation is not yet
  1534. * supported. In that case the user can take charge of the guest dt
  1535. * while qemu takes charge of the qom stuff.
  1536. */
  1537. if (info->dtb_filename == NULL) {
  1538. platform_bus_add_all_fdt_nodes(ms->fdt, "/intc",
  1539. vms->memmap[VIRT_PLATFORM_BUS].base,
  1540. vms->memmap[VIRT_PLATFORM_BUS].size,
  1541. vms->irqmap[VIRT_PLATFORM_BUS]);
  1542. }
  1543. if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms, cpu) < 0) {
  1544. exit(1);
  1545. }
  1546. pci_bus_add_fw_cfg_extra_pci_roots(vms->fw_cfg, vms->bus,
  1547. &error_abort);
  1548. virt_acpi_setup(vms);
  1549. virt_build_smbios(vms);
  1550. }
  1551. static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
  1552. {
  1553. uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
  1554. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1555. if (!vmc->disallow_affinity_adjustment) {
  1556. /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
  1557. * GIC's target-list limitations. 32-bit KVM hosts currently
  1558. * always create clusters of 4 CPUs, but that is expected to
  1559. * change when they gain support for gicv3. When KVM is enabled
  1560. * it will override the changes we make here, therefore our
  1561. * purposes are to make TCG consistent (with 64-bit KVM hosts)
  1562. * and to improve SGI efficiency.
  1563. */
  1564. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  1565. clustersz = GIC_TARGETLIST_BITS;
  1566. } else {
  1567. clustersz = GICV3_TARGETLIST_BITS;
  1568. }
  1569. }
  1570. return arm_build_mp_affinity(idx, clustersz);
  1571. }
  1572. static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
  1573. int index)
  1574. {
  1575. bool *enabled_array[] = {
  1576. &vms->highmem_redists,
  1577. &vms->highmem_ecam,
  1578. &vms->highmem_mmio,
  1579. };
  1580. assert(ARRAY_SIZE(extended_memmap) - VIRT_LOWMEMMAP_LAST ==
  1581. ARRAY_SIZE(enabled_array));
  1582. assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));
  1583. return enabled_array[index - VIRT_LOWMEMMAP_LAST];
  1584. }
  1585. static void virt_set_high_memmap(VirtMachineState *vms,
  1586. hwaddr base, int pa_bits)
  1587. {
  1588. hwaddr region_base, region_size;
  1589. bool *region_enabled, fits;
  1590. int i;
  1591. for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
  1592. region_enabled = virt_get_high_memmap_enabled(vms, i);
  1593. region_base = ROUND_UP(base, extended_memmap[i].size);
  1594. region_size = extended_memmap[i].size;
  1595. vms->memmap[i].base = region_base;
  1596. vms->memmap[i].size = region_size;
  1597. /*
  1598. * Check each device to see if it fits in the PA space,
  1599. * moving highest_gpa as we go. For compatibility, move
  1600. * highest_gpa for disabled fitting devices as well, if
  1601. * the compact layout has been disabled.
  1602. *
  1603. * For each device that doesn't fit, disable it.
  1604. */
  1605. fits = (region_base + region_size) <= BIT_ULL(pa_bits);
  1606. *region_enabled &= fits;
  1607. if (vms->highmem_compact && !*region_enabled) {
  1608. continue;
  1609. }
  1610. base = region_base + region_size;
  1611. if (fits) {
  1612. vms->highest_gpa = base - 1;
  1613. }
  1614. }
  1615. }
  1616. static void virt_set_memmap(VirtMachineState *vms, int pa_bits)
  1617. {
  1618. MachineState *ms = MACHINE(vms);
  1619. hwaddr base, device_memory_base, device_memory_size, memtop;
  1620. int i;
  1621. vms->memmap = extended_memmap;
  1622. for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
  1623. vms->memmap[i] = base_memmap[i];
  1624. }
  1625. if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) {
  1626. error_report("unsupported number of memory slots: %"PRIu64,
  1627. ms->ram_slots);
  1628. exit(EXIT_FAILURE);
  1629. }
  1630. /*
  1631. * !highmem is exactly the same as limiting the PA space to 32bit,
  1632. * irrespective of the underlying capabilities of the HW.
  1633. */
  1634. if (!vms->highmem) {
  1635. pa_bits = 32;
  1636. }
  1637. /*
  1638. * We compute the base of the high IO region depending on the
  1639. * amount of initial and device memory. The device memory start/size
  1640. * is aligned on 1GiB. We never put the high IO region below 256GiB
  1641. * so that if maxram_size is < 255GiB we keep the legacy memory map.
  1642. * The device region size assumes 1GiB page max alignment per slot.
  1643. */
  1644. device_memory_base =
  1645. ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB);
  1646. device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB;
  1647. /* Base address of the high IO region */
  1648. memtop = base = device_memory_base + ROUND_UP(device_memory_size, GiB);
  1649. if (memtop > BIT_ULL(pa_bits)) {
  1650. error_report("Addressing limited to %d bits, but memory exceeds it by %llu bytes",
  1651. pa_bits, memtop - BIT_ULL(pa_bits));
  1652. exit(EXIT_FAILURE);
  1653. }
  1654. if (base < device_memory_base) {
  1655. error_report("maxmem/slots too huge");
  1656. exit(EXIT_FAILURE);
  1657. }
  1658. if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) {
  1659. base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES;
  1660. }
  1661. /* We know for sure that at least the memory fits in the PA space */
  1662. vms->highest_gpa = memtop - 1;
  1663. virt_set_high_memmap(vms, base, pa_bits);
  1664. if (device_memory_size > 0) {
  1665. machine_memory_devices_init(ms, device_memory_base, device_memory_size);
  1666. }
  1667. }
  1668. static VirtGICType finalize_gic_version_do(const char *accel_name,
  1669. VirtGICType gic_version,
  1670. int gics_supported,
  1671. unsigned int max_cpus)
  1672. {
  1673. /* Convert host/max/nosel to GIC version number */
  1674. switch (gic_version) {
  1675. case VIRT_GIC_VERSION_HOST:
  1676. if (!kvm_enabled()) {
  1677. error_report("gic-version=host requires KVM");
  1678. exit(1);
  1679. }
  1680. /* For KVM, gic-version=host means gic-version=max */
  1681. return finalize_gic_version_do(accel_name, VIRT_GIC_VERSION_MAX,
  1682. gics_supported, max_cpus);
  1683. case VIRT_GIC_VERSION_MAX:
  1684. if (gics_supported & VIRT_GIC_VERSION_4_MASK) {
  1685. gic_version = VIRT_GIC_VERSION_4;
  1686. } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) {
  1687. gic_version = VIRT_GIC_VERSION_3;
  1688. } else {
  1689. gic_version = VIRT_GIC_VERSION_2;
  1690. }
  1691. break;
  1692. case VIRT_GIC_VERSION_NOSEL:
  1693. if ((gics_supported & VIRT_GIC_VERSION_2_MASK) &&
  1694. max_cpus <= GIC_NCPU) {
  1695. gic_version = VIRT_GIC_VERSION_2;
  1696. } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) {
  1697. /*
  1698. * in case the host does not support v2 emulation or
  1699. * the end-user requested more than 8 VCPUs we now default
  1700. * to v3. In any case defaulting to v2 would be broken.
  1701. */
  1702. gic_version = VIRT_GIC_VERSION_3;
  1703. } else if (max_cpus > GIC_NCPU) {
  1704. error_report("%s only supports GICv2 emulation but more than 8 "
  1705. "vcpus are requested", accel_name);
  1706. exit(1);
  1707. }
  1708. break;
  1709. case VIRT_GIC_VERSION_2:
  1710. case VIRT_GIC_VERSION_3:
  1711. case VIRT_GIC_VERSION_4:
  1712. break;
  1713. }
  1714. /* Check chosen version is effectively supported */
  1715. switch (gic_version) {
  1716. case VIRT_GIC_VERSION_2:
  1717. if (!(gics_supported & VIRT_GIC_VERSION_2_MASK)) {
  1718. error_report("%s does not support GICv2 emulation", accel_name);
  1719. exit(1);
  1720. }
  1721. break;
  1722. case VIRT_GIC_VERSION_3:
  1723. if (!(gics_supported & VIRT_GIC_VERSION_3_MASK)) {
  1724. error_report("%s does not support GICv3 emulation", accel_name);
  1725. exit(1);
  1726. }
  1727. break;
  1728. case VIRT_GIC_VERSION_4:
  1729. if (!(gics_supported & VIRT_GIC_VERSION_4_MASK)) {
  1730. error_report("%s does not support GICv4 emulation, is virtualization=on?",
  1731. accel_name);
  1732. exit(1);
  1733. }
  1734. break;
  1735. default:
  1736. error_report("logic error in finalize_gic_version");
  1737. exit(1);
  1738. break;
  1739. }
  1740. return gic_version;
  1741. }
  1742. /*
  1743. * finalize_gic_version - Determines the final gic_version
  1744. * according to the gic-version property
  1745. *
  1746. * Default GIC type is v2
  1747. */
  1748. static void finalize_gic_version(VirtMachineState *vms)
  1749. {
  1750. const char *accel_name = current_accel_name();
  1751. unsigned int max_cpus = MACHINE(vms)->smp.max_cpus;
  1752. int gics_supported = 0;
  1753. /* Determine which GIC versions the current environment supports */
  1754. if (kvm_enabled() && kvm_irqchip_in_kernel()) {
  1755. int probe_bitmap = kvm_arm_vgic_probe();
  1756. if (!probe_bitmap) {
  1757. error_report("Unable to determine GIC version supported by host");
  1758. exit(1);
  1759. }
  1760. if (probe_bitmap & KVM_ARM_VGIC_V2) {
  1761. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1762. }
  1763. if (probe_bitmap & KVM_ARM_VGIC_V3) {
  1764. gics_supported |= VIRT_GIC_VERSION_3_MASK;
  1765. }
  1766. } else if (kvm_enabled() && !kvm_irqchip_in_kernel()) {
  1767. /* KVM w/o kernel irqchip can only deal with GICv2 */
  1768. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1769. accel_name = "KVM with kernel-irqchip=off";
  1770. } else if (tcg_enabled() || hvf_enabled() || qtest_enabled()) {
  1771. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1772. if (module_object_class_by_name("arm-gicv3")) {
  1773. gics_supported |= VIRT_GIC_VERSION_3_MASK;
  1774. if (vms->virt) {
  1775. /* GICv4 only makes sense if CPU has EL2 */
  1776. gics_supported |= VIRT_GIC_VERSION_4_MASK;
  1777. }
  1778. }
  1779. } else {
  1780. error_report("Unsupported accelerator, can not determine GIC support");
  1781. exit(1);
  1782. }
  1783. /*
  1784. * Then convert helpers like host/max to concrete GIC versions and ensure
  1785. * the desired version is supported
  1786. */
  1787. vms->gic_version = finalize_gic_version_do(accel_name, vms->gic_version,
  1788. gics_supported, max_cpus);
  1789. }
  1790. /*
  1791. * virt_cpu_post_init() must be called after the CPUs have
  1792. * been realized and the GIC has been created.
  1793. */
  1794. static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
  1795. {
  1796. int max_cpus = MACHINE(vms)->smp.max_cpus;
  1797. bool aarch64, pmu, steal_time;
  1798. CPUState *cpu;
  1799. aarch64 = object_property_get_bool(OBJECT(first_cpu), "aarch64", NULL);
  1800. pmu = object_property_get_bool(OBJECT(first_cpu), "pmu", NULL);
  1801. steal_time = object_property_get_bool(OBJECT(first_cpu),
  1802. "kvm-steal-time", NULL);
  1803. if (kvm_enabled()) {
  1804. hwaddr pvtime_reg_base = vms->memmap[VIRT_PVTIME].base;
  1805. hwaddr pvtime_reg_size = vms->memmap[VIRT_PVTIME].size;
  1806. if (steal_time) {
  1807. MemoryRegion *pvtime = g_new(MemoryRegion, 1);
  1808. hwaddr pvtime_size = max_cpus * PVTIME_SIZE_PER_CPU;
  1809. /* The memory region size must be a multiple of host page size. */
  1810. pvtime_size = REAL_HOST_PAGE_ALIGN(pvtime_size);
  1811. if (pvtime_size > pvtime_reg_size) {
  1812. error_report("pvtime requires a %" HWADDR_PRId
  1813. " byte memory region for %d CPUs,"
  1814. " but only %" HWADDR_PRId " has been reserved",
  1815. pvtime_size, max_cpus, pvtime_reg_size);
  1816. exit(1);
  1817. }
  1818. memory_region_init_ram(pvtime, NULL, "pvtime", pvtime_size, NULL);
  1819. memory_region_add_subregion(sysmem, pvtime_reg_base, pvtime);
  1820. }
  1821. CPU_FOREACH(cpu) {
  1822. if (pmu) {
  1823. assert(arm_feature(&ARM_CPU(cpu)->env, ARM_FEATURE_PMU));
  1824. if (kvm_irqchip_in_kernel()) {
  1825. kvm_arm_pmu_set_irq(ARM_CPU(cpu), VIRTUAL_PMU_IRQ);
  1826. }
  1827. kvm_arm_pmu_init(ARM_CPU(cpu));
  1828. }
  1829. if (steal_time) {
  1830. kvm_arm_pvtime_init(ARM_CPU(cpu), pvtime_reg_base
  1831. + cpu->cpu_index
  1832. * PVTIME_SIZE_PER_CPU);
  1833. }
  1834. }
  1835. } else {
  1836. if (aarch64 && vms->highmem) {
  1837. int requested_pa_size = 64 - clz64(vms->highest_gpa);
  1838. int pamax = arm_pamax(ARM_CPU(first_cpu));
  1839. if (pamax < requested_pa_size) {
  1840. error_report("VCPU supports less PA bits (%d) than "
  1841. "requested by the memory map (%d)",
  1842. pamax, requested_pa_size);
  1843. exit(1);
  1844. }
  1845. }
  1846. }
  1847. }
  1848. static void machvirt_init(MachineState *machine)
  1849. {
  1850. VirtMachineState *vms = VIRT_MACHINE(machine);
  1851. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
  1852. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1853. const CPUArchIdList *possible_cpus;
  1854. MemoryRegion *sysmem = get_system_memory();
  1855. MemoryRegion *secure_sysmem = NULL;
  1856. MemoryRegion *tag_sysmem = NULL;
  1857. MemoryRegion *secure_tag_sysmem = NULL;
  1858. int n, virt_max_cpus;
  1859. bool firmware_loaded;
  1860. bool aarch64 = true;
  1861. bool has_ged = !vmc->no_ged;
  1862. unsigned int smp_cpus = machine->smp.cpus;
  1863. unsigned int max_cpus = machine->smp.max_cpus;
  1864. possible_cpus = mc->possible_cpu_arch_ids(machine);
  1865. /*
  1866. * In accelerated mode, the memory map is computed earlier in kvm_type()
  1867. * for Linux, or hvf_get_physical_address_range() for macOS to create a
  1868. * VM with the right number of IPA bits.
  1869. */
  1870. if (!vms->memmap) {
  1871. Object *cpuobj;
  1872. ARMCPU *armcpu;
  1873. int pa_bits;
  1874. /*
  1875. * Instantiate a temporary CPU object to find out about what
  1876. * we are about to deal with. Once this is done, get rid of
  1877. * the object.
  1878. */
  1879. cpuobj = object_new(possible_cpus->cpus[0].type);
  1880. armcpu = ARM_CPU(cpuobj);
  1881. pa_bits = arm_pamax(armcpu);
  1882. object_unref(cpuobj);
  1883. virt_set_memmap(vms, pa_bits);
  1884. }
  1885. /* We can probe only here because during property set
  1886. * KVM is not available yet
  1887. */
  1888. finalize_gic_version(vms);
  1889. if (vms->secure) {
  1890. /*
  1891. * The Secure view of the world is the same as the NonSecure,
  1892. * but with a few extra devices. Create it as a container region
  1893. * containing the system memory at low priority; any secure-only
  1894. * devices go in at higher priority and take precedence.
  1895. */
  1896. secure_sysmem = g_new(MemoryRegion, 1);
  1897. memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
  1898. UINT64_MAX);
  1899. memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
  1900. }
  1901. firmware_loaded = virt_firmware_init(vms, sysmem,
  1902. secure_sysmem ?: sysmem);
  1903. /* If we have an EL3 boot ROM then the assumption is that it will
  1904. * implement PSCI itself, so disable QEMU's internal implementation
  1905. * so it doesn't get in the way. Instead of starting secondary
  1906. * CPUs in PSCI powerdown state we will start them all running and
  1907. * let the boot ROM sort them out.
  1908. * The usual case is that we do use QEMU's PSCI implementation;
  1909. * if the guest has EL2 then we will use SMC as the conduit,
  1910. * and otherwise we will use HVC (for backwards compatibility and
  1911. * because if we're using KVM then we must use HVC).
  1912. */
  1913. if (vms->secure && firmware_loaded) {
  1914. vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
  1915. } else if (vms->virt) {
  1916. vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
  1917. } else {
  1918. vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
  1919. }
  1920. /*
  1921. * The maximum number of CPUs depends on the GIC version, or on how
  1922. * many redistributors we can fit into the memory map (which in turn
  1923. * depends on whether this is a GICv3 or v4).
  1924. */
  1925. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  1926. virt_max_cpus = GIC_NCPU;
  1927. } else {
  1928. virt_max_cpus = virt_redist_capacity(vms, VIRT_GIC_REDIST);
  1929. if (vms->highmem_redists) {
  1930. virt_max_cpus += virt_redist_capacity(vms, VIRT_HIGH_GIC_REDIST2);
  1931. }
  1932. }
  1933. if (max_cpus > virt_max_cpus) {
  1934. error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
  1935. "supported by machine 'mach-virt' (%d)",
  1936. max_cpus, virt_max_cpus);
  1937. if (vms->gic_version != VIRT_GIC_VERSION_2 && !vms->highmem_redists) {
  1938. error_printf("Try 'highmem-redists=on' for more CPUs\n");
  1939. }
  1940. exit(1);
  1941. }
  1942. if (vms->secure && (kvm_enabled() || hvf_enabled())) {
  1943. error_report("mach-virt: %s does not support providing "
  1944. "Security extensions (TrustZone) to the guest CPU",
  1945. current_accel_name());
  1946. exit(1);
  1947. }
  1948. if (vms->virt && (kvm_enabled() || hvf_enabled())) {
  1949. error_report("mach-virt: %s does not support providing "
  1950. "Virtualization extensions to the guest CPU",
  1951. current_accel_name());
  1952. exit(1);
  1953. }
  1954. if (vms->mte && hvf_enabled()) {
  1955. error_report("mach-virt: %s does not support providing "
  1956. "MTE to the guest CPU",
  1957. current_accel_name());
  1958. exit(1);
  1959. }
  1960. create_fdt(vms);
  1961. assert(possible_cpus->len == max_cpus);
  1962. for (n = 0; n < possible_cpus->len; n++) {
  1963. Object *cpuobj;
  1964. CPUState *cs;
  1965. if (n >= smp_cpus) {
  1966. break;
  1967. }
  1968. cpuobj = object_new(possible_cpus->cpus[n].type);
  1969. object_property_set_int(cpuobj, "mp-affinity",
  1970. possible_cpus->cpus[n].arch_id, NULL);
  1971. cs = CPU(cpuobj);
  1972. cs->cpu_index = n;
  1973. numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
  1974. &error_fatal);
  1975. aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
  1976. if (!vms->secure) {
  1977. object_property_set_bool(cpuobj, "has_el3", false, NULL);
  1978. }
  1979. if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
  1980. object_property_set_bool(cpuobj, "has_el2", false, NULL);
  1981. }
  1982. if (vmc->kvm_no_adjvtime &&
  1983. object_property_find(cpuobj, "kvm-no-adjvtime")) {
  1984. object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
  1985. }
  1986. if (vmc->no_kvm_steal_time &&
  1987. object_property_find(cpuobj, "kvm-steal-time")) {
  1988. object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
  1989. }
  1990. if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
  1991. object_property_set_bool(cpuobj, "pmu", false, NULL);
  1992. }
  1993. if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
  1994. object_property_set_bool(cpuobj, "lpa2", false, NULL);
  1995. }
  1996. if (object_property_find(cpuobj, "reset-cbar")) {
  1997. object_property_set_int(cpuobj, "reset-cbar",
  1998. vms->memmap[VIRT_CPUPERIPHS].base,
  1999. &error_abort);
  2000. }
  2001. object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
  2002. &error_abort);
  2003. if (vms->secure) {
  2004. object_property_set_link(cpuobj, "secure-memory",
  2005. OBJECT(secure_sysmem), &error_abort);
  2006. }
  2007. if (vms->mte) {
  2008. if (tcg_enabled()) {
  2009. /* Create the memory region only once, but link to all cpus. */
  2010. if (!tag_sysmem) {
  2011. /*
  2012. * The property exists only if MemTag is supported.
  2013. * If it is, we must allocate the ram to back that up.
  2014. */
  2015. if (!object_property_find(cpuobj, "tag-memory")) {
  2016. error_report("MTE requested, but not supported "
  2017. "by the guest CPU");
  2018. exit(1);
  2019. }
  2020. tag_sysmem = g_new(MemoryRegion, 1);
  2021. memory_region_init(tag_sysmem, OBJECT(machine),
  2022. "tag-memory", UINT64_MAX / 32);
  2023. if (vms->secure) {
  2024. secure_tag_sysmem = g_new(MemoryRegion, 1);
  2025. memory_region_init(secure_tag_sysmem, OBJECT(machine),
  2026. "secure-tag-memory",
  2027. UINT64_MAX / 32);
  2028. /* As with ram, secure-tag takes precedence over tag. */
  2029. memory_region_add_subregion_overlap(secure_tag_sysmem,
  2030. 0, tag_sysmem, -1);
  2031. }
  2032. }
  2033. object_property_set_link(cpuobj, "tag-memory",
  2034. OBJECT(tag_sysmem), &error_abort);
  2035. if (vms->secure) {
  2036. object_property_set_link(cpuobj, "secure-tag-memory",
  2037. OBJECT(secure_tag_sysmem),
  2038. &error_abort);
  2039. }
  2040. } else if (kvm_enabled()) {
  2041. if (!kvm_arm_mte_supported()) {
  2042. error_report("MTE requested, but not supported by KVM");
  2043. exit(1);
  2044. }
  2045. kvm_arm_enable_mte(cpuobj, &error_abort);
  2046. } else {
  2047. error_report("MTE requested, but not supported ");
  2048. exit(1);
  2049. }
  2050. }
  2051. qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
  2052. object_unref(cpuobj);
  2053. }
  2054. /* Now we've created the CPUs we can see if they have the hypvirt timer */
  2055. vms->ns_el2_virt_timer_irq = ns_el2_virt_timer_present() &&
  2056. !vmc->no_ns_el2_virt_timer_irq;
  2057. fdt_add_timer_nodes(vms);
  2058. fdt_add_cpu_nodes(vms);
  2059. memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base,
  2060. machine->ram);
  2061. virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem);
  2062. create_gic(vms, sysmem);
  2063. virt_cpu_post_init(vms, sysmem);
  2064. fdt_add_pmu_nodes(vms);
  2065. /*
  2066. * The first UART always exists. If the security extensions are
  2067. * enabled, the second UART also always exists. Otherwise, it only exists
  2068. * if a backend is configured explicitly via '-serial <backend>'.
  2069. * This avoids potentially breaking existing user setups that expect
  2070. * only one NonSecure UART to be present (for instance, older EDK2
  2071. * binaries).
  2072. *
  2073. * The nodes end up in the DTB in reverse order of creation, so we must
  2074. * create UART0 last to ensure it appears as the first node in the DTB,
  2075. * for compatibility with guest software that just iterates through the
  2076. * DTB to find the first UART, as older versions of EDK2 do.
  2077. * DTB readers that follow the spec, as Linux does, should honour the
  2078. * aliases node information and /chosen/stdout-path regardless of
  2079. * the order that nodes appear in the DTB.
  2080. *
  2081. * For similar back-compatibility reasons, if UART1 is the secure UART
  2082. * we create it second (and so it appears first in the DTB), because
  2083. * that's what QEMU has always done.
  2084. */
  2085. if (!vms->secure) {
  2086. Chardev *serial1 = serial_hd(1);
  2087. if (serial1) {
  2088. vms->second_ns_uart_present = true;
  2089. create_uart(vms, VIRT_UART1, sysmem, serial1, false);
  2090. }
  2091. }
  2092. create_uart(vms, VIRT_UART0, sysmem, serial_hd(0), false);
  2093. if (vms->secure) {
  2094. create_uart(vms, VIRT_UART1, secure_sysmem, serial_hd(1), true);
  2095. }
  2096. if (vms->secure) {
  2097. create_secure_ram(vms, secure_sysmem, secure_tag_sysmem);
  2098. }
  2099. if (tag_sysmem) {
  2100. create_tag_ram(tag_sysmem, vms->memmap[VIRT_MEM].base,
  2101. machine->ram_size, "mach-virt.tag");
  2102. }
  2103. vms->highmem_ecam &= (!firmware_loaded || aarch64);
  2104. create_rtc(vms);
  2105. create_pcie(vms);
  2106. if (has_ged && aarch64 && firmware_loaded && virt_is_acpi_enabled(vms)) {
  2107. vms->acpi_dev = create_acpi_ged(vms);
  2108. } else {
  2109. create_gpio_devices(vms, VIRT_GPIO, sysmem);
  2110. }
  2111. if (vms->secure && !vmc->no_secure_gpio) {
  2112. create_gpio_devices(vms, VIRT_SECURE_GPIO, secure_sysmem);
  2113. }
  2114. /* connect powerdown request */
  2115. vms->powerdown_notifier.notify = virt_powerdown_req;
  2116. qemu_register_powerdown_notifier(&vms->powerdown_notifier);
  2117. /* Create mmio transports, so the user can create virtio backends
  2118. * (which will be automatically plugged in to the transports). If
  2119. * no backend is created the transport will just sit harmlessly idle.
  2120. */
  2121. create_virtio_devices(vms);
  2122. vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
  2123. rom_set_fw(vms->fw_cfg);
  2124. create_platform_bus(vms);
  2125. if (machine->nvdimms_state->is_enabled) {
  2126. const struct AcpiGenericAddress arm_virt_nvdimm_acpi_dsmio = {
  2127. .space_id = AML_AS_SYSTEM_MEMORY,
  2128. .address = vms->memmap[VIRT_NVDIMM_ACPI].base,
  2129. .bit_width = NVDIMM_ACPI_IO_LEN << 3
  2130. };
  2131. nvdimm_init_acpi_state(machine->nvdimms_state, sysmem,
  2132. arm_virt_nvdimm_acpi_dsmio,
  2133. vms->fw_cfg, OBJECT(vms));
  2134. }
  2135. vms->bootinfo.ram_size = machine->ram_size;
  2136. vms->bootinfo.board_id = -1;
  2137. vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
  2138. vms->bootinfo.get_dtb = machvirt_dtb;
  2139. vms->bootinfo.skip_dtb_autoload = true;
  2140. vms->bootinfo.firmware_loaded = firmware_loaded;
  2141. vms->bootinfo.psci_conduit = vms->psci_conduit;
  2142. arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
  2143. vms->machine_done.notify = virt_machine_done;
  2144. qemu_add_machine_init_done_notifier(&vms->machine_done);
  2145. }
  2146. static bool virt_get_secure(Object *obj, Error **errp)
  2147. {
  2148. VirtMachineState *vms = VIRT_MACHINE(obj);
  2149. return vms->secure;
  2150. }
  2151. static void virt_set_secure(Object *obj, bool value, Error **errp)
  2152. {
  2153. VirtMachineState *vms = VIRT_MACHINE(obj);
  2154. vms->secure = value;
  2155. }
  2156. static bool virt_get_virt(Object *obj, Error **errp)
  2157. {
  2158. VirtMachineState *vms = VIRT_MACHINE(obj);
  2159. return vms->virt;
  2160. }
  2161. static void virt_set_virt(Object *obj, bool value, Error **errp)
  2162. {
  2163. VirtMachineState *vms = VIRT_MACHINE(obj);
  2164. vms->virt = value;
  2165. }
  2166. static bool virt_get_highmem(Object *obj, Error **errp)
  2167. {
  2168. VirtMachineState *vms = VIRT_MACHINE(obj);
  2169. return vms->highmem;
  2170. }
  2171. static void virt_set_highmem(Object *obj, bool value, Error **errp)
  2172. {
  2173. VirtMachineState *vms = VIRT_MACHINE(obj);
  2174. vms->highmem = value;
  2175. }
  2176. static bool virt_get_compact_highmem(Object *obj, Error **errp)
  2177. {
  2178. VirtMachineState *vms = VIRT_MACHINE(obj);
  2179. return vms->highmem_compact;
  2180. }
  2181. static void virt_set_compact_highmem(Object *obj, bool value, Error **errp)
  2182. {
  2183. VirtMachineState *vms = VIRT_MACHINE(obj);
  2184. vms->highmem_compact = value;
  2185. }
  2186. static bool virt_get_highmem_redists(Object *obj, Error **errp)
  2187. {
  2188. VirtMachineState *vms = VIRT_MACHINE(obj);
  2189. return vms->highmem_redists;
  2190. }
  2191. static void virt_set_highmem_redists(Object *obj, bool value, Error **errp)
  2192. {
  2193. VirtMachineState *vms = VIRT_MACHINE(obj);
  2194. vms->highmem_redists = value;
  2195. }
  2196. static bool virt_get_highmem_ecam(Object *obj, Error **errp)
  2197. {
  2198. VirtMachineState *vms = VIRT_MACHINE(obj);
  2199. return vms->highmem_ecam;
  2200. }
  2201. static void virt_set_highmem_ecam(Object *obj, bool value, Error **errp)
  2202. {
  2203. VirtMachineState *vms = VIRT_MACHINE(obj);
  2204. vms->highmem_ecam = value;
  2205. }
  2206. static bool virt_get_highmem_mmio(Object *obj, Error **errp)
  2207. {
  2208. VirtMachineState *vms = VIRT_MACHINE(obj);
  2209. return vms->highmem_mmio;
  2210. }
  2211. static void virt_set_highmem_mmio(Object *obj, bool value, Error **errp)
  2212. {
  2213. VirtMachineState *vms = VIRT_MACHINE(obj);
  2214. vms->highmem_mmio = value;
  2215. }
  2216. static void virt_get_highmem_mmio_size(Object *obj, Visitor *v,
  2217. const char *name, void *opaque,
  2218. Error **errp)
  2219. {
  2220. uint64_t size = extended_memmap[VIRT_HIGH_PCIE_MMIO].size;
  2221. visit_type_size(v, name, &size, errp);
  2222. }
  2223. static void virt_set_highmem_mmio_size(Object *obj, Visitor *v,
  2224. const char *name, void *opaque,
  2225. Error **errp)
  2226. {
  2227. uint64_t size;
  2228. if (!visit_type_size(v, name, &size, errp)) {
  2229. return;
  2230. }
  2231. if (!is_power_of_2(size)) {
  2232. error_setg(errp, "highmem-mmio-size is not a power of 2");
  2233. return;
  2234. }
  2235. if (size < DEFAULT_HIGH_PCIE_MMIO_SIZE) {
  2236. char *sz = size_to_str(DEFAULT_HIGH_PCIE_MMIO_SIZE);
  2237. error_setg(errp, "highmem-mmio-size cannot be set to a lower value "
  2238. "than the default (%s)", sz);
  2239. g_free(sz);
  2240. return;
  2241. }
  2242. extended_memmap[VIRT_HIGH_PCIE_MMIO].size = size;
  2243. }
  2244. static bool virt_get_its(Object *obj, Error **errp)
  2245. {
  2246. VirtMachineState *vms = VIRT_MACHINE(obj);
  2247. return vms->its;
  2248. }
  2249. static void virt_set_its(Object *obj, bool value, Error **errp)
  2250. {
  2251. VirtMachineState *vms = VIRT_MACHINE(obj);
  2252. vms->its = value;
  2253. }
  2254. static bool virt_get_dtb_randomness(Object *obj, Error **errp)
  2255. {
  2256. VirtMachineState *vms = VIRT_MACHINE(obj);
  2257. return vms->dtb_randomness;
  2258. }
  2259. static void virt_set_dtb_randomness(Object *obj, bool value, Error **errp)
  2260. {
  2261. VirtMachineState *vms = VIRT_MACHINE(obj);
  2262. vms->dtb_randomness = value;
  2263. }
  2264. static char *virt_get_oem_id(Object *obj, Error **errp)
  2265. {
  2266. VirtMachineState *vms = VIRT_MACHINE(obj);
  2267. return g_strdup(vms->oem_id);
  2268. }
  2269. static void virt_set_oem_id(Object *obj, const char *value, Error **errp)
  2270. {
  2271. VirtMachineState *vms = VIRT_MACHINE(obj);
  2272. size_t len = strlen(value);
  2273. if (len > 6) {
  2274. error_setg(errp,
  2275. "User specified oem-id value is bigger than 6 bytes in size");
  2276. return;
  2277. }
  2278. strncpy(vms->oem_id, value, 6);
  2279. }
  2280. static char *virt_get_oem_table_id(Object *obj, Error **errp)
  2281. {
  2282. VirtMachineState *vms = VIRT_MACHINE(obj);
  2283. return g_strdup(vms->oem_table_id);
  2284. }
  2285. static void virt_set_oem_table_id(Object *obj, const char *value,
  2286. Error **errp)
  2287. {
  2288. VirtMachineState *vms = VIRT_MACHINE(obj);
  2289. size_t len = strlen(value);
  2290. if (len > 8) {
  2291. error_setg(errp,
  2292. "User specified oem-table-id value is bigger than 8 bytes in size");
  2293. return;
  2294. }
  2295. strncpy(vms->oem_table_id, value, 8);
  2296. }
  2297. bool virt_is_acpi_enabled(VirtMachineState *vms)
  2298. {
  2299. if (vms->acpi == ON_OFF_AUTO_OFF) {
  2300. return false;
  2301. }
  2302. return true;
  2303. }
  2304. static void virt_get_acpi(Object *obj, Visitor *v, const char *name,
  2305. void *opaque, Error **errp)
  2306. {
  2307. VirtMachineState *vms = VIRT_MACHINE(obj);
  2308. OnOffAuto acpi = vms->acpi;
  2309. visit_type_OnOffAuto(v, name, &acpi, errp);
  2310. }
  2311. static void virt_set_acpi(Object *obj, Visitor *v, const char *name,
  2312. void *opaque, Error **errp)
  2313. {
  2314. VirtMachineState *vms = VIRT_MACHINE(obj);
  2315. visit_type_OnOffAuto(v, name, &vms->acpi, errp);
  2316. }
  2317. static bool virt_get_ras(Object *obj, Error **errp)
  2318. {
  2319. VirtMachineState *vms = VIRT_MACHINE(obj);
  2320. return vms->ras;
  2321. }
  2322. static void virt_set_ras(Object *obj, bool value, Error **errp)
  2323. {
  2324. VirtMachineState *vms = VIRT_MACHINE(obj);
  2325. vms->ras = value;
  2326. }
  2327. static bool virt_get_mte(Object *obj, Error **errp)
  2328. {
  2329. VirtMachineState *vms = VIRT_MACHINE(obj);
  2330. return vms->mte;
  2331. }
  2332. static void virt_set_mte(Object *obj, bool value, Error **errp)
  2333. {
  2334. VirtMachineState *vms = VIRT_MACHINE(obj);
  2335. vms->mte = value;
  2336. }
  2337. static char *virt_get_gic_version(Object *obj, Error **errp)
  2338. {
  2339. VirtMachineState *vms = VIRT_MACHINE(obj);
  2340. const char *val;
  2341. switch (vms->gic_version) {
  2342. case VIRT_GIC_VERSION_4:
  2343. val = "4";
  2344. break;
  2345. case VIRT_GIC_VERSION_3:
  2346. val = "3";
  2347. break;
  2348. default:
  2349. val = "2";
  2350. break;
  2351. }
  2352. return g_strdup(val);
  2353. }
  2354. static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
  2355. {
  2356. VirtMachineState *vms = VIRT_MACHINE(obj);
  2357. if (!strcmp(value, "4")) {
  2358. vms->gic_version = VIRT_GIC_VERSION_4;
  2359. } else if (!strcmp(value, "3")) {
  2360. vms->gic_version = VIRT_GIC_VERSION_3;
  2361. } else if (!strcmp(value, "2")) {
  2362. vms->gic_version = VIRT_GIC_VERSION_2;
  2363. } else if (!strcmp(value, "host")) {
  2364. vms->gic_version = VIRT_GIC_VERSION_HOST; /* Will probe later */
  2365. } else if (!strcmp(value, "max")) {
  2366. vms->gic_version = VIRT_GIC_VERSION_MAX; /* Will probe later */
  2367. } else {
  2368. error_setg(errp, "Invalid gic-version value");
  2369. error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
  2370. }
  2371. }
  2372. static char *virt_get_iommu(Object *obj, Error **errp)
  2373. {
  2374. VirtMachineState *vms = VIRT_MACHINE(obj);
  2375. switch (vms->iommu) {
  2376. case VIRT_IOMMU_NONE:
  2377. return g_strdup("none");
  2378. case VIRT_IOMMU_SMMUV3:
  2379. return g_strdup("smmuv3");
  2380. default:
  2381. g_assert_not_reached();
  2382. }
  2383. }
  2384. static void virt_set_iommu(Object *obj, const char *value, Error **errp)
  2385. {
  2386. VirtMachineState *vms = VIRT_MACHINE(obj);
  2387. if (!strcmp(value, "smmuv3")) {
  2388. vms->iommu = VIRT_IOMMU_SMMUV3;
  2389. } else if (!strcmp(value, "none")) {
  2390. vms->iommu = VIRT_IOMMU_NONE;
  2391. } else {
  2392. error_setg(errp, "Invalid iommu value");
  2393. error_append_hint(errp, "Valid values are none, smmuv3.\n");
  2394. }
  2395. }
  2396. static bool virt_get_default_bus_bypass_iommu(Object *obj, Error **errp)
  2397. {
  2398. VirtMachineState *vms = VIRT_MACHINE(obj);
  2399. return vms->default_bus_bypass_iommu;
  2400. }
  2401. static void virt_set_default_bus_bypass_iommu(Object *obj, bool value,
  2402. Error **errp)
  2403. {
  2404. VirtMachineState *vms = VIRT_MACHINE(obj);
  2405. vms->default_bus_bypass_iommu = value;
  2406. }
  2407. static CpuInstanceProperties
  2408. virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  2409. {
  2410. MachineClass *mc = MACHINE_GET_CLASS(ms);
  2411. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  2412. assert(cpu_index < possible_cpus->len);
  2413. return possible_cpus->cpus[cpu_index].props;
  2414. }
  2415. static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
  2416. {
  2417. int64_t socket_id = ms->possible_cpus->cpus[idx].props.socket_id;
  2418. return socket_id % ms->numa_state->num_nodes;
  2419. }
  2420. static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
  2421. {
  2422. int n;
  2423. unsigned int max_cpus = ms->smp.max_cpus;
  2424. VirtMachineState *vms = VIRT_MACHINE(ms);
  2425. MachineClass *mc = MACHINE_GET_CLASS(vms);
  2426. if (ms->possible_cpus) {
  2427. assert(ms->possible_cpus->len == max_cpus);
  2428. return ms->possible_cpus;
  2429. }
  2430. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  2431. sizeof(CPUArchId) * max_cpus);
  2432. ms->possible_cpus->len = max_cpus;
  2433. for (n = 0; n < ms->possible_cpus->len; n++) {
  2434. ms->possible_cpus->cpus[n].type = ms->cpu_type;
  2435. ms->possible_cpus->cpus[n].arch_id =
  2436. virt_cpu_mp_affinity(vms, n);
  2437. assert(!mc->smp_props.dies_supported);
  2438. ms->possible_cpus->cpus[n].props.has_socket_id = true;
  2439. ms->possible_cpus->cpus[n].props.socket_id =
  2440. n / (ms->smp.clusters * ms->smp.cores * ms->smp.threads);
  2441. ms->possible_cpus->cpus[n].props.has_cluster_id = true;
  2442. ms->possible_cpus->cpus[n].props.cluster_id =
  2443. (n / (ms->smp.cores * ms->smp.threads)) % ms->smp.clusters;
  2444. ms->possible_cpus->cpus[n].props.has_core_id = true;
  2445. ms->possible_cpus->cpus[n].props.core_id =
  2446. (n / ms->smp.threads) % ms->smp.cores;
  2447. ms->possible_cpus->cpus[n].props.has_thread_id = true;
  2448. ms->possible_cpus->cpus[n].props.thread_id =
  2449. n % ms->smp.threads;
  2450. }
  2451. return ms->possible_cpus;
  2452. }
  2453. static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  2454. Error **errp)
  2455. {
  2456. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2457. const MachineState *ms = MACHINE(hotplug_dev);
  2458. const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  2459. if (!vms->acpi_dev) {
  2460. error_setg(errp,
  2461. "memory hotplug is not enabled: missing acpi-ged device");
  2462. return;
  2463. }
  2464. if (vms->mte) {
  2465. error_setg(errp, "memory hotplug is not enabled: MTE is enabled");
  2466. return;
  2467. }
  2468. if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
  2469. error_setg(errp, "nvdimm is not enabled: add 'nvdimm=on' to '-M'");
  2470. return;
  2471. }
  2472. pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), errp);
  2473. }
  2474. static void virt_memory_plug(HotplugHandler *hotplug_dev,
  2475. DeviceState *dev, Error **errp)
  2476. {
  2477. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2478. MachineState *ms = MACHINE(hotplug_dev);
  2479. bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  2480. pc_dimm_plug(PC_DIMM(dev), MACHINE(vms));
  2481. if (is_nvdimm) {
  2482. nvdimm_plug(ms->nvdimms_state);
  2483. }
  2484. hotplug_handler_plug(HOTPLUG_HANDLER(vms->acpi_dev),
  2485. dev, &error_abort);
  2486. }
  2487. static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  2488. DeviceState *dev, Error **errp)
  2489. {
  2490. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2491. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2492. virt_memory_pre_plug(hotplug_dev, dev, errp);
  2493. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2494. virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2495. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2496. hwaddr db_start = 0, db_end = 0;
  2497. QList *reserved_regions;
  2498. char *resv_prop_str;
  2499. if (vms->iommu != VIRT_IOMMU_NONE) {
  2500. error_setg(errp, "virt machine does not support multiple IOMMUs");
  2501. return;
  2502. }
  2503. switch (vms->msi_controller) {
  2504. case VIRT_MSI_CTRL_NONE:
  2505. return;
  2506. case VIRT_MSI_CTRL_ITS:
  2507. /* GITS_TRANSLATER page */
  2508. db_start = base_memmap[VIRT_GIC_ITS].base + 0x10000;
  2509. db_end = base_memmap[VIRT_GIC_ITS].base +
  2510. base_memmap[VIRT_GIC_ITS].size - 1;
  2511. break;
  2512. case VIRT_MSI_CTRL_GICV2M:
  2513. /* MSI_SETSPI_NS page */
  2514. db_start = base_memmap[VIRT_GIC_V2M].base;
  2515. db_end = db_start + base_memmap[VIRT_GIC_V2M].size - 1;
  2516. break;
  2517. }
  2518. resv_prop_str = g_strdup_printf("0x%"PRIx64":0x%"PRIx64":%u",
  2519. db_start, db_end,
  2520. VIRTIO_IOMMU_RESV_MEM_T_MSI);
  2521. reserved_regions = qlist_new();
  2522. qlist_append_str(reserved_regions, resv_prop_str);
  2523. qdev_prop_set_array(dev, "reserved-regions", reserved_regions);
  2524. g_free(resv_prop_str);
  2525. }
  2526. }
  2527. static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
  2528. DeviceState *dev, Error **errp)
  2529. {
  2530. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2531. if (vms->platform_bus_dev) {
  2532. MachineClass *mc = MACHINE_GET_CLASS(vms);
  2533. if (device_is_dynamic_sysbus(mc, dev)) {
  2534. platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
  2535. SYS_BUS_DEVICE(dev));
  2536. }
  2537. }
  2538. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2539. virt_memory_plug(hotplug_dev, dev, errp);
  2540. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2541. virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2542. }
  2543. if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2544. PCIDevice *pdev = PCI_DEVICE(dev);
  2545. vms->iommu = VIRT_IOMMU_VIRTIO;
  2546. vms->virtio_iommu_bdf = pci_get_bdf(pdev);
  2547. create_virtio_iommu_dt_bindings(vms);
  2548. }
  2549. }
  2550. static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev,
  2551. DeviceState *dev, Error **errp)
  2552. {
  2553. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2554. if (!vms->acpi_dev) {
  2555. error_setg(errp,
  2556. "memory hotplug is not enabled: missing acpi-ged device");
  2557. return;
  2558. }
  2559. if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
  2560. error_setg(errp, "nvdimm device hot unplug is not supported yet.");
  2561. return;
  2562. }
  2563. hotplug_handler_unplug_request(HOTPLUG_HANDLER(vms->acpi_dev), dev,
  2564. errp);
  2565. }
  2566. static void virt_dimm_unplug(HotplugHandler *hotplug_dev,
  2567. DeviceState *dev, Error **errp)
  2568. {
  2569. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2570. Error *local_err = NULL;
  2571. hotplug_handler_unplug(HOTPLUG_HANDLER(vms->acpi_dev), dev, &local_err);
  2572. if (local_err) {
  2573. goto out;
  2574. }
  2575. pc_dimm_unplug(PC_DIMM(dev), MACHINE(vms));
  2576. qdev_unrealize(dev);
  2577. out:
  2578. error_propagate(errp, local_err);
  2579. }
  2580. static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  2581. DeviceState *dev, Error **errp)
  2582. {
  2583. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2584. virt_dimm_unplug_request(hotplug_dev, dev, errp);
  2585. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2586. virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev),
  2587. errp);
  2588. } else {
  2589. error_setg(errp, "device unplug request for unsupported device"
  2590. " type: %s", object_get_typename(OBJECT(dev)));
  2591. }
  2592. }
  2593. static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
  2594. DeviceState *dev, Error **errp)
  2595. {
  2596. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2597. virt_dimm_unplug(hotplug_dev, dev, errp);
  2598. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2599. virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2600. } else {
  2601. error_setg(errp, "virt: device unplug for unsupported device"
  2602. " type: %s", object_get_typename(OBJECT(dev)));
  2603. }
  2604. }
  2605. static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
  2606. DeviceState *dev)
  2607. {
  2608. MachineClass *mc = MACHINE_GET_CLASS(machine);
  2609. if (device_is_dynamic_sysbus(mc, dev) ||
  2610. object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
  2611. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI) ||
  2612. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2613. return HOTPLUG_HANDLER(machine);
  2614. }
  2615. return NULL;
  2616. }
  2617. /*
  2618. * for arm64 kvm_type [7-0] encodes the requested number of bits
  2619. * in the IPA address space
  2620. */
  2621. static int virt_kvm_type(MachineState *ms, const char *type_str)
  2622. {
  2623. VirtMachineState *vms = VIRT_MACHINE(ms);
  2624. int max_vm_pa_size, requested_pa_size;
  2625. bool fixed_ipa;
  2626. max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms, &fixed_ipa);
  2627. /* we freeze the memory map to compute the highest gpa */
  2628. virt_set_memmap(vms, max_vm_pa_size);
  2629. requested_pa_size = 64 - clz64(vms->highest_gpa);
  2630. /*
  2631. * KVM requires the IPA size to be at least 32 bits.
  2632. */
  2633. if (requested_pa_size < 32) {
  2634. requested_pa_size = 32;
  2635. }
  2636. if (requested_pa_size > max_vm_pa_size) {
  2637. error_report("-m and ,maxmem option values "
  2638. "require an IPA range (%d bits) larger than "
  2639. "the one supported by the host (%d bits)",
  2640. requested_pa_size, max_vm_pa_size);
  2641. return -1;
  2642. }
  2643. /*
  2644. * We return the requested PA log size, unless KVM only supports
  2645. * the implicit legacy 40b IPA setting, in which case the kvm_type
  2646. * must be 0.
  2647. */
  2648. return fixed_ipa ? 0 : requested_pa_size;
  2649. }
  2650. static int virt_hvf_get_physical_address_range(MachineState *ms)
  2651. {
  2652. VirtMachineState *vms = VIRT_MACHINE(ms);
  2653. int default_ipa_size = hvf_arm_get_default_ipa_bit_size();
  2654. int max_ipa_size = hvf_arm_get_max_ipa_bit_size();
  2655. /* We freeze the memory map to compute the highest gpa */
  2656. virt_set_memmap(vms, max_ipa_size);
  2657. int requested_ipa_size = 64 - clz64(vms->highest_gpa);
  2658. /*
  2659. * If we're <= the default IPA size just use the default.
  2660. * If we're above the default but below the maximum, round up to
  2661. * the maximum. hvf_arm_get_max_ipa_bit_size() conveniently only
  2662. * returns values that are valid ARM PARange values.
  2663. */
  2664. if (requested_ipa_size <= default_ipa_size) {
  2665. requested_ipa_size = default_ipa_size;
  2666. } else if (requested_ipa_size <= max_ipa_size) {
  2667. requested_ipa_size = max_ipa_size;
  2668. } else {
  2669. error_report("-m and ,maxmem option values "
  2670. "require an IPA range (%d bits) larger than "
  2671. "the one supported by the host (%d bits)",
  2672. requested_ipa_size, max_ipa_size);
  2673. return -1;
  2674. }
  2675. return requested_ipa_size;
  2676. }
  2677. static void virt_machine_class_init(ObjectClass *oc, void *data)
  2678. {
  2679. MachineClass *mc = MACHINE_CLASS(oc);
  2680. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  2681. static const char * const valid_cpu_types[] = {
  2682. #ifdef CONFIG_TCG
  2683. ARM_CPU_TYPE_NAME("cortex-a7"),
  2684. ARM_CPU_TYPE_NAME("cortex-a15"),
  2685. #ifdef TARGET_AARCH64
  2686. ARM_CPU_TYPE_NAME("cortex-a35"),
  2687. ARM_CPU_TYPE_NAME("cortex-a55"),
  2688. ARM_CPU_TYPE_NAME("cortex-a72"),
  2689. ARM_CPU_TYPE_NAME("cortex-a76"),
  2690. ARM_CPU_TYPE_NAME("cortex-a710"),
  2691. ARM_CPU_TYPE_NAME("a64fx"),
  2692. ARM_CPU_TYPE_NAME("neoverse-n1"),
  2693. ARM_CPU_TYPE_NAME("neoverse-v1"),
  2694. ARM_CPU_TYPE_NAME("neoverse-n2"),
  2695. #endif /* TARGET_AARCH64 */
  2696. #endif /* CONFIG_TCG */
  2697. #ifdef TARGET_AARCH64
  2698. ARM_CPU_TYPE_NAME("cortex-a53"),
  2699. ARM_CPU_TYPE_NAME("cortex-a57"),
  2700. #if defined(CONFIG_KVM) || defined(CONFIG_HVF)
  2701. ARM_CPU_TYPE_NAME("host"),
  2702. #endif /* CONFIG_KVM || CONFIG_HVF */
  2703. #endif /* TARGET_AARCH64 */
  2704. ARM_CPU_TYPE_NAME("max"),
  2705. NULL
  2706. };
  2707. mc->init = machvirt_init;
  2708. /* Start with max_cpus set to 512, which is the maximum supported by KVM.
  2709. * The value may be reduced later when we have more information about the
  2710. * configuration of the particular instance.
  2711. */
  2712. mc->max_cpus = 512;
  2713. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
  2714. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
  2715. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
  2716. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
  2717. #ifdef CONFIG_TPM
  2718. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
  2719. #endif
  2720. mc->block_default_type = IF_VIRTIO;
  2721. mc->no_cdrom = 1;
  2722. mc->pci_allow_0_address = true;
  2723. /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
  2724. mc->minimum_page_bits = 12;
  2725. mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
  2726. mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
  2727. #ifdef CONFIG_TCG
  2728. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
  2729. #else
  2730. mc->default_cpu_type = ARM_CPU_TYPE_NAME("max");
  2731. #endif
  2732. mc->valid_cpu_types = valid_cpu_types;
  2733. mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
  2734. mc->kvm_type = virt_kvm_type;
  2735. mc->hvf_get_physical_address_range = virt_hvf_get_physical_address_range;
  2736. assert(!mc->get_hotplug_handler);
  2737. mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
  2738. hc->pre_plug = virt_machine_device_pre_plug_cb;
  2739. hc->plug = virt_machine_device_plug_cb;
  2740. hc->unplug_request = virt_machine_device_unplug_request_cb;
  2741. hc->unplug = virt_machine_device_unplug_cb;
  2742. mc->nvdimm_supported = true;
  2743. mc->smp_props.clusters_supported = true;
  2744. mc->auto_enable_numa_with_memhp = true;
  2745. mc->auto_enable_numa_with_memdev = true;
  2746. /* platform instead of architectural choice */
  2747. mc->cpu_cluster_has_numa_boundary = true;
  2748. mc->default_ram_id = "mach-virt.ram";
  2749. mc->default_nic = "virtio-net-pci";
  2750. object_class_property_add(oc, "acpi", "OnOffAuto",
  2751. virt_get_acpi, virt_set_acpi,
  2752. NULL, NULL);
  2753. object_class_property_set_description(oc, "acpi",
  2754. "Enable ACPI");
  2755. object_class_property_add_bool(oc, "secure", virt_get_secure,
  2756. virt_set_secure);
  2757. object_class_property_set_description(oc, "secure",
  2758. "Set on/off to enable/disable the ARM "
  2759. "Security Extensions (TrustZone)");
  2760. object_class_property_add_bool(oc, "virtualization", virt_get_virt,
  2761. virt_set_virt);
  2762. object_class_property_set_description(oc, "virtualization",
  2763. "Set on/off to enable/disable emulating a "
  2764. "guest CPU which implements the ARM "
  2765. "Virtualization Extensions");
  2766. object_class_property_add_bool(oc, "highmem", virt_get_highmem,
  2767. virt_set_highmem);
  2768. object_class_property_set_description(oc, "highmem",
  2769. "Set on/off to enable/disable using "
  2770. "physical address space above 32 bits");
  2771. object_class_property_add_bool(oc, "compact-highmem",
  2772. virt_get_compact_highmem,
  2773. virt_set_compact_highmem);
  2774. object_class_property_set_description(oc, "compact-highmem",
  2775. "Set on/off to enable/disable compact "
  2776. "layout for high memory regions");
  2777. object_class_property_add_bool(oc, "highmem-redists",
  2778. virt_get_highmem_redists,
  2779. virt_set_highmem_redists);
  2780. object_class_property_set_description(oc, "highmem-redists",
  2781. "Set on/off to enable/disable high "
  2782. "memory region for GICv3 or GICv4 "
  2783. "redistributor");
  2784. object_class_property_add_bool(oc, "highmem-ecam",
  2785. virt_get_highmem_ecam,
  2786. virt_set_highmem_ecam);
  2787. object_class_property_set_description(oc, "highmem-ecam",
  2788. "Set on/off to enable/disable high "
  2789. "memory region for PCI ECAM");
  2790. object_class_property_add_bool(oc, "highmem-mmio",
  2791. virt_get_highmem_mmio,
  2792. virt_set_highmem_mmio);
  2793. object_class_property_set_description(oc, "highmem-mmio",
  2794. "Set on/off to enable/disable high "
  2795. "memory region for PCI MMIO");
  2796. object_class_property_add(oc, "highmem-mmio-size", "size",
  2797. virt_get_highmem_mmio_size,
  2798. virt_set_highmem_mmio_size,
  2799. NULL, NULL);
  2800. object_class_property_set_description(oc, "highmem-mmio-size",
  2801. "Set the high memory region size "
  2802. "for PCI MMIO");
  2803. object_class_property_add_str(oc, "gic-version", virt_get_gic_version,
  2804. virt_set_gic_version);
  2805. object_class_property_set_description(oc, "gic-version",
  2806. "Set GIC version. "
  2807. "Valid values are 2, 3, 4, host and max");
  2808. object_class_property_add_str(oc, "iommu", virt_get_iommu, virt_set_iommu);
  2809. object_class_property_set_description(oc, "iommu",
  2810. "Set the IOMMU type. "
  2811. "Valid values are none and smmuv3");
  2812. object_class_property_add_bool(oc, "default-bus-bypass-iommu",
  2813. virt_get_default_bus_bypass_iommu,
  2814. virt_set_default_bus_bypass_iommu);
  2815. object_class_property_set_description(oc, "default-bus-bypass-iommu",
  2816. "Set on/off to enable/disable "
  2817. "bypass_iommu for default root bus");
  2818. object_class_property_add_bool(oc, "ras", virt_get_ras,
  2819. virt_set_ras);
  2820. object_class_property_set_description(oc, "ras",
  2821. "Set on/off to enable/disable reporting host memory errors "
  2822. "to a KVM guest using ACPI and guest external abort exceptions");
  2823. object_class_property_add_bool(oc, "mte", virt_get_mte, virt_set_mte);
  2824. object_class_property_set_description(oc, "mte",
  2825. "Set on/off to enable/disable emulating a "
  2826. "guest CPU which implements the ARM "
  2827. "Memory Tagging Extension");
  2828. object_class_property_add_bool(oc, "its", virt_get_its,
  2829. virt_set_its);
  2830. object_class_property_set_description(oc, "its",
  2831. "Set on/off to enable/disable "
  2832. "ITS instantiation");
  2833. object_class_property_add_bool(oc, "dtb-randomness",
  2834. virt_get_dtb_randomness,
  2835. virt_set_dtb_randomness);
  2836. object_class_property_set_description(oc, "dtb-randomness",
  2837. "Set off to disable passing random or "
  2838. "non-deterministic dtb nodes to guest");
  2839. object_class_property_add_bool(oc, "dtb-kaslr-seed",
  2840. virt_get_dtb_randomness,
  2841. virt_set_dtb_randomness);
  2842. object_class_property_set_description(oc, "dtb-kaslr-seed",
  2843. "Deprecated synonym of dtb-randomness");
  2844. object_class_property_add_str(oc, "x-oem-id",
  2845. virt_get_oem_id,
  2846. virt_set_oem_id);
  2847. object_class_property_set_description(oc, "x-oem-id",
  2848. "Override the default value of field OEMID "
  2849. "in ACPI table header."
  2850. "The string may be up to 6 bytes in size");
  2851. object_class_property_add_str(oc, "x-oem-table-id",
  2852. virt_get_oem_table_id,
  2853. virt_set_oem_table_id);
  2854. object_class_property_set_description(oc, "x-oem-table-id",
  2855. "Override the default value of field OEM Table ID "
  2856. "in ACPI table header."
  2857. "The string may be up to 8 bytes in size");
  2858. }
  2859. static void virt_instance_init(Object *obj)
  2860. {
  2861. VirtMachineState *vms = VIRT_MACHINE(obj);
  2862. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  2863. /* EL3 is disabled by default on virt: this makes us consistent
  2864. * between KVM and TCG for this board, and it also allows us to
  2865. * boot UEFI blobs which assume no TrustZone support.
  2866. */
  2867. vms->secure = false;
  2868. /* EL2 is also disabled by default, for similar reasons */
  2869. vms->virt = false;
  2870. /* High memory is enabled by default */
  2871. vms->highmem = true;
  2872. vms->highmem_compact = !vmc->no_highmem_compact;
  2873. vms->gic_version = VIRT_GIC_VERSION_NOSEL;
  2874. vms->highmem_ecam = !vmc->no_highmem_ecam;
  2875. vms->highmem_mmio = true;
  2876. vms->highmem_redists = true;
  2877. if (vmc->no_its) {
  2878. vms->its = false;
  2879. } else {
  2880. /* Default allows ITS instantiation */
  2881. vms->its = true;
  2882. if (vmc->no_tcg_its) {
  2883. vms->tcg_its = false;
  2884. } else {
  2885. vms->tcg_its = true;
  2886. }
  2887. }
  2888. /* Default disallows iommu instantiation */
  2889. vms->iommu = VIRT_IOMMU_NONE;
  2890. /* The default root bus is attached to iommu by default */
  2891. vms->default_bus_bypass_iommu = false;
  2892. /* Default disallows RAS instantiation */
  2893. vms->ras = false;
  2894. /* MTE is disabled by default. */
  2895. vms->mte = false;
  2896. /* Supply kaslr-seed and rng-seed by default */
  2897. vms->dtb_randomness = true;
  2898. vms->irqmap = a15irqmap;
  2899. virt_flash_create(vms);
  2900. vms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6);
  2901. vms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8);
  2902. }
  2903. static const TypeInfo virt_machine_info = {
  2904. .name = TYPE_VIRT_MACHINE,
  2905. .parent = TYPE_MACHINE,
  2906. .abstract = true,
  2907. .instance_size = sizeof(VirtMachineState),
  2908. .class_size = sizeof(VirtMachineClass),
  2909. .class_init = virt_machine_class_init,
  2910. .instance_init = virt_instance_init,
  2911. .interfaces = (InterfaceInfo[]) {
  2912. { TYPE_HOTPLUG_HANDLER },
  2913. { }
  2914. },
  2915. };
  2916. static void machvirt_machine_init(void)
  2917. {
  2918. type_register_static(&virt_machine_info);
  2919. }
  2920. type_init(machvirt_machine_init);
  2921. static void virt_machine_10_0_options(MachineClass *mc)
  2922. {
  2923. }
  2924. DEFINE_VIRT_MACHINE_AS_LATEST(10, 0)
  2925. static void virt_machine_9_2_options(MachineClass *mc)
  2926. {
  2927. virt_machine_10_0_options(mc);
  2928. compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len);
  2929. }
  2930. DEFINE_VIRT_MACHINE(9, 2)
  2931. static void virt_machine_9_1_options(MachineClass *mc)
  2932. {
  2933. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2934. virt_machine_9_2_options(mc);
  2935. compat_props_add(mc->compat_props, hw_compat_9_1, hw_compat_9_1_len);
  2936. /* 9.1 and earlier have only a stage-1 SMMU, not a nested s1+2 one */
  2937. vmc->no_nested_smmu = true;
  2938. }
  2939. DEFINE_VIRT_MACHINE(9, 1)
  2940. static void virt_machine_9_0_options(MachineClass *mc)
  2941. {
  2942. virt_machine_9_1_options(mc);
  2943. mc->smbios_memory_device_size = 16 * GiB;
  2944. compat_props_add(mc->compat_props, hw_compat_9_0, hw_compat_9_0_len);
  2945. }
  2946. DEFINE_VIRT_MACHINE(9, 0)
  2947. static void virt_machine_8_2_options(MachineClass *mc)
  2948. {
  2949. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2950. virt_machine_9_0_options(mc);
  2951. compat_props_add(mc->compat_props, hw_compat_8_2, hw_compat_8_2_len);
  2952. /*
  2953. * Don't expose NS_EL2_VIRT timer IRQ in DTB on ACPI on 8.2 and
  2954. * earlier machines. (Exposing it tickles a bug in older EDK2
  2955. * guest BIOS binaries.)
  2956. */
  2957. vmc->no_ns_el2_virt_timer_irq = true;
  2958. }
  2959. DEFINE_VIRT_MACHINE(8, 2)
  2960. static void virt_machine_8_1_options(MachineClass *mc)
  2961. {
  2962. virt_machine_8_2_options(mc);
  2963. compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len);
  2964. }
  2965. DEFINE_VIRT_MACHINE(8, 1)
  2966. static void virt_machine_8_0_options(MachineClass *mc)
  2967. {
  2968. virt_machine_8_1_options(mc);
  2969. compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len);
  2970. }
  2971. DEFINE_VIRT_MACHINE(8, 0)
  2972. static void virt_machine_7_2_options(MachineClass *mc)
  2973. {
  2974. virt_machine_8_0_options(mc);
  2975. compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len);
  2976. }
  2977. DEFINE_VIRT_MACHINE(7, 2)
  2978. static void virt_machine_7_1_options(MachineClass *mc)
  2979. {
  2980. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2981. virt_machine_7_2_options(mc);
  2982. compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
  2983. /* Compact layout for high memory regions was introduced with 7.2 */
  2984. vmc->no_highmem_compact = true;
  2985. }
  2986. DEFINE_VIRT_MACHINE(7, 1)
  2987. static void virt_machine_7_0_options(MachineClass *mc)
  2988. {
  2989. virt_machine_7_1_options(mc);
  2990. compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
  2991. }
  2992. DEFINE_VIRT_MACHINE(7, 0)
  2993. static void virt_machine_6_2_options(MachineClass *mc)
  2994. {
  2995. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2996. virt_machine_7_0_options(mc);
  2997. compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
  2998. vmc->no_tcg_lpa2 = true;
  2999. }
  3000. DEFINE_VIRT_MACHINE(6, 2)
  3001. static void virt_machine_6_1_options(MachineClass *mc)
  3002. {
  3003. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3004. virt_machine_6_2_options(mc);
  3005. compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
  3006. mc->smp_props.prefer_sockets = true;
  3007. vmc->no_cpu_topology = true;
  3008. /* qemu ITS was introduced with 6.2 */
  3009. vmc->no_tcg_its = true;
  3010. }
  3011. DEFINE_VIRT_MACHINE(6, 1)
  3012. static void virt_machine_6_0_options(MachineClass *mc)
  3013. {
  3014. virt_machine_6_1_options(mc);
  3015. compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
  3016. }
  3017. DEFINE_VIRT_MACHINE(6, 0)
  3018. static void virt_machine_5_2_options(MachineClass *mc)
  3019. {
  3020. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3021. virt_machine_6_0_options(mc);
  3022. compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len);
  3023. vmc->no_secure_gpio = true;
  3024. }
  3025. DEFINE_VIRT_MACHINE(5, 2)
  3026. static void virt_machine_5_1_options(MachineClass *mc)
  3027. {
  3028. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3029. virt_machine_5_2_options(mc);
  3030. compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);
  3031. vmc->no_kvm_steal_time = true;
  3032. }
  3033. DEFINE_VIRT_MACHINE(5, 1)
  3034. static void virt_machine_5_0_options(MachineClass *mc)
  3035. {
  3036. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3037. virt_machine_5_1_options(mc);
  3038. compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
  3039. mc->numa_mem_supported = true;
  3040. vmc->acpi_expose_flash = true;
  3041. mc->auto_enable_numa_with_memdev = false;
  3042. }
  3043. DEFINE_VIRT_MACHINE(5, 0)
  3044. static void virt_machine_4_2_options(MachineClass *mc)
  3045. {
  3046. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3047. virt_machine_5_0_options(mc);
  3048. compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
  3049. vmc->kvm_no_adjvtime = true;
  3050. }
  3051. DEFINE_VIRT_MACHINE(4, 2)
  3052. static void virt_machine_4_1_options(MachineClass *mc)
  3053. {
  3054. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3055. virt_machine_4_2_options(mc);
  3056. compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
  3057. vmc->no_ged = true;
  3058. mc->auto_enable_numa_with_memhp = false;
  3059. }
  3060. DEFINE_VIRT_MACHINE(4, 1)
  3061. static void virt_machine_4_0_options(MachineClass *mc)
  3062. {
  3063. virt_machine_4_1_options(mc);
  3064. compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
  3065. }
  3066. DEFINE_VIRT_MACHINE(4, 0)
  3067. static void virt_machine_3_1_options(MachineClass *mc)
  3068. {
  3069. virt_machine_4_0_options(mc);
  3070. compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
  3071. }
  3072. DEFINE_VIRT_MACHINE(3, 1)
  3073. static void virt_machine_3_0_options(MachineClass *mc)
  3074. {
  3075. virt_machine_3_1_options(mc);
  3076. compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
  3077. }
  3078. DEFINE_VIRT_MACHINE(3, 0)
  3079. static void virt_machine_2_12_options(MachineClass *mc)
  3080. {
  3081. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3082. virt_machine_3_0_options(mc);
  3083. compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
  3084. vmc->no_highmem_ecam = true;
  3085. mc->max_cpus = 255;
  3086. }
  3087. DEFINE_VIRT_MACHINE(2, 12)
  3088. static void virt_machine_2_11_options(MachineClass *mc)
  3089. {
  3090. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3091. virt_machine_2_12_options(mc);
  3092. compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
  3093. vmc->smbios_old_sys_ver = true;
  3094. }
  3095. DEFINE_VIRT_MACHINE(2, 11)
  3096. static void virt_machine_2_10_options(MachineClass *mc)
  3097. {
  3098. virt_machine_2_11_options(mc);
  3099. compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
  3100. /* before 2.11 we never faulted accesses to bad addresses */
  3101. mc->ignore_memory_transaction_failures = true;
  3102. }
  3103. DEFINE_VIRT_MACHINE(2, 10)
  3104. static void virt_machine_2_9_options(MachineClass *mc)
  3105. {
  3106. virt_machine_2_10_options(mc);
  3107. compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
  3108. }
  3109. DEFINE_VIRT_MACHINE(2, 9)
  3110. static void virt_machine_2_8_options(MachineClass *mc)
  3111. {
  3112. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3113. virt_machine_2_9_options(mc);
  3114. compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
  3115. /* For 2.8 and earlier we falsely claimed in the DT that
  3116. * our timers were edge-triggered, not level-triggered.
  3117. */
  3118. vmc->claim_edge_triggered_timers = true;
  3119. }
  3120. DEFINE_VIRT_MACHINE(2, 8)
  3121. static void virt_machine_2_7_options(MachineClass *mc)
  3122. {
  3123. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3124. virt_machine_2_8_options(mc);
  3125. compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
  3126. /* ITS was introduced with 2.8 */
  3127. vmc->no_its = true;
  3128. /* Stick with 1K pages for migration compatibility */
  3129. mc->minimum_page_bits = 0;
  3130. }
  3131. DEFINE_VIRT_MACHINE(2, 7)
  3132. static void virt_machine_2_6_options(MachineClass *mc)
  3133. {
  3134. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3135. virt_machine_2_7_options(mc);
  3136. compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
  3137. vmc->disallow_affinity_adjustment = true;
  3138. /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
  3139. vmc->no_pmu = true;
  3140. }
  3141. DEFINE_VIRT_MACHINE(2, 6)