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