Loading...
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_allocate_pages_aligned() - Allocate memory pages
10 * @size: minimum number of bytes to allocate
11 * @addr: On return the address of the first allocated page. The first
12 * allocated page has alignment EFI_ALLOC_ALIGN which is an
13 * architecture dependent multiple of the page size.
14 * @max: the address that the last allocated memory page shall not
15 * exceed
16 * @align: minimum alignment of the base of the allocation
17 *
18 * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
19 * to @align, which should be >= EFI_ALLOC_ALIGN. The last allocated page will
20 * not exceed the address given by @max.
21 *
22 * Return: status code
23 */
24efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
25 unsigned long max, unsigned long align)
26{
27 efi_physical_addr_t alloc_addr;
28 efi_status_t status;
29 int slack;
30
31 if (align < EFI_ALLOC_ALIGN)
32 align = EFI_ALLOC_ALIGN;
33
34 alloc_addr = ALIGN_DOWN(max + 1, align) - 1;
35 size = round_up(size, EFI_ALLOC_ALIGN);
36 slack = align / EFI_PAGE_SIZE - 1;
37
38 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
39 EFI_LOADER_DATA, size / EFI_PAGE_SIZE + slack,
40 &alloc_addr);
41 if (status != EFI_SUCCESS)
42 return status;
43
44 *addr = ALIGN((unsigned long)alloc_addr, align);
45
46 if (slack > 0) {
47 int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
48
49 if (l) {
50 efi_bs_call(free_pages, alloc_addr, slack - l + 1);
51 slack = l - 1;
52 }
53 if (slack)
54 efi_bs_call(free_pages, *addr + size, slack);
55 }
56 return EFI_SUCCESS;
57}
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_allocate_pages_aligned() - Allocate memory pages
10 * @size: minimum number of bytes to allocate
11 * @addr: On return the address of the first allocated page. The first
12 * allocated page has alignment EFI_ALLOC_ALIGN which is an
13 * architecture dependent multiple of the page size.
14 * @max: the address that the last allocated memory page shall not
15 * exceed
16 * @align: minimum alignment of the base of the allocation
17 * @memory_type: the type of memory to allocate
18 *
19 * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
20 * to @align, which should be >= EFI_ALLOC_ALIGN. The last allocated page will
21 * not exceed the address given by @max.
22 *
23 * Return: status code
24 */
25efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
26 unsigned long max, unsigned long align,
27 int memory_type)
28{
29 efi_physical_addr_t alloc_addr;
30 efi_status_t status;
31 int slack;
32
33 max = min(max, EFI_ALLOC_LIMIT);
34
35 if (align < EFI_ALLOC_ALIGN)
36 align = EFI_ALLOC_ALIGN;
37
38 alloc_addr = ALIGN_DOWN(max + 1, align) - 1;
39 size = round_up(size, EFI_ALLOC_ALIGN);
40 slack = align / EFI_PAGE_SIZE - 1;
41
42 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
43 memory_type, size / EFI_PAGE_SIZE + slack,
44 &alloc_addr);
45 if (status != EFI_SUCCESS)
46 return status;
47
48 *addr = ALIGN((unsigned long)alloc_addr, align);
49
50 if (slack > 0) {
51 int l = (alloc_addr & (align - 1)) / EFI_PAGE_SIZE;
52
53 if (l) {
54 efi_bs_call(free_pages, alloc_addr, slack - l + 1);
55 slack = l - 1;
56 }
57 if (slack)
58 efi_bs_call(free_pages, *addr + size, slack);
59 }
60 return EFI_SUCCESS;
61}