637a304cefcd6df6125b152c1c52119401b45fd8
[coreboot.git] / src / mainboard / amd / persimmon / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include <console/console.h>
21 #include <string.h>
22 #include <arch/acpi.h>
23 #include <arch/ioapic.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <cpu/x86/msr.h>
27 #include "agesawrapper.h"
28 #include <cpu/amd/mtrr.h>
29 #include <cpu/amd/amdfam14.h>
30
31 #define DUMP_ACPI_TABLES 0
32
33 #if DUMP_ACPI_TABLES == 1
34
35 static void dump_mem(u32 start, u32 end)
36 {
37
38         u32 i;
39         print_debug("dump_mem:");
40         for (i = start; i < end; i++) {
41         if ((i & 0xf) == 0) {
42                 printk(BIOS_DEBUG, "\n%08x:", i);
43         }
44         printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
45         }
46         print_debug("\n");
47 }
48 #endif
49
50 extern const unsigned char AmlCode[];
51 extern const unsigned char AmlCode_ssdt[];
52
53 unsigned long acpi_fill_mcfg(unsigned long current)
54 {
55         /* Just a dummy */
56         return current;
57 }
58
59 unsigned long acpi_fill_madt(unsigned long current)
60 {
61         /* create all subtables for processors */
62         current = acpi_create_madt_lapics(current);
63
64         /* Write SB800 IOAPIC, only one */
65         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
66                                 CONFIG_MAX_CPUS, IO_APIC_ADDR, 0);
67
68         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
69                         current, 0, 0, 2, 0);
70         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
71                         current, 0, 9, 9, 0xF);
72
73         /* 0: mean bus 0--->ISA */
74         /* 0: PIC 0 */
75         /* 2: APIC 2 */
76         /* 5 mean: 0101 --> Edige-triggered, Active high */
77
78         /* create all subtables for processors */
79         /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
80         /* 1: LINT1 connect to NMI */
81
82         return current;
83 }
84
85 unsigned long acpi_fill_slit(unsigned long current)
86 {
87         // Not implemented
88         return current;
89 }
90
91 unsigned long acpi_fill_srat(unsigned long current)
92 {
93         /* No NUMA, no SRAT */
94         return current;
95 }
96
97 unsigned long write_acpi_tables(unsigned long start)
98 {
99         unsigned long current;
100         acpi_rsdp_t *rsdp;
101         acpi_rsdt_t *rsdt;
102         acpi_hpet_t *hpet;
103         acpi_madt_t *madt;
104         acpi_srat_t *srat;
105         acpi_slit_t *slit;
106         acpi_fadt_t *fadt;
107         acpi_facs_t *facs;
108         acpi_header_t *dsdt;
109         acpi_header_t *ssdt;
110
111         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
112
113         /* Align ACPI tables to 16 bytes */
114         start = (start + 0x0f) & -0x10;
115         current = start;
116
117         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
118
119         /* We need at least an RSDP and an RSDT Table */
120         rsdp = (acpi_rsdp_t *) current;
121         current += sizeof(acpi_rsdp_t);
122         rsdt = (acpi_rsdt_t *) current;
123         current += sizeof(acpi_rsdt_t);
124
125         /* clear all table memory */
126         memset((void *)start, 0, current - start);
127
128         acpi_write_rsdp(rsdp, rsdt, NULL);
129         acpi_write_rsdt(rsdt);
130
131         /* DSDT */
132         current  = ( current + 0x07) & -0x08;
133         printk(BIOS_DEBUG, "ACPI:       * DSDT at %lx\n", current);
134         dsdt = (acpi_header_t *)current; // it will used by fadt
135         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
136         current += dsdt->length;
137         memcpy(dsdt, &AmlCode, dsdt->length);
138         printk(BIOS_DEBUG, "ACPI:       * DSDT @ %p Length %x\n",dsdt,dsdt->length);
139
140         /* FACS */ // it needs 64 bit alignment
141         current  = ( current + 0x07) & -0x08;
142         printk(BIOS_DEBUG, "ACPI: * FACS at %lx\n", current);
143         facs = (acpi_facs_t *) current; // it will be used by fadt
144         current += sizeof(acpi_facs_t);
145         acpi_create_facs(facs);
146
147         /* FDAT */
148         current  = ( current + 0x07) & -0x08;
149         printk(BIOS_DEBUG, "ACPI:       * FADT at %lx\n", current);
150         fadt = (acpi_fadt_t *) current;
151         current += sizeof(acpi_fadt_t);
152
153         acpi_create_fadt(fadt, facs, dsdt);
154         acpi_add_table(rsdp, fadt);
155
156         /*
157          * We explicitly add these tables later on:
158          */
159         current  = ( current + 0x07) & -0x08;
160         printk(BIOS_DEBUG, "ACPI:       * HPET at %lx\n", current);
161         hpet = (acpi_hpet_t *) current;
162         current += sizeof(acpi_hpet_t);
163         acpi_create_hpet(hpet);
164         acpi_add_table(rsdp, hpet);
165
166         /* If we want to use HPET Timers Linux wants an MADT */
167         current  = ( current + 0x07) & -0x08;
168         printk(BIOS_DEBUG, "ACPI:       * MADT at %lx\n",current);
169         madt = (acpi_madt_t *) current;
170         acpi_create_madt(madt);
171         current += madt->header.length;
172         acpi_add_table(rsdp, madt);
173
174         /* SRAT */
175         current  = ( current + 0x07) & -0x08;
176         printk(BIOS_DEBUG, "ACPI:       * SRAT at %lx\n", current);
177         srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
178         if (srat != NULL) {
179         memcpy((void *)current, srat, srat->header.length);
180         srat = (acpi_srat_t *) current;
181         //acpi_create_srat(srat);
182         current += srat->header.length;
183         acpi_add_table(rsdp, srat);
184         }
185
186         /* SLIT */
187         current  = ( current + 0x07) & -0x08;
188         printk(BIOS_DEBUG, "ACPI:        * SLIT at %lx\n", current);
189         slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
190         if (slit != NULL) {
191         memcpy((void *)current, slit, slit->header.length);
192         slit = (acpi_slit_t *) current;
193         //acpi_create_slit(slit);
194         current += slit->header.length;
195         acpi_add_table(rsdp, slit);
196         }
197
198         /* SSDT */
199         current  = ( current + 0x0f) & -0x10;
200         printk(BIOS_DEBUG, "ACPI:       * SSDT at %lx\n", current);
201         ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
202         if (ssdt != NULL) {
203                 memcpy((void *)current, ssdt, ssdt->length);
204                 ssdt = (acpi_header_t *) current;
205                 current += ssdt->length;
206         }
207         else {
208                 ssdt = (acpi_header_t *) current;
209                 memcpy(ssdt, &AmlCode_ssdt, sizeof(acpi_header_t));
210                 current += ssdt->length;
211                 memcpy(ssdt, &AmlCode_ssdt, ssdt->length);
212                  /* recalculate checksum */
213                 ssdt->checksum = 0;
214                 ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
215         }
216         acpi_add_table(rsdp,ssdt);
217
218         printk(BIOS_DEBUG, "ACPI:       * SSDT for PState at %lx\n", current);
219
220 #if DUMP_ACPI_TABLES == 1
221         printk(BIOS_DEBUG, "rsdp\n");
222         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
223
224         printk(BIOS_DEBUG, "rsdt\n");
225         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
226
227         printk(BIOS_DEBUG, "madt\n");
228         dump_mem(madt, ((void *)madt) + madt->header.length);
229
230         printk(BIOS_DEBUG, "srat\n");
231         dump_mem(srat, ((void *)srat) + srat->header.length);
232
233         printk(BIOS_DEBUG, "slit\n");
234         dump_mem(slit, ((void *)slit) + slit->header.length);
235
236         printk(BIOS_DEBUG, "ssdt\n");
237         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
238
239         printk(BIOS_DEBUG, "fadt\n");
240         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
241 #endif
242
243         printk(BIOS_INFO, "ACPI: done.\n");
244         return current;
245 }