xref: /openbmc/linux/drivers/firmware/efi/libstub/mem.c (revision a37dac5c)
1f57db62cSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
2f57db62cSArd Biesheuvel 
3f57db62cSArd Biesheuvel #include <linux/efi.h>
4f57db62cSArd Biesheuvel #include <asm/efi.h>
5f57db62cSArd Biesheuvel 
6f57db62cSArd Biesheuvel #include "efistub.h"
7f57db62cSArd Biesheuvel 
81d9b1768SHeinrich Schuchardt /**
91d9b1768SHeinrich Schuchardt  * efi_get_memory_map() - get memory map
10eab31265SArd Biesheuvel  * @map:		pointer to memory map pointer to which to assign the
11eab31265SArd Biesheuvel  *			newly allocated memory map
12171539f5SArd Biesheuvel  * @install_cfg_tbl:	whether or not to install the boot memory map as a
13171539f5SArd Biesheuvel  *			configuration table
141d9b1768SHeinrich Schuchardt  *
151d9b1768SHeinrich Schuchardt  * Retrieve the UEFI memory map. The allocated memory leaves room for
161d9b1768SHeinrich Schuchardt  * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
171d9b1768SHeinrich Schuchardt  *
181d9b1768SHeinrich Schuchardt  * Return:	status code
191d9b1768SHeinrich Schuchardt  */
efi_get_memory_map(struct efi_boot_memmap ** map,bool install_cfg_tbl)20171539f5SArd Biesheuvel efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
21171539f5SArd Biesheuvel 				bool install_cfg_tbl)
22f57db62cSArd Biesheuvel {
23171539f5SArd Biesheuvel 	int memtype = install_cfg_tbl ? EFI_ACPI_RECLAIM_MEMORY
24171539f5SArd Biesheuvel 				      : EFI_LOADER_DATA;
25171539f5SArd Biesheuvel 	efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
26eab31265SArd Biesheuvel 	struct efi_boot_memmap *m, tmp;
27f57db62cSArd Biesheuvel 	efi_status_t status;
28eab31265SArd Biesheuvel 	unsigned long size;
29f57db62cSArd Biesheuvel 
30eab31265SArd Biesheuvel 	tmp.map_size = 0;
31eab31265SArd Biesheuvel 	status = efi_bs_call(get_memory_map, &tmp.map_size, NULL, &tmp.map_key,
32eab31265SArd Biesheuvel 			     &tmp.desc_size, &tmp.desc_ver);
33eab31265SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
34eab31265SArd Biesheuvel 		return EFI_LOAD_ERROR;
35eab31265SArd Biesheuvel 
36eab31265SArd Biesheuvel 	size = tmp.map_size + tmp.desc_size * EFI_MMAP_NR_SLACK_SLOTS;
37171539f5SArd Biesheuvel 	status = efi_bs_call(allocate_pool, memtype, sizeof(*m) + size,
38eab31265SArd Biesheuvel 			     (void **)&m);
39f57db62cSArd Biesheuvel 	if (status != EFI_SUCCESS)
40eab31265SArd Biesheuvel 		return status;
41f57db62cSArd Biesheuvel 
42171539f5SArd Biesheuvel 	if (install_cfg_tbl) {
43171539f5SArd Biesheuvel 		/*
44171539f5SArd Biesheuvel 		 * Installing a configuration table might allocate memory, and
45171539f5SArd Biesheuvel 		 * this may modify the memory map. This means we should install
46171539f5SArd Biesheuvel 		 * the configuration table first, and re-install or delete it
47171539f5SArd Biesheuvel 		 * as needed.
48171539f5SArd Biesheuvel 		 */
49171539f5SArd Biesheuvel 		status = efi_bs_call(install_configuration_table, &tbl_guid, m);
50171539f5SArd Biesheuvel 		if (status != EFI_SUCCESS)
51171539f5SArd Biesheuvel 			goto free_map;
52171539f5SArd Biesheuvel 	}
53171539f5SArd Biesheuvel 
54eab31265SArd Biesheuvel 	m->buff_size = m->map_size = size;
55eab31265SArd Biesheuvel 	status = efi_bs_call(get_memory_map, &m->map_size, m->map, &m->map_key,
56eab31265SArd Biesheuvel 			     &m->desc_size, &m->desc_ver);
57eab31265SArd Biesheuvel 	if (status != EFI_SUCCESS)
58171539f5SArd Biesheuvel 		goto uninstall_table;
59eab31265SArd Biesheuvel 
60eab31265SArd Biesheuvel 	*map = m;
61eab31265SArd Biesheuvel 	return EFI_SUCCESS;
62eab31265SArd Biesheuvel 
63171539f5SArd Biesheuvel uninstall_table:
64171539f5SArd Biesheuvel 	if (install_cfg_tbl)
65171539f5SArd Biesheuvel 		efi_bs_call(install_configuration_table, &tbl_guid, NULL);
66eab31265SArd Biesheuvel free_map:
67f57db62cSArd Biesheuvel 	efi_bs_call(free_pool, m);
68f57db62cSArd Biesheuvel 	return status;
69f57db62cSArd Biesheuvel }
70f57db62cSArd Biesheuvel 
71eaa6fc67SHeinrich Schuchardt /**
72eaa6fc67SHeinrich Schuchardt  * efi_allocate_pages() - Allocate memory pages
73eaa6fc67SHeinrich Schuchardt  * @size:	minimum number of bytes to allocate
74eaa6fc67SHeinrich Schuchardt  * @addr:	On return the address of the first allocated page. The first
75eaa6fc67SHeinrich Schuchardt  *		allocated page has alignment EFI_ALLOC_ALIGN which is an
76eaa6fc67SHeinrich Schuchardt  *		architecture dependent multiple of the page size.
77eaa6fc67SHeinrich Schuchardt  * @max:	the address that the last allocated memory page shall not
78eaa6fc67SHeinrich Schuchardt  *		exceed
79eaa6fc67SHeinrich Schuchardt  *
80eaa6fc67SHeinrich Schuchardt  * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
81eaa6fc67SHeinrich Schuchardt  * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
82eaa6fc67SHeinrich Schuchardt  * given by @max.
83eaa6fc67SHeinrich Schuchardt  *
84eaa6fc67SHeinrich Schuchardt  * Return:	status code
85f57db62cSArd Biesheuvel  */
efi_allocate_pages(unsigned long size,unsigned long * addr,unsigned long max)86a7495c28SArd Biesheuvel efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
87a7495c28SArd Biesheuvel 				unsigned long max)
88f57db62cSArd Biesheuvel {
8943b1df0eSArd Biesheuvel 	efi_physical_addr_t alloc_addr;
90f57db62cSArd Biesheuvel 	efi_status_t status;
91f57db62cSArd Biesheuvel 
92*a37dac5cSArd Biesheuvel 	max = min(max, EFI_ALLOC_LIMIT);
93*a37dac5cSArd Biesheuvel 
9443b1df0eSArd Biesheuvel 	if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
9543b1df0eSArd Biesheuvel 		return efi_allocate_pages_aligned(size, addr, max,
969cf42bcaSArd Biesheuvel 						  EFI_ALLOC_ALIGN,
979cf42bcaSArd Biesheuvel 						  EFI_LOADER_DATA);
9843b1df0eSArd Biesheuvel 
9943b1df0eSArd Biesheuvel 	alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
100a7495c28SArd Biesheuvel 	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
10143b1df0eSArd Biesheuvel 			     EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
102a7495c28SArd Biesheuvel 			     &alloc_addr);
103a7495c28SArd Biesheuvel 	if (status != EFI_SUCCESS)
104f57db62cSArd Biesheuvel 		return status;
105f57db62cSArd Biesheuvel 
10643b1df0eSArd Biesheuvel 	*addr = alloc_addr;
107a7495c28SArd Biesheuvel 	return EFI_SUCCESS;
108a7495c28SArd Biesheuvel }
10943b1df0eSArd Biesheuvel 
1101d9b1768SHeinrich Schuchardt /**
1111d9b1768SHeinrich Schuchardt  * efi_free() - free memory pages
1121d9b1768SHeinrich Schuchardt  * @size:	size of the memory area to free in bytes
1131d9b1768SHeinrich Schuchardt  * @addr:	start of the memory area to free (must be EFI_PAGE_SIZE
1141d9b1768SHeinrich Schuchardt  *		aligned)
1151d9b1768SHeinrich Schuchardt  *
1161d9b1768SHeinrich Schuchardt  * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
1171d9b1768SHeinrich Schuchardt  * architecture specific multiple of EFI_PAGE_SIZE. So this function should
1181d9b1768SHeinrich Schuchardt  * only be used to return pages allocated with efi_allocate_pages() or
1191d9b1768SHeinrich Schuchardt  * efi_low_alloc_above().
1201d9b1768SHeinrich Schuchardt  */
efi_free(unsigned long size,unsigned long addr)121f57db62cSArd Biesheuvel void efi_free(unsigned long size, unsigned long addr)
122f57db62cSArd Biesheuvel {
123f57db62cSArd Biesheuvel 	unsigned long nr_pages;
124f57db62cSArd Biesheuvel 
125f57db62cSArd Biesheuvel 	if (!size)
126f57db62cSArd Biesheuvel 		return;
127f57db62cSArd Biesheuvel 
128f57db62cSArd Biesheuvel 	nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
129f57db62cSArd Biesheuvel 	efi_bs_call(free_pages, addr, nr_pages);
130f57db62cSArd Biesheuvel }
131