Merge branch 'master' of git://git.denx.de/u-boot-spi
[platform/kernel/u-boot.git] / lib / efi_loader / efi_memory.c
index d533aee..4bb5174 100644 (file)
@@ -7,13 +7,16 @@
 
 #include <common.h>
 #include <efi_loader.h>
-#include <inttypes.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <watchdog.h>
 #include <linux/list_sort.h>
+#include <linux/sizes.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+efi_uintn_t efi_memory_map_key;
+
 struct efi_mem_list {
        struct list_head link;
        struct efi_mem_desc desc;
@@ -63,9 +66,54 @@ static int efi_mem_cmp(void *priv, struct list_head *a, struct list_head *b)
                return -1;
 }
 
+static uint64_t desc_get_end(struct efi_mem_desc *desc)
+{
+       return desc->physical_start + (desc->num_pages << EFI_PAGE_SHIFT);
+}
+
 static void efi_mem_sort(void)
 {
+       struct list_head *lhandle;
+       struct efi_mem_list *prevmem = NULL;
+       bool merge_again = true;
+
        list_sort(NULL, &efi_mem, efi_mem_cmp);
+
+       /* Now merge entries that can be merged */
+       while (merge_again) {
+               merge_again = false;
+               list_for_each(lhandle, &efi_mem) {
+                       struct efi_mem_list *lmem;
+                       struct efi_mem_desc *prev = &prevmem->desc;
+                       struct efi_mem_desc *cur;
+                       uint64_t pages;
+
+                       lmem = list_entry(lhandle, struct efi_mem_list, link);
+                       if (!prevmem) {
+                               prevmem = lmem;
+                               continue;
+                       }
+
+                       cur = &lmem->desc;
+
+                       if ((desc_get_end(cur) == prev->physical_start) &&
+                           (prev->type == cur->type) &&
+                           (prev->attribute == cur->attribute)) {
+                               /* There is an existing map before, reuse it */
+                               pages = cur->num_pages;
+                               prev->num_pages += pages;
+                               prev->physical_start -= pages << EFI_PAGE_SHIFT;
+                               prev->virtual_start -= pages << EFI_PAGE_SHIFT;
+                               list_del(&lmem->link);
+                               free(lmem);
+
+                               merge_again = true;
+                               break;
+                       }
+
+                       prevmem = lmem;
+               }
+       }
 }
 
 /** efi_mem_carve_out - unmap memory region
@@ -156,12 +204,16 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
        bool carve_again;
        uint64_t carved_pages = 0;
 
-       debug("%s: 0x%" PRIx64 " 0x%" PRIx64 " %d %s\n", __func__,
+       debug("%s: 0x%llx 0x%llx %d %s\n", __func__,
              start, pages, memory_type, overlap_only_ram ? "yes" : "no");
 
+       if (memory_type >= EFI_MAX_MEMORY_TYPE)
+               return EFI_INVALID_PARAMETER;
+
        if (!pages)
                return start;
 
+       ++efi_memory_map_key;
        newlist = calloc(1, sizeof(*newlist));
        newlist->desc.type = memory_type;
        newlist->desc.physical_start = start;
@@ -171,14 +223,13 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
        switch (memory_type) {
        case EFI_RUNTIME_SERVICES_CODE:
        case EFI_RUNTIME_SERVICES_DATA:
-               newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
-                                         (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
+               newlist->desc.attribute = EFI_MEMORY_WB | EFI_MEMORY_RUNTIME;
                break;
        case EFI_MMAP_IO:
-               newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
+               newlist->desc.attribute = EFI_MEMORY_RUNTIME;
                break;
        default:
-               newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
+               newlist->desc.attribute = EFI_MEMORY_WB;
                break;
        }
 
@@ -244,6 +295,12 @@ static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
 {
        struct list_head *lhandle;
 
+       /*
+        * Prealign input max address, so we simplify our matching
+        * logic below and can just reuse it as return pointer.
+        */
+       max_addr &= ~EFI_PAGE_MASK;
+
        list_for_each(lhandle, &efi_mem) {
                struct efi_mem_list *lmem = list_entry(lhandle,
                        struct efi_mem_list, link);
@@ -292,6 +349,9 @@ efi_status_t efi_allocate_pages(int type, int memory_type,
        efi_status_t r = EFI_SUCCESS;
        uint64_t addr;
 
+       if (!memory)
+               return EFI_INVALID_PARAMETER;
+
        switch (type) {
        case EFI_ALLOCATE_ANY_PAGES:
                /* Any page */
@@ -338,7 +398,7 @@ efi_status_t efi_allocate_pages(int type, int memory_type,
 void *efi_alloc(uint64_t len, int memory_type)
 {
        uint64_t ret = 0;
-       uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
+       uint64_t pages = efi_size_in_pages(len);
        efi_status_t r;
 
        r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
@@ -380,9 +440,12 @@ efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
 {
        efi_status_t r;
-       efi_physical_addr_t t;
-       u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
-                        EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
+       struct efi_pool_allocation *alloc;
+       u64 num_pages = efi_size_in_pages(size +
+                                         sizeof(struct efi_pool_allocation));
+
+       if (!buffer)
+               return EFI_INVALID_PARAMETER;
 
        if (size == 0) {
                *buffer = NULL;
@@ -390,10 +453,9 @@ efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size, void **buffer)
        }
 
        r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
-                              &t);
+                              (uint64_t *)&alloc);
 
        if (r == EFI_SUCCESS) {
-               struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
                alloc->num_pages = num_pages;
                *buffer = alloc->data;
        }
@@ -444,7 +506,12 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
        efi_uintn_t map_size = 0;
        int map_entries = 0;
        struct list_head *lhandle;
-       efi_uintn_t provided_map_size = *memory_map_size;
+       efi_uintn_t provided_map_size;
+
+       if (!memory_map_size)
+               return EFI_INVALID_PARAMETER;
+
+       provided_map_size = *memory_map_size;
 
        list_for_each(lhandle, &efi_mem)
                map_entries++;
@@ -456,6 +523,9 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
        if (provided_map_size < map_size)
                return EFI_BUFFER_TOO_SMALL;
 
+       if (!memory_map)
+               return EFI_INVALID_PARAMETER;
+
        if (descriptor_size)
                *descriptor_size = sizeof(struct efi_mem_desc);
 
@@ -463,36 +533,69 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
                *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
 
        /* Copy list into array */
-       if (memory_map) {
-               /* Return the list in ascending order */
-               memory_map = &memory_map[map_entries - 1];
-               list_for_each(lhandle, &efi_mem) {
-                       struct efi_mem_list *lmem;
+       /* Return the list in ascending order */
+       memory_map = &memory_map[map_entries - 1];
+       list_for_each(lhandle, &efi_mem) {
+               struct efi_mem_list *lmem;
 
-                       lmem = list_entry(lhandle, struct efi_mem_list, link);
-                       *memory_map = lmem->desc;
-                       memory_map--;
-               }
+               lmem = list_entry(lhandle, struct efi_mem_list, link);
+               *memory_map = lmem->desc;
+               memory_map--;
        }
 
-       *map_key = 0;
+       if (map_key)
+               *map_key = efi_memory_map_key;
 
        return EFI_SUCCESS;
 }
 
 __weak void efi_add_known_memory(void)
 {
+       u64 ram_top = board_get_usable_ram_top(0) & ~EFI_PAGE_MASK;
        int i;
 
+       /* Fix for 32bit targets with ram_top at 4G */
+       if (!ram_top)
+               ram_top = 0x100000000ULL;
+
        /* Add RAM */
        for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
-               u64 ram_start = gd->bd->bi_dram[i].start;
-               u64 ram_size = gd->bd->bi_dram[i].size;
-               u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
-               u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
+               u64 ram_end, ram_start, pages;
 
-               efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
-                                  false);
+               ram_start = (uintptr_t)map_sysmem(gd->bd->bi_dram[i].start, 0);
+               ram_end = ram_start + gd->bd->bi_dram[i].size;
+
+               /* Remove partial pages */
+               ram_end &= ~EFI_PAGE_MASK;
+               ram_start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
+
+               if (ram_end <= ram_start) {
+                       /* Invalid mapping, keep going. */
+                       continue;
+               }
+
+               pages = (ram_end - ram_start) >> EFI_PAGE_SHIFT;
+
+               efi_add_memory_map(ram_start, pages,
+                                  EFI_CONVENTIONAL_MEMORY, false);
+
+               /*
+                * Boards may indicate to the U-Boot memory core that they
+                * can not support memory above ram_top. Let's honor this
+                * in the efi_loader subsystem too by declaring any memory
+                * above ram_top as "already occupied by firmware".
+                */
+               if (ram_top < ram_start) {
+                       /* ram_top is before this region, reserve all */
+                       efi_add_memory_map(ram_start, pages,
+                                          EFI_BOOT_SERVICES_DATA, true);
+               } else if ((ram_top >= ram_start) && (ram_top < ram_end)) {
+                       /* ram_top is inside this region, reserve parts */
+                       pages = (ram_end - ram_top) >> EFI_PAGE_SHIFT;
+
+                       efi_add_memory_map(ram_top, pages,
+                                          EFI_BOOT_SERVICES_DATA, true);
+               }
        }
 }
 
@@ -500,6 +603,7 @@ __weak void efi_add_known_memory(void)
 static void add_u_boot_and_runtime(void)
 {
        unsigned long runtime_start, runtime_end, runtime_pages;
+       unsigned long runtime_mask = EFI_PAGE_MASK;
        unsigned long uboot_start, uboot_pages;
        unsigned long uboot_stack_size = 16 * 1024 * 1024;
 
@@ -508,10 +612,22 @@ static void add_u_boot_and_runtime(void)
        uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
        efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
 
-       /* Add Runtime Services */
-       runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
+#if defined(__aarch64__)
+       /*
+        * Runtime Services must be 64KiB aligned according to the
+        * "AArch64 Platforms" section in the UEFI spec (2.7+).
+        */
+
+       runtime_mask = SZ_64K - 1;
+#endif
+
+       /*
+        * Add Runtime Services. We mark surrounding boottime code as runtime as
+        * well to fulfill the runtime alignment constraints but avoid padding.
+        */
+       runtime_start = (ulong)&__efi_runtime_start & ~runtime_mask;
        runtime_end = (ulong)&__efi_runtime_stop;
-       runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
+       runtime_end = (runtime_end + runtime_mask) & ~runtime_mask;
        runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
        efi_add_memory_map(runtime_start, runtime_pages,
                           EFI_RUNTIME_SERVICES_CODE, false);