printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / northbridge / amd / amdk8 / amdk8_acpi.c
index c2819229920b9aa8afdd05488b2befa6f9bc39f4..1257f8663365b720e7d3529632bde5a7e4429027 100644 (file)
@@ -40,34 +40,14 @@ acknowledgement of AMD's proprietary rights in them.
 #include <console/console.h>
 #include <string.h>
 #include <arch/acpi.h>
+#include <arch/acpigen.h>
 #include <device/pci.h>
 #include <cpu/x86/msr.h>
 #include <cpu/amd/mtrr.h>
 #include <cpu/amd/amdk8_sysconf.h>
+#include "amdk8_acpi.h"
 
-//it seems these function can be moved arch/i386/boot/acpi.c
-
-unsigned long acpi_create_madt_lapics(unsigned long current)
-{
-       device_t cpu;
-       int cpu_index = 0;
-
-       for(cpu = all_devices; cpu; cpu = cpu->next) {
-               if ((cpu->path.type != DEVICE_PATH_APIC) ||
-                       (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
-               {
-                       continue;
-               }
-               if (!cpu->enabled) {
-                       continue;
-               }
-               current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, cpu_index, cpu->path.u.apic.apic_id);
-               cpu_index++;
-
-       }
-
-       return current;
-}
+//it seems some functions can be moved arch/i386/boot/acpi.c
 
 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
 {
@@ -76,8 +56,7 @@ unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 l
 
        for(cpu = all_devices; cpu; cpu = cpu->next) {
                if ((cpu->path.type != DEVICE_PATH_APIC) ||
-                       (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
-               {
+                   (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
                        continue;
                }
                if (!cpu->enabled) {
@@ -85,9 +64,7 @@ unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 l
                }
                current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
                cpu_index++;
-
        }
-
        return current;
 }
 
@@ -98,19 +75,16 @@ unsigned long acpi_create_srat_lapics(unsigned long current)
 
        for(cpu = all_devices; cpu; cpu = cpu->next) {
                if ((cpu->path.type != DEVICE_PATH_APIC) ||
-                       (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
-               {
+                   (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
                        continue;
                }
                if (!cpu->enabled) {
                        continue;
                }
-               printk_debug("SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n", cpu_index, cpu->path.u.apic.node_id, cpu->path.u.apic.apic_id);
-               current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current, cpu->path.u.apic.node_id, cpu->path.u.apic.apic_id);
+               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);
+               current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current, cpu->path.apic.node_id, cpu->path.apic.apic_id);
                cpu_index++;
-
        }
-
        return current;
 }
 
@@ -119,8 +93,7 @@ static unsigned long resk(uint64_t value)
        unsigned long resultk;
        if (value < (1ULL << 42)) {
                resultk = value >> 10;
-       }
-       else {
+       } else {
                resultk = 0xffffffff;
        }
        return resultk;
@@ -130,20 +103,20 @@ struct acpi_srat_mem_state {
        unsigned long current;
 };
 
-void set_srat_mem(void *gp, struct device *dev, struct resource *res)
+static void set_srat_mem(void *gp, struct device *dev, struct resource *res)
 {
        struct acpi_srat_mem_state *state = gp;
        unsigned long basek, sizek;
        basek = resk(res->base);
        sizek = resk(res->size);
 
-       printk_debug("set_srat_mem: dev %s, res->index=%04x startk=%08x, sizek=%08x\n",
+       printk(BIOS_DEBUG, "set_srat_mem: dev %s, res->index=%04lx startk=%08lx, sizek=%08lx\n",
                     dev_path(dev), res->index, basek, sizek);
        /*
-               0-640K must be on node 0
-               next range is from 1M---
-               So will cut off before 1M in the mem range
-       */
+        * 0-640K must be on node 0
+        * next range is from 1M---
+        * So will cut off before 1M in the mem range
+        */
        if((basek+sizek)<1024) return;
 
        if(basek<1024) {
@@ -151,7 +124,8 @@ void set_srat_mem(void *gp, struct device *dev, struct resource *res)
                basek = 1024;
        }
 
-       state->current += acpi_create_srat_mem((acpi_srat_mem_t *)state->current, (res->index & 0xf), basek, sizek, 1); // need to figure out NV
+       // need to figure out NV
+       state->current += acpi_create_srat_mem((acpi_srat_mem_t *)state->current, (res->index & 0xf), basek, sizek, 1);
 }
 
 unsigned long acpi_fill_srat(unsigned long current)
@@ -172,11 +146,9 @@ unsigned long acpi_fill_srat(unsigned long current)
                set_srat_mem, &srat_mem_state);
 
        current = srat_mem_state.current;
-
        return current;
 }
 
-
 unsigned long acpi_fill_slit(unsigned long current)
 {
        /* need to find out the node num at first */
@@ -184,7 +156,7 @@ unsigned long acpi_fill_slit(unsigned long current)
        /* fill the next num*num byte with distance, local is 10, 1 hop mean 20, and 2 hop with 30.... */
 
        /* because We has assume that we know the topology of the HT connection, So we can have set if we know the node_num */
-       static uint8_t hops_8[] = {   0, 1, 1, 2, 2, 3, 3, 4,
+       static u8 hops_8[] = {   0, 1, 1, 2, 2, 3, 3, 4,
                                      1, 0, 2, 1, 3, 2, 4, 3,
                                      1, 2, 0, 1, 1, 2, 2, 3,
                                      2, 1, 1, 0, 2, 1, 3, 2,
@@ -193,14 +165,14 @@ unsigned long acpi_fill_slit(unsigned long current)
                                      3, 4, 2, 3, 1, 2, 0, 1,
                                      4, 4, 3, 2, 2, 1, 1, 0 };
 
-//     uint8_t outer_node[8];
+//     u8 outer_node[8];
 
-       uint8_t *p = (uint8_t *)current;
+       u8 *p = (u8 *)current;
        int nodes = sysconf.nodes;
        int i,j;
        memset(p, 0, 8+nodes*nodes);
-//     memset((uint8_t *)outer_node, 0, 8);
-       *p = (uint8_t) nodes;
+//     memset((u8 *)outer_node, 0, 8);
+       *p = (u8) nodes;
        p += 8;
 
 #if 0
@@ -212,11 +184,12 @@ unsigned long acpi_fill_slit(unsigned long current)
 
        for(i=0;i<nodes;i++) {
                for(j=0;j<nodes; j++) {
-                       if(i==j) { p[i*nodes+j] = 10; }
-                       else {
+                       if(i==j) {
+                               p[i*nodes+j] = 10;
+                       } else {
 #if 0
                                int k;
-                               uint8_t latency_factor = 0;
+                               u8 latency_factor = 0;
                                int k_start, k_end;
                                if(i<j) {
                                        k_start = i;
@@ -236,7 +209,6 @@ unsigned long acpi_fill_slit(unsigned long current)
                                p[i*nodes+j] = hops_8[i*nodes+j] * 2 + 10;
 #endif
 
-
                        }
                }
        }
@@ -246,86 +218,76 @@ unsigned long acpi_fill_slit(unsigned long current)
        return current;
 }
 
+static int k8acpi_write_HT(void) {
+       int len, lenp, i;
 
-//end
-
-
-// moved from mb acpi_tables.c
-static void int_to_stream(uint32_t val, uint8_t *dest)
-{
-       int i;
-       for(i=0;i<4;i++) {
-               *(dest+i) = (val >> (8*i)) & 0xff;
-       }
-}
-
-
-// used by acpi_tables.h
-
-void update_ssdt(void *ssdt)
-{
-       uint8_t *BUSN;
-       uint8_t *MMIO;
-       uint8_t *PCIO;
-       uint8_t *SBLK;
-       uint8_t *TOM1;
-       uint8_t *SBDN;
-       uint8_t *HCLK;
-       uint8_t *HCDN;
-       uint8_t *CBST;
-
-       int i;
-       device_t dev;
-       uint32_t dword;
-       msr_t msr;
-
-       BUSN = ssdt+0x3a; //+5 will be next BUSN
-       MMIO = ssdt+0x57; //+5 will be next MMIO
-       PCIO = ssdt+0xaf; //+5 will be next PCIO
-       SBLK = ssdt+0xdc; // one byte
-       TOM1 = ssdt+0xe3; //
-       SBDN = ssdt+0xed; //
-       HCLK = ssdt+0xfa; //+5 will be next HCLK
-       HCDN = ssdt+0x12a; //+5 will be next HCDN
-       CBST = ssdt+0x157; //
+       len = acpigen_write_name("HCLK");
+       lenp = acpigen_write_package(HC_POSSIBLE_NUM);
 
-       dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
-       for(i=0;i<4;i++) {
-               dword = pci_read_config32(dev, 0xe0+i*4);
-               int_to_stream(dword, BUSN+i*5);
-       }
-       for(i=0;i<0x10;i++) {
-               dword = pci_read_config32(dev, 0x80+i*4);
-               int_to_stream(dword, MMIO+i*5);
+       for(i=0;i<sysconf.hc_possible_num;i++) {
+               lenp += acpigen_write_dword(sysconf.pci1234[i]);
        }
-       for(i=0;i<0x08;i++) {
-               dword = pci_read_config32(dev, 0xc0+i*4);
-               int_to_stream(dword, PCIO+i*5);
+       for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
+               lenp += acpigen_write_dword(0x0);
        }
 
-       *SBLK = (uint8_t)(sysconf.sblk);
+       acpigen_patch_len(lenp - 1);
+       len += lenp;
 
-       msr = rdmsr(TOP_MEM);
-       int_to_stream(msr.lo, TOM1);
+       len += acpigen_write_name("HCDN");
+       lenp = acpigen_write_package(HC_POSSIBLE_NUM);
 
        for(i=0;i<sysconf.hc_possible_num;i++) {
-               int_to_stream(sysconf.pci1234[i], HCLK + i*5);
-               int_to_stream(sysconf.hcdn[i],    HCDN + i*5);
+               lenp += acpigen_write_dword(sysconf.hcdn[i]);
        }
        for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
-               int_to_stream(0x00000000, HCLK + i*5);
-               int_to_stream(0x20202020, HCDN + i*5);
+               lenp += acpigen_write_dword(0x20202020);
        }
+       acpigen_patch_len(lenp - 1);
+       len += lenp;
 
-       int_to_stream(sysconf.sbdn, SBDN);
+       return len;
+}
 
-       if((sysconf.pci1234[0] >> 12) & 0xff) { //sb chain on  other than bus 0
-               *CBST = (uint8_t) (0x0f);
-       }
-       else {
-               *CBST = (uint8_t) (0x00);
-       }
+static int k8acpi_write_pci_data(int dlen, const char *name, int offset) {
+       device_t dev;
+       uint32_t dword;
+       int len, lenp, i;
 
+       dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
+
+       len = acpigen_write_name(name);
+       lenp = acpigen_write_package(dlen);
+       for(i=0; i<dlen; i++) {
+               dword = pci_read_config32(dev, offset+i*4);
+               lenp += acpigen_write_dword(dword);
+       }
+       // minus the opcode
+       acpigen_patch_len(lenp - 1);
+       return len + lenp;
 }
 
-//end
+int k8acpi_write_vars(void)
+{
+       int lens;
+       msr_t msr;
+       char pscope[] = "\\_SB.PCI0";
+
+       lens = acpigen_write_scope(pscope);
+       lens += k8acpi_write_pci_data(4, "BUSN", 0xe0);
+       lens += k8acpi_write_pci_data(8, "PCIO", 0xc0);
+       lens += k8acpi_write_pci_data(16, "MMIO", 0x80);
+       lens += acpigen_write_name_byte("SBLK", sysconf.sblk);
+       lens += acpigen_write_name_byte("CBST",
+           ((sysconf.pci1234[0] >> 12) & 0xff) ? 0xf : 0x0);
+       lens += acpigen_write_name_dword("SBDN", sysconf.sbdn);
+       msr = rdmsr(TOP_MEM);
+       lens += acpigen_write_name_dword("TOM1", msr.lo);
+       msr = rdmsr(TOP_MEM2);
+       lens += acpigen_write_name_qword("TOM2", (((uint64_t) msr.hi) << 32) | msr.lo);
+
+       lens += k8acpi_write_HT();
+       //minus opcode
+       acpigen_patch_len(lens - 1);
+       return lens;
+}