virt.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620
  1. /*
  2. * ARM mach-virt emulation
  3. *
  4. * Copyright (c) 2013 Linaro Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2 or later, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Emulate a virtual board which works by passing Linux all the information
  19. * it needs about what devices are present via the device tree.
  20. * There are some restrictions about what we can do here:
  21. * + we can only present devices whose Linux drivers will work based
  22. * purely on the device tree with no platform data at all
  23. * + we want to present a very stripped-down minimalist platform,
  24. * both because this reduces the security attack surface from the guest
  25. * and also because it reduces our exposure to being broken when
  26. * the kernel updates its device tree bindings and requires further
  27. * information in a device binding that we aren't providing.
  28. * This is essentially the same approach kvmtool uses.
  29. */
  30. #include "qemu/osdep.h"
  31. #include "qemu/datadir.h"
  32. #include "qemu/units.h"
  33. #include "qemu/option.h"
  34. #include "monitor/qdev.h"
  35. #include "hw/sysbus.h"
  36. #include "hw/arm/boot.h"
  37. #include "hw/arm/primecell.h"
  38. #include "hw/arm/virt.h"
  39. #include "hw/block/flash.h"
  40. #include "hw/vfio/vfio-calxeda-xgmac.h"
  41. #include "hw/vfio/vfio-amd-xgbe.h"
  42. #include "hw/display/ramfb.h"
  43. #include "net/net.h"
  44. #include "system/device_tree.h"
  45. #include "system/numa.h"
  46. #include "system/runstate.h"
  47. #include "system/tpm.h"
  48. #include "system/tcg.h"
  49. #include "system/kvm.h"
  50. #include "system/hvf.h"
  51. #include "system/qtest.h"
  52. #include "hw/loader.h"
  53. #include "qapi/error.h"
  54. #include "qemu/bitops.h"
  55. #include "qemu/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 "qobject/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 < PCI_NUM_PINS; 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. pci_bus_add_fw_cfg_extra_pci_roots(vms->fw_cfg, vms->bus,
  1540. &error_abort);
  1541. virt_acpi_setup(vms);
  1542. virt_build_smbios(vms);
  1543. }
  1544. static uint64_t virt_cpu_mp_affinity(VirtMachineState *vms, int idx)
  1545. {
  1546. uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
  1547. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  1548. if (!vmc->disallow_affinity_adjustment) {
  1549. /* Adjust MPIDR like 64-bit KVM hosts, which incorporate the
  1550. * GIC's target-list limitations. 32-bit KVM hosts currently
  1551. * always create clusters of 4 CPUs, but that is expected to
  1552. * change when they gain support for gicv3. When KVM is enabled
  1553. * it will override the changes we make here, therefore our
  1554. * purposes are to make TCG consistent (with 64-bit KVM hosts)
  1555. * and to improve SGI efficiency.
  1556. */
  1557. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  1558. clustersz = GIC_TARGETLIST_BITS;
  1559. } else {
  1560. clustersz = GICV3_TARGETLIST_BITS;
  1561. }
  1562. }
  1563. return arm_build_mp_affinity(idx, clustersz);
  1564. }
  1565. static inline bool *virt_get_high_memmap_enabled(VirtMachineState *vms,
  1566. int index)
  1567. {
  1568. bool *enabled_array[] = {
  1569. &vms->highmem_redists,
  1570. &vms->highmem_ecam,
  1571. &vms->highmem_mmio,
  1572. };
  1573. assert(ARRAY_SIZE(extended_memmap) - VIRT_LOWMEMMAP_LAST ==
  1574. ARRAY_SIZE(enabled_array));
  1575. assert(index - VIRT_LOWMEMMAP_LAST < ARRAY_SIZE(enabled_array));
  1576. return enabled_array[index - VIRT_LOWMEMMAP_LAST];
  1577. }
  1578. static void virt_set_high_memmap(VirtMachineState *vms,
  1579. hwaddr base, int pa_bits)
  1580. {
  1581. hwaddr region_base, region_size;
  1582. bool *region_enabled, fits;
  1583. int i;
  1584. for (i = VIRT_LOWMEMMAP_LAST; i < ARRAY_SIZE(extended_memmap); i++) {
  1585. region_enabled = virt_get_high_memmap_enabled(vms, i);
  1586. region_base = ROUND_UP(base, extended_memmap[i].size);
  1587. region_size = extended_memmap[i].size;
  1588. vms->memmap[i].base = region_base;
  1589. vms->memmap[i].size = region_size;
  1590. /*
  1591. * Check each device to see if it fits in the PA space,
  1592. * moving highest_gpa as we go. For compatibility, move
  1593. * highest_gpa for disabled fitting devices as well, if
  1594. * the compact layout has been disabled.
  1595. *
  1596. * For each device that doesn't fit, disable it.
  1597. */
  1598. fits = (region_base + region_size) <= BIT_ULL(pa_bits);
  1599. *region_enabled &= fits;
  1600. if (vms->highmem_compact && !*region_enabled) {
  1601. continue;
  1602. }
  1603. base = region_base + region_size;
  1604. if (fits) {
  1605. vms->highest_gpa = base - 1;
  1606. }
  1607. }
  1608. }
  1609. static void virt_set_memmap(VirtMachineState *vms, int pa_bits)
  1610. {
  1611. MachineState *ms = MACHINE(vms);
  1612. hwaddr base, device_memory_base, device_memory_size, memtop;
  1613. int i;
  1614. vms->memmap = extended_memmap;
  1615. for (i = 0; i < ARRAY_SIZE(base_memmap); i++) {
  1616. vms->memmap[i] = base_memmap[i];
  1617. }
  1618. if (ms->ram_slots > ACPI_MAX_RAM_SLOTS) {
  1619. error_report("unsupported number of memory slots: %"PRIu64,
  1620. ms->ram_slots);
  1621. exit(EXIT_FAILURE);
  1622. }
  1623. /*
  1624. * !highmem is exactly the same as limiting the PA space to 32bit,
  1625. * irrespective of the underlying capabilities of the HW.
  1626. */
  1627. if (!vms->highmem) {
  1628. pa_bits = 32;
  1629. }
  1630. /*
  1631. * We compute the base of the high IO region depending on the
  1632. * amount of initial and device memory. The device memory start/size
  1633. * is aligned on 1GiB. We never put the high IO region below 256GiB
  1634. * so that if maxram_size is < 255GiB we keep the legacy memory map.
  1635. * The device region size assumes 1GiB page max alignment per slot.
  1636. */
  1637. device_memory_base =
  1638. ROUND_UP(vms->memmap[VIRT_MEM].base + ms->ram_size, GiB);
  1639. device_memory_size = ms->maxram_size - ms->ram_size + ms->ram_slots * GiB;
  1640. /* Base address of the high IO region */
  1641. memtop = base = device_memory_base + ROUND_UP(device_memory_size, GiB);
  1642. if (memtop > BIT_ULL(pa_bits)) {
  1643. error_report("Addressing limited to %d bits, but memory exceeds it by %llu bytes",
  1644. pa_bits, memtop - BIT_ULL(pa_bits));
  1645. exit(EXIT_FAILURE);
  1646. }
  1647. if (base < device_memory_base) {
  1648. error_report("maxmem/slots too huge");
  1649. exit(EXIT_FAILURE);
  1650. }
  1651. if (base < vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES) {
  1652. base = vms->memmap[VIRT_MEM].base + LEGACY_RAMLIMIT_BYTES;
  1653. }
  1654. /* We know for sure that at least the memory fits in the PA space */
  1655. vms->highest_gpa = memtop - 1;
  1656. virt_set_high_memmap(vms, base, pa_bits);
  1657. if (device_memory_size > 0) {
  1658. machine_memory_devices_init(ms, device_memory_base, device_memory_size);
  1659. }
  1660. }
  1661. static VirtGICType finalize_gic_version_do(const char *accel_name,
  1662. VirtGICType gic_version,
  1663. int gics_supported,
  1664. unsigned int max_cpus)
  1665. {
  1666. /* Convert host/max/nosel to GIC version number */
  1667. switch (gic_version) {
  1668. case VIRT_GIC_VERSION_HOST:
  1669. if (!kvm_enabled()) {
  1670. error_report("gic-version=host requires KVM");
  1671. exit(1);
  1672. }
  1673. /* For KVM, gic-version=host means gic-version=max */
  1674. return finalize_gic_version_do(accel_name, VIRT_GIC_VERSION_MAX,
  1675. gics_supported, max_cpus);
  1676. case VIRT_GIC_VERSION_MAX:
  1677. if (gics_supported & VIRT_GIC_VERSION_4_MASK) {
  1678. gic_version = VIRT_GIC_VERSION_4;
  1679. } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) {
  1680. gic_version = VIRT_GIC_VERSION_3;
  1681. } else {
  1682. gic_version = VIRT_GIC_VERSION_2;
  1683. }
  1684. break;
  1685. case VIRT_GIC_VERSION_NOSEL:
  1686. if ((gics_supported & VIRT_GIC_VERSION_2_MASK) &&
  1687. max_cpus <= GIC_NCPU) {
  1688. gic_version = VIRT_GIC_VERSION_2;
  1689. } else if (gics_supported & VIRT_GIC_VERSION_3_MASK) {
  1690. /*
  1691. * in case the host does not support v2 emulation or
  1692. * the end-user requested more than 8 VCPUs we now default
  1693. * to v3. In any case defaulting to v2 would be broken.
  1694. */
  1695. gic_version = VIRT_GIC_VERSION_3;
  1696. } else if (max_cpus > GIC_NCPU) {
  1697. error_report("%s only supports GICv2 emulation but more than 8 "
  1698. "vcpus are requested", accel_name);
  1699. exit(1);
  1700. }
  1701. break;
  1702. case VIRT_GIC_VERSION_2:
  1703. case VIRT_GIC_VERSION_3:
  1704. case VIRT_GIC_VERSION_4:
  1705. break;
  1706. }
  1707. /* Check chosen version is effectively supported */
  1708. switch (gic_version) {
  1709. case VIRT_GIC_VERSION_2:
  1710. if (!(gics_supported & VIRT_GIC_VERSION_2_MASK)) {
  1711. error_report("%s does not support GICv2 emulation", accel_name);
  1712. exit(1);
  1713. }
  1714. break;
  1715. case VIRT_GIC_VERSION_3:
  1716. if (!(gics_supported & VIRT_GIC_VERSION_3_MASK)) {
  1717. error_report("%s does not support GICv3 emulation", accel_name);
  1718. exit(1);
  1719. }
  1720. break;
  1721. case VIRT_GIC_VERSION_4:
  1722. if (!(gics_supported & VIRT_GIC_VERSION_4_MASK)) {
  1723. error_report("%s does not support GICv4 emulation, is virtualization=on?",
  1724. accel_name);
  1725. exit(1);
  1726. }
  1727. break;
  1728. default:
  1729. error_report("logic error in finalize_gic_version");
  1730. exit(1);
  1731. break;
  1732. }
  1733. return gic_version;
  1734. }
  1735. /*
  1736. * finalize_gic_version - Determines the final gic_version
  1737. * according to the gic-version property
  1738. *
  1739. * Default GIC type is v2
  1740. */
  1741. static void finalize_gic_version(VirtMachineState *vms)
  1742. {
  1743. const char *accel_name = current_accel_name();
  1744. unsigned int max_cpus = MACHINE(vms)->smp.max_cpus;
  1745. int gics_supported = 0;
  1746. /* Determine which GIC versions the current environment supports */
  1747. if (kvm_enabled() && kvm_irqchip_in_kernel()) {
  1748. int probe_bitmap = kvm_arm_vgic_probe();
  1749. if (!probe_bitmap) {
  1750. error_report("Unable to determine GIC version supported by host");
  1751. exit(1);
  1752. }
  1753. if (probe_bitmap & KVM_ARM_VGIC_V2) {
  1754. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1755. }
  1756. if (probe_bitmap & KVM_ARM_VGIC_V3) {
  1757. gics_supported |= VIRT_GIC_VERSION_3_MASK;
  1758. }
  1759. } else if (kvm_enabled() && !kvm_irqchip_in_kernel()) {
  1760. /* KVM w/o kernel irqchip can only deal with GICv2 */
  1761. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1762. accel_name = "KVM with kernel-irqchip=off";
  1763. } else if (tcg_enabled() || hvf_enabled() || qtest_enabled()) {
  1764. gics_supported |= VIRT_GIC_VERSION_2_MASK;
  1765. if (module_object_class_by_name("arm-gicv3")) {
  1766. gics_supported |= VIRT_GIC_VERSION_3_MASK;
  1767. if (vms->virt) {
  1768. /* GICv4 only makes sense if CPU has EL2 */
  1769. gics_supported |= VIRT_GIC_VERSION_4_MASK;
  1770. }
  1771. }
  1772. } else {
  1773. error_report("Unsupported accelerator, can not determine GIC support");
  1774. exit(1);
  1775. }
  1776. /*
  1777. * Then convert helpers like host/max to concrete GIC versions and ensure
  1778. * the desired version is supported
  1779. */
  1780. vms->gic_version = finalize_gic_version_do(accel_name, vms->gic_version,
  1781. gics_supported, max_cpus);
  1782. }
  1783. /*
  1784. * virt_cpu_post_init() must be called after the CPUs have
  1785. * been realized and the GIC has been created.
  1786. */
  1787. static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
  1788. {
  1789. int max_cpus = MACHINE(vms)->smp.max_cpus;
  1790. bool aarch64, pmu, steal_time;
  1791. CPUState *cpu;
  1792. aarch64 = object_property_get_bool(OBJECT(first_cpu), "aarch64", NULL);
  1793. pmu = object_property_get_bool(OBJECT(first_cpu), "pmu", NULL);
  1794. steal_time = object_property_get_bool(OBJECT(first_cpu),
  1795. "kvm-steal-time", NULL);
  1796. if (kvm_enabled()) {
  1797. hwaddr pvtime_reg_base = vms->memmap[VIRT_PVTIME].base;
  1798. hwaddr pvtime_reg_size = vms->memmap[VIRT_PVTIME].size;
  1799. if (steal_time) {
  1800. MemoryRegion *pvtime = g_new(MemoryRegion, 1);
  1801. hwaddr pvtime_size = max_cpus * PVTIME_SIZE_PER_CPU;
  1802. /* The memory region size must be a multiple of host page size. */
  1803. pvtime_size = REAL_HOST_PAGE_ALIGN(pvtime_size);
  1804. if (pvtime_size > pvtime_reg_size) {
  1805. error_report("pvtime requires a %" HWADDR_PRId
  1806. " byte memory region for %d CPUs,"
  1807. " but only %" HWADDR_PRId " has been reserved",
  1808. pvtime_size, max_cpus, pvtime_reg_size);
  1809. exit(1);
  1810. }
  1811. memory_region_init_ram(pvtime, NULL, "pvtime", pvtime_size, NULL);
  1812. memory_region_add_subregion(sysmem, pvtime_reg_base, pvtime);
  1813. }
  1814. CPU_FOREACH(cpu) {
  1815. if (pmu) {
  1816. assert(arm_feature(&ARM_CPU(cpu)->env, ARM_FEATURE_PMU));
  1817. if (kvm_irqchip_in_kernel()) {
  1818. kvm_arm_pmu_set_irq(ARM_CPU(cpu), VIRTUAL_PMU_IRQ);
  1819. }
  1820. kvm_arm_pmu_init(ARM_CPU(cpu));
  1821. }
  1822. if (steal_time) {
  1823. kvm_arm_pvtime_init(ARM_CPU(cpu), pvtime_reg_base
  1824. + cpu->cpu_index
  1825. * PVTIME_SIZE_PER_CPU);
  1826. }
  1827. }
  1828. } else {
  1829. if (aarch64 && vms->highmem) {
  1830. int requested_pa_size = 64 - clz64(vms->highest_gpa);
  1831. int pamax = arm_pamax(ARM_CPU(first_cpu));
  1832. if (pamax < requested_pa_size) {
  1833. error_report("VCPU supports less PA bits (%d) than "
  1834. "requested by the memory map (%d)",
  1835. pamax, requested_pa_size);
  1836. exit(1);
  1837. }
  1838. }
  1839. }
  1840. }
  1841. static void machvirt_init(MachineState *machine)
  1842. {
  1843. VirtMachineState *vms = VIRT_MACHINE(machine);
  1844. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(machine);
  1845. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1846. const CPUArchIdList *possible_cpus;
  1847. MemoryRegion *sysmem = get_system_memory();
  1848. MemoryRegion *secure_sysmem = NULL;
  1849. MemoryRegion *tag_sysmem = NULL;
  1850. MemoryRegion *secure_tag_sysmem = NULL;
  1851. int n, virt_max_cpus;
  1852. bool firmware_loaded;
  1853. bool aarch64 = true;
  1854. bool has_ged = !vmc->no_ged;
  1855. unsigned int smp_cpus = machine->smp.cpus;
  1856. unsigned int max_cpus = machine->smp.max_cpus;
  1857. possible_cpus = mc->possible_cpu_arch_ids(machine);
  1858. /*
  1859. * In accelerated mode, the memory map is computed earlier in kvm_type()
  1860. * for Linux, or hvf_get_physical_address_range() for macOS to create a
  1861. * VM with the right number of IPA bits.
  1862. */
  1863. if (!vms->memmap) {
  1864. Object *cpuobj;
  1865. ARMCPU *armcpu;
  1866. int pa_bits;
  1867. /*
  1868. * Instantiate a temporary CPU object to find out about what
  1869. * we are about to deal with. Once this is done, get rid of
  1870. * the object.
  1871. */
  1872. cpuobj = object_new(possible_cpus->cpus[0].type);
  1873. armcpu = ARM_CPU(cpuobj);
  1874. pa_bits = arm_pamax(armcpu);
  1875. object_unref(cpuobj);
  1876. virt_set_memmap(vms, pa_bits);
  1877. }
  1878. /* We can probe only here because during property set
  1879. * KVM is not available yet
  1880. */
  1881. finalize_gic_version(vms);
  1882. if (vms->secure) {
  1883. /*
  1884. * The Secure view of the world is the same as the NonSecure,
  1885. * but with a few extra devices. Create it as a container region
  1886. * containing the system memory at low priority; any secure-only
  1887. * devices go in at higher priority and take precedence.
  1888. */
  1889. secure_sysmem = g_new(MemoryRegion, 1);
  1890. memory_region_init(secure_sysmem, OBJECT(machine), "secure-memory",
  1891. UINT64_MAX);
  1892. memory_region_add_subregion_overlap(secure_sysmem, 0, sysmem, -1);
  1893. }
  1894. firmware_loaded = virt_firmware_init(vms, sysmem,
  1895. secure_sysmem ?: sysmem);
  1896. /* If we have an EL3 boot ROM then the assumption is that it will
  1897. * implement PSCI itself, so disable QEMU's internal implementation
  1898. * so it doesn't get in the way. Instead of starting secondary
  1899. * CPUs in PSCI powerdown state we will start them all running and
  1900. * let the boot ROM sort them out.
  1901. * The usual case is that we do use QEMU's PSCI implementation;
  1902. * if the guest has EL2 then we will use SMC as the conduit,
  1903. * and otherwise we will use HVC (for backwards compatibility and
  1904. * because if we're using KVM then we must use HVC).
  1905. */
  1906. if (vms->secure && firmware_loaded) {
  1907. vms->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
  1908. } else if (vms->virt) {
  1909. vms->psci_conduit = QEMU_PSCI_CONDUIT_SMC;
  1910. } else {
  1911. vms->psci_conduit = QEMU_PSCI_CONDUIT_HVC;
  1912. }
  1913. /*
  1914. * The maximum number of CPUs depends on the GIC version, or on how
  1915. * many redistributors we can fit into the memory map (which in turn
  1916. * depends on whether this is a GICv3 or v4).
  1917. */
  1918. if (vms->gic_version == VIRT_GIC_VERSION_2) {
  1919. virt_max_cpus = GIC_NCPU;
  1920. } else {
  1921. virt_max_cpus = virt_redist_capacity(vms, VIRT_GIC_REDIST);
  1922. if (vms->highmem_redists) {
  1923. virt_max_cpus += virt_redist_capacity(vms, VIRT_HIGH_GIC_REDIST2);
  1924. }
  1925. }
  1926. if (max_cpus > virt_max_cpus) {
  1927. error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
  1928. "supported by machine 'mach-virt' (%d)",
  1929. max_cpus, virt_max_cpus);
  1930. if (vms->gic_version != VIRT_GIC_VERSION_2 && !vms->highmem_redists) {
  1931. error_printf("Try 'highmem-redists=on' for more CPUs\n");
  1932. }
  1933. exit(1);
  1934. }
  1935. if (vms->secure && (kvm_enabled() || hvf_enabled())) {
  1936. error_report("mach-virt: %s does not support providing "
  1937. "Security extensions (TrustZone) to the guest CPU",
  1938. current_accel_name());
  1939. exit(1);
  1940. }
  1941. if (vms->virt && (kvm_enabled() || hvf_enabled())) {
  1942. error_report("mach-virt: %s does not support providing "
  1943. "Virtualization extensions to the guest CPU",
  1944. current_accel_name());
  1945. exit(1);
  1946. }
  1947. if (vms->mte && hvf_enabled()) {
  1948. error_report("mach-virt: %s does not support providing "
  1949. "MTE to the guest CPU",
  1950. current_accel_name());
  1951. exit(1);
  1952. }
  1953. create_fdt(vms);
  1954. assert(possible_cpus->len == max_cpus);
  1955. for (n = 0; n < possible_cpus->len; n++) {
  1956. Object *cpuobj;
  1957. CPUState *cs;
  1958. if (n >= smp_cpus) {
  1959. break;
  1960. }
  1961. cpuobj = object_new(possible_cpus->cpus[n].type);
  1962. object_property_set_int(cpuobj, "mp-affinity",
  1963. possible_cpus->cpus[n].arch_id, NULL);
  1964. cs = CPU(cpuobj);
  1965. cs->cpu_index = n;
  1966. numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpuobj),
  1967. &error_fatal);
  1968. aarch64 &= object_property_get_bool(cpuobj, "aarch64", NULL);
  1969. if (!vms->secure) {
  1970. object_property_set_bool(cpuobj, "has_el3", false, NULL);
  1971. }
  1972. if (!vms->virt && object_property_find(cpuobj, "has_el2")) {
  1973. object_property_set_bool(cpuobj, "has_el2", false, NULL);
  1974. }
  1975. if (vmc->kvm_no_adjvtime &&
  1976. object_property_find(cpuobj, "kvm-no-adjvtime")) {
  1977. object_property_set_bool(cpuobj, "kvm-no-adjvtime", true, NULL);
  1978. }
  1979. if (vmc->no_kvm_steal_time &&
  1980. object_property_find(cpuobj, "kvm-steal-time")) {
  1981. object_property_set_bool(cpuobj, "kvm-steal-time", false, NULL);
  1982. }
  1983. if (vmc->no_pmu && object_property_find(cpuobj, "pmu")) {
  1984. object_property_set_bool(cpuobj, "pmu", false, NULL);
  1985. }
  1986. if (vmc->no_tcg_lpa2 && object_property_find(cpuobj, "lpa2")) {
  1987. object_property_set_bool(cpuobj, "lpa2", false, NULL);
  1988. }
  1989. if (object_property_find(cpuobj, "reset-cbar")) {
  1990. object_property_set_int(cpuobj, "reset-cbar",
  1991. vms->memmap[VIRT_CPUPERIPHS].base,
  1992. &error_abort);
  1993. }
  1994. object_property_set_link(cpuobj, "memory", OBJECT(sysmem),
  1995. &error_abort);
  1996. if (vms->secure) {
  1997. object_property_set_link(cpuobj, "secure-memory",
  1998. OBJECT(secure_sysmem), &error_abort);
  1999. }
  2000. if (vms->mte) {
  2001. if (tcg_enabled()) {
  2002. /* Create the memory region only once, but link to all cpus. */
  2003. if (!tag_sysmem) {
  2004. /*
  2005. * The property exists only if MemTag is supported.
  2006. * If it is, we must allocate the ram to back that up.
  2007. */
  2008. if (!object_property_find(cpuobj, "tag-memory")) {
  2009. error_report("MTE requested, but not supported "
  2010. "by the guest CPU");
  2011. exit(1);
  2012. }
  2013. tag_sysmem = g_new(MemoryRegion, 1);
  2014. memory_region_init(tag_sysmem, OBJECT(machine),
  2015. "tag-memory", UINT64_MAX / 32);
  2016. if (vms->secure) {
  2017. secure_tag_sysmem = g_new(MemoryRegion, 1);
  2018. memory_region_init(secure_tag_sysmem, OBJECT(machine),
  2019. "secure-tag-memory",
  2020. UINT64_MAX / 32);
  2021. /* As with ram, secure-tag takes precedence over tag. */
  2022. memory_region_add_subregion_overlap(secure_tag_sysmem,
  2023. 0, tag_sysmem, -1);
  2024. }
  2025. }
  2026. object_property_set_link(cpuobj, "tag-memory",
  2027. OBJECT(tag_sysmem), &error_abort);
  2028. if (vms->secure) {
  2029. object_property_set_link(cpuobj, "secure-tag-memory",
  2030. OBJECT(secure_tag_sysmem),
  2031. &error_abort);
  2032. }
  2033. } else if (kvm_enabled()) {
  2034. if (!kvm_arm_mte_supported()) {
  2035. error_report("MTE requested, but not supported by KVM");
  2036. exit(1);
  2037. }
  2038. kvm_arm_enable_mte(cpuobj, &error_abort);
  2039. } else {
  2040. error_report("MTE requested, but not supported ");
  2041. exit(1);
  2042. }
  2043. }
  2044. qdev_realize(DEVICE(cpuobj), NULL, &error_fatal);
  2045. object_unref(cpuobj);
  2046. }
  2047. /* Now we've created the CPUs we can see if they have the hypvirt timer */
  2048. vms->ns_el2_virt_timer_irq = ns_el2_virt_timer_present() &&
  2049. !vmc->no_ns_el2_virt_timer_irq;
  2050. fdt_add_timer_nodes(vms);
  2051. fdt_add_cpu_nodes(vms);
  2052. memory_region_add_subregion(sysmem, vms->memmap[VIRT_MEM].base,
  2053. machine->ram);
  2054. virt_flash_fdt(vms, sysmem, secure_sysmem ?: sysmem);
  2055. create_gic(vms, sysmem);
  2056. virt_cpu_post_init(vms, sysmem);
  2057. fdt_add_pmu_nodes(vms);
  2058. /*
  2059. * The first UART always exists. If the security extensions are
  2060. * enabled, the second UART also always exists. Otherwise, it only exists
  2061. * if a backend is configured explicitly via '-serial <backend>'.
  2062. * This avoids potentially breaking existing user setups that expect
  2063. * only one NonSecure UART to be present (for instance, older EDK2
  2064. * binaries).
  2065. *
  2066. * The nodes end up in the DTB in reverse order of creation, so we must
  2067. * create UART0 last to ensure it appears as the first node in the DTB,
  2068. * for compatibility with guest software that just iterates through the
  2069. * DTB to find the first UART, as older versions of EDK2 do.
  2070. * DTB readers that follow the spec, as Linux does, should honour the
  2071. * aliases node information and /chosen/stdout-path regardless of
  2072. * the order that nodes appear in the DTB.
  2073. *
  2074. * For similar back-compatibility reasons, if UART1 is the secure UART
  2075. * we create it second (and so it appears first in the DTB), because
  2076. * that's what QEMU has always done.
  2077. */
  2078. if (!vms->secure) {
  2079. Chardev *serial1 = serial_hd(1);
  2080. if (serial1) {
  2081. vms->second_ns_uart_present = true;
  2082. create_uart(vms, VIRT_UART1, sysmem, serial1, false);
  2083. }
  2084. }
  2085. create_uart(vms, VIRT_UART0, sysmem, serial_hd(0), false);
  2086. if (vms->secure) {
  2087. create_uart(vms, VIRT_UART1, secure_sysmem, serial_hd(1), true);
  2088. }
  2089. if (vms->secure) {
  2090. create_secure_ram(vms, secure_sysmem, secure_tag_sysmem);
  2091. }
  2092. if (tag_sysmem) {
  2093. create_tag_ram(tag_sysmem, vms->memmap[VIRT_MEM].base,
  2094. machine->ram_size, "mach-virt.tag");
  2095. }
  2096. vms->highmem_ecam &= (!firmware_loaded || aarch64);
  2097. create_rtc(vms);
  2098. create_pcie(vms);
  2099. if (has_ged && aarch64 && firmware_loaded && virt_is_acpi_enabled(vms)) {
  2100. vms->acpi_dev = create_acpi_ged(vms);
  2101. } else {
  2102. create_gpio_devices(vms, VIRT_GPIO, sysmem);
  2103. }
  2104. if (vms->secure && !vmc->no_secure_gpio) {
  2105. create_gpio_devices(vms, VIRT_SECURE_GPIO, secure_sysmem);
  2106. }
  2107. /* connect powerdown request */
  2108. vms->powerdown_notifier.notify = virt_powerdown_req;
  2109. qemu_register_powerdown_notifier(&vms->powerdown_notifier);
  2110. /* Create mmio transports, so the user can create virtio backends
  2111. * (which will be automatically plugged in to the transports). If
  2112. * no backend is created the transport will just sit harmlessly idle.
  2113. */
  2114. create_virtio_devices(vms);
  2115. vms->fw_cfg = create_fw_cfg(vms, &address_space_memory);
  2116. rom_set_fw(vms->fw_cfg);
  2117. create_platform_bus(vms);
  2118. if (machine->nvdimms_state->is_enabled) {
  2119. const struct AcpiGenericAddress arm_virt_nvdimm_acpi_dsmio = {
  2120. .space_id = AML_AS_SYSTEM_MEMORY,
  2121. .address = vms->memmap[VIRT_NVDIMM_ACPI].base,
  2122. .bit_width = NVDIMM_ACPI_IO_LEN << 3
  2123. };
  2124. nvdimm_init_acpi_state(machine->nvdimms_state, sysmem,
  2125. arm_virt_nvdimm_acpi_dsmio,
  2126. vms->fw_cfg, OBJECT(vms));
  2127. }
  2128. vms->bootinfo.ram_size = machine->ram_size;
  2129. vms->bootinfo.board_id = -1;
  2130. vms->bootinfo.loader_start = vms->memmap[VIRT_MEM].base;
  2131. vms->bootinfo.get_dtb = machvirt_dtb;
  2132. vms->bootinfo.skip_dtb_autoload = true;
  2133. vms->bootinfo.firmware_loaded = firmware_loaded;
  2134. vms->bootinfo.psci_conduit = vms->psci_conduit;
  2135. arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
  2136. vms->machine_done.notify = virt_machine_done;
  2137. qemu_add_machine_init_done_notifier(&vms->machine_done);
  2138. }
  2139. static bool virt_get_secure(Object *obj, Error **errp)
  2140. {
  2141. VirtMachineState *vms = VIRT_MACHINE(obj);
  2142. return vms->secure;
  2143. }
  2144. static void virt_set_secure(Object *obj, bool value, Error **errp)
  2145. {
  2146. VirtMachineState *vms = VIRT_MACHINE(obj);
  2147. vms->secure = value;
  2148. }
  2149. static bool virt_get_virt(Object *obj, Error **errp)
  2150. {
  2151. VirtMachineState *vms = VIRT_MACHINE(obj);
  2152. return vms->virt;
  2153. }
  2154. static void virt_set_virt(Object *obj, bool value, Error **errp)
  2155. {
  2156. VirtMachineState *vms = VIRT_MACHINE(obj);
  2157. vms->virt = value;
  2158. }
  2159. static bool virt_get_highmem(Object *obj, Error **errp)
  2160. {
  2161. VirtMachineState *vms = VIRT_MACHINE(obj);
  2162. return vms->highmem;
  2163. }
  2164. static void virt_set_highmem(Object *obj, bool value, Error **errp)
  2165. {
  2166. VirtMachineState *vms = VIRT_MACHINE(obj);
  2167. vms->highmem = value;
  2168. }
  2169. static bool virt_get_compact_highmem(Object *obj, Error **errp)
  2170. {
  2171. VirtMachineState *vms = VIRT_MACHINE(obj);
  2172. return vms->highmem_compact;
  2173. }
  2174. static void virt_set_compact_highmem(Object *obj, bool value, Error **errp)
  2175. {
  2176. VirtMachineState *vms = VIRT_MACHINE(obj);
  2177. vms->highmem_compact = value;
  2178. }
  2179. static bool virt_get_highmem_redists(Object *obj, Error **errp)
  2180. {
  2181. VirtMachineState *vms = VIRT_MACHINE(obj);
  2182. return vms->highmem_redists;
  2183. }
  2184. static void virt_set_highmem_redists(Object *obj, bool value, Error **errp)
  2185. {
  2186. VirtMachineState *vms = VIRT_MACHINE(obj);
  2187. vms->highmem_redists = value;
  2188. }
  2189. static bool virt_get_highmem_ecam(Object *obj, Error **errp)
  2190. {
  2191. VirtMachineState *vms = VIRT_MACHINE(obj);
  2192. return vms->highmem_ecam;
  2193. }
  2194. static void virt_set_highmem_ecam(Object *obj, bool value, Error **errp)
  2195. {
  2196. VirtMachineState *vms = VIRT_MACHINE(obj);
  2197. vms->highmem_ecam = value;
  2198. }
  2199. static bool virt_get_highmem_mmio(Object *obj, Error **errp)
  2200. {
  2201. VirtMachineState *vms = VIRT_MACHINE(obj);
  2202. return vms->highmem_mmio;
  2203. }
  2204. static void virt_set_highmem_mmio(Object *obj, bool value, Error **errp)
  2205. {
  2206. VirtMachineState *vms = VIRT_MACHINE(obj);
  2207. vms->highmem_mmio = value;
  2208. }
  2209. static bool virt_get_its(Object *obj, Error **errp)
  2210. {
  2211. VirtMachineState *vms = VIRT_MACHINE(obj);
  2212. return vms->its;
  2213. }
  2214. static void virt_set_its(Object *obj, bool value, Error **errp)
  2215. {
  2216. VirtMachineState *vms = VIRT_MACHINE(obj);
  2217. vms->its = value;
  2218. }
  2219. static bool virt_get_dtb_randomness(Object *obj, Error **errp)
  2220. {
  2221. VirtMachineState *vms = VIRT_MACHINE(obj);
  2222. return vms->dtb_randomness;
  2223. }
  2224. static void virt_set_dtb_randomness(Object *obj, bool value, Error **errp)
  2225. {
  2226. VirtMachineState *vms = VIRT_MACHINE(obj);
  2227. vms->dtb_randomness = value;
  2228. }
  2229. static char *virt_get_oem_id(Object *obj, Error **errp)
  2230. {
  2231. VirtMachineState *vms = VIRT_MACHINE(obj);
  2232. return g_strdup(vms->oem_id);
  2233. }
  2234. static void virt_set_oem_id(Object *obj, const char *value, Error **errp)
  2235. {
  2236. VirtMachineState *vms = VIRT_MACHINE(obj);
  2237. size_t len = strlen(value);
  2238. if (len > 6) {
  2239. error_setg(errp,
  2240. "User specified oem-id value is bigger than 6 bytes in size");
  2241. return;
  2242. }
  2243. strncpy(vms->oem_id, value, 6);
  2244. }
  2245. static char *virt_get_oem_table_id(Object *obj, Error **errp)
  2246. {
  2247. VirtMachineState *vms = VIRT_MACHINE(obj);
  2248. return g_strdup(vms->oem_table_id);
  2249. }
  2250. static void virt_set_oem_table_id(Object *obj, const char *value,
  2251. Error **errp)
  2252. {
  2253. VirtMachineState *vms = VIRT_MACHINE(obj);
  2254. size_t len = strlen(value);
  2255. if (len > 8) {
  2256. error_setg(errp,
  2257. "User specified oem-table-id value is bigger than 8 bytes in size");
  2258. return;
  2259. }
  2260. strncpy(vms->oem_table_id, value, 8);
  2261. }
  2262. bool virt_is_acpi_enabled(VirtMachineState *vms)
  2263. {
  2264. if (vms->acpi == ON_OFF_AUTO_OFF) {
  2265. return false;
  2266. }
  2267. return true;
  2268. }
  2269. static void virt_get_acpi(Object *obj, Visitor *v, const char *name,
  2270. void *opaque, Error **errp)
  2271. {
  2272. VirtMachineState *vms = VIRT_MACHINE(obj);
  2273. OnOffAuto acpi = vms->acpi;
  2274. visit_type_OnOffAuto(v, name, &acpi, errp);
  2275. }
  2276. static void virt_set_acpi(Object *obj, Visitor *v, const char *name,
  2277. void *opaque, Error **errp)
  2278. {
  2279. VirtMachineState *vms = VIRT_MACHINE(obj);
  2280. visit_type_OnOffAuto(v, name, &vms->acpi, errp);
  2281. }
  2282. static bool virt_get_ras(Object *obj, Error **errp)
  2283. {
  2284. VirtMachineState *vms = VIRT_MACHINE(obj);
  2285. return vms->ras;
  2286. }
  2287. static void virt_set_ras(Object *obj, bool value, Error **errp)
  2288. {
  2289. VirtMachineState *vms = VIRT_MACHINE(obj);
  2290. vms->ras = value;
  2291. }
  2292. static bool virt_get_mte(Object *obj, Error **errp)
  2293. {
  2294. VirtMachineState *vms = VIRT_MACHINE(obj);
  2295. return vms->mte;
  2296. }
  2297. static void virt_set_mte(Object *obj, bool value, Error **errp)
  2298. {
  2299. VirtMachineState *vms = VIRT_MACHINE(obj);
  2300. vms->mte = value;
  2301. }
  2302. static char *virt_get_gic_version(Object *obj, Error **errp)
  2303. {
  2304. VirtMachineState *vms = VIRT_MACHINE(obj);
  2305. const char *val;
  2306. switch (vms->gic_version) {
  2307. case VIRT_GIC_VERSION_4:
  2308. val = "4";
  2309. break;
  2310. case VIRT_GIC_VERSION_3:
  2311. val = "3";
  2312. break;
  2313. default:
  2314. val = "2";
  2315. break;
  2316. }
  2317. return g_strdup(val);
  2318. }
  2319. static void virt_set_gic_version(Object *obj, const char *value, Error **errp)
  2320. {
  2321. VirtMachineState *vms = VIRT_MACHINE(obj);
  2322. if (!strcmp(value, "4")) {
  2323. vms->gic_version = VIRT_GIC_VERSION_4;
  2324. } else if (!strcmp(value, "3")) {
  2325. vms->gic_version = VIRT_GIC_VERSION_3;
  2326. } else if (!strcmp(value, "2")) {
  2327. vms->gic_version = VIRT_GIC_VERSION_2;
  2328. } else if (!strcmp(value, "host")) {
  2329. vms->gic_version = VIRT_GIC_VERSION_HOST; /* Will probe later */
  2330. } else if (!strcmp(value, "max")) {
  2331. vms->gic_version = VIRT_GIC_VERSION_MAX; /* Will probe later */
  2332. } else {
  2333. error_setg(errp, "Invalid gic-version value");
  2334. error_append_hint(errp, "Valid values are 3, 2, host, max.\n");
  2335. }
  2336. }
  2337. static char *virt_get_iommu(Object *obj, Error **errp)
  2338. {
  2339. VirtMachineState *vms = VIRT_MACHINE(obj);
  2340. switch (vms->iommu) {
  2341. case VIRT_IOMMU_NONE:
  2342. return g_strdup("none");
  2343. case VIRT_IOMMU_SMMUV3:
  2344. return g_strdup("smmuv3");
  2345. default:
  2346. g_assert_not_reached();
  2347. }
  2348. }
  2349. static void virt_set_iommu(Object *obj, const char *value, Error **errp)
  2350. {
  2351. VirtMachineState *vms = VIRT_MACHINE(obj);
  2352. if (!strcmp(value, "smmuv3")) {
  2353. vms->iommu = VIRT_IOMMU_SMMUV3;
  2354. } else if (!strcmp(value, "none")) {
  2355. vms->iommu = VIRT_IOMMU_NONE;
  2356. } else {
  2357. error_setg(errp, "Invalid iommu value");
  2358. error_append_hint(errp, "Valid values are none, smmuv3.\n");
  2359. }
  2360. }
  2361. static bool virt_get_default_bus_bypass_iommu(Object *obj, Error **errp)
  2362. {
  2363. VirtMachineState *vms = VIRT_MACHINE(obj);
  2364. return vms->default_bus_bypass_iommu;
  2365. }
  2366. static void virt_set_default_bus_bypass_iommu(Object *obj, bool value,
  2367. Error **errp)
  2368. {
  2369. VirtMachineState *vms = VIRT_MACHINE(obj);
  2370. vms->default_bus_bypass_iommu = value;
  2371. }
  2372. static CpuInstanceProperties
  2373. virt_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  2374. {
  2375. MachineClass *mc = MACHINE_GET_CLASS(ms);
  2376. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  2377. assert(cpu_index < possible_cpus->len);
  2378. return possible_cpus->cpus[cpu_index].props;
  2379. }
  2380. static int64_t virt_get_default_cpu_node_id(const MachineState *ms, int idx)
  2381. {
  2382. int64_t socket_id = ms->possible_cpus->cpus[idx].props.socket_id;
  2383. return socket_id % ms->numa_state->num_nodes;
  2384. }
  2385. static const CPUArchIdList *virt_possible_cpu_arch_ids(MachineState *ms)
  2386. {
  2387. int n;
  2388. unsigned int max_cpus = ms->smp.max_cpus;
  2389. VirtMachineState *vms = VIRT_MACHINE(ms);
  2390. MachineClass *mc = MACHINE_GET_CLASS(vms);
  2391. if (ms->possible_cpus) {
  2392. assert(ms->possible_cpus->len == max_cpus);
  2393. return ms->possible_cpus;
  2394. }
  2395. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  2396. sizeof(CPUArchId) * max_cpus);
  2397. ms->possible_cpus->len = max_cpus;
  2398. for (n = 0; n < ms->possible_cpus->len; n++) {
  2399. ms->possible_cpus->cpus[n].type = ms->cpu_type;
  2400. ms->possible_cpus->cpus[n].arch_id =
  2401. virt_cpu_mp_affinity(vms, n);
  2402. assert(!mc->smp_props.dies_supported);
  2403. ms->possible_cpus->cpus[n].props.has_socket_id = true;
  2404. ms->possible_cpus->cpus[n].props.socket_id =
  2405. n / (ms->smp.clusters * ms->smp.cores * ms->smp.threads);
  2406. ms->possible_cpus->cpus[n].props.has_cluster_id = true;
  2407. ms->possible_cpus->cpus[n].props.cluster_id =
  2408. (n / (ms->smp.cores * ms->smp.threads)) % ms->smp.clusters;
  2409. ms->possible_cpus->cpus[n].props.has_core_id = true;
  2410. ms->possible_cpus->cpus[n].props.core_id =
  2411. (n / ms->smp.threads) % ms->smp.cores;
  2412. ms->possible_cpus->cpus[n].props.has_thread_id = true;
  2413. ms->possible_cpus->cpus[n].props.thread_id =
  2414. n % ms->smp.threads;
  2415. }
  2416. return ms->possible_cpus;
  2417. }
  2418. static void virt_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  2419. Error **errp)
  2420. {
  2421. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2422. const MachineState *ms = MACHINE(hotplug_dev);
  2423. const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  2424. if (!vms->acpi_dev) {
  2425. error_setg(errp,
  2426. "memory hotplug is not enabled: missing acpi-ged device");
  2427. return;
  2428. }
  2429. if (vms->mte) {
  2430. error_setg(errp, "memory hotplug is not enabled: MTE is enabled");
  2431. return;
  2432. }
  2433. if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
  2434. error_setg(errp, "nvdimm is not enabled: add 'nvdimm=on' to '-M'");
  2435. return;
  2436. }
  2437. pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), errp);
  2438. }
  2439. static void virt_memory_plug(HotplugHandler *hotplug_dev,
  2440. DeviceState *dev, Error **errp)
  2441. {
  2442. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2443. MachineState *ms = MACHINE(hotplug_dev);
  2444. bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  2445. pc_dimm_plug(PC_DIMM(dev), MACHINE(vms));
  2446. if (is_nvdimm) {
  2447. nvdimm_plug(ms->nvdimms_state);
  2448. }
  2449. hotplug_handler_plug(HOTPLUG_HANDLER(vms->acpi_dev),
  2450. dev, &error_abort);
  2451. }
  2452. static void virt_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  2453. DeviceState *dev, Error **errp)
  2454. {
  2455. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2456. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2457. virt_memory_pre_plug(hotplug_dev, dev, errp);
  2458. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2459. virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2460. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2461. hwaddr db_start = 0, db_end = 0;
  2462. QList *reserved_regions;
  2463. char *resv_prop_str;
  2464. if (vms->iommu != VIRT_IOMMU_NONE) {
  2465. error_setg(errp, "virt machine does not support multiple IOMMUs");
  2466. return;
  2467. }
  2468. switch (vms->msi_controller) {
  2469. case VIRT_MSI_CTRL_NONE:
  2470. return;
  2471. case VIRT_MSI_CTRL_ITS:
  2472. /* GITS_TRANSLATER page */
  2473. db_start = base_memmap[VIRT_GIC_ITS].base + 0x10000;
  2474. db_end = base_memmap[VIRT_GIC_ITS].base +
  2475. base_memmap[VIRT_GIC_ITS].size - 1;
  2476. break;
  2477. case VIRT_MSI_CTRL_GICV2M:
  2478. /* MSI_SETSPI_NS page */
  2479. db_start = base_memmap[VIRT_GIC_V2M].base;
  2480. db_end = db_start + base_memmap[VIRT_GIC_V2M].size - 1;
  2481. break;
  2482. }
  2483. resv_prop_str = g_strdup_printf("0x%"PRIx64":0x%"PRIx64":%u",
  2484. db_start, db_end,
  2485. VIRTIO_IOMMU_RESV_MEM_T_MSI);
  2486. reserved_regions = qlist_new();
  2487. qlist_append_str(reserved_regions, resv_prop_str);
  2488. qdev_prop_set_array(dev, "reserved-regions", reserved_regions);
  2489. g_free(resv_prop_str);
  2490. }
  2491. }
  2492. static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
  2493. DeviceState *dev, Error **errp)
  2494. {
  2495. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2496. if (vms->platform_bus_dev) {
  2497. MachineClass *mc = MACHINE_GET_CLASS(vms);
  2498. if (device_is_dynamic_sysbus(mc, dev)) {
  2499. platform_bus_link_device(PLATFORM_BUS_DEVICE(vms->platform_bus_dev),
  2500. SYS_BUS_DEVICE(dev));
  2501. }
  2502. }
  2503. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2504. virt_memory_plug(hotplug_dev, dev, errp);
  2505. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2506. virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2507. }
  2508. if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2509. PCIDevice *pdev = PCI_DEVICE(dev);
  2510. vms->iommu = VIRT_IOMMU_VIRTIO;
  2511. vms->virtio_iommu_bdf = pci_get_bdf(pdev);
  2512. create_virtio_iommu_dt_bindings(vms);
  2513. }
  2514. }
  2515. static void virt_dimm_unplug_request(HotplugHandler *hotplug_dev,
  2516. DeviceState *dev, Error **errp)
  2517. {
  2518. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2519. if (!vms->acpi_dev) {
  2520. error_setg(errp,
  2521. "memory hotplug is not enabled: missing acpi-ged device");
  2522. return;
  2523. }
  2524. if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
  2525. error_setg(errp, "nvdimm device hot unplug is not supported yet.");
  2526. return;
  2527. }
  2528. hotplug_handler_unplug_request(HOTPLUG_HANDLER(vms->acpi_dev), dev,
  2529. errp);
  2530. }
  2531. static void virt_dimm_unplug(HotplugHandler *hotplug_dev,
  2532. DeviceState *dev, Error **errp)
  2533. {
  2534. VirtMachineState *vms = VIRT_MACHINE(hotplug_dev);
  2535. Error *local_err = NULL;
  2536. hotplug_handler_unplug(HOTPLUG_HANDLER(vms->acpi_dev), dev, &local_err);
  2537. if (local_err) {
  2538. goto out;
  2539. }
  2540. pc_dimm_unplug(PC_DIMM(dev), MACHINE(vms));
  2541. qdev_unrealize(dev);
  2542. out:
  2543. error_propagate(errp, local_err);
  2544. }
  2545. static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  2546. DeviceState *dev, Error **errp)
  2547. {
  2548. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2549. virt_dimm_unplug_request(hotplug_dev, dev, errp);
  2550. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2551. virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev),
  2552. errp);
  2553. } else {
  2554. error_setg(errp, "device unplug request for unsupported device"
  2555. " type: %s", object_get_typename(OBJECT(dev)));
  2556. }
  2557. }
  2558. static void virt_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
  2559. DeviceState *dev, Error **errp)
  2560. {
  2561. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  2562. virt_dimm_unplug(hotplug_dev, dev, errp);
  2563. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  2564. virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  2565. } else {
  2566. error_setg(errp, "virt: device unplug for unsupported device"
  2567. " type: %s", object_get_typename(OBJECT(dev)));
  2568. }
  2569. }
  2570. static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
  2571. DeviceState *dev)
  2572. {
  2573. MachineClass *mc = MACHINE_GET_CLASS(machine);
  2574. if (device_is_dynamic_sysbus(mc, dev) ||
  2575. object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
  2576. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI) ||
  2577. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  2578. return HOTPLUG_HANDLER(machine);
  2579. }
  2580. return NULL;
  2581. }
  2582. /*
  2583. * for arm64 kvm_type [7-0] encodes the requested number of bits
  2584. * in the IPA address space
  2585. */
  2586. static int virt_kvm_type(MachineState *ms, const char *type_str)
  2587. {
  2588. VirtMachineState *vms = VIRT_MACHINE(ms);
  2589. int max_vm_pa_size, requested_pa_size;
  2590. bool fixed_ipa;
  2591. max_vm_pa_size = kvm_arm_get_max_vm_ipa_size(ms, &fixed_ipa);
  2592. /* we freeze the memory map to compute the highest gpa */
  2593. virt_set_memmap(vms, max_vm_pa_size);
  2594. requested_pa_size = 64 - clz64(vms->highest_gpa);
  2595. /*
  2596. * KVM requires the IPA size to be at least 32 bits.
  2597. */
  2598. if (requested_pa_size < 32) {
  2599. requested_pa_size = 32;
  2600. }
  2601. if (requested_pa_size > max_vm_pa_size) {
  2602. error_report("-m and ,maxmem option values "
  2603. "require an IPA range (%d bits) larger than "
  2604. "the one supported by the host (%d bits)",
  2605. requested_pa_size, max_vm_pa_size);
  2606. return -1;
  2607. }
  2608. /*
  2609. * We return the requested PA log size, unless KVM only supports
  2610. * the implicit legacy 40b IPA setting, in which case the kvm_type
  2611. * must be 0.
  2612. */
  2613. return fixed_ipa ? 0 : requested_pa_size;
  2614. }
  2615. static int virt_hvf_get_physical_address_range(MachineState *ms)
  2616. {
  2617. VirtMachineState *vms = VIRT_MACHINE(ms);
  2618. int default_ipa_size = hvf_arm_get_default_ipa_bit_size();
  2619. int max_ipa_size = hvf_arm_get_max_ipa_bit_size();
  2620. /* We freeze the memory map to compute the highest gpa */
  2621. virt_set_memmap(vms, max_ipa_size);
  2622. int requested_ipa_size = 64 - clz64(vms->highest_gpa);
  2623. /*
  2624. * If we're <= the default IPA size just use the default.
  2625. * If we're above the default but below the maximum, round up to
  2626. * the maximum. hvf_arm_get_max_ipa_bit_size() conveniently only
  2627. * returns values that are valid ARM PARange values.
  2628. */
  2629. if (requested_ipa_size <= default_ipa_size) {
  2630. requested_ipa_size = default_ipa_size;
  2631. } else if (requested_ipa_size <= max_ipa_size) {
  2632. requested_ipa_size = max_ipa_size;
  2633. } else {
  2634. error_report("-m and ,maxmem option values "
  2635. "require an IPA range (%d bits) larger than "
  2636. "the one supported by the host (%d bits)",
  2637. requested_ipa_size, max_ipa_size);
  2638. return -1;
  2639. }
  2640. return requested_ipa_size;
  2641. }
  2642. static void virt_machine_class_init(ObjectClass *oc, void *data)
  2643. {
  2644. MachineClass *mc = MACHINE_CLASS(oc);
  2645. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  2646. static const char * const valid_cpu_types[] = {
  2647. #ifdef CONFIG_TCG
  2648. ARM_CPU_TYPE_NAME("cortex-a7"),
  2649. ARM_CPU_TYPE_NAME("cortex-a15"),
  2650. #ifdef TARGET_AARCH64
  2651. ARM_CPU_TYPE_NAME("cortex-a35"),
  2652. ARM_CPU_TYPE_NAME("cortex-a55"),
  2653. ARM_CPU_TYPE_NAME("cortex-a72"),
  2654. ARM_CPU_TYPE_NAME("cortex-a76"),
  2655. ARM_CPU_TYPE_NAME("cortex-a710"),
  2656. ARM_CPU_TYPE_NAME("a64fx"),
  2657. ARM_CPU_TYPE_NAME("neoverse-n1"),
  2658. ARM_CPU_TYPE_NAME("neoverse-v1"),
  2659. ARM_CPU_TYPE_NAME("neoverse-n2"),
  2660. #endif /* TARGET_AARCH64 */
  2661. #endif /* CONFIG_TCG */
  2662. #ifdef TARGET_AARCH64
  2663. ARM_CPU_TYPE_NAME("cortex-a53"),
  2664. ARM_CPU_TYPE_NAME("cortex-a57"),
  2665. #if defined(CONFIG_KVM) || defined(CONFIG_HVF)
  2666. ARM_CPU_TYPE_NAME("host"),
  2667. #endif /* CONFIG_KVM || CONFIG_HVF */
  2668. #endif /* TARGET_AARCH64 */
  2669. ARM_CPU_TYPE_NAME("max"),
  2670. NULL
  2671. };
  2672. mc->init = machvirt_init;
  2673. /* Start with max_cpus set to 512, which is the maximum supported by KVM.
  2674. * The value may be reduced later when we have more information about the
  2675. * configuration of the particular instance.
  2676. */
  2677. mc->max_cpus = 512;
  2678. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_CALXEDA_XGMAC);
  2679. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_AMD_XGBE);
  2680. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
  2681. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_VFIO_PLATFORM);
  2682. #ifdef CONFIG_TPM
  2683. machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
  2684. #endif
  2685. mc->block_default_type = IF_VIRTIO;
  2686. mc->no_cdrom = 1;
  2687. mc->pci_allow_0_address = true;
  2688. /* We know we will never create a pre-ARMv7 CPU which needs 1K pages */
  2689. mc->minimum_page_bits = 12;
  2690. mc->possible_cpu_arch_ids = virt_possible_cpu_arch_ids;
  2691. mc->cpu_index_to_instance_props = virt_cpu_index_to_props;
  2692. #ifdef CONFIG_TCG
  2693. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a15");
  2694. #else
  2695. mc->default_cpu_type = ARM_CPU_TYPE_NAME("max");
  2696. #endif
  2697. mc->valid_cpu_types = valid_cpu_types;
  2698. mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
  2699. mc->kvm_type = virt_kvm_type;
  2700. mc->hvf_get_physical_address_range = virt_hvf_get_physical_address_range;
  2701. assert(!mc->get_hotplug_handler);
  2702. mc->get_hotplug_handler = virt_machine_get_hotplug_handler;
  2703. hc->pre_plug = virt_machine_device_pre_plug_cb;
  2704. hc->plug = virt_machine_device_plug_cb;
  2705. hc->unplug_request = virt_machine_device_unplug_request_cb;
  2706. hc->unplug = virt_machine_device_unplug_cb;
  2707. mc->nvdimm_supported = true;
  2708. mc->smp_props.clusters_supported = true;
  2709. mc->auto_enable_numa_with_memhp = true;
  2710. mc->auto_enable_numa_with_memdev = true;
  2711. /* platform instead of architectural choice */
  2712. mc->cpu_cluster_has_numa_boundary = true;
  2713. mc->default_ram_id = "mach-virt.ram";
  2714. mc->default_nic = "virtio-net-pci";
  2715. object_class_property_add(oc, "acpi", "OnOffAuto",
  2716. virt_get_acpi, virt_set_acpi,
  2717. NULL, NULL);
  2718. object_class_property_set_description(oc, "acpi",
  2719. "Enable ACPI");
  2720. object_class_property_add_bool(oc, "secure", virt_get_secure,
  2721. virt_set_secure);
  2722. object_class_property_set_description(oc, "secure",
  2723. "Set on/off to enable/disable the ARM "
  2724. "Security Extensions (TrustZone)");
  2725. object_class_property_add_bool(oc, "virtualization", virt_get_virt,
  2726. virt_set_virt);
  2727. object_class_property_set_description(oc, "virtualization",
  2728. "Set on/off to enable/disable emulating a "
  2729. "guest CPU which implements the ARM "
  2730. "Virtualization Extensions");
  2731. object_class_property_add_bool(oc, "highmem", virt_get_highmem,
  2732. virt_set_highmem);
  2733. object_class_property_set_description(oc, "highmem",
  2734. "Set on/off to enable/disable using "
  2735. "physical address space above 32 bits");
  2736. object_class_property_add_bool(oc, "compact-highmem",
  2737. virt_get_compact_highmem,
  2738. virt_set_compact_highmem);
  2739. object_class_property_set_description(oc, "compact-highmem",
  2740. "Set on/off to enable/disable compact "
  2741. "layout for high memory regions");
  2742. object_class_property_add_bool(oc, "highmem-redists",
  2743. virt_get_highmem_redists,
  2744. virt_set_highmem_redists);
  2745. object_class_property_set_description(oc, "highmem-redists",
  2746. "Set on/off to enable/disable high "
  2747. "memory region for GICv3 or GICv4 "
  2748. "redistributor");
  2749. object_class_property_add_bool(oc, "highmem-ecam",
  2750. virt_get_highmem_ecam,
  2751. virt_set_highmem_ecam);
  2752. object_class_property_set_description(oc, "highmem-ecam",
  2753. "Set on/off to enable/disable high "
  2754. "memory region for PCI ECAM");
  2755. object_class_property_add_bool(oc, "highmem-mmio",
  2756. virt_get_highmem_mmio,
  2757. virt_set_highmem_mmio);
  2758. object_class_property_set_description(oc, "highmem-mmio",
  2759. "Set on/off to enable/disable high "
  2760. "memory region for PCI MMIO");
  2761. object_class_property_add_str(oc, "gic-version", virt_get_gic_version,
  2762. virt_set_gic_version);
  2763. object_class_property_set_description(oc, "gic-version",
  2764. "Set GIC version. "
  2765. "Valid values are 2, 3, 4, host and max");
  2766. object_class_property_add_str(oc, "iommu", virt_get_iommu, virt_set_iommu);
  2767. object_class_property_set_description(oc, "iommu",
  2768. "Set the IOMMU type. "
  2769. "Valid values are none and smmuv3");
  2770. object_class_property_add_bool(oc, "default-bus-bypass-iommu",
  2771. virt_get_default_bus_bypass_iommu,
  2772. virt_set_default_bus_bypass_iommu);
  2773. object_class_property_set_description(oc, "default-bus-bypass-iommu",
  2774. "Set on/off to enable/disable "
  2775. "bypass_iommu for default root bus");
  2776. object_class_property_add_bool(oc, "ras", virt_get_ras,
  2777. virt_set_ras);
  2778. object_class_property_set_description(oc, "ras",
  2779. "Set on/off to enable/disable reporting host memory errors "
  2780. "to a KVM guest using ACPI and guest external abort exceptions");
  2781. object_class_property_add_bool(oc, "mte", virt_get_mte, virt_set_mte);
  2782. object_class_property_set_description(oc, "mte",
  2783. "Set on/off to enable/disable emulating a "
  2784. "guest CPU which implements the ARM "
  2785. "Memory Tagging Extension");
  2786. object_class_property_add_bool(oc, "its", virt_get_its,
  2787. virt_set_its);
  2788. object_class_property_set_description(oc, "its",
  2789. "Set on/off to enable/disable "
  2790. "ITS instantiation");
  2791. object_class_property_add_bool(oc, "dtb-randomness",
  2792. virt_get_dtb_randomness,
  2793. virt_set_dtb_randomness);
  2794. object_class_property_set_description(oc, "dtb-randomness",
  2795. "Set off to disable passing random or "
  2796. "non-deterministic dtb nodes to guest");
  2797. object_class_property_add_bool(oc, "dtb-kaslr-seed",
  2798. virt_get_dtb_randomness,
  2799. virt_set_dtb_randomness);
  2800. object_class_property_set_description(oc, "dtb-kaslr-seed",
  2801. "Deprecated synonym of dtb-randomness");
  2802. object_class_property_add_str(oc, "x-oem-id",
  2803. virt_get_oem_id,
  2804. virt_set_oem_id);
  2805. object_class_property_set_description(oc, "x-oem-id",
  2806. "Override the default value of field OEMID "
  2807. "in ACPI table header."
  2808. "The string may be up to 6 bytes in size");
  2809. object_class_property_add_str(oc, "x-oem-table-id",
  2810. virt_get_oem_table_id,
  2811. virt_set_oem_table_id);
  2812. object_class_property_set_description(oc, "x-oem-table-id",
  2813. "Override the default value of field OEM Table ID "
  2814. "in ACPI table header."
  2815. "The string may be up to 8 bytes in size");
  2816. }
  2817. static void virt_instance_init(Object *obj)
  2818. {
  2819. VirtMachineState *vms = VIRT_MACHINE(obj);
  2820. VirtMachineClass *vmc = VIRT_MACHINE_GET_CLASS(vms);
  2821. /* EL3 is disabled by default on virt: this makes us consistent
  2822. * between KVM and TCG for this board, and it also allows us to
  2823. * boot UEFI blobs which assume no TrustZone support.
  2824. */
  2825. vms->secure = false;
  2826. /* EL2 is also disabled by default, for similar reasons */
  2827. vms->virt = false;
  2828. /* High memory is enabled by default */
  2829. vms->highmem = true;
  2830. vms->highmem_compact = !vmc->no_highmem_compact;
  2831. vms->gic_version = VIRT_GIC_VERSION_NOSEL;
  2832. vms->highmem_ecam = !vmc->no_highmem_ecam;
  2833. vms->highmem_mmio = true;
  2834. vms->highmem_redists = true;
  2835. if (vmc->no_its) {
  2836. vms->its = false;
  2837. } else {
  2838. /* Default allows ITS instantiation */
  2839. vms->its = true;
  2840. if (vmc->no_tcg_its) {
  2841. vms->tcg_its = false;
  2842. } else {
  2843. vms->tcg_its = true;
  2844. }
  2845. }
  2846. /* Default disallows iommu instantiation */
  2847. vms->iommu = VIRT_IOMMU_NONE;
  2848. /* The default root bus is attached to iommu by default */
  2849. vms->default_bus_bypass_iommu = false;
  2850. /* Default disallows RAS instantiation */
  2851. vms->ras = false;
  2852. /* MTE is disabled by default. */
  2853. vms->mte = false;
  2854. /* Supply kaslr-seed and rng-seed by default */
  2855. vms->dtb_randomness = true;
  2856. vms->irqmap = a15irqmap;
  2857. virt_flash_create(vms);
  2858. vms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6);
  2859. vms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8);
  2860. }
  2861. static const TypeInfo virt_machine_info = {
  2862. .name = TYPE_VIRT_MACHINE,
  2863. .parent = TYPE_MACHINE,
  2864. .abstract = true,
  2865. .instance_size = sizeof(VirtMachineState),
  2866. .class_size = sizeof(VirtMachineClass),
  2867. .class_init = virt_machine_class_init,
  2868. .instance_init = virt_instance_init,
  2869. .interfaces = (InterfaceInfo[]) {
  2870. { TYPE_HOTPLUG_HANDLER },
  2871. { }
  2872. },
  2873. };
  2874. static void machvirt_machine_init(void)
  2875. {
  2876. type_register_static(&virt_machine_info);
  2877. }
  2878. type_init(machvirt_machine_init);
  2879. static void virt_machine_10_0_options(MachineClass *mc)
  2880. {
  2881. }
  2882. DEFINE_VIRT_MACHINE_AS_LATEST(10, 0)
  2883. static void virt_machine_9_2_options(MachineClass *mc)
  2884. {
  2885. virt_machine_10_0_options(mc);
  2886. compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len);
  2887. }
  2888. DEFINE_VIRT_MACHINE(9, 2)
  2889. static void virt_machine_9_1_options(MachineClass *mc)
  2890. {
  2891. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2892. virt_machine_9_2_options(mc);
  2893. compat_props_add(mc->compat_props, hw_compat_9_1, hw_compat_9_1_len);
  2894. /* 9.1 and earlier have only a stage-1 SMMU, not a nested s1+2 one */
  2895. vmc->no_nested_smmu = true;
  2896. }
  2897. DEFINE_VIRT_MACHINE(9, 1)
  2898. static void virt_machine_9_0_options(MachineClass *mc)
  2899. {
  2900. virt_machine_9_1_options(mc);
  2901. mc->smbios_memory_device_size = 16 * GiB;
  2902. compat_props_add(mc->compat_props, hw_compat_9_0, hw_compat_9_0_len);
  2903. }
  2904. DEFINE_VIRT_MACHINE(9, 0)
  2905. static void virt_machine_8_2_options(MachineClass *mc)
  2906. {
  2907. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2908. virt_machine_9_0_options(mc);
  2909. compat_props_add(mc->compat_props, hw_compat_8_2, hw_compat_8_2_len);
  2910. /*
  2911. * Don't expose NS_EL2_VIRT timer IRQ in DTB on ACPI on 8.2 and
  2912. * earlier machines. (Exposing it tickles a bug in older EDK2
  2913. * guest BIOS binaries.)
  2914. */
  2915. vmc->no_ns_el2_virt_timer_irq = true;
  2916. }
  2917. DEFINE_VIRT_MACHINE(8, 2)
  2918. static void virt_machine_8_1_options(MachineClass *mc)
  2919. {
  2920. virt_machine_8_2_options(mc);
  2921. compat_props_add(mc->compat_props, hw_compat_8_1, hw_compat_8_1_len);
  2922. }
  2923. DEFINE_VIRT_MACHINE(8, 1)
  2924. static void virt_machine_8_0_options(MachineClass *mc)
  2925. {
  2926. virt_machine_8_1_options(mc);
  2927. compat_props_add(mc->compat_props, hw_compat_8_0, hw_compat_8_0_len);
  2928. }
  2929. DEFINE_VIRT_MACHINE(8, 0)
  2930. static void virt_machine_7_2_options(MachineClass *mc)
  2931. {
  2932. virt_machine_8_0_options(mc);
  2933. compat_props_add(mc->compat_props, hw_compat_7_2, hw_compat_7_2_len);
  2934. }
  2935. DEFINE_VIRT_MACHINE(7, 2)
  2936. static void virt_machine_7_1_options(MachineClass *mc)
  2937. {
  2938. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2939. virt_machine_7_2_options(mc);
  2940. compat_props_add(mc->compat_props, hw_compat_7_1, hw_compat_7_1_len);
  2941. /* Compact layout for high memory regions was introduced with 7.2 */
  2942. vmc->no_highmem_compact = true;
  2943. }
  2944. DEFINE_VIRT_MACHINE(7, 1)
  2945. static void virt_machine_7_0_options(MachineClass *mc)
  2946. {
  2947. virt_machine_7_1_options(mc);
  2948. compat_props_add(mc->compat_props, hw_compat_7_0, hw_compat_7_0_len);
  2949. }
  2950. DEFINE_VIRT_MACHINE(7, 0)
  2951. static void virt_machine_6_2_options(MachineClass *mc)
  2952. {
  2953. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2954. virt_machine_7_0_options(mc);
  2955. compat_props_add(mc->compat_props, hw_compat_6_2, hw_compat_6_2_len);
  2956. vmc->no_tcg_lpa2 = true;
  2957. }
  2958. DEFINE_VIRT_MACHINE(6, 2)
  2959. static void virt_machine_6_1_options(MachineClass *mc)
  2960. {
  2961. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2962. virt_machine_6_2_options(mc);
  2963. compat_props_add(mc->compat_props, hw_compat_6_1, hw_compat_6_1_len);
  2964. mc->smp_props.prefer_sockets = true;
  2965. vmc->no_cpu_topology = true;
  2966. /* qemu ITS was introduced with 6.2 */
  2967. vmc->no_tcg_its = true;
  2968. }
  2969. DEFINE_VIRT_MACHINE(6, 1)
  2970. static void virt_machine_6_0_options(MachineClass *mc)
  2971. {
  2972. virt_machine_6_1_options(mc);
  2973. compat_props_add(mc->compat_props, hw_compat_6_0, hw_compat_6_0_len);
  2974. }
  2975. DEFINE_VIRT_MACHINE(6, 0)
  2976. static void virt_machine_5_2_options(MachineClass *mc)
  2977. {
  2978. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2979. virt_machine_6_0_options(mc);
  2980. compat_props_add(mc->compat_props, hw_compat_5_2, hw_compat_5_2_len);
  2981. vmc->no_secure_gpio = true;
  2982. }
  2983. DEFINE_VIRT_MACHINE(5, 2)
  2984. static void virt_machine_5_1_options(MachineClass *mc)
  2985. {
  2986. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2987. virt_machine_5_2_options(mc);
  2988. compat_props_add(mc->compat_props, hw_compat_5_1, hw_compat_5_1_len);
  2989. vmc->no_kvm_steal_time = true;
  2990. }
  2991. DEFINE_VIRT_MACHINE(5, 1)
  2992. static void virt_machine_5_0_options(MachineClass *mc)
  2993. {
  2994. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  2995. virt_machine_5_1_options(mc);
  2996. compat_props_add(mc->compat_props, hw_compat_5_0, hw_compat_5_0_len);
  2997. mc->numa_mem_supported = true;
  2998. vmc->acpi_expose_flash = true;
  2999. mc->auto_enable_numa_with_memdev = false;
  3000. }
  3001. DEFINE_VIRT_MACHINE(5, 0)
  3002. static void virt_machine_4_2_options(MachineClass *mc)
  3003. {
  3004. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3005. virt_machine_5_0_options(mc);
  3006. compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
  3007. vmc->kvm_no_adjvtime = true;
  3008. }
  3009. DEFINE_VIRT_MACHINE(4, 2)
  3010. static void virt_machine_4_1_options(MachineClass *mc)
  3011. {
  3012. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3013. virt_machine_4_2_options(mc);
  3014. compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
  3015. vmc->no_ged = true;
  3016. mc->auto_enable_numa_with_memhp = false;
  3017. }
  3018. DEFINE_VIRT_MACHINE(4, 1)
  3019. static void virt_machine_4_0_options(MachineClass *mc)
  3020. {
  3021. virt_machine_4_1_options(mc);
  3022. compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
  3023. }
  3024. DEFINE_VIRT_MACHINE(4, 0)
  3025. static void virt_machine_3_1_options(MachineClass *mc)
  3026. {
  3027. virt_machine_4_0_options(mc);
  3028. compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
  3029. }
  3030. DEFINE_VIRT_MACHINE(3, 1)
  3031. static void virt_machine_3_0_options(MachineClass *mc)
  3032. {
  3033. virt_machine_3_1_options(mc);
  3034. compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
  3035. }
  3036. DEFINE_VIRT_MACHINE(3, 0)
  3037. static void virt_machine_2_12_options(MachineClass *mc)
  3038. {
  3039. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3040. virt_machine_3_0_options(mc);
  3041. compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
  3042. vmc->no_highmem_ecam = true;
  3043. mc->max_cpus = 255;
  3044. }
  3045. DEFINE_VIRT_MACHINE(2, 12)
  3046. static void virt_machine_2_11_options(MachineClass *mc)
  3047. {
  3048. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3049. virt_machine_2_12_options(mc);
  3050. compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
  3051. vmc->smbios_old_sys_ver = true;
  3052. }
  3053. DEFINE_VIRT_MACHINE(2, 11)
  3054. static void virt_machine_2_10_options(MachineClass *mc)
  3055. {
  3056. virt_machine_2_11_options(mc);
  3057. compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
  3058. /* before 2.11 we never faulted accesses to bad addresses */
  3059. mc->ignore_memory_transaction_failures = true;
  3060. }
  3061. DEFINE_VIRT_MACHINE(2, 10)
  3062. static void virt_machine_2_9_options(MachineClass *mc)
  3063. {
  3064. virt_machine_2_10_options(mc);
  3065. compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
  3066. }
  3067. DEFINE_VIRT_MACHINE(2, 9)
  3068. static void virt_machine_2_8_options(MachineClass *mc)
  3069. {
  3070. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3071. virt_machine_2_9_options(mc);
  3072. compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
  3073. /* For 2.8 and earlier we falsely claimed in the DT that
  3074. * our timers were edge-triggered, not level-triggered.
  3075. */
  3076. vmc->claim_edge_triggered_timers = true;
  3077. }
  3078. DEFINE_VIRT_MACHINE(2, 8)
  3079. static void virt_machine_2_7_options(MachineClass *mc)
  3080. {
  3081. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3082. virt_machine_2_8_options(mc);
  3083. compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
  3084. /* ITS was introduced with 2.8 */
  3085. vmc->no_its = true;
  3086. /* Stick with 1K pages for migration compatibility */
  3087. mc->minimum_page_bits = 0;
  3088. }
  3089. DEFINE_VIRT_MACHINE(2, 7)
  3090. static void virt_machine_2_6_options(MachineClass *mc)
  3091. {
  3092. VirtMachineClass *vmc = VIRT_MACHINE_CLASS(OBJECT_CLASS(mc));
  3093. virt_machine_2_7_options(mc);
  3094. compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
  3095. vmc->disallow_affinity_adjustment = true;
  3096. /* Disable PMU for 2.6 as PMU support was first introduced in 2.7 */
  3097. vmc->no_pmu = true;
  3098. }
  3099. DEFINE_VIRT_MACHINE(2, 6)