We define IO_APIC_ADDR in <arch/ioapic.h>, let's use it.
[coreboot.git] / src / mainboard / asrock / 939a785gmh / 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 <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/amdk8_sysconf.h>
29 #include "northbridge/amd/amdk8/amdk8_acpi.h"
30 #include <arch/cpu.h>
31 #include <cpu/amd/model_fxx_powernow.h>
32
33 extern u16 pm_base;
34
35 #define DUMP_ACPI_TABLES 0
36
37 /*
38 * Assume the max pstate number is 8
39 * 0x21(33 bytes) is one package length of _PSS package
40 */
41
42 #define Maxpstate 8
43 #define Defpkglength 0x21
44
45 #if DUMP_ACPI_TABLES == 1
46 static void dump_mem(u32 start, u32 end)
47 {
48
49         u32 i;
50         print_debug("dump_mem:");
51         for (i = start; i < end; i++) {
52                 if ((i & 0xf) == 0) {
53                         printk(BIOS_DEBUG, "\n%08x:", i);
54                 }
55                 printk(BIOS_DEBUG, " %02x", (u8)*((u8 *)i));
56         }
57         print_debug("\n");
58 }
59 #endif
60
61 extern const unsigned char AmlCode[];
62
63 #if CONFIG_ACPI_SSDTX_NUM >= 1
64 extern const unsigned char AmlCode_ssdt2[];
65 extern const unsigned char AmlCode_ssdt3[];
66 extern const unsigned char AmlCode_ssdt4[];
67 extern const unsigned char AmlCode_ssdt5[];
68 #endif
69
70 unsigned long acpi_fill_mcfg(unsigned long current)
71 {
72         /* Just a dummy */
73         return current;
74 }
75
76 unsigned long acpi_fill_madt(unsigned long current)
77 {
78         /* create all subtables for processors */
79         current = acpi_create_madt_lapics(current);
80
81         /* Write SB700 IOAPIC, only one */
82         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current, 2,
83                                            IO_APIC_ADDR, 0);
84
85         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
86                                                 current, 0, 0, 2, 0);
87         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
88                                                 current, 0, 9, 9, 0xF);
89         /* 0: mean bus 0--->ISA */
90         /* 0: PIC 0 */
91         /* 2: APIC 2 */
92         /* 5 mean: 0101 --> Edige-triggered, Active high */
93
94         /* create all subtables for processors */
95         /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
96         /* 1: LINT1 connect to NMI */
97
98         return current;
99 }
100
101 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id) {
102         k8acpi_write_vars();
103         amd_model_fxx_generate_powernow(pm_base + 8, 6, 1);
104         return (unsigned long) (acpigen_get_current());
105 }
106
107 unsigned long write_acpi_tables(unsigned long start)
108 {
109         unsigned long current;
110         acpi_rsdp_t *rsdp;
111         acpi_rsdt_t *rsdt;
112         acpi_hpet_t *hpet;
113         acpi_madt_t *madt;
114         acpi_fadt_t *fadt;
115         acpi_facs_t *facs;
116         acpi_header_t *dsdt;
117         acpi_header_t *ssdt;
118 #if CONFIG_ACPI_SSDTX_NUM >= 1
119         acpi_header_t *ssdtx;
120         void *p;
121         int i;
122 #endif
123
124         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
125
126         /* Align ACPI tables to 16 bytes */
127         start = (start + 0x0f) & -0x10;
128         current = start;
129
130         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
131
132         /* We need at least an RSDP and an RSDT Table */
133         rsdp = (acpi_rsdp_t *) current;
134         current += sizeof(acpi_rsdp_t);
135         rsdt = (acpi_rsdt_t *) current;
136         current += sizeof(acpi_rsdt_t);
137
138         /* clear all table memory */
139         memset((void *)start, 0, current - start);
140
141         acpi_write_rsdp(rsdp, rsdt, NULL);
142         acpi_write_rsdt(rsdt);
143
144         /*
145          * We explicitly add these tables later on:
146          */
147         /* If we want to use HPET Timers Linux wants an MADT */
148         printk(BIOS_DEBUG, "ACPI:    * HPET\n");
149         hpet = (acpi_hpet_t *) current;
150         current += sizeof(acpi_hpet_t);
151         acpi_create_hpet(hpet);
152         acpi_add_table(rsdp, hpet);
153
154         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
155         madt = (acpi_madt_t *) current;
156         acpi_create_madt(madt);
157         current += madt->header.length;
158         acpi_add_table(rsdp, madt);
159
160         /* SSDT */
161         printk(BIOS_DEBUG, "ACPI:    * SSDT\n");
162         ssdt = (acpi_header_t *)current;
163
164         acpi_create_ssdt_generator(ssdt, "DYNADATA");
165         current += ssdt->length;
166         acpi_add_table(rsdp, ssdt);
167
168 #if CONFIG_ACPI_SSDTX_NUM >= 1
169
170         /* same htio, but different position? We may have to copy, change HCIN, and recalculate the checknum and add_table */
171
172         for (i = 1; i < sysconf.hc_possible_num; i++) { /* 0: is hc sblink */
173                 if ((sysconf.pci1234[i] & 1) != 1)
174                         continue;
175                 u8 c;
176                 if (i < 7) {
177                         c = (u8) ('4' + i - 1);
178                 } else {
179                         c = (u8) ('A' + i - 1 - 6);
180                 }
181                 printk(BIOS_DEBUG, "ACPI:    * SSDT for PCI%c Aka hcid = %d\n", c, sysconf.hcid[i]);    /* pci0 and pci1 are in dsdt */
182                 current = (current + 0x07) & -0x08;
183                 ssdtx = (acpi_header_t *) current;
184                 switch (sysconf.hcid[i]) {
185                 case 1: /* 8132 */
186                         p = &AmlCode_ssdt2;
187                         break;
188                 case 2: /* 8151 */
189                         p = &AmlCode_ssdt3;
190                         break;
191                 case 3: /* 8131 */
192                         p = &AmlCode_ssdt4;
193                         break;
194                 default:
195                         /* HTX no io apic */
196                         p = &AmlCode_ssdt5;
197                         break;
198                 }
199                 memcpy(ssdtx, p, sizeof(acpi_header_t));
200                 current += ssdtx->length;
201                 memcpy(ssdtx, p, ssdtx->length);
202                 update_ssdtx((void *)ssdtx, i);
203                 ssdtx->checksum = 0;
204                 ssdtx->checksum = acpi_checksum((u8 *)ssdtx, ssdtx->length);
205                 acpi_add_table(rsdp, ssdtx);
206         }
207 #endif
208
209         /* FACS */
210         printk(BIOS_DEBUG, "ACPI:    * FACS\n");
211         facs = (acpi_facs_t *) current;
212         current += sizeof(acpi_facs_t);
213         acpi_create_facs(facs);
214
215         /* DSDT */
216         printk(BIOS_DEBUG, "ACPI:    * DSDT\n");
217         dsdt = (acpi_header_t *)current;
218         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
219         current += dsdt->length;
220         memcpy(dsdt, &AmlCode, dsdt->length);
221         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n", dsdt, dsdt->length);
222         /* FADT */
223         printk(BIOS_DEBUG, "ACPI:    * FADT\n");
224         fadt = (acpi_fadt_t *) current;
225         current += sizeof(acpi_fadt_t);
226
227         acpi_create_fadt(fadt, facs, dsdt);
228         acpi_add_table(rsdp, fadt);
229
230 #if DUMP_ACPI_TABLES == 1
231         printk(BIOS_DEBUG, "rsdp\n");
232         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
233
234         printk(BIOS_DEBUG, "rsdt\n");
235         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
236
237         printk(BIOS_DEBUG, "madt\n");
238         dump_mem(madt, ((void *)madt) + madt->header.length);
239
240         printk(BIOS_DEBUG, "ssdt\n");
241         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
242
243         printk(BIOS_DEBUG, "fadt\n");
244         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
245 #endif
246
247         printk(BIOS_INFO, "ACPI: done.\n");
248         return current;
249 }