seabios: acpi: move out endian conversion helper function.
[seabios.git] / src / acpi.c
1 // Support for generating ACPI tables (on emulators)
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "acpi.h" // struct rsdp_descriptor
9 #include "util.h" // memcpy
10 #include "pci.h" // pci_find_device
11 #include "biosvar.h" // GET_EBDA
12 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
13 #include "pci_regs.h" // PCI_INTERRUPT_LINE
14 #include "paravirt.h"
15
16 /****************************************************/
17 /* ACPI tables init */
18
19 /* Table structure from Linux kernel (the ACPI tables are under the
20    BSD license) */
21
22 #define ACPI_TABLE_HEADER_DEF   /* ACPI common table header */ \
23     u32 signature;          /* ACPI signature (4 ASCII characters) */ \
24     u32 length;                 /* Length of table, in bytes, including header */ \
25     u8  revision;               /* ACPI Specification minor version # */ \
26     u8  checksum;               /* To make sum of entire table == 0 */ \
27     u8  oem_id [6];             /* OEM identification */ \
28     u8  oem_table_id [8];       /* OEM table identification */ \
29     u32 oem_revision;           /* OEM revision number */ \
30     u8  asl_compiler_id [4];    /* ASL compiler vendor ID */ \
31     u32 asl_compiler_revision;  /* ASL compiler revision number */
32
33
34 struct acpi_table_header         /* ACPI common table header */
35 {
36     ACPI_TABLE_HEADER_DEF
37 } PACKED;
38
39 /*
40  * ACPI 1.0 Root System Description Table (RSDT)
41  */
42 #define RSDT_SIGNATURE 0x54445352 // RSDT
43 struct rsdt_descriptor_rev1
44 {
45     ACPI_TABLE_HEADER_DEF       /* ACPI common table header */
46     u32 table_offset_entry[0];  /* Array of pointers to other */
47     /* ACPI tables */
48 } PACKED;
49
50 /*
51  * ACPI 1.0 Firmware ACPI Control Structure (FACS)
52  */
53 #define FACS_SIGNATURE 0x53434146 // FACS
54 struct facs_descriptor_rev1
55 {
56     u32 signature;           /* ACPI Signature */
57     u32 length;                 /* Length of structure, in bytes */
58     u32 hardware_signature;     /* Hardware configuration signature */
59     u32 firmware_waking_vector; /* ACPI OS waking vector */
60     u32 global_lock;            /* Global Lock */
61     u32 S4bios_f        : 1;    /* Indicates if S4BIOS support is present */
62     u32 reserved1       : 31;   /* Must be 0 */
63     u8  resverved3 [40];        /* Reserved - must be zero */
64 } PACKED;
65
66
67 /*
68  * ACPI 1.0 Fixed ACPI Description Table (FADT)
69  */
70 #define FACP_SIGNATURE 0x50434146 // FACP
71 struct fadt_descriptor_rev1
72 {
73     ACPI_TABLE_HEADER_DEF     /* ACPI common table header */
74     u32 firmware_ctrl;          /* Physical address of FACS */
75     u32 dsdt;                   /* Physical address of DSDT */
76     u8  model;                  /* System Interrupt Model */
77     u8  reserved1;              /* Reserved */
78     u16 sci_int;                /* System vector of SCI interrupt */
79     u32 smi_cmd;                /* Port address of SMI command port */
80     u8  acpi_enable;            /* Value to write to smi_cmd to enable ACPI */
81     u8  acpi_disable;           /* Value to write to smi_cmd to disable ACPI */
82     u8  S4bios_req;             /* Value to write to SMI CMD to enter S4BIOS state */
83     u8  reserved2;              /* Reserved - must be zero */
84     u32 pm1a_evt_blk;           /* Port address of Power Mgt 1a acpi_event Reg Blk */
85     u32 pm1b_evt_blk;           /* Port address of Power Mgt 1b acpi_event Reg Blk */
86     u32 pm1a_cnt_blk;           /* Port address of Power Mgt 1a Control Reg Blk */
87     u32 pm1b_cnt_blk;           /* Port address of Power Mgt 1b Control Reg Blk */
88     u32 pm2_cnt_blk;            /* Port address of Power Mgt 2 Control Reg Blk */
89     u32 pm_tmr_blk;             /* Port address of Power Mgt Timer Ctrl Reg Blk */
90     u32 gpe0_blk;               /* Port addr of General Purpose acpi_event 0 Reg Blk */
91     u32 gpe1_blk;               /* Port addr of General Purpose acpi_event 1 Reg Blk */
92     u8  pm1_evt_len;            /* Byte length of ports at pm1_x_evt_blk */
93     u8  pm1_cnt_len;            /* Byte length of ports at pm1_x_cnt_blk */
94     u8  pm2_cnt_len;            /* Byte Length of ports at pm2_cnt_blk */
95     u8  pm_tmr_len;             /* Byte Length of ports at pm_tm_blk */
96     u8  gpe0_blk_len;           /* Byte Length of ports at gpe0_blk */
97     u8  gpe1_blk_len;           /* Byte Length of ports at gpe1_blk */
98     u8  gpe1_base;              /* Offset in gpe model where gpe1 events start */
99     u8  reserved3;              /* Reserved */
100     u16 plvl2_lat;              /* Worst case HW latency to enter/exit C2 state */
101     u16 plvl3_lat;              /* Worst case HW latency to enter/exit C3 state */
102     u16 flush_size;             /* Size of area read to flush caches */
103     u16 flush_stride;           /* Stride used in flushing caches */
104     u8  duty_offset;            /* Bit location of duty cycle field in p_cnt reg */
105     u8  duty_width;             /* Bit width of duty cycle field in p_cnt reg */
106     u8  day_alrm;               /* Index to day-of-month alarm in RTC CMOS RAM */
107     u8  mon_alrm;               /* Index to month-of-year alarm in RTC CMOS RAM */
108     u8  century;                /* Index to century in RTC CMOS RAM */
109     u8  reserved4;              /* Reserved */
110     u8  reserved4a;             /* Reserved */
111     u8  reserved4b;             /* Reserved */
112 #if 0
113     u32 wb_invd         : 1;    /* The wbinvd instruction works properly */
114     u32 wb_invd_flush   : 1;    /* The wbinvd flushes but does not invalidate */
115     u32 proc_c1         : 1;    /* All processors support C1 state */
116     u32 plvl2_up        : 1;    /* C2 state works on MP system */
117     u32 pwr_button      : 1;    /* Power button is handled as a generic feature */
118     u32 sleep_button    : 1;    /* Sleep button is handled as a generic feature, or not present */
119     u32 fixed_rTC       : 1;    /* RTC wakeup stat not in fixed register space */
120     u32 rtcs4           : 1;    /* RTC wakeup stat not possible from S4 */
121     u32 tmr_val_ext     : 1;    /* The tmr_val width is 32 bits (0 = 24 bits) */
122     u32 reserved5       : 23;   /* Reserved - must be zero */
123 #else
124     u32 flags;
125 #endif
126 } PACKED;
127
128 /*
129  * MADT values and structures
130  */
131
132 /* Values for MADT PCATCompat */
133
134 #define DUAL_PIC                0
135 #define MULTIPLE_APIC           1
136
137
138 /* Master MADT */
139
140 #define APIC_SIGNATURE 0x43495041 // APIC
141 struct multiple_apic_table
142 {
143     ACPI_TABLE_HEADER_DEF     /* ACPI common table header */
144     u32 local_apic_address;     /* Physical address of local APIC */
145 #if 0
146     u32 PCATcompat      : 1;    /* A one indicates system also has dual 8259s */
147     u32 reserved1       : 31;
148 #else
149     u32 flags;
150 #endif
151 } PACKED;
152
153
154 /* Values for Type in APIC sub-headers */
155
156 #define APIC_PROCESSOR          0
157 #define APIC_IO                 1
158 #define APIC_XRUPT_OVERRIDE     2
159 #define APIC_NMI                3
160 #define APIC_LOCAL_NMI          4
161 #define APIC_ADDRESS_OVERRIDE   5
162 #define APIC_IO_SAPIC           6
163 #define APIC_LOCAL_SAPIC        7
164 #define APIC_XRUPT_SOURCE       8
165 #define APIC_RESERVED           9           /* 9 and greater are reserved */
166
167 /*
168  * MADT sub-structures (Follow MULTIPLE_APIC_DESCRIPTION_TABLE)
169  */
170 #define ACPI_SUB_HEADER_DEF   /* Common ACPI sub-structure header */\
171     u8  type;                               \
172     u8  length;
173
174 /* Sub-structures for MADT */
175
176 struct madt_processor_apic
177 {
178     ACPI_SUB_HEADER_DEF
179     u8  processor_id;           /* ACPI processor id */
180     u8  local_apic_id;          /* Processor's local APIC id */
181 #if 0
182     u32 processor_enabled: 1;   /* Processor is usable if set */
183     u32 reserved2       : 31;   /* Reserved, must be zero */
184 #else
185     u32 flags;
186 #endif
187 } PACKED;
188
189 struct madt_io_apic
190 {
191     ACPI_SUB_HEADER_DEF
192     u8  io_apic_id;             /* I/O APIC ID */
193     u8  reserved;               /* Reserved - must be zero */
194     u32 address;                /* APIC physical address */
195     u32 interrupt;              /* Global system interrupt where INTI
196                                  * lines start */
197 } PACKED;
198
199 /* IRQs 5,9,10,11 */
200 #define PCI_ISA_IRQ_MASK    0x0e20
201
202 struct madt_intsrcovr {
203     ACPI_SUB_HEADER_DEF
204     u8  bus;
205     u8  source;
206     u32 gsi;
207     u16 flags;
208 } PACKED;
209
210 /*
211  * ACPI 2.0 Generic Address Space definition.
212  */
213 struct acpi_20_generic_address {
214     u8  address_space_id;
215     u8  register_bit_width;
216     u8  register_bit_offset;
217     u8  reserved;
218     u64 address;
219 } PACKED;
220
221 /*
222  * HPET Description Table
223  */
224 struct acpi_20_hpet {
225     ACPI_TABLE_HEADER_DEF                    /* ACPI common table header */
226     u32           timer_block_id;
227     struct acpi_20_generic_address addr;
228     u8            hpet_number;
229     u16           min_tick;
230     u8            page_protect;
231 } PACKED;
232 #define ACPI_HPET_ADDRESS 0xFED00000UL
233
234 /*
235  * SRAT (NUMA topology description) table
236  */
237
238 #define SRAT_PROCESSOR          0
239 #define SRAT_MEMORY             1
240
241 struct system_resource_affinity_table
242 {
243     ACPI_TABLE_HEADER_DEF
244     u32    reserved1;
245     u32    reserved2[2];
246 } PACKED;
247
248 struct srat_processor_affinity
249 {
250     ACPI_SUB_HEADER_DEF
251     u8     proximity_lo;
252     u8     local_apic_id;
253     u32    flags;
254     u8     local_sapic_eid;
255     u8     proximity_hi[3];
256     u32    reserved;
257 } PACKED;
258
259 struct srat_memory_affinity
260 {
261     ACPI_SUB_HEADER_DEF
262     u8     proximity[4];
263     u16    reserved1;
264     u32    base_addr_low,base_addr_high;
265     u32    length_low,length_high;
266     u32    reserved2;
267     u32    flags;
268     u32    reserved3[2];
269 } PACKED;
270
271 #include "acpi-dsdt.hex"
272
273 static void
274 build_header(struct acpi_table_header *h, u32 sig, int len, u8 rev)
275 {
276     h->signature = sig;
277     h->length = cpu_to_le32(len);
278     h->revision = rev;
279     memcpy(h->oem_id, CONFIG_APPNAME6, 6);
280     memcpy(h->oem_table_id, CONFIG_APPNAME4, 4);
281     memcpy(h->asl_compiler_id, CONFIG_APPNAME4, 4);
282     memcpy(h->oem_table_id + 4, (void*)&sig, 4);
283     h->oem_revision = cpu_to_le32(1);
284     h->asl_compiler_revision = cpu_to_le32(1);
285     h->checksum -= checksum(h, len);
286 }
287
288 static void*
289 build_fadt(int bdf)
290 {
291     struct fadt_descriptor_rev1 *fadt = malloc_high(sizeof(*fadt));
292     struct facs_descriptor_rev1 *facs = memalign_high(64, sizeof(*facs));
293     void *dsdt = malloc_high(sizeof(AmlCode));
294
295     if (!fadt || !facs || !dsdt) {
296         warn_noalloc();
297         return NULL;
298     }
299
300     /* FACS */
301     memset(facs, 0, sizeof(*facs));
302     facs->signature = FACS_SIGNATURE;
303     facs->length = cpu_to_le32(sizeof(*facs));
304
305     /* DSDT */
306     memcpy(dsdt, AmlCode, sizeof(AmlCode));
307
308     /* FADT */
309     memset(fadt, 0, sizeof(*fadt));
310     fadt->firmware_ctrl = cpu_to_le32((u32)facs);
311     fadt->dsdt = cpu_to_le32((u32)dsdt);
312     fadt->model = 1;
313     fadt->reserved1 = 0;
314     int pm_sci_int = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
315     fadt->sci_int = cpu_to_le16(pm_sci_int);
316     fadt->smi_cmd = cpu_to_le32(PORT_SMI_CMD);
317     fadt->acpi_enable = 0xf1;
318     fadt->acpi_disable = 0xf0;
319     fadt->pm1a_evt_blk = cpu_to_le32(PORT_ACPI_PM_BASE);
320     fadt->pm1a_cnt_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x04);
321     fadt->pm_tmr_blk = cpu_to_le32(PORT_ACPI_PM_BASE + 0x08);
322     fadt->pm1_evt_len = 4;
323     fadt->pm1_cnt_len = 2;
324     fadt->pm_tmr_len = 4;
325     fadt->plvl2_lat = cpu_to_le16(0xfff); // C2 state not supported
326     fadt->plvl3_lat = cpu_to_le16(0xfff); // C3 state not supported
327     fadt->gpe0_blk = cpu_to_le32(0xafe0);
328     fadt->gpe0_blk_len = 4;
329     /* WBINVD + PROC_C1 + SLP_BUTTON + FIX_RTC */
330     fadt->flags = cpu_to_le32((1 << 0) | (1 << 2) | (1 << 5) | (1 << 6));
331
332     build_header((void*)fadt, FACP_SIGNATURE, sizeof(*fadt), 1);
333
334     return fadt;
335 }
336
337 static void*
338 build_madt(void)
339 {
340     int madt_size = (sizeof(struct multiple_apic_table)
341                      + sizeof(struct madt_processor_apic) * MaxCountCPUs
342                      + sizeof(struct madt_io_apic)
343                      + sizeof(struct madt_intsrcovr) * 16);
344     struct multiple_apic_table *madt = malloc_high(madt_size);
345     if (!madt) {
346         warn_noalloc();
347         return NULL;
348     }
349     memset(madt, 0, madt_size);
350     madt->local_apic_address = cpu_to_le32(BUILD_APIC_ADDR);
351     madt->flags = cpu_to_le32(1);
352     struct madt_processor_apic *apic = (void*)&madt[1];
353     int i;
354     for (i=0; i<MaxCountCPUs; i++) {
355         apic->type = APIC_PROCESSOR;
356         apic->length = sizeof(*apic);
357         apic->processor_id = i;
358         apic->local_apic_id = i;
359         if (i < CountCPUs)
360             apic->flags = cpu_to_le32(1);
361         else
362             apic->flags = cpu_to_le32(0);
363         apic++;
364     }
365     struct madt_io_apic *io_apic = (void*)apic;
366     io_apic->type = APIC_IO;
367     io_apic->length = sizeof(*io_apic);
368     io_apic->io_apic_id = CountCPUs;
369     io_apic->address = cpu_to_le32(BUILD_IOAPIC_ADDR);
370     io_apic->interrupt = cpu_to_le32(0);
371
372     struct madt_intsrcovr *intsrcovr = (void*)&io_apic[1];
373     if (qemu_cfg_irq0_override()) {
374         memset(intsrcovr, 0, sizeof(*intsrcovr));
375         intsrcovr->type   = APIC_XRUPT_OVERRIDE;
376         intsrcovr->length = sizeof(*intsrcovr);
377         intsrcovr->source = 0;
378         intsrcovr->gsi    = 2;
379         intsrcovr->flags  = 0; /* conforms to bus specifications */
380         intsrcovr++;
381     }
382     for (i = 1; i < 16; i++) {
383         if (!(PCI_ISA_IRQ_MASK & (1 << i)))
384             /* No need for a INT source override structure. */
385             continue;
386         memset(intsrcovr, 0, sizeof(*intsrcovr));
387         intsrcovr->type   = APIC_XRUPT_OVERRIDE;
388         intsrcovr->length = sizeof(*intsrcovr);
389         intsrcovr->source = i;
390         intsrcovr->gsi    = i;
391         intsrcovr->flags  = 0xd; /* active high, level triggered */
392         intsrcovr++;
393     }
394
395     build_header((void*)madt, APIC_SIGNATURE, (void*)intsrcovr - (void*)madt, 1);
396     return madt;
397 }
398
399 #define SSDT_SIGNATURE 0x54445353 // SSDT
400 static void*
401 build_ssdt(void)
402 {
403     int acpi_cpus = MaxCountCPUs > 0xff ? 0xff : MaxCountCPUs;
404     // calculate the length of processor block and scope block
405     // excluding PkgLength
406     int cpu_length = 13 * acpi_cpus + 4;
407
408     int length = sizeof(struct acpi_table_header) + 3 + cpu_length;
409     u8 *ssdt = malloc_high(length);
410     if (! ssdt) {
411         warn_noalloc();
412         return NULL;
413     }
414
415     u8 *ssdt_ptr = ssdt;
416     ssdt_ptr[9] = 0; // checksum;
417     ssdt_ptr += sizeof(struct acpi_table_header);
418
419     // build processor scope header
420     *(ssdt_ptr++) = 0x10; // ScopeOp
421     if (cpu_length <= 0x3e) {
422         /* Handle 1-4 CPUs with one byte encoding */
423         *(ssdt_ptr++) = cpu_length + 1;
424     } else {
425         /* Handle 5-314 CPUs with two byte encoding */
426         *(ssdt_ptr++) = 0x40 | ((cpu_length + 2) & 0xf);
427         *(ssdt_ptr++) = (cpu_length + 2) >> 4;
428     }
429     *(ssdt_ptr++) = '_'; // Name
430     *(ssdt_ptr++) = 'P';
431     *(ssdt_ptr++) = 'R';
432     *(ssdt_ptr++) = '_';
433
434     // build object for each processor
435     int i;
436     for (i=0; i<acpi_cpus; i++) {
437         *(ssdt_ptr++) = 0x5B; // ProcessorOp
438         *(ssdt_ptr++) = 0x83;
439         *(ssdt_ptr++) = 0x0B; // Length
440         *(ssdt_ptr++) = 'C';  // Name (CPUxx)
441         *(ssdt_ptr++) = 'P';
442         if ((i & 0xf0) != 0)
443             *(ssdt_ptr++) = (i >> 4) < 0xa ? (i >> 4) + '0' : (i >> 4) + 'A' - 0xa;
444         else
445             *(ssdt_ptr++) = 'U';
446         *(ssdt_ptr++) = (i & 0xf) < 0xa ? (i & 0xf) + '0' : (i & 0xf) + 'A' - 0xa;
447         *(ssdt_ptr++) = i;
448         *(ssdt_ptr++) = 0x10; // Processor block address
449         *(ssdt_ptr++) = 0xb0;
450         *(ssdt_ptr++) = 0;
451         *(ssdt_ptr++) = 0;
452         *(ssdt_ptr++) = 6;    // Processor block length
453     }
454
455     build_header((void*)ssdt, SSDT_SIGNATURE, ssdt_ptr - ssdt, 1);
456
457     return ssdt;
458 }
459
460 #define HPET_SIGNATURE 0x54455048 //HPET
461 static void*
462 build_hpet(void)
463 {
464     struct acpi_20_hpet *hpet = malloc_high(sizeof(*hpet));
465     if (!hpet) {
466         warn_noalloc();
467         return NULL;
468     }
469
470     memset(hpet, 0, sizeof(*hpet));
471     /* Note timer_block_id value must be kept in sync with value advertised by
472      * emulated hpet
473      */
474     hpet->timer_block_id = cpu_to_le32(0x8086a201);
475     hpet->addr.address = cpu_to_le32(ACPI_HPET_ADDRESS);
476     build_header((void*)hpet, HPET_SIGNATURE, sizeof(*hpet), 1);
477
478     return hpet;
479 }
480
481 static void
482 acpi_build_srat_memory(struct srat_memory_affinity *numamem,
483                        u64 base, u64 len, int node, int enabled)
484 {
485     numamem->type = SRAT_MEMORY;
486     numamem->length = sizeof(*numamem);
487     memset (numamem->proximity, 0 ,4);
488     numamem->proximity[0] = node;
489     numamem->flags = cpu_to_le32(!!enabled);
490     numamem->base_addr_low = base & 0xFFFFFFFF;
491     numamem->base_addr_high = base >> 32;
492     numamem->length_low = len & 0xFFFFFFFF;
493     numamem->length_high = len >> 32;
494 }
495
496 #define SRAT_SIGNATURE 0x54415253 //HPET
497 static void *
498 build_srat(void)
499 {
500     int nb_numa_nodes = qemu_cfg_get_numa_nodes();
501
502     if (nb_numa_nodes == 0)
503         return NULL;
504
505     u64 *numadata = malloc_tmphigh(sizeof(u64) * (MaxCountCPUs + nb_numa_nodes));
506     if (!numadata) {
507         warn_noalloc();
508         return NULL;
509     }
510
511     qemu_cfg_get_numa_data(numadata, MaxCountCPUs + nb_numa_nodes);
512
513     struct system_resource_affinity_table *srat;
514     int srat_size = sizeof(*srat) +
515         sizeof(struct srat_processor_affinity) * MaxCountCPUs +
516         sizeof(struct srat_memory_affinity) * (nb_numa_nodes + 2);
517
518     srat = malloc_high(srat_size);
519     if (!srat) {
520         warn_noalloc();
521         free(numadata);
522         return NULL;
523     }
524
525     memset(srat, 0, srat_size);
526     srat->reserved1=1;
527     struct srat_processor_affinity *core = (void*)(srat + 1);
528     int i;
529     u64 curnode;
530
531     for (i = 0; i < MaxCountCPUs; ++i) {
532         core->type = SRAT_PROCESSOR;
533         core->length = sizeof(*core);
534         core->local_apic_id = i;
535         curnode = *numadata++;
536         core->proximity_lo = curnode;
537         memset(core->proximity_hi, 0, 3);
538         core->local_sapic_eid = 0;
539         if (i < CountCPUs)
540             core->flags = cpu_to_le32(1);
541         else
542             core->flags = 0;
543         core++;
544     }
545
546
547     /* the memory map is a bit tricky, it contains at least one hole
548      * from 640k-1M and possibly another one from 3.5G-4G.
549      */
550     struct srat_memory_affinity *numamem = (void*)core;
551     int slots = 0;
552     u64 mem_len, mem_base, next_base = 0;
553
554     acpi_build_srat_memory(numamem, 0, 640*1024, 0, 1);
555     next_base = 1024 * 1024;
556     numamem++;
557     slots++;
558     for (i = 1; i < nb_numa_nodes + 1; ++i) {
559         mem_base = next_base;
560         mem_len = *numadata++;
561         if (i == 1)
562             mem_len -= 1024 * 1024;
563         next_base = mem_base + mem_len;
564
565         /* Cut out the PCI hole */
566         if (mem_base <= RamSize && next_base > RamSize) {
567             mem_len -= next_base - RamSize;
568             if (mem_len > 0) {
569                 acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
570                 numamem++;
571                 slots++;
572             }
573             mem_base = 1ULL << 32;
574             mem_len = next_base - RamSize;
575             next_base += (1ULL << 32) - RamSize;
576         }
577         acpi_build_srat_memory(numamem, mem_base, mem_len, i-1, 1);
578         numamem++;
579         slots++;
580     }
581     for (; slots < nb_numa_nodes + 2; slots++) {
582         acpi_build_srat_memory(numamem, 0, 0, 0, 0);
583         numamem++;
584     }
585
586     build_header((void*)srat, SRAT_SIGNATURE, srat_size, 1);
587
588     free(numadata);
589     return srat;
590 }
591
592 struct rsdp_descriptor *RsdpAddr;
593
594 #define MAX_ACPI_TABLES 20
595 void
596 acpi_bios_init(void)
597 {
598     if (! CONFIG_ACPI)
599         return;
600
601     dprintf(3, "init ACPI tables\n");
602
603     // This code is hardcoded for PIIX4 Power Management device.
604     int bdf = pci_find_device(PCI_VENDOR_ID_INTEL
605                               , PCI_DEVICE_ID_INTEL_82371AB_3);
606     if (bdf < 0)
607         // Device not found
608         return;
609
610     // Create initial rsdt table
611     struct rsdp_descriptor *rsdp = malloc_fseg(sizeof(*rsdp));
612     if (!rsdp) {
613         warn_noalloc();
614         return;
615     }
616
617     u32 tables[MAX_ACPI_TABLES], tbl_idx = 0;
618
619 #define ACPI_INIT_TABLE(X)                                   \
620     do {                                                     \
621         tables[tbl_idx] = (u32)(X);                          \
622         if (tables[tbl_idx])                                 \
623             tbl_idx++;                                       \
624     } while(0)
625
626     // Add tables
627     ACPI_INIT_TABLE(build_fadt(bdf));
628     ACPI_INIT_TABLE(build_ssdt());
629     ACPI_INIT_TABLE(build_madt());
630     ACPI_INIT_TABLE(build_hpet());
631     ACPI_INIT_TABLE(build_srat());
632
633     u16 i, external_tables = qemu_cfg_acpi_additional_tables();
634
635     for(i = 0; i < external_tables; i++) {
636         u16 len = qemu_cfg_next_acpi_table_len();
637         void *addr = malloc_high(len);
638         if (!addr) {
639             warn_noalloc();
640             continue;
641         }
642         ACPI_INIT_TABLE(qemu_cfg_next_acpi_table_load(addr, len));
643         if (tbl_idx == MAX_ACPI_TABLES) {
644             warn_noalloc();
645             break;
646         }
647     }
648
649     struct rsdt_descriptor_rev1 *rsdt;
650     size_t rsdt_len = sizeof(*rsdt) + sizeof(u32) * tbl_idx;
651     rsdt = malloc_high(rsdt_len);
652
653     if (!rsdt) {
654         warn_noalloc();
655         return;
656     }
657     memset(rsdt, 0, rsdt_len);
658     memcpy(rsdt->table_offset_entry, tables, sizeof(u32) * tbl_idx);
659
660     build_header((void*)rsdt, RSDT_SIGNATURE, rsdt_len, 1);
661
662     // Build rsdp pointer table
663     memset(rsdp, 0, sizeof(*rsdp));
664     rsdp->signature = RSDP_SIGNATURE;
665     memcpy(rsdp->oem_id, CONFIG_APPNAME6, 6);
666     rsdp->rsdt_physical_address = cpu_to_le32((u32)rsdt);
667     rsdp->checksum -= checksum(rsdp, 20);
668     RsdpAddr = rsdp;
669     dprintf(1, "ACPI tables: RSDP=%p RSDT=%p\n", rsdp, rsdt);
670 }
671
672 u32
673 find_resume_vector(void)
674 {
675     dprintf(4, "rsdp=%p\n", RsdpAddr);
676     if (!RsdpAddr || RsdpAddr->signature != RSDP_SIGNATURE)
677         return 0;
678     struct rsdt_descriptor_rev1 *rsdt = (void*)RsdpAddr->rsdt_physical_address;
679     dprintf(4, "rsdt=%p\n", rsdt);
680     if (!rsdt || rsdt->signature != RSDT_SIGNATURE)
681         return 0;
682     void *end = (void*)rsdt + rsdt->length;
683     int i;
684     for (i=0; (void*)&rsdt->table_offset_entry[i] < end; i++) {
685         struct fadt_descriptor_rev1 *fadt = (void*)rsdt->table_offset_entry[i];
686         if (!fadt || fadt->signature != FACP_SIGNATURE)
687             continue;
688         dprintf(4, "fadt=%p\n", fadt);
689         struct facs_descriptor_rev1 *facs = (void*)fadt->firmware_ctrl;
690         dprintf(4, "facs=%p\n", facs);
691         if (! facs || facs->signature != FACS_SIGNATURE)
692             return 0;
693         // Found it.
694         dprintf(4, "resume addr=%d\n", facs->firmware_waking_vector);
695         return facs->firmware_waking_vector;
696     }
697     return 0;
698 }