063127ba106b847db6bef1abc35748794377d3a9
[coreboot.git] / src / mainboard / ibase / mb899 / 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 modify
7  * it under the terms of the GNU General Public License as published by
8  * 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 extern const unsigned char AmlCode[];
33 #if CONFIG_HAVE_ACPI_SLIC
34 unsigned long acpi_create_slic(unsigned long current);
35 #endif
36
37 #include "southbridge/intel/i82801gx/i82801gx_nvs.h"
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 both COM ports */
45         gnvs->cmap = 0x01;
46         gnvs->cmbp = 0x01;
47
48         /* IGD Displays */
49         gnvs->ndid = 3;
50         gnvs->did[0] = 0x80000100;
51         gnvs->did[1] = 0x80000240;
52         gnvs->did[2] = 0x80000410;
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, "COREBOOT", 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 #define IO_APIC_ADDR    0xfec00000UL
90
91 unsigned long acpi_fill_madt(unsigned long current)
92 {
93         /* Local APICs */
94         current = acpi_create_madt_lapics(current);
95
96         /* IOAPIC */
97         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
98                                 2, IO_APIC_ADDR, 0);
99
100         /* INT_SRC_OVR */
101         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
102                  current, 0, 0, 2, 0);
103         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
104                  current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH);
105
106         return current;
107 }
108
109 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
110 {
111         generate_cpu_entries();
112         return (unsigned long) (acpigen_get_current());
113 }
114
115 unsigned long acpi_fill_slit(unsigned long current)
116 {
117         // Not implemented
118         return current;
119 }
120
121 unsigned long acpi_fill_srat(unsigned long current)
122 {
123         /* No NUMA, no SRAT */
124         return current;
125 }
126
127 void smm_setup_structures(void *gnvs, void *tcg, void *smi1);
128
129 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
130 unsigned long write_acpi_tables(unsigned long start)
131 {
132         unsigned long current;
133         int i;
134         acpi_rsdp_t *rsdp;
135         acpi_rsdt_t *rsdt;
136         acpi_xsdt_t *xsdt;
137         acpi_hpet_t *hpet;
138         acpi_madt_t *madt;
139         acpi_mcfg_t *mcfg;
140         acpi_fadt_t *fadt;
141         acpi_facs_t *facs;
142 #if CONFIG_HAVE_ACPI_SLIC
143         acpi_header_t *slic;
144 #endif
145         acpi_header_t *ssdt;
146         acpi_header_t *dsdt;
147
148         current = start;
149
150         /* Align ACPI tables to 16byte */
151         ALIGN_CURRENT;
152
153         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx.\n", start);
154
155         /* We need at least an RSDP and an RSDT Table */
156         rsdp = (acpi_rsdp_t *) current;
157         current += sizeof(acpi_rsdp_t);
158         ALIGN_CURRENT;
159         rsdt = (acpi_rsdt_t *) current;
160         current += sizeof(acpi_rsdt_t);
161         ALIGN_CURRENT;
162         xsdt = (acpi_xsdt_t *) current;
163         current += sizeof(acpi_xsdt_t);
164         ALIGN_CURRENT;
165
166         /* clear all table memory */
167         memset((void *) start, 0, current - start);
168
169         acpi_write_rsdp(rsdp, rsdt, xsdt);
170         acpi_write_rsdt(rsdt);
171         acpi_write_xsdt(xsdt);
172
173         /*
174          * We explicitly add these tables later on:
175          */
176         printk(BIOS_DEBUG, "ACPI:    * HPET\n");
177
178         hpet = (acpi_hpet_t *) current;
179         current += sizeof(acpi_hpet_t);
180         ALIGN_CURRENT;
181         acpi_create_intel_hpet(hpet);
182         acpi_add_table(rsdp, hpet);
183
184         /* If we want to use HPET Timers Linux wants an MADT */
185         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
186
187         madt = (acpi_madt_t *) current;
188         acpi_create_madt(madt);
189         current += madt->header.length;
190         ALIGN_CURRENT;
191         acpi_add_table(rsdp, madt);
192
193         printk(BIOS_DEBUG, "ACPI:    * MCFG\n");
194         mcfg = (acpi_mcfg_t *) current;
195         acpi_create_mcfg(mcfg);
196         current += mcfg->header.length;
197         ALIGN_CURRENT;
198         acpi_add_table(rsdp, mcfg);
199
200         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
201         facs = (acpi_facs_t *) current;
202         current += sizeof(acpi_facs_t);
203         ALIGN_CURRENT;
204         acpi_create_facs(facs);
205
206         dsdt = (acpi_header_t *) current;
207         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
208         current += dsdt->length;
209         memcpy(dsdt, &AmlCode, dsdt->length);
210
211         ALIGN_CURRENT;
212
213         /* Pack GNVS into the ACPI table area */
214         for (i=0; i < dsdt->length; i++) {
215                 if (*(u32*)(((u32)dsdt) + i) == 0xC0DEBABE) {
216                         printk(BIOS_DEBUG, "ACPI: Patching up global NVS in DSDT at offset 0x%04x -> 0x%08lx\n", i, current);
217                         *(u32*)(((u32)dsdt) + i) = current; // 0x92 bytes
218                         break;
219                 }
220         }
221
222         /* And fill it */
223         acpi_create_gnvs((global_nvs_t *)current);
224
225         current += 0x100;
226         ALIGN_CURRENT;
227
228         /* And tell SMI about it */
229         smm_setup_structures((void *)current, NULL, NULL);
230
231         /* We patched up the DSDT, so we need to recalculate the checksum */
232         dsdt->checksum = 0;
233         dsdt->checksum = acpi_checksum((void *)dsdt, dsdt->length);
234
235         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n", dsdt,
236                      dsdt->length);
237
238 #if CONFIG_HAVE_ACPI_SLIC
239         printk(BIOS_DEBUG, "ACPI:     * SLIC\n");
240         slic = (acpi_header_t *)current;
241         current += acpi_create_slic(current);
242         ALIGN_CURRENT;
243         acpi_add_table(rsdp, slic);
244 #endif
245
246         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
247         fadt = (acpi_fadt_t *) current;
248         current += sizeof(acpi_fadt_t);
249         ALIGN_CURRENT;
250
251         acpi_create_fadt(fadt, facs, dsdt);
252         acpi_add_table(rsdp, fadt);
253
254         printk(BIOS_DEBUG, "ACPI:     * SSDT\n");
255         ssdt = (acpi_header_t *)current;
256         acpi_create_ssdt_generator(ssdt, "COREBOOT");
257         current += ssdt->length;
258         acpi_add_table(rsdp, ssdt);
259         ALIGN_CURRENT;
260
261         printk(BIOS_DEBUG, "current = %lx\n", current);
262
263         printk(BIOS_DEBUG, "ACPI:     * DMI (Linux workaround)\n");
264         memcpy((void *)0xfff80, dmi_table, DMI_TABLE_SIZE);
265 #if CONFIG_WRITE_HIGH_TABLES == 1
266         memcpy((void *)current, dmi_table, DMI_TABLE_SIZE);
267         current += DMI_TABLE_SIZE;
268         ALIGN_CURRENT;
269 #endif
270
271         printk(BIOS_INFO, "ACPI: done.\n");
272         return current;
273 }