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