We define IO_APIC_ADDR in <arch/ioapic.h>, let's use it.
[coreboot.git] / src / mainboard / jetway / pa78vm5 / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2010 Wang Qing Pei <wangqingpei@gmail.com>
5  * Copyright (C) 2010 Advanced Micro Devices, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <console/console.h>
22 #include <string.h>
23 #include <arch/acpi.h>
24 #include <arch/ioapic.h>
25 #include <device/pci.h>
26 #include <device/pci_ids.h>
27 #include <cpu/x86/msr.h>
28 #include <cpu/amd/mtrr.h>
29 #include <cpu/amd/amdfam10_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 unsigned long acpi_fill_mcfg(unsigned long current)
60 {
61         /* Just a dummy */
62         return current;
63 }
64
65 unsigned long acpi_fill_madt(unsigned long current)
66 {
67         /* create all subtables for processors */
68         current = acpi_create_madt_lapics(current);
69
70         /* Write SB700 IOAPIC, only one */
71         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *) current, 2,
72                                            IO_APIC_ADDR, 0);
73
74         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
75                                                 current, 0, 0, 2, 0);
76         current += acpi_create_madt_irqoverride((acpi_madt_irqoverride_t *)
77                                                 current, 0, 9, 9, 0xF);
78         /* 0: mean bus 0--->ISA */
79         /* 0: PIC 0 */
80         /* 2: APIC 2 */
81         /* 5 mean: 0101 --> Edige-triggered, Active high */
82
83         /* create all subtables for processors */
84         /* current = acpi_create_madt_lapic_nmis(current, 5, 1); */
85         /* 1: LINT1 connect to NMI */
86
87         return current;
88 }
89
90 unsigned long write_acpi_tables(unsigned long start)
91 {
92         unsigned long current;
93         acpi_rsdp_t *rsdp;
94         acpi_rsdt_t *rsdt;
95         acpi_hpet_t *hpet;
96         acpi_madt_t *madt;
97         acpi_srat_t *srat;
98         acpi_slit_t *slit;
99         acpi_fadt_t *fadt;
100         acpi_facs_t *facs;
101         acpi_header_t *dsdt;
102         acpi_header_t *ssdt;
103 #if CONFIG_ACPI_SSDTX_NUM >= 1
104         acpi_header_t *ssdtx;
105         void *p;
106         int i;
107 #endif
108
109         get_bus_conf(); /* it will get sblk, pci1234, hcdn, and sbdn */
110
111         /* Align ACPI tables to 16 bytes */
112         start = (start + 0x0f) & -0x10;
113         current = start;
114
115         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
116
117         /* We need at least an RSDP and an RSDT Table */
118         rsdp = (acpi_rsdp_t *) current;
119         current += sizeof(acpi_rsdp_t);
120         rsdt = (acpi_rsdt_t *) current;
121         current += sizeof(acpi_rsdt_t);
122
123         /* clear all table memory */
124         memset((void *)start, 0, current - start);
125
126         acpi_write_rsdp(rsdp, rsdt, NULL);
127         acpi_write_rsdt(rsdt);
128
129         /*
130          * We explicitly add these tables later on:
131          */
132         current   = ( current + 0x07) & -0x08;
133         printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
134         hpet = (acpi_hpet_t *) current;
135         current += sizeof(acpi_hpet_t);
136         acpi_create_hpet(hpet);
137         acpi_add_table(rsdp, hpet);
138
139         /* If we want to use HPET Timers Linux wants an MADT */
140         current   = ( current + 0x07) & -0x08;
141         printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n",current);
142         madt = (acpi_madt_t *) current;
143         acpi_create_madt(madt);
144         current += madt->header.length;
145         acpi_add_table(rsdp, madt);
146
147         /* SRAT */
148         current   = ( current + 0x07) & -0x08;
149         printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
150         srat = (acpi_srat_t *) current;
151         acpi_create_srat(srat);
152         current += srat->header.length;
153         acpi_add_table(rsdp, srat);
154
155         /* SLIT */
156         current   = ( current + 0x07) & -0x08;
157         printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
158         slit = (acpi_slit_t *) current;
159         acpi_create_slit(slit);
160         current += slit->header.length;
161         acpi_add_table(rsdp, slit);
162
163         /* SSDT */
164         current   = ( current + 0x0f) & -0x10;
165         printk(BIOS_DEBUG, "ACPI:    * SSDT at %lx\n", current);
166         ssdt = (acpi_header_t *)current;
167         memcpy(ssdt, &AmlCode_ssdt, sizeof(acpi_header_t));
168         current += ssdt->length;
169         memcpy(ssdt, &AmlCode_ssdt, ssdt->length);
170         //Here you need to set value in pci1234, sblk and sbdn in get_bus_conf.c
171         update_ssdt((void*)ssdt);
172         /* recalculate checksum */
173         ssdt->checksum = 0;
174         ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
175         acpi_add_table(rsdp,ssdt);
176
177         printk(BIOS_DEBUG, "ACPI:    * SSDT for PState at %lx\n", current);
178         current = acpi_add_ssdt_pstates(rsdp, current);
179
180 #if CONFIG_ACPI_SSDTX_NUM >= 1
181
182         /* same htio, but different position? We may have to copy,
183         change HCIN, and recalculate the checknum and add_table */
184
185         for(i=1;i<sysconf.hc_possible_num;i++) {  // 0: is hc sblink
186                 if((sysconf.pci1234[i] & 1) != 1 ) continue;
187                 u8 c;
188                 if (i < 7) {
189                         c = (u8) ('4' + i - 1);
190                 } else {
191                         c = (u8) ('A' + i - 1 - 6);
192                 }
193                 current   = ( current + 0x07) & -0x08;
194                 printk(BIOS_DEBUG, "ACPI:    * SSDT for PCI%c at %lx\n", c, current); //pci0 and pci1 are in dsdt
195                 ssdtx = (acpi_header_t *)current;
196                 switch (sysconf.hcid[i]) {
197                 case 1:
198                         p = &AmlCode_ssdt2;
199                         break;
200                 case 2:
201                         p = &AmlCode_ssdt3;
202                         break;
203                 case 3: /* 8131 */
204                         p = &AmlCode_ssdt4;
205                         break;
206                 default:
207                         /* HTX no io apic */
208                         p = &AmlCode_ssdt5;
209                         break;
210                 }
211                 memcpy(ssdtx, p, sizeof(acpi_header_t));
212                 current += ssdtx->length;
213                 memcpy(ssdtx, p, ssdtx->length);
214                 update_ssdtx((void *)ssdtx, i);
215                 ssdtx->checksum = 0;
216                 ssdtx->checksum = acpi_checksum((u8 *)ssdtx, ssdtx->length);
217                 acpi_add_table(rsdp, ssdtx);
218         }
219 #endif
220
221         /* DSDT */
222         current   = ( current + 0x07) & -0x08;
223         printk(BIOS_DEBUG, "ACPI:    * DSDT at %lx\n", current);
224         dsdt = (acpi_header_t *)current; // it will used by fadt
225         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
226         current += dsdt->length;
227         memcpy(dsdt, &AmlCode, dsdt->length);
228         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n",dsdt,dsdt->length);
229
230         /* FACS */ // it needs 64 bit alignment
231         current   = ( current + 0x07) & -0x08;
232         printk(BIOS_DEBUG, "ACPI:       * FACS at %lx\n", current);
233         facs = (acpi_facs_t *) current; // it will be used by fadt
234         current += sizeof(acpi_facs_t);
235         acpi_create_facs(facs);
236
237         /* FDAT */
238         current   = ( current + 0x07) & -0x08;
239         printk(BIOS_DEBUG, "ACPI:    * FADT at %lx\n", current);
240         fadt = (acpi_fadt_t *) current;
241         current += sizeof(acpi_fadt_t);
242
243         acpi_create_fadt(fadt, facs, dsdt);
244         acpi_add_table(rsdp, fadt);
245
246 #if DUMP_ACPI_TABLES == 1
247         printk(BIOS_DEBUG, "rsdp\n");
248         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
249
250         printk(BIOS_DEBUG, "rsdt\n");
251         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
252
253         printk(BIOS_DEBUG, "madt\n");
254         dump_mem(madt, ((void *)madt) + madt->header.length);
255
256         printk(BIOS_DEBUG, "srat\n");
257         dump_mem(srat, ((void *)srat) + srat->header.length);
258
259         printk(BIOS_DEBUG, "slit\n");
260         dump_mem(slit, ((void *)slit) + slit->header.length);
261
262         printk(BIOS_DEBUG, "ssdt\n");
263         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
264
265         printk(BIOS_DEBUG, "fadt\n");
266         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
267 #endif
268
269         printk(BIOS_INFO, "ACPI: done.\n");
270         return current;
271 }