2 * This file is part of the coreboot project.
4 * Copyright (C) 2007 Advanced Micro Devices, Inc.
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.
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.
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
20 #include <console/console.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>
29 //it seems some functions can be moved arch/x86/boot/acpi.c
31 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
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)) {
44 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
50 unsigned long acpi_create_srat_lapics(unsigned long current)
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)) {
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);
70 static unsigned long resk(uint64_t value)
72 unsigned long resultk;
73 if (value < (1ULL << 42)) {
74 resultk = value >> 10;
81 struct acpi_srat_mem_state {
82 unsigned long current;
85 static void set_srat_mem(void *gp, struct device *dev, struct resource *res)
87 struct acpi_srat_mem_state *state = gp;
88 unsigned long basek, sizek;
89 basek = resk(res->base);
90 sizek = resk(res->size);
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);
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
99 if((basek+sizek)<1024) return;
102 sizek -= 1024 - basek;
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);
110 unsigned long acpi_fill_srat(unsigned long current)
112 struct acpi_srat_mem_state srat_mem_state;
114 /* create all subtables for processors */
115 current = acpi_create_srat_lapics(current);
117 /* create all subteble for memory range */
119 /* 0-640K must be on node 0 */
120 current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, 0, 640, 1);//enable
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);
127 current = srat_mem_state.current;
131 unsigned long acpi_fill_slit(unsigned long current)
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.... */
137 struct sys_info *sysinfox = (struct sys_info *)((CONFIG_RAMTOP) - CONFIG_DCACHE_RAM_GLOBAL_VAR_SIZE);
138 u8 *ln = sysinfox->ln;
141 u8 *p = (u8 *)current;
142 int nodes = sysconf.nodes;
146 memset(p, 0, 8+nodes*nodes);
150 for(i=0;i<nodes;i++) {
151 for(j=0;j<nodes; j++) {
155 hops = (((ln[i*NODE_NUMS+j]>>4) & 0x7)+1);
156 p[i*nodes+j] = hops * 2 + 10;
161 current += 8+nodes*nodes;
165 // moved from mb acpi_tables.c
166 static void intx_to_stream(u32 val, u32 len, u8 *dest)
170 *(dest+i) = (val >> (8*i)) & 0xff;
174 static void int_to_stream(u32 val, u8 *dest)
176 return intx_to_stream(val, 4, dest);
179 // used by acpi_tables.h
180 void update_ssdt(void *ssdt)
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; //
207 HCLK = ssdt+0x4d1; //+5 will be next HCLK
208 HCDN = ssdt+0x57a; //+5 will be next HCDN
209 CBBX = ssdt+0x61f; //
211 CBB2 = ssdt+0x62d; //
214 for(i=0;i<HC_NUMS;i++) {
215 dword = sysconf.ht_c_conf_bus[i];
216 int_to_stream(dword, BUSN+i*5);
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);
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);
232 *SBLK = (u8)(sysconf.sblk);
234 msr = rdmsr(TOP_MEM);
235 int_to_stream(msr.lo, TOM1);
237 int_to_stream(sysconf.sbdn, SBDN);
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);
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);
248 *CBBX = (u8)(CONFIG_CBB);
250 if(CONFIG_CBB == 0xff) {
253 if((sysconf.pci1234[0] >> 12) & 0xff) { //sb chain on other than bus 0
261 if((CONFIG_CBB == 0xff) && (sysconf.nodes>32)) {
263 *CBB2 = (u8)(CONFIG_CBB-1);
271 void update_ssdtx(void *ssdtx, int i)
282 *PCI = (u8) ('4' + i - 1);
284 *PCI = (u8) ('A' + i - 1 - 6);
289 /* FIXME: need to update the GSI id in the ssdtx too */
293 static void update_sspr(void *sspr, u32 nodeid, u32 cpuindex)
303 unsigned offset = 0x94 - 0x7f;
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;
316 sprintf((char*)CPU, "%02x", (char)cpuindex);
317 *CPUIN = (u8) cpuindex;
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;
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[];
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)
344 if(!sysconf.p_state_num) return current;
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;
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)) {
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);
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 */
373 ssdt->checksum = acpi_checksum((unsigned char *)ssdt,ssdt->length);
374 acpi_add_table(rsdp, ssdt);