virt.c 131 KB

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