Merge tag 'm68k-for-v6.4-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert...
[linux-block.git] / drivers / firmware / efi / libstub / randomalloc.c
CommitLineData
0ed02bda
AB
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
4 */
5
6#include <linux/efi.h>
7#include <linux/log2.h>
8#include <asm/efi.h>
9
10#include "efistub.h"
11
12/*
13 * Return the number of slots covered by this entry, i.e., the number of
14 * addresses it covers that are suitably aligned and supply enough room
15 * for the allocation.
16 */
17static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
18 unsigned long size,
19 unsigned long align_shift)
20{
21 unsigned long align = 1UL << align_shift;
22 u64 first_slot, last_slot, region_end;
23
24 if (md->type != EFI_CONVENTIONAL_MEMORY)
25 return 0;
26
27 if (efi_soft_reserve_enabled() &&
28 (md->attribute & EFI_MEMORY_SP))
29 return 0;
30
31 region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
a37dac5c 32 (u64)EFI_ALLOC_LIMIT);
4152433c
BH
33 if (region_end < size)
34 return 0;
0ed02bda
AB
35
36 first_slot = round_up(md->phys_addr, align);
37 last_slot = round_down(region_end - size + 1, align);
38
39 if (first_slot > last_slot)
40 return 0;
41
42 return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
43}
44
45/*
46 * The UEFI memory descriptors have a virtual address field that is only used
47 * when installing the virtual mapping using SetVirtualAddressMap(). Since it
48 * is unused here, we can reuse it to keep track of each descriptor's slot
49 * count.
50 */
51#define MD_NUM_SLOTS(md) ((md)->virt_addr)
52
53efi_status_t efi_random_alloc(unsigned long size,
54 unsigned long align,
55 unsigned long *addr,
9cf42bca
AB
56 unsigned long random_seed,
57 int memory_type)
0ed02bda 58{
eab31265 59 unsigned long total_slots = 0, target_slot;
a6cfe03c 60 unsigned long total_mirrored_slots = 0;
eab31265 61 struct efi_boot_memmap *map;
0ed02bda 62 efi_status_t status;
0ed02bda 63 int map_offset;
0ed02bda 64
171539f5 65 status = efi_get_memory_map(&map, false);
0ed02bda
AB
66 if (status != EFI_SUCCESS)
67 return status;
68
69 if (align < EFI_ALLOC_ALIGN)
70 align = EFI_ALLOC_ALIGN;
71
e1df73e2
AB
72 size = round_up(size, EFI_ALLOC_ALIGN);
73
0ed02bda 74 /* count the suitable slots in each memory map entry */
eab31265
AB
75 for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
76 efi_memory_desc_t *md = (void *)map->map + map_offset;
0ed02bda
AB
77 unsigned long slots;
78
79 slots = get_entry_num_slots(md, size, ilog2(align));
80 MD_NUM_SLOTS(md) = slots;
81 total_slots += slots;
a6cfe03c
AB
82 if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
83 total_mirrored_slots += slots;
0ed02bda
AB
84 }
85
a6cfe03c
AB
86 /* consider only mirrored slots for randomization if any exist */
87 if (total_mirrored_slots > 0)
88 total_slots = total_mirrored_slots;
89
0ed02bda 90 /* find a random number between 0 and total_slots */
c37c9162 91 target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
0ed02bda
AB
92
93 /*
94 * target_slot is now a value in the range [0, total_slots), and so
95 * it corresponds with exactly one of the suitable slots we recorded
96 * when iterating over the memory map the first time around.
97 *
98 * So iterate over the memory map again, subtracting the number of
99 * slots of each entry at each iteration, until we have found the entry
100 * that covers our chosen slot. Use the residual value of target_slot
101 * to calculate the randomly chosen address, and allocate it directly
102 * using EFI_ALLOCATE_ADDRESS.
103 */
0b1d9deb 104 status = EFI_OUT_OF_RESOURCES;
eab31265
AB
105 for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
106 efi_memory_desc_t *md = (void *)map->map + map_offset;
0ed02bda
AB
107 efi_physical_addr_t target;
108 unsigned long pages;
109
a6cfe03c
AB
110 if (total_mirrored_slots > 0 &&
111 !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
112 continue;
113
0ed02bda
AB
114 if (target_slot >= MD_NUM_SLOTS(md)) {
115 target_slot -= MD_NUM_SLOTS(md);
116 continue;
117 }
118
119 target = round_up(md->phys_addr, align) + target_slot * align;
e1df73e2 120 pages = size / EFI_PAGE_SIZE;
0ed02bda
AB
121
122 status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
9cf42bca 123 memory_type, pages, &target);
0ed02bda
AB
124 if (status == EFI_SUCCESS)
125 *addr = target;
126 break;
127 }
128
eab31265 129 efi_bs_call(free_pool, map);
0ed02bda
AB
130
131 return status;
132}