After this has been brought up many times before, rename src/arch/i386 to
[coreboot.git] / src / northbridge / amd / 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/x86/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=%04lx startk=%08lx, sizek=%08lx\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_ssdtx(void *ssdtx, int i)
272 {
273         u8 *PCI;
274         u8 *HCIN;
275         u8 *UID;
276
277         PCI = ssdtx + 0x32;
278         HCIN = ssdtx + 0x39;
279         UID = ssdtx + 0x40;
280
281         if (i < 7) {
282                 *PCI = (u8) ('4' + i - 1);
283         } else {
284                 *PCI = (u8) ('A' + i - 1 - 6);
285         }
286         *HCIN = (u8) i;
287         *UID = (u8) (i + 3);
288
289         /* FIXME: need to update the GSI id in the ssdtx too */
290
291 }
292
293 static void update_sspr(void *sspr, u32 nodeid, u32 cpuindex)
294 {
295         u8 *CPU;
296         u8 *CPUIN;
297         u8 *COREFREQ;
298         u8 *POWER;
299         u8 *TRANSITION_LAT;
300         u8 *BUSMASTER_LAT;
301         u8 *CONTROL;
302         u8 *STATUS;
303         unsigned offset = 0x94 - 0x7f;
304         int i;
305
306         CPU = sspr + 0x38;
307         CPUIN = sspr + 0x3a;
308
309         COREFREQ = sspr + 0x7f; //2 byte
310         POWER = sspr + 0x82; //3 bytes
311         TRANSITION_LAT = sspr + 0x87; //two bytes
312         BUSMASTER_LAT = sspr + 0x8a; //two bytes
313         CONTROL = sspr + 0x8d;
314         STATUS = sspr + 0x8f;
315
316         sprintf((char*)CPU, "%02x", (char)cpuindex);
317         *CPUIN = (u8) cpuindex;
318
319         for(i=0;i<sysconf.p_state_num;i++) {
320                 struct p_state_t *p_state = &sysconf.p_state[nodeid * 5 + i];
321                 intx_to_stream(p_state->corefreq, 2, COREFREQ + i*offset);
322                 intx_to_stream(p_state->power, 3, POWER + i*offset);
323                 intx_to_stream(p_state->transition_lat, 2, TRANSITION_LAT + i*offset);
324                 intx_to_stream(p_state->busmaster_lat, 2, BUSMASTER_LAT + i*offset);
325                 *((u8 *)(CONTROL + i*offset)) =(u8) p_state->control;
326                 *((u8 *)(STATUS + i*offset)) =(u8) p_state->status;
327         }
328 }
329
330 extern const unsigned char AmlCode_sspr5[];
331 extern const unsigned char AmlCode_sspr4[];
332 extern const unsigned char AmlCode_sspr3[];
333 extern const unsigned char AmlCode_sspr2[];
334 extern const unsigned char AmlCode_sspr1[];
335
336 /* fixme: find one good way for different p_state_num */
337 unsigned long acpi_add_ssdt_pstates(acpi_rsdp_t *rsdp, unsigned long current)
338 {
339         device_t cpu;
340         int cpu_index = 0;
341
342         acpi_header_t *ssdt;
343
344         if(!sysconf.p_state_num) return current;
345
346         void *AmlCode_sspr;
347         switch(sysconf.p_state_num) {
348                 case 1: AmlCode_sspr = &AmlCode_sspr1; break;
349                 case 2: AmlCode_sspr = &AmlCode_sspr2; break;
350                 case 3: AmlCode_sspr = &AmlCode_sspr3; break;
351                 case 4: AmlCode_sspr = &AmlCode_sspr4; break;
352                 default: AmlCode_sspr = &AmlCode_sspr5; break;
353         }
354
355         for(cpu = all_devices; cpu; cpu = cpu->next) {
356                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
357                    (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
358                         continue;
359                 }
360                 if (!cpu->enabled) {
361                          continue;
362                 }
363                 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);
364
365                 current   = ( current + 0x0f) & -0x10;
366                 ssdt = (acpi_header_t *)current;
367                 memcpy(ssdt, AmlCode_sspr, sizeof(acpi_header_t));
368                 current += ssdt->length;
369                 memcpy(ssdt, AmlCode_sspr, ssdt->length);
370                 update_sspr((void*)ssdt,cpu->path.apic.node_id, cpu_index);
371                 /* recalculate checksum */
372                 ssdt->checksum = 0;
373                 ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
374                 acpi_add_table(rsdp, ssdt);
375
376                 cpu_index++;
377         }
378         return current;
379 }