7c5aa390df958ec5e81623ef9af05c14e459e7ae
[coreboot.git] / src / mainboard / amd / persimmon / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Advanced Micro Devices, Inc.
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 <console/console.h>
21 #include <string.h>
22 #include <arch/acpi.h>
23 #include <arch/ioapic.h>
24 #include <device/pci.h>
25 #include <device/pci_ids.h>
26 #include <cpu/x86/msr.h>
27 #include "agesawrapper.h"
28 #include <cpu/amd/mtrr.h>
29 #include <cpu/amd/amdfam14.h>
30
31 #define DUMP_ACPI_TABLES 0
32
33 #if DUMP_ACPI_TABLES == 1
34
35 static void dump_mem(u32 start, u32 end)
36 {
37
38   u32 i;
39   print_debug("dump_mem:");
40   for (i = start; i < end; i++) {
41     if ((i & 0xf) == 0) {
42       printk(BIOS_DEBUG, "\n%08x:", i);
43     }
44     printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
45   }
46   print_debug("\n");
47 }
48 #endif
49
50 extern const unsigned char AmlCode[];
51 extern const unsigned char AmlCode_ssdt[];
52
53 unsigned long acpi_fill_mcfg(unsigned long current)
54 {
55   /* Just a dummy */
56   return current;
57 }
58
59 unsigned long acpi_fill_madt(unsigned long current)
60 {
61   /* create all subtables for processors */
62   current = acpi_create_madt_lapics(current);
63
64   /* Write SB800 IOAPIC, only one */
65   current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current, CONFIG_MAX_CPUS,
66              IO_APIC_ADDR, 0);
67
68   current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
69             current, 0, 0, 2, 0);
70   current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
71             current, 0, 9, 9, 0xF);
72   /* 0: mean bus 0--->ISA */
73   /* 0: PIC 0 */
74   /* 2: APIC 2 */
75   /* 5 mean: 0101 --> Edige-triggered, Active high */
76
77   /* create all subtables for processors */
78   /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
79   /* 1: LINT1 connect to NMI */
80
81   return current;
82 }
83
84 unsigned long acpi_fill_slit(unsigned long current)
85 {
86   // Not implemented
87   return current;
88 }
89
90 unsigned long acpi_fill_srat(unsigned long current)
91 {
92   /* No NUMA, no SRAT */
93   return current;
94 }
95
96 unsigned long write_acpi_tables(unsigned long start)
97 {
98   unsigned long current;
99   acpi_rsdp_t *rsdp;
100   acpi_rsdt_t *rsdt;
101   acpi_hpet_t *hpet;
102   acpi_madt_t *madt;
103   acpi_srat_t *srat;
104   acpi_slit_t *slit;
105   acpi_fadt_t *fadt;
106   acpi_facs_t *facs;
107   acpi_header_t *dsdt;
108   acpi_header_t *ssdt;
109
110   get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
111
112   /* Align ACPI tables to 16 bytes */
113   start = (start + 0x0f) & -0x10;
114   current = start;
115
116   printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
117
118   /* We need at least an RSDP and an RSDT Table */
119   rsdp = (acpi_rsdp_t *) current;
120   current += sizeof(acpi_rsdp_t);
121   rsdt = (acpi_rsdt_t *) current;
122   current += sizeof(acpi_rsdt_t);
123
124   /* clear all table memory */
125   memset((void *)start, 0, current - start);
126
127   acpi_write_rsdp(rsdp, rsdt, NULL);
128   acpi_write_rsdt(rsdt);
129
130   /* DSDT */
131   current   = ( current + 0x07) & -0x08;
132   printk(BIOS_DEBUG, "ACPI:    * DSDT at %lx\n", current);
133   dsdt = (acpi_header_t *)current; // it will used by fadt
134   memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
135   current += dsdt->length;
136   memcpy(dsdt, &AmlCode, dsdt->length);
137   printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n",dsdt,dsdt->length);
138
139   /* FACS */ // it needs 64 bit alignment
140   current   = ( current + 0x07) & -0x08;
141   printk(BIOS_DEBUG, "ACPI: * FACS at %lx\n", current);
142   facs = (acpi_facs_t *) current; // it will be used by fadt
143   current += sizeof(acpi_facs_t);
144   acpi_create_facs(facs);
145
146   /* FDAT */
147   current   = ( current + 0x07) & -0x08;
148   printk(BIOS_DEBUG, "ACPI:    * FADT at %lx\n", current);
149   fadt = (acpi_fadt_t *) current;
150   current += sizeof(acpi_fadt_t);
151
152   acpi_create_fadt(fadt, facs, dsdt);
153   acpi_add_table(rsdp, fadt);
154
155   /*
156    * We explicitly add these tables later on:
157    */
158   current   = ( current + 0x07) & -0x08;
159   printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
160   hpet = (acpi_hpet_t *) current;
161   current += sizeof(acpi_hpet_t);
162   acpi_create_hpet(hpet);
163   acpi_add_table(rsdp, hpet);
164
165   /* If we want to use HPET Timers Linux wants an MADT */
166   current   = ( current + 0x07) & -0x08;
167   printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n",current);
168   madt = (acpi_madt_t *) current;
169   acpi_create_madt(madt);
170   current += madt->header.length;
171   acpi_add_table(rsdp, madt);
172
173   /* SRAT */
174   current   = ( current + 0x07) & -0x08;
175   printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
176   srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
177   if (srat != NULL) {
178     memcpy((void *)current, srat, srat->header.length);
179     srat = (acpi_srat_t *) current;
180     //acpi_create_srat(srat);
181     current += srat->header.length;
182     acpi_add_table(rsdp, srat);
183   }
184
185   /* SLIT */
186   current   = ( current + 0x07) & -0x08;
187   printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
188   slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
189   if (slit != NULL) {
190     memcpy((void *)current, slit, slit->header.length);
191     slit = (acpi_slit_t *) current;
192     //acpi_create_slit(slit);
193     current += slit->header.length;
194     acpi_add_table(rsdp, slit);
195   }
196
197   /* SSDT */
198   current   = ( current + 0x0f) & -0x10;
199   printk(BIOS_DEBUG, "ACPI:    * SSDT at %lx\n", current);
200   ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
201   if (ssdt != NULL) {
202     memcpy((void *)current, ssdt, ssdt->length);
203     ssdt = (acpi_header_t *) current;
204     current += ssdt->length;
205   }
206   else {
207     ssdt = (acpi_header_t *) current;
208     memcpy(ssdt, &AmlCode_ssdt, sizeof(acpi_header_t));
209     current += ssdt->length;
210     memcpy(ssdt, &AmlCode_ssdt, ssdt->length);
211    /* recalculate checksum */
212     ssdt->checksum = 0;
213     ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
214   }
215   acpi_add_table(rsdp,ssdt);
216
217   printk(BIOS_DEBUG, "ACPI:    * SSDT for PState at %lx\n", current);
218
219 #if DUMP_ACPI_TABLES == 1
220   printk(BIOS_DEBUG, "rsdp\n");
221   dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
222
223   printk(BIOS_DEBUG, "rsdt\n");
224   dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
225
226   printk(BIOS_DEBUG, "madt\n");
227   dump_mem(madt, ((void *)madt) + madt->header.length);
228
229   printk(BIOS_DEBUG, "srat\n");
230   dump_mem(srat, ((void *)srat) + srat->header.length);
231
232   printk(BIOS_DEBUG, "slit\n");
233   dump_mem(slit, ((void *)slit) + slit->header.length);
234
235   printk(BIOS_DEBUG, "ssdt\n");
236   dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
237
238   printk(BIOS_DEBUG, "fadt\n");
239   dump_mem(fadt, ((void *)fadt) + fadt->header.length);
240 #endif
241
242   printk(BIOS_INFO, "ACPI: done.\n");
243   return current;
244 }