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