virt.c 124 KB

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