Use default table creator macro for all SSDTs
[coreboot.git] / src / southbridge / intel / i82371eb / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2004 Stefan Reinauer <stepan@openbios.org>
5  * Copyright (C) 2005 Nick Barker <nick.barker9@btinternet.com>
6  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <console/console.h>
23 #include <string.h>
24 #include <arch/acpi.h>
25 #include <arch/acpigen.h>
26 #include <arch/smp/mpspec.h>
27 #include <device/device.h>
28 #include <device/pci_ids.h>
29 #include "i82371eb.h"
30
31 extern const unsigned char AmlCode[];
32
33 static int determine_total_number_of_cores(void)
34 {
35         device_t cpu;
36         int count = 0;
37         for(cpu = all_devices; cpu; cpu = cpu->next) {
38                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
39                         (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
40                         continue;
41                 }
42                 if (!cpu->enabled) {
43                         continue;
44                 }
45                 count++;
46         }
47         return count;
48 }
49
50 void generate_cpu_entries(void)
51 {
52         int len;
53         int len_pr;
54         int cpu, pcontrol_blk=DEFAULT_PMBASE+PCNTRL, plen=6;
55         int numcpus = determine_total_number_of_cores();
56         printk(BIOS_DEBUG, "Found %d CPU(s).\n", numcpus);
57
58         /* without the outer scope, furhter ssdt addition will end up
59          * within the processor statement */
60         len = acpigen_write_scope("\\_PR");
61         for (cpu=0; cpu < numcpus; cpu++) {
62                 len_pr = acpigen_write_processor(cpu, pcontrol_blk, plen);
63                 acpigen_patch_len(len_pr - 1);
64                 len += len_pr;
65         }
66         acpigen_patch_len(len - 1);
67 }
68
69 unsigned long __attribute__((weak)) acpi_fill_slit(unsigned long current)
70 {
71         // Not implemented
72         return current;
73 }
74
75 unsigned long __attribute__((weak)) acpi_fill_srat(unsigned long current)
76 {
77         // Not implemented
78         return current;
79 }
80
81 unsigned long __attribute__((weak)) acpi_fill_madt(unsigned long current)
82 {
83         /* mainboard has no ioapic */
84         return current;
85 }
86
87 unsigned long __attribute__((weak)) acpi_fill_mcfg(unsigned long current)
88 {
89         /* chipset doesn't have mmconfig */
90         return current;
91 }
92
93 unsigned long __attribute__((weak)) acpi_fill_ssdt_generator(unsigned long current,
94                                                  const char *oem_table_id)
95 {
96         acpigen_write_mainboard_resources("\\_SB.PCI0.MBRS", "_CRS");
97         /* generate_cpu_entries() generates weird bytecode and has to come
98          * last or else the following entries will end up inside the
99          * processor scope */
100         generate_cpu_entries();
101         return (unsigned long) acpigen_get_current();
102 }
103
104 unsigned long __attribute__((weak)) write_acpi_tables(unsigned long start)
105 {
106         unsigned long current;
107         acpi_rsdp_t *rsdp;
108         acpi_rsdt_t *rsdt;
109         acpi_fadt_t *fadt;
110         acpi_facs_t *facs;
111         acpi_madt_t *madt;
112         acpi_header_t *ssdt;
113         acpi_header_t *dsdt;
114
115         /* Align ACPI tables to 16 byte. */
116         start = (start + 0x0f) & -0x10;
117         current = start;
118
119         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
120
121         /* We need at least an RSDP and an RSDT table. */
122         rsdp = (acpi_rsdp_t *) current;
123         current += sizeof(acpi_rsdp_t);
124         rsdt = (acpi_rsdt_t *) current;
125         current += sizeof(acpi_rsdt_t);
126
127         /* Clear all table memory. */
128         memset((void *) start, 0, current - start);
129
130         acpi_write_rsdp(rsdp, rsdt, NULL);
131         acpi_write_rsdt(rsdt);
132
133         /* We explicitly add these tables later on: */
134         printk(BIOS_DEBUG, "ACPI:     * FACS\n");
135
136         /* we should align FACS to 64B as per ACPI specs */
137         current = ALIGN(current, 64);
138         facs = (acpi_facs_t *) current;
139         current += sizeof(acpi_facs_t);
140         acpi_create_facs(facs);
141
142         dsdt = (acpi_header_t *)current;
143         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
144         current += dsdt->length;
145         memcpy(dsdt, &AmlCode, dsdt->length);
146         /* Don't trust iasl to get checksum right. */
147         dsdt->checksum = 0; /* needs to be set to 0 first (part of csum) */
148         dsdt->checksum = acpi_checksum((u8*)dsdt, dsdt->length);
149         printk(BIOS_DEBUG, "ACPI:     * DSDT @ %p Length %x\n", dsdt,
150                      dsdt->length);
151         printk(BIOS_DEBUG, "ACPI:     * FADT\n");
152
153         fadt = (acpi_fadt_t *) current;
154         current += sizeof(acpi_fadt_t);
155
156         acpi_create_fadt(fadt, facs, dsdt);
157         acpi_add_table(rsdp, fadt);
158
159         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
160         madt = (acpi_madt_t *) current;
161         acpi_create_madt(madt);
162         if (madt->header.length > sizeof(acpi_madt_t)) {
163                 current += madt->header.length;
164                 acpi_add_table(rsdp, madt);
165         } else {
166                 /* don't add empty madt */
167                 current = (unsigned long)madt;
168         }
169
170         printk(BIOS_DEBUG, "ACPI:    * SSDT\n");
171         ssdt = (acpi_header_t *)current;
172         acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
173         current += ssdt->length;
174         acpi_add_table(rsdp, ssdt);
175
176         printk(BIOS_INFO, "ACPI: done.\n");
177         return current;
178 }