]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - lib/efi_loader/efi_memory.c
Merge git://git.denx.de/u-boot-samsung
[people/ms/u-boot.git] / lib / efi_loader / efi_memory.c
index 1d23783020213bf3d3bde7ad05483ae7fe1ec625..0aa3e0881d43ec1d451897ab76ffdbd673d49f82 100644 (file)
@@ -33,6 +33,19 @@ LIST_HEAD(efi_mem);
 void *efi_bounce_buffer;
 #endif
 
+/*
+ * U-Boot services each EFI AllocatePool request as a separate
+ * (multiple) page allocation.  We have to track the number of pages
+ * to be able to free the correct amount later.
+ * EFI requires 8 byte alignment for pool allocations, so we can
+ * prepend each allocation with an 64 bit header tracking the
+ * allocation size, and hand out the remainder to the caller.
+ */
+struct efi_pool_allocation {
+       u64 num_pages;
+       char data[] __aligned(ARCH_DMA_MINALIGN);
+};
+
 /*
  * Sorts the memory list from highest address to lowest address
  *
@@ -103,10 +116,13 @@ static int efi_mem_carve_out(struct efi_mem_list *map,
                if (map_end == carve_end) {
                        /* Full overlap, just remove map */
                        list_del(&map->link);
+                       free(map);
+               } else {
+                       map->desc.physical_start = carve_end;
+                       map->desc.num_pages = (map_end - carve_end)
+                                             >> EFI_PAGE_SHIFT;
                }
 
-               map_desc->physical_start = carve_end;
-               map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
                return (carve_end - carve_start) >> EFI_PAGE_SHIFT;
        }
 
@@ -122,7 +138,8 @@ static int efi_mem_carve_out(struct efi_mem_list *map,
        newmap->desc = map->desc;
        newmap->desc.physical_start = carve_start;
        newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
-        list_add_tail(&newmap->link, &efi_mem);
+       /* Insert before current entry (descending address order) */
+       list_add_tail(&newmap->link, &map->link);
 
        /* Shrink the map to [ map_start ... carve_start ] */
        map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
@@ -259,7 +276,7 @@ static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
 }
 
 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;
@@ -321,22 +338,70 @@ void *efi_alloc(uint64_t len, int memory_type)
        return NULL;
 }
 
-efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
+efi_status_t efi_free_pages(uint64_t memory, efi_uintn_t pages)
 {
-       /* We don't free, let's cross our fingers we have plenty RAM */
-       return EFI_SUCCESS;
+       uint64_t r = 0;
+
+       r = efi_add_memory_map(memory, pages, EFI_CONVENTIONAL_MEMORY, false);
+       /* Merging of adjacent free regions is missing */
+
+       if (r == memory)
+               return EFI_SUCCESS;
+
+       return EFI_NOT_FOUND;
 }
 
-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)
+efi_status_t efi_allocate_pool(int pool_type, efi_uintn_t size,
+                              void **buffer)
 {
-       ulong map_size = 0;
+       efi_status_t r;
+       efi_physical_addr_t t;
+       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);
+
+       if (r == EFI_SUCCESS) {
+               struct efi_pool_allocation *alloc = (void *)(uintptr_t)t;
+               alloc->num_pages = num_pages;
+               *buffer = alloc->data;
+       }
+
+       return r;
+}
+
+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);
+
+       r = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
+
+       return r;
+}
+
+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)
+{
+       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++;
@@ -345,15 +410,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 */
@@ -367,14 +432,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 */
@@ -387,6 +451,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;