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