We hardcode highmemory size in every northbridge! This is bad, and especially if...
[coreboot.git] / src / lib / cbmem.c
index c57018231a22b49198156f5bdede6e7bb99d75c7..aa10a15798ab44d375607c6ef6a5a7008bbf51b3 100644 (file)
 #include <cbmem.h>
 #include <console/console.h>
 
-#if 1
-#define debug(x...) printk(BIOS_DEBUG, x)
-#else
-#define debug(x...)
-#endif
-
 // The CBMEM TOC reserves 512 bytes to keep
 // the other entries somewhat aligned.
 // Increase if MAX_CBMEM_ENTRIES exceeds 21
@@ -42,8 +36,32 @@ struct cbmem_entry {
        u64 size;
 } __attribute__((packed));
 
-#ifndef __PRE_RAM__
-struct cbmem_entry *bss_cbmem_toc;
+
+#ifdef __PRE_RAM__
+
+/* note this should be done as weak function but we do #include
+   of C files in the romstage breaking this (in same compile
+   unit is weak and non weak function
+struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
+*/
+#ifndef get_cbmem_toc
+       #define get_cbmem_toc() (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE)
+#endif
+
+#else
+
+static struct cbmem_entry *bss_cbmem_toc;
+
+struct cbmem_entry *__attribute__((weak)) get_cbmem_toc(void)
+{
+       return bss_cbmem_toc;
+}
+
+void __attribute__((weak)) set_cbmem_toc(struct cbmem_entry * x)
+{
+       /* do nothing, this should be called by chipset to save TOC in NVRAM */
+}
+
 #endif
 
 /**
@@ -64,14 +82,19 @@ void cbmem_init(u64 baseaddr, u64 size)
 #ifndef __PRE_RAM__
        bss_cbmem_toc = cbmem_toc;
 #endif
-       
-       debug("Initializing CBMEM area to 0x%llx (%lld bytes)\n", baseaddr, size);
+
+       printk(BIOS_DEBUG, "Initializing CBMEM area to 0x%llx (%lld bytes)\n",
+              baseaddr, size);
 
        if (size < (64 * 1024)) {
-               debug("Increase CBMEM size!!\n");
+               printk(BIOS_DEBUG, "Increase CBMEM size!\n");
                for (;;) ;
        }
 
+       /* we don't need to call this in romstage, usefull only from ramstage */
+#ifndef __PRE_RAM__
+       set_cbmem_toc((struct cbmem_entry *)(unsigned long)baseaddr);
+#endif
        memset(cbmem_toc, 0, CBMEM_TOC_RESERVED);
 
        cbmem_toc[0] = (struct cbmem_entry) {
@@ -87,7 +110,9 @@ int cbmem_reinit(u64 baseaddr)
        struct cbmem_entry *cbmem_toc;
        cbmem_toc = (struct cbmem_entry *)(unsigned long)baseaddr;
 
-       debug("Re-Initializing CBMEM area to 0x%lx\n", (unsigned long)baseaddr);
+       printk(BIOS_DEBUG, "Re-Initializing CBMEM area to 0x%lx\n",
+              (unsigned long)baseaddr);
+
 #ifndef __PRE_RAM__
        bss_cbmem_toc = cbmem_toc;
 #endif
@@ -99,12 +124,8 @@ void *cbmem_add(u32 id, u64 size)
 {
        struct cbmem_entry *cbmem_toc;
        int i;
-#ifdef __PRE_RAM__
-        cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
-#else
-        cbmem_toc = bss_cbmem_toc;
-#endif
-       
+       cbmem_toc = get_cbmem_toc();
+
        if (cbmem_toc == NULL) {
                return NULL;
        }
@@ -122,7 +143,7 @@ void *cbmem_add(u32 id, u64 size)
 
        /* Align size to 512 byte blocks */
 
-       size = ALIGN(size, 512) < cbmem_toc[0].size ? 
+       size = ALIGN(size, 512) < cbmem_toc[0].size ?
                ALIGN(size, 512) : cbmem_toc[0].size;
 
        /* Now look for the first free/usable TOC entry */
@@ -136,7 +157,7 @@ void *cbmem_add(u32 id, u64 size)
                return NULL;
        }
 
-       debug("Adding CBMEM entry as no. %d\n", i);
+       printk(BIOS_DEBUG, "Adding CBMEM entry as no. %d\n", i);
 
        cbmem_toc[i] = (struct cbmem_entry) {
                .magic = CBMEM_MAGIC,
@@ -155,12 +176,8 @@ void *cbmem_find(u32 id)
 {
        struct cbmem_entry *cbmem_toc;
        int i;
-#ifdef __PRE_RAM__
-        cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
-#else
-        cbmem_toc = bss_cbmem_toc;
-#endif
-       
+       cbmem_toc = get_cbmem_toc();
+
        if (cbmem_toc == NULL)
                return NULL;
 
@@ -176,7 +193,6 @@ void *cbmem_find(u32 id)
 #if CONFIG_HAVE_ACPI_RESUME
 extern u8 acpi_slp_type;
 #endif
-extern uint64_t high_tables_base, high_tables_size;
 
 void cbmem_initialize(void)
 {
@@ -201,12 +217,8 @@ void cbmem_list(void)
 {
        struct cbmem_entry *cbmem_toc;
        int i;
-#ifdef __PRE_RAM__
-        cbmem_toc = (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
-#else
-        cbmem_toc = bss_cbmem_toc;
-#endif
-       
+       cbmem_toc = get_cbmem_toc();
+
        if (cbmem_toc == NULL)
                return;