acpi-build.c 97 KB

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