blob: 6bb6ff632e819daafaab0ede748bb6550b119835 [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'Connor9521e262008-07-04 13:04:29 -040011#include "biosvar.h" // GET_EBDA
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'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/*
Kevin O'Connor276d4a92008-06-11 22:47:01 -040056 * MADT values and structures
57 */
58
59/* Values for MADT PCATCompat */
60
61#define DUAL_PIC 0
62#define MULTIPLE_APIC 1
63
64
65/* Master MADT */
66
Kevin O'Connor9967ab72008-12-18 21:57:33 -050067#define APIC_SIGNATURE 0x43495041 // APIC
Kevin O'Connor276d4a92008-06-11 22:47:01 -040068struct multiple_apic_table
69{
Kevin O'Connord10e4442009-03-01 12:41:20 -050070 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
71 u32 local_apic_address; /* Physical address of local APIC */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040072#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -050073 u32 PCATcompat : 1; /* A one indicates system also has dual 8259s */
74 u32 reserved1 : 31;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040075#else
Kevin O'Connord10e4442009-03-01 12:41:20 -050076 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040077#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040078} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -040079
80
Kevin O'Connor590e5542009-10-08 22:09:02 -040081/* Values for Type in APIC sub-headers */
Kevin O'Connor276d4a92008-06-11 22:47:01 -040082
83#define APIC_PROCESSOR 0
84#define APIC_IO 1
85#define APIC_XRUPT_OVERRIDE 2
86#define APIC_NMI 3
87#define APIC_LOCAL_NMI 4
88#define APIC_ADDRESS_OVERRIDE 5
89#define APIC_IO_SAPIC 6
90#define APIC_LOCAL_SAPIC 7
91#define APIC_XRUPT_SOURCE 8
92#define APIC_RESERVED 9 /* 9 and greater are reserved */
93
94/*
95 * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
96 */
Kevin O'Connor590e5542009-10-08 22:09:02 -040097#define ACPI_SUB_HEADER_DEF /* Common ACPI sub-structure header */\
Kevin O'Connord10e4442009-03-01 12:41:20 -050098 u8 type; \
99 u8 length;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400100
101/* Sub-structures for MADT */
102
103struct madt_processor_apic
104{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400105 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500106 u8 processor_id; /* ACPI processor id */
107 u8 local_apic_id; /* Processor's local APIC id */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400108#if 0
Kevin O'Connord10e4442009-03-01 12:41:20 -0500109 u32 processor_enabled: 1; /* Processor is usable if set */
110 u32 reserved2 : 31; /* Reserved, must be zero */
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400111#else
Kevin O'Connord10e4442009-03-01 12:41:20 -0500112 u32 flags;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400113#endif
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400114} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400115
116struct madt_io_apic
117{
Kevin O'Connor590e5542009-10-08 22:09:02 -0400118 ACPI_SUB_HEADER_DEF
Kevin O'Connord10e4442009-03-01 12:41:20 -0500119 u8 io_apic_id; /* I/O APIC ID */
120 u8 reserved; /* Reserved - must be zero */
121 u32 address; /* APIC physical address */
122 u32 interrupt; /* Global system interrupt where INTI
123 * lines start */
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400124} PACKED;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400125
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500126/* IRQs 5,9,10,11 */
127#define PCI_ISA_IRQ_MASK 0x0e20
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500128
129struct madt_intsrcovr {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400130 ACPI_SUB_HEADER_DEF
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500131 u8 bus;
132 u8 source;
133 u32 gsi;
134 u16 flags;
135} PACKED;
136
Kevin O'Connorf4343772009-10-08 22:05:21 -0400137/*
138 * ACPI 2.0 Generic Address Space definition.
139 */
140struct acpi_20_generic_address {
141 u8 address_space_id;
142 u8 register_bit_width;
143 u8 register_bit_offset;
144 u8 reserved;
145 u64 address;
146} PACKED;
147
148/*
149 * HPET Description Table
150 */
151struct acpi_20_hpet {
152 ACPI_TABLE_HEADER_DEF /* ACPI common table header */
153 u32 timer_block_id;
154 struct acpi_20_generic_address addr;
155 u8 hpet_number;
156 u16 min_tick;
157 u8 page_protect;
158} PACKED;
159#define ACPI_HPET_ADDRESS 0xFED00000UL
160
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200161#define HPET_ID 0x000
162#define HPET_PERIOD 0x004
163
Kevin O'Connor590e5542009-10-08 22:09:02 -0400164/*
165 * SRAT (NUMA topology description) table
166 */
167
168#define SRAT_PROCESSOR 0
169#define SRAT_MEMORY 1
170
171struct system_resource_affinity_table
172{
173 ACPI_TABLE_HEADER_DEF
174 u32 reserved1;
175 u32 reserved2[2];
176} PACKED;
177
178struct srat_processor_affinity
179{
180 ACPI_SUB_HEADER_DEF
181 u8 proximity_lo;
182 u8 local_apic_id;
183 u32 flags;
184 u8 local_sapic_eid;
185 u8 proximity_hi[3];
186 u32 reserved;
187} PACKED;
188
189struct srat_memory_affinity
190{
191 ACPI_SUB_HEADER_DEF
192 u8 proximity[4];
193 u16 reserved1;
194 u32 base_addr_low,base_addr_high;
195 u32 length_low,length_high;
196 u32 reserved2;
197 u32 flags;
198 u32 reserved3[2];
199} PACKED;
200
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400201#include "acpi-dsdt.hex"
202
Kevin O'Connor2929c352009-07-25 13:48:27 -0400203static void
Kevin O'Connora9242a22009-10-07 19:42:07 -0400204build_header(struct acpi_table_header *h, u32 sig, int len, u8 rev)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400205{
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500206 h->signature = sig;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400207 h->length = cpu_to_le32(len);
208 h->revision = rev;
Kevin O'Connor6cb8ba92008-08-17 11:03:24 -0400209 memcpy(h->oem_id, CONFIG_APPNAME6, 6);
210 memcpy(h->oem_table_id, CONFIG_APPNAME4, 4);
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500211 memcpy(h->oem_table_id + 4, (void*)&sig, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400212 h->oem_revision = cpu_to_le32(1);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400213 memcpy(h->asl_compiler_id, CONFIG_APPNAME4, 4);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400214 h->asl_compiler_revision = cpu_to_le32(1);
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400215 h->checksum -= checksum(h, len);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400216}
217
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400218#define PIIX4_ACPI_ENABLE 0xf1
219#define PIIX4_ACPI_DISABLE 0xf0
220#define PIIX4_GPE0_BLK 0xafe0
221#define PIIX4_GPE0_BLK_LEN 4
222
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400223static void piix4_fadt_init(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400224{
225 struct fadt_descriptor_rev1 *fadt = arg;
226 fadt->acpi_enable = PIIX4_ACPI_ENABLE;
227 fadt->acpi_disable = PIIX4_ACPI_DISABLE;
228 fadt->gpe0_blk = cpu_to_le32(PIIX4_GPE0_BLK);
229 fadt->gpe0_blk_len = PIIX4_GPE0_BLK_LEN;
230}
231
Isaku Yamahatafe54a532010-07-20 16:37:18 +0900232static const struct pci_device_id fadt_init_tbl[] = {
233 /* PIIX4 Power Management device (for ACPI) */
234 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
235 piix4_fadt_init),
236
237 PCI_DEVICE_END
238};
239
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400240static void *
241build_fadt(struct pci_device *pci)
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400242{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400243 struct fadt_descriptor_rev1 *fadt = malloc_high(sizeof(*fadt));
Kevin O'Connor415d4292009-08-30 19:19:31 -0400244 struct facs_descriptor_rev1 *facs = memalign_high(64, sizeof(*facs));
Kevin O'Connor2929c352009-07-25 13:48:27 -0400245 void *dsdt = malloc_high(sizeof(AmlCode));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400246
Kevin O'Connor2929c352009-07-25 13:48:27 -0400247 if (!fadt || !facs || !dsdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500248 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400249 return NULL;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400250 }
251
Kevin O'Connor2929c352009-07-25 13:48:27 -0400252 /* FACS */
Kevin O'Connor2929c352009-07-25 13:48:27 -0400253 memset(facs, 0, sizeof(*facs));
254 facs->signature = FACS_SIGNATURE;
255 facs->length = cpu_to_le32(sizeof(*facs));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400256
Kevin O'Connor2929c352009-07-25 13:48:27 -0400257 /* DSDT */
258 memcpy(dsdt, AmlCode, sizeof(AmlCode));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400259
260 /* FADT */
261 memset(fadt, 0, sizeof(*fadt));
Kevin O'Connor2929c352009-07-25 13:48:27 -0400262 fadt->firmware_ctrl = cpu_to_le32((u32)facs);
263 fadt->dsdt = cpu_to_le32((u32)dsdt);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400264 fadt->model = 1;
265 fadt->reserved1 = 0;
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400266 int pm_sci_int = pci_config_readb(pci->bdf, PCI_INTERRUPT_LINE);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400267 fadt->sci_int = cpu_to_le16(pm_sci_int);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500268 fadt->smi_cmd = cpu_to_le32(PORT_SMI_CMD);
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500269 fadt->pm1a_evt_blk = cpu_to_le32(PORT_ACPI_PM_BASE);
270 fadt->pm1a_cnt_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x04);
271 fadt->pm_tmr_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x08);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400272 fadt->pm1_evt_len = 4;
273 fadt->pm1_cnt_len = 2;
274 fadt->pm_tmr_len = 4;
275 fadt->plvl2_lat = cpu_to_le16(0xfff); // C2 state not supported
276 fadt->plvl3_lat = cpu_to_le16(0xfff); // C3 state not supported
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400277 pci_init_device(fadt_init_tbl, pci, fadt);
Gleb Natapov20fcf9b2011-01-30 14:17:11 +0200278 /* WBINVD + PROC_C1 + SLP_BUTTON + FIX_RTC + RTC_S4 */
279 fadt->flags = cpu_to_le32((1 << 0) | (1 << 2) | (1 << 5) | (1 << 6) | (1 << 7));
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400280
Kevin O'Connora9242a22009-10-07 19:42:07 -0400281 build_header((void*)fadt, FACP_SIGNATURE, sizeof(*fadt), 1);
282
283 return fadt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400284}
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400285
Kevin O'Connora9242a22009-10-07 19:42:07 -0400286static void*
287build_madt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400288{
Kevin O'Connor2929c352009-07-25 13:48:27 -0400289 int madt_size = (sizeof(struct multiple_apic_table)
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400290 + sizeof(struct madt_processor_apic) * MaxCountCPUs
Kevin O'Connor2929c352009-07-25 13:48:27 -0400291 + sizeof(struct madt_io_apic)
292 + sizeof(struct madt_intsrcovr) * 16);
293 struct multiple_apic_table *madt = malloc_high(madt_size);
294 if (!madt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500295 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400296 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400297 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500298 memset(madt, 0, madt_size);
299 madt->local_apic_address = cpu_to_le32(BUILD_APIC_ADDR);
300 madt->flags = cpu_to_le32(1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400301 struct madt_processor_apic *apic = (void*)&madt[1];
302 int i;
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400303 for (i=0; i<MaxCountCPUs; i++) {
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500304 apic->type = APIC_PROCESSOR;
305 apic->length = sizeof(*apic);
306 apic->processor_id = i;
307 apic->local_apic_id = i;
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400308 if (i < CountCPUs)
309 apic->flags = cpu_to_le32(1);
310 else
311 apic->flags = cpu_to_le32(0);
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500312 apic++;
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400313 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400314 struct madt_io_apic *io_apic = (void*)apic;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500315 io_apic->type = APIC_IO;
316 io_apic->length = sizeof(*io_apic);
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400317 io_apic->io_apic_id = CountCPUs;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500318 io_apic->address = cpu_to_le32(BUILD_IOAPIC_ADDR);
319 io_apic->interrupt = cpu_to_le32(0);
320
Kevin O'Connord10e4442009-03-01 12:41:20 -0500321 struct madt_intsrcovr *intsrcovr = (void*)&io_apic[1];
Kevin O'Connor4d2b6192009-10-08 21:37:21 -0400322 if (qemu_cfg_irq0_override()) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400323 memset(intsrcovr, 0, sizeof(*intsrcovr));
324 intsrcovr->type = APIC_XRUPT_OVERRIDE;
325 intsrcovr->length = sizeof(*intsrcovr);
326 intsrcovr->source = 0;
327 intsrcovr->gsi = 2;
328 intsrcovr->flags = 0; /* conforms to bus specifications */
329 intsrcovr++;
330 }
331 for (i = 1; i < 16; i++) {
Kevin O'Connord10e4442009-03-01 12:41:20 -0500332 if (!(PCI_ISA_IRQ_MASK & (1 << i)))
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500333 /* No need for a INT source override structure. */
334 continue;
Kevin O'Connord10e4442009-03-01 12:41:20 -0500335 memset(intsrcovr, 0, sizeof(*intsrcovr));
336 intsrcovr->type = APIC_XRUPT_OVERRIDE;
337 intsrcovr->length = sizeof(*intsrcovr);
338 intsrcovr->source = i;
339 intsrcovr->gsi = i;
340 intsrcovr->flags = 0xd; /* active high, level triggered */
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500341 intsrcovr++;
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500342 }
343
Kevin O'Connora9242a22009-10-07 19:42:07 -0400344 build_header((void*)madt, APIC_SIGNATURE, (void*)intsrcovr - (void*)madt, 1);
345 return madt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400346}
347
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400348// Encode a hex value
349static inline char getHex(u32 val) {
350 val &= 0x0f;
351 return (val <= 9) ? ('0' + val) : ('A' + val - 10);
352}
353
354// Encode a length in an SSDT.
355static u8 *
356encodeLen(u8 *ssdt_ptr, int length, int bytes)
357{
358 switch (bytes) {
359 default:
360 case 4: ssdt_ptr[3] = ((length >> 20) & 0xff);
361 case 3: ssdt_ptr[2] = ((length >> 12) & 0xff);
362 case 2: ssdt_ptr[1] = ((length >> 4) & 0xff);
363 ssdt_ptr[0] = (((bytes-1) & 0x3) << 6) | (length & 0x0f);
364 break;
365 case 1: ssdt_ptr[0] = length & 0x3f;
366 }
367 return ssdt_ptr + bytes;
368}
369
370// AML Processor() object. See src/ssdt-proc.dsl for info.
371static unsigned char ssdt_proc[] = {
372 0x5b,0x83,0x42,0x05,0x43,0x50,0x41,0x41,
373 0xaa,0x10,0xb0,0x00,0x00,0x06,0x08,0x49,
374 0x44,0x5f,0x5f,0x0a,0xaa,0x08,0x5f,0x48,
375 0x49,0x44,0x0d,0x41,0x43,0x50,0x49,0x30,
376 0x30,0x30,0x37,0x00,0x14,0x0f,0x5f,0x4d,
377 0x41,0x54,0x00,0xa4,0x43,0x50,0x4d,0x41,
378 0x49,0x44,0x5f,0x5f,0x14,0x0f,0x5f,0x53,
379 0x54,0x41,0x00,0xa4,0x43,0x50,0x53,0x54,
380 0x49,0x44,0x5f,0x5f,0x14,0x0f,0x5f,0x45,
381 0x4a,0x30,0x01,0x43,0x50,0x45,0x4a,0x49,
382 0x44,0x5f,0x5f,0x68
383};
384#define SD_OFFSET_CPUHEX 6
385#define SD_OFFSET_CPUID1 8
386#define SD_OFFSET_CPUID2 20
387
Kevin O'Connor2929c352009-07-25 13:48:27 -0400388#define SSDT_SIGNATURE 0x54445353 // SSDT
Kevin O'Connora9242a22009-10-07 19:42:07 -0400389static void*
390build_ssdt(void)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400391{
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400392 int acpi_cpus = MaxCountCPUs > 0xff ? 0xff : MaxCountCPUs;
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400393 // length = ScopeOp + procs + NTYF method + CPON package
394 int length = ((1+3+4)
395 + (acpi_cpus * sizeof(ssdt_proc))
396 + (1+2+5+(12*acpi_cpus))
397 + (6+2+1+(1*acpi_cpus)));
398 u8 *ssdt = malloc_high(sizeof(struct acpi_table_header) + length);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400399 if (! ssdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500400 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400401 return NULL;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400402 }
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400403 u8 *ssdt_ptr = ssdt + sizeof(struct acpi_table_header);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400404
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400405 // build Scope(_SB_) header
Kevin O'Connor2929c352009-07-25 13:48:27 -0400406 *(ssdt_ptr++) = 0x10; // ScopeOp
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400407 ssdt_ptr = encodeLen(ssdt_ptr, length-1, 3);
408 *(ssdt_ptr++) = '_';
409 *(ssdt_ptr++) = 'S';
410 *(ssdt_ptr++) = 'B';
Kevin O'Connor2929c352009-07-25 13:48:27 -0400411 *(ssdt_ptr++) = '_';
412
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400413 // build Processor object for each processor
Kevin O'Connor2929c352009-07-25 13:48:27 -0400414 int i;
415 for (i=0; i<acpi_cpus; i++) {
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400416 memcpy(ssdt_ptr, ssdt_proc, sizeof(ssdt_proc));
417 ssdt_ptr[SD_OFFSET_CPUHEX] = getHex(i >> 4);
418 ssdt_ptr[SD_OFFSET_CPUHEX+1] = getHex(i);
419 ssdt_ptr[SD_OFFSET_CPUID1] = i;
420 ssdt_ptr[SD_OFFSET_CPUID2] = i;
421 ssdt_ptr += sizeof(ssdt_proc);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400422 }
423
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400424 // build "Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...}"
425 *(ssdt_ptr++) = 0x14; // MethodOp
426 ssdt_ptr = encodeLen(ssdt_ptr, 2+5+(12*acpi_cpus), 2);
427 *(ssdt_ptr++) = 'N';
428 *(ssdt_ptr++) = 'T';
429 *(ssdt_ptr++) = 'F';
430 *(ssdt_ptr++) = 'Y';
431 *(ssdt_ptr++) = 0x02;
432 for (i=0; i<acpi_cpus; i++) {
433 *(ssdt_ptr++) = 0xA0; // IfOp
434 ssdt_ptr = encodeLen(ssdt_ptr, 11, 1);
435 *(ssdt_ptr++) = 0x93; // LEqualOp
436 *(ssdt_ptr++) = 0x68; // Arg0Op
437 *(ssdt_ptr++) = 0x0A; // BytePrefix
438 *(ssdt_ptr++) = i;
439 *(ssdt_ptr++) = 0x86; // NotifyOp
440 *(ssdt_ptr++) = 'C';
441 *(ssdt_ptr++) = 'P';
442 *(ssdt_ptr++) = getHex(i >> 4);
443 *(ssdt_ptr++) = getHex(i);
444 *(ssdt_ptr++) = 0x69; // Arg1Op
445 }
446
447 // build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })"
448 *(ssdt_ptr++) = 0x08; // NameOp
449 *(ssdt_ptr++) = 'C';
450 *(ssdt_ptr++) = 'P';
451 *(ssdt_ptr++) = 'O';
452 *(ssdt_ptr++) = 'N';
453 *(ssdt_ptr++) = 0x12; // PackageOp
454 ssdt_ptr = encodeLen(ssdt_ptr, 2+1+(1*acpi_cpus), 2);
455 *(ssdt_ptr++) = acpi_cpus;
456 for (i=0; i<acpi_cpus; i++)
457 *(ssdt_ptr++) = (i < CountCPUs) ? 0x01 : 0x00;
458
Kevin O'Connora9242a22009-10-07 19:42:07 -0400459 build_header((void*)ssdt, SSDT_SIGNATURE, ssdt_ptr - ssdt, 1);
460
Kevin O'Connore2074bf2010-08-03 21:30:03 -0400461 //hexdump(ssdt, ssdt_ptr - ssdt);
462
Kevin O'Connora9242a22009-10-07 19:42:07 -0400463 return ssdt;
Kevin O'Connor2929c352009-07-25 13:48:27 -0400464}
465
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400466#define HPET_SIGNATURE 0x54455048 // HPET
Kevin O'Connorf4343772009-10-08 22:05:21 -0400467static void*
468build_hpet(void)
469{
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200470 struct acpi_20_hpet *hpet;
471 const void *hpet_base = (void *)ACPI_HPET_ADDRESS;
472 u32 hpet_vendor = readl(hpet_base + HPET_ID) >> 16;
473 u32 hpet_period = readl(hpet_base + HPET_PERIOD);
474
475 if (hpet_vendor == 0 || hpet_vendor == 0xffff ||
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400476 hpet_period == 0 || hpet_period > 100000000)
Jan Kiszkab8a90f52011-08-29 17:50:10 +0200477 return NULL;
478
479 hpet = malloc_high(sizeof(*hpet));
Kevin O'Connorf4343772009-10-08 22:05:21 -0400480 if (!hpet) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500481 warn_noalloc();
Kevin O'Connorf4343772009-10-08 22:05:21 -0400482 return NULL;
483 }
484
485 memset(hpet, 0, sizeof(*hpet));
486 /* Note timer_block_id value must be kept in sync with value advertised by
487 * emulated hpet
488 */
489 hpet->timer_block_id = cpu_to_le32(0x8086a201);
490 hpet->addr.address = cpu_to_le32(ACPI_HPET_ADDRESS);
491 build_header((void*)hpet, HPET_SIGNATURE, sizeof(*hpet), 1);
492
493 return hpet;
494}
495
Kevin O'Connor590e5542009-10-08 22:09:02 -0400496static void
497acpi_build_srat_memory(struct srat_memory_affinity *numamem,
498 u64 base, u64 len, int node, int enabled)
499{
500 numamem->type = SRAT_MEMORY;
501 numamem->length = sizeof(*numamem);
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400502 memset(numamem->proximity, 0 ,4);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400503 numamem->proximity[0] = node;
504 numamem->flags = cpu_to_le32(!!enabled);
505 numamem->base_addr_low = base & 0xFFFFFFFF;
506 numamem->base_addr_high = base >> 32;
507 numamem->length_low = len & 0xFFFFFFFF;
508 numamem->length_high = len >> 32;
509}
510
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400511#define SRAT_SIGNATURE 0x54415253 // SRAT
Kevin O'Connor590e5542009-10-08 22:09:02 -0400512static void *
513build_srat(void)
514{
515 int nb_numa_nodes = qemu_cfg_get_numa_nodes();
516
517 if (nb_numa_nodes == 0)
518 return NULL;
519
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400520 u64 *numadata = malloc_tmphigh(sizeof(u64) * (MaxCountCPUs + nb_numa_nodes));
Kevin O'Connor590e5542009-10-08 22:09:02 -0400521 if (!numadata) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500522 warn_noalloc();
Kevin O'Connor590e5542009-10-08 22:09:02 -0400523 return NULL;
524 }
525
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400526 qemu_cfg_get_numa_data(numadata, MaxCountCPUs + nb_numa_nodes);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400527
528 struct system_resource_affinity_table *srat;
529 int srat_size = sizeof(*srat) +
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400530 sizeof(struct srat_processor_affinity) * MaxCountCPUs +
Kevin O'Connor590e5542009-10-08 22:09:02 -0400531 sizeof(struct srat_memory_affinity) * (nb_numa_nodes + 2);
532
533 srat = malloc_high(srat_size);
534 if (!srat) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500535 warn_noalloc();
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900536 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400537 return NULL;
538 }
539
540 memset(srat, 0, srat_size);
541 srat->reserved1=1;
542 struct srat_processor_affinity *core = (void*)(srat + 1);
543 int i;
544 u64 curnode;
545
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400546 for (i = 0; i < MaxCountCPUs; ++i) {
Kevin O'Connor590e5542009-10-08 22:09:02 -0400547 core->type = SRAT_PROCESSOR;
548 core->length = sizeof(*core);
549 core->local_apic_id = i;
550 curnode = *numadata++;
551 core->proximity_lo = curnode;
552 memset(core->proximity_hi, 0, 3);
553 core->local_sapic_eid = 0;
554 if (i < CountCPUs)
555 core->flags = cpu_to_le32(1);
556 else
557 core->flags = 0;
558 core++;
559 }
560
561
562 /* the memory map is a bit tricky, it contains at least one hole
563 * from 640k-1M and possibly another one from 3.5G-4G.
564 */
565 struct srat_memory_affinity *numamem = (void*)core;
566 int slots = 0;
567 u64 mem_len, mem_base, next_base = 0;
568
569 acpi_build_srat_memory(numamem, 0, 640*1024, 0, 1);
570 next_base = 1024 * 1024;
571 numamem++;
572 slots++;
573 for (i = 1; i < nb_numa_nodes + 1; ++i) {
574 mem_base = next_base;
575 mem_len = *numadata++;
576 if (i == 1)
577 mem_len -= 1024 * 1024;
578 next_base = mem_base + mem_len;
579
580 /* Cut out the PCI hole */
581 if (mem_base <= RamSize && next_base > RamSize) {
582 mem_len -= next_base - RamSize;
583 if (mem_len > 0) {
584 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
585 numamem++;
586 slots++;
587 }
588 mem_base = 1ULL << 32;
589 mem_len = next_base - RamSize;
590 next_base += (1ULL << 32) - RamSize;
591 }
592 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
593 numamem++;
594 slots++;
595 }
596 for (; slots < nb_numa_nodes + 2; slots++) {
597 acpi_build_srat_memory(numamem, 0, 0, 0, 0);
598 numamem++;
599 }
600
601 build_header((void*)srat, SRAT_SIGNATURE, srat_size, 1);
602
Isaku Yamahatae1920be2010-02-12 11:36:20 +0900603 free(numadata);
Kevin O'Connor590e5542009-10-08 22:09:02 -0400604 return srat;
605}
606
Isaku Yamahata4c67f902010-07-20 16:37:19 +0900607static const struct pci_device_id acpi_find_tbl[] = {
608 /* PIIX4 Power Management device. */
609 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL),
610
611 PCI_DEVICE_END,
612};
613
Kevin O'Connor2929c352009-07-25 13:48:27 -0400614struct rsdp_descriptor *RsdpAddr;
615
Kevin O'Connora9242a22009-10-07 19:42:07 -0400616#define MAX_ACPI_TABLES 20
Kevin O'Connor2929c352009-07-25 13:48:27 -0400617void
618acpi_bios_init(void)
619{
620 if (! CONFIG_ACPI)
621 return;
622
623 dprintf(3, "init ACPI tables\n");
624
625 // This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400626 struct pci_device *pci = pci_find_init_device(acpi_find_tbl, NULL);
627 if (!pci)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400628 // Device not found
629 return;
630
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400631 // Build ACPI tables
Kevin O'Connora9242a22009-10-07 19:42:07 -0400632 u32 tables[MAX_ACPI_TABLES], tbl_idx = 0;
633
634#define ACPI_INIT_TABLE(X) \
635 do { \
636 tables[tbl_idx] = (u32)(X); \
637 if (tables[tbl_idx]) \
638 tbl_idx++; \
639 } while(0)
Kevin O'Connor2929c352009-07-25 13:48:27 -0400640
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400641 ACPI_INIT_TABLE(build_fadt(pci));
Kevin O'Connora9242a22009-10-07 19:42:07 -0400642 ACPI_INIT_TABLE(build_ssdt());
643 ACPI_INIT_TABLE(build_madt());
Kevin O'Connorf4343772009-10-08 22:05:21 -0400644 ACPI_INIT_TABLE(build_hpet());
Kevin O'Connor590e5542009-10-08 22:09:02 -0400645 ACPI_INIT_TABLE(build_srat());
Kevin O'Connor2929c352009-07-25 13:48:27 -0400646
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400647 u16 i, external_tables = qemu_cfg_acpi_additional_tables();
648
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400649 for (i = 0; i < external_tables; i++) {
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400650 u16 len = qemu_cfg_next_acpi_table_len();
651 void *addr = malloc_high(len);
652 if (!addr) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500653 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400654 continue;
655 }
656 ACPI_INIT_TABLE(qemu_cfg_next_acpi_table_load(addr, len));
657 if (tbl_idx == MAX_ACPI_TABLES) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500658 warn_noalloc();
Kevin O'Connorcc6dc462009-10-08 21:18:41 -0400659 break;
660 }
661 }
662
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400663 // Build final rsdt table
Kevin O'Connora9242a22009-10-07 19:42:07 -0400664 struct rsdt_descriptor_rev1 *rsdt;
665 size_t rsdt_len = sizeof(*rsdt) + sizeof(u32) * tbl_idx;
666 rsdt = malloc_high(rsdt_len);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400667 if (!rsdt) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500668 warn_noalloc();
Kevin O'Connora9242a22009-10-07 19:42:07 -0400669 return;
670 }
671 memset(rsdt, 0, rsdt_len);
672 memcpy(rsdt->table_offset_entry, tables, sizeof(u32) * tbl_idx);
Kevin O'Connora9242a22009-10-07 19:42:07 -0400673 build_header((void*)rsdt, RSDT_SIGNATURE, rsdt_len, 1);
Kevin O'Connor2929c352009-07-25 13:48:27 -0400674
675 // Build rsdp pointer table
Kevin O'Connor8a161c92011-09-02 18:11:58 -0400676 struct rsdp_descriptor *rsdp = malloc_fseg(sizeof(*rsdp));
677 if (!rsdp) {
678 warn_noalloc();
679 return;
680 }
Kevin O'Connor2929c352009-07-25 13:48:27 -0400681 memset(rsdp, 0, sizeof(*rsdp));
682 rsdp->signature = RSDP_SIGNATURE;
683 memcpy(rsdp->oem_id, CONFIG_APPNAME6, 6);
684 rsdp->rsdt_physical_address = cpu_to_le32((u32)rsdt);
685 rsdp->checksum -= checksum(rsdp, 20);
686 RsdpAddr = rsdp;
687 dprintf(1, "ACPI tables: RSDP=%p RSDT=%p\n", rsdp, rsdt);
Kevin O'Connor276d4a92008-06-11 22:47:01 -0400688}
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500689
690u32
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500691find_resume_vector(void)
Kevin O'Connor9967ab72008-12-18 21:57:33 -0500692{
693 dprintf(4, "rsdp=%p\n", RsdpAddr);
694 if (!RsdpAddr || RsdpAddr->signature != RSDP_SIGNATURE)
695 return 0;
696 struct rsdt_descriptor_rev1 *rsdt = (void*)RsdpAddr->rsdt_physical_address;
697 dprintf(4, "rsdt=%p\n", rsdt);
698 if (!rsdt || rsdt->signature != RSDT_SIGNATURE)
699 return 0;
700 void *end = (void*)rsdt + rsdt->length;
701 int i;
702 for (i=0; (void*)&rsdt->table_offset_entry[i] < end; i++) {
703 struct fadt_descriptor_rev1 *fadt = (void*)rsdt->table_offset_entry[i];
704 if (!fadt || fadt->signature != FACP_SIGNATURE)
705 continue;
706 dprintf(4, "fadt=%p\n", fadt);
707 struct facs_descriptor_rev1 *facs = (void*)fadt->firmware_ctrl;
708 dprintf(4, "facs=%p\n", facs);
709 if (! facs || facs->signature != FACS_SIGNATURE)
710 return 0;
711 // Found it.
712 dprintf(4, "resume addr=%d\n", facs->firmware_waking_vector);
713 return facs->firmware_waking_vector;
714 }
715 return 0;
716}