acpi-build.c 101 KB

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