printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / northbridge / amd / amdfam10 / amdfam10_acpi.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 <cpu/x86/msr.h>
25 #include <cpu/amd/mtrr.h>
26 #include <cpu/amd/amdfam10_sysconf.h>
27 #include "amdfam10.h"
28
29 //it seems some functions can be moved arch/i386/boot/acpi.c
30
31 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
32 {
33         device_t cpu;
34         int cpu_index = 0;
35
36         for(cpu = all_devices; cpu; cpu = cpu->next) {
37                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
38                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
39                         continue;
40                 }
41                 if (!cpu->enabled) {
42                         continue;
43                 }
44                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
45                 cpu_index++;
46         }
47         return current;
48 }
49
50 unsigned long acpi_create_srat_lapics(unsigned long current)
51 {
52         device_t cpu;
53         int cpu_index = 0;
54
55         for(cpu = all_devices; cpu; cpu = cpu->next) {
56                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
57                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
58                         continue;
59                 }
60                 if (!cpu->enabled) {
61                         continue;
62                 }
63                 printk(BIOS_DEBUG, "SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n", cpu_index, cpu->path.apic.node_id, cpu->path.apic.apic_id);
64                 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current, cpu->path.apic.node_id, cpu->path.apic.apic_id);
65                 cpu_index++;
66         }
67         return current;
68 }
69
70 static unsigned long resk(uint64_t value)
71 {
72         unsigned long resultk;
73         if (value < (1ULL << 42)) {
74                 resultk = value >> 10;
75         } else {
76                 resultk = 0xffffffff;
77         }
78         return resultk;
79 }
80
81 struct acpi_srat_mem_state {
82         unsigned long current;
83 };
84
85 static void set_srat_mem(void *gp, struct device *dev, struct resource *res)
86 {
87         struct acpi_srat_mem_state *state = gp;
88         unsigned long basek, sizek;
89         basek = resk(res->base);
90         sizek = resk(res->size);
91
92         printk(BIOS_DEBUG, "set_srat_mem: dev %s, res->index=%04x startk=%08x, sizek=%08x\n",
93                         dev_path(dev), res->index, basek, sizek);
94         /*
95          * 0-640K must be on node 0
96          * next range is from 1M---
97          * So will cut off before 1M in the mem range
98          */
99         if((basek+sizek)<1024) return;
100
101         if(basek<1024) {
102                 sizek -= 1024 - basek;
103                 basek = 1024;
104         }
105
106         // need to figure out NV
107         state->current += acpi_create_srat_mem((acpi_srat_mem_t *)state->current, (res->index & 0xf), basek, sizek, 1);
108 }
109
110 unsigned long acpi_fill_srat(unsigned long current)
111 {
112         struct acpi_srat_mem_state srat_mem_state;
113
114         /* create all subtables for processors */
115         current = acpi_create_srat_lapics(current);
116
117         /* create all subteble for memory range */
118
119         /* 0-640K must be on node 0 */
120         current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, 0, 640, 1);//enable
121
122         srat_mem_state.current = current;
123         search_global_resources(
124                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
125                 set_srat_mem, &srat_mem_state);
126
127         current = srat_mem_state.current;
128         return current;
129 }
130
131 unsigned long acpi_fill_slit(unsigned long current)
132 {
133         /* need to find out the node num at first */
134         /* fill the first 8 byte with that num */
135         /* fill the next num*num byte with distance, local is 10, 1 hop mean 20, and 2 hop with 30.... */
136
137         struct sys_info *sysinfox = (struct sys_info *)((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
138         u8 *ln = sysinfox->ln;
139
140
141         u8 *p = (u8 *)current;
142         int nodes = sysconf.nodes;
143         int i,j;
144         u32 hops;
145
146         memset(p, 0, 8+nodes*nodes);
147         *p = (u8) nodes;
148         p += 8;
149
150         for(i=0;i<nodes;i++) {
151                 for(j=0;j<nodes; j++) {
152                         if(i==j) {
153                                 p[i*nodes+j] = 10;
154                         } else {
155                                 hops = (((ln[i*NODE_NUMS+j]>>4) & 0x7)+1);
156                                 p[i*nodes+j] = hops * 2 + 10;
157                         }
158                 }
159         }
160
161         current += 8+nodes*nodes;
162         return current;
163 }
164
165 // moved from mb acpi_tables.c
166 static void intx_to_stream(u32 val, u32 len, u8 *dest)
167 {
168         int i;
169         for(i=0;i<len;i++) {
170                 *(dest+i) = (val >> (8*i)) & 0xff;
171         }
172 }
173
174 static void int_to_stream(u32 val, u8 *dest)
175 {
176         return intx_to_stream(val, 4, dest);
177 }
178
179 // used by acpi_tables.h
180 void update_ssdt(void *ssdt)
181 {
182         u8 *BUSN;
183         u8 *MMIO;
184         u8 *PCIO;
185         u8 *SBLK;
186         u8 *TOM1;
187         u8 *SBDN;
188         u8 *HCLK;
189         u8 *HCDN;
190         u8 *CBST;
191         u8 *CBBX;
192         u8 *CBS2;
193         u8 *CBB2;
194
195
196         int i;
197         u32 dword;
198         msr_t msr;
199
200         // the offset could be different if have different HC_NUMS, and HC_POSSIBLE_NUM and ssdt.asl
201         BUSN = ssdt+0x3b; //+5 will be next BUSN
202         MMIO = ssdt+0xe4; //+5 will be next MMIO
203         PCIO = ssdt+0x36d; //+5 will be next PCIO
204         SBLK = ssdt+0x4b2; // one byte
205         TOM1 = ssdt+0x4b9; //
206         SBDN = ssdt+0x4c3;//
207         HCLK = ssdt+0x4d1; //+5 will be next HCLK
208         HCDN = ssdt+0x57a; //+5 will be next HCDN
209         CBBX = ssdt+0x61f; //
210         CBST = ssdt+0x626;
211         CBB2 = ssdt+0x62d; //
212         CBS2 = ssdt+0x634;
213
214         for(i=0;i<HC_NUMS;i++) {
215                 dword = sysconf.ht_c_conf_bus[i];
216                 int_to_stream(dword, BUSN+i*5);
217         }
218
219         for(i=0;i<(HC_NUMS*2);i++) { // FIXME: change to more chain
220                 dword = sysconf.conf_mmio_addrx[i]; //base
221                 int_to_stream(dword, MMIO+(i*2)*5);
222                 dword = sysconf.conf_mmio_addr[i]; //mask
223                 int_to_stream(dword, MMIO+(i*2+1)*5);
224         }
225         for(i=0;i<HC_NUMS;i++) { // FIXME: change to more chain
226                 dword = sysconf.conf_io_addrx[i];
227                 int_to_stream(dword, PCIO+(i*2)*5);
228                 dword = sysconf.conf_io_addr[i];
229                 int_to_stream(dword, PCIO+(i*2+1)*5);
230         }
231
232         *SBLK = (u8)(sysconf.sblk);
233
234         msr = rdmsr(TOP_MEM);
235         int_to_stream(msr.lo, TOM1);
236
237         int_to_stream(sysconf.sbdn, SBDN);
238
239         for(i=0;i<sysconf.hc_possible_num;i++) {
240                 int_to_stream(sysconf.pci1234[i], HCLK + i*5);
241                 int_to_stream(sysconf.hcdn[i],     HCDN + i*5);
242         }
243         for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
244                 int_to_stream(0x00000000, HCLK + i*5);
245                 int_to_stream(0x20202020, HCDN + i*5);
246         }
247
248         *CBBX = (u8)(CONFIG_CBB);
249
250         if(CONFIG_CBB == 0xff) {
251                 *CBST = (u8) (0x0f);
252         } else {
253                 if((sysconf.pci1234[0] >> 12) & 0xff) { //sb chain on  other than bus 0
254                         *CBST = (u8) (0x0f);
255                 }
256                 else {
257                         *CBST = (u8) (0x00);
258                 }
259         }
260
261         if((CONFIG_CBB == 0xff) && (sysconf.nodes>32)) {
262                  *CBS2 = 0x0f;
263                  *CBB2 = (u8)(CONFIG_CBB-1);
264         } else {
265                 *CBS2 = 0x00;
266                 *CBB2 = 0x00;
267         }
268
269 }
270
271 void update_sspr(void *sspr, u32 nodeid, u32 cpuindex)
272 {
273         u8 *CPU;
274         u8 *CPUIN;
275         u8 *COREFREQ;
276         u8 *POWER;
277         u8 *TRANSITION_LAT;
278         u8 *BUSMASTER_LAT;
279         u8 *CONTROL;
280         u8 *STATUS;
281         unsigned offset = 0x94 - 0x7f;
282         int i;
283
284         CPU = sspr + 0x38;
285         CPUIN = sspr + 0x3a;
286
287         COREFREQ = sspr + 0x7f; //2 byte
288         POWER = sspr + 0x82; //3 bytes
289         TRANSITION_LAT = sspr + 0x87; //two bytes
290         BUSMASTER_LAT = sspr + 0x8a; //two bytes
291         CONTROL = sspr + 0x8d;
292         STATUS = sspr + 0x8f;
293
294         sprintf(CPU, "%02x", (u8)cpuindex);
295         *CPUIN = (u8) cpuindex;
296
297         for(i=0;i<sysconf.p_state_num;i++) {
298                 struct p_state_t *p_state = &sysconf.p_state[nodeid * 5 + i];
299                 intx_to_stream(COREFREQ + i*offset, 2, p_state->corefreq);
300                 intx_to_stream(POWER + i*offset, 3, p_state->power);
301                 intx_to_stream(TRANSITION_LAT + i*offset, 2, p_state->transition_lat);
302                 intx_to_stream(BUSMASTER_LAT + i*offset, 2, p_state->busmaster_lat);
303                 *((u8 *)(CONTROL + i*offset)) =(u8) p_state->control;
304                 *((u8 *)(STATUS + i*offset)) =(u8) p_state->status;
305         }
306 }
307
308 extern unsigned char AmlCode_sspr5[];
309 extern unsigned char AmlCode_sspr4[];
310 extern unsigned char AmlCode_sspr3[];
311 extern unsigned char AmlCode_sspr2[];
312 extern unsigned char AmlCode_sspr1[];
313
314 /* fixme: find one good way for different p_state_num */
315 unsigned long acpi_add_ssdt_pstates(acpi_rsdp_t *rsdp, unsigned long current)
316 {
317         device_t cpu;
318         int cpu_index = 0;
319
320         acpi_header_t *ssdt;
321
322         if(!sysconf.p_state_num) return current;
323
324         u8 *AmlCode_sspr;
325         switch(sysconf.p_state_num) {
326                 case 1: AmlCode_sspr = AmlCode_sspr1; break;
327                 case 2: AmlCode_sspr = AmlCode_sspr2; break;
328                 case 3: AmlCode_sspr = AmlCode_sspr3; break;
329                 case 4: AmlCode_sspr = AmlCode_sspr4; break;
330                 default: AmlCode_sspr = AmlCode_sspr5; break;
331         }
332
333         for(cpu = all_devices; cpu; cpu = cpu->next) {
334                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
335                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
336                         continue;
337                 }
338                 if (!cpu->enabled) {
339                          continue;
340                 }
341                 printk(BIOS_DEBUG, "ACPI: pstate cpu_index=%02x, node_id=%02x, core_id=%02x\n", cpu_index, cpu->path.apic.node_id, cpu->path.apic.core_id);
342
343                 current   = ( current + 0x0f) & -0x10;
344                 ssdt = (acpi_header_t *)current;
345                 current += ((acpi_header_t *)AmlCode_sspr)->length;
346                 memcpy((void *)ssdt, (void *)AmlCode_sspr, ((acpi_header_t *)AmlCode_sspr)->length);
347                 update_sspr((void*)ssdt,cpu->path.apic.node_id, cpu_index);
348                 /* recalculate checksum */
349                 ssdt->checksum = 0;
350                 ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
351                 acpi_add_table(rsdp, ssdt);
352
353                 cpu_index++;
354         }
355         return current;
356 }