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