Add constants for fast path resume copying
[coreboot.git] / src / mainboard / supermicro / h8qgi / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 - 2012 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 <arch/io.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <cpu/x86/msr.h>
29 #include <cpu/amd/mtrr.h>
30 #include <cpu/amd/amdfam10_sysconf.h>
31 #include "agesawrapper.h"
32
33 #define DUMP_ACPI_TABLES 0
34
35 #if DUMP_ACPI_TABLES == 1
36 static void dump_mem(u32 start, u32 end)
37 {
38
39         u32 i;
40         print_debug("dump_mem:");
41         for (i = start; i < end; i++) {
42                 if ((i & 0xf) == 0) {
43                         printk(BIOS_DEBUG, "\n%08x:", i);
44                 }
45                 printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
46         }
47         print_debug("\n");
48 }
49 #endif
50
51 extern const unsigned char AmlCode[];
52
53
54 unsigned long acpi_fill_mcfg(unsigned long current)
55 {
56         /* Just a dummy */
57         return current;
58 }
59
60 unsigned long acpi_fill_madt(unsigned long current)
61 {
62         device_t dev;
63         u32 dword;
64         u32 gsi_base = 0;
65         u32 apicid_sp5100;
66         u32 apicid_sr5650;
67
68         /*
69          * AGESA v5 Apply apic enumeration rules
70          * For systems with >= 16 APICs, put the IO-APICs at 0..n and
71          * put the local-APICs at m..z
72          * For systems with < 16 APICs, put the Local-APICs at 0..n and
73          * put the IO-APICs at (n + 1)..z
74          */
75 #if CONFIG_MAX_CPUS >= 16
76         apicid_sp5100 = 0x0;
77 #else
78         apicid_sp5100 = CONFIG_MAX_CPUS + 1
79 #endif
80                 apicid_sr5650 = apicid_sp5100 + 1;
81
82         /* create all subtables for processors */
83         current = acpi_create_madt_lapics(current);
84
85         /* Write sp5100 IOAPIC, only one */
86         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
87                         apicid_sp5100,
88                         IO_APIC_ADDR,
89                         0
90                         );
91
92         /* IOAPIC on rs5690 */
93         gsi_base += IO_APIC_INTERRUPTS;  /* SP5100 has 24 IOAPIC entries. */
94         dev = dev_find_slot(0, PCI_DEVFN(0, 0));
95         if (dev) {
96                 pci_write_config32(dev, 0xF8, 0x1);
97                 dword = pci_read_config32(dev, 0xFC) & 0xfffffff0;
98                 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
99                                 apicid_sr5650,
100                                 dword,
101                                 gsi_base
102                                 );
103         }
104
105         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *) current,
106                         0, //BUS
107                         0, //SOURCE
108                         2, //gsirq
109                         0  //flags
110                         );
111
112         /* 0: mean bus 0--->ISA */
113         /* 0: PIC 0 */
114         /* 2: APIC 2 */
115         /* 5 mean: 0101 --> Edige-triggered, Active high */
116
117         /* create all subtables for processors */
118         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, 0, 5, 1);
119         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, 1, 5, 1);
120         /* 1: LINT1 connect to NMI */
121
122         return current;
123 }
124
125 unsigned long acpi_fill_slit(unsigned long current)
126 {
127         // Not implemented
128         return current;
129 }
130
131 unsigned long acpi_fill_srat(unsigned long current)
132 {
133         /* No NUMA, no SRAT */
134         return current;
135 }
136
137 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
138 {
139         int lens;
140         msr_t msr;
141         char pscope[] = "\\_SB.PCI0";
142
143         lens = acpigen_write_scope(pscope);
144         msr = rdmsr(TOP_MEM);
145         lens += acpigen_write_name_dword("TOM1", msr.lo);
146         msr = rdmsr(TOP_MEM2);
147         /*
148          * Since XP only implements parts of ACPI 2.0, we can't use a qword
149          * here.
150          * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
151          * slide 22ff.
152          * Shift value right by 20 bit to make it fit into 32bit,
153          * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
154          */
155         lens += acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
156         acpigen_patch_len(lens - 1);
157         return (unsigned long) (acpigen_get_current());
158 }
159
160 unsigned long write_acpi_tables(unsigned long start)
161 {
162         unsigned long current;
163         acpi_rsdp_t *rsdp;
164         acpi_rsdt_t *rsdt;
165         //acpi_hpet_t *hpet;
166         acpi_madt_t *madt;
167         acpi_srat_t *srat;
168         acpi_slit_t *slit;
169         acpi_fadt_t *fadt;
170         acpi_facs_t *facs;
171         acpi_header_t *dsdt;
172         acpi_header_t *ssdt;
173         acpi_header_t *ssdt2;
174         acpi_header_t *alib;
175
176         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
177
178         /* Align ACPI tables to 16 bytes */
179         start = (start + 0x0f) & -0x10;
180         current = start;
181
182         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
183
184         /* We need at least an RSDP and an RSDT Table */
185         rsdp = (acpi_rsdp_t *) current;
186         current += sizeof(acpi_rsdp_t);
187         rsdt = (acpi_rsdt_t *) current;
188         current += sizeof(acpi_rsdt_t);
189
190         /* clear all table memory */
191         memset((void *)start, 0, current - start);
192
193         acpi_write_rsdp(rsdp, rsdt, NULL);
194         acpi_write_rsdt(rsdt);
195
196         /* FACS */
197         printk(BIOS_DEBUG, "ACPI:    * FACS\n");
198         facs = (acpi_facs_t *) current;
199         current += sizeof(acpi_facs_t);
200         acpi_create_facs(facs);
201
202         /* DSDT */
203         printk(BIOS_DEBUG, "ACPI:    * DSDT\n");
204         dsdt = (acpi_header_t *)current;
205         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
206         current += dsdt->length;
207         memcpy(dsdt, &AmlCode, dsdt->length);
208         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n", dsdt, dsdt->length);
209         /* FADT */
210         printk(BIOS_DEBUG, "ACPI:    * FADT\n");
211         fadt = (acpi_fadt_t *) current;
212         current += sizeof(acpi_fadt_t);
213
214         acpi_create_fadt(fadt, facs, dsdt);
215         acpi_add_table(rsdp, fadt);
216
217         /*
218          * We explicitly add these tables later on:
219          */
220 #ifdef UNUSED_CODE // Don't need HPET table. we have one in dsdt
221         current   = ( current + 0x07) & -0x08;
222         printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
223         hpet = (acpi_hpet_t *) current;
224         current += sizeof(acpi_hpet_t);
225         acpi_create_hpet(hpet);
226         acpi_add_table(rsdp, hpet);
227 #endif
228
229         /* If we want to use HPET Timers Linux wants an MADT */
230         current   = ( current + 0x07) & -0x08;
231         printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n",current);
232         madt = (acpi_madt_t *) current;
233         acpi_create_madt(madt);
234         current += madt->header.length;
235         acpi_add_table(rsdp, madt);
236
237         /* SRAT */
238         current   = ( current + 0x07) & -0x08;
239         printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
240         srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
241         if (srat != NULL) {
242                 memcpy((void *)current, srat, srat->header.length);
243                 srat = (acpi_srat_t *) current;
244                 //acpi_create_srat(srat);
245                 current += srat->header.length;
246                 acpi_add_table(rsdp, srat);
247         }
248
249         /* SLIT */
250         current   = ( current + 0x07) & -0x08;
251         printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
252         slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
253         if (slit != NULL) {
254                 memcpy((void *)current, slit, slit->header.length);
255                 slit = (acpi_slit_t *) current;
256                 //acpi_create_slit(slit);
257                 current += slit->header.length;
258                 acpi_add_table(rsdp, slit);
259         }
260
261         /* SSDT */
262         current  = (current + 0x0f) & -0x10;
263         printk(BIOS_DEBUG, "ACPI:  * AGESA ALIB SSDT at %lx\n", current);
264         alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
265         if (alib != NULL) {
266                 memcpy((void *)current, alib, alib->length);
267                 ssdt = (acpi_header_t *) current;
268                 current += alib->length;
269                 acpi_add_table(rsdp,alib);
270         } else {
271                 printk(BIOS_DEBUG, "    AGESA ALIB SSDT table NULL. Skipping.\n");
272         }
273
274 #if 0 // The DSDT needs additional work for the AGESA SSDT Pstate table
275         current  = ( current + 0x0f) & -0x10;
276         printk(BIOS_DEBUG, "ACPI:  * AGESA SSDT Pstate at %lx\n", current);
277         ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
278         if (ssdt != NULL) {
279                 memcpy((void *)current, ssdt, ssdt->length);
280                 ssdt = (acpi_header_t *) current;
281                 current += ssdt->length;
282         } else {
283                 printk(BIOS_DEBUG, "  AGESA SSDT table NULL. Skipping.\n");
284         }
285         acpi_add_table(rsdp,ssdt);
286 #endif
287
288         current  = ( current + 0x0f) & -0x10;
289         printk(BIOS_DEBUG, "ACPI:  * coreboot TOM SSDT2 at %lx\n", current);
290         ssdt2 = (acpi_header_t *) current;
291         acpi_create_ssdt_generator(ssdt2, ACPI_TABLE_CREATOR);
292         current += ssdt2->length;
293         acpi_add_table(rsdp,ssdt2);
294
295 #if DUMP_ACPI_TABLES == 1
296         printk(BIOS_DEBUG, "rsdp\n");
297         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
298
299         printk(BIOS_DEBUG, "rsdt\n");
300         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
301
302         printk(BIOS_DEBUG, "madt\n");
303         dump_mem(madt, ((void *)madt) + madt->header.length);
304
305         printk(BIOS_DEBUG, "srat\n");
306         dump_mem(srat, ((void *)srat) + srat->header.length);
307
308         printk(BIOS_DEBUG, "slit\n");
309         dump_mem(slit, ((void *)slit) + slit->header.length);
310
311         printk(BIOS_DEBUG, "ssdt\n");
312         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
313
314         printk(BIOS_DEBUG, "fadt\n");
315         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
316 #endif
317
318         printk(BIOS_INFO, "ACPI: done.\n");
319         return current;
320 }