589e14c60653353b1cb2c1589449d40605173fdf
[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 <arch/ioapic.h>
34 #include <device/device.h>
35 #include <device/pci.h>
36 #include <device/pci_ids.h>
37 #include "southbridge/via/vt8237r/vt8237r.h"
38
39 extern const unsigned char AmlCode[];
40
41 /*
42  * These 8 macros are copied from <arch/smp/mpspec.h>, I have to do this
43  * since the "CONFIG_GENERATE_MP_TABLE = 0", and also since
44  * mainboard/via/... have no mptable.c (so that I can not set
45  * "CONFIG_GENERATE_MP_TABLE = 1" as many other mainboards.
46  * So I have to copy these four to here. acpi_fill_madt() needs this.
47  */
48 #define MP_IRQ_POLARITY_DEFAULT 0x0
49 #define MP_IRQ_POLARITY_HIGH    0x1
50 #define MP_IRQ_POLARITY_LOW             0x3
51 #define MP_IRQ_POLARITY_MASK    0x3
52 #define MP_IRQ_TRIGGER_DEFAULT  0x0
53 #define MP_IRQ_TRIGGER_EDGE             0x4
54 #define MP_IRQ_TRIGGER_LEVEL    0xc
55 #define MP_IRQ_TRIGGER_MASK     0xc
56
57 unsigned long acpi_fill_mcfg(unsigned long current)
58 {
59         /* Nothing to do */
60         return current;
61 }
62
63 unsigned long acpi_fill_slit(unsigned long current)
64 {
65         // Not implemented
66         return current;
67 }
68
69 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags,
70                                           u8 lint)
71 {
72         device_t cpu;
73         int cpu_index = 0;
74
75         for (cpu = all_devices; cpu; cpu = cpu->next) {
76                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
77                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
78                         continue;
79                 }
80                 if (!cpu->enabled)
81                         continue;
82                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)
83                                               current, cpu_index, flags, lint);
84                 cpu_index++;
85         }
86         return current;
87 }
88
89 unsigned long acpi_fill_madt(unsigned long current)
90 {
91         unsigned int gsi_base = 0x00;
92
93         /* Create all subtables for processors. */
94         current = acpi_create_madt_lapics(current);
95
96         /* Write SB IOAPIC. */
97         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
98                                    VT8237R_APIC_ID, IO_APIC_ADDR, gsi_base);
99
100         /* IRQ0 -> APIC IRQ2. */
101         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
102                                                 current, 0, 0, 2, 0x0);
103
104         /* IRQ9 ACPI active low. */
105         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
106                 current, 0, 9, 9, MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_LOW);
107
108         /* Create all subtables for processors. */
109         current = acpi_create_madt_lapic_nmis(current,
110                               MP_IRQ_TRIGGER_EDGE | MP_IRQ_POLARITY_HIGH, 1);
111
112         return current;
113 }
114
115 unsigned long acpi_fill_srat(unsigned long current)
116 {
117         /* No NUMA, no SRAT */
118         return current;
119 }
120
121 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
122 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
123 #define ALIGN_CURRENT current = ((current + 0x0f) & -0x10)
124
125 unsigned long write_acpi_tables(unsigned long start)
126 {
127         unsigned long current;
128         acpi_rsdp_t *rsdp;
129         acpi_rsdt_t *rsdt;
130         acpi_madt_t *madt;
131         acpi_fadt_t *fadt;
132         acpi_facs_t *facs;
133         acpi_header_t *dsdt;
134
135         /* Align ACPI tables to 16byte */
136         start   = ( start + 0x0f ) & -0x10;
137         current = start;
138
139         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
140
141         /* We need at least an RSDP and an RSDT Table */
142         rsdp = (acpi_rsdp_t *) current;
143         current += sizeof(acpi_rsdp_t);
144         rsdt = (acpi_rsdt_t *) current;
145         current += sizeof(acpi_rsdt_t);
146
147         /* clear all table memory */
148         memset((void *)start, 0, current - start);
149
150         acpi_write_rsdp(rsdp, rsdt, NULL);
151         acpi_write_rsdt(rsdt);
152
153         /*
154          * We explicitly add these tables later on:
155          */
156         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
157         current = ALIGN(current, 64);
158         facs = (acpi_facs_t *) current;
159         current += sizeof(acpi_facs_t);
160         acpi_create_facs(facs);
161
162         printk(BIOS_DEBUG, "ACPI:     * DSDT\n");
163         dsdt = (acpi_header_t *)current;
164         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
165         current += dsdt->length;
166         memcpy(dsdt, &AmlCode, dsdt->length);
167 #if 0
168         dsdt->checksum = 0; // don't trust intel iasl compiler to get this right
169         dsdt->checksum = acpi_checksum(dsdt,dsdt->length);
170 #endif
171         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n",dsdt,dsdt->length);
172         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
173
174         fadt = (acpi_fadt_t *) current;
175         current += sizeof(acpi_fadt_t);
176
177         acpi_create_fadt(fadt,facs,dsdt);
178         acpi_add_table(rsdp,fadt);
179
180         /* If we want IOAPIC Support Linux wants it in MADT. */
181         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
182         madt = (acpi_madt_t *) current;
183         acpi_create_madt(madt);
184         current += madt->header.length;
185         acpi_add_table(rsdp, madt);
186
187         printk(BIOS_INFO, "ACPI: done.\n");
188         return current;
189 }
190