Signed-off-by: Rudolf Marek <r.marek@assembler.cz>
[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 acpi_header_t AmlCode;
61
62 #if CONFIG_ACPI_SSDTX_NUM >= 1
63 extern const acpi_header_t AmlCode_ssdt2;
64 extern const acpi_header_t AmlCode_ssdt3;
65 extern const acpi_header_t AmlCode_ssdt4;
66 extern const acpi_header_t 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 extern void get_bus_conf(void);
103
104 #if CONFIG_ACPI_SSDTX_NUM >= 1
105 static void update_ssdtx(void *ssdtx, int i)
106 {
107         u8 *PCI;
108         u8 *HCIN;
109         u8 *UID;
110
111         PCI = ssdtx + 0x32;
112         HCIN = ssdtx + 0x39;
113         UID = ssdtx + 0x40;
114
115         if (i < 7) {
116                 *PCI = (u8) ('4' + i - 1);
117         } else {
118                 *PCI = (u8) ('A' + i - 1 - 6);
119         }
120         *HCIN = (u8) i;
121         *UID = (u8) (i + 3);
122
123         /* FIXME: need to update the GSI id in the ssdtx too */
124
125 }
126 #endif
127
128 unsigned long acpi_fill_ssdt_generator(unsigned long current, const char *oem_table_id) {
129         k8acpi_write_vars();
130         amd_model_fxx_generate_powernow(pm_base + 8, 6, 1);
131         return (unsigned long) (acpigen_get_current());
132 }
133
134 unsigned long write_acpi_tables(unsigned long start)
135 {
136         unsigned long current;
137         acpi_rsdp_t *rsdp;
138         acpi_rsdt_t *rsdt;
139         acpi_hpet_t *hpet;
140         acpi_madt_t *madt;
141         acpi_fadt_t *fadt;
142         acpi_facs_t *facs;
143         acpi_header_t *dsdt;
144         acpi_header_t *ssdt;
145 #if CONFIG_ACPI_SSDTX_NUM >= 1
146         acpi_header_t *ssdtx;
147         acpi_header_t const *p;
148         int i;
149 #endif
150
151         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
152
153         /* Align ACPI tables to 16 bytes */
154         start = (start + 0x0f) & -0x10;
155         current = start;
156
157         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
158
159         /* We need at least an RSDP and an RSDT Table */
160         rsdp = (acpi_rsdp_t *) current;
161         current += sizeof(acpi_rsdp_t);
162         rsdt = (acpi_rsdt_t *) current;
163         current += sizeof(acpi_rsdt_t);
164
165         /* clear all table memory */
166         memset((void *)start, 0, current - start);
167
168         acpi_write_rsdp(rsdp, rsdt, NULL);
169         acpi_write_rsdt(rsdt);
170
171         /*
172          * We explicitly add these tables later on:
173          */
174         /* If we want to use HPET Timers Linux wants an MADT */
175         printk(BIOS_DEBUG, "ACPI:    * HPET\n");
176         hpet = (acpi_hpet_t *) current;
177         current += sizeof(acpi_hpet_t);
178         acpi_create_hpet(hpet);
179         acpi_add_table(rsdp, hpet);
180
181         printk(BIOS_DEBUG, "ACPI:    * MADT\n");
182         madt = (acpi_madt_t *) current;
183         acpi_create_madt(madt);
184         current += madt->header.length;
185         acpi_add_table(rsdp, madt);
186
187         /* SSDT */
188         printk(BIOS_DEBUG, "ACPI:    * SSDT\n");
189         ssdt = (acpi_header_t *)current;
190
191         acpi_create_ssdt_generator(ssdt, "DYNADATA");
192         current += ssdt->length;
193         acpi_add_table(rsdp, ssdt);
194
195 #if CONFIG_ACPI_SSDTX_NUM >= 1
196
197         /* same htio, but different position? We may have to copy, change HCIN, and recalculate the checknum and add_table */
198
199         for (i = 1; i < sysconf.hc_possible_num; i++) { /* 0: is hc sblink */
200                 if ((sysconf.pci1234[i] & 1) != 1)
201                         continue;
202                 u8 c;
203                 if (i < 7) {
204                         c = (u8) ('4' + i - 1);
205                 } else {
206                         c = (u8) ('A' + i - 1 - 6);
207                 }
208                 printk(BIOS_DEBUG, "ACPI:    * SSDT for PCI%c Aka hcid = %d\n", c, sysconf.hcid[i]);    /* pci0 and pci1 are in dsdt */
209                 current = (current + 0x07) & -0x08;
210                 ssdtx = (acpi_header_t *) current;
211                 switch (sysconf.hcid[i]) {
212                 case 1: /* 8132 */
213                         p = &AmlCode_ssdt2;
214                         break;
215                 case 2: /* 8151 */
216                         p = &AmlCode_ssdt3;
217                         break;
218                 case 3: /* 8131 */
219                         p = &AmlCode_ssdt4;
220                         break;
221                 default:
222                         /* HTX no io apic */
223                         p = &AmlCode_ssdt5;
224                         break;
225                 }
226                 current += p->length;
227                 memcpy((void *)ssdtx, p, p->length);
228                 update_ssdtx((void *)ssdtx, i);
229                 ssdtx->checksum = 0;
230                 ssdtx->checksum = acpi_checksum((u8 *)ssdtx, ssdtx->length);
231                 acpi_add_table(rsdp, ssdtx);
232         }
233 #endif
234
235         /* FACS */
236         printk(BIOS_DEBUG, "ACPI:    * FACS\n");
237         facs = (acpi_facs_t *) current;
238         current += sizeof(acpi_facs_t);
239         acpi_create_facs(facs);
240
241         /* DSDT */
242         printk(BIOS_DEBUG, "ACPI:    * DSDT\n");
243         dsdt = (acpi_header_t *) current;
244         memcpy((void *)dsdt, &AmlCode, AmlCode.length);
245         current += dsdt->length;
246         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n", dsdt, dsdt->length);
247         /* FADT */
248         printk(BIOS_DEBUG, "ACPI:    * FADT\n");
249         fadt = (acpi_fadt_t *) current;
250         current += sizeof(acpi_fadt_t);
251
252         acpi_create_fadt(fadt, facs, dsdt);
253         acpi_add_table(rsdp, fadt);
254
255 #if DUMP_ACPI_TABLES == 1
256         printk(BIOS_DEBUG, "rsdp\n");
257         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
258
259         printk(BIOS_DEBUG, "rsdt\n");
260         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
261
262         printk(BIOS_DEBUG, "madt\n");
263         dump_mem(madt, ((void *)madt) + madt->header.length);
264
265         printk(BIOS_DEBUG, "ssdt\n");
266         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
267
268         printk(BIOS_DEBUG, "fadt\n");
269         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
270 #endif
271
272         printk(BIOS_INFO, "ACPI: done.\n");
273         return current;
274 }