Fix messy code in ALIB creation
[coreboot.git] / src / mainboard / amd / dinar / acpi_tables.c
blobdb01d79f52b478272069f5b1a0c3245a45b718ec
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 Advanced Micro Devices, Inc.
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.
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.
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
20 #include <console/console.h>
21 #include <string.h>
22 #include <arch/acpi.h>
23 #include <arch/acpigen.h>
24 #include <arch/ioapic.h>
25 #include <arch/io.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <cpu/x86/msr.h>
29 #include <cpu/amd/mtrr.h>
31 #include "agesawrapper.h"
33 #define DUMP_ACPI_TABLES 0
35 #if DUMP_ACPI_TABLES == 1
36 static void dump_mem(u32 start, u32 end)
39 u32 i;
40 print_debug("dump_mem:");
41 for (i = start; i < end; i++) {
42 if ((i & 0xf) == 0) {
43 printk(BIOS_DEBUG, "\n%08x:", i);
45 printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
47 print_debug("\n");
49 #endif
51 extern const unsigned char AmlCode[];
54 unsigned long acpi_fill_mcfg(unsigned long current)
56 /* Just a dummy */
57 return current;
60 unsigned long acpi_fill_madt(unsigned long current)
62 device_t dev;
63 u32 dword;
64 u32 gsi_base = 0;
65 u32 apicid_sb700;
66 u32 apicid_rd890;
69 * AGESA v5 Apply apic enumeration rules
70 * For systems with >= 16 APICs, put the IO-APICs at 0..n and
71 * put the local-APICs at m..z
72 * For systems with < 16 APICs, put the Local-APICs at 0..n and
73 * put the IO-APICs at (n + 1)..z
75 #if CONFIG_MAX_CPUS >= 16
76 apicid_sb700 = 0x0;
77 #else
78 apicid_sb700 = CONFIG_MAX_CPUS + 1
79 #endif
80 apicid_rd890 = apicid_sb700 + 1;
82 /* create all subtables for processors */
83 current = acpi_create_madt_lapics(current);
85 /* Write sb700 IOAPIC, only one */
86 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
87 apicid_sb700,
88 IO_APIC_ADDR,
92 /* IOAPIC on rs5690 */
93 gsi_base += IO_APIC_INTERRUPTS; /* sb700 has 24 IOAPIC entries. */
94 dev = dev_find_slot(0, PCI_DEVFN(0, 0));
95 if (dev) {
96 pci_write_config32(dev, 0xF8, 0x1);
97 dword = pci_read_config32(dev, 0xFC) & 0xfffffff0;
98 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current,
99 apicid_rd890,
100 dword,
101 gsi_base
105 current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *) current,
106 0, //BUS
107 0, //SOURCE
108 2, //gsirq
109 0 //flags
112 /* 0: mean bus 0--->ISA */
113 /* 0: PIC 0 */
114 /* 2: APIC 2 */
115 /* 5 mean: 0101 --> Edige-triggered, Active high */
117 /* create all subtables for processors */
118 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, 0, 5, 1);
119 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, 1, 5, 1);
120 /* 1: LINT1 connect to NMI */
122 return current;
125 unsigned long acpi_fill_hest(acpi_hest_t *hest)
127 void *addr, *current;
129 /* Skip the HEST header. */
130 current = (void *)(hest + 1);
132 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
133 if (addr != NULL)
134 current += acpi_create_hest_error_source(hest, current, 0, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
136 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
137 if (addr != NULL)
138 current += acpi_create_hest_error_source(hest, current, 1, (void *)((u32)addr + 2), *(UINT16 *)addr - 2);
140 return (unsigned long)current;
143 unsigned long acpi_fill_slit(unsigned long current)
145 // Not implemented
146 return current;
149 unsigned long acpi_fill_srat(unsigned long current)
151 /* No NUMA, no SRAT */
152 return current;
155 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id)
157 int lens;
158 msr_t msr;
159 char pscope[] = "\\_SB.PCI0";
161 lens = acpigen_write_scope(pscope);
162 msr = rdmsr(TOP_MEM);
163 lens += acpigen_write_name_dword("TOM1", msr.lo);
164 msr = rdmsr(TOP_MEM2);
166 * Since XP only implements parts of ACPI 2.0, we can't use a qword
167 * here.
168 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
169 * slide 22ff.
170 * Shift value right by 20 bit to make it fit into 32bit,
171 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
173 lens += acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
174 acpigen_patch_len(lens - 1);
175 return (unsigned long) (acpigen_get_current());
178 unsigned long write_acpi_tables(unsigned long start)
180 unsigned long current;
181 acpi_rsdp_t *rsdp;
182 acpi_rsdt_t *rsdt;
183 //acpi_hpet_t *hpet;
184 acpi_madt_t *madt;
185 acpi_srat_t *srat;
186 acpi_slit_t *slit;
187 acpi_fadt_t *fadt;
188 acpi_facs_t *facs;
189 acpi_header_t *dsdt;
190 acpi_header_t *ssdt;
191 acpi_header_t *ssdt2;
192 acpi_header_t *alib;
193 acpi_hest_t *hest;
195 get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
197 /* Align ACPI tables to 16 bytes */
198 start = (start + 0x0f) & -0x10;
199 current = start;
201 printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
203 /* We need at least an RSDP and an RSDT Table */
204 rsdp = (acpi_rsdp_t *) current;
205 current += sizeof(acpi_rsdp_t);
206 rsdt = (acpi_rsdt_t *) current;
207 current += sizeof(acpi_rsdt_t);
209 /* clear all table memory */
210 memset((void *)start, 0, current - start);
212 acpi_write_rsdp(rsdp, rsdt, NULL);
213 acpi_write_rsdt(rsdt);
215 /* FACS */
216 printk(BIOS_DEBUG, "ACPI: * FACS\n");
217 facs = (acpi_facs_t *) current;
218 current += sizeof(acpi_facs_t);
219 acpi_create_facs(facs);
221 /* DSDT */
222 printk(BIOS_DEBUG, "ACPI: * DSDT\n");
223 dsdt = (acpi_header_t *)current;
224 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
225 current += dsdt->length;
226 memcpy(dsdt, &AmlCode, dsdt->length);
227 printk(BIOS_DEBUG, "ACPI: * DSDT @ %p Length %x\n", dsdt, dsdt->length);
228 /* FADT */
229 printk(BIOS_DEBUG, "ACPI: * FADT\n");
230 fadt = (acpi_fadt_t *) current;
231 current += sizeof(acpi_fadt_t);
233 acpi_create_fadt(fadt, facs, dsdt);
234 acpi_add_table(rsdp, fadt);
237 * We explicitly add these tables later on:
239 #ifdef UNUSED_CODE // Don't need HPET table. we have one in dsdt
240 current = (current + 0x07) & -0x08;
241 printk(BIOS_DEBUG, "ACPI: * HPET at %lx\n", current);
242 hpet = (acpi_hpet_t *) current;
243 current += sizeof(acpi_hpet_t);
244 acpi_create_hpet(hpet);
245 acpi_add_table(rsdp, hpet);
246 #endif
248 /* If we want to use HPET Timers Linux wants an MADT */
249 current = (current + 0x07) & -0x08;
250 printk(BIOS_DEBUG, "ACPI: * MADT at %lx\n",current);
251 madt = (acpi_madt_t *) current;
252 acpi_create_madt(madt);
253 current += madt->header.length;
254 acpi_add_table(rsdp, madt);
256 /* HEST */
257 current = (current + 0x07) & -0x08;
258 hest = (acpi_hest_t *)current;
259 acpi_write_hest((void *)current);
260 acpi_add_table(rsdp, (void *)current);
261 current += ((acpi_header_t *)current)->length;
263 /* SRAT */
264 current = (current + 0x07) & -0x08;
265 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
266 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
267 if (srat != NULL) {
268 memcpy((void *)current, srat, srat->header.length);
269 srat = (acpi_srat_t *) current;
270 //acpi_create_srat(srat);
271 current += srat->header.length;
272 acpi_add_table(rsdp, srat);
275 /* SLIT */
276 current = (current + 0x07) & -0x08;
277 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
278 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
279 if (slit != NULL) {
280 memcpy((void *)current, slit, slit->header.length);
281 slit = (acpi_slit_t *) current;
282 //acpi_create_slit(slit);
283 current += slit->header.length;
284 acpi_add_table(rsdp, slit);
287 /* SSDT */
288 current = (current + 0x0f) & -0x10;
289 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
290 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
291 if (alib != NULL) {
292 memcpy((void *)current, alib, alib->length);
293 alib = (acpi_header_t *) current;
294 current += alib->length;
295 acpi_add_table(rsdp, (void *)alib);
296 } else {
297 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
300 /* The DSDT needs additional work for the AGESA SSDT Pstate table */
301 /* Keep the comment for a while. */
302 current = (current + 0x0f) & -0x10;
303 printk(BIOS_DEBUG, "ACPI: * AGESA SSDT Pstate at %lx\n", current);
304 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
305 if (ssdt != NULL) {
306 memcpy((void *)current, ssdt, ssdt->length);
307 ssdt = (acpi_header_t *) current;
308 current += ssdt->length;
309 acpi_add_table(rsdp,ssdt);
310 } else {
311 printk(BIOS_DEBUG, " AGESA SSDT Pstate table NULL. Skipping.\n");
314 current = (current + 0x0f) & -0x10;
315 printk(BIOS_DEBUG, "ACPI: * coreboot TOM SSDT2 at %lx\n", current);
316 ssdt2 = (acpi_header_t *) current;
317 acpi_create_ssdt_generator(ssdt2, ACPI_TABLE_CREATOR);
318 current += ssdt2->length;
319 acpi_add_table(rsdp,ssdt2);
321 #if DUMP_ACPI_TABLES == 1
322 printk(BIOS_DEBUG, "rsdp\n");
323 dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
325 printk(BIOS_DEBUG, "rsdt\n");
326 dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
328 printk(BIOS_DEBUG, "madt\n");
329 dump_mem(madt, ((void *)madt) + madt->header.length);
331 printk(BIOS_DEBUG, "srat\n");
332 dump_mem(srat, ((void *)srat) + srat->header.length);
334 printk(BIOS_DEBUG, "slit\n");
335 dump_mem(slit, ((void *)slit) + slit->header.length);
337 printk(BIOS_DEBUG, "ssdt\n");
338 dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
340 printk(BIOS_DEBUG, "fadt\n");
341 dump_mem(fadt, ((void *)fadt) + fadt->header.length);
343 printk(BIOS_DEBUG, "hest\n");
344 dump_mem(hest, ((void *)hest) + hest->header.length);
345 #endif
347 printk(BIOS_INFO, "ACPI: done.\n");
348 return current;