10ed02bdaSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0
20ed02bdaSArd Biesheuvel /*
30ed02bdaSArd Biesheuvel * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
40ed02bdaSArd Biesheuvel */
50ed02bdaSArd Biesheuvel
60ed02bdaSArd Biesheuvel #include <linux/efi.h>
70ed02bdaSArd Biesheuvel #include <linux/log2.h>
80ed02bdaSArd Biesheuvel #include <asm/efi.h>
90ed02bdaSArd Biesheuvel
100ed02bdaSArd Biesheuvel #include "efistub.h"
110ed02bdaSArd Biesheuvel
120ed02bdaSArd Biesheuvel /*
130ed02bdaSArd Biesheuvel * Return the number of slots covered by this entry, i.e., the number of
140ed02bdaSArd Biesheuvel * addresses it covers that are suitably aligned and supply enough room
150ed02bdaSArd Biesheuvel * for the allocation.
160ed02bdaSArd Biesheuvel */
get_entry_num_slots(efi_memory_desc_t * md,unsigned long size,unsigned long align_shift,u64 alloc_min,u64 alloc_max)170ed02bdaSArd Biesheuvel static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
180ed02bdaSArd Biesheuvel unsigned long size,
19bc5ddcefSArd Biesheuvel unsigned long align_shift,
200e7ca435SArd Biesheuvel u64 alloc_min, u64 alloc_max)
210ed02bdaSArd Biesheuvel {
220ed02bdaSArd Biesheuvel unsigned long align = 1UL << align_shift;
230ed02bdaSArd Biesheuvel u64 first_slot, last_slot, region_end;
240ed02bdaSArd Biesheuvel
250ed02bdaSArd Biesheuvel if (md->type != EFI_CONVENTIONAL_MEMORY)
260ed02bdaSArd Biesheuvel return 0;
270ed02bdaSArd Biesheuvel
280ed02bdaSArd Biesheuvel if (efi_soft_reserve_enabled() &&
290ed02bdaSArd Biesheuvel (md->attribute & EFI_MEMORY_SP))
300ed02bdaSArd Biesheuvel return 0;
310ed02bdaSArd Biesheuvel
320ed02bdaSArd Biesheuvel region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
330e7ca435SArd Biesheuvel alloc_max);
344152433cSBenjamin Herrenschmidt if (region_end < size)
354152433cSBenjamin Herrenschmidt return 0;
360ed02bdaSArd Biesheuvel
370e7ca435SArd Biesheuvel first_slot = round_up(max(md->phys_addr, alloc_min), align);
380ed02bdaSArd Biesheuvel last_slot = round_down(region_end - size + 1, align);
390ed02bdaSArd Biesheuvel
400ed02bdaSArd Biesheuvel if (first_slot > last_slot)
410ed02bdaSArd Biesheuvel return 0;
420ed02bdaSArd Biesheuvel
430ed02bdaSArd Biesheuvel return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
440ed02bdaSArd Biesheuvel }
450ed02bdaSArd Biesheuvel
460ed02bdaSArd Biesheuvel /*
470ed02bdaSArd Biesheuvel * The UEFI memory descriptors have a virtual address field that is only used
480ed02bdaSArd Biesheuvel * when installing the virtual mapping using SetVirtualAddressMap(). Since it
490ed02bdaSArd Biesheuvel * is unused here, we can reuse it to keep track of each descriptor's slot
500ed02bdaSArd Biesheuvel * count.
510ed02bdaSArd Biesheuvel */
520ed02bdaSArd Biesheuvel #define MD_NUM_SLOTS(md) ((md)->virt_addr)
530ed02bdaSArd Biesheuvel
efi_random_alloc(unsigned long size,unsigned long align,unsigned long * addr,unsigned long random_seed,int memory_type,unsigned long alloc_min,unsigned long alloc_max)540ed02bdaSArd Biesheuvel efi_status_t efi_random_alloc(unsigned long size,
550ed02bdaSArd Biesheuvel unsigned long align,
560ed02bdaSArd Biesheuvel unsigned long *addr,
579cf42bcaSArd Biesheuvel unsigned long random_seed,
58bc5ddcefSArd Biesheuvel int memory_type,
590e7ca435SArd Biesheuvel unsigned long alloc_min,
600e7ca435SArd Biesheuvel unsigned long alloc_max)
610ed02bdaSArd Biesheuvel {
62eab31265SArd Biesheuvel unsigned long total_slots = 0, target_slot;
63a6cfe03cSArd Biesheuvel unsigned long total_mirrored_slots = 0;
64eab31265SArd Biesheuvel struct efi_boot_memmap *map;
650ed02bdaSArd Biesheuvel efi_status_t status;
660ed02bdaSArd Biesheuvel int map_offset;
670ed02bdaSArd Biesheuvel
68171539f5SArd Biesheuvel status = efi_get_memory_map(&map, false);
690ed02bdaSArd Biesheuvel if (status != EFI_SUCCESS)
700ed02bdaSArd Biesheuvel return status;
710ed02bdaSArd Biesheuvel
720ed02bdaSArd Biesheuvel if (align < EFI_ALLOC_ALIGN)
730ed02bdaSArd Biesheuvel align = EFI_ALLOC_ALIGN;
740ed02bdaSArd Biesheuvel
75e1df73e2SArd Biesheuvel size = round_up(size, EFI_ALLOC_ALIGN);
76e1df73e2SArd Biesheuvel
770ed02bdaSArd Biesheuvel /* count the suitable slots in each memory map entry */
78eab31265SArd Biesheuvel for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
79eab31265SArd Biesheuvel efi_memory_desc_t *md = (void *)map->map + map_offset;
800ed02bdaSArd Biesheuvel unsigned long slots;
810ed02bdaSArd Biesheuvel
820e7ca435SArd Biesheuvel slots = get_entry_num_slots(md, size, ilog2(align), alloc_min,
830e7ca435SArd Biesheuvel alloc_max);
840ed02bdaSArd Biesheuvel MD_NUM_SLOTS(md) = slots;
850ed02bdaSArd Biesheuvel total_slots += slots;
86a6cfe03cSArd Biesheuvel if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
87a6cfe03cSArd Biesheuvel total_mirrored_slots += slots;
880ed02bdaSArd Biesheuvel }
890ed02bdaSArd Biesheuvel
90a6cfe03cSArd Biesheuvel /* consider only mirrored slots for randomization if any exist */
91a6cfe03cSArd Biesheuvel if (total_mirrored_slots > 0)
92a6cfe03cSArd Biesheuvel total_slots = total_mirrored_slots;
93a6cfe03cSArd Biesheuvel
940ed02bdaSArd Biesheuvel /* find a random number between 0 and total_slots */
95c37c9162SArd Biesheuvel target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
960ed02bdaSArd Biesheuvel
970ed02bdaSArd Biesheuvel /*
980ed02bdaSArd Biesheuvel * target_slot is now a value in the range [0, total_slots), and so
990ed02bdaSArd Biesheuvel * it corresponds with exactly one of the suitable slots we recorded
1000ed02bdaSArd Biesheuvel * when iterating over the memory map the first time around.
1010ed02bdaSArd Biesheuvel *
1020ed02bdaSArd Biesheuvel * So iterate over the memory map again, subtracting the number of
1030ed02bdaSArd Biesheuvel * slots of each entry at each iteration, until we have found the entry
1040ed02bdaSArd Biesheuvel * that covers our chosen slot. Use the residual value of target_slot
1050ed02bdaSArd Biesheuvel * to calculate the randomly chosen address, and allocate it directly
1060ed02bdaSArd Biesheuvel * using EFI_ALLOCATE_ADDRESS.
1070ed02bdaSArd Biesheuvel */
1080b1d9debSArd Biesheuvel status = EFI_OUT_OF_RESOURCES;
109eab31265SArd Biesheuvel for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
110eab31265SArd Biesheuvel efi_memory_desc_t *md = (void *)map->map + map_offset;
1110ed02bdaSArd Biesheuvel efi_physical_addr_t target;
1120ed02bdaSArd Biesheuvel unsigned long pages;
1130ed02bdaSArd Biesheuvel
114a6cfe03cSArd Biesheuvel if (total_mirrored_slots > 0 &&
115a6cfe03cSArd Biesheuvel !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
116a6cfe03cSArd Biesheuvel continue;
117a6cfe03cSArd Biesheuvel
1180ed02bdaSArd Biesheuvel if (target_slot >= MD_NUM_SLOTS(md)) {
1190ed02bdaSArd Biesheuvel target_slot -= MD_NUM_SLOTS(md);
1200ed02bdaSArd Biesheuvel continue;
1210ed02bdaSArd Biesheuvel }
1220ed02bdaSArd Biesheuvel
12390048007SArd Biesheuvel target = round_up(max_t(u64, md->phys_addr, alloc_min), align) + target_slot * align;
124e1df73e2SArd Biesheuvel pages = size / EFI_PAGE_SIZE;
1250ed02bdaSArd Biesheuvel
1260ed02bdaSArd Biesheuvel status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
1279cf42bcaSArd Biesheuvel memory_type, pages, &target);
1280ed02bdaSArd Biesheuvel if (status == EFI_SUCCESS)
1290ed02bdaSArd Biesheuvel *addr = target;
1300ed02bdaSArd Biesheuvel break;
1310ed02bdaSArd Biesheuvel }
1320ed02bdaSArd Biesheuvel
133eab31265SArd Biesheuvel efi_bs_call(free_pool, map);
1340ed02bdaSArd Biesheuvel
1350ed02bdaSArd Biesheuvel return status;
1360ed02bdaSArd Biesheuvel }
137