]> git.sur5r.net Git - u-boot/blobdiff - lib/efi_loader/efi_memory.c
Merge tag 'signed-efi-next' of git://github.com/agraf/u-boot
[u-boot] / lib / efi_loader / efi_memory.c
index 95aa590c8af7a4d53697091b4292e2bac59a1f9e..ec66af98ea8f765e9be2dde26682f4c5c9379ad6 100644 (file)
@@ -1,19 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  *  EFI application memory management
  *
  *  Copyright (c) 2016 Alexander Graf
- *
- *  SPDX-License-Identifier:     GPL-2.0+
  */
 
 #include <common.h>
 #include <efi_loader.h>
-#include <malloc.h>
-#include <asm/global_data.h>
-#include <libfdt_env.h>
-#include <linux/list_sort.h>
 #include <inttypes.h>
+#include <malloc.h>
 #include <watchdog.h>
+#include <linux/list_sort.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -43,7 +40,7 @@ void *efi_bounce_buffer;
  */
 struct efi_pool_allocation {
        u64 num_pages;
-       char data[];
+       char data[] __aligned(ARCH_DMA_MINALIGN);
 };
 
 /*
@@ -71,23 +68,27 @@ static void efi_mem_sort(void)
        list_sort(NULL, &efi_mem, efi_mem_cmp);
 }
 
-/*
- * Unmaps all memory occupied by the carve_desc region from the
- * list entry pointed to by map.
+/** efi_mem_carve_out - unmap memory region
  *
- * Returns EFI_CARVE_NO_OVERLAP if the regions don't overlap.
- * Returns EFI_CARVE_OVERLAPS_NONRAM if the carve and map overlap,
- *    and the map contains anything but free ram.
- *    (only when overlap_only_ram is true)
- * Returns EFI_CARVE_LOOP_AGAIN if the mapping list should be traversed
- *    again, as it has been altered
- * Returns the number of overlapping pages. The pages are removed from
- *     the mapping list.
+ * @map:               memory map
+ * @carve_desc:                memory region to unmap
+ * @overlap_only_ram:  the carved out region may only overlap RAM
+ * Return Value:       the number of overlapping pages which have been
+ *                     removed from the map,
+ *                     EFI_CARVE_NO_OVERLAP, if the regions don't overlap,
+ *                     EFI_CARVE_OVERLAPS_NONRAM, if the carve and map overlap,
+ *                     and the map contains anything but free ram
+ *                     (only when overlap_only_ram is true),
+ *                     EFI_CARVE_LOOP_AGAIN, if the mapping list should be
+ *                     traversed again, as it has been altered.
+ *
+ * Unmaps all memory occupied by the carve_desc region from the list entry
+ * pointed to by map.
  *
  * In case of EFI_CARVE_OVERLAPS_NONRAM it is the callers responsibility
- * to readd the already carved out pages to the mapping.
+ * to re-add the already carved out pages to the mapping.
  */
-static int efi_mem_carve_out(struct efi_mem_list *map,
+static s64 efi_mem_carve_out(struct efi_mem_list *map,
                             struct efi_mem_desc *carve_desc,
                             bool overlap_only_ram)
 {
@@ -186,7 +187,7 @@ uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
                carve_again = false;
                list_for_each(lhandle, &efi_mem) {
                        struct efi_mem_list *lmem;
-                       int r;
+                       s64 r;
 
                        lmem = list_entry(lhandle, struct efi_mem_list, link);
                        r = efi_mem_carve_out(lmem, &newlist->desc,
@@ -275,15 +276,24 @@ static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
        return 0;
 }
 
+/*
+ * Allocate memory pages.
+ *
+ * @type               type of allocation to be performed
+ * @memory_type                usage type of the allocated memory
+ * @pages              number of pages to be allocated
+ * @memory             allocated memory
+ * @return             status code
+ */
 efi_status_t efi_allocate_pages(int type, int memory_type,
-                               unsigned long pages, uint64_t *memory)
+                               efi_uintn_t pages, uint64_t *memory)
 {
        u64 len = pages << EFI_PAGE_SHIFT;
        efi_status_t r = EFI_SUCCESS;
        uint64_t addr;
 
        switch (type) {
-       case 0:
+       case EFI_ALLOCATE_ANY_PAGES:
                /* Any page */
                addr = efi_find_free_memory(len, gd->start_addr_sp);
                if (!addr) {
@@ -291,7 +301,7 @@ efi_status_t efi_allocate_pages(int type, int memory_type,
                        break;
                }
                break;
-       case 1:
+       case EFI_ALLOCATE_MAX_ADDRESS:
                /* Max address */
                addr = efi_find_free_memory(len, *memory);
                if (!addr) {
@@ -299,7 +309,7 @@ efi_status_t efi_allocate_pages(int type, int memory_type,
                        break;
                }
                break;
-       case 2:
+       case EFI_ALLOCATE_ADDRESS:
                /* Exact address, reserve it. The addr is already in *memory. */
                addr = *memory;
                break;
@@ -331,14 +341,22 @@ void *efi_alloc(uint64_t len, int memory_type)
        uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
        efi_status_t r;
 
-       r = efi_allocate_pages(0, memory_type, pages, &ret);
+       r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, memory_type, pages,
+                              &ret);
        if (r == EFI_SUCCESS)
                return (void*)(uintptr_t)ret;
 
        return NULL;
 }
 
-efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
+/*
+ * Free memory pages.
+ *
+ * @memory     start of the memory area to be freed
+ * @pages      number of pages to be freed
+ * @return     status code
+ */
+efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 {
        uint64_t r = 0;
 
@@ -351,19 +369,28 @@ efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
        return EFI_NOT_FOUND;
 }
 
-efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
-                              void **buffer)
+/*
+ * Allocate memory from pool.
+ *
+ * @pool_type  type of the pool from which memory is to be allocated
+ * @size       number of bytes to be allocated
+ * @buffer     allocated memory
+ * @return     status code
+ */
+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(u64) + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
+       u64 num_pages = (size + sizeof(struct efi_pool_allocation) +
+                        EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
 
        if (size == 0) {
                *buffer = NULL;
                return EFI_SUCCESS;
        }
 
-       r = efi_allocate_pages(0, pool_type, num_pages, &t);
+       r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
+                              &t);
 
        if (r == EFI_SUCCESS) {
                struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
@@ -374,11 +401,20 @@ efi_status_t efi_allocate_pool(int pool_type, unsigned long size,
        return r;
 }
 
+/*
+ * Free memory from pool.
+ *
+ * @buffer     start of memory to be freed
+ * @return     status code
+ */
 efi_status_t efi_free_pool(void *buffer)
 {
        efi_status_t r;
        struct efi_pool_allocation *alloc;
 
+       if (buffer == NULL)
+               return EFI_INVALID_PARAMETER;
+
        alloc = container_of(buffer, struct efi_pool_allocation, data);
        /* Sanity check, was the supplied address returned by allocate_pool */
        assert(((uintptr_t)alloc & EFI_PAGE_MASK) == 0);
@@ -388,16 +424,27 @@ efi_status_t efi_free_pool(void *buffer)
        return r;
 }
 
-efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
-                              struct efi_mem_desc *memory_map,
-                              unsigned long *map_key,
-                              unsigned long *descriptor_size,
-                              uint32_t *descriptor_version)
+/*
+ * Get map describing memory usage.
+ *
+ * @memory_map_size    on entry the size, in bytes, of the memory map buffer,
+ *                     on exit the size of the copied memory map
+ * @memory_map         buffer to which the memory map is written
+ * @map_key            key for the memory map
+ * @descriptor_size    size of an individual memory descriptor
+ * @descriptor_version version number of the memory descriptor structure
+ * @return             status code
+ */
+efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
+                               struct efi_mem_desc *memory_map,
+                               efi_uintn_t *map_key,
+                               efi_uintn_t *descriptor_size,
+                               uint32_t *descriptor_version)
 {
-       ulong map_size = 0;
+       efi_uintn_t map_size = 0;
        int map_entries = 0;
        struct list_head *lhandle;
-       unsigned long provided_map_size = *memory_map_size;
+       efi_uintn_t provided_map_size = *memory_map_size;
 
        list_for_each(lhandle, &efi_mem)
                map_entries++;
@@ -406,15 +453,15 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
 
        *memory_map_size = map_size;
 
+       if (provided_map_size < map_size)
+               return EFI_BUFFER_TOO_SMALL;
+
        if (descriptor_size)
                *descriptor_size = sizeof(struct efi_mem_desc);
 
        if (descriptor_version)
                *descriptor_version = EFI_MEMORY_DESCRIPTOR_VERSION;
 
-       if (provided_map_size < map_size)
-               return EFI_BUFFER_TOO_SMALL;
-
        /* Copy list into array */
        if (memory_map) {
                /* Return the list in ascending order */
@@ -428,14 +475,13 @@ efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
                }
        }
 
+       *map_key = 0;
+
        return EFI_SUCCESS;
 }
 
-int efi_memory_init(void)
+__weak void efi_add_known_memory(void)
 {
-       unsigned long runtime_start, runtime_end, runtime_pages;
-       unsigned long uboot_start, uboot_pages;
-       unsigned long uboot_stack_size = 16 * 1024 * 1024;
        int i;
 
        /* Add RAM */
@@ -448,6 +494,15 @@ int efi_memory_init(void)
                efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
                                   false);
        }
+}
+
+int efi_memory_init(void)
+{
+       unsigned long runtime_start, runtime_end, runtime_pages;
+       unsigned long uboot_start, uboot_pages;
+       unsigned long uboot_stack_size = 16 * 1024 * 1024;
+
+       efi_add_known_memory();
 
        /* Add U-Boot */
        uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
@@ -466,7 +521,7 @@ int efi_memory_init(void)
        /* Request a 32bit 64MB bounce buffer region */
        uint64_t efi_bounce_buffer_addr = 0xffffffff;
 
-       if (efi_allocate_pages(1, EFI_LOADER_DATA,
+       if (efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
                               (64 * 1024 * 1024) >> EFI_PAGE_SHIFT,
                               &efi_bounce_buffer_addr) != EFI_SUCCESS)
                return -1;