pc.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. /*
  2. * QEMU PC System Emulator
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "hw/i386/pc.h"
  27. #include "hw/char/serial-isa.h"
  28. #include "hw/char/parallel.h"
  29. #include "hw/hyperv/hv-balloon.h"
  30. #include "hw/i386/fw_cfg.h"
  31. #include "hw/i386/vmport.h"
  32. #include "system/cpus.h"
  33. #include "hw/ide/ide-bus.h"
  34. #include "hw/timer/hpet.h"
  35. #include "hw/loader.h"
  36. #include "hw/rtc/mc146818rtc.h"
  37. #include "hw/intc/i8259.h"
  38. #include "hw/timer/i8254.h"
  39. #include "hw/input/i8042.h"
  40. #include "hw/audio/pcspk.h"
  41. #include "system/system.h"
  42. #include "system/xen.h"
  43. #include "system/reset.h"
  44. #include "kvm/kvm_i386.h"
  45. #include "hw/xen/xen.h"
  46. #include "qobject/qlist.h"
  47. #include "qemu/error-report.h"
  48. #include "hw/acpi/cpu_hotplug.h"
  49. #include "acpi-build.h"
  50. #include "hw/mem/nvdimm.h"
  51. #include "hw/cxl/cxl_host.h"
  52. #include "hw/usb.h"
  53. #include "hw/i386/intel_iommu.h"
  54. #include "hw/net/ne2000-isa.h"
  55. #include "hw/virtio/virtio-iommu.h"
  56. #include "hw/virtio/virtio-md-pci.h"
  57. #include "hw/i386/kvm/xen_overlay.h"
  58. #include "hw/i386/kvm/xen_evtchn.h"
  59. #include "hw/i386/kvm/xen_gnttab.h"
  60. #include "hw/i386/kvm/xen_xenstore.h"
  61. #include "hw/mem/memory-device.h"
  62. #include "e820_memory_layout.h"
  63. #include "trace.h"
  64. #include "sev.h"
  65. #include CONFIG_DEVICES
  66. #ifdef CONFIG_XEN_EMU
  67. #include "hw/xen/xen-legacy-backend.h"
  68. #include "hw/xen/xen-bus.h"
  69. #endif
  70. /*
  71. * Helper for setting model-id for CPU models that changed model-id
  72. * depending on QEMU versions up to QEMU 2.4.
  73. */
  74. #define PC_CPU_MODEL_IDS(v) \
  75. { "qemu32-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },\
  76. { "qemu64-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },\
  77. { "athlon-" TYPE_X86_CPU, "model-id", "QEMU Virtual CPU version " v, },
  78. GlobalProperty pc_compat_9_2[] = {};
  79. const size_t pc_compat_9_2_len = G_N_ELEMENTS(pc_compat_9_2);
  80. GlobalProperty pc_compat_9_1[] = {
  81. { "ICH9-LPC", "x-smi-swsmi-timer", "off" },
  82. { "ICH9-LPC", "x-smi-periodic-timer", "off" },
  83. { TYPE_INTEL_IOMMU_DEVICE, "stale-tm", "on" },
  84. { TYPE_INTEL_IOMMU_DEVICE, "aw-bits", "39" },
  85. };
  86. const size_t pc_compat_9_1_len = G_N_ELEMENTS(pc_compat_9_1);
  87. GlobalProperty pc_compat_9_0[] = {
  88. { TYPE_X86_CPU, "x-amd-topoext-features-only", "false" },
  89. { TYPE_X86_CPU, "x-l1-cache-per-thread", "false" },
  90. { TYPE_X86_CPU, "guest-phys-bits", "0" },
  91. { "sev-guest", "legacy-vm-type", "on" },
  92. { TYPE_X86_CPU, "legacy-multi-node", "on" },
  93. };
  94. const size_t pc_compat_9_0_len = G_N_ELEMENTS(pc_compat_9_0);
  95. GlobalProperty pc_compat_8_2[] = {};
  96. const size_t pc_compat_8_2_len = G_N_ELEMENTS(pc_compat_8_2);
  97. GlobalProperty pc_compat_8_1[] = {};
  98. const size_t pc_compat_8_1_len = G_N_ELEMENTS(pc_compat_8_1);
  99. GlobalProperty pc_compat_8_0[] = {
  100. { "virtio-mem", "unplugged-inaccessible", "auto" },
  101. };
  102. const size_t pc_compat_8_0_len = G_N_ELEMENTS(pc_compat_8_0);
  103. GlobalProperty pc_compat_7_2[] = {
  104. { "ICH9-LPC", "noreboot", "true" },
  105. };
  106. const size_t pc_compat_7_2_len = G_N_ELEMENTS(pc_compat_7_2);
  107. GlobalProperty pc_compat_7_1[] = {};
  108. const size_t pc_compat_7_1_len = G_N_ELEMENTS(pc_compat_7_1);
  109. GlobalProperty pc_compat_7_0[] = {};
  110. const size_t pc_compat_7_0_len = G_N_ELEMENTS(pc_compat_7_0);
  111. GlobalProperty pc_compat_6_2[] = {
  112. { "virtio-mem", "unplugged-inaccessible", "off" },
  113. };
  114. const size_t pc_compat_6_2_len = G_N_ELEMENTS(pc_compat_6_2);
  115. GlobalProperty pc_compat_6_1[] = {
  116. { TYPE_X86_CPU, "hv-version-id-build", "0x1bbc" },
  117. { TYPE_X86_CPU, "hv-version-id-major", "0x0006" },
  118. { TYPE_X86_CPU, "hv-version-id-minor", "0x0001" },
  119. { "ICH9-LPC", "x-keep-pci-slot-hpc", "false" },
  120. };
  121. const size_t pc_compat_6_1_len = G_N_ELEMENTS(pc_compat_6_1);
  122. GlobalProperty pc_compat_6_0[] = {
  123. { "qemu64" "-" TYPE_X86_CPU, "family", "6" },
  124. { "qemu64" "-" TYPE_X86_CPU, "model", "6" },
  125. { "qemu64" "-" TYPE_X86_CPU, "stepping", "3" },
  126. { TYPE_X86_CPU, "x-vendor-cpuid-only", "off" },
  127. { "ICH9-LPC", ACPI_PM_PROP_ACPI_PCIHP_BRIDGE, "off" },
  128. { "ICH9-LPC", "x-keep-pci-slot-hpc", "true" },
  129. };
  130. const size_t pc_compat_6_0_len = G_N_ELEMENTS(pc_compat_6_0);
  131. GlobalProperty pc_compat_5_2[] = {
  132. { "ICH9-LPC", "x-smi-cpu-hotunplug", "off" },
  133. };
  134. const size_t pc_compat_5_2_len = G_N_ELEMENTS(pc_compat_5_2);
  135. GlobalProperty pc_compat_5_1[] = {
  136. { "ICH9-LPC", "x-smi-cpu-hotplug", "off" },
  137. { TYPE_X86_CPU, "kvm-msi-ext-dest-id", "off" },
  138. };
  139. const size_t pc_compat_5_1_len = G_N_ELEMENTS(pc_compat_5_1);
  140. GlobalProperty pc_compat_5_0[] = {
  141. };
  142. const size_t pc_compat_5_0_len = G_N_ELEMENTS(pc_compat_5_0);
  143. GlobalProperty pc_compat_4_2[] = {
  144. { "mch", "smbase-smram", "off" },
  145. };
  146. const size_t pc_compat_4_2_len = G_N_ELEMENTS(pc_compat_4_2);
  147. GlobalProperty pc_compat_4_1[] = {};
  148. const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
  149. GlobalProperty pc_compat_4_0[] = {};
  150. const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
  151. GlobalProperty pc_compat_3_1[] = {
  152. { "intel-iommu", "dma-drain", "off" },
  153. { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
  154. { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
  155. { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
  156. { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
  157. { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
  158. { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
  159. { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
  160. { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
  161. { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
  162. { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
  163. { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
  164. { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
  165. { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
  166. { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  167. { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
  168. { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  169. { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
  170. { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  171. { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
  172. { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
  173. };
  174. const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
  175. GlobalProperty pc_compat_3_0[] = {
  176. { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
  177. { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
  178. { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
  179. };
  180. const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
  181. GlobalProperty pc_compat_2_12[] = {
  182. { TYPE_X86_CPU, "legacy-cache", "on" },
  183. { TYPE_X86_CPU, "topoext", "off" },
  184. { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
  185. { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
  186. };
  187. const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
  188. GlobalProperty pc_compat_2_11[] = {
  189. { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
  190. { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
  191. };
  192. const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
  193. GlobalProperty pc_compat_2_10[] = {
  194. { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
  195. { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
  196. { "q35-pcihost", "x-pci-hole64-fix", "off" },
  197. };
  198. const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
  199. GlobalProperty pc_compat_2_9[] = {
  200. { "mch", "extended-tseg-mbytes", "0" },
  201. };
  202. const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
  203. GlobalProperty pc_compat_2_8[] = {
  204. { TYPE_X86_CPU, "tcg-cpuid", "off" },
  205. { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
  206. { "ICH9-LPC", "x-smi-broadcast", "off" },
  207. { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
  208. { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
  209. };
  210. const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
  211. GlobalProperty pc_compat_2_7[] = {
  212. { TYPE_X86_CPU, "l3-cache", "off" },
  213. { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
  214. { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
  215. { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
  216. { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
  217. { "isa-pcspk", "migrate", "off" },
  218. };
  219. const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
  220. GlobalProperty pc_compat_2_6[] = {
  221. { TYPE_X86_CPU, "cpuid-0xb", "off" },
  222. { "vmxnet3", "romfile", "" },
  223. { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
  224. { "apic-common", "legacy-instance-id", "on", }
  225. };
  226. const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
  227. GlobalProperty pc_compat_2_5[] = {};
  228. const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
  229. GlobalProperty pc_compat_2_4[] = {
  230. PC_CPU_MODEL_IDS("2.4.0")
  231. { "Haswell-" TYPE_X86_CPU, "abm", "off" },
  232. { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
  233. { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
  234. { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
  235. { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
  236. { TYPE_X86_CPU, "check", "off" },
  237. { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
  238. { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
  239. { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
  240. { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
  241. { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
  242. { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
  243. { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
  244. { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
  245. };
  246. const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
  247. /*
  248. * @PC_FW_DATA:
  249. * Size of the chunk of memory at the top of RAM for the BIOS ACPI tables
  250. * and other BIOS datastructures.
  251. *
  252. * BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K
  253. * reported to be used at the moment, 32K should be enough for a while.
  254. */
  255. #define PC_FW_DATA (0x20000 + 0x8000)
  256. GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
  257. {
  258. GSIState *s;
  259. s = g_new0(GSIState, 1);
  260. if (kvm_ioapic_in_kernel()) {
  261. kvm_pc_setup_irq_routing(pci_enabled);
  262. }
  263. *irqs = qemu_allocate_irqs(gsi_handler, s, IOAPIC_NUM_PINS);
  264. return s;
  265. }
  266. static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
  267. unsigned size)
  268. {
  269. }
  270. static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
  271. {
  272. return 0xffffffffffffffffULL;
  273. }
  274. /* MS-DOS compatibility mode FPU exception support */
  275. static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
  276. unsigned size)
  277. {
  278. if (tcg_enabled()) {
  279. cpu_set_ignne();
  280. }
  281. }
  282. static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
  283. {
  284. return 0xffffffffffffffffULL;
  285. }
  286. /* PC cmos mappings */
  287. #define REG_EQUIPMENT_BYTE 0x14
  288. static void cmos_init_hd(MC146818RtcState *s, int type_ofs, int info_ofs,
  289. int16_t cylinders, int8_t heads, int8_t sectors)
  290. {
  291. mc146818rtc_set_cmos_data(s, type_ofs, 47);
  292. mc146818rtc_set_cmos_data(s, info_ofs, cylinders);
  293. mc146818rtc_set_cmos_data(s, info_ofs + 1, cylinders >> 8);
  294. mc146818rtc_set_cmos_data(s, info_ofs + 2, heads);
  295. mc146818rtc_set_cmos_data(s, info_ofs + 3, 0xff);
  296. mc146818rtc_set_cmos_data(s, info_ofs + 4, 0xff);
  297. mc146818rtc_set_cmos_data(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
  298. mc146818rtc_set_cmos_data(s, info_ofs + 6, cylinders);
  299. mc146818rtc_set_cmos_data(s, info_ofs + 7, cylinders >> 8);
  300. mc146818rtc_set_cmos_data(s, info_ofs + 8, sectors);
  301. }
  302. /* convert boot_device letter to something recognizable by the bios */
  303. static int boot_device2nibble(char boot_device)
  304. {
  305. switch(boot_device) {
  306. case 'a':
  307. case 'b':
  308. return 0x01; /* floppy boot */
  309. case 'c':
  310. return 0x02; /* hard drive boot */
  311. case 'd':
  312. return 0x03; /* CD-ROM boot */
  313. case 'n':
  314. return 0x04; /* Network boot */
  315. }
  316. return 0;
  317. }
  318. static void set_boot_dev(PCMachineState *pcms, MC146818RtcState *s,
  319. const char *boot_device, Error **errp)
  320. {
  321. #define PC_MAX_BOOT_DEVICES 3
  322. int nbds, bds[3] = { 0, };
  323. int i;
  324. nbds = strlen(boot_device);
  325. if (nbds > PC_MAX_BOOT_DEVICES) {
  326. error_setg(errp, "Too many boot devices for PC");
  327. return;
  328. }
  329. for (i = 0; i < nbds; i++) {
  330. bds[i] = boot_device2nibble(boot_device[i]);
  331. if (bds[i] == 0) {
  332. error_setg(errp, "Invalid boot device for PC: '%c'",
  333. boot_device[i]);
  334. return;
  335. }
  336. }
  337. mc146818rtc_set_cmos_data(s, 0x3d, (bds[1] << 4) | bds[0]);
  338. mc146818rtc_set_cmos_data(s, 0x38, (bds[2] << 4) | !pcms->fd_bootchk);
  339. }
  340. static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
  341. {
  342. PCMachineState *pcms = opaque;
  343. X86MachineState *x86ms = X86_MACHINE(pcms);
  344. set_boot_dev(pcms, MC146818_RTC(x86ms->rtc), boot_device, errp);
  345. }
  346. static void pc_cmos_init_floppy(MC146818RtcState *rtc_state, ISADevice *floppy)
  347. {
  348. int val, nb;
  349. FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
  350. FLOPPY_DRIVE_TYPE_NONE };
  351. #ifdef CONFIG_FDC_ISA
  352. /* floppy type */
  353. if (floppy) {
  354. for (int i = 0; i < 2; i++) {
  355. fd_type[i] = isa_fdc_get_drive_type(floppy, i);
  356. }
  357. }
  358. #endif
  359. val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
  360. cmos_get_fd_drive_type(fd_type[1]);
  361. mc146818rtc_set_cmos_data(rtc_state, 0x10, val);
  362. val = mc146818rtc_get_cmos_data(rtc_state, REG_EQUIPMENT_BYTE);
  363. nb = 0;
  364. if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
  365. nb++;
  366. }
  367. if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
  368. nb++;
  369. }
  370. switch (nb) {
  371. case 0:
  372. break;
  373. case 1:
  374. val |= 0x01; /* 1 drive, ready for boot */
  375. break;
  376. case 2:
  377. val |= 0x41; /* 2 drives, ready for boot */
  378. break;
  379. }
  380. mc146818rtc_set_cmos_data(rtc_state, REG_EQUIPMENT_BYTE, val);
  381. }
  382. typedef struct check_fdc_state {
  383. ISADevice *floppy;
  384. bool multiple;
  385. } CheckFdcState;
  386. static int check_fdc(Object *obj, void *opaque)
  387. {
  388. CheckFdcState *state = opaque;
  389. Object *fdc;
  390. uint32_t iobase;
  391. Error *local_err = NULL;
  392. fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
  393. if (!fdc) {
  394. return 0;
  395. }
  396. iobase = object_property_get_uint(obj, "iobase", &local_err);
  397. if (local_err || iobase != 0x3f0) {
  398. error_free(local_err);
  399. return 0;
  400. }
  401. if (state->floppy) {
  402. state->multiple = true;
  403. } else {
  404. state->floppy = ISA_DEVICE(obj);
  405. }
  406. return 0;
  407. }
  408. static const char * const fdc_container_path[] = {
  409. "unattached", "peripheral", "peripheral-anon"
  410. };
  411. /*
  412. * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
  413. * and ACPI objects.
  414. */
  415. static ISADevice *pc_find_fdc0(void)
  416. {
  417. int i;
  418. Object *container;
  419. CheckFdcState state = { 0 };
  420. for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
  421. container = machine_get_container(fdc_container_path[i]);
  422. object_child_foreach(container, check_fdc, &state);
  423. }
  424. if (state.multiple) {
  425. warn_report("multiple floppy disk controllers with "
  426. "iobase=0x3f0 have been found");
  427. error_printf("the one being picked for CMOS setup might not reflect "
  428. "your intent");
  429. }
  430. return state.floppy;
  431. }
  432. static void pc_cmos_init_late(PCMachineState *pcms)
  433. {
  434. X86MachineState *x86ms = X86_MACHINE(pcms);
  435. MC146818RtcState *s = MC146818_RTC(x86ms->rtc);
  436. int16_t cylinders;
  437. int8_t heads, sectors;
  438. int val;
  439. int i, trans;
  440. val = 0;
  441. if (pcms->idebus[0] &&
  442. ide_get_geometry(pcms->idebus[0], 0,
  443. &cylinders, &heads, &sectors) >= 0) {
  444. cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
  445. val |= 0xf0;
  446. }
  447. if (pcms->idebus[0] &&
  448. ide_get_geometry(pcms->idebus[0], 1,
  449. &cylinders, &heads, &sectors) >= 0) {
  450. cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
  451. val |= 0x0f;
  452. }
  453. mc146818rtc_set_cmos_data(s, 0x12, val);
  454. val = 0;
  455. for (i = 0; i < 4; i++) {
  456. /* NOTE: ide_get_geometry() returns the physical
  457. geometry. It is always such that: 1 <= sects <= 63, 1
  458. <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
  459. geometry can be different if a translation is done. */
  460. BusState *idebus = pcms->idebus[i / 2];
  461. if (idebus &&
  462. ide_get_geometry(idebus, i % 2,
  463. &cylinders, &heads, &sectors) >= 0) {
  464. trans = ide_get_bios_chs_trans(idebus, i % 2) - 1;
  465. assert((trans & ~3) == 0);
  466. val |= trans << (i * 2);
  467. }
  468. }
  469. mc146818rtc_set_cmos_data(s, 0x39, val);
  470. pc_cmos_init_floppy(s, pc_find_fdc0());
  471. /* various important CMOS locations needed by PC/Bochs bios */
  472. /* memory size */
  473. /* base memory (first MiB) */
  474. val = MIN(x86ms->below_4g_mem_size / KiB, 640);
  475. mc146818rtc_set_cmos_data(s, 0x15, val);
  476. mc146818rtc_set_cmos_data(s, 0x16, val >> 8);
  477. /* extended memory (next 64MiB) */
  478. if (x86ms->below_4g_mem_size > 1 * MiB) {
  479. val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
  480. } else {
  481. val = 0;
  482. }
  483. if (val > 65535)
  484. val = 65535;
  485. mc146818rtc_set_cmos_data(s, 0x17, val);
  486. mc146818rtc_set_cmos_data(s, 0x18, val >> 8);
  487. mc146818rtc_set_cmos_data(s, 0x30, val);
  488. mc146818rtc_set_cmos_data(s, 0x31, val >> 8);
  489. /* memory between 16MiB and 4GiB */
  490. if (x86ms->below_4g_mem_size > 16 * MiB) {
  491. val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
  492. } else {
  493. val = 0;
  494. }
  495. if (val > 65535)
  496. val = 65535;
  497. mc146818rtc_set_cmos_data(s, 0x34, val);
  498. mc146818rtc_set_cmos_data(s, 0x35, val >> 8);
  499. /* memory above 4GiB */
  500. val = x86ms->above_4g_mem_size / 65536;
  501. mc146818rtc_set_cmos_data(s, 0x5b, val);
  502. mc146818rtc_set_cmos_data(s, 0x5c, val >> 8);
  503. mc146818rtc_set_cmos_data(s, 0x5d, val >> 16);
  504. val = 0;
  505. val |= 0x02; /* FPU is there */
  506. val |= 0x04; /* PS/2 mouse installed */
  507. mc146818rtc_set_cmos_data(s, REG_EQUIPMENT_BYTE, val);
  508. }
  509. static void handle_a20_line_change(void *opaque, int irq, int level)
  510. {
  511. X86CPU *cpu = opaque;
  512. /* XXX: send to all CPUs ? */
  513. /* XXX: add logic to handle multiple A20 line sources */
  514. x86_cpu_set_a20(cpu, level);
  515. }
  516. #define NE2000_NB_MAX 6
  517. static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
  518. 0x280, 0x380 };
  519. static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
  520. static gboolean pc_init_ne2k_isa(ISABus *bus, NICInfo *nd, Error **errp)
  521. {
  522. static int nb_ne2k = 0;
  523. if (nb_ne2k == NE2000_NB_MAX) {
  524. error_setg(errp,
  525. "maximum number of ISA NE2000 devices exceeded");
  526. return false;
  527. }
  528. isa_ne2000_init(bus, ne2000_io[nb_ne2k],
  529. ne2000_irq[nb_ne2k], nd);
  530. nb_ne2k++;
  531. return true;
  532. }
  533. void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
  534. {
  535. X86CPU *cpu = opaque;
  536. if (level) {
  537. cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
  538. }
  539. }
  540. static
  541. void pc_machine_done(Notifier *notifier, void *data)
  542. {
  543. PCMachineState *pcms = container_of(notifier,
  544. PCMachineState, machine_done);
  545. X86MachineState *x86ms = X86_MACHINE(pcms);
  546. cxl_hook_up_pxb_registers(pcms->pcibus, &pcms->cxl_devices_state,
  547. &error_fatal);
  548. if (pcms->cxl_devices_state.is_enabled) {
  549. cxl_fmws_link_targets(&pcms->cxl_devices_state, &error_fatal);
  550. }
  551. /* set the number of CPUs */
  552. x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  553. pci_bus_add_fw_cfg_extra_pci_roots(x86ms->fw_cfg, pcms->pcibus,
  554. &error_abort);
  555. acpi_setup();
  556. if (x86ms->fw_cfg) {
  557. fw_cfg_build_smbios(pcms, x86ms->fw_cfg, pcms->smbios_entry_point_type);
  558. fw_cfg_add_e820(x86ms->fw_cfg);
  559. fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
  560. /* update FW_CFG_NB_CPUS to account for -device added CPUs */
  561. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  562. }
  563. pc_cmos_init_late(pcms);
  564. }
  565. /* setup pci memory address space mapping into system address space */
  566. void pc_pci_as_mapping_init(MemoryRegion *system_memory,
  567. MemoryRegion *pci_address_space)
  568. {
  569. /* Set to lower priority than RAM */
  570. memory_region_add_subregion_overlap(system_memory, 0x0,
  571. pci_address_space, -1);
  572. }
  573. void xen_load_linux(PCMachineState *pcms)
  574. {
  575. int i;
  576. FWCfgState *fw_cfg;
  577. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  578. X86MachineState *x86ms = X86_MACHINE(pcms);
  579. assert(MACHINE(pcms)->kernel_filename != NULL);
  580. fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4,
  581. &address_space_memory);
  582. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  583. rom_set_fw(fw_cfg);
  584. x86_load_linux(x86ms, fw_cfg, PC_FW_DATA, pcmc->pvh_enabled);
  585. for (i = 0; i < nb_option_roms; i++) {
  586. assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
  587. !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
  588. !strcmp(option_rom[i].name, "pvh.bin") ||
  589. !strcmp(option_rom[i].name, "multiboot.bin") ||
  590. !strcmp(option_rom[i].name, "multiboot_dma.bin"));
  591. rom_add_option(option_rom[i].name, option_rom[i].bootindex);
  592. }
  593. x86ms->fw_cfg = fw_cfg;
  594. }
  595. #define PC_ROM_MIN_VGA 0xc0000
  596. #define PC_ROM_MIN_OPTION 0xc8000
  597. #define PC_ROM_MAX 0xe0000
  598. #define PC_ROM_ALIGN 0x800
  599. #define PC_ROM_SIZE (PC_ROM_MAX - PC_ROM_MIN_VGA)
  600. static hwaddr pc_above_4g_end(PCMachineState *pcms)
  601. {
  602. X86MachineState *x86ms = X86_MACHINE(pcms);
  603. if (pcms->sgx_epc.size != 0) {
  604. return sgx_epc_above_4g_end(&pcms->sgx_epc);
  605. }
  606. return x86ms->above_4g_mem_start + x86ms->above_4g_mem_size;
  607. }
  608. static void pc_get_device_memory_range(PCMachineState *pcms,
  609. hwaddr *base,
  610. ram_addr_t *device_mem_size)
  611. {
  612. MachineState *machine = MACHINE(pcms);
  613. ram_addr_t size;
  614. hwaddr addr;
  615. size = machine->maxram_size - machine->ram_size;
  616. addr = ROUND_UP(pc_above_4g_end(pcms), 1 * GiB);
  617. /* size device region assuming 1G page max alignment per slot */
  618. size += (1 * GiB) * machine->ram_slots;
  619. *base = addr;
  620. *device_mem_size = size;
  621. }
  622. static uint64_t pc_get_cxl_range_start(PCMachineState *pcms)
  623. {
  624. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  625. MachineState *ms = MACHINE(pcms);
  626. hwaddr cxl_base;
  627. ram_addr_t size;
  628. if (pcmc->has_reserved_memory &&
  629. (ms->ram_size < ms->maxram_size)) {
  630. pc_get_device_memory_range(pcms, &cxl_base, &size);
  631. cxl_base += size;
  632. } else {
  633. cxl_base = pc_above_4g_end(pcms);
  634. }
  635. return cxl_base;
  636. }
  637. static uint64_t pc_get_cxl_range_end(PCMachineState *pcms)
  638. {
  639. uint64_t start = pc_get_cxl_range_start(pcms) + MiB;
  640. if (pcms->cxl_devices_state.fixed_windows) {
  641. GList *it;
  642. start = ROUND_UP(start, 256 * MiB);
  643. for (it = pcms->cxl_devices_state.fixed_windows; it; it = it->next) {
  644. CXLFixedWindow *fw = it->data;
  645. start += fw->size;
  646. }
  647. }
  648. return start;
  649. }
  650. static hwaddr pc_max_used_gpa(PCMachineState *pcms, uint64_t pci_hole64_size)
  651. {
  652. X86CPU *cpu = X86_CPU(first_cpu);
  653. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  654. MachineState *ms = MACHINE(pcms);
  655. if (cpu->env.features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
  656. /* 64-bit systems */
  657. return pc_pci_hole64_start() + pci_hole64_size - 1;
  658. }
  659. /* 32-bit systems */
  660. if (pcmc->broken_32bit_mem_addr_check) {
  661. /* old value for compatibility reasons */
  662. return ((hwaddr)1 << cpu->phys_bits) - 1;
  663. }
  664. /*
  665. * 32-bit systems don't have hole64 but they might have a region for
  666. * memory devices. Even if additional hotplugged memory devices might
  667. * not be usable by most guest OSes, we need to still consider them for
  668. * calculating the highest possible GPA so that we can properly report
  669. * if someone configures them on a CPU that cannot possibly address them.
  670. */
  671. if (pcmc->has_reserved_memory &&
  672. (ms->ram_size < ms->maxram_size)) {
  673. hwaddr devmem_start;
  674. ram_addr_t devmem_size;
  675. pc_get_device_memory_range(pcms, &devmem_start, &devmem_size);
  676. devmem_start += devmem_size;
  677. return devmem_start - 1;
  678. }
  679. /* configuration without any memory hotplug */
  680. return pc_above_4g_end(pcms) - 1;
  681. }
  682. /*
  683. * AMD systems with an IOMMU have an additional hole close to the
  684. * 1Tb, which are special GPAs that cannot be DMA mapped. Depending
  685. * on kernel version, VFIO may or may not let you DMA map those ranges.
  686. * Starting Linux v5.4 we validate it, and can't create guests on AMD machines
  687. * with certain memory sizes. It's also wrong to use those IOVA ranges
  688. * in detriment of leading to IOMMU INVALID_DEVICE_REQUEST or worse.
  689. * The ranges reserved for Hyper-Transport are:
  690. *
  691. * FD_0000_0000h - FF_FFFF_FFFFh
  692. *
  693. * The ranges represent the following:
  694. *
  695. * Base Address Top Address Use
  696. *
  697. * FD_0000_0000h FD_F7FF_FFFFh Reserved interrupt address space
  698. * FD_F800_0000h FD_F8FF_FFFFh Interrupt/EOI IntCtl
  699. * FD_F900_0000h FD_F90F_FFFFh Legacy PIC IACK
  700. * FD_F910_0000h FD_F91F_FFFFh System Management
  701. * FD_F920_0000h FD_FAFF_FFFFh Reserved Page Tables
  702. * FD_FB00_0000h FD_FBFF_FFFFh Address Translation
  703. * FD_FC00_0000h FD_FDFF_FFFFh I/O Space
  704. * FD_FE00_0000h FD_FFFF_FFFFh Configuration
  705. * FE_0000_0000h FE_1FFF_FFFFh Extended Configuration/Device Messages
  706. * FE_2000_0000h FF_FFFF_FFFFh Reserved
  707. *
  708. * See AMD IOMMU spec, section 2.1.2 "IOMMU Logical Topology",
  709. * Table 3: Special Address Controls (GPA) for more information.
  710. */
  711. #define AMD_HT_START 0xfd00000000UL
  712. #define AMD_HT_END 0xffffffffffUL
  713. #define AMD_ABOVE_1TB_START (AMD_HT_END + 1)
  714. #define AMD_HT_SIZE (AMD_ABOVE_1TB_START - AMD_HT_START)
  715. void pc_memory_init(PCMachineState *pcms,
  716. MemoryRegion *system_memory,
  717. MemoryRegion *rom_memory,
  718. uint64_t pci_hole64_size)
  719. {
  720. int linux_boot, i;
  721. MemoryRegion *option_rom_mr;
  722. MemoryRegion *ram_below_4g, *ram_above_4g;
  723. FWCfgState *fw_cfg;
  724. MachineState *machine = MACHINE(pcms);
  725. MachineClass *mc = MACHINE_GET_CLASS(machine);
  726. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  727. X86MachineState *x86ms = X86_MACHINE(pcms);
  728. hwaddr maxphysaddr, maxusedaddr;
  729. hwaddr cxl_base, cxl_resv_end = 0;
  730. X86CPU *cpu = X86_CPU(first_cpu);
  731. assert(machine->ram_size == x86ms->below_4g_mem_size +
  732. x86ms->above_4g_mem_size);
  733. linux_boot = (machine->kernel_filename != NULL);
  734. /*
  735. * The HyperTransport range close to the 1T boundary is unique to AMD
  736. * hosts with IOMMUs enabled. Restrict the ram-above-4g relocation
  737. * to above 1T to AMD vCPUs only. @enforce_amd_1tb_hole is only false in
  738. * older machine types (<= 7.0) for compatibility purposes.
  739. */
  740. if (IS_AMD_CPU(&cpu->env) && pcmc->enforce_amd_1tb_hole) {
  741. /* Bail out if max possible address does not cross HT range */
  742. if (pc_max_used_gpa(pcms, pci_hole64_size) >= AMD_HT_START) {
  743. x86ms->above_4g_mem_start = AMD_ABOVE_1TB_START;
  744. }
  745. /*
  746. * Advertise the HT region if address space covers the reserved
  747. * region or if we relocate.
  748. */
  749. if (cpu->phys_bits >= 40) {
  750. e820_add_entry(AMD_HT_START, AMD_HT_SIZE, E820_RESERVED);
  751. }
  752. }
  753. /*
  754. * phys-bits is required to be appropriately configured
  755. * to make sure max used GPA is reachable.
  756. */
  757. maxusedaddr = pc_max_used_gpa(pcms, pci_hole64_size);
  758. maxphysaddr = ((hwaddr)1 << cpu->phys_bits) - 1;
  759. if (maxphysaddr < maxusedaddr) {
  760. error_report("Address space limit 0x%"PRIx64" < 0x%"PRIx64
  761. " phys-bits too low (%u)",
  762. maxphysaddr, maxusedaddr, cpu->phys_bits);
  763. exit(EXIT_FAILURE);
  764. }
  765. /*
  766. * Split single memory region and use aliases to address portions of it,
  767. * done for backwards compatibility with older qemus.
  768. */
  769. ram_below_4g = g_malloc(sizeof(*ram_below_4g));
  770. memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", machine->ram,
  771. 0, x86ms->below_4g_mem_size);
  772. memory_region_add_subregion(system_memory, 0, ram_below_4g);
  773. e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
  774. if (x86ms->above_4g_mem_size > 0) {
  775. ram_above_4g = g_malloc(sizeof(*ram_above_4g));
  776. memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g",
  777. machine->ram,
  778. x86ms->below_4g_mem_size,
  779. x86ms->above_4g_mem_size);
  780. memory_region_add_subregion(system_memory, x86ms->above_4g_mem_start,
  781. ram_above_4g);
  782. e820_add_entry(x86ms->above_4g_mem_start, x86ms->above_4g_mem_size,
  783. E820_RAM);
  784. }
  785. if (pcms->sgx_epc.size != 0) {
  786. e820_add_entry(pcms->sgx_epc.base, pcms->sgx_epc.size, E820_RESERVED);
  787. }
  788. if (!pcmc->has_reserved_memory &&
  789. (machine->ram_slots ||
  790. (machine->maxram_size > machine->ram_size))) {
  791. error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
  792. mc->name);
  793. exit(EXIT_FAILURE);
  794. }
  795. /* initialize device memory address space */
  796. if (pcmc->has_reserved_memory &&
  797. (machine->ram_size < machine->maxram_size)) {
  798. ram_addr_t device_mem_size;
  799. hwaddr device_mem_base;
  800. if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
  801. error_report("unsupported amount of memory slots: %"PRIu64,
  802. machine->ram_slots);
  803. exit(EXIT_FAILURE);
  804. }
  805. if (QEMU_ALIGN_UP(machine->maxram_size,
  806. TARGET_PAGE_SIZE) != machine->maxram_size) {
  807. error_report("maximum memory size must by aligned to multiple of "
  808. "%d bytes", TARGET_PAGE_SIZE);
  809. exit(EXIT_FAILURE);
  810. }
  811. pc_get_device_memory_range(pcms, &device_mem_base, &device_mem_size);
  812. if (device_mem_base + device_mem_size < device_mem_size) {
  813. error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
  814. machine->maxram_size);
  815. exit(EXIT_FAILURE);
  816. }
  817. machine_memory_devices_init(machine, device_mem_base, device_mem_size);
  818. }
  819. if (pcms->cxl_devices_state.is_enabled) {
  820. MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
  821. hwaddr cxl_size = MiB;
  822. cxl_base = pc_get_cxl_range_start(pcms);
  823. memory_region_init(mr, OBJECT(machine), "cxl_host_reg", cxl_size);
  824. memory_region_add_subregion(system_memory, cxl_base, mr);
  825. cxl_resv_end = cxl_base + cxl_size;
  826. if (pcms->cxl_devices_state.fixed_windows) {
  827. hwaddr cxl_fmw_base;
  828. GList *it;
  829. cxl_fmw_base = ROUND_UP(cxl_base + cxl_size, 256 * MiB);
  830. for (it = pcms->cxl_devices_state.fixed_windows; it; it = it->next) {
  831. CXLFixedWindow *fw = it->data;
  832. fw->base = cxl_fmw_base;
  833. memory_region_init_io(&fw->mr, OBJECT(machine), &cfmws_ops, fw,
  834. "cxl-fixed-memory-region", fw->size);
  835. memory_region_add_subregion(system_memory, fw->base, &fw->mr);
  836. cxl_fmw_base += fw->size;
  837. cxl_resv_end = cxl_fmw_base;
  838. }
  839. }
  840. }
  841. /* Initialize PC system firmware */
  842. pc_system_firmware_init(pcms, rom_memory);
  843. option_rom_mr = g_malloc(sizeof(*option_rom_mr));
  844. if (machine_require_guest_memfd(machine)) {
  845. memory_region_init_ram_guest_memfd(option_rom_mr, NULL, "pc.rom",
  846. PC_ROM_SIZE, &error_fatal);
  847. } else {
  848. memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
  849. &error_fatal);
  850. if (pcmc->pci_enabled) {
  851. memory_region_set_readonly(option_rom_mr, true);
  852. }
  853. }
  854. memory_region_add_subregion_overlap(rom_memory,
  855. PC_ROM_MIN_VGA,
  856. option_rom_mr,
  857. 1);
  858. fw_cfg = fw_cfg_arch_create(machine,
  859. x86ms->boot_cpus, x86ms->apic_id_limit);
  860. rom_set_fw(fw_cfg);
  861. if (machine->device_memory) {
  862. uint64_t *val = g_malloc(sizeof(*val));
  863. uint64_t res_mem_end = machine->device_memory->base;
  864. if (!pcmc->broken_reserved_end) {
  865. res_mem_end += memory_region_size(&machine->device_memory->mr);
  866. }
  867. if (pcms->cxl_devices_state.is_enabled) {
  868. res_mem_end = cxl_resv_end;
  869. }
  870. *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
  871. fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
  872. }
  873. if (linux_boot) {
  874. x86_load_linux(x86ms, fw_cfg, PC_FW_DATA, pcmc->pvh_enabled);
  875. }
  876. for (i = 0; i < nb_option_roms; i++) {
  877. rom_add_option(option_rom[i].name, option_rom[i].bootindex);
  878. }
  879. x86ms->fw_cfg = fw_cfg;
  880. /* Init default IOAPIC address space */
  881. x86ms->ioapic_as = &address_space_memory;
  882. /* Init ACPI memory hotplug IO base address */
  883. pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
  884. }
  885. /*
  886. * The 64bit pci hole starts after "above 4G RAM" and
  887. * potentially the space reserved for memory hotplug.
  888. */
  889. uint64_t pc_pci_hole64_start(void)
  890. {
  891. PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
  892. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  893. MachineState *ms = MACHINE(pcms);
  894. uint64_t hole64_start = 0;
  895. ram_addr_t size = 0;
  896. if (pcms->cxl_devices_state.is_enabled) {
  897. hole64_start = pc_get_cxl_range_end(pcms);
  898. } else if (pcmc->has_reserved_memory && (ms->ram_size < ms->maxram_size)) {
  899. pc_get_device_memory_range(pcms, &hole64_start, &size);
  900. if (!pcmc->broken_reserved_end) {
  901. hole64_start += size;
  902. }
  903. } else {
  904. hole64_start = pc_above_4g_end(pcms);
  905. }
  906. return ROUND_UP(hole64_start, 1 * GiB);
  907. }
  908. DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
  909. {
  910. DeviceState *dev = NULL;
  911. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
  912. if (pci_bus) {
  913. PCIDevice *pcidev = pci_vga_init(pci_bus);
  914. dev = pcidev ? &pcidev->qdev : NULL;
  915. } else if (isa_bus) {
  916. ISADevice *isadev = isa_vga_init(isa_bus);
  917. dev = isadev ? DEVICE(isadev) : NULL;
  918. }
  919. rom_reset_order_override();
  920. return dev;
  921. }
  922. static const MemoryRegionOps ioport80_io_ops = {
  923. .write = ioport80_write,
  924. .read = ioport80_read,
  925. .endianness = DEVICE_LITTLE_ENDIAN,
  926. .impl = {
  927. .min_access_size = 1,
  928. .max_access_size = 1,
  929. },
  930. };
  931. static const MemoryRegionOps ioportF0_io_ops = {
  932. .write = ioportF0_write,
  933. .read = ioportF0_read,
  934. .endianness = DEVICE_LITTLE_ENDIAN,
  935. .impl = {
  936. .min_access_size = 1,
  937. .max_access_size = 1,
  938. },
  939. };
  940. static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl,
  941. bool create_i8042, bool no_vmport, Error **errp)
  942. {
  943. int i;
  944. DriveInfo *fd[MAX_FD];
  945. qemu_irq *a20_line;
  946. ISADevice *i8042, *port92, *vmmouse;
  947. serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
  948. parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
  949. for (i = 0; i < MAX_FD; i++) {
  950. fd[i] = drive_get(IF_FLOPPY, 0, i);
  951. create_fdctrl |= !!fd[i];
  952. }
  953. if (create_fdctrl) {
  954. #ifdef CONFIG_FDC_ISA
  955. ISADevice *fdc = isa_new(TYPE_ISA_FDC);
  956. if (fdc) {
  957. isa_realize_and_unref(fdc, isa_bus, &error_fatal);
  958. isa_fdc_init_drives(fdc, fd);
  959. }
  960. #endif
  961. }
  962. if (!create_i8042) {
  963. if (!no_vmport) {
  964. error_setg(errp,
  965. "vmport requires the i8042 controller to be enabled");
  966. }
  967. return;
  968. }
  969. i8042 = isa_create_simple(isa_bus, TYPE_I8042);
  970. if (!no_vmport) {
  971. isa_create_simple(isa_bus, TYPE_VMPORT);
  972. vmmouse = isa_try_new("vmmouse");
  973. } else {
  974. vmmouse = NULL;
  975. }
  976. if (vmmouse) {
  977. object_property_set_link(OBJECT(vmmouse), TYPE_I8042, OBJECT(i8042),
  978. &error_abort);
  979. isa_realize_and_unref(vmmouse, isa_bus, &error_fatal);
  980. }
  981. port92 = isa_create_simple(isa_bus, TYPE_PORT92);
  982. a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
  983. qdev_connect_gpio_out_named(DEVICE(i8042),
  984. I8042_A20_LINE, 0, a20_line[0]);
  985. qdev_connect_gpio_out_named(DEVICE(port92),
  986. PORT92_A20_LINE, 0, a20_line[1]);
  987. g_free(a20_line);
  988. }
  989. void pc_basic_device_init(struct PCMachineState *pcms,
  990. ISABus *isa_bus, qemu_irq *gsi,
  991. ISADevice *rtc_state,
  992. bool create_fdctrl,
  993. uint32_t hpet_irqs)
  994. {
  995. int i;
  996. DeviceState *hpet = NULL;
  997. int pit_isa_irq = 0;
  998. qemu_irq pit_alt_irq = NULL;
  999. ISADevice *pit = NULL;
  1000. MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
  1001. MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
  1002. X86MachineState *x86ms = X86_MACHINE(pcms);
  1003. memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
  1004. memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
  1005. memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
  1006. memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
  1007. /*
  1008. * Check if an HPET shall be created.
  1009. */
  1010. if (pcms->hpet_enabled) {
  1011. qemu_irq rtc_irq;
  1012. hpet = qdev_try_new(TYPE_HPET);
  1013. if (!hpet) {
  1014. error_report("couldn't create HPET device");
  1015. exit(1);
  1016. }
  1017. /*
  1018. * For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-*,
  1019. * use IRQ16~23, IRQ8 and IRQ2. If the user has already set
  1020. * the property, use whatever mask they specified.
  1021. */
  1022. uint8_t compat = object_property_get_uint(OBJECT(hpet),
  1023. HPET_INTCAP, NULL);
  1024. if (!compat) {
  1025. qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
  1026. }
  1027. sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), &error_fatal);
  1028. sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
  1029. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  1030. sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
  1031. }
  1032. pit_isa_irq = -1;
  1033. pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
  1034. rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
  1035. /* overwrite connection created by south bridge */
  1036. qdev_connect_gpio_out(DEVICE(rtc_state), 0, rtc_irq);
  1037. }
  1038. object_property_add_alias(OBJECT(pcms), "rtc-time", OBJECT(rtc_state),
  1039. "date");
  1040. #ifdef CONFIG_XEN_EMU
  1041. if (xen_mode == XEN_EMULATE) {
  1042. xen_overlay_create();
  1043. xen_evtchn_create(IOAPIC_NUM_PINS, gsi);
  1044. xen_gnttab_create();
  1045. xen_xenstore_create();
  1046. if (pcms->pcibus) {
  1047. pci_create_simple(pcms->pcibus, -1, "xen-platform");
  1048. }
  1049. xen_bus_init();
  1050. }
  1051. #endif
  1052. qemu_register_boot_set(pc_boot_set, pcms);
  1053. set_boot_dev(pcms, MC146818_RTC(rtc_state),
  1054. MACHINE(pcms)->boot_config.order, &error_fatal);
  1055. if (!xen_enabled() &&
  1056. (x86ms->pit == ON_OFF_AUTO_AUTO || x86ms->pit == ON_OFF_AUTO_ON)) {
  1057. if (kvm_pit_in_kernel()) {
  1058. pit = kvm_pit_init(isa_bus, 0x40);
  1059. } else {
  1060. pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
  1061. }
  1062. if (hpet) {
  1063. /* connect PIT to output control line of the HPET */
  1064. qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
  1065. }
  1066. object_property_set_link(OBJECT(pcms->pcspk), "pit",
  1067. OBJECT(pit), &error_fatal);
  1068. isa_realize_and_unref(pcms->pcspk, isa_bus, &error_fatal);
  1069. }
  1070. if (pcms->vmport == ON_OFF_AUTO_AUTO) {
  1071. pcms->vmport = (xen_enabled() || !pcms->i8042_enabled)
  1072. ? ON_OFF_AUTO_OFF : ON_OFF_AUTO_ON;
  1073. }
  1074. /* Super I/O */
  1075. pc_superio_init(isa_bus, create_fdctrl, pcms->i8042_enabled,
  1076. pcms->vmport != ON_OFF_AUTO_ON, &error_fatal);
  1077. }
  1078. void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
  1079. {
  1080. MachineClass *mc = MACHINE_CLASS(pcmc);
  1081. bool default_is_ne2k = g_str_equal(mc->default_nic, TYPE_ISA_NE2000);
  1082. NICInfo *nd;
  1083. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
  1084. while ((nd = qemu_find_nic_info(TYPE_ISA_NE2000, default_is_ne2k, NULL))) {
  1085. pc_init_ne2k_isa(isa_bus, nd, &error_fatal);
  1086. }
  1087. /* Anything remaining should be a PCI NIC */
  1088. if (pci_bus) {
  1089. pci_init_nic_devices(pci_bus, mc->default_nic);
  1090. }
  1091. rom_reset_order_override();
  1092. }
  1093. void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs)
  1094. {
  1095. qemu_irq *i8259;
  1096. if (kvm_pic_in_kernel()) {
  1097. i8259 = kvm_i8259_init(isa_bus);
  1098. } else if (xen_enabled()) {
  1099. i8259 = xen_interrupt_controller_init();
  1100. } else {
  1101. i8259 = i8259_init(isa_bus, x86_allocate_cpu_irq());
  1102. }
  1103. for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
  1104. i8259_irqs[i] = i8259[i];
  1105. }
  1106. g_free(i8259);
  1107. }
  1108. static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  1109. Error **errp)
  1110. {
  1111. const X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  1112. const MachineState *ms = MACHINE(hotplug_dev);
  1113. const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  1114. Error *local_err = NULL;
  1115. /*
  1116. * When "acpi=off" is used with the Q35 machine type, no ACPI is built,
  1117. * but pcms->acpi_dev is still created. Check !acpi_enabled in
  1118. * addition to cover this case.
  1119. */
  1120. if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
  1121. error_setg(errp,
  1122. "memory hotplug is not enabled: missing acpi device or acpi disabled");
  1123. return;
  1124. }
  1125. if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
  1126. error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
  1127. return;
  1128. }
  1129. hotplug_handler_pre_plug(x86ms->acpi_dev, dev, &local_err);
  1130. if (local_err) {
  1131. error_propagate(errp, local_err);
  1132. return;
  1133. }
  1134. pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), errp);
  1135. }
  1136. static void pc_memory_plug(HotplugHandler *hotplug_dev,
  1137. DeviceState *dev, Error **errp)
  1138. {
  1139. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1140. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  1141. MachineState *ms = MACHINE(hotplug_dev);
  1142. bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  1143. pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms));
  1144. if (is_nvdimm) {
  1145. nvdimm_plug(ms->nvdimms_state);
  1146. }
  1147. hotplug_handler_plug(x86ms->acpi_dev, dev, &error_abort);
  1148. }
  1149. static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
  1150. DeviceState *dev, Error **errp)
  1151. {
  1152. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  1153. /*
  1154. * When "acpi=off" is used with the Q35 machine type, no ACPI is built,
  1155. * but pcms->acpi_dev is still created. Check !acpi_enabled in
  1156. * addition to cover this case.
  1157. */
  1158. if (!x86ms->acpi_dev || !x86_machine_is_acpi_enabled(x86ms)) {
  1159. error_setg(errp,
  1160. "memory hotplug is not enabled: missing acpi device or acpi disabled");
  1161. return;
  1162. }
  1163. if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
  1164. error_setg(errp, "nvdimm device hot unplug is not supported yet.");
  1165. return;
  1166. }
  1167. hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
  1168. errp);
  1169. }
  1170. static void pc_memory_unplug(HotplugHandler *hotplug_dev,
  1171. DeviceState *dev, Error **errp)
  1172. {
  1173. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1174. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  1175. Error *local_err = NULL;
  1176. hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
  1177. if (local_err) {
  1178. goto out;
  1179. }
  1180. pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
  1181. qdev_unrealize(dev);
  1182. out:
  1183. error_propagate(errp, local_err);
  1184. }
  1185. static void pc_hv_balloon_pre_plug(HotplugHandler *hotplug_dev,
  1186. DeviceState *dev, Error **errp)
  1187. {
  1188. /* The vmbus handler has no hotplug handler; we should never end up here. */
  1189. g_assert(!dev->hotplugged);
  1190. memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), errp);
  1191. }
  1192. static void pc_hv_balloon_plug(HotplugHandler *hotplug_dev,
  1193. DeviceState *dev, Error **errp)
  1194. {
  1195. memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
  1196. }
  1197. static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  1198. DeviceState *dev, Error **errp)
  1199. {
  1200. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1201. pc_memory_pre_plug(hotplug_dev, dev, errp);
  1202. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1203. x86_cpu_pre_plug(hotplug_dev, dev, errp);
  1204. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  1205. virtio_md_pci_pre_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  1206. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  1207. /* Declare the APIC range as the reserved MSI region */
  1208. char *resv_prop_str = g_strdup_printf("0xfee00000:0xfeefffff:%d",
  1209. VIRTIO_IOMMU_RESV_MEM_T_MSI);
  1210. QList *reserved_regions = qlist_new();
  1211. qlist_append_str(reserved_regions, resv_prop_str);
  1212. qdev_prop_set_array(dev, "reserved-regions", reserved_regions);
  1213. g_free(resv_prop_str);
  1214. }
  1215. if (object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE) ||
  1216. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
  1217. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1218. if (pcms->iommu) {
  1219. error_setg(errp, "QEMU does not support multiple vIOMMUs "
  1220. "for x86 yet.");
  1221. return;
  1222. }
  1223. pcms->iommu = dev;
  1224. } else if (object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON)) {
  1225. pc_hv_balloon_pre_plug(hotplug_dev, dev, errp);
  1226. }
  1227. }
  1228. static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
  1229. DeviceState *dev, Error **errp)
  1230. {
  1231. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1232. pc_memory_plug(hotplug_dev, dev, errp);
  1233. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1234. x86_cpu_plug(hotplug_dev, dev, errp);
  1235. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  1236. virtio_md_pci_plug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  1237. } else if (object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON)) {
  1238. pc_hv_balloon_plug(hotplug_dev, dev, errp);
  1239. }
  1240. }
  1241. static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  1242. DeviceState *dev, Error **errp)
  1243. {
  1244. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1245. pc_memory_unplug_request(hotplug_dev, dev, errp);
  1246. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1247. x86_cpu_unplug_request_cb(hotplug_dev, dev, errp);
  1248. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  1249. virtio_md_pci_unplug_request(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev),
  1250. errp);
  1251. } else {
  1252. error_setg(errp, "acpi: device unplug request for not supported device"
  1253. " type: %s", object_get_typename(OBJECT(dev)));
  1254. }
  1255. }
  1256. static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
  1257. DeviceState *dev, Error **errp)
  1258. {
  1259. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1260. pc_memory_unplug(hotplug_dev, dev, errp);
  1261. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1262. x86_cpu_unplug_cb(hotplug_dev, dev, errp);
  1263. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI)) {
  1264. virtio_md_pci_unplug(VIRTIO_MD_PCI(dev), MACHINE(hotplug_dev), errp);
  1265. } else {
  1266. error_setg(errp, "acpi: device unplug for not supported device"
  1267. " type: %s", object_get_typename(OBJECT(dev)));
  1268. }
  1269. }
  1270. static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
  1271. DeviceState *dev)
  1272. {
  1273. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
  1274. object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
  1275. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_MD_PCI) ||
  1276. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI) ||
  1277. object_dynamic_cast(OBJECT(dev), TYPE_HV_BALLOON) ||
  1278. object_dynamic_cast(OBJECT(dev), TYPE_X86_IOMMU_DEVICE)) {
  1279. return HOTPLUG_HANDLER(machine);
  1280. }
  1281. return NULL;
  1282. }
  1283. static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
  1284. void *opaque, Error **errp)
  1285. {
  1286. PCMachineState *pcms = PC_MACHINE(obj);
  1287. OnOffAuto vmport = pcms->vmport;
  1288. visit_type_OnOffAuto(v, name, &vmport, errp);
  1289. }
  1290. static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
  1291. void *opaque, Error **errp)
  1292. {
  1293. PCMachineState *pcms = PC_MACHINE(obj);
  1294. visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
  1295. }
  1296. static bool pc_machine_get_fd_bootchk(Object *obj, Error **errp)
  1297. {
  1298. PCMachineState *pcms = PC_MACHINE(obj);
  1299. return pcms->fd_bootchk;
  1300. }
  1301. static void pc_machine_set_fd_bootchk(Object *obj, bool value, Error **errp)
  1302. {
  1303. PCMachineState *pcms = PC_MACHINE(obj);
  1304. pcms->fd_bootchk = value;
  1305. }
  1306. static bool pc_machine_get_smbus(Object *obj, Error **errp)
  1307. {
  1308. PCMachineState *pcms = PC_MACHINE(obj);
  1309. return pcms->smbus_enabled;
  1310. }
  1311. static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
  1312. {
  1313. PCMachineState *pcms = PC_MACHINE(obj);
  1314. pcms->smbus_enabled = value;
  1315. }
  1316. static bool pc_machine_get_sata(Object *obj, Error **errp)
  1317. {
  1318. PCMachineState *pcms = PC_MACHINE(obj);
  1319. return pcms->sata_enabled;
  1320. }
  1321. static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
  1322. {
  1323. PCMachineState *pcms = PC_MACHINE(obj);
  1324. pcms->sata_enabled = value;
  1325. }
  1326. static bool pc_machine_get_hpet(Object *obj, Error **errp)
  1327. {
  1328. PCMachineState *pcms = PC_MACHINE(obj);
  1329. return pcms->hpet_enabled;
  1330. }
  1331. static void pc_machine_set_hpet(Object *obj, bool value, Error **errp)
  1332. {
  1333. PCMachineState *pcms = PC_MACHINE(obj);
  1334. pcms->hpet_enabled = value;
  1335. }
  1336. static bool pc_machine_get_i8042(Object *obj, Error **errp)
  1337. {
  1338. PCMachineState *pcms = PC_MACHINE(obj);
  1339. return pcms->i8042_enabled;
  1340. }
  1341. static void pc_machine_set_i8042(Object *obj, bool value, Error **errp)
  1342. {
  1343. PCMachineState *pcms = PC_MACHINE(obj);
  1344. pcms->i8042_enabled = value;
  1345. }
  1346. static bool pc_machine_get_default_bus_bypass_iommu(Object *obj, Error **errp)
  1347. {
  1348. PCMachineState *pcms = PC_MACHINE(obj);
  1349. return pcms->default_bus_bypass_iommu;
  1350. }
  1351. static void pc_machine_set_default_bus_bypass_iommu(Object *obj, bool value,
  1352. Error **errp)
  1353. {
  1354. PCMachineState *pcms = PC_MACHINE(obj);
  1355. pcms->default_bus_bypass_iommu = value;
  1356. }
  1357. static void pc_machine_get_smbios_ep(Object *obj, Visitor *v, const char *name,
  1358. void *opaque, Error **errp)
  1359. {
  1360. PCMachineState *pcms = PC_MACHINE(obj);
  1361. SmbiosEntryPointType smbios_entry_point_type = pcms->smbios_entry_point_type;
  1362. visit_type_SmbiosEntryPointType(v, name, &smbios_entry_point_type, errp);
  1363. }
  1364. static void pc_machine_set_smbios_ep(Object *obj, Visitor *v, const char *name,
  1365. void *opaque, Error **errp)
  1366. {
  1367. PCMachineState *pcms = PC_MACHINE(obj);
  1368. visit_type_SmbiosEntryPointType(v, name, &pcms->smbios_entry_point_type, errp);
  1369. }
  1370. static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
  1371. const char *name, void *opaque,
  1372. Error **errp)
  1373. {
  1374. PCMachineState *pcms = PC_MACHINE(obj);
  1375. uint64_t value = pcms->max_ram_below_4g;
  1376. visit_type_size(v, name, &value, errp);
  1377. }
  1378. static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
  1379. const char *name, void *opaque,
  1380. Error **errp)
  1381. {
  1382. PCMachineState *pcms = PC_MACHINE(obj);
  1383. uint64_t value;
  1384. if (!visit_type_size(v, name, &value, errp)) {
  1385. return;
  1386. }
  1387. if (value > 4 * GiB) {
  1388. error_setg(errp,
  1389. "Machine option 'max-ram-below-4g=%"PRIu64
  1390. "' expects size less than or equal to 4G", value);
  1391. return;
  1392. }
  1393. if (value < 1 * MiB) {
  1394. warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
  1395. "BIOS may not work with less than 1MiB", value);
  1396. }
  1397. pcms->max_ram_below_4g = value;
  1398. }
  1399. static void pc_machine_get_max_fw_size(Object *obj, Visitor *v,
  1400. const char *name, void *opaque,
  1401. Error **errp)
  1402. {
  1403. PCMachineState *pcms = PC_MACHINE(obj);
  1404. uint64_t value = pcms->max_fw_size;
  1405. visit_type_size(v, name, &value, errp);
  1406. }
  1407. static void pc_machine_set_max_fw_size(Object *obj, Visitor *v,
  1408. const char *name, void *opaque,
  1409. Error **errp)
  1410. {
  1411. PCMachineState *pcms = PC_MACHINE(obj);
  1412. uint64_t value;
  1413. if (!visit_type_size(v, name, &value, errp)) {
  1414. return;
  1415. }
  1416. /*
  1417. * We don't have a theoretically justifiable exact lower bound on the base
  1418. * address of any flash mapping. In practice, the IO-APIC MMIO range is
  1419. * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
  1420. * only 18MiB-4KiB below 4GiB. For now, restrict the cumulative mapping to
  1421. * 16MiB in size.
  1422. */
  1423. if (value > 16 * MiB) {
  1424. error_setg(errp,
  1425. "User specified max allowed firmware size %" PRIu64 " is "
  1426. "greater than 16MiB. If combined firmware size exceeds "
  1427. "16MiB the system may not boot, or experience intermittent"
  1428. "stability issues.",
  1429. value);
  1430. return;
  1431. }
  1432. pcms->max_fw_size = value;
  1433. }
  1434. static void pc_machine_initfn(Object *obj)
  1435. {
  1436. PCMachineState *pcms = PC_MACHINE(obj);
  1437. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  1438. #ifdef CONFIG_VMPORT
  1439. pcms->vmport = ON_OFF_AUTO_AUTO;
  1440. #else
  1441. pcms->vmport = ON_OFF_AUTO_OFF;
  1442. #endif /* CONFIG_VMPORT */
  1443. pcms->max_ram_below_4g = 0; /* use default */
  1444. pcms->smbios_entry_point_type = pcmc->default_smbios_ep_type;
  1445. pcms->south_bridge = pcmc->default_south_bridge;
  1446. /* acpi build is enabled by default if machine supports it */
  1447. pcms->acpi_build_enabled = pcmc->has_acpi_build;
  1448. pcms->smbus_enabled = true;
  1449. pcms->sata_enabled = true;
  1450. pcms->i8042_enabled = true;
  1451. pcms->max_fw_size = 8 * MiB;
  1452. #ifdef CONFIG_HPET
  1453. pcms->hpet_enabled = true;
  1454. #endif
  1455. pcms->fd_bootchk = true;
  1456. pcms->default_bus_bypass_iommu = false;
  1457. pc_system_flash_create(pcms);
  1458. pcms->pcspk = isa_new(TYPE_PC_SPEAKER);
  1459. object_property_add_alias(OBJECT(pcms), "pcspk-audiodev",
  1460. OBJECT(pcms->pcspk), "audiodev");
  1461. if (pcmc->pci_enabled) {
  1462. cxl_machine_init(obj, &pcms->cxl_devices_state);
  1463. }
  1464. pcms->machine_done.notify = pc_machine_done;
  1465. qemu_add_machine_init_done_notifier(&pcms->machine_done);
  1466. }
  1467. static void pc_machine_reset(MachineState *machine, ResetType type)
  1468. {
  1469. CPUState *cs;
  1470. X86CPU *cpu;
  1471. qemu_devices_reset(type);
  1472. /* Reset APIC after devices have been reset to cancel
  1473. * any changes that qemu_devices_reset() might have done.
  1474. */
  1475. CPU_FOREACH(cs) {
  1476. cpu = X86_CPU(cs);
  1477. x86_cpu_after_reset(cpu);
  1478. }
  1479. }
  1480. static void pc_machine_wakeup(MachineState *machine)
  1481. {
  1482. cpu_synchronize_all_states();
  1483. pc_machine_reset(machine, RESET_TYPE_WAKEUP);
  1484. cpu_synchronize_all_post_reset();
  1485. }
  1486. static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
  1487. {
  1488. X86IOMMUState *iommu = x86_iommu_get_default();
  1489. IntelIOMMUState *intel_iommu;
  1490. if (iommu &&
  1491. object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
  1492. object_dynamic_cast((Object *)dev, "vfio-pci")) {
  1493. intel_iommu = INTEL_IOMMU_DEVICE(iommu);
  1494. if (!intel_iommu->caching_mode) {
  1495. error_setg(errp, "Device assignment is not allowed without "
  1496. "enabling caching-mode=on for Intel IOMMU.");
  1497. return false;
  1498. }
  1499. }
  1500. return true;
  1501. }
  1502. static void pc_machine_class_init(ObjectClass *oc, void *data)
  1503. {
  1504. MachineClass *mc = MACHINE_CLASS(oc);
  1505. X86MachineClass *x86mc = X86_MACHINE_CLASS(oc);
  1506. PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
  1507. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  1508. pcmc->pci_enabled = true;
  1509. pcmc->has_acpi_build = true;
  1510. pcmc->smbios_defaults = true;
  1511. pcmc->gigabyte_align = true;
  1512. pcmc->has_reserved_memory = true;
  1513. pcmc->enforce_amd_1tb_hole = true;
  1514. pcmc->isa_bios_alias = true;
  1515. pcmc->pvh_enabled = true;
  1516. pcmc->kvmclock_create_always = true;
  1517. x86mc->apic_xrupt_override = true;
  1518. assert(!mc->get_hotplug_handler);
  1519. mc->get_hotplug_handler = pc_get_hotplug_handler;
  1520. mc->hotplug_allowed = pc_hotplug_allowed;
  1521. mc->auto_enable_numa_with_memhp = true;
  1522. mc->auto_enable_numa_with_memdev = true;
  1523. mc->has_hotpluggable_cpus = true;
  1524. mc->default_boot_order = "cad";
  1525. mc->block_default_type = IF_IDE;
  1526. mc->max_cpus = 255;
  1527. mc->reset = pc_machine_reset;
  1528. mc->wakeup = pc_machine_wakeup;
  1529. hc->pre_plug = pc_machine_device_pre_plug_cb;
  1530. hc->plug = pc_machine_device_plug_cb;
  1531. hc->unplug_request = pc_machine_device_unplug_request_cb;
  1532. hc->unplug = pc_machine_device_unplug_cb;
  1533. mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
  1534. mc->nvdimm_supported = true;
  1535. mc->smp_props.dies_supported = true;
  1536. mc->smp_props.modules_supported = true;
  1537. mc->default_ram_id = "pc.ram";
  1538. pcmc->default_smbios_ep_type = SMBIOS_ENTRY_POINT_TYPE_AUTO;
  1539. object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
  1540. pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
  1541. NULL, NULL);
  1542. object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
  1543. "Maximum ram below the 4G boundary (32bit boundary)");
  1544. object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
  1545. pc_machine_get_vmport, pc_machine_set_vmport,
  1546. NULL, NULL);
  1547. object_class_property_set_description(oc, PC_MACHINE_VMPORT,
  1548. "Enable vmport (pc & q35)");
  1549. object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
  1550. pc_machine_get_smbus, pc_machine_set_smbus);
  1551. object_class_property_set_description(oc, PC_MACHINE_SMBUS,
  1552. "Enable/disable system management bus");
  1553. object_class_property_add_bool(oc, PC_MACHINE_SATA,
  1554. pc_machine_get_sata, pc_machine_set_sata);
  1555. object_class_property_set_description(oc, PC_MACHINE_SATA,
  1556. "Enable/disable Serial ATA bus");
  1557. object_class_property_add_bool(oc, "hpet",
  1558. pc_machine_get_hpet, pc_machine_set_hpet);
  1559. object_class_property_set_description(oc, "hpet",
  1560. "Enable/disable high precision event timer emulation");
  1561. object_class_property_add_bool(oc, PC_MACHINE_I8042,
  1562. pc_machine_get_i8042, pc_machine_set_i8042);
  1563. object_class_property_set_description(oc, PC_MACHINE_I8042,
  1564. "Enable/disable Intel 8042 PS/2 controller emulation");
  1565. object_class_property_add_bool(oc, "default-bus-bypass-iommu",
  1566. pc_machine_get_default_bus_bypass_iommu,
  1567. pc_machine_set_default_bus_bypass_iommu);
  1568. object_class_property_add(oc, PC_MACHINE_MAX_FW_SIZE, "size",
  1569. pc_machine_get_max_fw_size, pc_machine_set_max_fw_size,
  1570. NULL, NULL);
  1571. object_class_property_set_description(oc, PC_MACHINE_MAX_FW_SIZE,
  1572. "Maximum combined firmware size");
  1573. object_class_property_add(oc, PC_MACHINE_SMBIOS_EP, "str",
  1574. pc_machine_get_smbios_ep, pc_machine_set_smbios_ep,
  1575. NULL, NULL);
  1576. object_class_property_set_description(oc, PC_MACHINE_SMBIOS_EP,
  1577. "SMBIOS Entry Point type [32, 64]");
  1578. object_class_property_add_bool(oc, "fd-bootchk",
  1579. pc_machine_get_fd_bootchk,
  1580. pc_machine_set_fd_bootchk);
  1581. }
  1582. static const TypeInfo pc_machine_info = {
  1583. .name = TYPE_PC_MACHINE,
  1584. .parent = TYPE_X86_MACHINE,
  1585. .abstract = true,
  1586. .instance_size = sizeof(PCMachineState),
  1587. .instance_init = pc_machine_initfn,
  1588. .class_size = sizeof(PCMachineClass),
  1589. .class_init = pc_machine_class_init,
  1590. .interfaces = (InterfaceInfo[]) {
  1591. { TYPE_HOTPLUG_HANDLER },
  1592. { }
  1593. },
  1594. };
  1595. static void pc_machine_register_types(void)
  1596. {
  1597. type_register_static(&pc_machine_info);
  1598. }
  1599. type_init(pc_machine_register_types)