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