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