acpi-build.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  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. dev = aml_device("TPM");
  1494. aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
  1495. aml_append(dev, aml_name_decl("_STR",
  1496. aml_string("TPM 2.0 Device")));
  1497. crs = aml_resource_template();
  1498. aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
  1499. TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
  1500. aml_append(dev, aml_name_decl("_CRS", crs));
  1501. aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
  1502. aml_append(dev, aml_name_decl("_UID", aml_int(1)));
  1503. tpm_build_ppi_acpi(tpm, dev);
  1504. aml_append(sb_scope, dev);
  1505. }
  1506. #endif
  1507. if (pcms->sgx_epc.size != 0) {
  1508. uint64_t epc_base = pcms->sgx_epc.base;
  1509. uint64_t epc_size = pcms->sgx_epc.size;
  1510. dev = aml_device("EPC");
  1511. aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
  1512. aml_append(dev, aml_name_decl("_STR",
  1513. aml_unicode("Enclave Page Cache 1.0")));
  1514. crs = aml_resource_template();
  1515. aml_append(crs,
  1516. aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
  1517. AML_MAX_FIXED, AML_NON_CACHEABLE,
  1518. AML_READ_WRITE, 0, epc_base,
  1519. epc_base + epc_size - 1, 0, epc_size));
  1520. aml_append(dev, aml_name_decl("_CRS", crs));
  1521. method = aml_method("_STA", 0, AML_NOTSERIALIZED);
  1522. aml_append(method, aml_return(aml_int(0x0f)));
  1523. aml_append(dev, method);
  1524. aml_append(sb_scope, dev);
  1525. }
  1526. aml_append(dsdt, sb_scope);
  1527. if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
  1528. bool has_pcnt;
  1529. Object *pci_host = acpi_get_i386_pci_host();
  1530. PCIBus *b = PCI_HOST_BRIDGE(pci_host)->bus;
  1531. scope = aml_scope("\\_SB.PCI0");
  1532. has_pcnt = build_append_notfication_callback(scope, b);
  1533. if (has_pcnt) {
  1534. aml_append(dsdt, scope);
  1535. }
  1536. scope = aml_scope("_GPE");
  1537. {
  1538. method = aml_method("_E01", 0, AML_NOTSERIALIZED);
  1539. if (has_pcnt) {
  1540. aml_append(method,
  1541. aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
  1542. aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
  1543. aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
  1544. }
  1545. aml_append(scope, method);
  1546. }
  1547. aml_append(dsdt, scope);
  1548. }
  1549. /* copy AML table into ACPI tables blob and patch header there */
  1550. g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
  1551. acpi_table_end(linker, &table);
  1552. free_aml_allocator();
  1553. }
  1554. /*
  1555. * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
  1556. * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
  1557. */
  1558. static void
  1559. build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1560. const char *oem_table_id)
  1561. {
  1562. AcpiTable table = { .sig = "HPET", .rev = 1,
  1563. .oem_id = oem_id, .oem_table_id = oem_table_id };
  1564. acpi_table_begin(&table, table_data);
  1565. /* Note timer_block_id value must be kept in sync with value advertised by
  1566. * emulated hpet
  1567. */
  1568. /* Event Timer Block ID */
  1569. build_append_int_noprefix(table_data, 0x8086a201, 4);
  1570. /* BASE_ADDRESS */
  1571. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
  1572. /* HPET Number */
  1573. build_append_int_noprefix(table_data, 0, 1);
  1574. /* Main Counter Minimum Clock_tick in Periodic Mode */
  1575. build_append_int_noprefix(table_data, 0, 2);
  1576. /* Page Protection And OEM Attribute */
  1577. build_append_int_noprefix(table_data, 0, 1);
  1578. acpi_table_end(linker, &table);
  1579. }
  1580. #ifdef CONFIG_TPM
  1581. /*
  1582. * TCPA Description Table
  1583. *
  1584. * Following Level 00, Rev 00.37 of specs:
  1585. * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
  1586. * 7.1.2 ACPI Table Layout
  1587. */
  1588. static void
  1589. build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
  1590. const char *oem_id, const char *oem_table_id)
  1591. {
  1592. unsigned log_addr_offset;
  1593. AcpiTable table = { .sig = "TCPA", .rev = 2,
  1594. .oem_id = oem_id, .oem_table_id = oem_table_id };
  1595. acpi_table_begin(&table, table_data);
  1596. /* Platform Class */
  1597. build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
  1598. /* Log Area Minimum Length (LAML) */
  1599. build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
  1600. /* Log Area Start Address (LASA) */
  1601. log_addr_offset = table_data->len;
  1602. build_append_int_noprefix(table_data, 0, 8);
  1603. /* allocate/reserve space for TPM log area */
  1604. acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
  1605. bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
  1606. false /* high memory */);
  1607. /* log area start address to be filled by Guest linker */
  1608. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  1609. log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
  1610. acpi_table_end(linker, &table);
  1611. }
  1612. #endif
  1613. #define HOLE_640K_START (640 * KiB)
  1614. #define HOLE_640K_END (1 * MiB)
  1615. /*
  1616. * ACPI spec, Revision 3.0
  1617. * 5.2.15 System Resource Affinity Table (SRAT)
  1618. */
  1619. static void
  1620. build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
  1621. {
  1622. int i;
  1623. int numa_mem_start, slots;
  1624. uint64_t mem_len, mem_base, next_base;
  1625. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1626. X86MachineState *x86ms = X86_MACHINE(machine);
  1627. const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
  1628. int nb_numa_nodes = machine->numa_state->num_nodes;
  1629. NodeInfo *numa_info = machine->numa_state->nodes;
  1630. AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
  1631. .oem_table_id = x86ms->oem_table_id };
  1632. acpi_table_begin(&table, table_data);
  1633. build_append_int_noprefix(table_data, 1, 4); /* Reserved */
  1634. build_append_int_noprefix(table_data, 0, 8); /* Reserved */
  1635. for (i = 0; i < apic_ids->len; i++) {
  1636. int node_id = apic_ids->cpus[i].props.node_id;
  1637. uint32_t apic_id = apic_ids->cpus[i].arch_id;
  1638. if (apic_id < 255) {
  1639. /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
  1640. build_append_int_noprefix(table_data, 0, 1); /* Type */
  1641. build_append_int_noprefix(table_data, 16, 1); /* Length */
  1642. /* Proximity Domain [7:0] */
  1643. build_append_int_noprefix(table_data, node_id, 1);
  1644. build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
  1645. /* Flags, Table 5-36 */
  1646. build_append_int_noprefix(table_data, 1, 4);
  1647. build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
  1648. /* Proximity Domain [31:8] */
  1649. build_append_int_noprefix(table_data, 0, 3);
  1650. build_append_int_noprefix(table_data, 0, 4); /* Reserved */
  1651. } else {
  1652. /*
  1653. * ACPI spec, Revision 4.0
  1654. * 5.2.16.3 Processor Local x2APIC Affinity Structure
  1655. */
  1656. build_append_int_noprefix(table_data, 2, 1); /* Type */
  1657. build_append_int_noprefix(table_data, 24, 1); /* Length */
  1658. build_append_int_noprefix(table_data, 0, 2); /* Reserved */
  1659. /* Proximity Domain */
  1660. build_append_int_noprefix(table_data, node_id, 4);
  1661. build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
  1662. /* Flags, Table 5-39 */
  1663. build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
  1664. build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
  1665. build_append_int_noprefix(table_data, 0, 4); /* Reserved */
  1666. }
  1667. }
  1668. /* the memory map is a bit tricky, it contains at least one hole
  1669. * from 640k-1M and possibly another one from 3.5G-4G.
  1670. */
  1671. next_base = 0;
  1672. numa_mem_start = table_data->len;
  1673. for (i = 1; i < nb_numa_nodes + 1; ++i) {
  1674. mem_base = next_base;
  1675. mem_len = numa_info[i - 1].node_mem;
  1676. next_base = mem_base + mem_len;
  1677. /* Cut out the 640K hole */
  1678. if (mem_base <= HOLE_640K_START &&
  1679. next_base > HOLE_640K_START) {
  1680. mem_len -= next_base - HOLE_640K_START;
  1681. if (mem_len > 0) {
  1682. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1683. MEM_AFFINITY_ENABLED);
  1684. }
  1685. /* Check for the rare case: 640K < RAM < 1M */
  1686. if (next_base <= HOLE_640K_END) {
  1687. next_base = HOLE_640K_END;
  1688. continue;
  1689. }
  1690. mem_base = HOLE_640K_END;
  1691. mem_len = next_base - HOLE_640K_END;
  1692. }
  1693. /* Cut out the ACPI_PCI hole */
  1694. if (mem_base <= x86ms->below_4g_mem_size &&
  1695. next_base > x86ms->below_4g_mem_size) {
  1696. mem_len -= next_base - x86ms->below_4g_mem_size;
  1697. if (mem_len > 0) {
  1698. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1699. MEM_AFFINITY_ENABLED);
  1700. }
  1701. mem_base = x86ms->above_4g_mem_start;
  1702. mem_len = next_base - x86ms->below_4g_mem_size;
  1703. next_base = mem_base + mem_len;
  1704. }
  1705. if (mem_len > 0) {
  1706. build_srat_memory(table_data, mem_base, mem_len, i - 1,
  1707. MEM_AFFINITY_ENABLED);
  1708. }
  1709. }
  1710. if (machine->nvdimms_state->is_enabled) {
  1711. nvdimm_build_srat(table_data);
  1712. }
  1713. sgx_epc_build_srat(table_data);
  1714. /*
  1715. * TODO: this part is not in ACPI spec and current linux kernel boots fine
  1716. * without these entries. But I recall there were issues the last time I
  1717. * tried to remove it with some ancient guest OS, however I can't remember
  1718. * what that was so keep this around for now
  1719. */
  1720. slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
  1721. for (; slots < nb_numa_nodes + 2; slots++) {
  1722. build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
  1723. }
  1724. build_srat_generic_affinity_structures(table_data);
  1725. /*
  1726. * Entry is required for Windows to enable memory hotplug in OS
  1727. * and for Linux to enable SWIOTLB when booted with less than
  1728. * 4G of RAM. Windows works better if the entry sets proximity
  1729. * to the highest NUMA node in the machine.
  1730. * Memory devices may override proximity set by this entry,
  1731. * providing _PXM method if necessary.
  1732. */
  1733. if (machine->device_memory) {
  1734. build_srat_memory(table_data, machine->device_memory->base,
  1735. memory_region_size(&machine->device_memory->mr),
  1736. nb_numa_nodes - 1,
  1737. MEM_AFFINITY_HOTPLUGGABLE | MEM_AFFINITY_ENABLED);
  1738. }
  1739. acpi_table_end(linker, &table);
  1740. }
  1741. /*
  1742. * Insert DMAR scope for PCI bridges and endpoint devices
  1743. */
  1744. static void
  1745. insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
  1746. {
  1747. const size_t device_scope_size = 6 /* device scope structure */ +
  1748. 2 /* 1 path entry */;
  1749. GArray *scope_blob = opaque;
  1750. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  1751. /* Dmar Scope Type: 0x02 for PCI Bridge */
  1752. build_append_int_noprefix(scope_blob, 0x02, 1);
  1753. } else {
  1754. /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
  1755. build_append_int_noprefix(scope_blob, 0x01, 1);
  1756. }
  1757. /* length */
  1758. build_append_int_noprefix(scope_blob, device_scope_size, 1);
  1759. /* reserved */
  1760. build_append_int_noprefix(scope_blob, 0, 2);
  1761. /* enumeration_id */
  1762. build_append_int_noprefix(scope_blob, 0, 1);
  1763. /* bus */
  1764. build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
  1765. /* device */
  1766. build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
  1767. /* function */
  1768. build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
  1769. }
  1770. /* For a given PCI host bridge, walk and insert DMAR scope */
  1771. static int
  1772. dmar_host_bridges(Object *obj, void *opaque)
  1773. {
  1774. GArray *scope_blob = opaque;
  1775. if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
  1776. PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
  1777. if (bus && !pci_bus_bypass_iommu(bus)) {
  1778. pci_for_each_device_under_bus(bus, insert_scope, scope_blob);
  1779. }
  1780. }
  1781. return 0;
  1782. }
  1783. /*
  1784. * Intel ® Virtualization Technology for Directed I/O
  1785. * Architecture Specification. Revision 3.3
  1786. * 8.1 DMA Remapping Reporting Structure
  1787. */
  1788. static void
  1789. build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1790. const char *oem_table_id)
  1791. {
  1792. uint8_t dmar_flags = 0;
  1793. uint8_t rsvd10[10] = {};
  1794. /* Root complex IOAPIC uses one path only */
  1795. const size_t ioapic_scope_size = 6 /* device scope structure */ +
  1796. 2 /* 1 path entry */;
  1797. X86IOMMUState *iommu = x86_iommu_get_default();
  1798. IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
  1799. GArray *scope_blob = g_array_new(false, true, 1);
  1800. AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id,
  1801. .oem_table_id = oem_table_id };
  1802. /*
  1803. * A PCI bus walk, for each PCI host bridge.
  1804. * Insert scope for each PCI bridge and endpoint device which
  1805. * is attached to a bus with iommu enabled.
  1806. */
  1807. object_child_foreach_recursive(object_get_root(),
  1808. dmar_host_bridges, scope_blob);
  1809. assert(iommu);
  1810. if (x86_iommu_ir_supported(iommu)) {
  1811. dmar_flags |= 0x1; /* Flags: 0x1: INT_REMAP */
  1812. }
  1813. acpi_table_begin(&table, table_data);
  1814. /* Host Address Width */
  1815. build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1);
  1816. build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */
  1817. g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */
  1818. /* 8.3 DMAR Remapping Hardware Unit Definition structure */
  1819. build_append_int_noprefix(table_data, 0, 2); /* Type */
  1820. /* Length */
  1821. build_append_int_noprefix(table_data,
  1822. 16 + ioapic_scope_size + scope_blob->len, 2);
  1823. /* Flags */
  1824. build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ ,
  1825. 1);
  1826. build_append_int_noprefix(table_data, 0 , 1); /* Reserved */
  1827. build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */
  1828. /* Register Base Address */
  1829. build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8);
  1830. /* Scope definition for the root-complex IOAPIC. See VT-d spec
  1831. * 8.3.1 (version Oct. 2014 or later). */
  1832. build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */
  1833. build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */
  1834. build_append_int_noprefix(table_data, 0, 2); /* Reserved */
  1835. /* Enumeration ID */
  1836. build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1);
  1837. /* Start Bus Number */
  1838. build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1);
  1839. /* Path, {Device, Function} pair */
  1840. build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1);
  1841. build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1);
  1842. /* Add scope found above */
  1843. g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
  1844. g_array_free(scope_blob, true);
  1845. if (iommu->dt_supported) {
  1846. /* 8.5 Root Port ATS Capability Reporting Structure */
  1847. build_append_int_noprefix(table_data, 2, 2); /* Type */
  1848. build_append_int_noprefix(table_data, 8, 2); /* Length */
  1849. build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */
  1850. build_append_int_noprefix(table_data, 0, 1); /* Reserved */
  1851. build_append_int_noprefix(table_data, 0, 2); /* Segment Number */
  1852. }
  1853. acpi_table_end(linker, &table);
  1854. }
  1855. /*
  1856. * Windows ACPI Emulated Devices Table
  1857. * (Version 1.0 - April 6, 2009)
  1858. * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
  1859. *
  1860. * Helpful to speedup Windows guests and ignored by others.
  1861. */
  1862. static void
  1863. build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1864. const char *oem_table_id)
  1865. {
  1866. AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id,
  1867. .oem_table_id = oem_table_id };
  1868. acpi_table_begin(&table, table_data);
  1869. /*
  1870. * Set "ACPI PM timer good" flag.
  1871. *
  1872. * Tells Windows guests that our ACPI PM timer is reliable in the
  1873. * sense that guest can read it only once to obtain a reliable value.
  1874. * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
  1875. */
  1876. build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
  1877. acpi_table_end(linker, &table);
  1878. }
  1879. /*
  1880. * IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
  1881. * accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
  1882. */
  1883. #define IOAPIC_SB_DEVID (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
  1884. /*
  1885. * Insert IVHD entry for device and recurse, insert alias, or insert range as
  1886. * necessary for the PCI topology.
  1887. */
  1888. static void
  1889. insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
  1890. {
  1891. GArray *table_data = opaque;
  1892. uint32_t entry;
  1893. /* "Select" IVHD entry, type 0x2 */
  1894. entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
  1895. build_append_int_noprefix(table_data, entry, 4);
  1896. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  1897. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
  1898. uint8_t sec = pci_bus_num(sec_bus);
  1899. uint8_t sub = dev->config[PCI_SUBORDINATE_BUS];
  1900. if (pci_bus_is_express(sec_bus)) {
  1901. /*
  1902. * Walk the bus if there are subordinates, otherwise use a range
  1903. * to cover an entire leaf bus. We could potentially also use a
  1904. * range for traversed buses, but we'd need to take care not to
  1905. * create both Select and Range entries covering the same device.
  1906. * This is easier and potentially more compact.
  1907. *
  1908. * An example bare metal system seems to use Select entries for
  1909. * root ports without a slot (ie. built-ins) and Range entries
  1910. * when there is a slot. The same system also only hard-codes
  1911. * the alias range for an onboard PCIe-to-PCI bridge, apparently
  1912. * making no effort to support nested bridges. We attempt to
  1913. * be more thorough here.
  1914. */
  1915. if (sec == sub) { /* leaf bus */
  1916. /* "Start of Range" IVHD entry, type 0x3 */
  1917. entry = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0)) << 8 | 0x3;
  1918. build_append_int_noprefix(table_data, entry, 4);
  1919. /* "End of Range" IVHD entry, type 0x4 */
  1920. entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
  1921. build_append_int_noprefix(table_data, entry, 4);
  1922. } else {
  1923. pci_for_each_device(sec_bus, sec, insert_ivhd, table_data);
  1924. }
  1925. } else {
  1926. /*
  1927. * If the secondary bus is conventional, then we need to create an
  1928. * Alias range for everything downstream. The range covers the
  1929. * first devfn on the secondary bus to the last devfn on the
  1930. * subordinate bus. The alias target depends on legacy versus
  1931. * express bridges, just as in pci_device_iommu_address_space().
  1932. * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
  1933. */
  1934. uint16_t dev_id_a, dev_id_b;
  1935. dev_id_a = PCI_BUILD_BDF(sec, PCI_DEVFN(0, 0));
  1936. if (pci_is_express(dev) &&
  1937. pcie_cap_get_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
  1938. dev_id_b = dev_id_a;
  1939. } else {
  1940. dev_id_b = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn);
  1941. }
  1942. /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
  1943. build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
  1944. build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
  1945. /* "End of Range" IVHD entry, type 0x4 */
  1946. entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
  1947. build_append_int_noprefix(table_data, entry, 4);
  1948. }
  1949. }
  1950. }
  1951. /* For all PCI host bridges, walk and insert IVHD entries */
  1952. static int
  1953. ivrs_host_bridges(Object *obj, void *opaque)
  1954. {
  1955. GArray *ivhd_blob = opaque;
  1956. if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
  1957. PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
  1958. if (bus && !pci_bus_bypass_iommu(bus)) {
  1959. pci_for_each_device_under_bus(bus, insert_ivhd, ivhd_blob);
  1960. }
  1961. }
  1962. return 0;
  1963. }
  1964. static void
  1965. build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
  1966. const char *oem_table_id)
  1967. {
  1968. AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
  1969. GArray *ivhd_blob = g_array_new(false, true, 1);
  1970. AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
  1971. .oem_table_id = oem_table_id };
  1972. uint64_t feature_report;
  1973. acpi_table_begin(&table, table_data);
  1974. /* IVinfo - IO virtualization information common to all
  1975. * IOMMU units in a system
  1976. */
  1977. build_append_int_noprefix(table_data,
  1978. (1UL << 0) | /* EFRSup */
  1979. (40UL << 8), /* PASize */
  1980. 4);
  1981. /* reserved */
  1982. build_append_int_noprefix(table_data, 0, 8);
  1983. /*
  1984. * A PCI bus walk, for each PCI host bridge, is necessary to create a
  1985. * complete set of IVHD entries. Do this into a separate blob so that we
  1986. * can calculate the total IVRS table length here and then append the new
  1987. * blob further below. Fall back to an entry covering all devices, which
  1988. * is sufficient when no aliases are present.
  1989. */
  1990. object_child_foreach_recursive(object_get_root(),
  1991. ivrs_host_bridges, ivhd_blob);
  1992. if (!ivhd_blob->len) {
  1993. /*
  1994. * Type 1 device entry reporting all devices
  1995. * These are 4-byte device entries currently reporting the range of
  1996. * Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
  1997. */
  1998. build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
  1999. }
  2000. /*
  2001. * When interrupt remapping is supported, we add a special IVHD device
  2002. * for type IO-APIC
  2003. * Refer to spec - Table 95: IVHD device entry type codes
  2004. *
  2005. * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
  2006. * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
  2007. */
  2008. if (x86_iommu_ir_supported(x86_iommu_get_default())) {
  2009. build_append_int_noprefix(ivhd_blob,
  2010. (0x1ull << 56) | /* type IOAPIC */
  2011. (IOAPIC_SB_DEVID << 40) | /* IOAPIC devid */
  2012. 0x48, /* special device */
  2013. 8);
  2014. }
  2015. /* IVHD definition - type 10h */
  2016. build_append_int_noprefix(table_data, 0x10, 1);
  2017. /* virtualization flags */
  2018. build_append_int_noprefix(table_data,
  2019. (1UL << 0) | /* HtTunEn */
  2020. (1UL << 4) | /* iotblSup */
  2021. (1UL << 6) | /* PrefSup */
  2022. (1UL << 7), /* PPRSup */
  2023. 1);
  2024. /* IVHD length */
  2025. build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
  2026. /* DeviceID */
  2027. build_append_int_noprefix(table_data,
  2028. object_property_get_int(OBJECT(&s->pci), "addr",
  2029. &error_abort), 2);
  2030. /* Capability offset */
  2031. build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
  2032. /* IOMMU base address */
  2033. build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
  2034. /* PCI Segment Group */
  2035. build_append_int_noprefix(table_data, 0, 2);
  2036. /* IOMMU info */
  2037. build_append_int_noprefix(table_data, 0, 2);
  2038. /* IOMMU Feature Reporting */
  2039. feature_report = (48UL << 30) | /* HATS */
  2040. (48UL << 28) | /* GATS */
  2041. (1UL << 2) | /* GTSup */
  2042. (1UL << 6); /* GASup */
  2043. if (s->xtsup) {
  2044. feature_report |= (1UL << 0); /* XTSup */
  2045. }
  2046. build_append_int_noprefix(table_data, feature_report, 4);
  2047. /* IVHD entries as found above */
  2048. g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
  2049. /* IVHD definition - type 11h */
  2050. build_append_int_noprefix(table_data, 0x11, 1);
  2051. /* virtualization flags */
  2052. build_append_int_noprefix(table_data,
  2053. (1UL << 0) | /* HtTunEn */
  2054. (1UL << 4), /* iotblSup */
  2055. 1);
  2056. /* IVHD length */
  2057. build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
  2058. /* DeviceID */
  2059. build_append_int_noprefix(table_data,
  2060. object_property_get_int(OBJECT(&s->pci), "addr",
  2061. &error_abort), 2);
  2062. /* Capability offset */
  2063. build_append_int_noprefix(table_data, s->pci.capab_offset, 2);
  2064. /* IOMMU base address */
  2065. build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
  2066. /* PCI Segment Group */
  2067. build_append_int_noprefix(table_data, 0, 2);
  2068. /* IOMMU info */
  2069. build_append_int_noprefix(table_data, 0, 2);
  2070. /* IOMMU Attributes */
  2071. build_append_int_noprefix(table_data, 0, 4);
  2072. /* EFR Register Image */
  2073. build_append_int_noprefix(table_data,
  2074. amdvi_extended_feature_register(s),
  2075. 8);
  2076. /* EFR Register Image 2 */
  2077. build_append_int_noprefix(table_data, 0, 8);
  2078. /* IVHD entries as found above */
  2079. g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
  2080. g_array_free(ivhd_blob, TRUE);
  2081. acpi_table_end(linker, &table);
  2082. }
  2083. typedef
  2084. struct AcpiBuildState {
  2085. /* Copy of table in RAM (for patching). */
  2086. MemoryRegion *table_mr;
  2087. /* Is table patched? */
  2088. uint8_t patched;
  2089. MemoryRegion *rsdp_mr;
  2090. MemoryRegion *linker_mr;
  2091. } AcpiBuildState;
  2092. static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
  2093. {
  2094. Object *pci_host;
  2095. QObject *o;
  2096. pci_host = acpi_get_i386_pci_host();
  2097. if (!pci_host) {
  2098. return false;
  2099. }
  2100. o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
  2101. if (!o) {
  2102. return false;
  2103. }
  2104. mcfg->base = qnum_get_uint(qobject_to(QNum, o));
  2105. qobject_unref(o);
  2106. if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
  2107. return false;
  2108. }
  2109. o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
  2110. assert(o);
  2111. mcfg->size = qnum_get_uint(qobject_to(QNum, o));
  2112. qobject_unref(o);
  2113. return true;
  2114. }
  2115. static
  2116. void acpi_build(AcpiBuildTables *tables, MachineState *machine)
  2117. {
  2118. PCMachineState *pcms = PC_MACHINE(machine);
  2119. X86MachineState *x86ms = X86_MACHINE(machine);
  2120. DeviceState *iommu = pcms->iommu;
  2121. GArray *table_offsets;
  2122. unsigned facs, dsdt, rsdt;
  2123. AcpiPmInfo pm;
  2124. AcpiMiscInfo misc;
  2125. AcpiMcfgInfo mcfg;
  2126. Range pci_hole = {}, pci_hole64 = {};
  2127. uint8_t *u;
  2128. GArray *tables_blob = tables->table_data;
  2129. AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
  2130. Object *vmgenid_dev, *vmclock_dev;
  2131. char *oem_id;
  2132. char *oem_table_id;
  2133. acpi_get_pm_info(machine, &pm);
  2134. acpi_get_misc_info(&misc);
  2135. acpi_get_pci_holes(&pci_hole, &pci_hole64);
  2136. acpi_get_slic_oem(&slic_oem);
  2137. if (slic_oem.id) {
  2138. oem_id = slic_oem.id;
  2139. } else {
  2140. oem_id = x86ms->oem_id;
  2141. }
  2142. if (slic_oem.table_id) {
  2143. oem_table_id = slic_oem.table_id;
  2144. } else {
  2145. oem_table_id = x86ms->oem_table_id;
  2146. }
  2147. table_offsets = g_array_new(false, true /* clear */,
  2148. sizeof(uint32_t));
  2149. ACPI_BUILD_DPRINTF("init ACPI tables\n");
  2150. bios_linker_loader_alloc(tables->linker,
  2151. ACPI_BUILD_TABLE_FILE, tables_blob,
  2152. 64 /* Ensure FACS is aligned */,
  2153. false /* high memory */);
  2154. /*
  2155. * FACS is pointed to by FADT.
  2156. * We place it first since it's the only table that has alignment
  2157. * requirements.
  2158. */
  2159. facs = tables_blob->len;
  2160. build_facs(tables_blob);
  2161. /* DSDT is pointed to by FADT */
  2162. dsdt = tables_blob->len;
  2163. build_dsdt(tables_blob, tables->linker, &pm, &misc,
  2164. &pci_hole, &pci_hole64, machine);
  2165. /* ACPI tables pointed to by RSDT */
  2166. acpi_add_table(table_offsets, tables_blob);
  2167. pm.fadt.facs_tbl_offset = &facs;
  2168. pm.fadt.dsdt_tbl_offset = &dsdt;
  2169. pm.fadt.xdsdt_tbl_offset = &dsdt;
  2170. build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
  2171. acpi_add_table(table_offsets, tables_blob);
  2172. acpi_build_madt(tables_blob, tables->linker, x86ms,
  2173. x86ms->oem_id, x86ms->oem_table_id);
  2174. #ifdef CONFIG_ACPI_ERST
  2175. {
  2176. Object *erst_dev;
  2177. erst_dev = find_erst_dev();
  2178. if (erst_dev) {
  2179. acpi_add_table(table_offsets, tables_blob);
  2180. build_erst(tables_blob, tables->linker, erst_dev,
  2181. x86ms->oem_id, x86ms->oem_table_id);
  2182. }
  2183. }
  2184. #endif
  2185. vmgenid_dev = find_vmgenid_dev();
  2186. if (vmgenid_dev) {
  2187. acpi_add_table(table_offsets, tables_blob);
  2188. vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
  2189. tables->vmgenid, tables->linker, x86ms->oem_id);
  2190. }
  2191. vmclock_dev = find_vmclock_dev();
  2192. if (vmclock_dev) {
  2193. acpi_add_table(table_offsets, tables_blob);
  2194. vmclock_build_acpi(VMCLOCK(vmclock_dev), tables_blob, tables->linker,
  2195. x86ms->oem_id);
  2196. }
  2197. if (misc.has_hpet) {
  2198. acpi_add_table(table_offsets, tables_blob);
  2199. build_hpet(tables_blob, tables->linker, x86ms->oem_id,
  2200. x86ms->oem_table_id);
  2201. }
  2202. #ifdef CONFIG_TPM
  2203. if (misc.tpm_version != TPM_VERSION_UNSPEC) {
  2204. if (misc.tpm_version == TPM_VERSION_1_2) {
  2205. acpi_add_table(table_offsets, tables_blob);
  2206. build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
  2207. x86ms->oem_id, x86ms->oem_table_id);
  2208. } else { /* TPM_VERSION_2_0 */
  2209. acpi_add_table(table_offsets, tables_blob);
  2210. build_tpm2(tables_blob, tables->linker, tables->tcpalog,
  2211. x86ms->oem_id, x86ms->oem_table_id);
  2212. }
  2213. }
  2214. #endif
  2215. if (machine->numa_state->num_nodes) {
  2216. acpi_add_table(table_offsets, tables_blob);
  2217. build_srat(tables_blob, tables->linker, machine);
  2218. if (machine->numa_state->have_numa_distance) {
  2219. acpi_add_table(table_offsets, tables_blob);
  2220. build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
  2221. x86ms->oem_table_id);
  2222. }
  2223. if (machine->numa_state->hmat_enabled) {
  2224. acpi_add_table(table_offsets, tables_blob);
  2225. build_hmat(tables_blob, tables->linker, machine->numa_state,
  2226. x86ms->oem_id, x86ms->oem_table_id);
  2227. }
  2228. }
  2229. if (acpi_get_mcfg(&mcfg)) {
  2230. acpi_add_table(table_offsets, tables_blob);
  2231. build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
  2232. x86ms->oem_table_id);
  2233. }
  2234. if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
  2235. acpi_add_table(table_offsets, tables_blob);
  2236. build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
  2237. x86ms->oem_table_id);
  2238. } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) {
  2239. acpi_add_table(table_offsets, tables_blob);
  2240. build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
  2241. x86ms->oem_table_id);
  2242. } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) {
  2243. PCIDevice *pdev = PCI_DEVICE(iommu);
  2244. acpi_add_table(table_offsets, tables_blob);
  2245. build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev),
  2246. x86ms->oem_id, x86ms->oem_table_id);
  2247. }
  2248. if (machine->nvdimms_state->is_enabled) {
  2249. nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
  2250. machine->nvdimms_state, machine->ram_slots,
  2251. x86ms->oem_id, x86ms->oem_table_id);
  2252. }
  2253. if (pcms->cxl_devices_state.is_enabled) {
  2254. cxl_build_cedt(table_offsets, tables_blob, tables->linker,
  2255. x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state);
  2256. }
  2257. acpi_add_table(table_offsets, tables_blob);
  2258. build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
  2259. /* Add tables supplied by user (if any) */
  2260. for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
  2261. unsigned len = acpi_table_len(u);
  2262. acpi_add_table(table_offsets, tables_blob);
  2263. g_array_append_vals(tables_blob, u, len);
  2264. }
  2265. /* RSDT is pointed to by RSDP */
  2266. rsdt = tables_blob->len;
  2267. build_rsdt(tables_blob, tables->linker, table_offsets,
  2268. oem_id, oem_table_id);
  2269. /* RSDP is in FSEG memory, so allocate it separately */
  2270. {
  2271. AcpiRsdpData rsdp_data = {
  2272. .revision = 0,
  2273. .oem_id = x86ms->oem_id,
  2274. .xsdt_tbl_offset = NULL,
  2275. .rsdt_tbl_offset = &rsdt,
  2276. };
  2277. build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
  2278. }
  2279. /* We'll expose it all to Guest so we want to reduce
  2280. * chance of size changes.
  2281. *
  2282. * We used to align the tables to 4k, but of course this would
  2283. * too simple to be enough. 4k turned out to be too small an
  2284. * alignment very soon, and in fact it is almost impossible to
  2285. * keep the table size stable for all (max_cpus, max_memory_slots)
  2286. * combinations.
  2287. */
  2288. acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
  2289. acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
  2290. /* Cleanup memory that's no longer used. */
  2291. g_array_free(table_offsets, true);
  2292. g_free(slic_oem.id);
  2293. g_free(slic_oem.table_id);
  2294. }
  2295. static void acpi_ram_update(MemoryRegion *mr, GArray *data)
  2296. {
  2297. uint32_t size = acpi_data_len(data);
  2298. /* Make sure RAM size is correct - in case it got changed e.g. by migration */
  2299. memory_region_ram_resize(mr, size, &error_abort);
  2300. memcpy(memory_region_get_ram_ptr(mr), data->data, size);
  2301. memory_region_set_dirty(mr, 0, size);
  2302. }
  2303. static void acpi_build_update(void *build_opaque)
  2304. {
  2305. AcpiBuildState *build_state = build_opaque;
  2306. AcpiBuildTables tables;
  2307. /* No state to update or already patched? Nothing to do. */
  2308. if (!build_state || build_state->patched) {
  2309. return;
  2310. }
  2311. build_state->patched = 1;
  2312. acpi_build_tables_init(&tables);
  2313. acpi_build(&tables, MACHINE(qdev_get_machine()));
  2314. acpi_ram_update(build_state->table_mr, tables.table_data);
  2315. acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
  2316. acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
  2317. acpi_build_tables_cleanup(&tables, true);
  2318. }
  2319. static void acpi_build_reset(void *build_opaque)
  2320. {
  2321. AcpiBuildState *build_state = build_opaque;
  2322. build_state->patched = 0;
  2323. }
  2324. static const VMStateDescription vmstate_acpi_build = {
  2325. .name = "acpi_build",
  2326. .version_id = 1,
  2327. .minimum_version_id = 1,
  2328. .fields = (const VMStateField[]) {
  2329. VMSTATE_UINT8(patched, AcpiBuildState),
  2330. VMSTATE_END_OF_LIST()
  2331. },
  2332. };
  2333. void acpi_setup(void)
  2334. {
  2335. PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
  2336. X86MachineState *x86ms = X86_MACHINE(pcms);
  2337. AcpiBuildTables tables;
  2338. AcpiBuildState *build_state;
  2339. Object *vmgenid_dev;
  2340. #ifdef CONFIG_TPM
  2341. TPMIf *tpm;
  2342. static FwCfgTPMConfig tpm_config;
  2343. #endif
  2344. if (!x86ms->fw_cfg) {
  2345. ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
  2346. return;
  2347. }
  2348. if (!pcms->acpi_build_enabled) {
  2349. ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
  2350. return;
  2351. }
  2352. if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
  2353. ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
  2354. return;
  2355. }
  2356. build_state = g_malloc0(sizeof *build_state);
  2357. acpi_build_tables_init(&tables);
  2358. acpi_build(&tables, MACHINE(pcms));
  2359. /* Now expose it all to Guest */
  2360. build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
  2361. build_state, tables.table_data,
  2362. ACPI_BUILD_TABLE_FILE);
  2363. assert(build_state->table_mr != NULL);
  2364. build_state->linker_mr =
  2365. acpi_add_rom_blob(acpi_build_update, build_state,
  2366. tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
  2367. #ifdef CONFIG_TPM
  2368. fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
  2369. tables.tcpalog->data, acpi_data_len(tables.tcpalog));
  2370. tpm = tpm_find();
  2371. if (tpm && object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
  2372. tpm_config = (FwCfgTPMConfig) {
  2373. .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
  2374. .tpm_version = tpm_get_version(tpm),
  2375. .tpmppi_version = TPM_PPI_VERSION_1_30
  2376. };
  2377. fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
  2378. &tpm_config, sizeof tpm_config);
  2379. }
  2380. #endif
  2381. vmgenid_dev = find_vmgenid_dev();
  2382. if (vmgenid_dev) {
  2383. vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
  2384. tables.vmgenid);
  2385. }
  2386. build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
  2387. build_state, tables.rsdp,
  2388. ACPI_BUILD_RSDP_FILE);
  2389. qemu_register_reset(acpi_build_reset, build_state);
  2390. acpi_build_reset(build_state);
  2391. vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
  2392. /* Cleanup tables but don't free the memory: we track it
  2393. * in build_state.
  2394. */
  2395. acpi_build_tables_cleanup(&tables, false);
  2396. }