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