virt.c 133 KB

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