1 // SPDX-License-Identifier: GPL-2.0
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 * @install_cfg_tbl: whether or not to install the boot memory map as a
15 * Retrieve the UEFI memory map. The allocated memory leaves room for
16 * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
20 efi_status_t
efi_get_memory_map(struct efi_boot_memmap
**map
,
23 struct efi_boot_memmap tmp
, *m
__free(efi_pool
) = NULL
;
24 int memtype
= install_cfg_tbl
? EFI_ACPI_RECLAIM_MEMORY
26 efi_guid_t tbl_guid
= LINUX_EFI_BOOT_MEMMAP_GUID
;
31 status
= efi_bs_call(get_memory_map
, &tmp
.map_size
, NULL
, &tmp
.map_key
,
32 &tmp
.desc_size
, &tmp
.desc_ver
);
33 if (status
!= EFI_BUFFER_TOO_SMALL
)
34 return EFI_LOAD_ERROR
;
36 size
= tmp
.map_size
+ tmp
.desc_size
* EFI_MMAP_NR_SLACK_SLOTS
;
37 status
= efi_bs_call(allocate_pool
, memtype
, sizeof(*m
) + size
,
39 if (status
!= EFI_SUCCESS
)
42 if (install_cfg_tbl
) {
44 * Installing a configuration table might allocate memory, and
45 * this may modify the memory map. This means we should install
46 * the configuration table first, and re-install or delete it
49 status
= efi_bs_call(install_configuration_table
, &tbl_guid
, m
);
50 if (status
!= EFI_SUCCESS
)
54 m
->buff_size
= m
->map_size
= size
;
55 status
= efi_bs_call(get_memory_map
, &m
->map_size
, m
->map
, &m
->map_key
,
56 &m
->desc_size
, &m
->desc_ver
);
57 if (status
!= EFI_SUCCESS
) {
59 efi_bs_call(install_configuration_table
, &tbl_guid
, NULL
);
63 *map
= no_free_ptr(m
);
68 * efi_allocate_pages() - Allocate memory pages
69 * @size: minimum number of bytes to allocate
70 * @addr: On return the address of the first allocated page. The first
71 * allocated page has alignment EFI_ALLOC_ALIGN which is an
72 * architecture dependent multiple of the page size.
73 * @max: the address that the last allocated memory page shall not
76 * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
77 * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
82 efi_status_t
efi_allocate_pages(unsigned long size
, unsigned long *addr
,
85 efi_physical_addr_t alloc_addr
;
88 max
= min(max
, EFI_ALLOC_LIMIT
);
90 if (EFI_ALLOC_ALIGN
> EFI_PAGE_SIZE
)
91 return efi_allocate_pages_aligned(size
, addr
, max
,
95 alloc_addr
= ALIGN_DOWN(max
+ 1, EFI_ALLOC_ALIGN
) - 1;
96 status
= efi_bs_call(allocate_pages
, EFI_ALLOCATE_MAX_ADDRESS
,
97 EFI_LOADER_DATA
, DIV_ROUND_UP(size
, EFI_PAGE_SIZE
),
99 if (status
!= EFI_SUCCESS
)
107 * efi_free() - free memory pages
108 * @size: size of the memory area to free in bytes
109 * @addr: start of the memory area to free (must be EFI_PAGE_SIZE
112 * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
113 * architecture specific multiple of EFI_PAGE_SIZE. So this function should
114 * only be used to return pages allocated with efi_allocate_pages() or
115 * efi_low_alloc_above().
117 void efi_free(unsigned long size
, unsigned long addr
)
119 unsigned long nr_pages
;
124 nr_pages
= round_up(size
, EFI_ALLOC_ALIGN
) / EFI_PAGE_SIZE
;
125 efi_bs_call(free_pages
, addr
, nr_pages
);