printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / mainboard / intel / d945gclf / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  * 
4  * Copyright (C) 2007-2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by 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 <types.h>
21 #include <string.h>
22 #include <console/console.h>
23 #include <arch/acpi.h>
24 #include <arch/acpigen.h>
25 #include <arch/smp/mpspec.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
29 #include <cpu/x86/msr.h>
30 #include "dmi.h"
31
32 #define OLD_ACPI 0
33
34 extern unsigned char AmlCode[];
35 #if CONFIG_HAVE_ACPI_SLIC
36 unsigned long acpi_create_slic(unsigned long current);
37 #endif
38
39 #if OLD_ACPI
40 typedef struct acpi_oemb {
41         acpi_header_t header;
42         u8  ss;
43         u16 iost;
44         u32 topm;
45         u32 roms;
46         u32 mg1b;
47         u32 mg1l;
48         u32 mg2b;
49         u32 mg2l;
50         u8  rsvd;
51         u8  dmax;
52         u32 hpta;
53         u32 cpb0;
54         u32 cpb1;
55         u32 cpb2;
56         u32 cpb3;
57         u8  assb;
58         u8  aotb;
59         u32 aaxb;
60         u8  smif;
61         u8  dtse;
62         u8  dts1;
63         u8  dts2;
64         u8  mpen;
65 } __attribute__((packed)) acpi_oemb_t;
66 #endif
67
68 typedef struct acpi_gnvs {
69         // 0x00
70         u16 osys;
71         u8  smif;
72         u8  reserved[13];
73         // 0x10
74         u8  mpen;
75 } __attribute__((packed)) acpi_gnvs_t;
76
77 #if OLD_ACPI
78 void acpi_create_oemb(acpi_oemb_t *oemb)
79 {
80         acpi_header_t *header = &(oemb->header);
81         unsigned long tolud;
82
83         memset (oemb, 0, sizeof(*oemb));
84
85         /* fill out header fields */
86         memcpy(header->signature, "OEMB", 4);
87         memcpy(header->oem_id, OEM_ID, 6);
88         memcpy(header->oem_table_id, "COREBOOT", 8);
89         memcpy(header->asl_compiler_id, ASLC, 4);
90
91         header->length = sizeof(acpi_oemb_t);
92         header->revision = 1;
93
94         oemb->ss   =   0x09; // ss1 + ss 4
95         oemb->iost = 0x0403; // ??
96
97         tolud = pci_read_config32(dev_find_slot(0, PCI_DEVFN(2, 0)), 0x5c);
98         oemb->topm = tolud;
99
100         oemb->roms = 0xfff00000; // 1M hardcoded
101
102         oemb->mg1b = 0x000d0000;
103         oemb->mg1l = 0x00010000;
104
105         oemb->mg2b = tolud;
106         oemb->mg2l = 0-tolud;
107
108         oemb->dmax = 0x87;
109         oemb->hpta = 0x000e36c0;
110
111         header->checksum =
112             acpi_checksum((void *) oemb, sizeof(acpi_oemb_t));
113
114 };
115 #endif
116
117 void acpi_create_gnvs(acpi_gnvs_t *gnvs)
118 {
119         memset((void *)gnvs, 0, sizeof(*gnvs));
120         gnvs->mpen = 1;
121 }
122
123 void acpi_create_intel_hpet(acpi_hpet_t * hpet)
124 {
125 #define HPET_ADDR  0xfed00000ULL
126         acpi_header_t *header = &(hpet->header);
127         acpi_addr_t *addr = &(hpet->addr);
128
129         memset((void *) hpet, 0, sizeof(acpi_hpet_t));
130
131         /* fill out header fields */
132         memcpy(header->signature, "HPET", 4);
133         memcpy(header->oem_id, OEM_ID, 6);
134         memcpy(header->oem_table_id, "COREBOOT", 8);
135         memcpy(header->asl_compiler_id, ASLC, 4);
136
137         header->length = sizeof(acpi_hpet_t);
138         header->revision = 1;
139
140         /* fill out HPET address */
141         addr->space_id = 0;     /* Memory */
142         addr->bit_width = 64;
143         addr->bit_offset = 0;
144         addr->addrl = HPET_ADDR & 0xffffffff;
145         addr->addrh = HPET_ADDR >> 32;
146
147         hpet->id = 0x8086a201;  /* Intel */
148         hpet->number = 0x00;
149         hpet->min_tick = 0x0080;
150
151         header->checksum =
152             acpi_checksum((void *) hpet, sizeof(acpi_hpet_t));
153 }
154
155 #define IO_APIC_ADDR    0xfec00000UL
156
157 unsigned long acpi_fill_madt(unsigned long current)
158 {
159         /* Local APICs */
160         current = acpi_create_madt_lapics(current);
161
162         /* IOAPIC */
163         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
164                                 2, IO_APIC_ADDR, 0);
165
166         /* INT_SRC_OVR */
167         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
168                  current, 0, 0, 2, 0);
169         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
170                  current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH);
171
172         return current;
173 }
174
175 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
176 {
177         generate_cpu_entries();
178         return (unsigned long) (acpigen_get_current());
179 }
180
181 unsigned long acpi_fill_slit(unsigned long current)
182 {
183         // Not implemented
184         return current;
185 }
186
187 unsigned long acpi_fill_srat(unsigned long current)
188 {
189         /* No NUMA, no SRAT */
190         return current;
191 }
192
193 #if CONFIG_HAVE_SMI_HANDLER
194 void smm_setup_structures(void *gnvs, void *tcg, void *smi1);
195 #endif
196
197 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
198 unsigned long write_acpi_tables(unsigned long start)
199 {
200         unsigned long current;
201         int i;
202         acpi_rsdp_t *rsdp;
203         acpi_rsdt_t *rsdt;
204         acpi_hpet_t *hpet;
205         acpi_madt_t *madt;
206         acpi_mcfg_t *mcfg;
207         acpi_fadt_t *fadt;
208         acpi_facs_t *facs;
209 #if CONFIG_HAVE_ACPI_SLIC
210         acpi_header_t *slic;
211 #endif
212 #if OLD_ACPI
213         acpi_oemb_t *oemb;
214 #endif
215         acpi_gnvs_t *gnvs;
216         acpi_header_t *ssdt;
217         acpi_header_t *dsdt;
218
219         current = start;
220
221         /* Align ACPI tables to 16byte */
222         ALIGN_CURRENT;
223
224         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
225
226         /* We need at least an RSDP and an RSDT Table */
227         rsdp = (acpi_rsdp_t *) current;
228         current += sizeof(acpi_rsdp_t);
229         ALIGN_CURRENT;
230         rsdt = (acpi_rsdt_t *) current;
231         current += sizeof(acpi_rsdt_t);
232         ALIGN_CURRENT;
233
234         /* clear all table memory */
235         memset((void *) start, 0, current - start);
236
237         acpi_write_rsdp(rsdp, rsdt, NULL);
238         acpi_write_rsdt(rsdt);
239
240         /*
241          * We explicitly add these tables later on:
242          */
243         printk(BIOS_DEBUG, "ACPI:    * HPET\n");
244
245         hpet = (acpi_hpet_t *) current;
246         current += sizeof(acpi_hpet_t);
247         ALIGN_CURRENT;
248         acpi_create_intel_hpet(hpet);
249         acpi_add_table(rsdp, hpet);
250
251         /* If we want to use HPET Timers Linux wants an MADT */
252         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
253
254         madt = (acpi_madt_t *) current;
255         acpi_create_madt(madt);
256         current += madt->header.length;
257         ALIGN_CURRENT;
258         acpi_add_table(rsdp, madt);
259
260         printk(BIOS_DEBUG, "ACPI:    * MCFG\n");
261         mcfg = (acpi_mcfg_t *) current;
262         acpi_create_mcfg(mcfg);
263         current += mcfg->header.length;
264         ALIGN_CURRENT;
265         acpi_add_table(rsdp, mcfg);
266
267 #if OLD_ACPI
268         printk(BIOS_DEBUG, "ACPI:    * OEMB\n");
269         oemb=(acpi_oemb_t *)current;
270         current += sizeof(acpi_oemb_t);
271         ALIGN_CURRENT;
272         acpi_create_oemb(oemb);
273         acpi_add_table(rsdp, oemb);
274 #endif
275
276         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
277         facs = (acpi_facs_t *) current;
278         current += sizeof(acpi_facs_t);
279         ALIGN_CURRENT;
280         acpi_create_facs(facs);
281
282         dsdt = (acpi_header_t *) current;
283         current += ((acpi_header_t *) AmlCode)->length;
284         memcpy((void *) dsdt, (void *) AmlCode,
285                ((acpi_header_t *) AmlCode)->length);
286
287 #if OLD_ACPI
288         for (i=0; i < dsdt->length; i++) {
289                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBEEF) {
290                         printk(BIOS_DEBUG, "ACPI: Patching up DSDT at offset 0x%04x -> 0x%08x\n", i, 0x24 + (u32)oemb);
291                         *(u32*)(((u32)dsdt) + i) = 0x24 + (u32)oemb;
292                         break;
293                 }
294         }
295 #endif
296
297         ALIGN_CURRENT;
298
299         /* Pack GNVS into the ACPI table area */
300         for (i=0; i < dsdt->length; i++) {
301                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBABE) {
302                         printk(BIOS_DEBUG, "ACPI: Patching up global NVS in DSDT at offset 0x%04x -> 0x%08x\n", i, current);
303                         *(u32*)(((u32)dsdt) + i) = current; // 0x92 bytes
304                         break;
305                 }
306         }
307
308         /* And fill it */
309         acpi_create_gnvs(current);
310
311         current += 0x100;
312         ALIGN_CURRENT;
313
314 #if CONFIG_HAVE_SMI_HANDLER
315         /* And tell SMI about it */
316         smm_setup_structures((void *)current, NULL, NULL);
317 #endif
318
319         /* We patched up the DSDT, so we need to recalculate the checksum */
320         dsdt->checksum = 0;
321         dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
322
323         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n", dsdt,
324                      dsdt->length);
325
326 #if CONFIG_HAVE_ACPI_SLIC
327         printk(BIOS_DEBUG, "ACPI:     * SLIC\n");
328         slic = (acpi_header_t *)current;
329         current += acpi_create_slic(current);
330         ALIGN_CURRENT;
331         acpi_add_table(rsdp, slic);
332 #endif
333
334         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
335         fadt = (acpi_fadt_t *) current;
336         current += sizeof(acpi_fadt_t);
337         ALIGN_CURRENT;
338
339         acpi_create_fadt(fadt, facs, dsdt);
340         acpi_add_table(rsdp, fadt);
341
342         printk(BIOS_DEBUG, "ACPI:     * SSDT\n");
343         ssdt = (acpi_header_t *)current;
344         acpi_create_ssdt_generator(ssdt, "DYNADATA");
345         current += ssdt->length;
346         acpi_add_table(rsdp, ssdt);
347
348         printk(BIOS_DEBUG, "current = %lx\n", current);
349
350         printk(BIOS_DEBUG, "ACPI:     * DMI (Linux workaround)\n");
351         memcpy((void *)0xfff80, dmi_table, DMI_TABLE_SIZE);
352 #if CONFIG_WRITE_HIGH_TABLES == 1
353         memcpy((void *)current, dmi_table, DMI_TABLE_SIZE);
354         current += DMI_TABLE_SIZE;
355         ALIGN_CURRENT;
356 #endif
357
358         printk(BIOS_INFO, "ACPI: done.\n");
359         return current;
360 }