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