blob: da3bc57ec7aff5a6d1ba3ab22ce604680159af8a [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'Connor0cd70052011-07-02 14:04:19 -040010#include "pci.h" // pci_find_init_device
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050011#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
12#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor4bc49972012-05-13 22:58:08 -040013#include "ioport.h" // inl
Kevin O'Connorcc6dc462009-10-08 21:18:41 -040014#include "paravirt.h"
Kevin O'Connor276d4a92008-06-11 22:47:01 -040015
16/****************************************************/
17/* ACPI tables init */
18
19/* Table structure from Linux kernel (the ACPI tables are under the
20 BSD license) */
21
Kevin O'Connor276d4a92008-06-11 22:47:01 -040022struct acpi_table_header /* ACPI common table header */
23{
Kevin O'Connord10e4442009-03-01 12:41:20 -050024 ACPI_TABLE_HEADER_DEF
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040025} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040026
27/*
28 * ACPI 1.0 Root System Description Table (RSDT)
29 */
Kevin O'Connor9967ab72008-12-18 21:57:33 -050030#define RSDT_SIGNATURE 0x54445352 // RSDT
Kevin O'Connor276d4a92008-06-11 22:47:01 -040031struct rsdt_descriptor_rev1
32{
Kevin O'Connord10e4442009-03-01 12:41:20 -050033 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
Kevin O'Connora9242a22009-10-07 19:42:07 -040034 u32 table_offset_entry[0]; /* Array of pointers to other */
Kevin O'Connord10e4442009-03-01 12:41:20 -050035 /* ACPI tables */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040036} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040037
38/*
39 * ACPI 1.0 Firmware ACPI Control Structure (FACS)
40 */
Kevin O'Connor9967ab72008-12-18 21:57:33 -050041#define FACS_SIGNATURE 0x53434146 // FACS
Kevin O'Connor276d4a92008-06-11 22:47:01 -040042struct facs_descriptor_rev1
43{
Kevin O'Connord10e4442009-03-01 12:41:20 -050044 u32 signature; /* ACPI Signature */
45 u32 length; /* Length of structure, in bytes */
46 u32 hardware_signature; /* Hardware configuration signature */
47 u32 firmware_waking_vector; /* ACPI OS waking vector */
48 u32 global_lock; /* Global Lock */
49 u32 S4bios_f : 1; /* Indicates if S4BIOS support is present */
50 u32 reserved1 : 31; /* Must be 0 */
51 u8 resverved3 [40]; /* Reserved - must be zero */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040052} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040053
54
55/*
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +090056 * Differentiated System Description Table (DSDT)
57 */
58#define DSDT_SIGNATURE 0x54445344 // DSDT
59
60/*
Kevin O'Connor276d4a92008-06-11 22:47:01 -040061 * MADT values and structures
62 */
63
64/* Values for MADT PCATCompat */
65
66#define DUAL_PIC 0
67#define MULTIPLE_APIC 1
68
69
70/* Master MADT */
71
Kevin O'Connor9967ab72008-12-18 21:57:33 -050072#define APIC_SIGNATURE 0x43495041 // APIC
Kevin O'Connor276d4a92008-06-11 22:47:01 -040073struct multiple_apic_table
74{
Kevin O'Connord10e4442009-03-01 12:41:20 -050075 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
76 u32 local_apic_address; /* Physical address of local APIC */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040077#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -050078 u32 PCATcompat : 1; /* A one indicates system also has dual 8259s */
79 u32 reserved1 : 31;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040080#else
Kevin O'Connord10e4442009-03-01 12:41:20 -050081 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040082#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040083} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040084
85
Kevin O'Connor590e5542009-10-08 22:09:02 -040086/* Values for Type in APIC sub-headers */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040087
88#define APIC_PROCESSOR 0
89#define APIC_IO 1
90#define APIC_XRUPT_OVERRIDE 2
91#define APIC_NMI 3
92#define APIC_LOCAL_NMI 4
93#define APIC_ADDRESS_OVERRIDE 5
94#define APIC_IO_SAPIC 6
95#define APIC_LOCAL_SAPIC 7
96#define APIC_XRUPT_SOURCE 8
97#define APIC_RESERVED 9 /* 9 and greater are reserved */
98
99/*
100 * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
101 */
Kevin O'Connor590e5542009-10-08 22:09:02 -0400102#define ACPI_SUB_HEADER_DEF /* Common ACPI sub-structure header */\
Kevin O'Connord10e4442009-03-01 12:41:20 -0500103 u8 type; \
104 u8 length;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400105
106/* Sub-structures for MADT */
107
108struct madt_processor_apic
109{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400110 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500111 u8 processor_id; /* ACPI processor id */
112 u8 local_apic_id; /* Processor's local APIC id */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400113#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -0500114 u32 processor_enabled: 1; /* Processor is usable if set */
115 u32 reserved2 : 31; /* Reserved, must be zero */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400116#else
Kevin O'Connord10e4442009-03-01 12:41:20 -0500117 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400118#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400119} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400120
121struct madt_io_apic
122{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400123 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500124 u8 io_apic_id; /* I/O APIC ID */
125 u8 reserved; /* Reserved - must be zero */
126 u32 address; /* APIC physical address */
127 u32 interrupt; /* Global system interrupt where INTI
128 * lines start */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400129} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400130
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500131/* IRQs 5,9,10,11 */
132#define PCI_ISA_IRQ_MASK 0x0e20
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500133
134struct madt_intsrcovr {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400135 ACPI_SUB_HEADER_DEF
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500136 u8 bus;
137 u8 source;
138 u32 gsi;
139 u16 flags;
140} PACKED;
141
Kenji Kaneshige70986382011-10-10 14:06:17 +0800142struct madt_local_nmi {
143 ACPI_SUB_HEADER_DEF
144 u8 processor_id; /* ACPI processor id */
145 u16 flags; /* MPS INTI flags */
146 u8 lint; /* Local APIC LINT# */
147} PACKED;
148
149
Kevin O'Connorf4343772009-10-08 22:05:21 -0400150/*
151 * ACPI 2.0 Generic Address Space definition.
152 */
153struct acpi_20_generic_address {
154 u8 address_space_id;
155 u8 register_bit_width;
156 u8 register_bit_offset;
157 u8 reserved;
158 u64 address;
159} PACKED;
160
161/*
162 * HPET Description Table
163 */
164struct acpi_20_hpet {
165 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
166 u32 timer_block_id;
167 struct acpi_20_generic_address addr;
168 u8 hpet_number;
169 u16 min_tick;
170 u8 page_protect;
171} PACKED;
Kevin O'Connorf4343772009-10-08 22:05:21 -0400172
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200173#define HPET_ID 0x000
174#define HPET_PERIOD 0x004
175
Kevin O'Connor590e5542009-10-08 22:09:02 -0400176/*
177 * SRAT (NUMA topology description) table
178 */
179
180#define SRAT_PROCESSOR 0
181#define SRAT_MEMORY 1
182
183struct system_resource_affinity_table
184{
185 ACPI_TABLE_HEADER_DEF
186 u32 reserved1;
187 u32 reserved2[2];
188} PACKED;
189
190struct srat_processor_affinity
191{
192 ACPI_SUB_HEADER_DEF
193 u8 proximity_lo;
194 u8 local_apic_id;
195 u32 flags;
196 u8 local_sapic_eid;
197 u8 proximity_hi[3];
198 u32 reserved;
199} PACKED;
200
201struct srat_memory_affinity
202{
203 ACPI_SUB_HEADER_DEF
204 u8 proximity[4];
205 u16 reserved1;
206 u32 base_addr_low,base_addr_high;
207 u32 length_low,length_high;
208 u32 reserved2;
209 u32 flags;
210 u32 reserved3[2];
211} PACKED;
212
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400213#include "acpi-dsdt.hex"
214
Kevin O'Connor2929c352009-07-25 13:48:27 -0400215static void
Kevin O'Connora9242a22009-10-07 19:42:07 -0400216build_header(struct acpi_table_header *h, u32 sig, int len, u8 rev)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400217{
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500218 h->signature = sig;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400219 h->length = cpu_to_le32(len);
220 h->revision = rev;
Kevin O'Connor6cb8ba92008-08-17 11:03:24 -0400221 memcpy(h->oem_id, CONFIG_APPNAME6, 6);
222 memcpy(h->oem_table_id, CONFIG_APPNAME4, 4);
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500223 memcpy(h->oem_table_id + 4, (void*)&sig, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400224 h->oem_revision = cpu_to_le32(1);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400225 memcpy(h->asl_compiler_id, CONFIG_APPNAME4, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400226 h->asl_compiler_revision = cpu_to_le32(1);
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400227 h->checksum -= checksum(h, len);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400228}
229
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400230#define PIIX4_ACPI_ENABLE 0xf1
231#define PIIX4_ACPI_DISABLE 0xf0
232#define PIIX4_GPE0_BLK 0xafe0
233#define PIIX4_GPE0_BLK_LEN 4
234
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400235static void piix4_fadt_init(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400236{
237 struct fadt_descriptor_rev1 *fadt = arg;
238 fadt->acpi_enable = PIIX4_ACPI_ENABLE;
239 fadt->acpi_disable = PIIX4_ACPI_DISABLE;
240 fadt->gpe0_blk = cpu_to_le32(PIIX4_GPE0_BLK);
241 fadt->gpe0_blk_len = PIIX4_GPE0_BLK_LEN;
242}
243
Isaku Yamahatafe54a532010-07-20 16:37:18 +0900244static const struct pci_device_id fadt_init_tbl[] = {
245 /* PIIX4 Power Management device (for ACPI) */
246 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
247 piix4_fadt_init),
248
249 PCI_DEVICE_END
250};
251
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900252static void fill_dsdt(struct fadt_descriptor_rev1 *fadt, void *dsdt)
253{
254 if (fadt->dsdt) {
255 free((void *)le32_to_cpu(fadt->dsdt));
256 }
257 fadt->dsdt = cpu_to_le32((u32)dsdt);
258 fadt->checksum -= checksum(fadt, sizeof(*fadt));
259 dprintf(1, "ACPI DSDT=%p\n", dsdt);
260}
261
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400262static void *
263build_fadt(struct pci_device *pci)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400264{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400265 struct fadt_descriptor_rev1 *fadt = malloc_high(sizeof(*fadt));
Kevin O'Connor415d4292009-08-30 19:19:31 -0400266 struct facs_descriptor_rev1 *facs = memalign_high(64, sizeof(*facs));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400267
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900268 if (!fadt || !facs) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500269 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400270 return NULL;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400271 }
272
Kevin O'Connor2929c352009-07-25 13:48:27 -0400273 /* FACS */
Kevin O'Connor2929c352009-07-25 13:48:27 -0400274 memset(facs, 0, sizeof(*facs));
275 facs->signature = FACS_SIGNATURE;
276 facs->length = cpu_to_le32(sizeof(*facs));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400277
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400278 /* FADT */
279 memset(fadt, 0, sizeof(*fadt));
Kevin O'Connor2929c352009-07-25 13:48:27 -0400280 fadt->firmware_ctrl = cpu_to_le32((u32)facs);
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900281 fadt->dsdt = 0; /* dsdt will be filled later in acpi_bios_init()
282 by fill_dsdt() */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400283 fadt->model = 1;
284 fadt->reserved1 = 0;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400285 int pm_sci_int = pci_config_readb(pci->bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400286 fadt->sci_int = cpu_to_le16(pm_sci_int);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500287 fadt->smi_cmd = cpu_to_le32(PORT_SMI_CMD);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500288 fadt->pm1a_evt_blk = cpu_to_le32(PORT_ACPI_PM_BASE);
289 fadt->pm1a_cnt_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x04);
290 fadt->pm_tmr_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x08);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400291 fadt->pm1_evt_len = 4;
292 fadt->pm1_cnt_len = 2;
293 fadt->pm_tmr_len = 4;
294 fadt->plvl2_lat = cpu_to_le16(0xfff); // C2 state not supported
295 fadt->plvl3_lat = cpu_to_le16(0xfff); // C3 state not supported
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400296 pci_init_device(fadt_init_tbl, pci, fadt);
Kevin O'Connorafdeda02012-07-20 02:42:08 -0400297 /* WBINVD + PROC_C1 + SLP_BUTTON + RTC_S4 + USE_PLATFORM_CLOCK */
298 fadt->flags = cpu_to_le32((1 << 0) | (1 << 2) | (1 << 5) | (1 << 7) |
Marcelo Tosattia4e2b9a2012-07-17 14:18:00 -0300299 (1 << 15));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400300
Kevin O'Connora9242a22009-10-07 19:42:07 -0400301 build_header((void*)fadt, FACP_SIGNATURE, sizeof(*fadt), 1);
302
303 return fadt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400304}
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400305
Kevin O'Connora9242a22009-10-07 19:42:07 -0400306static void*
307build_madt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400308{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400309 int madt_size = (sizeof(struct multiple_apic_table)
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400310 + sizeof(struct madt_processor_apic) * MaxCountCPUs
Kevin O'Connor2929c352009-07-25 13:48:27 -0400311 + sizeof(struct madt_io_apic)
Kenji Kaneshige70986382011-10-10 14:06:17 +0800312 + sizeof(struct madt_intsrcovr) * 16
313 + sizeof(struct madt_local_nmi));
314
Kevin O'Connor2929c352009-07-25 13:48:27 -0400315 struct multiple_apic_table *madt = malloc_high(madt_size);
316 if (!madt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500317 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400318 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400319 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500320 memset(madt, 0, madt_size);
321 madt->local_apic_address = cpu_to_le32(BUILD_APIC_ADDR);
322 madt->flags = cpu_to_le32(1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400323 struct madt_processor_apic *apic = (void*)&madt[1];
324 int i;
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400325 for (i=0; i<MaxCountCPUs; i++) {
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500326 apic->type = APIC_PROCESSOR;
327 apic->length = sizeof(*apic);
328 apic->processor_id = i;
329 apic->local_apic_id = i;
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400330 if (i < CountCPUs)
331 apic->flags = cpu_to_le32(1);
332 else
333 apic->flags = cpu_to_le32(0);
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500334 apic++;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400335 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400336 struct madt_io_apic *io_apic = (void*)apic;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500337 io_apic->type = APIC_IO;
338 io_apic->length = sizeof(*io_apic);
Eduardo Habkoste39b9382012-07-25 15:45:29 -0300339 io_apic->io_apic_id = BUILD_IOAPIC_ID;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500340 io_apic->address = cpu_to_le32(BUILD_IOAPIC_ADDR);
341 io_apic->interrupt = cpu_to_le32(0);
342
Kevin O'Connord10e4442009-03-01 12:41:20 -0500343 struct madt_intsrcovr *intsrcovr = (void*)&io_apic[1];
Kevin O'Connor4d2b6192009-10-08 21:37:21 -0400344 if (qemu_cfg_irq0_override()) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400345 memset(intsrcovr, 0, sizeof(*intsrcovr));
346 intsrcovr->type = APIC_XRUPT_OVERRIDE;
347 intsrcovr->length = sizeof(*intsrcovr);
348 intsrcovr->source = 0;
349 intsrcovr->gsi = 2;
350 intsrcovr->flags = 0; /* conforms to bus specifications */
351 intsrcovr++;
352 }
353 for (i = 1; i < 16; i++) {
Kevin O'Connord10e4442009-03-01 12:41:20 -0500354 if (!(PCI_ISA_IRQ_MASK & (1 << i)))
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500355 /* No need for a INT source override structure. */
356 continue;
Kevin O'Connord10e4442009-03-01 12:41:20 -0500357 memset(intsrcovr, 0, sizeof(*intsrcovr));
358 intsrcovr->type = APIC_XRUPT_OVERRIDE;
359 intsrcovr->length = sizeof(*intsrcovr);
360 intsrcovr->source = i;
361 intsrcovr->gsi = i;
362 intsrcovr->flags = 0xd; /* active high, level triggered */
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500363 intsrcovr++;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500364 }
365
Kenji Kaneshige70986382011-10-10 14:06:17 +0800366 struct madt_local_nmi *local_nmi = (void*)intsrcovr;
367 local_nmi->type = APIC_LOCAL_NMI;
368 local_nmi->length = sizeof(*local_nmi);
369 local_nmi->processor_id = 0xff; /* all processors */
370 local_nmi->flags = 0;
371 local_nmi->lint = 1; /* LINT1 */
372 local_nmi++;
373
374 build_header((void*)madt, APIC_SIGNATURE, (void*)local_nmi - (void*)madt, 1);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400375 return madt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400376}
377
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400378// Encode a hex value
379static inline char getHex(u32 val) {
380 val &= 0x0f;
381 return (val <= 9) ? ('0' + val) : ('A' + val - 10);
382}
383
384// Encode a length in an SSDT.
385static u8 *
386encodeLen(u8 *ssdt_ptr, int length, int bytes)
387{
388 switch (bytes) {
389 default:
390 case 4: ssdt_ptr[3] = ((length >> 20) & 0xff);
391 case 3: ssdt_ptr[2] = ((length >> 12) & 0xff);
392 case 2: ssdt_ptr[1] = ((length >> 4) & 0xff);
393 ssdt_ptr[0] = (((bytes-1) & 0x3) << 6) | (length & 0x0f);
394 break;
395 case 1: ssdt_ptr[0] = length & 0x3f;
396 }
397 return ssdt_ptr + bytes;
398}
399
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200400#include "ssdt-proc.hex"
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200401
402/* 0x5B 0x83 ProcessorOp PkgLength NameString ProcID */
403#define SD_OFFSET_CPUHEX (*ssdt_proc_name - *ssdt_proc_start + 2)
404#define SD_OFFSET_CPUID1 (*ssdt_proc_name - *ssdt_proc_start + 4)
405#define SD_OFFSET_CPUID2 (*ssdt_proc_id - *ssdt_proc_start)
406#define SD_SIZEOF (*ssdt_proc_end - *ssdt_proc_start)
407#define SD_PROC (ssdp_proc_aml + *ssdt_proc_start)
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400408
Kevin O'Connor2929c352009-07-25 13:48:27 -0400409#define SSDT_SIGNATURE 0x54445353 // SSDT
Michael S. Tsirkinc9737882011-11-20 19:56:59 +0200410
Kevin O'Connora9242a22009-10-07 19:42:07 -0400411static void*
412build_ssdt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400413{
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400414 int acpi_cpus = MaxCountCPUs > 0xff ? 0xff : MaxCountCPUs;
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400415 // length = ScopeOp + procs + NTYF method + CPON package
416 int length = ((1+3+4)
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200417 + (acpi_cpus * SD_SIZEOF)
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400418 + (1+2+5+(12*acpi_cpus))
Gerd Hoffmann2062f2b2012-06-07 10:34:33 +0200419 + (6+2+1+(1*acpi_cpus))
420 + 17);
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400421 u8 *ssdt = malloc_high(sizeof(struct acpi_table_header) + length);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400422 if (! ssdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500423 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400424 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400425 }
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400426 u8 *ssdt_ptr = ssdt + sizeof(struct acpi_table_header);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400427
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400428 // build Scope(_SB_) header
Kevin O'Connor2929c352009-07-25 13:48:27 -0400429 *(ssdt_ptr++) = 0x10; // ScopeOp
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400430 ssdt_ptr = encodeLen(ssdt_ptr, length-1, 3);
431 *(ssdt_ptr++) = '_';
432 *(ssdt_ptr++) = 'S';
433 *(ssdt_ptr++) = 'B';
Kevin O'Connor2929c352009-07-25 13:48:27 -0400434 *(ssdt_ptr++) = '_';
435
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400436 // build Processor object for each processor
Kevin O'Connor2929c352009-07-25 13:48:27 -0400437 int i;
438 for (i=0; i<acpi_cpus; i++) {
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200439 memcpy(ssdt_ptr, SD_PROC, SD_SIZEOF);
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400440 ssdt_ptr[SD_OFFSET_CPUHEX] = getHex(i >> 4);
441 ssdt_ptr[SD_OFFSET_CPUHEX+1] = getHex(i);
442 ssdt_ptr[SD_OFFSET_CPUID1] = i;
443 ssdt_ptr[SD_OFFSET_CPUID2] = i;
Michael S. Tsirkinc736f0a2011-10-04 15:26:19 +0200444 ssdt_ptr += SD_SIZEOF;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400445 }
446
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400447 // build "Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}"
448 *(ssdt_ptr++) = 0x14; // MethodOp
449 ssdt_ptr = encodeLen(ssdt_ptr, 2+5+(12*acpi_cpus), 2);
450 *(ssdt_ptr++) = 'N';
451 *(ssdt_ptr++) = 'T';
452 *(ssdt_ptr++) = 'F';
453 *(ssdt_ptr++) = 'Y';
454 *(ssdt_ptr++) = 0x02;
455 for (i=0; i<acpi_cpus; i++) {
456 *(ssdt_ptr++) = 0xA0; // IfOp
457 ssdt_ptr = encodeLen(ssdt_ptr, 11, 1);
458 *(ssdt_ptr++) = 0x93; // LEqualOp
459 *(ssdt_ptr++) = 0x68; // Arg0Op
460 *(ssdt_ptr++) = 0x0A; // BytePrefix
461 *(ssdt_ptr++) = i;
462 *(ssdt_ptr++) = 0x86; // NotifyOp
463 *(ssdt_ptr++) = 'C';
464 *(ssdt_ptr++) = 'P';
465 *(ssdt_ptr++) = getHex(i >> 4);
466 *(ssdt_ptr++) = getHex(i);
467 *(ssdt_ptr++) = 0x69; // Arg1Op
468 }
469
470 // build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
471 *(ssdt_ptr++) = 0x08; // NameOp
472 *(ssdt_ptr++) = 'C';
473 *(ssdt_ptr++) = 'P';
474 *(ssdt_ptr++) = 'O';
475 *(ssdt_ptr++) = 'N';
476 *(ssdt_ptr++) = 0x12; // PackageOp
477 ssdt_ptr = encodeLen(ssdt_ptr, 2+1+(1*acpi_cpus), 2);
478 *(ssdt_ptr++) = acpi_cpus;
479 for (i=0; i<acpi_cpus; i++)
480 *(ssdt_ptr++) = (i < CountCPUs) ? 0x01 : 0x00;
481
Gerd Hoffmann2062f2b2012-06-07 10:34:33 +0200482 // store pci io windows: start, end, length
483 // this way we don't have to do the math in the dsdt
484 struct bfld *bfld = malloc_high(sizeof(struct bfld));
485 bfld->p0s = pcimem_start;
486 bfld->p0e = pcimem_end - 1;
487 bfld->p0l = pcimem_end - pcimem_start;
488 bfld->p1s = pcimem64_start;
489 bfld->p1e = pcimem64_end - 1;
490 bfld->p1l = pcimem64_end - pcimem64_start;
491
492 // build "OperationRegion(BDAT, SystemMemory, 0x12345678, 0x87654321)"
493 *(ssdt_ptr++) = 0x5B; // ExtOpPrefix
494 *(ssdt_ptr++) = 0x80; // OpRegionOp
495 *(ssdt_ptr++) = 'B';
496 *(ssdt_ptr++) = 'D';
497 *(ssdt_ptr++) = 'A';
498 *(ssdt_ptr++) = 'T';
499 *(ssdt_ptr++) = 0x00; // SystemMemory
500 *(ssdt_ptr++) = 0x0C; // DWordPrefix
501 *(u32*)ssdt_ptr = (u32)bfld;
502 ssdt_ptr += 4;
503 *(ssdt_ptr++) = 0x0C; // DWordPrefix
504 *(u32*)ssdt_ptr = sizeof(struct bfld);
505 ssdt_ptr += 4;
506
Kevin O'Connora9242a22009-10-07 19:42:07 -0400507 build_header((void*)ssdt, SSDT_SIGNATURE, ssdt_ptr - ssdt, 1);
508
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400509 //hexdump(ssdt, ssdt_ptr - ssdt);
510
Kevin O'Connora9242a22009-10-07 19:42:07 -0400511 return ssdt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400512}
513
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200514#include "ssdt-pcihp.hex"
515
516#define PCI_RMV_BASE 0xae0c
517
518extern void link_time_assertion(void);
519
520static void* build_pcihp(void)
Michael S. Tsirkinc9737882011-11-20 19:56:59 +0200521{
Gleb Natapovd51c4a02012-05-20 12:03:40 +0300522 char *sys_states;
523 int sys_state_size;
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200524 u32 rmvc_pcrm;
525 int i;
526
527 u8 *ssdt = malloc_high(sizeof ssdp_pcihp_aml);
Kevin O'Connor890d9852012-02-15 20:13:05 -0500528 if (!ssdt) {
529 warn_noalloc();
530 return NULL;
531 }
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200532 memcpy(ssdt, ssdp_pcihp_aml, sizeof ssdp_pcihp_aml);
533
534 /* Runtime patching of EJ0: to disable hotplug for a slot,
535 * replace the method name: _EJ0 by EJ0_. */
536 if (ARRAY_SIZE(aml_ej0_name) != ARRAY_SIZE(aml_adr_dword)) {
537 link_time_assertion();
538 }
539
540 rmvc_pcrm = inl(PCI_RMV_BASE);
541 for (i = 0; i < ARRAY_SIZE(aml_ej0_name); ++i) {
542 /* Slot is in byte 2 in _ADR */
543 u8 slot = ssdp_pcihp_aml[aml_adr_dword[i] + 2] & 0x1F;
Kevin O'Connor7a08ae72012-03-14 21:11:39 -0400544 /* Sanity check */
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200545 if (memcmp(ssdp_pcihp_aml + aml_ej0_name[i], "_EJ0", 4)) {
546 warn_internalerror();
547 free(ssdt);
548 return NULL;
549 }
550 if (!(rmvc_pcrm & (0x1 << slot))) {
551 memcpy(ssdt + aml_ej0_name[i], "EJ0_", 4);
552 }
553 }
554
Gleb Natapovd51c4a02012-05-20 12:03:40 +0300555 sys_states = romfile_loadfile("etc/system-states", &sys_state_size);
556 if (!sys_states || sys_state_size != 6)
557 sys_states = (char[]){128, 0, 0, 129, 128, 128};
558
559 if (!(sys_states[3] & 128))
560 ssdt[acpi_s3_name[0]] = 'X';
561 if (!(sys_states[4] & 128))
562 ssdt[acpi_s4_name[0]] = 'X';
563 else
564 ssdt[acpi_s4_pkg[0] + 1] = ssdt[acpi_s4_pkg[0] + 3] = sys_states[4] & 127;
565 ((struct acpi_table_header*)ssdt)->checksum = 0;
566 ((struct acpi_table_header*)ssdt)->checksum -= checksum(ssdt, sizeof(ssdp_pcihp_aml));
567
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200568 return ssdt;
Michael S. Tsirkinc9737882011-11-20 19:56:59 +0200569}
570
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400571#define HPET_SIGNATURE 0x54455048 // HPET
Kevin O'Connorf4343772009-10-08 22:05:21 -0400572static void*
573build_hpet(void)
574{
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200575 struct acpi_20_hpet *hpet;
Kevin O'Connor7859eda2011-09-20 19:40:28 -0400576 const void *hpet_base = (void *)BUILD_HPET_ADDRESS;
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200577 u32 hpet_vendor = readl(hpet_base + HPET_ID) >> 16;
578 u32 hpet_period = readl(hpet_base + HPET_PERIOD);
579
580 if (hpet_vendor == 0 || hpet_vendor == 0xffff ||
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400581 hpet_period == 0 || hpet_period > 100000000)
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200582 return NULL;
583
584 hpet = malloc_high(sizeof(*hpet));
Kevin O'Connorf4343772009-10-08 22:05:21 -0400585 if (!hpet) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500586 warn_noalloc();
Kevin O'Connorf4343772009-10-08 22:05:21 -0400587 return NULL;
588 }
589
590 memset(hpet, 0, sizeof(*hpet));
591 /* Note timer_block_id value must be kept in sync with value advertised by
592 * emulated hpet
593 */
594 hpet->timer_block_id = cpu_to_le32(0x8086a201);
Kevin O'Connor7859eda2011-09-20 19:40:28 -0400595 hpet->addr.address = cpu_to_le32(BUILD_HPET_ADDRESS);
Kevin O'Connorf4343772009-10-08 22:05:21 -0400596 build_header((void*)hpet, HPET_SIGNATURE, sizeof(*hpet), 1);
597
598 return hpet;
599}
600
Kevin O'Connor590e5542009-10-08 22:09:02 -0400601static void
602acpi_build_srat_memory(struct srat_memory_affinity *numamem,
603 u64 base, u64 len, int node, int enabled)
604{
605 numamem->type = SRAT_MEMORY;
606 numamem->length = sizeof(*numamem);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400607 memset(numamem->proximity, 0 ,4);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400608 numamem->proximity[0] = node;
609 numamem->flags = cpu_to_le32(!!enabled);
610 numamem->base_addr_low = base & 0xFFFFFFFF;
611 numamem->base_addr_high = base >> 32;
612 numamem->length_low = len & 0xFFFFFFFF;
613 numamem->length_high = len >> 32;
614}
615
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400616#define SRAT_SIGNATURE 0x54415253 // SRAT
Kevin O'Connor590e5542009-10-08 22:09:02 -0400617static void *
618build_srat(void)
619{
620 int nb_numa_nodes = qemu_cfg_get_numa_nodes();
621
622 if (nb_numa_nodes == 0)
623 return NULL;
624
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400625 u64 *numadata = malloc_tmphigh(sizeof(u64) * (MaxCountCPUs + nb_numa_nodes));
Kevin O'Connor590e5542009-10-08 22:09:02 -0400626 if (!numadata) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500627 warn_noalloc();
Kevin O'Connor590e5542009-10-08 22:09:02 -0400628 return NULL;
629 }
630
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400631 qemu_cfg_get_numa_data(numadata, MaxCountCPUs + nb_numa_nodes);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400632
633 struct system_resource_affinity_table *srat;
634 int srat_size = sizeof(*srat) +
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400635 sizeof(struct srat_processor_affinity) * MaxCountCPUs +
Kevin O'Connor590e5542009-10-08 22:09:02 -0400636 sizeof(struct srat_memory_affinity) * (nb_numa_nodes + 2);
637
638 srat = malloc_high(srat_size);
639 if (!srat) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500640 warn_noalloc();
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900641 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400642 return NULL;
643 }
644
645 memset(srat, 0, srat_size);
646 srat->reserved1=1;
647 struct srat_processor_affinity *core = (void*)(srat + 1);
648 int i;
649 u64 curnode;
650
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400651 for (i = 0; i < MaxCountCPUs; ++i) {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400652 core->type = SRAT_PROCESSOR;
653 core->length = sizeof(*core);
654 core->local_apic_id = i;
655 curnode = *numadata++;
656 core->proximity_lo = curnode;
657 memset(core->proximity_hi, 0, 3);
658 core->local_sapic_eid = 0;
659 if (i < CountCPUs)
660 core->flags = cpu_to_le32(1);
661 else
662 core->flags = 0;
663 core++;
664 }
665
666
667 /* the memory map is a bit tricky, it contains at least one hole
668 * from 640k-1M and possibly another one from 3.5G-4G.
669 */
670 struct srat_memory_affinity *numamem = (void*)core;
671 int slots = 0;
672 u64 mem_len, mem_base, next_base = 0;
673
674 acpi_build_srat_memory(numamem, 0, 640*1024, 0, 1);
675 next_base = 1024 * 1024;
676 numamem++;
677 slots++;
678 for (i = 1; i < nb_numa_nodes + 1; ++i) {
679 mem_base = next_base;
680 mem_len = *numadata++;
681 if (i == 1)
682 mem_len -= 1024 * 1024;
683 next_base = mem_base + mem_len;
684
685 /* Cut out the PCI hole */
686 if (mem_base <= RamSize && next_base > RamSize) {
687 mem_len -= next_base - RamSize;
688 if (mem_len > 0) {
689 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
690 numamem++;
691 slots++;
692 }
693 mem_base = 1ULL << 32;
694 mem_len = next_base - RamSize;
695 next_base += (1ULL << 32) - RamSize;
696 }
697 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
698 numamem++;
699 slots++;
700 }
701 for (; slots < nb_numa_nodes + 2; slots++) {
702 acpi_build_srat_memory(numamem, 0, 0, 0, 0);
703 numamem++;
704 }
705
706 build_header((void*)srat, SRAT_SIGNATURE, srat_size, 1);
707
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900708 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400709 return srat;
710}
711
Isaku Yamahata4c67f902010-07-20 16:37:19 +0900712static const struct pci_device_id acpi_find_tbl[] = {
713 /* PIIX4 Power Management device. */
714 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL),
715
716 PCI_DEVICE_END,
717};
718
Kevin O'Connor2929c352009-07-25 13:48:27 -0400719struct rsdp_descriptor *RsdpAddr;
720
Kevin O'Connora9242a22009-10-07 19:42:07 -0400721#define MAX_ACPI_TABLES 20
Kevin O'Connor2929c352009-07-25 13:48:27 -0400722void
723acpi_bios_init(void)
724{
725 if (! CONFIG_ACPI)
726 return;
727
728 dprintf(3, "init ACPI tables\n");
729
730 // This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400731 struct pci_device *pci = pci_find_init_device(acpi_find_tbl, NULL);
732 if (!pci)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400733 // Device not found
734 return;
735
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400736 // Build ACPI tables
Kevin O'Connora9242a22009-10-07 19:42:07 -0400737 u32 tables[MAX_ACPI_TABLES], tbl_idx = 0;
738
739#define ACPI_INIT_TABLE(X) \
740 do { \
741 tables[tbl_idx] = (u32)(X); \
742 if (tables[tbl_idx]) \
743 tbl_idx++; \
744 } while(0)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400745
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900746 struct fadt_descriptor_rev1 *fadt = build_fadt(pci);
747 ACPI_INIT_TABLE(fadt);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400748 ACPI_INIT_TABLE(build_ssdt());
749 ACPI_INIT_TABLE(build_madt());
Kevin O'Connorf4343772009-10-08 22:05:21 -0400750 ACPI_INIT_TABLE(build_hpet());
Kevin O'Connor590e5542009-10-08 22:09:02 -0400751 ACPI_INIT_TABLE(build_srat());
Michael S. Tsirkin5a73f3b2011-11-20 19:57:05 +0200752 ACPI_INIT_TABLE(build_pcihp());
Kevin O'Connor2929c352009-07-25 13:48:27 -0400753
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400754 u16 i, external_tables = qemu_cfg_acpi_additional_tables();
755
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400756 for (i = 0; i < external_tables; i++) {
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400757 u16 len = qemu_cfg_next_acpi_table_len();
758 void *addr = malloc_high(len);
759 if (!addr) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500760 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400761 continue;
762 }
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900763 struct acpi_table_header *header =
764 qemu_cfg_next_acpi_table_load(addr, len);
765 if (header->signature == DSDT_SIGNATURE) {
766 if (fadt) {
767 fill_dsdt(fadt, addr);
768 }
769 } else {
770 ACPI_INIT_TABLE(header);
771 }
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400772 if (tbl_idx == MAX_ACPI_TABLES) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500773 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400774 break;
775 }
776 }
Isaku Yamahataf2fd79b2011-10-24 17:11:08 +0900777 if (fadt && !fadt->dsdt) {
778 /* default DSDT */
779 void *dsdt = malloc_high(sizeof(AmlCode));
780 if (!dsdt) {
781 warn_noalloc();
782 return;
783 }
784 memcpy(dsdt, AmlCode, sizeof(AmlCode));
785 fill_dsdt(fadt, dsdt);
786 }
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400787
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400788 // Build final rsdt table
Kevin O'Connora9242a22009-10-07 19:42:07 -0400789 struct rsdt_descriptor_rev1 *rsdt;
790 size_t rsdt_len = sizeof(*rsdt) + sizeof(u32) * tbl_idx;
791 rsdt = malloc_high(rsdt_len);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400792 if (!rsdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500793 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400794 return;
795 }
796 memset(rsdt, 0, rsdt_len);
797 memcpy(rsdt->table_offset_entry, tables, sizeof(u32) * tbl_idx);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400798 build_header((void*)rsdt, RSDT_SIGNATURE, rsdt_len, 1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400799
800 // Build rsdp pointer table
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400801 struct rsdp_descriptor *rsdp = malloc_fseg(sizeof(*rsdp));
802 if (!rsdp) {
803 warn_noalloc();
804 return;
805 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400806 memset(rsdp, 0, sizeof(*rsdp));
807 rsdp->signature = RSDP_SIGNATURE;
808 memcpy(rsdp->oem_id, CONFIG_APPNAME6, 6);
809 rsdp->rsdt_physical_address = cpu_to_le32((u32)rsdt);
810 rsdp->checksum -= checksum(rsdp, 20);
811 RsdpAddr = rsdp;
812 dprintf(1, "ACPI tables: RSDP=%p RSDT=%p\n", rsdp, rsdt);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400813}
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500814
815u32
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500816find_resume_vector(void)
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500817{
818 dprintf(4, "rsdp=%p\n", RsdpAddr);
819 if (!RsdpAddr || RsdpAddr->signature != RSDP_SIGNATURE)
820 return 0;
821 struct rsdt_descriptor_rev1 *rsdt = (void*)RsdpAddr->rsdt_physical_address;
822 dprintf(4, "rsdt=%p\n", rsdt);
823 if (!rsdt || rsdt->signature != RSDT_SIGNATURE)
824 return 0;
825 void *end = (void*)rsdt + rsdt->length;
826 int i;
827 for (i=0; (void*)&rsdt->table_offset_entry[i] < end; i++) {
828 struct fadt_descriptor_rev1 *fadt = (void*)rsdt->table_offset_entry[i];
829 if (!fadt || fadt->signature != FACP_SIGNATURE)
830 continue;
831 dprintf(4, "fadt=%p\n", fadt);
832 struct facs_descriptor_rev1 *facs = (void*)fadt->firmware_ctrl;
833 dprintf(4, "facs=%p\n", facs);
834 if (! facs || facs->signature != FACS_SIGNATURE)
835 return 0;
836 // Found it.
837 dprintf(4, "resume addr=%d\n", facs->firmware_waking_vector);
838 return facs->firmware_waking_vector;
839 }
840 return 0;
841}