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