2e09a2395df1ea883f2c763ec9b92a9daebd78b7
[coreboot.git] / src / mainboard / asus / m4a785-m / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 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 <device/pci.h>
24 #include <device/pci_ids.h>
25 #include <cpu/x86/msr.h>
26 #include <cpu/amd/mtrr.h>
27 #include <cpu/amd/amdfam10_sysconf.h>
28
29 #include "mb_sysconf.h"
30
31 #define DUMP_ACPI_TABLES 0
32
33 #if DUMP_ACPI_TABLES == 1
34 static void dump_mem(u32 start, u32 end)
35 {
36
37         u32 i;
38         print_debug("dump_mem:");
39         for (i = start; i < end; i++) {
40                 if ((i & 0xf) == 0) {
41                         printk(BIOS_DEBUG, "\n%08x:", i);
42                 }
43                 printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
44         }
45         print_debug("\n");
46 }
47 #endif
48
49 extern const unsigned char AmlCode[];
50 extern const unsigned char AmlCode_ssdt[];
51
52 #if CONFIG_ACPI_SSDTX_NUM >= 1
53 extern const unsigned char AmlCode_ssdt2[];
54 extern const unsigned char AmlCode_ssdt3[];
55 extern const unsigned char AmlCode_ssdt4[];
56 extern const unsigned char AmlCode_ssdt5[];
57 #endif
58
59 #define IO_APIC_ADDR    0xfec00000UL
60
61 unsigned long acpi_fill_mcfg(unsigned long current)
62 {
63         /* Just a dummy */
64         return current;
65 }
66
67 unsigned long acpi_fill_madt(unsigned long current)
68 {
69         /* create all subtables for processors */
70         current = acpi_create_madt_lapics(current);
71
72         /* Write SB700 IOAPIC, only one */
73         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current, 2,
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         /* 0: mean bus 0--->ISA */
81         /* 0: PIC 0 */
82         /* 2: APIC 2 */
83         /* 5 mean: 0101 --> Edige-triggered, Active high */
84
85         /* create all subtables for processors */
86         /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
87         /* 1: LINT1 connect to NMI */
88
89         return current;
90 }
91
92 unsigned long write_acpi_tables(unsigned long start)
93 {
94         unsigned long current;
95         acpi_rsdp_t *rsdp;
96         acpi_rsdt_t *rsdt;
97         acpi_hpet_t *hpet;
98         acpi_madt_t *madt;
99         acpi_srat_t *srat;
100         acpi_slit_t *slit;
101         acpi_fadt_t *fadt;
102         acpi_facs_t *facs;
103         acpi_header_t *dsdt;
104         acpi_header_t *ssdt;
105 #if CONFIG_ACPI_SSDTX_NUM >= 1
106         acpi_header_t *ssdtx;
107         void *p;
108         int i;
109 #endif
110
111         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
112
113         /* Align ACPI tables to 16 bytes */
114         start = (start + 0x0f) & -0x10;
115         current = start;
116
117         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
118
119         /* We need at least an RSDP and an RSDT Table */
120         rsdp = (acpi_rsdp_t *) current;
121         current += sizeof(acpi_rsdp_t);
122         rsdt = (acpi_rsdt_t *) current;
123         current += sizeof(acpi_rsdt_t);
124
125         /* clear all table memory */
126         memset((void *)start, 0, current - start);
127
128         acpi_write_rsdp(rsdp, rsdt, NULL);
129         acpi_write_rsdt(rsdt);
130
131         /*
132          * We explicitly add these tables later on:
133          */
134         current   = ( current + 0x07) & -0x08;
135         printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
136         hpet = (acpi_hpet_t *) current;
137         current += sizeof(acpi_hpet_t);
138         acpi_create_hpet(hpet);
139         acpi_add_table(rsdp, hpet);
140
141         /* If we want to use HPET Timers Linux wants an MADT */
142         current   = ( current + 0x07) & -0x08;
143         printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n",current);
144         madt = (acpi_madt_t *) current;
145         acpi_create_madt(madt);
146         current += madt->header.length;
147         acpi_add_table(rsdp, madt);
148
149         /* SRAT */
150         current   = ( current + 0x07) & -0x08;
151         printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
152         srat = (acpi_srat_t *) current;
153         acpi_create_srat(srat);
154         current += srat->header.length;
155         acpi_add_table(rsdp, srat);
156
157         /* SLIT */
158         current   = ( current + 0x07) & -0x08;
159         printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
160         slit = (acpi_slit_t *) current;
161         acpi_create_slit(slit);
162         current += slit->header.length;
163         acpi_add_table(rsdp, slit);
164
165         /* SSDT */
166         current   = ( current + 0x0f) & -0x10;
167         printk(BIOS_DEBUG, "ACPI:    * SSDT at %lx\n", current);
168         ssdt = (acpi_header_t *)current;
169         memcpy(ssdt, &AmlCode_ssdt, sizeof(acpi_header_t));
170         current += ssdt->length;
171         memcpy(ssdt, &AmlCode_ssdt, ssdt->length);
172         //Here you need to set value in pci1234, sblk and sbdn in get_bus_conf.c
173         update_ssdt((void*)ssdt);
174         /* recalculate checksum */
175         ssdt->checksum = 0;
176         ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
177         acpi_add_table(rsdp,ssdt);
178
179         printk(BIOS_DEBUG, "ACPI:    * SSDT for PState at %lx\n", current);
180         current = acpi_add_ssdt_pstates(rsdp, current);
181
182 #if CONFIG_ACPI_SSDTX_NUM >= 1
183
184         /* same htio, but different position? We may have to copy,
185         change HCIN, and recalculate the checknum and add_table */
186
187         for(i=1;i<sysconf.hc_possible_num;i++) {  // 0: is hc sblink
188                 if((sysconf.pci1234[i] & 1) != 1 ) continue;
189                 u8 c;
190                 if (i < 7) {
191                         c = (u8) ('4' + i - 1);
192                 } else {
193                         c = (u8) ('A' + i - 1 - 6);
194                 }
195                 current   = ( current + 0x07) & -0x08;
196                 printk(BIOS_DEBUG, "ACPI:    * SSDT for PCI%c at %lx\n", c, current); //pci0 and pci1 are in dsdt
197                 ssdtx = (acpi_header_t *)current;
198                 switch (sysconf.hcid[i]) {
199                 case 1:
200                         p = &AmlCode_ssdt2;
201                         break;
202                 case 2:
203                         p = &AmlCode_ssdt3;
204                         break;
205                 case 3: /* 8131 */
206                         p = &AmlCode_ssdt4;
207                         break;
208                 default:
209                         /* HTX no io apic */
210                         p = &AmlCode_ssdt5;
211                         break;
212                 }
213                 memcpy(ssdtx, p, sizeof(acpi_header_t));
214                 current += ssdtx->length;
215                 memcpy(ssdtx, p, ssdtx->length);
216                 update_ssdtx((void *)ssdtx, i);
217                 ssdtx->checksum = 0;
218                 ssdtx->checksum = acpi_checksum((u8 *)ssdtx, ssdtx->length);
219                 acpi_add_table(rsdp, ssdtx);
220         }
221 #endif
222
223         /* DSDT */
224         current   = ( current + 0x07) & -0x08;
225         printk(BIOS_DEBUG, "ACPI:    * DSDT at %lx\n", current);
226         dsdt = (acpi_header_t *)current; // it will used by fadt
227         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
228         current += dsdt->length;
229         memcpy(dsdt, &AmlCode, dsdt->length);
230         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n",dsdt,dsdt->length);
231
232         /* FACS */ // it needs 64 bit alignment
233         current   = ( current + 0x07) & -0x08;
234         printk(BIOS_DEBUG, "ACPI:       * FACS at %lx\n", current);
235         facs = (acpi_facs_t *) current; // it will be used by fadt
236         current += sizeof(acpi_facs_t);
237         acpi_create_facs(facs);
238
239         /* FDAT */
240         current   = ( current + 0x07) & -0x08;
241         printk(BIOS_DEBUG, "ACPI:    * FADT at %lx\n", current);
242         fadt = (acpi_fadt_t *) current;
243         current += sizeof(acpi_fadt_t);
244
245         acpi_create_fadt(fadt, facs, dsdt);
246         acpi_add_table(rsdp, fadt);
247
248 #if DUMP_ACPI_TABLES == 1
249         printk(BIOS_DEBUG, "rsdp\n");
250         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
251
252         printk(BIOS_DEBUG, "rsdt\n");
253         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
254
255         printk(BIOS_DEBUG, "madt\n");
256         dump_mem(madt, ((void *)madt) + madt->header.length);
257
258         printk(BIOS_DEBUG, "srat\n");
259         dump_mem(srat, ((void *)srat) + srat->header.length);
260
261         printk(BIOS_DEBUG, "slit\n");
262         dump_mem(slit, ((void *)slit) + slit->header.length);
263
264         printk(BIOS_DEBUG, "ssdt\n");
265         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
266
267         printk(BIOS_DEBUG, "fadt\n");
268         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
269 #endif
270
271         printk(BIOS_INFO, "ACPI: done.\n");
272         return current;
273 }