acpi-build.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. /* Support for generating ACPI tables and passing them to Guests
  2. *
  3. * Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
  4. * Copyright (C) 2006 Fabrice Bellard
  5. * Copyright (C) 2013 Red Hat Inc
  6. *
  7. * Author: Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qobject/qnum.h"
  23. #include "acpi-build.h"
  24. #include "acpi-common.h"
  25. #include "qemu/bitmap.h"
  26. #include "qemu/error-report.h"
  27. #include "hw/pci/pci_bridge.h"
  28. #include "hw/cxl/cxl.h"
  29. #include "hw/core/cpu.h"
  30. #include "target/i386/cpu.h"
  31. #include "hw/timer/hpet.h"
  32. #include "hw/acpi/acpi-defs.h"
  33. #include "hw/acpi/acpi.h"
  34. #include "hw/acpi/cpu.h"
  35. #include "hw/nvram/fw_cfg.h"
  36. #include "hw/acpi/bios-linker-loader.h"
  37. #include "hw/acpi/acpi_aml_interface.h"
  38. #include "hw/input/i8042.h"
  39. #include "hw/acpi/memory_hotplug.h"
  40. #include "system/tpm.h"
  41. #include "hw/acpi/tpm.h"
  42. #include "hw/acpi/vmgenid.h"
  43. #include "hw/acpi/vmclock.h"
  44. #include "hw/acpi/erst.h"
  45. #include "hw/acpi/piix4.h"
  46. #include "system/tpm_backend.h"
  47. #include "hw/rtc/mc146818rtc_regs.h"
  48. #include "migration/vmstate.h"
  49. #include "hw/mem/memory-device.h"
  50. #include "hw/mem/nvdimm.h"
  51. #include "system/numa.h"
  52. #include "system/reset.h"
  53. #include "hw/hyperv/vmbus-bridge.h"
  54. /* Supported chipsets: */
  55. #include "hw/southbridge/ich9.h"
  56. #include "hw/acpi/pcihp.h"
  57. #include "hw/i386/fw_cfg.h"
  58. #include "hw/i386/pc.h"
  59. #include "hw/pci/pci_bus.h"
  60. #include "hw/pci-host/i440fx.h"
  61. #include "hw/pci-host/q35.h"
  62. #include "hw/i386/x86-iommu.h"
  63. #include "hw/acpi/aml-build.h"
  64. #include "hw/acpi/utils.h"
  65. #include "hw/acpi/pci.h"
  66. #include "hw/acpi/cxl.h"
  67. #include "qom/qom-qobject.h"
  68. #include "hw/i386/amd_iommu.h"
  69. #include "hw/i386/intel_iommu.h"
  70. #include "hw/virtio/virtio-iommu.h"
  71. #include "hw/acpi/hmat.h"
  72. #include "hw/acpi/viot.h"
  73. #include CONFIG_DEVICES
  74. /* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
  75. * -M pc-i440fx-2.0. Even if the actual amount of AML generated grows
  76. * a little bit, there should be plenty of free space since the DSDT
  77. * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
  78. */
  79. #define ACPI_BUILD_ALIGN_SIZE 0x1000
  80. #define ACPI_BUILD_TABLE_SIZE 0x20000
  81. /* #define DEBUG_ACPI_BUILD */
  82. #ifdef DEBUG_ACPI_BUILD
  83. #define ACPI_BUILD_DPRINTF(fmt, ...) \
  84. do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
  85. #else
  86. #define ACPI_BUILD_DPRINTF(fmt, ...)
  87. #endif
  88. typedef struct AcpiPmInfo {
  89. bool s3_disabled;
  90. bool s4_disabled;
  91. bool pcihp_bridge_en;
  92. bool smi_on_cpuhp;
  93. bool smi_on_cpu_unplug;
  94. bool pcihp_root_en;
  95. uint8_t s4_val;
  96. AcpiFadtData fadt;
  97. uint16_t cpu_hp_io_base;
  98. uint16_t pcihp_io_base;
  99. uint16_t pcihp_io_len;
  100. } AcpiPmInfo;
  101. typedef struct AcpiMiscInfo {
  102. bool has_hpet;
  103. #ifdef CONFIG_TPM
  104. TPMVersion tpm_version;
  105. #endif
  106. } AcpiMiscInfo;
  107. typedef struct FwCfgTPMConfig {
  108. uint32_t tpmppi_address;
  109. uint8_t tpm_version;
  110. uint8_t tpmppi_version;
  111. } QEMU_PACKED FwCfgTPMConfig;
  112. static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
  113. const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
  114. .space_id = AML_AS_SYSTEM_IO,
  115. .address = NVDIMM_ACPI_IO_BASE,
  116. .bit_width = NVDIMM_ACPI_IO_LEN << 3
  117. };
  118. static void init_common_fadt_data(MachineState *ms, Object *o,
  119. AcpiFadtData *data)
  120. {
  121. X86MachineState *x86ms = X86_MACHINE(ms);
  122. /*
  123. * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
  124. * behavior for compatibility irrelevant to smm_enabled, which doesn't
  125. * comforms to ACPI spec.
  126. */
  127. bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
  128. true : x86_machine_is_smm_enabled(x86ms);
  129. uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
  130. AmlAddressSpace as = AML_AS_SYSTEM_IO;
  131. AcpiFadtData fadt = {
  132. .rev = 3,
  133. .flags =
  134. (1 << ACPI_FADT_F_WBINVD) |
  135. (1 << ACPI_FADT_F_PROC_C1) |
  136. (1 << ACPI_FADT_F_SLP_BUTTON) |
  137. (1 << ACPI_FADT_F_RTC_S4) |
  138. (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
  139. /* APIC destination mode ("Flat Logical") has an upper limit of 8
  140. * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
  141. * used
  142. */
  143. ((ms->smp.max_cpus > 8) ?
  144. (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
  145. .int_model = 1 /* Multiple APIC */,
  146. .rtc_century = RTC_CENTURY,
  147. .plvl2_lat = 0xfff /* C2 state not supported */,
  148. .plvl3_lat = 0xfff /* C3 state not supported */,
  149. .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
  150. .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
  151. .acpi_enable_cmd =
  152. smm_enabled ?
  153. object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
  154. 0,
  155. .acpi_disable_cmd =
  156. smm_enabled ?
  157. object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
  158. 0,
  159. .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
  160. .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
  161. .address = io + 0x04 },
  162. .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
  163. .gpe0_blk = { .space_id = as, .bit_width =
  164. object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
  165. .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
  166. },
  167. };
  168. /*
  169. * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
  170. * Flags, bit offset 1 - 8042.
  171. */
  172. fadt.iapc_boot_arch = iapc_boot_arch_8042();
  173. *data = fadt;
  174. }
  175. static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
  176. {
  177. Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM, NULL);
  178. Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE, NULL);
  179. Object *obj = piix ? piix : lpc;
  180. QObject *o;
  181. pm->cpu_hp_io_base = 0;
  182. pm->pcihp_io_base = 0;
  183. pm->pcihp_io_len = 0;
  184. pm->smi_on_cpuhp = false;
  185. pm->smi_on_cpu_unplug = false;
  186. assert(obj);
  187. init_common_fadt_data(machine, obj, &pm->fadt);
  188. if (piix) {
  189. /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
  190. pm->fadt.rev = 1;
  191. pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
  192. }
  193. if (lpc) {
  194. uint64_t smi_features = object_property_get_uint(lpc,
  195. ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
  196. struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
  197. .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
  198. pm->fadt.reset_reg = r;
  199. pm->fadt.reset_val = 0xf;
  200. pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
  201. pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
  202. pm->smi_on_cpuhp =
  203. !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
  204. pm->smi_on_cpu_unplug =
  205. !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
  206. }
  207. pm->pcihp_io_base =
  208. object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
  209. pm->pcihp_io_len =
  210. object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
  211. /* Fill in optional s3/s4 related properties */
  212. o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
  213. if (o) {
  214. pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
  215. } else {
  216. pm->s3_disabled = false;
  217. }
  218. qobject_unref(o);
  219. o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
  220. if (o) {
  221. pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
  222. } else {
  223. pm->s4_disabled = false;
  224. }
  225. qobject_unref(o);
  226. o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
  227. if (o) {
  228. pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
  229. } else {
  230. pm->s4_val = false;
  231. }
  232. qobject_unref(o);
  233. pm->pcihp_bridge_en =
  234. object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE,
  235. NULL);
  236. pm->pcihp_root_en =
  237. object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP,
  238. NULL);
  239. }
  240. static void acpi_get_misc_info(AcpiMiscInfo *info)
  241. {
  242. info->has_hpet = hpet_find();
  243. #ifdef CONFIG_TPM
  244. info->tpm_version = tpm_get_version(tpm_find());
  245. #endif
  246. }
  247. /*
  248. * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
  249. * On i386 arch we only have two pci hosts, so we can look only for them.
  250. */
  251. Object *acpi_get_i386_pci_host(void)
  252. {
  253. PCIHostState *host;
  254. host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
  255. if (!host) {
  256. host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
  257. }
  258. return OBJECT(host);
  259. }
  260. static void acpi_get_pci_holes(Range *hole, Range *hole64)
  261. {
  262. Object *pci_host;
  263. pci_host = acpi_get_i386_pci_host();
  264. if (!pci_host) {
  265. return;
  266. }
  267. range_set_bounds1(hole,
  268. object_property_get_uint(pci_host,
  269. PCI_HOST_PROP_PCI_HOLE_START,
  270. NULL),
  271. object_property_get_uint(pci_host,
  272. PCI_HOST_PROP_PCI_HOLE_END,
  273. NULL));
  274. range_set_bounds1(hole64,
  275. object_property_get_uint(pci_host,
  276. PCI_HOST_PROP_PCI_HOLE64_START,
  277. NULL),
  278. object_property_get_uint(pci_host,
  279. PCI_HOST_PROP_PCI_HOLE64_END,
  280. NULL));
  281. }
  282. static void acpi_align_size(GArray *blob, unsigned align)
  283. {
  284. /* Align size to multiple of given size. This reduces the chance
  285. * we need to change size in the future (breaking cross version migration).
  286. */
  287. g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
  288. }
  289. /*
  290. * ACPI spec 1.0b,
  291. * 5.2.6 Firmware ACPI Control Structure
  292. */
  293. static void
  294. build_facs(GArray *table_data)
  295. {
  296. const char *sig = "FACS";
  297. const uint8_t reserved[40] = {};
  298. g_array_append_vals(table_data, sig, 4); /* Signature */
  299. build_append_int_noprefix(table_data, 64, 4); /* Length */
  300. build_append_int_noprefix(table_data, 0, 4); /* Hardware Signature */
  301. build_append_int_noprefix(table_data, 0, 4); /* Firmware Waking Vector */
  302. build_append_int_noprefix(table_data, 0, 4); /* Global Lock */
  303. build_append_int_noprefix(table_data, 0, 4); /* Flags */
  304. g_array_append_vals(table_data, reserved, 40); /* Reserved */
  305. }
  306. Aml *aml_pci_device_dsm(void)
  307. {
  308. Aml *method;
  309. method = aml_method("_DSM", 4, AML_SERIALIZED);
  310. {
  311. Aml *params = aml_local(0);
  312. Aml *pkg = aml_package(2);
  313. aml_append(pkg, aml_int(0));
  314. aml_append(pkg, aml_int(0));
  315. aml_append(method, aml_store(pkg, params));
  316. aml_append(method,
  317. aml_store(aml_name("BSEL"), aml_index(params, aml_int(0))));
  318. aml_append(method,
  319. aml_store(aml_name("ASUN"), aml_index(params, aml_int(1))));
  320. aml_append(method,
  321. aml_return(aml_call5("PDSM", aml_arg(0), aml_arg(1),
  322. aml_arg(2), aml_arg(3), params))
  323. );
  324. }
  325. return method;
  326. }
  327. static void build_append_pci_dsm_func0_common(Aml *ctx, Aml *retvar)
  328. {
  329. Aml *UUID, *ifctx1;
  330. uint8_t byte_list[1] = { 0 }; /* nothing supported yet */
  331. aml_append(ctx, aml_store(aml_buffer(1, byte_list), retvar));
  332. /*
  333. * PCI Firmware Specification 3.1
  334. * 4.6. _DSM Definitions for PCI
  335. */
  336. UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
  337. ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(0), UUID)));
  338. {
  339. /* call is for unsupported UUID, bail out */
  340. aml_append(ifctx1, aml_return(retvar));
  341. }
  342. aml_append(ctx, ifctx1);
  343. ifctx1 = aml_if(aml_lless(aml_arg(1), aml_int(2)));
  344. {
  345. /* call is for unsupported REV, bail out */
  346. aml_append(ifctx1, aml_return(retvar));
  347. }
  348. aml_append(ctx, ifctx1);
  349. }
  350. static Aml *aml_pci_edsm(void)
  351. {
  352. Aml *method, *ifctx;
  353. Aml *zero = aml_int(0);
  354. Aml *func = aml_arg(2);
  355. Aml *ret = aml_local(0);
  356. Aml *aidx = aml_local(1);
  357. Aml *params = aml_arg(4);
  358. method = aml_method("EDSM", 5, AML_SERIALIZED);
  359. /* get supported functions */
  360. ifctx = aml_if(aml_equal(func, zero));
  361. {
  362. /* 1: have supported functions */
  363. /* 7: support for function 7 */
  364. const uint8_t caps = 1 | BIT(7);
  365. build_append_pci_dsm_func0_common(ifctx, ret);
  366. aml_append(ifctx, aml_store(aml_int(caps), aml_index(ret, zero)));
  367. aml_append(ifctx, aml_return(ret));
  368. }
  369. aml_append(method, ifctx);
  370. /* handle specific functions requests */
  371. /*
  372. * PCI Firmware Specification 3.1
  373. * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
  374. * Operating Systems
  375. */
  376. ifctx = aml_if(aml_equal(func, aml_int(7)));
  377. {
  378. Aml *pkg = aml_package(2);
  379. aml_append(pkg, zero);
  380. /* optional, if not impl. should return null string */
  381. aml_append(pkg, aml_string("%s", ""));
  382. aml_append(ifctx, aml_store(pkg, ret));
  383. /*
  384. * IASL is fine when initializing Package with computational data,
  385. * however it makes guest unhappy /it fails to process such AML/.
  386. * So use runtime assignment to set acpi-index after initializer
  387. * to make OSPM happy.
  388. */
  389. aml_append(ifctx,
  390. aml_store(aml_derefof(aml_index(params, aml_int(0))), aidx));
  391. aml_append(ifctx, aml_store(aidx, aml_index(ret, zero)));
  392. aml_append(ifctx, aml_return(ret));
  393. }
  394. aml_append(method, ifctx);
  395. return method;
  396. }
  397. static Aml *aml_pci_static_endpoint_dsm(PCIDevice *pdev)
  398. {
  399. Aml *method;
  400. g_assert(pdev->acpi_index != 0);
  401. method = aml_method("_DSM", 4, AML_SERIALIZED);
  402. {
  403. Aml *params = aml_local(0);
  404. Aml *pkg = aml_package(1);
  405. aml_append(pkg, aml_int(pdev->acpi_index));
  406. aml_append(method, aml_store(pkg, params));
  407. aml_append(method,
  408. aml_return(aml_call5("EDSM", aml_arg(0), aml_arg(1),
  409. aml_arg(2), aml_arg(3), params))
  410. );
  411. }
  412. return method;
  413. }
  414. static void build_append_pcihp_notify_entry(Aml *method, int slot)
  415. {
  416. Aml *if_ctx;
  417. int32_t devfn = PCI_DEVFN(slot, 0);
  418. if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
  419. aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
  420. aml_append(method, if_ctx);
  421. }
  422. static bool is_devfn_ignored_generic(const int devfn, const PCIBus *bus)
  423. {
  424. const PCIDevice *pdev = bus->devices[devfn];
  425. if (PCI_FUNC(devfn)) {
  426. if (IS_PCI_BRIDGE(pdev)) {
  427. /*
  428. * Ignore only hotplugged PCI bridges on !0 functions, but
  429. * allow describing cold plugged bridges on all functions
  430. */
  431. if (DEVICE(pdev)->hotplugged) {
  432. return true;
  433. }
  434. }
  435. }
  436. return false;
  437. }
  438. static bool is_devfn_ignored_hotplug(const int devfn, const PCIBus *bus)
  439. {
  440. PCIDevice *pdev = bus->devices[devfn];
  441. if (pdev) {
  442. return is_devfn_ignored_generic(devfn, bus) ||
  443. !DEVICE_GET_CLASS(pdev)->hotpluggable ||
  444. /* Cold plugged bridges aren't themselves hot-pluggable */
  445. (IS_PCI_BRIDGE(pdev) && !DEVICE(pdev)->hotplugged);
  446. } else { /* non populated slots */
  447. /*
  448. * hotplug is supported only for non-multifunction device
  449. * so generate device description only for function 0
  450. */
  451. if (PCI_FUNC(devfn) ||
  452. (pci_bus_is_express(bus) && PCI_SLOT(devfn) > 0)) {
  453. return true;
  454. }
  455. }
  456. return false;
  457. }
  458. void build_append_pcihp_slots(Aml *parent_scope, PCIBus *bus)
  459. {
  460. int devfn;
  461. Aml *dev, *notify_method = NULL, *method;
  462. QObject *bsel = object_property_get_qobject(OBJECT(bus),
  463. ACPI_PCIHP_PROP_BSEL, NULL);
  464. uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
  465. qobject_unref(bsel);
  466. aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
  467. notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
  468. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  469. int slot = PCI_SLOT(devfn);
  470. int adr = slot << 16 | PCI_FUNC(devfn);
  471. if (is_devfn_ignored_hotplug(devfn, bus)) {
  472. continue;
  473. }
  474. if (bus->devices[devfn]) {
  475. dev = aml_scope("S%.02X", devfn);
  476. } else {
  477. dev = aml_device("S%.02X", devfn);
  478. aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
  479. }
  480. /*
  481. * Can't declare _SUN here for every device as it changes 'slot'
  482. * enumeration order in linux kernel, so use another variable for it
  483. */
  484. aml_append(dev, aml_name_decl("ASUN", aml_int(slot)));
  485. aml_append(dev, aml_pci_device_dsm());
  486. aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
  487. /* add _EJ0 to make slot hotpluggable */
  488. method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
  489. aml_append(method,
  490. aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
  491. );
  492. aml_append(dev, method);
  493. build_append_pcihp_notify_entry(notify_method, slot);
  494. /* device descriptor has been composed, add it into parent context */
  495. aml_append(parent_scope, dev);
  496. }
  497. aml_append(parent_scope, notify_method);
  498. }
  499. void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus)
  500. {
  501. int devfn;
  502. Aml *dev;
  503. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  504. /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */
  505. int adr = PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn);
  506. PCIDevice *pdev = bus->devices[devfn];
  507. if (!pdev || is_devfn_ignored_generic(devfn, bus)) {
  508. continue;
  509. }
  510. /* start to compose PCI device descriptor */
  511. dev = aml_device("S%.02X", devfn);
  512. aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
  513. call_dev_aml_func(DEVICE(bus->devices[devfn]), dev);
  514. /* add _DSM if device has acpi-index set */
  515. if (pdev->acpi_index &&
  516. !object_property_get_bool(OBJECT(pdev), "hotpluggable",
  517. &error_abort)) {
  518. aml_append(dev, aml_pci_static_endpoint_dsm(pdev));
  519. }
  520. /* device descriptor has been composed, add it into parent context */
  521. aml_append(parent_scope, dev);
  522. }
  523. }
  524. static bool build_append_notfication_callback(Aml *parent_scope,
  525. const PCIBus *bus)
  526. {
  527. Aml *method;
  528. PCIBus *sec;
  529. QObject *bsel;
  530. int nr_notifiers = 0;
  531. GQueue *pcnt_bus_list = g_queue_new();
  532. QLIST_FOREACH(sec, &bus->child, sibling) {
  533. Aml *br_scope = aml_scope("S%.02X", sec->parent_dev->devfn);
  534. if (pci_bus_is_root(sec)) {
  535. continue;
  536. }
  537. nr_notifiers = nr_notifiers +
  538. build_append_notfication_callback(br_scope, sec);
  539. /*
  540. * add new child scope to parent
  541. * and keep track of bus that have PCNT,
  542. * bus list is used later to call children PCNTs from this level PCNT
  543. */
  544. if (nr_notifiers) {
  545. g_queue_push_tail(pcnt_bus_list, sec);
  546. aml_append(parent_scope, br_scope);
  547. }
  548. }
  549. /*
  550. * Append PCNT method to notify about events on local and child buses.
  551. * ps: hostbridge might not have hotplug (bsel) enabled but might have
  552. * child bridges that do have bsel.
  553. */
  554. method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
  555. /* If bus supports hotplug select it and notify about local events */
  556. bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
  557. if (bsel) {
  558. uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
  559. aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
  560. aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
  561. aml_int(1))); /* Device Check */
  562. aml_append(method, aml_call2("DVNT", aml_name("PCID"),
  563. aml_int(3))); /* Eject Request */
  564. nr_notifiers++;
  565. }
  566. /* Notify about child bus events in any case */
  567. while ((sec = g_queue_pop_head(pcnt_bus_list))) {
  568. aml_append(method, aml_name("^S%.02X.PCNT", sec->parent_dev->devfn));
  569. }
  570. aml_append(parent_scope, method);
  571. qobject_unref(bsel);
  572. g_queue_free(pcnt_bus_list);
  573. return !!nr_notifiers;
  574. }
  575. static Aml *aml_pci_pdsm(void)
  576. {
  577. Aml *method, *ifctx, *ifctx1;
  578. Aml *ret = aml_local(0);
  579. Aml *caps = aml_local(1);
  580. Aml *acpi_index = aml_local(2);
  581. Aml *zero = aml_int(0);
  582. Aml *one = aml_int(1);
  583. Aml *not_supp = aml_int(0xFFFFFFFF);
  584. Aml *func = aml_arg(2);
  585. Aml *params = aml_arg(4);
  586. Aml *bnum = aml_derefof(aml_index(params, aml_int(0)));
  587. Aml *sunum = aml_derefof(aml_index(params, aml_int(1)));
  588. method = aml_method("PDSM", 5, AML_SERIALIZED);
  589. /* get supported functions */
  590. ifctx = aml_if(aml_equal(func, zero));
  591. {
  592. build_append_pci_dsm_func0_common(ifctx, ret);
  593. aml_append(ifctx, aml_store(zero, caps));
  594. aml_append(ifctx,
  595. aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
  596. /*
  597. * advertise function 7 if device has acpi-index
  598. * acpi_index values:
  599. * 0: not present (default value)
  600. * FFFFFFFF: not supported (old QEMU without PIDX reg)
  601. * other: device's acpi-index
  602. */
  603. ifctx1 = aml_if(aml_lnot(
  604. aml_or(aml_equal(acpi_index, zero),
  605. aml_equal(acpi_index, not_supp), NULL)
  606. ));
  607. {
  608. /* have supported functions */
  609. aml_append(ifctx1, aml_or(caps, one, caps));
  610. /* support for function 7 */
  611. aml_append(ifctx1,
  612. aml_or(caps, aml_shiftleft(one, aml_int(7)), caps));
  613. }
  614. aml_append(ifctx, ifctx1);
  615. aml_append(ifctx, aml_store(caps, aml_index(ret, zero)));
  616. aml_append(ifctx, aml_return(ret));
  617. }
  618. aml_append(method, ifctx);
  619. /* handle specific functions requests */
  620. /*
  621. * PCI Firmware Specification 3.1
  622. * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
  623. * Operating Systems
  624. */
  625. ifctx = aml_if(aml_equal(func, aml_int(7)));
  626. {
  627. Aml *pkg = aml_package(2);
  628. aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
  629. aml_append(ifctx, aml_store(pkg, ret));
  630. /*
  631. * Windows calls func=7 without checking if it's available,
  632. * as workaround Microsoft has suggested to return invalid for func7
  633. * Package, so return 2 elements package but only initialize elements
  634. * when acpi_index is supported and leave them uninitialized, which
  635. * leads elements to being Uninitialized ObjectType and should trip
  636. * Windows into discarding result as an unexpected and prevent setting
  637. * bogus 'PCI Label' on the device.
  638. */
  639. ifctx1 = aml_if(aml_lnot(aml_lor(
  640. aml_equal(acpi_index, zero), aml_equal(acpi_index, not_supp)
  641. )));
  642. {
  643. aml_append(ifctx1, aml_store(acpi_index, aml_index(ret, zero)));
  644. /*
  645. * optional, if not impl. should return null string
  646. */
  647. aml_append(ifctx1, aml_store(aml_string("%s", ""),
  648. aml_index(ret, one)));
  649. }
  650. aml_append(ifctx, ifctx1);
  651. aml_append(ifctx, aml_return(ret));
  652. }
  653. aml_append(method, ifctx);
  654. return method;
  655. }
  656. /*
  657. * build_prt - Define interrupt routing rules
  658. *
  659. * Returns an array of 128 routes, one for each device,
  660. * based on device location.
  661. * The main goal is to equally distribute the interrupts
  662. * over the 4 existing ACPI links (works only for i440fx).
  663. * The hash function is: (slot + pin) & 3 -> "LNK[D|A|B|C]".
  664. *
  665. */
  666. static Aml *build_prt(bool is_pci0_prt)
  667. {
  668. const int nroutes = 128;
  669. Aml *rt_pkg, *method;
  670. int pin;
  671. method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
  672. assert(nroutes < 256);
  673. rt_pkg = aml_package(nroutes);
  674. for (pin = 0; pin < nroutes; pin++) {
  675. Aml *pkg = aml_package(4);
  676. int slot = pin >> 2;
  677. aml_append(pkg, aml_int((slot << 16) | 0xFFFF));
  678. aml_append(pkg, aml_int(pin & 3));
  679. /* device 1 is the power-management device, needs SCI */
  680. if (is_pci0_prt && pin == 4) {
  681. aml_append(pkg, aml_name("%s", "LNKS"));
  682. } else {
  683. static const char link_name[][5] = {"LNKD", "LNKA", "LNKB", "LNKC"};
  684. int hash = (slot + pin) & 3;
  685. aml_append(pkg, aml_name("%s", link_name[hash]));
  686. }
  687. aml_append(pkg, aml_int(0));
  688. aml_append(rt_pkg, pkg);
  689. }
  690. aml_append(method, aml_return(rt_pkg));
  691. return method;
  692. }
  693. static void build_hpet_aml(Aml *table)
  694. {
  695. Aml *crs;
  696. Aml *field;
  697. Aml *method;
  698. Aml *if_ctx;
  699. Aml *scope = aml_scope("_SB");
  700. Aml *dev = aml_device("HPET");
  701. Aml *zero = aml_int(0);
  702. Aml *id = aml_local(0);
  703. Aml *period = aml_local(1);
  704. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
  705. aml_append(dev, aml_name_decl("_UID", zero));
  706. aml_append(dev,
  707. aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
  708. HPET_LEN));
  709. field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
  710. aml_append(field, aml_named_field("VEND", 32));
  711. aml_append(field, aml_named_field("PRD", 32));
  712. aml_append(dev, field);
  713. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  714. aml_append(method, aml_store(aml_name("VEND"), id));
  715. aml_append(method, aml_store(aml_name("PRD"), period));
  716. aml_append(method, aml_shiftright(id, aml_int(16), id));
  717. if_ctx = aml_if(aml_lor(aml_equal(id, zero),
  718. aml_equal(id, aml_int(0xffff))));
  719. {
  720. aml_append(if_ctx, aml_return(zero));
  721. }
  722. aml_append(method, if_ctx);
  723. if_ctx = aml_if(aml_lor(aml_equal(period, zero),
  724. aml_lgreater(period, aml_int(100000000))));
  725. {
  726. aml_append(if_ctx, aml_return(zero));
  727. }
  728. aml_append(method, if_ctx);
  729. aml_append(method, aml_return(aml_int(0x0F)));
  730. aml_append(dev, method);
  731. crs = aml_resource_template();
  732. aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
  733. aml_append(dev, aml_name_decl("_CRS", crs));
  734. aml_append(scope, dev);
  735. aml_append(table, scope);
  736. }
  737. static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
  738. {
  739. Aml *dev;
  740. Aml *method;
  741. Aml *crs;
  742. dev = aml_device("VMBS");
  743. aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
  744. aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
  745. aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
  746. aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
  747. method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
  748. aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
  749. aml_name("STA")));
  750. aml_append(dev, method);
  751. method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
  752. aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
  753. aml_name("STA")));
  754. aml_append(dev, method);
  755. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  756. aml_append(method, aml_return(aml_name("STA")));
  757. aml_append(dev, method);
  758. aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
  759. crs = aml_resource_template();
  760. aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
  761. aml_append(dev, aml_name_decl("_CRS", crs));
  762. return dev;
  763. }
  764. static void build_dbg_aml(Aml *table)
  765. {
  766. Aml *field;
  767. Aml *method;
  768. Aml *while_ctx;
  769. Aml *scope = aml_scope("\\");
  770. Aml *buf = aml_local(0);
  771. Aml *len = aml_local(1);
  772. Aml *idx = aml_local(2);
  773. aml_append(scope,
  774. aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
  775. field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
  776. aml_append(field, aml_named_field("DBGB", 8));
  777. aml_append(scope, field);
  778. method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
  779. aml_append(method, aml_to_hexstring(aml_arg(0), buf));
  780. aml_append(method, aml_to_buffer(buf, buf));
  781. aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
  782. aml_append(method, aml_store(aml_int(0), idx));
  783. while_ctx = aml_while(aml_lless(idx, len));
  784. aml_append(while_ctx,
  785. aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
  786. aml_append(while_ctx, aml_increment(idx));
  787. aml_append(method, while_ctx);
  788. aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
  789. aml_append(scope, method);
  790. aml_append(table, scope);
  791. }
  792. static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
  793. {
  794. Aml *dev;
  795. Aml *crs;
  796. Aml *method;
  797. uint32_t irqs[] = {5, 10, 11};
  798. dev = aml_device("%s", name);
  799. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
  800. aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
  801. crs = aml_resource_template();
  802. aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
  803. AML_SHARED, irqs, ARRAY_SIZE(irqs)));
  804. aml_append(dev, aml_name_decl("_PRS", crs));
  805. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  806. aml_append(method, aml_return(aml_call1("IQST", reg)));
  807. aml_append(dev, method);
  808. method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
  809. aml_append(method, aml_or(reg, aml_int(0x80), reg));
  810. aml_append(dev, method);
  811. method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
  812. aml_append(method, aml_return(aml_call1("IQCR", reg)));
  813. aml_append(dev, method);
  814. method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
  815. aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
  816. aml_append(method, aml_store(aml_name("PRRI"), reg));
  817. aml_append(dev, method);
  818. return dev;
  819. }
  820. static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
  821. {
  822. Aml *dev;
  823. Aml *crs;
  824. Aml *method;
  825. uint32_t irqs;
  826. dev = aml_device("%s", name);
  827. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
  828. aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
  829. crs = aml_resource_template();
  830. irqs = gsi;
  831. aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
  832. AML_SHARED, &irqs, 1));
  833. aml_append(dev, aml_name_decl("_PRS", crs));
  834. aml_append(dev, aml_name_decl("_CRS", crs));
  835. /*
  836. * _DIS can be no-op because the interrupt cannot be disabled.
  837. */
  838. method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
  839. aml_append(dev, method);
  840. method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
  841. aml_append(dev, method);
  842. return dev;
  843. }
  844. /* _CRS method - get current settings */
  845. static Aml *build_iqcr_method(bool is_piix4)
  846. {
  847. Aml *if_ctx;
  848. uint32_t irqs;
  849. Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
  850. Aml *crs = aml_resource_template();
  851. irqs = 0;
  852. aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
  853. AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
  854. aml_append(method, aml_name_decl("PRR0", crs));
  855. aml_append(method,
  856. aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
  857. if (is_piix4) {
  858. if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
  859. aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
  860. aml_append(method, if_ctx);
  861. } else {
  862. aml_append(method,
  863. aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
  864. aml_name("PRRI")));
  865. }
  866. aml_append(method, aml_return(aml_name("PRR0")));
  867. return method;
  868. }
  869. /* _STA method - get status */
  870. static Aml *build_irq_status_method(void)
  871. {
  872. Aml *if_ctx;
  873. Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
  874. if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
  875. aml_append(if_ctx, aml_return(aml_int(0x09)));
  876. aml_append(method, if_ctx);
  877. aml_append(method, aml_return(aml_int(0x0B)));
  878. return method;
  879. }
  880. static void build_piix4_pci0_int(Aml *table)
  881. {
  882. Aml *dev;
  883. Aml *crs;
  884. Aml *method;
  885. uint32_t irqs;
  886. Aml *sb_scope = aml_scope("_SB");
  887. Aml *pci0_scope = aml_scope("PCI0");
  888. aml_append(pci0_scope, build_prt(true));
  889. aml_append(sb_scope, pci0_scope);
  890. aml_append(sb_scope, build_irq_status_method());
  891. aml_append(sb_scope, build_iqcr_method(true));
  892. aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
  893. aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
  894. aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
  895. aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
  896. dev = aml_device("LNKS");
  897. {
  898. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
  899. aml_append(dev, aml_name_decl("_UID", aml_int(4)));
  900. crs = aml_resource_template();
  901. irqs = 9;
  902. aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
  903. AML_ACTIVE_HIGH, AML_SHARED,
  904. &irqs, 1));
  905. aml_append(dev, aml_name_decl("_PRS", crs));
  906. /* The SCI cannot be disabled and is always attached to GSI 9,
  907. * so these are no-ops. We only need this link to override the
  908. * polarity to active high and match the content of the MADT.
  909. */
  910. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  911. aml_append(method, aml_return(aml_int(0x0b)));
  912. aml_append(dev, method);
  913. method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
  914. aml_append(dev, method);
  915. method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
  916. aml_append(method, aml_return(aml_name("_PRS")));
  917. aml_append(dev, method);
  918. method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
  919. aml_append(dev, method);
  920. }
  921. aml_append(sb_scope, dev);
  922. aml_append(table, sb_scope);
  923. }
  924. static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
  925. {
  926. int i;
  927. int head;
  928. Aml *pkg;
  929. char base = name[3] < 'E' ? 'A' : 'E';
  930. char *s = g_strdup(name);
  931. Aml *a_nr = aml_int((nr << 16) | 0xffff);
  932. assert(strlen(s) == 4);
  933. head = name[3] - base;
  934. for (i = 0; i < 4; i++) {
  935. if (head + i > 3) {
  936. head = i * -1;
  937. }
  938. s[3] = base + head + i;
  939. pkg = aml_package(4);
  940. aml_append(pkg, a_nr);
  941. aml_append(pkg, aml_int(i));
  942. aml_append(pkg, aml_name("%s", s));
  943. aml_append(pkg, aml_int(0));
  944. aml_append(ctx, pkg);
  945. }
  946. g_free(s);
  947. }
  948. static Aml *build_q35_routing_table(const char *str)
  949. {
  950. int i;
  951. Aml *pkg;
  952. char *name = g_strdup_printf("%s ", str);
  953. pkg = aml_package(128);
  954. for (i = 0; i < 0x18; i++) {
  955. name[3] = 'E' + (i & 0x3);
  956. append_q35_prt_entry(pkg, i, name);
  957. }
  958. name[3] = 'E';
  959. append_q35_prt_entry(pkg, 0x18, name);
  960. /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
  961. for (i = 0x0019; i < 0x1e; i++) {
  962. name[3] = 'A';
  963. append_q35_prt_entry(pkg, i, name);
  964. }
  965. /* PCIe->PCI bridge. use PIRQ[E-H] */
  966. name[3] = 'E';
  967. append_q35_prt_entry(pkg, 0x1e, name);
  968. name[3] = 'A';
  969. append_q35_prt_entry(pkg, 0x1f, name);
  970. g_free(name);
  971. return pkg;
  972. }
  973. static void build_q35_pci0_int(Aml *table)
  974. {
  975. Aml *method;
  976. Aml *sb_scope = aml_scope("_SB");
  977. Aml *pci0_scope = aml_scope("PCI0");
  978. /* Zero => PIC mode, One => APIC Mode */
  979. aml_append(table, aml_name_decl("PICF", aml_int(0)));
  980. method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
  981. {
  982. aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
  983. }
  984. aml_append(table, method);
  985. aml_append(pci0_scope,
  986. aml_name_decl("PRTP", build_q35_routing_table("LNK")));
  987. aml_append(pci0_scope,
  988. aml_name_decl("PRTA", build_q35_routing_table("GSI")));
  989. method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
  990. {
  991. Aml *if_ctx;
  992. Aml *else_ctx;
  993. /* PCI IRQ routing table, example from ACPI 2.0a specification,
  994. section 6.2.8.1 */
  995. /* Note: we provide the same info as the PCI routing
  996. table of the Bochs BIOS */
  997. if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
  998. aml_append(if_ctx, aml_return(aml_name("PRTP")));
  999. aml_append(method, if_ctx);
  1000. else_ctx = aml_else();
  1001. aml_append(else_ctx, aml_return(aml_name("PRTA")));
  1002. aml_append(method, else_ctx);
  1003. }
  1004. aml_append(pci0_scope, method);
  1005. aml_append(sb_scope, pci0_scope);
  1006. aml_append(sb_scope, build_irq_status_method());
  1007. aml_append(sb_scope, build_iqcr_method(false));
  1008. aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
  1009. aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
  1010. aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
  1011. aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
  1012. aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
  1013. aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
  1014. aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
  1015. aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
  1016. aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
  1017. aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
  1018. aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
  1019. aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
  1020. aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
  1021. aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
  1022. aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
  1023. aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
  1024. aml_append(table, sb_scope);
  1025. }
  1026. static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
  1027. {
  1028. Aml *dev;
  1029. Aml *resource_template;
  1030. /* DRAM controller */
  1031. dev = aml_device("DRAC");
  1032. aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
  1033. resource_template = aml_resource_template();
  1034. if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
  1035. aml_append(resource_template,
  1036. aml_qword_memory(AML_POS_DECODE,
  1037. AML_MIN_FIXED,
  1038. AML_MAX_FIXED,
  1039. AML_NON_CACHEABLE,
  1040. AML_READ_WRITE,
  1041. 0x0000000000000000,
  1042. mcfg->base,
  1043. mcfg->base + mcfg->size - 1,
  1044. 0x0000000000000000,
  1045. mcfg->size));
  1046. } else {
  1047. aml_append(resource_template,
  1048. aml_dword_memory(AML_POS_DECODE,
  1049. AML_MIN_FIXED,
  1050. AML_MAX_FIXED,
  1051. AML_NON_CACHEABLE,
  1052. AML_READ_WRITE,
  1053. 0x0000000000000000,
  1054. mcfg->base,
  1055. mcfg->base + mcfg->size - 1,
  1056. 0x0000000000000000,
  1057. mcfg->size));
  1058. }
  1059. aml_append(dev, aml_name_decl("_CRS", resource_template));
  1060. return dev;
  1061. }
  1062. static void build_x86_acpi_pci_hotplug(Aml *table, uint64_t pcihp_addr)
  1063. {
  1064. Aml *scope;
  1065. Aml *field;
  1066. Aml *method;
  1067. scope = aml_scope("_SB.PCI0");
  1068. aml_append(scope,
  1069. aml_operation_region("PCST", AML_SYSTEM_IO, aml_int(pcihp_addr), 0x08));
  1070. field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
  1071. aml_append(field, aml_named_field("PCIU", 32));
  1072. aml_append(field, aml_named_field("PCID", 32));
  1073. aml_append(scope, field);
  1074. aml_append(scope,
  1075. aml_operation_region("SEJ", AML_SYSTEM_IO,
  1076. aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04));
  1077. field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
  1078. aml_append(field, aml_named_field("B0EJ", 32));
  1079. aml_append(scope, field);
  1080. aml_append(scope,
  1081. aml_operation_region("BNMR", AML_SYSTEM_IO,
  1082. aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08));
  1083. field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
  1084. aml_append(field, aml_named_field("BNUM", 32));
  1085. aml_append(field, aml_named_field("PIDX", 32));
  1086. aml_append(scope, field);
  1087. aml_append(scope, aml_mutex("BLCK", 0));
  1088. method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
  1089. aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
  1090. aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
  1091. aml_append(method,
  1092. aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
  1093. aml_append(method, aml_release(aml_name("BLCK")));
  1094. aml_append(method, aml_return(aml_int(0)));
  1095. aml_append(scope, method);
  1096. method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
  1097. aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
  1098. aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
  1099. aml_append(method,
  1100. aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
  1101. aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
  1102. aml_append(method, aml_release(aml_name("BLCK")));
  1103. aml_append(method, aml_return(aml_local(0)));
  1104. aml_append(scope, method);
  1105. aml_append(scope, aml_pci_pdsm());
  1106. aml_append(table, scope);
  1107. }
  1108. static Aml *build_q35_osc_method(bool enable_native_pcie_hotplug)
  1109. {
  1110. Aml *if_ctx;
  1111. Aml *if_ctx2;
  1112. Aml *else_ctx;
  1113. Aml *method;
  1114. Aml *a_cwd1 = aml_name("CDW1");
  1115. Aml *a_ctrl = aml_local(0);
  1116. method = aml_method("_OSC", 4, AML_NOTSERIALIZED);
  1117. aml_append(method, aml_create_dword_field(aml_arg(3), aml_int(0), "CDW1"));
  1118. if_ctx = aml_if(aml_equal(
  1119. aml_arg(0), aml_touuid("33DB4D5B-1FF7-401C-9657-7441C03DD766")));
  1120. aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(4), "CDW2"));
  1121. aml_append(if_ctx, aml_create_dword_field(aml_arg(3), aml_int(8), "CDW3"));
  1122. aml_append(if_ctx, aml_store(aml_name("CDW3"), a_ctrl));
  1123. /*
  1124. * Always allow native PME, AER (no dependencies)
  1125. * Allow SHPC (PCI bridges can have SHPC controller)
  1126. * Disable PCIe Native Hot-plug if ACPI PCI Hot-plug is enabled.
  1127. */
  1128. aml_append(if_ctx, aml_and(a_ctrl,
  1129. aml_int(0x1E | (enable_native_pcie_hotplug ? 0x1 : 0x0)), a_ctrl));
  1130. if_ctx2 = aml_if(aml_lnot(aml_equal(aml_arg(1), aml_int(1))));
  1131. /* Unknown revision */
  1132. aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x08), a_cwd1));
  1133. aml_append(if_ctx, if_ctx2);
  1134. if_ctx2 = aml_if(aml_lnot(aml_equal(aml_name("CDW3"), a_ctrl)));
  1135. /* Capabilities bits were masked */
  1136. aml_append(if_ctx2, aml_or(a_cwd1, aml_int(0x10), a_cwd1));
  1137. aml_append(if_ctx, if_ctx2);
  1138. /* Update DWORD3 in the buffer */
  1139. aml_append(if_ctx, aml_store(a_ctrl, aml_name("CDW3")));
  1140. aml_append(method, if_ctx);
  1141. else_ctx = aml_else();
  1142. /* Unrecognized UUID */
  1143. aml_append(else_ctx, aml_or(a_cwd1, aml_int(4), a_cwd1));
  1144. aml_append(method, else_ctx);
  1145. aml_append(method, aml_return(aml_arg(3)));
  1146. return method;
  1147. }
  1148. static void build_acpi0017(Aml *table)
  1149. {
  1150. Aml *dev, *scope, *method;
  1151. scope = aml_scope("_SB");
  1152. dev = aml_device("CXLM");
  1153. aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0017")));
  1154. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  1155. aml_append(method, aml_return(aml_int(0x0B)));
  1156. aml_append(dev, method);
  1157. build_cxl_dsm_method(dev);
  1158. aml_append(scope, dev);
  1159. aml_append(table, scope);
  1160. }
  1161. static void
  1162. build_dsdt(GArray *table_data, BIOSLinker *linker,
  1163. AcpiPmInfo *pm, AcpiMiscInfo *misc,
  1164. Range *pci_hole, Range *pci_hole64, MachineState *machine)
  1165. {
  1166. Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE,
  1167. NULL);
  1168. Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE, NULL);
  1169. CrsRangeEntry *entry;
  1170. Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
  1171. CrsRangeSet crs_range_set;
  1172. PCMachineState *pcms = PC_MACHINE(machine);
  1173. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
  1174. X86MachineState *x86ms = X86_MACHINE(machine);
  1175. AcpiMcfgInfo mcfg;
  1176. bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
  1177. uint32_t nr_mem = machine->ram_slots;
  1178. int root_bus_limit = 0xFF;
  1179. PCIBus *bus = NULL;
  1180. #ifdef CONFIG_TPM
  1181. TPMIf *tpm = tpm_find();
  1182. #endif
  1183. bool cxl_present = false;
  1184. int i;
  1185. VMBusBridge *vmbus_bridge = vmbus_bridge_find();
  1186. AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id,
  1187. .oem_table_id = x86ms->oem_table_id };
  1188. assert(!!i440fx != !!q35);
  1189. acpi_table_begin(&table, table_data);
  1190. dsdt = init_aml_allocator();
  1191. build_dbg_aml(dsdt);
  1192. if (i440fx) {
  1193. sb_scope = aml_scope("_SB");
  1194. dev = aml_device("PCI0");
  1195. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
  1196. aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
  1197. aml_append(dev, aml_pci_edsm());
  1198. aml_append(sb_scope, dev);
  1199. aml_append(dsdt, sb_scope);
  1200. if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
  1201. build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
  1202. }
  1203. build_piix4_pci0_int(dsdt);
  1204. } else if (q35) {
  1205. sb_scope = aml_scope("_SB");
  1206. dev = aml_device("PCI0");
  1207. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
  1208. aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
  1209. aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
  1210. aml_append(dev, build_q35_osc_method(!pm->pcihp_bridge_en));
  1211. aml_append(dev, aml_pci_edsm());
  1212. aml_append(sb_scope, dev);
  1213. if (mcfg_valid) {
  1214. aml_append(sb_scope, build_q35_dram_controller(&mcfg));
  1215. }
  1216. if (pm->smi_on_cpuhp) {
  1217. /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
  1218. dev = aml_device("PCI0.SMI0");
  1219. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
  1220. aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
  1221. crs = aml_resource_template();
  1222. aml_append(crs,
  1223. aml_io(
  1224. AML_DECODE16,
  1225. pm->fadt.smi_cmd,
  1226. pm->fadt.smi_cmd,
  1227. 1,
  1228. 2)
  1229. );
  1230. aml_append(dev, aml_name_decl("_CRS", crs));
  1231. aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
  1232. aml_int(pm->fadt.smi_cmd), 2));
  1233. field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
  1234. AML_WRITE_AS_ZEROS);
  1235. aml_append(field, aml_named_field("SMIC", 8));
  1236. aml_append(field, aml_reserved_field(8));
  1237. aml_append(dev, field);
  1238. aml_append(sb_scope, dev);
  1239. }
  1240. aml_append(dsdt, sb_scope);
  1241. if (pm->pcihp_bridge_en) {
  1242. build_x86_acpi_pci_hotplug(dsdt, pm->pcihp_io_base);
  1243. }
  1244. build_q35_pci0_int(dsdt);
  1245. }
  1246. if (misc->has_hpet) {
  1247. build_hpet_aml(dsdt);
  1248. }
  1249. if (vmbus_bridge) {
  1250. sb_scope = aml_scope("_SB");
  1251. aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
  1252. aml_append(dsdt, sb_scope);
  1253. }
  1254. scope = aml_scope("_GPE");
  1255. {
  1256. aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
  1257. if (machine->nvdimms_state->is_enabled) {
  1258. method = aml_method("_E04", 0, AML_NOTSERIALIZED);
  1259. aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
  1260. aml_int(0x80)));
  1261. aml_append(scope, method);
  1262. }
  1263. }
  1264. aml_append(dsdt, scope);
  1265. if (pcmc->legacy_cpu_hotplug) {
  1266. build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base);
  1267. } else {
  1268. CPUHotplugFeatures opts = {
  1269. .acpi_1_compatible = true, .has_legacy_cphp = true,
  1270. .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
  1271. .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
  1272. };
  1273. build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
  1274. pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
  1275. AML_SYSTEM_IO);
  1276. }
  1277. if (pcms->memhp_io_base && nr_mem) {
  1278. build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
  1279. "\\_GPE._E03", AML_SYSTEM_IO,
  1280. pcms->memhp_io_base);
  1281. }
  1282. crs_range_set_init(&crs_range_set);
  1283. bus = PC_MACHINE(machine)->pcibus;
  1284. if (bus) {
  1285. QLIST_FOREACH(bus, &bus->child, sibling) {
  1286. uint8_t bus_num = pci_bus_num(bus);
  1287. uint8_t numa_node = pci_bus_numa_node(bus);
  1288. uint32_t uid;
  1289. /* look only for expander root buses */
  1290. if (!pci_bus_is_root(bus)) {
  1291. continue;
  1292. }
  1293. if (bus_num < root_bus_limit) {
  1294. root_bus_limit = bus_num - 1;
  1295. }
  1296. uid = object_property_get_uint(OBJECT(bus), "acpi_uid",
  1297. &error_fatal);
  1298. scope = aml_scope("\\_SB");
  1299. if (pci_bus_is_cxl(bus)) {
  1300. dev = aml_device("CL%.02X", bus_num);
  1301. } else {
  1302. dev = aml_device("PC%.02X", bus_num);
  1303. }
  1304. aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
  1305. aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
  1306. if (pci_bus_is_cxl(bus)) {
  1307. struct Aml *aml_pkg = aml_package(2);
  1308. aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016")));
  1309. aml_append(aml_pkg, aml_eisaid("PNP0A08"));
  1310. aml_append(aml_pkg, aml_eisaid("PNP0A03"));
  1311. aml_append(dev, aml_name_decl("_CID", aml_pkg));
  1312. build_cxl_osc_method(dev);
  1313. } else if (pci_bus_is_express(bus)) {
  1314. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
  1315. aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
  1316. /* Expander bridges do not have ACPI PCI Hot-plug enabled */
  1317. aml_append(dev, build_q35_osc_method(true));
  1318. } else {
  1319. aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
  1320. }
  1321. if (numa_node != NUMA_NODE_UNASSIGNED) {
  1322. aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
  1323. }
  1324. aml_append(dev, build_prt(false));
  1325. crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
  1326. 0, 0, 0, 0);
  1327. aml_append(dev, aml_name_decl("_CRS", crs));
  1328. aml_append(scope, dev);
  1329. aml_append(dsdt, scope);
  1330. /* Handle the ranges for the PXB expanders */
  1331. if (pci_bus_is_cxl(bus)) {
  1332. MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
  1333. uint64_t base = mr->addr;
  1334. cxl_present = true;
  1335. crs_range_insert(crs_range_set.mem_ranges, base,
  1336. base + memory_region_size(mr) - 1);
  1337. }
  1338. }
  1339. }
  1340. if (cxl_present) {
  1341. build_acpi0017(dsdt);
  1342. }
  1343. /*
  1344. * At this point crs_range_set has all the ranges used by pci
  1345. * busses *other* than PCI0. These ranges will be excluded from
  1346. * the PCI0._CRS. Add mmconfig to the set so it will be excluded
  1347. * too.
  1348. */
  1349. if (mcfg_valid) {
  1350. crs_range_insert(crs_range_set.mem_ranges,
  1351. mcfg.base, mcfg.base + mcfg.size - 1);
  1352. }
  1353. scope = aml_scope("\\_SB.PCI0");
  1354. /* build PCI0._CRS */
  1355. crs = aml_resource_template();
  1356. aml_append(crs,
  1357. aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
  1358. 0x0000, 0x0, root_bus_limit,
  1359. 0x0000, root_bus_limit + 1));
  1360. aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
  1361. aml_append(crs,
  1362. aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
  1363. AML_POS_DECODE, AML_ENTIRE_RANGE,
  1364. 0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
  1365. crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
  1366. for (i = 0; i < crs_range_set.io_ranges->len; i++) {
  1367. entry = g_ptr_array_index(crs_range_set.io_ranges, i);
  1368. aml_append(crs,
  1369. aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
  1370. AML_POS_DECODE, AML_ENTIRE_RANGE,
  1371. 0x0000, entry->base, entry->limit,
  1372. 0x0000, entry->limit - entry->base + 1));
  1373. }
  1374. aml_append(crs,
  1375. aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
  1376. AML_CACHEABLE, AML_READ_WRITE,
  1377. 0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
  1378. crs_replace_with_free_ranges(crs_range_set.mem_ranges,
  1379. range_lob(pci_hole),
  1380. range_upb(pci_hole));
  1381. for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
  1382. entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
  1383. aml_append(crs,
  1384. aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
  1385. AML_NON_CACHEABLE, AML_READ_WRITE,
  1386. 0, entry->base, entry->limit,
  1387. 0, entry->limit - entry->base + 1));
  1388. }
  1389. if (!range_is_empty(pci_hole64)) {
  1390. crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
  1391. range_lob(pci_hole64),
  1392. range_upb(pci_hole64));
  1393. for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
  1394. entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
  1395. aml_append(crs,
  1396. aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
  1397. AML_MAX_FIXED,
  1398. AML_CACHEABLE, AML_READ_WRITE,
  1399. 0, entry->base, entry->limit,
  1400. 0, entry->limit - entry->base + 1));
  1401. }
  1402. }
  1403. #ifdef CONFIG_TPM
  1404. if (TPM_IS_TIS_ISA(tpm_find())) {
  1405. aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
  1406. TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
  1407. }
  1408. #endif
  1409. aml_append(scope, aml_name_decl("_CRS", crs));
  1410. /* reserve GPE0 block resources */
  1411. dev = aml_device("GPE0");
  1412. aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
  1413. aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
  1414. /* device present, functioning, decoding, not shown in UI */
  1415. aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
  1416. crs = aml_resource_template();
  1417. aml_append(crs,
  1418. aml_io(
  1419. AML_DECODE16,
  1420. pm->fadt.gpe0_blk.address,
  1421. pm->fadt.gpe0_blk.address,
  1422. 1,
  1423. pm->fadt.gpe0_blk.bit_width / 8)
  1424. );
  1425. aml_append(dev, aml_name_decl("_CRS", crs));
  1426. aml_append(scope, dev);
  1427. crs_range_set_free(&crs_range_set);
  1428. /* reserve PCIHP resources */
  1429. if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
  1430. dev = aml_device("PHPR");
  1431. aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
  1432. aml_append(dev,
  1433. aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
  1434. /* device present, functioning, decoding, not shown in UI */
  1435. aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
  1436. crs = aml_resource_template();
  1437. aml_append(crs,
  1438. aml_io(AML_DECODE16, pm->pcihp_io_base, pm->pcihp_io_base, 1,
  1439. pm->pcihp_io_len)
  1440. );
  1441. aml_append(dev, aml_name_decl("_CRS", crs));
  1442. aml_append(scope, dev);
  1443. }
  1444. aml_append(dsdt, scope);
  1445. /* create S3_ / S4_ / S5_ packages if necessary */
  1446. scope = aml_scope("\\");
  1447. if (!pm->s3_disabled) {
  1448. pkg = aml_package(4);
  1449. aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
  1450. aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
  1451. aml_append(pkg, aml_int(0)); /* reserved */
  1452. aml_append(pkg, aml_int(0)); /* reserved */
  1453. aml_append(scope, aml_name_decl("_S3", pkg));
  1454. }
  1455. if (!pm->s4_disabled) {
  1456. pkg = aml_package(4);
  1457. aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
  1458. /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
  1459. aml_append(pkg, aml_int(pm->s4_val));
  1460. aml_append(pkg, aml_int(0)); /* reserved */
  1461. aml_append(pkg, aml_int(0)); /* reserved */
  1462. aml_append(scope, aml_name_decl("_S4", pkg));
  1463. }
  1464. pkg = aml_package(4);
  1465. aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
  1466. aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
  1467. aml_append(pkg, aml_int(0)); /* reserved */
  1468. aml_append(pkg, aml_int(0)); /* reserved */
  1469. aml_append(scope, aml_name_decl("_S5", pkg));
  1470. aml_append(dsdt, scope);
  1471. /* create fw_cfg node, unconditionally */
  1472. {
  1473. scope = aml_scope("\\_SB.PCI0");
  1474. fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
  1475. aml_append(dsdt, scope);
  1476. }
  1477. sb_scope = aml_scope("\\_SB");
  1478. {
  1479. Object *pci_host = acpi_get_i386_pci_host();
  1480. if (pci_host) {
  1481. PCIBus *pbus = PCI_HOST_BRIDGE(pci_host)->bus;
  1482. Aml *ascope = aml_scope("PCI0");
  1483. /* Scan all PCI buses. Generate tables to support hotplug. */
  1484. build_append_pci_bus_devices(ascope, pbus);
  1485. if (object_property_find(OBJECT(pbus), ACPI_PCIHP_PROP_BSEL)) {
  1486. build_append_pcihp_slots(ascope, pbus);
  1487. }
  1488. aml_append(sb_scope, ascope);
  1489. }
  1490. }
  1491. #ifdef CONFIG_TPM
  1492. if (TPM_IS_CRB(tpm)) {
  1493. call_dev_aml_func(DEVICE(tpm), scope);
  1494. }
  1495. #endif
  1496. if (pcms->sgx_epc.size != 0) {
  1497. uint64_t epc_base = pcms->sgx_epc.base;
  1498. uint64_t epc_size = pcms->sgx_epc.size;
  1499. dev = aml_device("EPC");
  1500. aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
  1501. aml_append(dev, aml_name_decl("_STR",
  1502. aml_unicode("Enclave Page Cache 1.0")));
  1503. crs = aml_resource_template();
  1504. aml_append(crs,
  1505. aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
  1506. AML_MAX_FIXED, AML_NON_CACHEABLE,
  1507. AML_READ_WRITE, 0, epc_base,
  1508. epc_base + epc_size - 1, 0, epc_size));
  1509. aml_append(dev, aml_name_decl("_CRS", crs));
  1510. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  1511. aml_append(method, aml_return(aml_int(0x0f)));
  1512. aml_append(dev, method);
  1513. aml_append(sb_scope, dev);
  1514. }
  1515. aml_append(dsdt, sb_scope);
  1516. if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
  1517. bool has_pcnt;
  1518. Object *pci_host = acpi_get_i386_pci_host();
  1519. PCIBus *b = PCI_HOST_BRIDGE(pci_host)->bus;
  1520. scope = aml_scope("\\_SB.PCI0");
  1521. has_pcnt = build_append_notfication_callback(scope, b);
  1522. if (has_pcnt) {
  1523. aml_append(dsdt, scope);
  1524. }
  1525. scope = aml_scope("_GPE");
  1526. {
  1527. method = aml_method("_E01", 0, AML_NOTSERIALIZED);
  1528. if (has_pcnt) {
  1529. aml_append(method,
  1530. aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
  1531. aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
  1532. aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
  1533. }
  1534. aml_append(scope, method);
  1535. }
  1536. aml_append(dsdt, scope);
  1537. }
  1538. /* copy AML table into ACPI tables blob and patch header there */
  1539. g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
  1540. acpi_table_end(linker, &table);
  1541. free_aml_allocator();
  1542. }
  1543. /*
  1544. * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
  1545. * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
  1546. */
  1547. static void
  1548. build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1549. const char *oem_table_id)
  1550. {
  1551. AcpiTable table = { .sig = "HPET", .rev = 1,
  1552. .oem_id = oem_id, .oem_table_id = oem_table_id };
  1553. acpi_table_begin(&table, table_data);
  1554. /* Note timer_block_id value must be kept in sync with value advertised by
  1555. * emulated hpet
  1556. */
  1557. /* Event Timer Block ID */
  1558. build_append_int_noprefix(table_data, 0x8086a201, 4);
  1559. /* BASE_ADDRESS */
  1560. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
  1561. /* HPET Number */
  1562. build_append_int_noprefix(table_data, 0, 1);
  1563. /* Main Counter Minimum Clock_tick in Periodic Mode */
  1564. build_append_int_noprefix(table_data, 0, 2);
  1565. /* Page Protection And OEM Attribute */
  1566. build_append_int_noprefix(table_data, 0, 1);
  1567. acpi_table_end(linker, &table);
  1568. }
  1569. #ifdef CONFIG_TPM
  1570. /*
  1571. * TCPA Description Table
  1572. *
  1573. * Following Level 00, Rev 00.37 of specs:
  1574. * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
  1575. * 7.1.2 ACPI Table Layout
  1576. */
  1577. static void
  1578. build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
  1579. const char *oem_id, const char *oem_table_id)
  1580. {
  1581. unsigned log_addr_offset;
  1582. AcpiTable table = { .sig = "TCPA", .rev = 2,
  1583. .oem_id = oem_id, .oem_table_id = oem_table_id };
  1584. acpi_table_begin(&table, table_data);
  1585. /* Platform Class */
  1586. build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
  1587. /* Log Area Minimum Length (LAML) */
  1588. build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
  1589. /* Log Area Start Address (LASA) */
  1590. log_addr_offset = table_data->len;
  1591. build_append_int_noprefix(table_data, 0, 8);
  1592. /* allocate/reserve space for TPM log area */
  1593. acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
  1594. bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
  1595. false /* high memory */);
  1596. /* log area start address to be filled by Guest linker */
  1597. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  1598. log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
  1599. acpi_table_end(linker, &table);
  1600. }
  1601. #endif
  1602. #define HOLE_640K_START (640 * KiB)
  1603. #define HOLE_640K_END (1 * MiB)
  1604. /*
  1605. * ACPI spec, Revision 3.0
  1606. * 5.2.15 System Resource Affinity Table (SRAT)
  1607. */
  1608. static void
  1609. build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
  1610. {
  1611. int i;
  1612. int numa_mem_start, slots;
  1613. uint64_t mem_len, mem_base, next_base;
  1614. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1615. X86MachineState *x86ms = X86_MACHINE(machine);
  1616. const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
  1617. int nb_numa_nodes = machine->numa_state->num_nodes;
  1618. NodeInfo *numa_info = machine->numa_state->nodes;
  1619. AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
  1620. .oem_table_id = x86ms->oem_table_id };
  1621. acpi_table_begin(&table, table_data);
  1622. build_append_int_noprefix(table_data, 1, 4); /* Reserved */
  1623. build_append_int_noprefix(table_data, 0, 8); /* Reserved */
  1624. for (i = 0; i < apic_ids->len; i++) {
  1625. int node_id = apic_ids->cpus[i].props.node_id;
  1626. uint32_t apic_id = apic_ids->cpus[i].arch_id;
  1627. if (apic_id < 255) {
  1628. /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
  1629. build_append_int_noprefix(table_data, 0, 1); /* Type */
  1630. build_append_int_noprefix(table_data, 16, 1); /* Length */
  1631. /* Proximity Domain [7:0] */
  1632. build_append_int_noprefix(table_data, node_id, 1);
  1633. build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
  1634. /* Flags, Table 5-36 */
  1635. build_append_int_noprefix(table_data, 1, 4);
  1636. build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
  1637. /* Proximity Domain [31:8] */
  1638. build_append_int_noprefix(table_data, 0, 3);
  1639. build_append_int_noprefix(table_data, 0, 4); /* Reserved */
  1640. } else {
  1641. /*
  1642. * ACPI spec, Revision 4.0
  1643. * 5.2.16.3 Processor Local x2APIC Affinity Structure
  1644. */
  1645. build_append_int_noprefix(table_data, 2, 1); /* Type */
  1646. build_append_int_noprefix(table_data, 24, 1); /* Length */
  1647. build_append_int_noprefix(table_data, 0, 2); /* Reserved */
  1648. /* Proximity Domain */
  1649. build_append_int_noprefix(table_data, node_id, 4);
  1650. build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
  1651. /* Flags, Table 5-39 */
  1652. build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
  1653. build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
  1654. build_append_int_noprefix(table_data, 0, 4); /* Reserved */
  1655. }
  1656. }
  1657. /* the memory map is a bit tricky, it contains at least one hole
  1658. * from 640k-1M and possibly another one from 3.5G-4G.
  1659. */
  1660. next_base = 0;
  1661. numa_mem_start = table_data->len;
  1662. for (i = 1; i < nb_numa_nodes + 1; ++i) {
  1663. mem_base = next_base;
  1664. mem_len = numa_info[i - 1].node_mem;
  1665. next_base = mem_base + mem_len;
  1666. /* Cut out the 640K hole */
  1667. if (mem_base <= HOLE_640K_START &&
  1668. next_base > HOLE_640K_START) {
  1669. mem_len -= next_base - HOLE_640K_START;
  1670. if (mem_len > 0) {
  1671. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1672. MEM_AFFINITY_ENABLED);
  1673. }
  1674. /* Check for the rare case: 640K < RAM < 1M */
  1675. if (next_base <= HOLE_640K_END) {
  1676. next_base = HOLE_640K_END;
  1677. continue;
  1678. }
  1679. mem_base = HOLE_640K_END;
  1680. mem_len = next_base - HOLE_640K_END;
  1681. }
  1682. /* Cut out the ACPI_PCI hole */
  1683. if (mem_base <= x86ms->below_4g_mem_size &&
  1684. next_base > x86ms->below_4g_mem_size) {
  1685. mem_len -= next_base - x86ms->below_4g_mem_size;
  1686. if (mem_len > 0) {
  1687. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1688. MEM_AFFINITY_ENABLED);
  1689. }
  1690. mem_base = x86ms->above_4g_mem_start;
  1691. mem_len = next_base - x86ms->below_4g_mem_size;
  1692. next_base = mem_base + mem_len;
  1693. }
  1694. if (mem_len > 0) {
  1695. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1696. MEM_AFFINITY_ENABLED);
  1697. }
  1698. }
  1699. if (machine->nvdimms_state->is_enabled) {
  1700. nvdimm_build_srat(table_data);
  1701. }
  1702. sgx_epc_build_srat(table_data);
  1703. /*
  1704. * TODO: this part is not in ACPI spec and current linux kernel boots fine
  1705. * without these entries. But I recall there were issues the last time I
  1706. * tried to remove it with some ancient guest OS, however I can't remember
  1707. * what that was so keep this around for now
  1708. */
  1709. slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
  1710. for (; slots < nb_numa_nodes + 2; slots++) {
  1711. build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
  1712. }
  1713. build_srat_generic_affinity_structures(table_data);
  1714. /*
  1715. * Entry is required for Windows to enable memory hotplug in OS
  1716. * and for Linux to enable SWIOTLB when booted with less than
  1717. * 4G of RAM. Windows works better if the entry sets proximity
  1718. * to the highest NUMA node in the machine.
  1719. * Memory devices may override proximity set by this entry,
  1720. * providing _PXM method if necessary.
  1721. */
  1722. if (machine->device_memory) {
  1723. build_srat_memory(table_data, machine->device_memory->base,
  1724. memory_region_size(&machine->device_memory->mr),
  1725. nb_numa_nodes - 1,
  1726. MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
  1727. }
  1728. acpi_table_end(linker, &table);
  1729. }
  1730. /*
  1731. * Insert DMAR scope for PCI bridges and endpoint devices
  1732. */
  1733. static void
  1734. insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
  1735. {
  1736. const size_t device_scope_size = 6 /* device scope structure */ +
  1737. 2 /* 1 path entry */;
  1738. GArray *scope_blob = opaque;
  1739. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  1740. /* Dmar Scope Type: 0x02 for PCI Bridge */
  1741. build_append_int_noprefix(scope_blob, 0x02, 1);
  1742. } else {
  1743. /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
  1744. build_append_int_noprefix(scope_blob, 0x01, 1);
  1745. }
  1746. /* length */
  1747. build_append_int_noprefix(scope_blob, device_scope_size, 1);
  1748. /* reserved */
  1749. build_append_int_noprefix(scope_blob, 0, 2);
  1750. /* enumeration_id */
  1751. build_append_int_noprefix(scope_blob, 0, 1);
  1752. /* bus */
  1753. build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
  1754. /* device */
  1755. build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
  1756. /* function */
  1757. build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
  1758. }
  1759. /* For a given PCI host bridge, walk and insert DMAR scope */
  1760. static int
  1761. dmar_host_bridges(Object *obj, void *opaque)
  1762. {
  1763. GArray *scope_blob = opaque;
  1764. if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
  1765. PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
  1766. if (bus && !pci_bus_bypass_iommu(bus)) {
  1767. pci_for_each_device_under_bus(bus, insert_scope, scope_blob);
  1768. }
  1769. }
  1770. return 0;
  1771. }
  1772. /*
  1773. * Intel ® Virtualization Technology for Directed I/O
  1774. * Architecture Specification. Revision 3.3
  1775. * 8.1 DMA Remapping Reporting Structure
  1776. */
  1777. static void
  1778. build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1779. const char *oem_table_id)
  1780. {
  1781. uint8_t dmar_flags = 0;
  1782. uint8_t rsvd10[10] = {};
  1783. /* Root complex IOAPIC uses one path only */
  1784. const size_t ioapic_scope_size = 6 /* device scope structure */ +
  1785. 2 /* 1 path entry */;
  1786. X86IOMMUState *iommu = x86_iommu_get_default();
  1787. IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
  1788. GArray *scope_blob = g_array_new(false, true, 1);
  1789. AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id,
  1790. .oem_table_id = oem_table_id };
  1791. /*
  1792. * A PCI bus walk, for each PCI host bridge.
  1793. * Insert scope for each PCI bridge and endpoint device which
  1794. * is attached to a bus with iommu enabled.
  1795. */
  1796. object_child_foreach_recursive(object_get_root(),
  1797. dmar_host_bridges, scope_blob);
  1798. assert(iommu);
  1799. if (x86_iommu_ir_supported(iommu)) {
  1800. dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
  1801. }
  1802. acpi_table_begin(&table, table_data);
  1803. /* Host Address Width */
  1804. build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1);
  1805. build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */
  1806. g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */
  1807. /* 8.3 DMAR Remapping Hardware Unit Definition structure */
  1808. build_append_int_noprefix(table_data, 0, 2); /* Type */
  1809. /* Length */
  1810. build_append_int_noprefix(table_data,
  1811. 16 + ioapic_scope_size + scope_blob->len, 2);
  1812. /* Flags */
  1813. build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ ,
  1814. 1);
  1815. build_append_int_noprefix(table_data, 0 , 1); /* Reserved */
  1816. build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */
  1817. /* Register Base Address */
  1818. build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8);
  1819. /* Scope definition for the root-complex IOAPIC. See VT-d spec
  1820. * 8.3.1 (version Oct. 2014 or later). */
  1821. build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */
  1822. build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */
  1823. build_append_int_noprefix(table_data, 0, 2); /* Reserved */
  1824. /* Enumeration ID */
  1825. build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1);
  1826. /* Start Bus Number */
  1827. build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1);
  1828. /* Path, {Device, Function} pair */
  1829. build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1);
  1830. build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1);
  1831. /* Add scope found above */
  1832. g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
  1833. g_array_free(scope_blob, true);
  1834. if (iommu->dt_supported) {
  1835. /* 8.5 Root Port ATS Capability Reporting Structure */
  1836. build_append_int_noprefix(table_data, 2, 2); /* Type */
  1837. build_append_int_noprefix(table_data, 8, 2); /* Length */
  1838. build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */
  1839. build_append_int_noprefix(table_data, 0, 1); /* Reserved */
  1840. build_append_int_noprefix(table_data, 0, 2); /* Segment Number */
  1841. }
  1842. acpi_table_end(linker, &table);
  1843. }
  1844. /*
  1845. * Windows ACPI Emulated Devices Table
  1846. * (Version 1.0 - April 6, 2009)
  1847. * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
  1848. *
  1849. * Helpful to speedup Windows guests and ignored by others.
  1850. */
  1851. static void
  1852. build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1853. const char *oem_table_id)
  1854. {
  1855. AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id,
  1856. .oem_table_id = oem_table_id };
  1857. acpi_table_begin(&table, table_data);
  1858. /*
  1859. * Set "ACPI PM timer good" flag.
  1860. *
  1861. * Tells Windows guests that our ACPI PM timer is reliable in the
  1862. * sense that guest can read it only once to obtain a reliable value.
  1863. * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
  1864. */
  1865. build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
  1866. acpi_table_end(linker, &table);
  1867. }
  1868. /*
  1869. * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
  1870. * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
  1871. */
  1872. #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
  1873. /*
  1874. * Insert IVHD entry for device and recurse, insert alias, or insert range as
  1875. * necessary for the PCI topology.
  1876. */
  1877. static void
  1878. insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
  1879. {
  1880. GArray *table_data = opaque;
  1881. uint32_t entry;
  1882. /* "Select" IVHD entry, type 0x2 */
  1883. entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
  1884. build_append_int_noprefix(table_data, entry, 4);
  1885. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  1886. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
  1887. uint8_t sec = pci_bus_num(sec_bus);
  1888. uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
  1889. if (pci_bus_is_express(sec_bus)) {
  1890. /*
  1891. * Walk the bus if there are subordinates, otherwise use a range
  1892. * to cover an entire leaf bus. We could potentially also use a
  1893. * range for traversed buses, but we'd need to take care not to
  1894. * create both Select and Range entries covering the same device.
  1895. * This is easier and potentially more compact.
  1896. *
  1897. * An example bare metal system seems to use Select entries for
  1898. * root ports without a slot (ie. built-ins) and Range entries
  1899. * when there is a slot. The same system also only hard-codes
  1900. * the alias range for an onboard PCIe-to-PCI bridge, apparently
  1901. * making no effort to support nested bridges. We attempt to
  1902. * be more thorough here.
  1903. */
  1904. if (sec == sub) { /* leaf bus */
  1905. /* "Start of Range" IVHD entry, type 0x3 */
  1906. entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
  1907. build_append_int_noprefix(table_data, entry, 4);
  1908. /* "End of Range" IVHD entry, type 0x4 */
  1909. entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
  1910. build_append_int_noprefix(table_data, entry, 4);
  1911. } else {
  1912. pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
  1913. }
  1914. } else {
  1915. /*
  1916. * If the secondary bus is conventional, then we need to create an
  1917. * Alias range for everything downstream. The range covers the
  1918. * first devfn on the secondary bus to the last devfn on the
  1919. * subordinate bus. The alias target depends on legacy versus
  1920. * express bridges, just as in pci_device_iommu_address_space().
  1921. * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
  1922. */
  1923. uint16_t dev_id_a, dev_id_b;
  1924. dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
  1925. if (pci_is_express(dev) &&
  1926. pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
  1927. dev_id_b = dev_id_a;
  1928. } else {
  1929. dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
  1930. }
  1931. /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
  1932. build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
  1933. build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
  1934. /* "End of Range" IVHD entry, type 0x4 */
  1935. entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
  1936. build_append_int_noprefix(table_data, entry, 4);
  1937. }
  1938. }
  1939. }
  1940. /* For all PCI host bridges, walk and insert IVHD entries */
  1941. static int
  1942. ivrs_host_bridges(Object *obj, void *opaque)
  1943. {
  1944. GArray *ivhd_blob = opaque;
  1945. if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
  1946. PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
  1947. if (bus && !pci_bus_bypass_iommu(bus)) {
  1948. pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob);
  1949. }
  1950. }
  1951. return 0;
  1952. }
  1953. static void
  1954. build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1955. const char *oem_table_id)
  1956. {
  1957. AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
  1958. GArray *ivhd_blob = g_array_new(false, true, 1);
  1959. AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
  1960. .oem_table_id = oem_table_id };
  1961. uint64_t feature_report;
  1962. acpi_table_begin(&table, table_data);
  1963. /* IVinfo - IO virtualization information common to all
  1964. * IOMMU units in a system
  1965. */
  1966. build_append_int_noprefix(table_data,
  1967. (1UL << 0) | /* EFRSup */
  1968. (40UL << 8), /* PASize */
  1969. 4);
  1970. /* reserved */
  1971. build_append_int_noprefix(table_data, 0, 8);
  1972. /*
  1973. * A PCI bus walk, for each PCI host bridge, is necessary to create a
  1974. * complete set of IVHD entries. Do this into a separate blob so that we
  1975. * can calculate the total IVRS table length here and then append the new
  1976. * blob further below. Fall back to an entry covering all devices, which
  1977. * is sufficient when no aliases are present.
  1978. */
  1979. object_child_foreach_recursive(object_get_root(),
  1980. ivrs_host_bridges, ivhd_blob);
  1981. if (!ivhd_blob->len) {
  1982. /*
  1983. * Type 1 device entry reporting all devices
  1984. * These are 4-byte device entries currently reporting the range of
  1985. * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
  1986. */
  1987. build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
  1988. }
  1989. /*
  1990. * When interrupt remapping is supported, we add a special IVHD device
  1991. * for type IO-APIC
  1992. * Refer to spec - Table 95: IVHD device entry type codes
  1993. *
  1994. * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
  1995. * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
  1996. */
  1997. if (x86_iommu_ir_supported(x86_iommu_get_default())) {
  1998. build_append_int_noprefix(ivhd_blob,
  1999. (0x1ull << 56) | /* type IOAPIC */
  2000. (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
  2001. 0x48, /* special device */
  2002. 8);
  2003. }
  2004. /* IVHD definition - type 10h */
  2005. build_append_int_noprefix(table_data, 0x10, 1);
  2006. /* virtualization flags */
  2007. build_append_int_noprefix(table_data,
  2008. (1UL << 0) | /* HtTunEn */
  2009. (1UL << 4) | /* iotblSup */
  2010. (1UL << 6) | /* PrefSup */
  2011. (1UL << 7), /* PPRSup */
  2012. 1);
  2013. /* IVHD length */
  2014. build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
  2015. /* DeviceID */
  2016. build_append_int_noprefix(table_data,
  2017. object_property_get_int(OBJECT(&s->pci), "addr",
  2018. &error_abort), 2);
  2019. /* Capability offset */
  2020. build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
  2021. /* IOMMU base address */
  2022. build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
  2023. /* PCI Segment Group */
  2024. build_append_int_noprefix(table_data, 0, 2);
  2025. /* IOMMU info */
  2026. build_append_int_noprefix(table_data, 0, 2);
  2027. /* IOMMU Feature Reporting */
  2028. feature_report = (48UL << 30) | /* HATS */
  2029. (48UL << 28) | /* GATS */
  2030. (1UL << 2) | /* GTSup */
  2031. (1UL << 6); /* GASup */
  2032. if (s->xtsup) {
  2033. feature_report |= (1UL << 0); /* XTSup */
  2034. }
  2035. build_append_int_noprefix(table_data, feature_report, 4);
  2036. /* IVHD entries as found above */
  2037. g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
  2038. /* IVHD definition - type 11h */
  2039. build_append_int_noprefix(table_data, 0x11, 1);
  2040. /* virtualization flags */
  2041. build_append_int_noprefix(table_data,
  2042. (1UL << 0) | /* HtTunEn */
  2043. (1UL << 4), /* iotblSup */
  2044. 1);
  2045. /* IVHD length */
  2046. build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
  2047. /* DeviceID */
  2048. build_append_int_noprefix(table_data,
  2049. object_property_get_int(OBJECT(&s->pci), "addr",
  2050. &error_abort), 2);
  2051. /* Capability offset */
  2052. build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
  2053. /* IOMMU base address */
  2054. build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
  2055. /* PCI Segment Group */
  2056. build_append_int_noprefix(table_data, 0, 2);
  2057. /* IOMMU info */
  2058. build_append_int_noprefix(table_data, 0, 2);
  2059. /* IOMMU Attributes */
  2060. build_append_int_noprefix(table_data, 0, 4);
  2061. /* EFR Register Image */
  2062. build_append_int_noprefix(table_data,
  2063. amdvi_extended_feature_register(s),
  2064. 8);
  2065. /* EFR Register Image 2 */
  2066. build_append_int_noprefix(table_data, 0, 8);
  2067. /* IVHD entries as found above */
  2068. g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
  2069. g_array_free(ivhd_blob, TRUE);
  2070. acpi_table_end(linker, &table);
  2071. }
  2072. typedef
  2073. struct AcpiBuildState {
  2074. /* Copy of table in RAM (for patching). */
  2075. MemoryRegion *table_mr;
  2076. /* Is table patched? */
  2077. uint8_t patched;
  2078. MemoryRegion *rsdp_mr;
  2079. MemoryRegion *linker_mr;
  2080. } AcpiBuildState;
  2081. static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
  2082. {
  2083. Object *pci_host;
  2084. QObject *o;
  2085. pci_host = acpi_get_i386_pci_host();
  2086. if (!pci_host) {
  2087. return false;
  2088. }
  2089. o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
  2090. if (!o) {
  2091. return false;
  2092. }
  2093. mcfg->base = qnum_get_uint(qobject_to(QNum, o));
  2094. qobject_unref(o);
  2095. if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
  2096. return false;
  2097. }
  2098. o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
  2099. assert(o);
  2100. mcfg->size = qnum_get_uint(qobject_to(QNum, o));
  2101. qobject_unref(o);
  2102. return true;
  2103. }
  2104. static
  2105. void acpi_build(AcpiBuildTables *tables, MachineState *machine)
  2106. {
  2107. PCMachineState *pcms = PC_MACHINE(machine);
  2108. X86MachineState *x86ms = X86_MACHINE(machine);
  2109. DeviceState *iommu = pcms->iommu;
  2110. GArray *table_offsets;
  2111. unsigned facs, dsdt, rsdt;
  2112. AcpiPmInfo pm;
  2113. AcpiMiscInfo misc;
  2114. AcpiMcfgInfo mcfg;
  2115. Range pci_hole = {}, pci_hole64 = {};
  2116. uint8_t *u;
  2117. GArray *tables_blob = tables->table_data;
  2118. AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
  2119. Object *vmgenid_dev, *vmclock_dev;
  2120. char *oem_id;
  2121. char *oem_table_id;
  2122. acpi_get_pm_info(machine, &pm);
  2123. acpi_get_misc_info(&misc);
  2124. acpi_get_pci_holes(&pci_hole, &pci_hole64);
  2125. acpi_get_slic_oem(&slic_oem);
  2126. if (slic_oem.id) {
  2127. oem_id = slic_oem.id;
  2128. } else {
  2129. oem_id = x86ms->oem_id;
  2130. }
  2131. if (slic_oem.table_id) {
  2132. oem_table_id = slic_oem.table_id;
  2133. } else {
  2134. oem_table_id = x86ms->oem_table_id;
  2135. }
  2136. table_offsets = g_array_new(false, true /* clear */,
  2137. sizeof(uint32_t));
  2138. ACPI_BUILD_DPRINTF("init ACPI tables\n");
  2139. bios_linker_loader_alloc(tables->linker,
  2140. ACPI_BUILD_TABLE_FILE, tables_blob,
  2141. 64 /* Ensure FACS is aligned */,
  2142. false /* high memory */);
  2143. /*
  2144. * FACS is pointed to by FADT.
  2145. * We place it first since it's the only table that has alignment
  2146. * requirements.
  2147. */
  2148. facs = tables_blob->len;
  2149. build_facs(tables_blob);
  2150. /* DSDT is pointed to by FADT */
  2151. dsdt = tables_blob->len;
  2152. build_dsdt(tables_blob, tables->linker, &pm, &misc,
  2153. &pci_hole, &pci_hole64, machine);
  2154. /* ACPI tables pointed to by RSDT */
  2155. acpi_add_table(table_offsets, tables_blob);
  2156. pm.fadt.facs_tbl_offset = &facs;
  2157. pm.fadt.dsdt_tbl_offset = &dsdt;
  2158. pm.fadt.xdsdt_tbl_offset = &dsdt;
  2159. build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
  2160. acpi_add_table(table_offsets, tables_blob);
  2161. acpi_build_madt(tables_blob, tables->linker, x86ms,
  2162. x86ms->oem_id, x86ms->oem_table_id);
  2163. #ifdef CONFIG_ACPI_ERST
  2164. {
  2165. Object *erst_dev;
  2166. erst_dev = find_erst_dev();
  2167. if (erst_dev) {
  2168. acpi_add_table(table_offsets, tables_blob);
  2169. build_erst(tables_blob, tables->linker, erst_dev,
  2170. x86ms->oem_id, x86ms->oem_table_id);
  2171. }
  2172. }
  2173. #endif
  2174. vmgenid_dev = find_vmgenid_dev();
  2175. if (vmgenid_dev) {
  2176. acpi_add_table(table_offsets, tables_blob);
  2177. vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
  2178. tables->vmgenid, tables->linker, x86ms->oem_id);
  2179. }
  2180. vmclock_dev = find_vmclock_dev();
  2181. if (vmclock_dev) {
  2182. acpi_add_table(table_offsets, tables_blob);
  2183. vmclock_build_acpi(VMCLOCK(vmclock_dev), tables_blob, tables->linker,
  2184. x86ms->oem_id);
  2185. }
  2186. if (misc.has_hpet) {
  2187. acpi_add_table(table_offsets, tables_blob);
  2188. build_hpet(tables_blob, tables->linker, x86ms->oem_id,
  2189. x86ms->oem_table_id);
  2190. }
  2191. #ifdef CONFIG_TPM
  2192. if (misc.tpm_version != TPM_VERSION_UNSPEC) {
  2193. if (misc.tpm_version == TPM_VERSION_1_2) {
  2194. acpi_add_table(table_offsets, tables_blob);
  2195. build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
  2196. x86ms->oem_id, x86ms->oem_table_id);
  2197. } else { /* TPM_VERSION_2_0 */
  2198. acpi_add_table(table_offsets, tables_blob);
  2199. build_tpm2(tables_blob, tables->linker, tables->tcpalog,
  2200. x86ms->oem_id, x86ms->oem_table_id);
  2201. }
  2202. }
  2203. #endif
  2204. if (machine->numa_state->num_nodes) {
  2205. acpi_add_table(table_offsets, tables_blob);
  2206. build_srat(tables_blob, tables->linker, machine);
  2207. if (machine->numa_state->have_numa_distance) {
  2208. acpi_add_table(table_offsets, tables_blob);
  2209. build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
  2210. x86ms->oem_table_id);
  2211. }
  2212. if (machine->numa_state->hmat_enabled) {
  2213. acpi_add_table(table_offsets, tables_blob);
  2214. build_hmat(tables_blob, tables->linker, machine->numa_state,
  2215. x86ms->oem_id, x86ms->oem_table_id);
  2216. }
  2217. }
  2218. if (acpi_get_mcfg(&mcfg)) {
  2219. acpi_add_table(table_offsets, tables_blob);
  2220. build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
  2221. x86ms->oem_table_id);
  2222. }
  2223. if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
  2224. acpi_add_table(table_offsets, tables_blob);
  2225. build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
  2226. x86ms->oem_table_id);
  2227. } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) {
  2228. acpi_add_table(table_offsets, tables_blob);
  2229. build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
  2230. x86ms->oem_table_id);
  2231. } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) {
  2232. PCIDevice *pdev = PCI_DEVICE(iommu);
  2233. acpi_add_table(table_offsets, tables_blob);
  2234. build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev),
  2235. x86ms->oem_id, x86ms->oem_table_id);
  2236. }
  2237. if (machine->nvdimms_state->is_enabled) {
  2238. nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
  2239. machine->nvdimms_state, machine->ram_slots,
  2240. x86ms->oem_id, x86ms->oem_table_id);
  2241. }
  2242. if (pcms->cxl_devices_state.is_enabled) {
  2243. cxl_build_cedt(table_offsets, tables_blob, tables->linker,
  2244. x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state);
  2245. }
  2246. acpi_add_table(table_offsets, tables_blob);
  2247. build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
  2248. /* Add tables supplied by user (if any) */
  2249. for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
  2250. unsigned len = acpi_table_len(u);
  2251. acpi_add_table(table_offsets, tables_blob);
  2252. g_array_append_vals(tables_blob, u, len);
  2253. }
  2254. /* RSDT is pointed to by RSDP */
  2255. rsdt = tables_blob->len;
  2256. build_rsdt(tables_blob, tables->linker, table_offsets,
  2257. oem_id, oem_table_id);
  2258. /* RSDP is in FSEG memory, so allocate it separately */
  2259. {
  2260. AcpiRsdpData rsdp_data = {
  2261. .revision = 0,
  2262. .oem_id = x86ms->oem_id,
  2263. .xsdt_tbl_offset = NULL,
  2264. .rsdt_tbl_offset = &rsdt,
  2265. };
  2266. build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
  2267. }
  2268. /* We'll expose it all to Guest so we want to reduce
  2269. * chance of size changes.
  2270. *
  2271. * We used to align the tables to 4k, but of course this would
  2272. * too simple to be enough. 4k turned out to be too small an
  2273. * alignment very soon, and in fact it is almost impossible to
  2274. * keep the table size stable for all (max_cpus, max_memory_slots)
  2275. * combinations.
  2276. */
  2277. acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
  2278. acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
  2279. /* Cleanup memory that's no longer used. */
  2280. g_array_free(table_offsets, true);
  2281. g_free(slic_oem.id);
  2282. g_free(slic_oem.table_id);
  2283. }
  2284. static void acpi_ram_update(MemoryRegion *mr, GArray *data)
  2285. {
  2286. uint32_t size = acpi_data_len(data);
  2287. /* Make sure RAM size is correct - in case it got changed e.g. by migration */
  2288. memory_region_ram_resize(mr, size, &error_abort);
  2289. memcpy(memory_region_get_ram_ptr(mr), data->data, size);
  2290. memory_region_set_dirty(mr, 0, size);
  2291. }
  2292. static void acpi_build_update(void *build_opaque)
  2293. {
  2294. AcpiBuildState *build_state = build_opaque;
  2295. AcpiBuildTables tables;
  2296. /* No state to update or already patched? Nothing to do. */
  2297. if (!build_state || build_state->patched) {
  2298. return;
  2299. }
  2300. build_state->patched = 1;
  2301. acpi_build_tables_init(&tables);
  2302. acpi_build(&tables, MACHINE(qdev_get_machine()));
  2303. acpi_ram_update(build_state->table_mr, tables.table_data);
  2304. acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
  2305. acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
  2306. acpi_build_tables_cleanup(&tables, true);
  2307. }
  2308. static void acpi_build_reset(void *build_opaque)
  2309. {
  2310. AcpiBuildState *build_state = build_opaque;
  2311. build_state->patched = 0;
  2312. }
  2313. static const VMStateDescription vmstate_acpi_build = {
  2314. .name = "acpi_build",
  2315. .version_id = 1,
  2316. .minimum_version_id = 1,
  2317. .fields = (const VMStateField[]) {
  2318. VMSTATE_UINT8(patched, AcpiBuildState),
  2319. VMSTATE_END_OF_LIST()
  2320. },
  2321. };
  2322. void acpi_setup(void)
  2323. {
  2324. PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
  2325. X86MachineState *x86ms = X86_MACHINE(pcms);
  2326. AcpiBuildTables tables;
  2327. AcpiBuildState *build_state;
  2328. Object *vmgenid_dev;
  2329. #ifdef CONFIG_TPM
  2330. TPMIf *tpm;
  2331. static FwCfgTPMConfig tpm_config;
  2332. #endif
  2333. if (!x86ms->fw_cfg) {
  2334. ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
  2335. return;
  2336. }
  2337. if (!pcms->acpi_build_enabled) {
  2338. ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
  2339. return;
  2340. }
  2341. if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
  2342. ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
  2343. return;
  2344. }
  2345. build_state = g_malloc0(sizeof *build_state);
  2346. acpi_build_tables_init(&tables);
  2347. acpi_build(&tables, MACHINE(pcms));
  2348. /* Now expose it all to Guest */
  2349. build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
  2350. build_state, tables.table_data,
  2351. ACPI_BUILD_TABLE_FILE);
  2352. assert(build_state->table_mr != NULL);
  2353. build_state->linker_mr =
  2354. acpi_add_rom_blob(acpi_build_update, build_state,
  2355. tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
  2356. #ifdef CONFIG_TPM
  2357. fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
  2358. tables.tcpalog->data, acpi_data_len(tables.tcpalog));
  2359. tpm = tpm_find();
  2360. if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
  2361. tpm_config = (FwCfgTPMConfig) {
  2362. .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
  2363. .tpm_version = tpm_get_version(tpm),
  2364. .tpmppi_version = TPM_PPI_VERSION_1_30
  2365. };
  2366. fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
  2367. &tpm_config, sizeof tpm_config);
  2368. }
  2369. #endif
  2370. vmgenid_dev = find_vmgenid_dev();
  2371. if (vmgenid_dev) {
  2372. vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
  2373. tables.vmgenid);
  2374. }
  2375. build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
  2376. build_state, tables.rsdp,
  2377. ACPI_BUILD_RSDP_FILE);
  2378. qemu_register_reset(acpi_build_reset, build_state);
  2379. acpi_build_reset(build_state);
  2380. vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
  2381. /* Cleanup tables but don't free the memory: we track it
  2382. * in build_state.
  2383. */
  2384. acpi_build_tables_cleanup(&tables, false);
  2385. }