beef891424d9d6a763228653c4fef8e79aec8180
[coreboot.git] / src / arch / i386 / boot / acpi.c
1 /*
2  * coreboot ACPI Table support
3  * written by Stefan Reinauer <stepan@openbios.org>
4  *  Copyright (C) 2004 SUSE LINUX AG
5  *  Copyright (C) 2005-2009 coresystems GmbH
6  *
7  * ACPI FADT, FACS, and DSDT table support added by 
8  * Nick Barker <nick.barker9@btinternet.com>, and those portions
9  *  (C) Copyright 2004 Nick Barker
10  *
11  * Copyright 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
12  * 2005.9 yhlu add SRAT table generation
13  */
14
15 /* 
16  * Each system port implementing ACPI has to provide two functions:
17  * 
18  *   write_acpi_tables()
19  *   acpi_dump_apics()
20  *   
21  * See Kontron 986LCD-M port for a good example of an ACPI implementation
22  * in coreboot.
23  */
24
25 #include <console/console.h>
26 #include <string.h>
27 #include <arch/acpi.h>
28 #include <arch/acpigen.h>
29 #include <device/pci.h>
30 #include <cbmem.h>
31
32 u8 acpi_checksum(u8 *table, u32 length)
33 {
34         u8 ret=0;
35         while (length--) {
36                 ret += *table;
37                 table++;
38         }
39         return -ret;
40 }
41
42 /**
43  * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length and checksum
44  */
45
46 void acpi_add_table(acpi_rsdp_t *rsdp, void *table)
47 {
48         int i, entries_num;
49         acpi_rsdt_t *rsdt;
50         acpi_xsdt_t *xsdt = NULL;
51
52         /* The RSDT is mandatory ... */
53         rsdt = (acpi_rsdt_t *)rsdp->rsdt_address;
54
55         /* ... while the XSDT is not */
56         if (rsdp->xsdt_address) {
57                 xsdt = (acpi_xsdt_t *)((u32)rsdp->xsdt_address);
58         }
59         
60         /* This should always be MAX_ACPI_TABLES */
61         entries_num = ARRAY_SIZE(rsdt->entry);
62         
63         for (i = 0; i < entries_num; i++) {
64                 if(rsdt->entry[i] == 0)
65                         break;
66         }
67
68         if (i >= entries_num) {
69                 printk_err("ACPI: Error: Could not add ACPI table, too many tables.\n");
70                 return;
71         }
72
73         /* Add table to the RSDT */
74         rsdt->entry[i] = (u32)table;
75
76         /* Fix RSDT length or the kernel will assume invalid entries */
77         rsdt->header.length = sizeof(acpi_header_t) + (sizeof(u32) * (i+1));
78
79         /* Re-calculate checksum */
80         rsdt->header.checksum = 0; /* Hope this won't get optimized away */
81         rsdt->header.checksum = acpi_checksum((u8 *)rsdt,
82                         rsdt->header.length);
83
84         /* And now the same thing for the XSDT. We use the same index as for
85          * now we want the XSDT and RSDT to always be in sync in coreboot.
86          */
87         if (xsdt) {
88                 /* Add table to the XSDT */
89                 xsdt->entry[i]=(u64)(u32)table;
90
91                 /* Fix XSDT length */
92                 xsdt->header.length = sizeof(acpi_header_t) +
93                         (sizeof(u64) * (i+1));
94
95                 /* Re-calculate checksum */
96                 xsdt->header.checksum=0;
97                 xsdt->header.checksum=acpi_checksum((u8 *)xsdt,
98                                 xsdt->header.length);
99         }
100
101         printk_debug("ACPI: added table %d/%d Length now %d\n",
102                         i+1, entries_num, rsdt->header.length);
103 }
104
105 int acpi_create_mcfg_mmconfig(acpi_mcfg_mmconfig_t *mmconfig, u32 base, u16 seg_nr, u8 start, u8 end)
106 {
107         mmconfig->base_address = base;
108         mmconfig->base_reserved = 0;
109         mmconfig->pci_segment_group_number = seg_nr;
110         mmconfig->start_bus_number = start;
111         mmconfig->end_bus_number = end;
112         return (sizeof(acpi_mcfg_mmconfig_t));
113 }
114
115 int acpi_create_madt_lapic(acpi_madt_lapic_t *lapic, u8 cpu, u8 apic)
116 {
117         lapic->type=0;
118         lapic->length=sizeof(acpi_madt_lapic_t);
119         lapic->flags=1;
120         
121         lapic->processor_id=cpu;
122         lapic->apic_id=apic;
123         
124         return(lapic->length);
125 }
126
127 unsigned long acpi_create_madt_lapics(unsigned long current)
128 {
129         device_t cpu;
130         int cpu_index = 0;
131
132         for(cpu = all_devices; cpu; cpu = cpu->next) {
133                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
134                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
135                         continue;
136                 }
137                 if (!cpu->enabled) {
138                         continue;
139                 }
140                 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, cpu_index, cpu->path.apic.apic_id);
141                 cpu_index++;
142         }
143         return current;
144 }
145
146 int acpi_create_madt_ioapic(acpi_madt_ioapic_t *ioapic, u8 id, u32 addr,u32 gsi_base) 
147 {
148         ioapic->type=1;
149         ioapic->length=sizeof(acpi_madt_ioapic_t);
150         ioapic->reserved=0x00;
151         ioapic->gsi_base=gsi_base;
152         
153         ioapic->ioapic_id=id;
154         ioapic->ioapic_addr=addr;
155         
156         return(ioapic->length);
157 }
158
159 int acpi_create_madt_irqoverride(acpi_madt_irqoverride_t *irqoverride,
160                 u8 bus, u8 source, u32 gsirq, u16 flags)
161 {
162         irqoverride->type=2;
163         irqoverride->length=sizeof(acpi_madt_irqoverride_t);
164         irqoverride->bus=bus;
165         irqoverride->source=source;
166         irqoverride->gsirq=gsirq;
167         irqoverride->flags=flags;
168         
169         return(irqoverride->length);
170 }
171
172 int acpi_create_madt_lapic_nmi(acpi_madt_lapic_nmi_t *lapic_nmi, u8 cpu,
173                 u16 flags, u8 lint)
174 {
175         lapic_nmi->type=4;
176         lapic_nmi->length=sizeof(acpi_madt_lapic_nmi_t);
177         
178         lapic_nmi->flags=flags;
179         lapic_nmi->processor_id=cpu;
180         lapic_nmi->lint=lint;
181         
182         return(lapic_nmi->length);
183 }
184
185 void acpi_create_madt(acpi_madt_t *madt)
186 {
187 #define LOCAL_APIC_ADDR 0xfee00000ULL
188         
189         acpi_header_t *header=&(madt->header);
190         unsigned long current=(unsigned long)madt+sizeof(acpi_madt_t);
191         
192         memset((void *)madt, 0, sizeof(acpi_madt_t));
193         
194         /* fill out header fields */
195         memcpy(header->signature, "APIC", 4);
196         memcpy(header->oem_id, OEM_ID, 6);
197         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
198         memcpy(header->asl_compiler_id, ASLC, 4);
199         
200         header->length = sizeof(acpi_madt_t);
201         header->revision = 1;
202
203         madt->lapic_addr= LOCAL_APIC_ADDR;
204         madt->flags     = 0x1; /* PCAT_COMPAT */
205
206         current = acpi_fill_madt(current);
207         
208         /* recalculate length */
209         header->length= current - (unsigned long)madt;
210         
211         header->checksum        = acpi_checksum((void *)madt, header->length);
212 }
213
214 void acpi_create_mcfg(acpi_mcfg_t *mcfg)
215 {
216
217         acpi_header_t *header=&(mcfg->header);
218         unsigned long current=(unsigned long)mcfg+sizeof(acpi_mcfg_t);
219         
220         memset((void *)mcfg, 0, sizeof(acpi_mcfg_t));
221         
222         /* fill out header fields */
223         memcpy(header->signature, "MCFG", 4);
224         memcpy(header->oem_id, OEM_ID, 6);
225         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
226         memcpy(header->asl_compiler_id, ASLC, 4);
227         
228         header->length = sizeof(acpi_mcfg_t);
229         header->revision = 1;
230
231         current = acpi_fill_mcfg(current);
232         
233         /* recalculate length */
234         header->length= current - (unsigned long)mcfg;
235         
236         header->checksum        = acpi_checksum((void *)mcfg, header->length);
237 }
238
239 /* this can be overriden by platform ACPI setup code,
240  * if it calls acpi_create_ssdt_generator
241  */
242 unsigned long __attribute__((weak)) acpi_fill_ssdt_generator(unsigned long current,
243                                                     const char *oem_table_id) {
244         return current;
245 }
246
247 void acpi_create_ssdt_generator(acpi_header_t *ssdt, const char *oem_table_id)
248 {
249         unsigned long current=(unsigned long)ssdt+sizeof(acpi_header_t);
250         memset((void *)ssdt, 0, sizeof(acpi_header_t));
251         memcpy(&ssdt->signature, "SSDT", 4);
252         ssdt->revision = 2;
253         memcpy(&ssdt->oem_id, OEM_ID, 6);
254         memcpy(&ssdt->oem_table_id, oem_table_id, 8);
255         ssdt->oem_revision = 42;
256         memcpy(&ssdt->asl_compiler_id, "CORE", 4);
257         ssdt->asl_compiler_revision = 42;
258         ssdt->length = sizeof(acpi_header_t);
259
260         acpigen_set_current((char *) current);
261         current = acpi_fill_ssdt_generator(current, oem_table_id);
262
263         /* recalculate length */
264         ssdt->length = current - (unsigned long)ssdt;
265         ssdt->checksum = acpi_checksum((void *)ssdt, ssdt->length);
266 }
267
268 int acpi_create_srat_lapic(acpi_srat_lapic_t *lapic, u8 node, u8 apic)
269 {
270         memset((void *)lapic, 0, sizeof(acpi_srat_lapic_t));
271         lapic->type=0;
272         lapic->length=sizeof(acpi_srat_lapic_t);
273         lapic->flags=1;
274
275         lapic->proximity_domain_7_0 = node;
276         lapic->apic_id=apic;
277
278         return(lapic->length);
279 }
280
281 int acpi_create_srat_mem(acpi_srat_mem_t *mem, u8 node, u32 basek,u32 sizek, u32 flags)
282 {
283         mem->type=1;
284         mem->length=sizeof(acpi_srat_mem_t);
285
286         mem->base_address_low = (basek<<10);
287         mem->base_address_high = (basek>>(32-10));
288
289         mem->length_low = (sizek<<10);
290         mem->length_high = (sizek>>(32-10));
291
292         mem->proximity_domain = node;
293
294         mem->flags = flags; 
295
296         return(mem->length);
297 }
298
299 void acpi_create_srat(acpi_srat_t *srat)
300 {
301
302         acpi_header_t *header=&(srat->header);
303         unsigned long current=(unsigned long)srat+sizeof(acpi_srat_t);
304
305         memset((void *)srat, 0, sizeof(acpi_srat_t));
306
307         /* fill out header fields */
308         memcpy(header->signature, "SRAT", 4);
309         memcpy(header->oem_id, OEM_ID, 6);
310         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
311         memcpy(header->asl_compiler_id, ASLC, 4);
312
313         header->length = sizeof(acpi_srat_t);
314         header->revision = 1;
315
316         srat->resv     = 0x1; /* BACK COMP */
317
318         current = acpi_fill_srat(current);
319
320         /* recalculate length */
321         header->length= current - (unsigned long)srat;
322
323         header->checksum        = acpi_checksum((void *)srat, header->length);
324 }
325
326 void acpi_create_slit(acpi_slit_t *slit)
327 {
328
329         acpi_header_t *header=&(slit->header);
330         unsigned long current=(unsigned long)slit+sizeof(acpi_slit_t);
331
332         memset((void *)slit, 0, sizeof(acpi_slit_t));
333
334         /* fill out header fields */
335         memcpy(header->signature, "SLIT", 4);
336         memcpy(header->oem_id, OEM_ID, 6);
337         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
338         memcpy(header->asl_compiler_id, ASLC, 4);
339
340         header->length = sizeof(acpi_slit_t);
341         header->revision = 1;
342
343         current = acpi_fill_slit(current);
344
345         /* recalculate length */
346         header->length= current - (unsigned long)slit;
347
348         header->checksum        = acpi_checksum((void *)slit, header->length);
349 }
350
351 void acpi_create_hpet(acpi_hpet_t *hpet)
352 {
353 #define HPET_ADDR  0xfed00000ULL
354         acpi_header_t *header=&(hpet->header);
355         acpi_addr_t *addr=&(hpet->addr);
356         
357         memset((void *)hpet, 0, sizeof(acpi_hpet_t));
358         
359         /* fill out header fields */
360         memcpy(header->signature, "HPET", 4);
361         memcpy(header->oem_id, OEM_ID, 6);
362         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
363         memcpy(header->asl_compiler_id, ASLC, 4);
364         
365         header->length = sizeof(acpi_hpet_t);
366         header->revision = 1;
367
368         /* fill out HPET address */
369         addr->space_id          = 0; /* Memory */
370         addr->bit_width         = 64;
371         addr->bit_offset        = 0;
372         addr->addrl             = HPET_ADDR & 0xffffffff;
373         addr->addrh             = HPET_ADDR >> 32;
374
375         hpet->id        = 0x102282a0; /* AMD ? */
376         hpet->number    = 0;
377         hpet->min_tick  = 4096;
378         
379         header->checksum        = acpi_checksum((void *)hpet, sizeof(acpi_hpet_t));
380 }
381 void acpi_create_facs(acpi_facs_t *facs)
382 {
383         
384         memset( (void *)facs,0, sizeof(acpi_facs_t));
385
386         memcpy(facs->signature, "FACS", 4);
387         facs->length = sizeof(acpi_facs_t);
388         facs->hardware_signature = 0;
389         facs->firmware_waking_vector = 0;
390         facs->global_lock = 0;
391         facs->flags = 0;
392         facs->x_firmware_waking_vector_l = 0;
393         facs->x_firmware_waking_vector_h = 0;
394         facs->version = 1;
395 }
396
397 void acpi_write_rsdt(acpi_rsdt_t *rsdt)
398
399         acpi_header_t *header=&(rsdt->header);
400         
401         /* fill out header fields */
402         memcpy(header->signature, "RSDT", 4);
403         memcpy(header->oem_id, OEM_ID, 6);
404         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
405         memcpy(header->asl_compiler_id, ASLC, 4);
406         
407         header->length = sizeof(acpi_rsdt_t);
408         header->revision = 1;
409         
410         /* fill out entries */
411
412         // entries are filled in later, we come with an empty set.
413         
414         /* fix checksum */
415         
416         header->checksum = acpi_checksum((void *)rsdt, sizeof(acpi_rsdt_t));
417 }
418
419 void acpi_write_xsdt(acpi_xsdt_t *xsdt)
420
421         acpi_header_t *header=&(xsdt->header);
422         
423         /* fill out header fields */
424         memcpy(header->signature, "XSDT", 4);
425         memcpy(header->oem_id, OEM_ID, 6);
426         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
427         memcpy(header->asl_compiler_id, ASLC, 4);
428         
429         header->length = sizeof(acpi_xsdt_t);
430         header->revision = 1;
431         
432         /* fill out entries */
433
434         // entries are filled in later, we come with an empty set.
435         
436         /* fix checksum */
437         
438         header->checksum = acpi_checksum((void *)xsdt, sizeof(acpi_xsdt_t));
439 }
440
441 void acpi_write_rsdp(acpi_rsdp_t *rsdp, acpi_rsdt_t *rsdt, acpi_xsdt_t *xsdt)
442 {
443         memset(rsdp, 0, sizeof(acpi_rsdp_t));
444         memcpy(rsdp->signature, RSDP_SIG, 8);
445         memcpy(rsdp->oem_id, OEM_ID, 6);
446         rsdp->length            = sizeof(acpi_rsdp_t);
447         rsdp->rsdt_address      = (u32)rsdt;
448         /* Some OSes expect an XSDT to be present for RSD PTR 
449          * revisions >= 2. If we don't have an ACPI XSDT, force
450          * ACPI 1.0 (and thus RSD PTR revision 0)
451          */
452         if (xsdt == NULL) {
453                 rsdp->revision          = 0;
454         } else {
455                 rsdp->xsdt_address      = (u64)(u32)xsdt;
456                 rsdp->revision          = 2;
457         }
458         rsdp->checksum          = acpi_checksum((void *)rsdp, 20);
459         rsdp->ext_checksum      = acpi_checksum((void *)rsdp, sizeof(acpi_rsdp_t));
460 }
461
462 #if CONFIG_HAVE_ACPI_RESUME == 1
463 void suspend_resume(void)
464 {
465         void *wake_vec;
466
467 #if 0
468 #if CONFIG_MEM_TRAIN_SEQ != 0
469         #error "So far it works on AMD and CONFIG_MEM_TRAIN_SEQ == 0"
470 #endif
471
472 #if CONFIG_RAMBASE < 0x1F00000
473         #error "For ACPI RESUME you need to have CONFIG_RAMBASE at least 31MB"
474         #error "Chipset support (S3_NVRAM_EARLY and ACPI_IS_WAKEUP_EARLY functions and memory ctrl)"
475         #error "And coreboot memory reserved in mainboard.c"
476 #endif
477 #endif
478         /* if we happen to be resuming find wakeup vector and jump to OS */
479         wake_vec = acpi_find_wakeup_vector();
480         if (wake_vec)
481                 acpi_jump_to_wakeup(wake_vec);
482 }
483
484 /* this is to be filled by SB code - startup value what was found */
485 u8 acpi_slp_type = 0;
486
487 int acpi_is_wakeup(void)
488 {
489         return (acpi_slp_type == 3);
490 }
491
492 static acpi_rsdp_t *valid_rsdp(acpi_rsdp_t *rsdp)
493 {
494         if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
495                 return NULL;
496
497         printk_debug("Looking on %p for valid checksum\n", rsdp);
498
499         if (acpi_checksum((void *)rsdp, 20) != 0)
500                 return NULL;
501         printk_debug("Checksum 1 passed\n");
502
503         if ((rsdp->revision > 1) && (acpi_checksum((void *)rsdp,
504                                                 rsdp->length) != 0))
505                 return NULL;
506
507         printk_debug("Checksum 2 passed all OK\n");
508
509         return rsdp;
510 }
511
512 static acpi_rsdp_t *rsdp;
513
514 void *acpi_get_wakeup_rsdp(void)
515 {
516         return rsdp;
517 }
518
519 void *acpi_find_wakeup_vector(void)
520 {
521         char *p, *end;
522
523         acpi_rsdt_t *rsdt;
524         acpi_facs_t *facs;
525         acpi_fadt_t *fadt;
526         void  *wake_vec;
527         int i;
528
529         rsdp = NULL;
530
531         if (!acpi_is_wakeup())
532                 return NULL;
533
534         printk_debug("Trying to find the wakeup vector ...\n");
535
536         /* find RSDP */
537         for (p = (char *) 0xe0000; p <  (char *) 0xfffff; p+=16) {
538                 if ((rsdp = valid_rsdp((acpi_rsdp_t *) p)))
539                         break;
540         }
541
542         if (rsdp == NULL)
543                 return NULL;
544
545         printk_debug("RSDP found at %p\n", rsdp);
546         rsdt = (acpi_rsdt_t *) rsdp->rsdt_address;
547         
548         end = (char *) rsdt + rsdt->header.length;
549         printk_debug("RSDT found at %p ends at %p\n", rsdt, end);
550
551         for (i = 0; ((char *) &rsdt->entry[i]) < end; i++) {
552                 fadt = (acpi_fadt_t *) rsdt->entry[i];
553                 if (strncmp((char *)fadt, "FACP", 4) == 0)
554                         break;
555                 fadt = NULL;
556         }
557
558         if (fadt == NULL)
559                 return NULL;
560
561         printk_debug("FADT found at %p\n", fadt);
562         facs = (acpi_facs_t *)fadt->firmware_ctrl;
563
564         if (facs == NULL) {
565                 printk_debug("No FACS found, wake up from S3 not possible.\n");
566                 return NULL;
567         }
568
569         printk_debug("FACS found at %p\n", facs);
570         wake_vec = (void *) facs->firmware_waking_vector;
571         printk_debug("OS waking vector is %p\n", wake_vec);
572         return wake_vec;
573 }
574
575 extern char *lowmem_backup;
576 extern char *lowmem_backup_ptr;
577 extern int lowmem_backup_size;
578
579 #define WAKEUP_BASE             0x600
580
581 void (*acpi_do_wakeup)(u32 vector, u32 backup_source, u32 backup_target, u32
582                 backup_size) __attribute__((regparm(0))) = (void *)WAKEUP_BASE;
583
584 extern unsigned char __wakeup, __wakeup_size;
585
586 void acpi_jump_to_wakeup(void *vector)
587 {
588         u32 acpi_backup_memory = (u32) cbmem_find(CBMEM_ID_RESUME);
589
590         if (!acpi_backup_memory) {
591                 printk(BIOS_WARNING, "ACPI: Backup memory missing. No S3 Resume.\n");
592                 return;
593         }
594
595         // FIXME this should go into the ACPI backup memory, too. No pork saussages.
596         /* just restore the SMP trampoline and continue with wakeup on assembly level */
597         memcpy(lowmem_backup_ptr, lowmem_backup, lowmem_backup_size);
598
599         /* copy wakeup trampoline in place */
600         memcpy(WAKEUP_BASE, &__wakeup, &__wakeup_size);
601
602         acpi_do_wakeup((u32)vector, acpi_backup_memory, CONFIG_RAMBASE, HIGH_MEMORY_SAVE);
603 }
604 #endif