d8b28b25e705a5f3bfba3f1d473d0018e0055f32
[coreboot.git] / src / mainboard / via / epia-n / acpi_tables.c
1 /*
2  * coreboot ACPI Table support
3  * written by Stefan Reinauer <stepan@openbios.org>
4  * ACPI FADT, FACS, and DSDT table support added by
5  * Nick Barker <nick.barker9@btinternet.com>, and those portions
6  * (C) Copyright 2004 Nick Barker
7  * (C) Copyright 2005 Stefan Reinauer
8  * (C) Copyright 2009 Jon Harrison <bothlyn@blueyonder.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24
25 /*
26  * Most parts of this file copied from via\epia-m\acpi_tables.c,
27  * and via\epia-m700\acpi_tables.c
28  */
29
30 #include <console/console.h>
31 #include <string.h>
32 #include <arch/acpi.h>
33 #include <device/device.h>
34 #include <device/pci.h>
35 #include <device/pci_ids.h>
36 #include "southbridge/via/vt8237r/vt8237r.h"
37
38 extern const unsigned char AmlCode[];
39
40 /*
41  * These four macros are copied from <arch/smp/mpspec.h>, I have to do this
42  * since the "default CONFIG_GENERATE_MP_TABLE = 0" in Options.lb, and also since
43  * mainboard/via/... have no mptable.c (so that I can not set
44  * CONFIG_GENERATE_MP_TABLE = 1) as many other mainboards.
45  * So I have to copy these four to here. acpi_fill_madt() needs this.
46  */
47 #define MP_IRQ_POLARITY_DEFAULT 0x0
48 #define MP_IRQ_POLARITY_HIGH    0x1
49 #define MP_IRQ_POLARITY_LOW             0x3
50 #define MP_IRQ_POLARITY_MASK    0x3
51 #define MP_IRQ_TRIGGER_DEFAULT  0x0
52 #define MP_IRQ_TRIGGER_EDGE             0x4
53 #define MP_IRQ_TRIGGER_LEVEL    0xc
54 #define MP_IRQ_TRIGGER_MASK     0xc
55
56 unsigned long acpi_fill_mcfg(unsigned long current)
57 {
58         /* Nothing to do */
59         return current;
60 }
61
62 unsigned long acpi_fill_slit(unsigned long current)
63 {
64         // Not implemented
65         return current;
66 }
67
68 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags,
69                                           u8 lint)
70 {
71         device_t cpu;
72         int cpu_index = 0;
73
74         for (cpu = all_devices; cpu; cpu = cpu->next) {
75                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
76                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
77                         continue;
78                 }
79                 if (!cpu->enabled)
80                         continue;
81                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
82                                               current, cpu_index, flags, lint);
83                 cpu_index++;
84         }
85         return current;
86 }
87
88 unsigned long acpi_fill_madt(unsigned long current)
89 {
90         unsigned int gsi_base = 0x00;
91
92         /* Create all subtables for processors. */
93         current = acpi_create_madt_lapics(current);
94
95         /* Write SB IOAPIC. */
96         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
97                                    VT8237R_APIC_ID, VT8237R_APIC_BASE, gsi_base);
98
99         /* IRQ0 -> APIC IRQ2. */
100         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
101                                                 current, 0, 0, 2, 0x0);
102
103         /* IRQ9 ACPI active low. */
104         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
105                 current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
106
107         /* Create all subtables for processors. */
108         current = acpi_create_madt_lapic_nmis(current,
109                               MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH, 1);
110
111         return current;
112 }
113
114 unsigned long acpi_fill_srat(unsigned long current)
115 {
116         /* No NUMA, no SRAT */
117         return current;
118 }
119
120 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
121 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
122 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
123
124 unsigned long write_acpi_tables(unsigned long start)
125 {
126         unsigned long current;
127         acpi_rsdp_t *rsdp;
128         acpi_rsdt_t *rsdt;
129         acpi_madt_t *madt;
130         acpi_fadt_t *fadt;
131         acpi_facs_t *facs;
132         acpi_header_t *dsdt;
133
134         /* Align ACPI tables to 16byte */
135         start   = ( start + 0x0f ) & -0x10;
136         current = start;
137
138         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
139
140         /* We need at least an RSDP and an RSDT Table */
141         rsdp = (acpi_rsdp_t *) current;
142         current += sizeof(acpi_rsdp_t);
143         rsdt = (acpi_rsdt_t *) current;
144         current += sizeof(acpi_rsdt_t);
145
146         /* clear all table memory */
147         memset((void *)start, 0, current - start);
148
149         acpi_write_rsdp(rsdp, rsdt, NULL);
150         acpi_write_rsdt(rsdt);
151
152         /*
153          * We explicitly add these tables later on:
154          */
155         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
156         current = ALIGN(current, 64);
157         facs = (acpi_facs_t *) current;
158         current += sizeof(acpi_facs_t);
159         acpi_create_facs(facs);
160
161         printk(BIOS_DEBUG, "ACPI:     * DSDT\n");
162         dsdt = (acpi_header_t *)current;
163         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
164         current += dsdt->length;
165         memcpy(dsdt, &AmlCode, dsdt->length);
166 #if 0
167         dsdt->checksum = 0; // don't trust intel iasl compiler to get this right
168         dsdt->checksum = acpi_checksum(dsdt,dsdt->length);
169 #endif
170         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n",dsdt,dsdt->length);
171         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
172
173         fadt = (acpi_fadt_t *) current;
174         current += sizeof(acpi_fadt_t);
175
176         acpi_create_fadt(fadt,facs,dsdt);
177         acpi_add_table(rsdp,fadt);
178
179         /* If we want IOAPIC Support Linux wants it in MADT. */
180         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
181         madt = (acpi_madt_t *) current;
182         acpi_create_madt(madt);
183         current += madt->header.length;
184         acpi_add_table(rsdp, madt);
185
186         printk(BIOS_INFO, "ACPI: done.\n");
187         return current;
188 }
189