blob: 9320787635c21988085bc26571cb98b28620642c [file] [log] [blame]
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04001// Support for generating ACPI tables (on emulators)
Kevin O'Connor276d4a92008-06-11 22:47:01 -04002//
Kevin O'Connore2074bf2010-08-03 21:30:03 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor276d4a92008-06-11 22:47:01 -04004// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor276d4a92008-06-11 22:47:01 -04007
8#include "acpi.h" // struct rsdp_descriptor
9#include "util.h" // memcpy
Kevin O'Connorb3064592012-08-14 21:20:10 -040010#include "byteorder.h" // cpu_to_le16
Kevin O'Connor0cd70052011-07-02 14:04:19 -040011#include "pci.h" // pci_find_init_device
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050012#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
13#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor4bc49972012-05-13 22:58:08 -040014#include "ioport.h" // inl
Kevin O'Connorb3064592012-08-14 21:20:10 -040015#include "paravirt.h" // qemu_cfg_irq0_override
Kevin O'Connor276d4a92008-06-11 22:47:01 -040016
17/****************************************************/
18/* ACPI tables init */
19
20/* Table structure from Linux kernel (the ACPI tables are under the
21 BSD license) */
22
Kevin O'Connor276d4a92008-06-11 22:47:01 -040023struct acpi_table_header /* ACPI common table header */
24{
Kevin O'Connord10e4442009-03-01 12:41:20 -050025 ACPI_TABLE_HEADER_DEF
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040026} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040027
28/*
29 * ACPI 1.0 Root System Description Table (RSDT)
30 */
Kevin O'Connor9967ab72008-12-18 21:57:33 -050031#define RSDT_SIGNATURE 0x54445352 // RSDT
Kevin O'Connor276d4a92008-06-11 22:47:01 -040032struct rsdt_descriptor_rev1
33{
Kevin O'Connord10e4442009-03-01 12:41:20 -050034 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
Kevin O'Connora9242a22009-10-07 19:42:07 -040035 u32 table_offset_entry[0]; /* Array of pointers to other */
Kevin O'Connord10e4442009-03-01 12:41:20 -050036 /* ACPI tables */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040037} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040038
39/*
40 * ACPI 1.0 Firmware ACPI Control Structure (FACS)
41 */
Kevin O'Connor9967ab72008-12-18 21:57:33 -050042#define FACS_SIGNATURE 0x53434146 // FACS
Kevin O'Connor276d4a92008-06-11 22:47:01 -040043struct facs_descriptor_rev1
44{
Kevin O'Connord10e4442009-03-01 12:41:20 -050045 u32 signature; /* ACPI Signature */
46 u32 length; /* Length of structure, in bytes */
47 u32 hardware_signature; /* Hardware configuration signature */
48 u32 firmware_waking_vector; /* ACPI OS waking vector */
49 u32 global_lock; /* Global Lock */
50 u32 S4bios_f : 1; /* Indicates if S4BIOS support is present */
51 u32 reserved1 : 31; /* Must be 0 */
52 u8 resverved3 [40]; /* Reserved - must be zero */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040053} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040054
55
56/*
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +090057 * Differentiated System Description Table (DSDT)
58 */
59#define DSDT_SIGNATURE 0x54445344 // DSDT
60
61/*
Kevin O'Connor276d4a92008-06-11 22:47:01 -040062 * MADT values and structures
63 */
64
65/* Values for MADT PCATCompat */
66
67#define DUAL_PIC 0
68#define MULTIPLE_APIC 1
69
70
71/* Master MADT */
72
Kevin O'Connor9967ab72008-12-18 21:57:33 -050073#define APIC_SIGNATURE 0x43495041 // APIC
Kevin O'Connor276d4a92008-06-11 22:47:01 -040074struct multiple_apic_table
75{
Kevin O'Connord10e4442009-03-01 12:41:20 -050076 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
77 u32 local_apic_address; /* Physical address of local APIC */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040078#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -050079 u32 PCATcompat : 1; /* A one indicates system also has dual 8259s */
80 u32 reserved1 : 31;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040081#else
Kevin O'Connord10e4442009-03-01 12:41:20 -050082 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040083#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040084} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040085
86
Kevin O'Connor590e5542009-10-08 22:09:02 -040087/* Values for Type in APIC sub-headers */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040088
89#define APIC_PROCESSOR 0
90#define APIC_IO 1
91#define APIC_XRUPT_OVERRIDE 2
92#define APIC_NMI 3
93#define APIC_LOCAL_NMI 4
94#define APIC_ADDRESS_OVERRIDE 5
95#define APIC_IO_SAPIC 6
96#define APIC_LOCAL_SAPIC 7
97#define APIC_XRUPT_SOURCE 8
98#define APIC_RESERVED 9 /* 9 and greater are reserved */
99
100/*
101 * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
102 */
Kevin O'Connor590e5542009-10-08 22:09:02 -0400103#define ACPI_SUB_HEADER_DEF /* Common ACPI sub-structure header */\
Kevin O'Connord10e4442009-03-01 12:41:20 -0500104 u8 type; \
105 u8 length;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400106
107/* Sub-structures for MADT */
108
109struct madt_processor_apic
110{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400111 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500112 u8 processor_id; /* ACPI processor id */
113 u8 local_apic_id; /* Processor's local APIC id */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400114#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -0500115 u32 processor_enabled: 1; /* Processor is usable if set */
116 u32 reserved2 : 31; /* Reserved, must be zero */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400117#else
Kevin O'Connord10e4442009-03-01 12:41:20 -0500118 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400119#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400120} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400121
122struct madt_io_apic
123{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400124 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500125 u8 io_apic_id; /* I/O APIC ID */
126 u8 reserved; /* Reserved - must be zero */
127 u32 address; /* APIC physical address */
128 u32 interrupt; /* Global system interrupt where INTI
129 * lines start */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400130} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400131
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500132/* IRQs 5,9,10,11 */
133#define PCI_ISA_IRQ_MASK 0x0e20
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500134
135struct madt_intsrcovr {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400136 ACPI_SUB_HEADER_DEF
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500137 u8 bus;
138 u8 source;
139 u32 gsi;
140 u16 flags;
141} PACKED;
142
Kenji Kaneshige70986382011-10-10 14:06:17 +0800143struct madt_local_nmi {
144 ACPI_SUB_HEADER_DEF
145 u8 processor_id; /* ACPI processor id */
146 u16 flags; /* MPS INTI flags */
147 u8 lint; /* Local APIC LINT# */
148} PACKED;
149
150
Kevin O'Connorf4343772009-10-08 22:05:21 -0400151/*
152 * ACPI 2.0 Generic Address Space definition.
153 */
154struct acpi_20_generic_address {
155 u8 address_space_id;
156 u8 register_bit_width;
157 u8 register_bit_offset;
158 u8 reserved;
159 u64 address;
160} PACKED;
161
162/*
163 * HPET Description Table
164 */
165struct acpi_20_hpet {
166 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
167 u32 timer_block_id;
168 struct acpi_20_generic_address addr;
169 u8 hpet_number;
170 u16 min_tick;
171 u8 page_protect;
172} PACKED;
Kevin O'Connorf4343772009-10-08 22:05:21 -0400173
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200174#define HPET_ID 0x000
175#define HPET_PERIOD 0x004
176
Kevin O'Connor590e5542009-10-08 22:09:02 -0400177/*
178 * SRAT (NUMA topology description) table
179 */
180
181#define SRAT_PROCESSOR 0
182#define SRAT_MEMORY 1
183
184struct system_resource_affinity_table
185{
186 ACPI_TABLE_HEADER_DEF
187 u32 reserved1;
188 u32 reserved2[2];
189} PACKED;
190
191struct srat_processor_affinity
192{
193 ACPI_SUB_HEADER_DEF
194 u8 proximity_lo;
195 u8 local_apic_id;
196 u32 flags;
197 u8 local_sapic_eid;
198 u8 proximity_hi[3];
199 u32 reserved;
200} PACKED;
201
202struct srat_memory_affinity
203{
204 ACPI_SUB_HEADER_DEF
205 u8 proximity[4];
206 u16 reserved1;
207 u32 base_addr_low,base_addr_high;
208 u32 length_low,length_high;
209 u32 reserved2;
210 u32 flags;
211 u32 reserved3[2];
212} PACKED;
213
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400214#include "acpi-dsdt.hex"
215
Kevin O'Connor2929c352009-07-25 13:48:27 -0400216static void
Kevin O'Connora9242a22009-10-07 19:42:07 -0400217build_header(struct acpi_table_header *h, u32 sig, int len, u8 rev)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400218{
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500219 h->signature = sig;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400220 h->length = cpu_to_le32(len);
221 h->revision = rev;
Kevin O'Connor6cb8ba92008-08-17 11:03:24 -0400222 memcpy(h->oem_id, CONFIG_APPNAME6, 6);
223 memcpy(h->oem_table_id, CONFIG_APPNAME4, 4);
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500224 memcpy(h->oem_table_id + 4, (void*)&sig, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400225 h->oem_revision = cpu_to_le32(1);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400226 memcpy(h->asl_compiler_id, CONFIG_APPNAME4, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400227 h->asl_compiler_revision = cpu_to_le32(1);
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400228 h->checksum -= checksum(h, len);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400229}
230
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400231#define PIIX4_ACPI_ENABLE 0xf1
232#define PIIX4_ACPI_DISABLE 0xf0
233#define PIIX4_GPE0_BLK 0xafe0
234#define PIIX4_GPE0_BLK_LEN 4
235
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400236static void piix4_fadt_init(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400237{
238 struct fadt_descriptor_rev1 *fadt = arg;
239 fadt->acpi_enable = PIIX4_ACPI_ENABLE;
240 fadt->acpi_disable = PIIX4_ACPI_DISABLE;
241 fadt->gpe0_blk = cpu_to_le32(PIIX4_GPE0_BLK);
242 fadt->gpe0_blk_len = PIIX4_GPE0_BLK_LEN;
243}
244
Isaku Yamahatafe54a532010-07-20 16:37:18 +0900245static const struct pci_device_id fadt_init_tbl[] = {
246 /* PIIX4 Power Management device (for ACPI) */
247 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
248 piix4_fadt_init),
249
250 PCI_DEVICE_END
251};
252
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900253static void fill_dsdt(struct fadt_descriptor_rev1 *fadt, void *dsdt)
254{
255 if (fadt->dsdt) {
256 free((void *)le32_to_cpu(fadt->dsdt));
257 }
258 fadt->dsdt = cpu_to_le32((u32)dsdt);
259 fadt->checksum -= checksum(fadt, sizeof(*fadt));
260 dprintf(1, "ACPI DSDT=%p\n", dsdt);
261}
262
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400263static void *
264build_fadt(struct pci_device *pci)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400265{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400266 struct fadt_descriptor_rev1 *fadt = malloc_high(sizeof(*fadt));
Kevin O'Connor415d4292009-08-30 19:19:31 -0400267 struct facs_descriptor_rev1 *facs = memalign_high(64, sizeof(*facs));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400268
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900269 if (!fadt || !facs) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500270 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400271 return NULL;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400272 }
273
Kevin O'Connor2929c352009-07-25 13:48:27 -0400274 /* FACS */
Kevin O'Connor2929c352009-07-25 13:48:27 -0400275 memset(facs, 0, sizeof(*facs));
276 facs->signature = FACS_SIGNATURE;
277 facs->length = cpu_to_le32(sizeof(*facs));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400278
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400279 /* FADT */
280 memset(fadt, 0, sizeof(*fadt));
Kevin O'Connor2929c352009-07-25 13:48:27 -0400281 fadt->firmware_ctrl = cpu_to_le32((u32)facs);
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900282 fadt->dsdt = 0; /* dsdt will be filled later in acpi_bios_init()
283 by fill_dsdt() */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400284 fadt->model = 1;
285 fadt->reserved1 = 0;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400286 int pm_sci_int = pci_config_readb(pci->bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400287 fadt->sci_int = cpu_to_le16(pm_sci_int);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500288 fadt->smi_cmd = cpu_to_le32(PORT_SMI_CMD);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500289 fadt->pm1a_evt_blk = cpu_to_le32(PORT_ACPI_PM_BASE);
290 fadt->pm1a_cnt_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x04);
291 fadt->pm_tmr_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x08);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400292 fadt->pm1_evt_len = 4;
293 fadt->pm1_cnt_len = 2;
294 fadt->pm_tmr_len = 4;
295 fadt->plvl2_lat = cpu_to_le16(0xfff); // C2 state not supported
296 fadt->plvl3_lat = cpu_to_le16(0xfff); // C3 state not supported
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400297 pci_init_device(fadt_init_tbl, pci, fadt);
Kevin O'Connorafdeda02012-07-20 02:42:08 -0400298 /* WBINVD + PROC_C1 + SLP_BUTTON + RTC_S4 + USE_PLATFORM_CLOCK */
299 fadt->flags = cpu_to_le32((1 << 0) | (1 << 2) | (1 << 5) | (1 << 7) |
Marcelo Tosattia4e2b9a2012-07-17 14:18:00 -0300300 (1 << 15));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400301
Kevin O'Connora9242a22009-10-07 19:42:07 -0400302 build_header((void*)fadt, FACP_SIGNATURE, sizeof(*fadt), 1);
303
304 return fadt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400305}
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400306
Kevin O'Connora9242a22009-10-07 19:42:07 -0400307static void*
308build_madt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400309{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400310 int madt_size = (sizeof(struct multiple_apic_table)
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400311 + sizeof(struct madt_processor_apic) * MaxCountCPUs
Kevin O'Connor2929c352009-07-25 13:48:27 -0400312 + sizeof(struct madt_io_apic)
Kenji Kaneshige70986382011-10-10 14:06:17 +0800313 + sizeof(struct madt_intsrcovr) * 16
314 + sizeof(struct madt_local_nmi));
315
Kevin O'Connor2929c352009-07-25 13:48:27 -0400316 struct multiple_apic_table *madt = malloc_high(madt_size);
317 if (!madt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500318 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400319 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400320 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500321 memset(madt, 0, madt_size);
322 madt->local_apic_address = cpu_to_le32(BUILD_APIC_ADDR);
323 madt->flags = cpu_to_le32(1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400324 struct madt_processor_apic *apic = (void*)&madt[1];
325 int i;
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400326 for (i=0; i<MaxCountCPUs; i++) {
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500327 apic->type = APIC_PROCESSOR;
328 apic->length = sizeof(*apic);
329 apic->processor_id = i;
330 apic->local_apic_id = i;
Eduardo Habkost008c1fc2012-07-25 15:45:30 -0300331 if (apic_id_is_present(apic->local_apic_id))
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400332 apic->flags = cpu_to_le32(1);
333 else
334 apic->flags = cpu_to_le32(0);
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500335 apic++;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400336 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400337 struct madt_io_apic *io_apic = (void*)apic;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500338 io_apic->type = APIC_IO;
339 io_apic->length = sizeof(*io_apic);
Eduardo Habkoste39b9382012-07-25 15:45:29 -0300340 io_apic->io_apic_id = BUILD_IOAPIC_ID;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500341 io_apic->address = cpu_to_le32(BUILD_IOAPIC_ADDR);
342 io_apic->interrupt = cpu_to_le32(0);
343
Kevin O'Connord10e4442009-03-01 12:41:20 -0500344 struct madt_intsrcovr *intsrcovr = (void*)&io_apic[1];
Kevin O'Connor4d2b6192009-10-08 21:37:21 -0400345 if (qemu_cfg_irq0_override()) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400346 memset(intsrcovr, 0, sizeof(*intsrcovr));
347 intsrcovr->type = APIC_XRUPT_OVERRIDE;
348 intsrcovr->length = sizeof(*intsrcovr);
349 intsrcovr->source = 0;
350 intsrcovr->gsi = 2;
351 intsrcovr->flags = 0; /* conforms to bus specifications */
352 intsrcovr++;
353 }
354 for (i = 1; i < 16; i++) {
Kevin O'Connord10e4442009-03-01 12:41:20 -0500355 if (!(PCI_ISA_IRQ_MASK & (1 << i)))
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500356 /* No need for a INT source override structure. */
357 continue;
Kevin O'Connord10e4442009-03-01 12:41:20 -0500358 memset(intsrcovr, 0, sizeof(*intsrcovr));
359 intsrcovr->type = APIC_XRUPT_OVERRIDE;
360 intsrcovr->length = sizeof(*intsrcovr);
361 intsrcovr->source = i;
362 intsrcovr->gsi = i;
363 intsrcovr->flags = 0xd; /* active high, level triggered */
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500364 intsrcovr++;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500365 }
366
Kenji Kaneshige70986382011-10-10 14:06:17 +0800367 struct madt_local_nmi *local_nmi = (void*)intsrcovr;
368 local_nmi->type = APIC_LOCAL_NMI;
369 local_nmi->length = sizeof(*local_nmi);
370 local_nmi->processor_id = 0xff; /* all processors */
371 local_nmi->flags = 0;
372 local_nmi->lint = 1; /* LINT1 */
373 local_nmi++;
374
375 build_header((void*)madt, APIC_SIGNATURE, (void*)local_nmi - (void*)madt, 1);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400376 return madt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400377}
378
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400379// Encode a hex value
380static inline char getHex(u32 val) {
381 val &= 0x0f;
382 return (val <= 9) ? ('0' + val) : ('A' + val - 10);
383}
384
385// Encode a length in an SSDT.
386static u8 *
387encodeLen(u8 *ssdt_ptr, int length, int bytes)
388{
389 switch (bytes) {
390 default:
391 case 4: ssdt_ptr[3] = ((length >> 20) & 0xff);
392 case 3: ssdt_ptr[2] = ((length >> 12) & 0xff);
393 case 2: ssdt_ptr[1] = ((length >> 4) & 0xff);
394 ssdt_ptr[0] = (((bytes-1) & 0x3) << 6) | (length & 0x0f);
395 break;
396 case 1: ssdt_ptr[0] = length & 0x3f;
397 }
398 return ssdt_ptr + bytes;
399}
400
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200401#include "ssdt-proc.hex"
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200402
403/* 0x5B 0x83 ProcessorOp PkgLength NameString ProcID */
Paolo Bonzinic8ed45e2012-08-02 15:07:22 +0200404#define PROC_OFFSET_CPUHEX (*ssdt_proc_name - *ssdt_proc_start + 2)
405#define PROC_OFFSET_CPUID1 (*ssdt_proc_name - *ssdt_proc_start + 4)
406#define PROC_OFFSET_CPUID2 (*ssdt_proc_id - *ssdt_proc_start)
407#define PROC_SIZEOF (*ssdt_proc_end - *ssdt_proc_start)
408#define PROC_AML (ssdp_proc_aml + *ssdt_proc_start)
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400409
Paolo Bonzini7dceba32012-08-02 15:07:25 +0200410#define PCI_SLOTS 32
411
Kevin O'Connor2929c352009-07-25 13:48:27 -0400412#define SSDT_SIGNATURE 0x54445353 // SSDT
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200413#define SSDT_HEADER_LENGTH 36
414
415#include "ssdt-susp.hex"
416
Paolo Bonzini7dceba32012-08-02 15:07:25 +0200417static u8*
418build_notify(u8 *ssdt_ptr, const char *name, int skip, int count,
419 const char *target, int ofs)
420{
421 count -= skip;
422
423 *(ssdt_ptr++) = 0x14; // MethodOp
424 ssdt_ptr = encodeLen(ssdt_ptr, 2+5+(12*count), 2);
425 memcpy(ssdt_ptr, name, 4);
426 ssdt_ptr += 4;
427 *(ssdt_ptr++) = 0x02; // MethodOp
428
429 int i;
430 for (i = skip; count-- > 0; i++) {
431 *(ssdt_ptr++) = 0xA0; // IfOp
432 ssdt_ptr = encodeLen(ssdt_ptr, 11, 1);
433 *(ssdt_ptr++) = 0x93; // LEqualOp
434 *(ssdt_ptr++) = 0x68; // Arg0Op
435 *(ssdt_ptr++) = 0x0A; // BytePrefix
436 *(ssdt_ptr++) = i;
437 *(ssdt_ptr++) = 0x86; // NotifyOp
438 memcpy(ssdt_ptr, target, 4);
439 ssdt_ptr[ofs] = getHex(i >> 4);
440 ssdt_ptr[ofs + 1] = getHex(i);
441 ssdt_ptr += 4;
442 *(ssdt_ptr++) = 0x69; // Arg1Op
443 }
444 return ssdt_ptr;
445}
446
Kevin O'Connora9242a22009-10-07 19:42:07 -0400447static void*
448build_ssdt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400449{
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400450 int acpi_cpus = MaxCountCPUs > 0xff ? 0xff : MaxCountCPUs;
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200451 int length = (sizeof(ssdp_susp_aml) // _S3_ / _S4_ / _S5_
452 + (1+3+4) // Scope(_SB_)
Paolo Bonzinic8ed45e2012-08-02 15:07:22 +0200453 + (acpi_cpus * PROC_SIZEOF) // procs
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200454 + (1+2+5+(12*acpi_cpus)) // NTFY
455 + (6+2+1+(1*acpi_cpus)) // CPON
Paolo Bonzini7dceba32012-08-02 15:07:25 +0200456 + 17 // BDAT
457 + (1+3+4) // Scope(PCI0)
458 + (1+2+5+(12*(PCI_SLOTS - 1)))); // PCNT
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200459 u8 *ssdt = malloc_high(length);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400460 if (! ssdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500461 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400462 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400463 }
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200464 u8 *ssdt_ptr = ssdt;
465
466 // Copy header and encode fwcfg values in the S3_ / S4_ / S5_ packages
467 int sys_state_size;
468 char *sys_states = romfile_loadfile("etc/system-states", &sys_state_size);
469 if (!sys_states || sys_state_size != 6)
470 sys_states = (char[]){128, 0, 0, 129, 128, 128};
471
472 memcpy(ssdt_ptr, ssdp_susp_aml, sizeof(ssdp_susp_aml));
473 if (!(sys_states[3] & 128))
474 ssdt_ptr[acpi_s3_name[0]] = 'X';
475 if (!(sys_states[4] & 128))
476 ssdt_ptr[acpi_s4_name[0]] = 'X';
477 else
478 ssdt_ptr[acpi_s4_pkg[0] + 1] = ssdt[acpi_s4_pkg[0] + 3] = sys_states[4] & 127;
479 ssdt_ptr += sizeof(ssdp_susp_aml);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400480
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400481 // build Scope(_SB_) header
Kevin O'Connor2929c352009-07-25 13:48:27 -0400482 *(ssdt_ptr++) = 0x10; // ScopeOp
Paolo Bonzinicf5bef42012-08-02 15:07:21 +0200483 ssdt_ptr = encodeLen(ssdt_ptr, length - (ssdt_ptr - ssdt), 3);
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400484 *(ssdt_ptr++) = '_';
485 *(ssdt_ptr++) = 'S';
486 *(ssdt_ptr++) = 'B';
Kevin O'Connor2929c352009-07-25 13:48:27 -0400487 *(ssdt_ptr++) = '_';
488
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400489 // build Processor object for each processor
Kevin O'Connor2929c352009-07-25 13:48:27 -0400490 int i;
491 for (i=0; i<acpi_cpus; i++) {
Paolo Bonzinic8ed45e2012-08-02 15:07:22 +0200492 memcpy(ssdt_ptr, PROC_AML, PROC_SIZEOF);
493 ssdt_ptr[PROC_OFFSET_CPUHEX] = getHex(i >> 4);
494 ssdt_ptr[PROC_OFFSET_CPUHEX+1] = getHex(i);
495 ssdt_ptr[PROC_OFFSET_CPUID1] = i;
496 ssdt_ptr[PROC_OFFSET_CPUID2] = i;
497 ssdt_ptr += PROC_SIZEOF;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400498 }
499
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400500 // build "Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}"
Eduardo Habkost008c1fc2012-07-25 15:45:30 -0300501 // Arg0 = Processor ID = APIC ID
Paolo Bonzini7dceba32012-08-02 15:07:25 +0200502 ssdt_ptr = build_notify(ssdt_ptr, "NTFY", 0, acpi_cpus, "CP00", 2);
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400503
504 // build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
505 *(ssdt_ptr++) = 0x08; // NameOp
506 *(ssdt_ptr++) = 'C';
507 *(ssdt_ptr++) = 'P';
508 *(ssdt_ptr++) = 'O';
509 *(ssdt_ptr++) = 'N';
510 *(ssdt_ptr++) = 0x12; // PackageOp
511 ssdt_ptr = encodeLen(ssdt_ptr, 2+1+(1*acpi_cpus), 2);
512 *(ssdt_ptr++) = acpi_cpus;
513 for (i=0; i<acpi_cpus; i++)
Eduardo Habkost008c1fc2012-07-25 15:45:30 -0300514 *(ssdt_ptr++) = (apic_id_is_present(i)) ? 0x01 : 0x00;
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400515
Gerd Hoffmann2062f2b2012-06-07 10:34:33 +0200516 // store pci io windows: start, end, length
517 // this way we don't have to do the math in the dsdt
518 struct bfld *bfld = malloc_high(sizeof(struct bfld));
519 bfld->p0s = pcimem_start;
520 bfld->p0e = pcimem_end - 1;
521 bfld->p0l = pcimem_end - pcimem_start;
522 bfld->p1s = pcimem64_start;
523 bfld->p1e = pcimem64_end - 1;
524 bfld->p1l = pcimem64_end - pcimem64_start;
525
526 // build "OperationRegion(BDAT, SystemMemory, 0x12345678, 0x87654321)"
527 *(ssdt_ptr++) = 0x5B; // ExtOpPrefix
528 *(ssdt_ptr++) = 0x80; // OpRegionOp
529 *(ssdt_ptr++) = 'B';
530 *(ssdt_ptr++) = 'D';
531 *(ssdt_ptr++) = 'A';
532 *(ssdt_ptr++) = 'T';
533 *(ssdt_ptr++) = 0x00; // SystemMemory
534 *(ssdt_ptr++) = 0x0C; // DWordPrefix
535 *(u32*)ssdt_ptr = (u32)bfld;
536 ssdt_ptr += 4;
537 *(ssdt_ptr++) = 0x0C; // DWordPrefix
538 *(u32*)ssdt_ptr = sizeof(struct bfld);
539 ssdt_ptr += 4;
540
Paolo Bonzini7dceba32012-08-02 15:07:25 +0200541 // build Scope(PCI0) opcode
542 *(ssdt_ptr++) = 0x10; // ScopeOp
543 ssdt_ptr = encodeLen(ssdt_ptr, length - (ssdt_ptr - ssdt), 3);
544 *(ssdt_ptr++) = 'P';
545 *(ssdt_ptr++) = 'C';
546 *(ssdt_ptr++) = 'I';
547 *(ssdt_ptr++) = '0';
548
549 ssdt_ptr = build_notify(ssdt_ptr, "PCNT", 1, PCI_SLOTS, "S00_", 1);
550
Kevin O'Connora9242a22009-10-07 19:42:07 -0400551 build_header((void*)ssdt, SSDT_SIGNATURE, ssdt_ptr - ssdt, 1);
552
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400553 //hexdump(ssdt, ssdt_ptr - ssdt);
554
Kevin O'Connora9242a22009-10-07 19:42:07 -0400555 return ssdt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400556}
557
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200558#include "ssdt-pcihp.hex"
559
560#define PCI_RMV_BASE 0xae0c
561
562extern void link_time_assertion(void);
563
564static void* build_pcihp(void)
Michael S. Tsirkinc9737882011-11-20 19:56:59 +0200565{
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200566 u32 rmvc_pcrm;
567 int i;
568
569 u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
Kevin O'Connor890d9852012-02-15 20:13:05 -0500570 if (!ssdt) {
571 warn_noalloc();
572 return NULL;
573 }
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200574 memcpy(ssdt, ssdp_pcihp_aml, sizeof ssdp_pcihp_aml);
575
576 /* Runtime patching of EJ0: to disable hotplug for a slot,
577 * replace the method name: _EJ0 by EJ0_. */
578 if (ARRAY_SIZE(aml_ej0_name) != ARRAY_SIZE(aml_adr_dword)) {
579 link_time_assertion();
580 }
581
582 rmvc_pcrm = inl(PCI_RMV_BASE);
583 for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
584 /* Slot is in byte 2 in _ADR */
585 u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
Kevin O'Connor7a08ae72012-03-14 21:11:39 -0400586 /* Sanity check */
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200587 if (memcmp(ssdp_pcihp_aml + aml_ej0_name[i], "_EJ0", 4)) {
588 warn_internalerror();
589 free(ssdt);
590 return NULL;
591 }
592 if (!(rmvc_pcrm & (0x1 << slot))) {
593 memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
594 }
595 }
596
Gleb Natapovd51c4a02012-05-20 12:03:40 +0300597 ((struct acpi_table_header*)ssdt)->checksum = 0;
598 ((struct acpi_table_header*)ssdt)->checksum -= checksum(ssdt, sizeof(ssdp_pcihp_aml));
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200599 return ssdt;
Michael S. Tsirkinc9737882011-11-20 19:56:59 +0200600}
601
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400602#define HPET_SIGNATURE 0x54455048 // HPET
Kevin O'Connorf4343772009-10-08 22:05:21 -0400603static void*
604build_hpet(void)
605{
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200606 struct acpi_20_hpet *hpet;
Kevin O'Connor7859eda2011-09-20 19:40:28 -0400607 const void *hpet_base = (void *)BUILD_HPET_ADDRESS;
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200608 u32 hpet_vendor = readl(hpet_base + HPET_ID) >> 16;
609 u32 hpet_period = readl(hpet_base + HPET_PERIOD);
610
611 if (hpet_vendor == 0 || hpet_vendor == 0xffff ||
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400612 hpet_period == 0 || hpet_period > 100000000)
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200613 return NULL;
614
615 hpet = malloc_high(sizeof(*hpet));
Kevin O'Connorf4343772009-10-08 22:05:21 -0400616 if (!hpet) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500617 warn_noalloc();
Kevin O'Connorf4343772009-10-08 22:05:21 -0400618 return NULL;
619 }
620
621 memset(hpet, 0, sizeof(*hpet));
622 /* Note timer_block_id value must be kept in sync with value advertised by
623 * emulated hpet
624 */
625 hpet->timer_block_id = cpu_to_le32(0x8086a201);
Kevin O'Connor7859eda2011-09-20 19:40:28 -0400626 hpet->addr.address = cpu_to_le32(BUILD_HPET_ADDRESS);
Kevin O'Connorf4343772009-10-08 22:05:21 -0400627 build_header((void*)hpet, HPET_SIGNATURE, sizeof(*hpet), 1);
628
629 return hpet;
630}
631
Kevin O'Connor590e5542009-10-08 22:09:02 -0400632static void
633acpi_build_srat_memory(struct srat_memory_affinity *numamem,
634 u64 base, u64 len, int node, int enabled)
635{
636 numamem->type = SRAT_MEMORY;
637 numamem->length = sizeof(*numamem);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400638 memset(numamem->proximity, 0 ,4);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400639 numamem->proximity[0] = node;
640 numamem->flags = cpu_to_le32(!!enabled);
641 numamem->base_addr_low = base & 0xFFFFFFFF;
642 numamem->base_addr_high = base >> 32;
643 numamem->length_low = len & 0xFFFFFFFF;
644 numamem->length_high = len >> 32;
645}
646
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400647#define SRAT_SIGNATURE 0x54415253 // SRAT
Kevin O'Connor590e5542009-10-08 22:09:02 -0400648static void *
649build_srat(void)
650{
651 int nb_numa_nodes = qemu_cfg_get_numa_nodes();
652
653 if (nb_numa_nodes == 0)
654 return NULL;
655
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400656 u64 *numadata = malloc_tmphigh(sizeof(u64) * (MaxCountCPUs + nb_numa_nodes));
Kevin O'Connor590e5542009-10-08 22:09:02 -0400657 if (!numadata) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500658 warn_noalloc();
Kevin O'Connor590e5542009-10-08 22:09:02 -0400659 return NULL;
660 }
661
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400662 qemu_cfg_get_numa_data(numadata, MaxCountCPUs + nb_numa_nodes);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400663
664 struct system_resource_affinity_table *srat;
665 int srat_size = sizeof(*srat) +
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400666 sizeof(struct srat_processor_affinity) * MaxCountCPUs +
Kevin O'Connor590e5542009-10-08 22:09:02 -0400667 sizeof(struct srat_memory_affinity) * (nb_numa_nodes + 2);
668
669 srat = malloc_high(srat_size);
670 if (!srat) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500671 warn_noalloc();
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900672 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400673 return NULL;
674 }
675
676 memset(srat, 0, srat_size);
677 srat->reserved1=1;
678 struct srat_processor_affinity *core = (void*)(srat + 1);
679 int i;
680 u64 curnode;
681
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400682 for (i = 0; i < MaxCountCPUs; ++i) {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400683 core->type = SRAT_PROCESSOR;
684 core->length = sizeof(*core);
685 core->local_apic_id = i;
686 curnode = *numadata++;
687 core->proximity_lo = curnode;
688 memset(core->proximity_hi, 0, 3);
689 core->local_sapic_eid = 0;
Eduardo Habkost008c1fc2012-07-25 15:45:30 -0300690 if (apic_id_is_present(i))
Kevin O'Connor590e5542009-10-08 22:09:02 -0400691 core->flags = cpu_to_le32(1);
692 else
Eduardo Habkost008c1fc2012-07-25 15:45:30 -0300693 core->flags = cpu_to_le32(0);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400694 core++;
695 }
696
697
698 /* the memory map is a bit tricky, it contains at least one hole
699 * from 640k-1M and possibly another one from 3.5G-4G.
700 */
701 struct srat_memory_affinity *numamem = (void*)core;
702 int slots = 0;
703 u64 mem_len, mem_base, next_base = 0;
704
705 acpi_build_srat_memory(numamem, 0, 640*1024, 0, 1);
706 next_base = 1024 * 1024;
707 numamem++;
708 slots++;
709 for (i = 1; i < nb_numa_nodes + 1; ++i) {
710 mem_base = next_base;
711 mem_len = *numadata++;
712 if (i == 1)
713 mem_len -= 1024 * 1024;
714 next_base = mem_base + mem_len;
715
716 /* Cut out the PCI hole */
717 if (mem_base <= RamSize && next_base > RamSize) {
718 mem_len -= next_base - RamSize;
719 if (mem_len > 0) {
720 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
721 numamem++;
722 slots++;
723 }
724 mem_base = 1ULL << 32;
725 mem_len = next_base - RamSize;
726 next_base += (1ULL << 32) - RamSize;
727 }
728 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
729 numamem++;
730 slots++;
731 }
732 for (; slots < nb_numa_nodes + 2; slots++) {
733 acpi_build_srat_memory(numamem, 0, 0, 0, 0);
734 numamem++;
735 }
736
737 build_header((void*)srat, SRAT_SIGNATURE, srat_size, 1);
738
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900739 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400740 return srat;
741}
742
Isaku Yamahata4c67f902010-07-20 16:37:19 +0900743static const struct pci_device_id acpi_find_tbl[] = {
744 /* PIIX4 Power Management device. */
745 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL),
746
747 PCI_DEVICE_END,
748};
749
Kevin O'Connor2929c352009-07-25 13:48:27 -0400750struct rsdp_descriptor *RsdpAddr;
751
Kevin O'Connora9242a22009-10-07 19:42:07 -0400752#define MAX_ACPI_TABLES 20
Kevin O'Connor2929c352009-07-25 13:48:27 -0400753void
754acpi_bios_init(void)
755{
756 if (! CONFIG_ACPI)
757 return;
758
759 dprintf(3, "init ACPI tables\n");
760
761 // This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400762 struct pci_device *pci = pci_find_init_device(acpi_find_tbl, NULL);
763 if (!pci)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400764 // Device not found
765 return;
766
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400767 // Build ACPI tables
Kevin O'Connora9242a22009-10-07 19:42:07 -0400768 u32 tables[MAX_ACPI_TABLES], tbl_idx = 0;
769
770#define ACPI_INIT_TABLE(X) \
771 do { \
772 tables[tbl_idx] = (u32)(X); \
773 if (tables[tbl_idx]) \
774 tbl_idx++; \
775 } while(0)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400776
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900777 struct fadt_descriptor_rev1 *fadt = build_fadt(pci);
778 ACPI_INIT_TABLE(fadt);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400779 ACPI_INIT_TABLE(build_ssdt());
780 ACPI_INIT_TABLE(build_madt());
Kevin O'Connorf4343772009-10-08 22:05:21 -0400781 ACPI_INIT_TABLE(build_hpet());
Kevin O'Connor590e5542009-10-08 22:09:02 -0400782 ACPI_INIT_TABLE(build_srat());
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200783 ACPI_INIT_TABLE(build_pcihp());
Kevin O'Connor2929c352009-07-25 13:48:27 -0400784
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400785 u16 i, external_tables = qemu_cfg_acpi_additional_tables();
786
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400787 for (i = 0; i < external_tables; i++) {
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400788 u16 len = qemu_cfg_next_acpi_table_len();
789 void *addr = malloc_high(len);
790 if (!addr) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500791 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400792 continue;
793 }
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900794 struct acpi_table_header *header =
795 qemu_cfg_next_acpi_table_load(addr, len);
796 if (header->signature == DSDT_SIGNATURE) {
797 if (fadt) {
798 fill_dsdt(fadt, addr);
799 }
800 } else {
801 ACPI_INIT_TABLE(header);
802 }
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400803 if (tbl_idx == MAX_ACPI_TABLES) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500804 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400805 break;
806 }
807 }
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900808 if (fadt && !fadt->dsdt) {
809 /* default DSDT */
810 void *dsdt = malloc_high(sizeof(AmlCode));
811 if (!dsdt) {
812 warn_noalloc();
813 return;
814 }
815 memcpy(dsdt, AmlCode, sizeof(AmlCode));
816 fill_dsdt(fadt, dsdt);
817 }
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400818
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400819 // Build final rsdt table
Kevin O'Connora9242a22009-10-07 19:42:07 -0400820 struct rsdt_descriptor_rev1 *rsdt;
821 size_t rsdt_len = sizeof(*rsdt) + sizeof(u32) * tbl_idx;
822 rsdt = malloc_high(rsdt_len);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400823 if (!rsdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500824 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400825 return;
826 }
827 memset(rsdt, 0, rsdt_len);
828 memcpy(rsdt->table_offset_entry, tables, sizeof(u32) * tbl_idx);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400829 build_header((void*)rsdt, RSDT_SIGNATURE, rsdt_len, 1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400830
831 // Build rsdp pointer table
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400832 struct rsdp_descriptor *rsdp = malloc_fseg(sizeof(*rsdp));
833 if (!rsdp) {
834 warn_noalloc();
835 return;
836 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400837 memset(rsdp, 0, sizeof(*rsdp));
838 rsdp->signature = RSDP_SIGNATURE;
839 memcpy(rsdp->oem_id, CONFIG_APPNAME6, 6);
840 rsdp->rsdt_physical_address = cpu_to_le32((u32)rsdt);
841 rsdp->checksum -= checksum(rsdp, 20);
842 RsdpAddr = rsdp;
843 dprintf(1, "ACPI tables: RSDP=%p RSDT=%p\n", rsdp, rsdt);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400844}
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500845
846u32
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500847find_resume_vector(void)
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500848{
849 dprintf(4, "rsdp=%p\n", RsdpAddr);
850 if (!RsdpAddr || RsdpAddr->signature != RSDP_SIGNATURE)
851 return 0;
852 struct rsdt_descriptor_rev1 *rsdt = (void*)RsdpAddr->rsdt_physical_address;
853 dprintf(4, "rsdt=%p\n", rsdt);
854 if (!rsdt || rsdt->signature != RSDT_SIGNATURE)
855 return 0;
856 void *end = (void*)rsdt + rsdt->length;
857 int i;
858 for (i=0; (void*)&rsdt->table_offset_entry[i] < end; i++) {
859 struct fadt_descriptor_rev1 *fadt = (void*)rsdt->table_offset_entry[i];
860 if (!fadt || fadt->signature != FACP_SIGNATURE)
861 continue;
862 dprintf(4, "fadt=%p\n", fadt);
863 struct facs_descriptor_rev1 *facs = (void*)fadt->firmware_ctrl;
864 dprintf(4, "facs=%p\n", facs);
865 if (! facs || facs->signature != FACS_SIGNATURE)
866 return 0;
867 // Found it.
868 dprintf(4, "resume addr=%d\n", facs->firmware_waking_vector);
869 return facs->firmware_waking_vector;
870 }
871 return 0;
872}