We define IO_APIC_ADDR in <arch/ioapic.h>, let's use it.
[coreboot.git] / src / mainboard / getac / p470 / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  * 
4  * Copyright (C) 2007-2010 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
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <types.h>
23 #include <string.h>
24 #include <console/console.h>
25 #include <arch/acpi.h>
26 #include <arch/ioapic.h>
27 #include <arch/acpigen.h>
28 #include <arch/smp/mpspec.h>
29 #include <device/device.h>
30 #include <device/pci.h>
31 #include <device/pci_ids.h>
32 #include "dmi.h"
33
34 extern unsigned char AmlCode[];
35
36 #include "southbridge/intel/i82801gx/i82801gx_nvs.h"
37
38 static void acpi_create_gnvs(global_nvs_t *gnvs)
39 {
40         memset((void *)gnvs, 0, sizeof(*gnvs));
41         gnvs->apic = 1;
42         gnvs->mpen = 1; /* Enable Multi Processing */
43
44         /* Enable COM port(s) */
45         gnvs->cmap = 0x01;
46         gnvs->cmbp = 0x00;
47
48         /* IGD Displays  */
49         gnvs->ndid = 2;
50         gnvs->did[0] = 0x80000100;
51         gnvs->did[1] = 0x80000410;
52         gnvs->did[2] = 0x80000320;
53         gnvs->did[3] = 0x80000410;
54         gnvs->did[4] = 0x00000005;
55 }
56
57 static void acpi_create_intel_hpet(acpi_hpet_t * hpet)
58 {
59 #define HPET_ADDR  0xfed00000ULL
60         acpi_header_t *header = &(hpet->header);
61         acpi_addr_t *addr = &(hpet->addr);
62
63         memset((void *) hpet, 0, sizeof(acpi_hpet_t));
64
65         /* fill out header fields */
66         memcpy(header->signature, "HPET", 4);
67         memcpy(header->oem_id, OEM_ID, 6);
68         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
69         memcpy(header->asl_compiler_id, ASLC, 4);
70
71         header->length = sizeof(acpi_hpet_t);
72         header->revision = 1;
73
74         /* fill out HPET address */
75         addr->space_id = 0;     /* Memory */
76         addr->bit_width = 64;
77         addr->bit_offset = 0;
78         addr->addrl = HPET_ADDR & 0xffffffff;
79         addr->addrh = HPET_ADDR >> 32;
80
81         hpet->id = 0x8086a201;  /* Intel */
82         hpet->number = 0x00;
83         hpet->min_tick = 0x0080;
84
85         header->checksum =
86             acpi_checksum((void *) hpet, sizeof(acpi_hpet_t));
87 }
88
89 static long acpi_create_ecdt(acpi_ecdt_t * ecdt)
90 {
91         /* Attention: Make sure these match the values from 
92          * the DSDT's ec.asl
93          */
94         static const char ec_id[] = "\\_SB.PCI0.LPCB.EC0";
95         int ecdt_len = sizeof(acpi_ecdt_t) + strlen(ec_id) + 1;
96
97         acpi_header_t *header = &(ecdt->header);
98
99         memset((void *) ecdt, 0, ecdt_len);
100
101         /* fill out header fields */
102         memcpy(header->signature, "ECDT", 4);
103         memcpy(header->oem_id, OEM_ID, 6);
104         memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
105         memcpy(header->asl_compiler_id, ASLC, 4);
106
107         header->length = ecdt_len;
108         header->revision = 1;
109
110         /* Location of the two EC registers */
111         ecdt->ec_control.space_id = ACPI_ADDRESS_SPACE_IO;
112         ecdt->ec_control.bit_width = 8;
113         ecdt->ec_control.bit_offset = 0;
114         ecdt->ec_control.addrl = 0x66;
115         ecdt->ec_control.addrh = 0;
116
117         ecdt->ec_data.space_id = ACPI_ADDRESS_SPACE_IO; /* Memory */
118         ecdt->ec_data.bit_width = 8;
119         ecdt->ec_data.bit_offset = 0;
120         ecdt->ec_data.addrl = 0x62;
121         ecdt->ec_data.addrh = 0;
122
123         ecdt->uid = 1; // Must match _UID of the EC0 node.
124         
125         ecdt->gpe_bit = 23; // SCI interrupt within GPEx_STS
126
127         strncpy((char *)ecdt->ec_id, ec_id, strlen(ec_id));
128
129         header->checksum =
130             acpi_checksum((void *) ecdt, ecdt_len);
131
132         return header->length;
133 }
134
135 unsigned long acpi_fill_madt(unsigned long current)
136 {
137         /* Local APICs */
138         current = acpi_create_madt_lapics(current);
139
140         /* IOAPIC */
141         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
142                                 2, IO_APIC_ADDR, 0);
143
144         /* INT_SRC_OVR */
145         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
146                  current, 0, 0, 2, 0);
147         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
148                  current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH);
149
150         /* LAPIC_NMI */
151         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
152                                 current, 0, 0x0005, 0x01);
153         current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
154                                 current, 1, 0x0005, 0x01);
155
156         return current;
157 }
158
159 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
160 {
161         generate_cpu_entries();
162         return (unsigned long) (acpigen_get_current());
163 }
164
165 unsigned long acpi_fill_slit(unsigned long current)
166 {
167         // Not implemented
168         return current;
169 }
170
171 unsigned long acpi_fill_srat(unsigned long current)
172 {
173         /* No NUMA, no SRAT */
174         return current;
175 }
176
177 void smm_setup_structures(void *gnvs, void *tcg, void *smi1);
178
179 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
180 unsigned long write_acpi_tables(unsigned long start)
181 {
182         unsigned long current;
183         int i;
184         acpi_rsdp_t *rsdp;
185         acpi_rsdt_t *rsdt;
186         acpi_xsdt_t *xsdt;
187         acpi_hpet_t *hpet;
188         acpi_madt_t *madt;
189         acpi_mcfg_t *mcfg;
190         acpi_fadt_t *fadt;
191         acpi_facs_t *facs;
192 #if CONFIG_HAVE_ACPI_SLIC
193         acpi_header_t *slic;
194 #endif
195         acpi_header_t *ssdt;
196         acpi_header_t *dsdt;
197         acpi_header_t *ecdt;
198
199         void *gnvs, *smi1;
200
201         current = start;
202
203         /* Align ACPI tables to 16byte */
204         ALIGN_CURRENT;
205
206         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
207
208         /* We need at least an RSDP and an RSDT Table */
209         rsdp = (acpi_rsdp_t *) current;
210         current += sizeof(acpi_rsdp_t);
211         ALIGN_CURRENT;
212         rsdt = (acpi_rsdt_t *) current;
213         current += sizeof(acpi_rsdt_t);
214         ALIGN_CURRENT;
215         xsdt = (acpi_xsdt_t *) current;
216         current += sizeof(acpi_xsdt_t);
217         ALIGN_CURRENT;
218
219         /* clear all table memory */
220         memset((void *) start, 0, current - start);
221
222         acpi_write_rsdp(rsdp, rsdt, xsdt);
223         acpi_write_rsdt(rsdt);
224         acpi_write_xsdt(xsdt);
225
226         /*
227          * We explicitly add these tables later on:
228          */
229         printk(BIOS_DEBUG, "ACPI:    * HPET\n");
230
231         hpet = (acpi_hpet_t *) current;
232         current += sizeof(acpi_hpet_t);
233         ALIGN_CURRENT;
234         acpi_create_intel_hpet(hpet);
235         acpi_add_table(rsdp, hpet);
236
237         /* If we want to use HPET Timers Linux wants an MADT */
238         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
239
240         madt = (acpi_madt_t *) current;
241         acpi_create_madt(madt);
242         current += madt->header.length;
243         ALIGN_CURRENT;
244         acpi_add_table(rsdp, madt);
245
246         printk(BIOS_DEBUG, "ACPI:    * MCFG\n");
247         mcfg = (acpi_mcfg_t *) current;
248         acpi_create_mcfg(mcfg);
249         current += mcfg->header.length;
250         ALIGN_CURRENT;
251         acpi_add_table(rsdp, mcfg);
252
253         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
254         facs = (acpi_facs_t *) current;
255         current += sizeof(acpi_facs_t);
256         ALIGN_CURRENT;
257         acpi_create_facs(facs);
258
259         dsdt = (acpi_header_t *) current;
260         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
261         current += dsdt->length;
262         memcpy(dsdt, &AmlCode, dsdt->length);
263
264         /* Fix up global NVS region for SMI handler. The GNVS region lives 
265          * in the (high) table area. The low memory map looks like this:
266          *
267          * 0x00000000 - 0x000003ff      Real Mode IVT
268          * 0x00000400 - 0x000004ff      BDA (somewhat unused)
269          * 0x00000500 - 0x00000518      coreboot table forwarder
270          * 0x00000600 - 0x00000???      realmode trampoline
271          * 0x0007c000 - 0x0007dfff      OS boot sector (unused?)
272          * 0x0007e000 - 0x0007ffff      free to use (so no good for acpi+smi)
273          * 0x00080000 - 0x0009fbff      usable ram
274          * 0x0009fc00 - 0x0009ffff      EBDA (unused?)
275          * 0x000a0000 - 0x000bffff      VGA memory
276          * 0x000c0000 - 0x000cffff      VGA option rom
277          * 0x000d0000 - 0x000dffff      free for other option roms?
278          * 0x000e0000 - 0x000fffff      SeaBIOS? (conflict with low tables:)
279          * 0x000f0000 - 0x000f03ff      PIRQ table
280          * 0x000f0400 - 0x000f66??      ACPI tables
281          * 0x000f66?? - 0x000f????      DMI tables
282          */
283
284         ALIGN_CURRENT;
285
286         /* Pack GNVS into the ACPI table area */
287         for (i=0; i < dsdt->length; i++) {
288                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBABE) {
289                         printk(BIOS_DEBUG, "ACPI: Patching up global NVS in DSDT at offset 0x%04x -> 0x%08x\n", i, (u32)current);
290                         *(u32*)(((u32)dsdt) + i) = current; // 0x92 bytes
291                         break;
292                 }
293         }
294
295         /* And fill it */
296         acpi_create_gnvs((global_nvs_t *)current);
297
298         /* Keep pointer around */
299         gnvs = (void *)current;
300
301         current += 0x100;
302         ALIGN_CURRENT;
303
304         for (i=0; i < dsdt->length; i++) {
305                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEDEAD) {
306                         printk(BIOS_DEBUG, "ACPI: Patching up SMI1 area in DSDT at offset 0x%04x -> 0x%08x\n", i, (u32)current);
307                         *(u32*)(((u32)dsdt) + i) = current; // 0x100 bytes
308                         break;
309                 }
310         }
311
312         /* Keep pointer around */
313         smi1 = (void *)current;
314
315         current += 0x100;
316         ALIGN_CURRENT;
317         
318         /* And tell SMI about it */
319         smm_setup_structures(gnvs, NULL, smi1);
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(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n", dsdt,
326                      dsdt->length);
327
328         printk(BIOS_DEBUG, "ACPI:     * ECDT\n");
329         ecdt = (acpi_header_t *)current;
330         current += acpi_create_ecdt((acpi_ecdt_t *)current);
331         ALIGN_CURRENT;
332         acpi_add_table(rsdp, ecdt);
333
334 #if CONFIG_HAVE_ACPI_SLIC
335         printk(BIOS_DEBUG, "ACPI:     * SLIC\n");
336         slic = (acpi_header_t *)current;
337         current += acpi_create_slic(current);
338         ALIGN_CURRENT;
339         acpi_add_table(rsdp, slic);
340 #endif
341
342         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
343         fadt = (acpi_fadt_t *) current;
344         current += sizeof(acpi_fadt_t);
345         ALIGN_CURRENT;
346
347         acpi_create_fadt(fadt, facs, dsdt);
348         acpi_add_table(rsdp, fadt);
349
350         printk(BIOS_DEBUG, "ACPI:     * SSDT\n");
351         ssdt = (acpi_header_t *)current;
352         acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
353         current += ssdt->length;
354         acpi_add_table(rsdp, ssdt);
355         ALIGN_CURRENT;
356
357         printk(BIOS_DEBUG, "current = %lx\n", current);
358
359         printk(BIOS_DEBUG, "ACPI:     * DMI (Linux workaround)\n");
360         memcpy((void *)0xfff80, dmi_table, DMI_TABLE_SIZE);
361 #if CONFIG_WRITE_HIGH_TABLES == 1
362         memcpy((void *)current, dmi_table, DMI_TABLE_SIZE);
363         current += DMI_TABLE_SIZE;
364         ALIGN_CURRENT;
365 #endif
366
367         printk(BIOS_INFO, "ACPI: done.\n");
368         return current;
369 }