AMD Mahogany Fam10 ACPI table fixes.
[coreboot.git] / src / mainboard / amd / mahogany_fam10 / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010-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/acpigen.h>
24 #include <arch/ioapic.h>
25 #include <device/pci.h>
26 #include <device/pci_ids.h>
27 #include <cpu/x86/msr.h>
28 #include <cpu/amd/mtrr.h>
29 #include <cpu/amd/amdfam10_sysconf.h>
30 #include "mb_sysconf.h"
31
32 #define DUMP_ACPI_TABLES 0
33
34 #if DUMP_ACPI_TABLES == 1
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_ssdt_generator(unsigned long current, const char *oem_table_id)
54 {
55         int lens;
56         msr_t msr;
57         char pscope[] = "\\_SB.PCI0";
58
59         lens = acpigen_write_scope(pscope);
60         msr = rdmsr(TOP_MEM);
61         lens += acpigen_write_name_dword("TOM1", msr.lo);
62         msr = rdmsr(TOP_MEM2);
63         /*
64          * Since XP only implements parts of ACPI 2.0, we can't use a qword
65          * here.
66          * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
67          * slide 22ff.
68          * Shift value right by 20 bit to make it fit into 32bit,
69          * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
70          */
71         lens += acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
72         acpigen_patch_len(lens - 1);
73
74         /* TODO: More HT and other tables need to go into this table generation.
75          * This should also be moved out to the silicon level if it can.
76          */
77
78         return (unsigned long) (acpigen_get_current());
79 }
80
81 unsigned long acpi_fill_mcfg(unsigned long current)
82 {
83         /* Just a dummy */
84         return current;
85 }
86
87 unsigned long acpi_fill_madt(unsigned long current)
88 {
89         /* create all subtables for processors */
90         current = acpi_create_madt_lapics(current);
91
92         /* Write SB700 IOAPIC, only one */
93         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
94                                 CONFIG_MAX_CPUS, IO_APIC_ADDR, 0);
95
96         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
97                                                 current, 0, 0, 2, 0);
98         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
99                                                 current, 0, 9, 9, 0xF);
100         /* 0: mean bus 0--->ISA */
101         /* 0: PIC 0 */
102         /* 2: APIC 2 */
103         /* 5 mean: 0101 --> Edige-triggered, Active high */
104
105         /* create all subtables for processors */
106         /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
107         /* 1: LINT1 connect to NMI */
108
109         return current;
110 }
111
112 unsigned long write_acpi_tables(unsigned long start)
113 {
114         unsigned long current;
115         acpi_rsdp_t *rsdp;
116         acpi_rsdt_t *rsdt;
117         acpi_hpet_t *hpet;
118         acpi_madt_t *madt;
119         acpi_srat_t *srat;
120         acpi_slit_t *slit;
121         acpi_fadt_t *fadt;
122         acpi_facs_t *facs;
123         acpi_header_t *dsdt;
124         acpi_header_t *ssdt;
125
126         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
127
128         /* Align ACPI tables to 16 bytes */
129         start = (start + 0x0f) & -0x10;
130         current = start;
131
132         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
133
134         /* We need at least an RSDP and an RSDT Table */
135         rsdp = (acpi_rsdp_t *) current;
136         current += sizeof(acpi_rsdp_t);
137         rsdt = (acpi_rsdt_t *) current;
138         current += sizeof(acpi_rsdt_t);
139
140         /* clear all table memory */
141         memset((void *)start, 0, current - start);
142
143         acpi_write_rsdp(rsdp, rsdt, NULL);
144         acpi_write_rsdt(rsdt);
145
146         /* DSDT */
147         current   = ( current + 0x07) & -0x08;
148         printk(BIOS_DEBUG, "ACPI:    * DSDT at %lx\n", current);
149         dsdt = (acpi_header_t *)current; // it will used by fadt
150         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
151         current += dsdt->length;
152         memcpy(dsdt, &AmlCode, dsdt->length);
153         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n",dsdt,dsdt->length);
154
155         /* FACS */ // it needs 64 bit alignment
156         current   = ( current + 0x07) & -0x08;
157         printk(BIOS_DEBUG, "ACPI:       * FACS at %lx\n", current);
158         facs = (acpi_facs_t *) current; // it will be used by fadt
159         current += sizeof(acpi_facs_t);
160         acpi_create_facs(facs);
161
162         /* FADT */
163         current   = ( current + 0x07) & -0x08;
164         printk(BIOS_DEBUG, "ACPI:    * FADT at %lx\n", current);
165         fadt = (acpi_fadt_t *) current;
166         current += sizeof(acpi_fadt_t);
167
168         acpi_create_fadt(fadt, facs, dsdt);
169         acpi_add_table(rsdp, fadt);
170
171         /*
172          * We explicitly add these tables later on:
173          */
174         current   = ( current + 0x07) & -0x08;
175         printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
176         hpet = (acpi_hpet_t *) current;
177         current += sizeof(acpi_hpet_t);
178         acpi_create_hpet(hpet);
179         acpi_add_table(rsdp, hpet);
180
181         /* If we want to use HPET Timers Linux wants an MADT */
182         current   = ( current + 0x07) & -0x08;
183         printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n",current);
184         madt = (acpi_madt_t *) current;
185         acpi_create_madt(madt);
186         current += madt->header.length;
187         acpi_add_table(rsdp, madt);
188
189         /* SRAT */
190         current   = ( current + 0x07) & -0x08;
191         printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
192         srat = (acpi_srat_t *) current;
193         acpi_create_srat(srat);
194         current += srat->header.length;
195         acpi_add_table(rsdp, srat);
196
197         /* SLIT */
198         current   = ( current + 0x07) & -0x08;
199         printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
200         slit = (acpi_slit_t *) current;
201         acpi_create_slit(slit);
202         current += slit->header.length;
203         acpi_add_table(rsdp, slit);
204
205         /* SSDT */
206         current  = ( current + 0x0f) & -0x10;
207         printk(BIOS_DEBUG, "ACPI:  * coreboot PSTATE/TOM SSDT at %lx\n", current);
208         ssdt = (acpi_header_t *) current;
209         acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
210         current += ssdt->length;
211         acpi_add_table(rsdp,ssdt);
212
213
214 #if DUMP_ACPI_TABLES == 1
215         printk(BIOS_DEBUG, "rsdp\n");
216         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
217
218         printk(BIOS_DEBUG, "rsdt\n");
219         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
220
221         printk(BIOS_DEBUG, "madt\n");
222         dump_mem(madt, ((void *)madt) + madt->header.length);
223
224         printk(BIOS_DEBUG, "srat\n");
225         dump_mem(srat, ((void *)srat) + srat->header.length);
226
227         printk(BIOS_DEBUG, "slit\n");
228         dump_mem(slit, ((void *)slit) + slit->header.length);
229
230         printk(BIOS_DEBUG, "ssdt\n");
231         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
232
233         printk(BIOS_DEBUG, "fadt\n");
234         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
235 #endif
236
237         printk(BIOS_INFO, "ACPI: done.\n");
238         return current;
239 }