xref: /openbmc/linux/drivers/firmware/efi/libstub/mem.c (revision eab31265)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/efi.h>
4 #include <asm/efi.h>
5 
6 #include "efistub.h"
7 
8 /**
9  * efi_get_memory_map() - get memory map
10  * @map:		pointer to memory map pointer to which to assign the
11  *			newly allocated memory map
12  *
13  * Retrieve the UEFI memory map. The allocated memory leaves room for
14  * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
15  *
16  * Return:	status code
17  */
18 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map)
19 {
20 	struct efi_boot_memmap *m, tmp;
21 	efi_status_t status;
22 	unsigned long size;
23 
24 	tmp.map_size = 0;
25 	status = efi_bs_call(get_memory_map, &tmp.map_size, NULL, &tmp.map_key,
26 			     &tmp.desc_size, &tmp.desc_ver);
27 	if (status != EFI_BUFFER_TOO_SMALL)
28 		return EFI_LOAD_ERROR;
29 
30 	size = tmp.map_size + tmp.desc_size * EFI_MMAP_NR_SLACK_SLOTS;
31 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*m) + size,
32 			     (void **)&m);
33 	if (status != EFI_SUCCESS)
34 		return status;
35 
36 	m->buff_size = m->map_size = size;
37 	status = efi_bs_call(get_memory_map, &m->map_size, m->map, &m->map_key,
38 			     &m->desc_size, &m->desc_ver);
39 	if (status != EFI_SUCCESS)
40 		goto free_map;
41 
42 	*map = m;
43 	return EFI_SUCCESS;
44 
45 free_map:
46 	efi_bs_call(free_pool, m);
47 	return status;
48 }
49 
50 /**
51  * efi_allocate_pages() - Allocate memory pages
52  * @size:	minimum number of bytes to allocate
53  * @addr:	On return the address of the first allocated page. The first
54  *		allocated page has alignment EFI_ALLOC_ALIGN which is an
55  *		architecture dependent multiple of the page size.
56  * @max:	the address that the last allocated memory page shall not
57  *		exceed
58  *
59  * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
60  * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
61  * given by @max.
62  *
63  * Return:	status code
64  */
65 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
66 				unsigned long max)
67 {
68 	efi_physical_addr_t alloc_addr;
69 	efi_status_t status;
70 
71 	if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
72 		return efi_allocate_pages_aligned(size, addr, max,
73 						  EFI_ALLOC_ALIGN);
74 
75 	alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
76 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
77 			     EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
78 			     &alloc_addr);
79 	if (status != EFI_SUCCESS)
80 		return status;
81 
82 	*addr = alloc_addr;
83 	return EFI_SUCCESS;
84 }
85 
86 /**
87  * efi_free() - free memory pages
88  * @size:	size of the memory area to free in bytes
89  * @addr:	start of the memory area to free (must be EFI_PAGE_SIZE
90  *		aligned)
91  *
92  * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
93  * architecture specific multiple of EFI_PAGE_SIZE. So this function should
94  * only be used to return pages allocated with efi_allocate_pages() or
95  * efi_low_alloc_above().
96  */
97 void efi_free(unsigned long size, unsigned long addr)
98 {
99 	unsigned long nr_pages;
100 
101 	if (!size)
102 		return;
103 
104 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
105 	efi_bs_call(free_pages, addr, nr_pages);
106 }
107