Merge tag 'tpm-030822' of https://source.denx.de/u-boot/custodians/u-boot-tpm
[platform/kernel/u-boot.git] / lib / lmb.c
index 7bd1255..f21fe67 100644 (file)
--- a/lib/lmb.c
+++ b/lib/lmb.c
 #include <log.h>
 #include <malloc.h>
 
+#include <asm/global_data.h>
+#include <asm/sections.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
 #define LMB_ALLOC_ANYWHERE     0
 
 static void lmb_dump_region(struct lmb_region *rgn, char *name)
@@ -103,7 +108,7 @@ void lmb_init(struct lmb *lmb)
 #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
        lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
        lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
-#else
+#elif IS_ENABLED(CONFIG_LMB_MEMORY_REGIONS)
        lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
        lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
        lmb->memory.region = lmb->memory_regions;
@@ -113,12 +118,47 @@ void lmb_init(struct lmb *lmb)
        lmb->reserved.cnt = 0;
 }
 
+void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align)
+{
+       ulong bank_end;
+       int bank;
+
+       /*
+        * Reserve memory from aligned address below the bottom of U-Boot stack
+        * until end of U-Boot area using LMB to prevent U-Boot from overwriting
+        * that memory.
+        */
+       debug("## Current stack ends at 0x%08lx ", sp);
+
+       /* adjust sp by 4K to be safe */
+       sp -= align;
+       for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
+               if (!gd->bd->bi_dram[bank].size ||
+                   sp < gd->bd->bi_dram[bank].start)
+                       continue;
+               /* Watch out for RAM at end of address space! */
+               bank_end = gd->bd->bi_dram[bank].start +
+                       gd->bd->bi_dram[bank].size - 1;
+               if (sp > bank_end)
+                       continue;
+               if (bank_end > end)
+                       bank_end = end - 1;
+
+               lmb_reserve(lmb, sp, bank_end - sp + 1);
+
+               if (gd->flags & GD_FLG_SKIP_RELOC)
+                       lmb_reserve(lmb, (phys_addr_t)(uintptr_t)_start, gd->mon_len);
+
+               break;
+       }
+}
+
 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
 {
        arch_lmb_reserve(lmb);
        board_lmb_reserve(lmb);
 
-       if (IMAGE_ENABLE_OF_LIBFDT && fdt_blob)
+       if (CONFIG_IS_ENABLED(OF_LIBFDT) && fdt_blob)
                boot_fdt_add_mem_rsv_regions(lmb, fdt_blob);
 }