We define IO_APIC_ADDR in <arch/ioapic.h>, let's use it.
[coreboot.git] / src / mainboard / amd / serengeti_cheetah_fam10 / acpi_tables.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007 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/amdfam10_sysconf.h>
29
30 #include "mb_sysconf.h"
31
32 #define DUMP_ACPI_TABLES 0
33
34 #if DUMP_ACPI_TABLES == 1
35 static void dump_mem(u32 start, u32 end)
36 {
37
38         u32 i;
39         print_debug("dump_mem:");
40         for(i=start;i<end;i++) {
41                 if((i & 0xf)==0) {
42                         printk(BIOS_DEBUG, "\n%08x:", i);
43                 }
44                 printk(BIOS_DEBUG, " %02x", (unsigned char)*((unsigned char *)i));
45         }
46         print_debug("\n");
47 }
48 #endif
49
50 extern const unsigned char AmlCode[];
51 extern const unsigned char AmlCode_ssdt[];
52
53 #if CONFIG_ACPI_SSDTX_NUM >= 1
54 extern const unsigned char AmlCode_ssdt2[];
55 extern const unsigned char AmlCode_ssdt3[];
56 extern const unsigned char AmlCode_ssdt4[];
57 extern const unsigned char AmlCode_ssdt5[];
58 #endif
59
60 unsigned long acpi_fill_mcfg(unsigned long current)
61 {
62         /* Just a dummy */
63         return current;
64 }
65
66 unsigned long acpi_fill_madt(unsigned long current)
67 {
68         u32 gsi_base=0x18;
69
70         struct mb_sysconf_t *m;
71
72         m = sysconf.mb;
73
74         /* create all subtables for processors */
75         current = acpi_create_madt_lapics(current);
76
77         /* Write 8111 IOAPIC */
78         current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current, m->apicid_8111,
79                         IO_APIC_ADDR, 0);
80
81         /* Write all 8131 IOAPICs */
82         {
83                 device_t dev;
84                 struct resource *res;
85                 dev = dev_find_slot(m->bus_8132_0, PCI_DEVFN((sysconf.hcdn[0]&0xff), 1));
86                 if (dev) {
87                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
88                         if (res) {
89                                 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current, m->apicid_8132_1,
90                                         res->base, gsi_base );
91                                 gsi_base+=7;
92
93                         }
94                 }
95                 dev = dev_find_slot(m->bus_8132_0, PCI_DEVFN((sysconf.hcdn[0] & 0xff)+1, 1));
96                 if (dev) {
97                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
98                         if (res) {
99                                 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current, m->apicid_8132_2,
100                                         res->base, gsi_base );
101                                 gsi_base+=7;
102                         }
103                 }
104
105                 int i;
106                 int j = 0;
107
108                 for(i=1; i< sysconf.hc_possible_num; i++) {
109                         u32 d = 0;
110                         if(!(sysconf.pci1234[i] & 0x1) ) continue;
111                         // 8131 need to use +4
112                         switch (sysconf.hcid[i]) {
113                         case 1:
114                                 d = 7;
115                                 break;
116                         case 3:
117                                 d = 4;
118                                 break;
119                         }
120                         switch (sysconf.hcid[i]) {
121                         case 1:
122                         case 3:
123                                 dev = dev_find_slot(m->bus_8132a[j][0], PCI_DEVFN(m->sbdn3a[j], 1));
124                                 if (dev) {
125                                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
126                                         if (res) {
127                                                 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current, m->apicid_8132a[j][0],
128                                                         res->base, gsi_base );
129                                                 gsi_base+=d;
130                                         }
131                                 }
132                                 dev = dev_find_slot(m->bus_8132a[j][0], PCI_DEVFN(m->sbdn3a[j]+1, 1));
133                                 if (dev) {
134                                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
135                                         if (res) {
136                                                 current += acpi_create_madt_ioapic((acpi_madt_ioapic_t *)current, m->apicid_8132a[j][1],
137                                                         res->base, gsi_base );
138                                                 gsi_base+=d;
139
140                                         }
141                                 }
142                                 break;
143                         }
144                         j++;
145                 }
146         }
147
148         current += acpi_create_madt_irqoverride( (acpi_madt_irqoverride_t *) current, 0, 0, 2, 5 );
149                 /* 0: mean bus 0--->ISA */
150                 /* 0: PIC 0 */
151                 /* 2: APIC 2 */
152                 /* 5 mean: 0101 --> Edige-triggered, Active high*/
153
154
155                 /* create all subtables for processors */
156         current = acpi_create_madt_lapic_nmis(current, 5, 1);
157                 /* 1: LINT1 connect to NMI */
158
159         return current;
160 }
161
162 unsigned long write_acpi_tables(unsigned long start)
163 {
164         unsigned long current;
165         acpi_rsdp_t *rsdp;
166         acpi_rsdt_t *rsdt;
167         acpi_hpet_t *hpet;
168         acpi_madt_t *madt;
169         acpi_srat_t *srat;
170         acpi_slit_t *slit;
171         acpi_fadt_t *fadt;
172         acpi_facs_t *facs;
173         acpi_header_t *dsdt;
174         acpi_header_t *ssdt;
175         acpi_header_t *ssdtx;
176         void *p;
177
178         int i;
179
180         get_bus_conf(); //it will get sblk, pci1234, hcdn, and sbdn
181
182         /* Align ACPI tables to 16 bytes */
183         start = ( start + 0x0f) & -0x10;
184         current = start;
185
186         printk(BIOS_INFO, "ACPI: Writing ACPI tables at %lx...\n", start);
187
188         /* We need at least an RSDP and an RSDT Table */
189         rsdp = (acpi_rsdp_t *) current;
190         current += sizeof(acpi_rsdp_t);
191         rsdt = (acpi_rsdt_t *) current;
192         current += sizeof(acpi_rsdt_t);
193
194         /* clear all table memory */
195         memset((void *)start, 0, current - start);
196
197         acpi_write_rsdp(rsdp, rsdt, NULL);
198         acpi_write_rsdt(rsdt);
199
200         /*
201          * We explicitly add these tables later on:
202          */
203         current   = ( current + 0x07) & -0x08;
204         printk(BIOS_DEBUG, "ACPI:    * HPET at %lx\n", current);
205         hpet = (acpi_hpet_t *) current;
206         current += sizeof(acpi_hpet_t);
207         acpi_create_hpet(hpet);
208         acpi_add_table(rsdp, hpet);
209
210         /* If we want to use HPET Timers Linux wants an MADT */
211         current   = ( current + 0x07) & -0x08;
212         printk(BIOS_DEBUG, "ACPI:    * MADT at %lx\n", current);
213         madt = (acpi_madt_t *) current;
214         acpi_create_madt(madt);
215         current+=madt->header.length;
216         acpi_add_table(rsdp, madt);
217
218         /* SRAT */
219         current   = ( current + 0x07) & -0x08;
220         printk(BIOS_DEBUG, "ACPI:    * SRAT at %lx\n", current);
221         srat = (acpi_srat_t *) current;
222         acpi_create_srat(srat);
223         current+=srat->header.length;
224         acpi_add_table(rsdp, srat);
225
226         /* SLIT */
227         current   = ( current + 0x07) & -0x08;
228         printk(BIOS_DEBUG, "ACPI:   * SLIT at %lx\n", current);
229         slit = (acpi_slit_t *) current;
230         acpi_create_slit(slit);
231         current+=slit->header.length;
232         acpi_add_table(rsdp, slit);
233
234         /* SSDT */
235         current   = ( current + 0x0f) & -0x10;
236         printk(BIOS_DEBUG, "ACPI:    * SSDT at %lx\n", current);
237         ssdt = (acpi_header_t *)current;
238         memcpy(ssdt, &AmlCode_ssdt, sizeof(acpi_header_t));
239         current += ssdt->length;
240         memcpy(ssdt, &AmlCode_ssdt, ssdt->length);
241         //Here you need to set value in pci1234, sblk and sbdn in get_bus_conf.c
242         update_ssdt((void*)ssdt);
243         /* recalculate checksum */
244         ssdt->checksum = 0;
245         ssdt->checksum = acpi_checksum((unsigned char *)ssdt, ssdt->length);
246         acpi_add_table(rsdp, ssdt);
247
248         printk(BIOS_DEBUG, "ACPI:    * SSDT for PState at %lx\n", current);
249         current = acpi_add_ssdt_pstates(rsdp, current);
250
251 #if CONFIG_ACPI_SSDTX_NUM >= 1
252
253         /* same htio, but different possition? We may have to copy,
254         change HCIN, and recalculate the checknum and add_table */
255
256         for(i=1;i<sysconf.hc_possible_num;i++) {  // 0: is hc sblink
257                 if((sysconf.pci1234[i] & 1) != 1 ) continue;
258                 u8 c;
259                 if(i<7) {
260                         c  = (u8) ('4' + i - 1);
261                 }
262                 else {
263                         c  = (u8) ('A' + i - 1 - 6);
264                 }
265                 current = ( current + 0x07) & -0x08;
266                 printk(BIOS_DEBUG, "ACPI:    * SSDT for PCI%c at %lx\n", c, current); //pci0 and pci1 are in dsdt
267                 ssdtx = (acpi_header_t *)current;
268                 switch(sysconf.hcid[i]) {
269                 case 1:
270                         p = &AmlCode_ssdt2;
271                         break;
272                 case 2:
273                         p = &AmlCode_ssdt3;
274                         break;
275                 case 3: //8131
276                         p = &AmlCode_ssdt4;
277                         break;
278                 default:
279                         //HTX no io apic
280                         p = &AmlCode_ssdt5;
281                 }
282                 memcpy(ssdtx, p, sizeof(acpi_header_t));
283                 current += ssdtx->length;
284                 memcpy(ssdtx, p, ssdtx->length);
285                 update_ssdtx((void *)ssdtx, i);
286                 ssdtx->checksum = 0;
287                 ssdtx->checksum = acpi_checksum((u8 *)ssdtx, ssdtx->length);
288                 acpi_add_table(rsdp, ssdtx);
289         }
290 #endif
291
292         /* DSDT */
293         current   = ( current + 0x07) & -0x08;
294         printk(BIOS_DEBUG, "ACPI:    * DSDT at %lx\n", current);
295         dsdt = (acpi_header_t *)current;
296         memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
297         current += dsdt->length;
298         memcpy(dsdt, &AmlCode, dsdt->length);
299         printk(BIOS_DEBUG, "ACPI:    * DSDT @ %p Length %x\n", dsdt, dsdt->length);
300
301         /* FACS */ // it needs 64 bit alignment
302         current   = ( current + 0x07) & -0x08;
303         printk(BIOS_DEBUG, "ACPI:       * FACS at %lx\n", current);
304         facs = (acpi_facs_t *) current;
305         current += sizeof(acpi_facs_t);
306         acpi_create_facs(facs);
307
308         /* FDAT */
309         current   = ( current + 0x07) & -0x08;
310         printk(BIOS_DEBUG, "ACPI:    * FADT at %lx\n", current);
311         fadt = (acpi_fadt_t *) current;
312         current += sizeof(acpi_fadt_t);
313
314         acpi_create_fadt(fadt, facs, dsdt);
315         acpi_add_table(rsdp, fadt);
316
317 #if DUMP_ACPI_TABLES == 1
318         printk(BIOS_DEBUG, "rsdp\n");
319         dump_mem(rsdp, ((void *)rsdp) + sizeof(acpi_rsdp_t));
320
321         printk(BIOS_DEBUG, "rsdt\n");
322         dump_mem(rsdt, ((void *)rsdt) + sizeof(acpi_rsdt_t));
323
324         printk(BIOS_DEBUG, "madt\n");
325         dump_mem(madt, ((void *)madt) + madt->header.length);
326
327         printk(BIOS_DEBUG, "srat\n");
328         dump_mem(srat, ((void *)srat) + srat->header.length);
329
330         printk(BIOS_DEBUG, "slit\n");
331         dump_mem(slit, ((void *)slit) + slit->header.length);
332
333         printk(BIOS_DEBUG, "ssdt\n");
334         dump_mem(ssdt, ((void *)ssdt) + ssdt->length);
335
336         printk(BIOS_DEBUG, "fadt\n");
337         dump_mem(fadt, ((void *)fadt) + fadt->header.length);
338 #endif
339
340         printk(BIOS_INFO, "ACPI: done.\n");
341         return current;
342 }
343