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  */
170ed02bdaSArd Biesheuvel static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
180ed02bdaSArd Biesheuvel 					 unsigned long size,
190ed02bdaSArd Biesheuvel 					 unsigned long align_shift)
200ed02bdaSArd Biesheuvel {
210ed02bdaSArd Biesheuvel 	unsigned long align = 1UL << align_shift;
220ed02bdaSArd Biesheuvel 	u64 first_slot, last_slot, region_end;
230ed02bdaSArd Biesheuvel 
240ed02bdaSArd Biesheuvel 	if (md->type != EFI_CONVENTIONAL_MEMORY)
250ed02bdaSArd Biesheuvel 		return 0;
260ed02bdaSArd Biesheuvel 
270ed02bdaSArd Biesheuvel 	if (efi_soft_reserve_enabled() &&
280ed02bdaSArd Biesheuvel 	    (md->attribute & EFI_MEMORY_SP))
290ed02bdaSArd Biesheuvel 		return 0;
300ed02bdaSArd Biesheuvel 
310ed02bdaSArd Biesheuvel 	region_end = min(md->phys_addr + md->num_pages * EFI_PAGE_SIZE - 1,
32a37dac5cSArd Biesheuvel 			 (u64)EFI_ALLOC_LIMIT);
334152433cSBenjamin Herrenschmidt 	if (region_end < size)
344152433cSBenjamin Herrenschmidt 		return 0;
350ed02bdaSArd Biesheuvel 
360ed02bdaSArd Biesheuvel 	first_slot = round_up(md->phys_addr, align);
370ed02bdaSArd Biesheuvel 	last_slot = round_down(region_end - size + 1, align);
380ed02bdaSArd Biesheuvel 
390ed02bdaSArd Biesheuvel 	if (first_slot > last_slot)
400ed02bdaSArd Biesheuvel 		return 0;
410ed02bdaSArd Biesheuvel 
420ed02bdaSArd Biesheuvel 	return ((unsigned long)(last_slot - first_slot) >> align_shift) + 1;
430ed02bdaSArd Biesheuvel }
440ed02bdaSArd Biesheuvel 
450ed02bdaSArd Biesheuvel /*
460ed02bdaSArd Biesheuvel  * The UEFI memory descriptors have a virtual address field that is only used
470ed02bdaSArd Biesheuvel  * when installing the virtual mapping using SetVirtualAddressMap(). Since it
480ed02bdaSArd Biesheuvel  * is unused here, we can reuse it to keep track of each descriptor's slot
490ed02bdaSArd Biesheuvel  * count.
500ed02bdaSArd Biesheuvel  */
510ed02bdaSArd Biesheuvel #define MD_NUM_SLOTS(md)	((md)->virt_addr)
520ed02bdaSArd Biesheuvel 
530ed02bdaSArd Biesheuvel efi_status_t efi_random_alloc(unsigned long size,
540ed02bdaSArd Biesheuvel 			      unsigned long align,
550ed02bdaSArd Biesheuvel 			      unsigned long *addr,
569cf42bcaSArd Biesheuvel 			      unsigned long random_seed,
579cf42bcaSArd Biesheuvel 			      int memory_type)
580ed02bdaSArd Biesheuvel {
59eab31265SArd Biesheuvel 	unsigned long total_slots = 0, target_slot;
60a6cfe03cSArd Biesheuvel 	unsigned long total_mirrored_slots = 0;
61eab31265SArd Biesheuvel 	struct efi_boot_memmap *map;
620ed02bdaSArd Biesheuvel 	efi_status_t status;
630ed02bdaSArd Biesheuvel 	int map_offset;
640ed02bdaSArd Biesheuvel 
65171539f5SArd Biesheuvel 	status = efi_get_memory_map(&map, false);
660ed02bdaSArd Biesheuvel 	if (status != EFI_SUCCESS)
670ed02bdaSArd Biesheuvel 		return status;
680ed02bdaSArd Biesheuvel 
690ed02bdaSArd Biesheuvel 	if (align < EFI_ALLOC_ALIGN)
700ed02bdaSArd Biesheuvel 		align = EFI_ALLOC_ALIGN;
710ed02bdaSArd Biesheuvel 
72e1df73e2SArd Biesheuvel 	size = round_up(size, EFI_ALLOC_ALIGN);
73e1df73e2SArd Biesheuvel 
740ed02bdaSArd Biesheuvel 	/* count the suitable slots in each memory map entry */
75eab31265SArd Biesheuvel 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
76eab31265SArd Biesheuvel 		efi_memory_desc_t *md = (void *)map->map + map_offset;
770ed02bdaSArd Biesheuvel 		unsigned long slots;
780ed02bdaSArd Biesheuvel 
790ed02bdaSArd Biesheuvel 		slots = get_entry_num_slots(md, size, ilog2(align));
800ed02bdaSArd Biesheuvel 		MD_NUM_SLOTS(md) = slots;
810ed02bdaSArd Biesheuvel 		total_slots += slots;
82a6cfe03cSArd Biesheuvel 		if (md->attribute & EFI_MEMORY_MORE_RELIABLE)
83a6cfe03cSArd Biesheuvel 			total_mirrored_slots += slots;
840ed02bdaSArd Biesheuvel 	}
850ed02bdaSArd Biesheuvel 
86a6cfe03cSArd Biesheuvel 	/* consider only mirrored slots for randomization if any exist */
87a6cfe03cSArd Biesheuvel 	if (total_mirrored_slots > 0)
88a6cfe03cSArd Biesheuvel 		total_slots = total_mirrored_slots;
89a6cfe03cSArd Biesheuvel 
900ed02bdaSArd Biesheuvel 	/* find a random number between 0 and total_slots */
91c37c9162SArd Biesheuvel 	target_slot = (total_slots * (u64)(random_seed & U32_MAX)) >> 32;
920ed02bdaSArd Biesheuvel 
930ed02bdaSArd Biesheuvel 	/*
940ed02bdaSArd Biesheuvel 	 * target_slot is now a value in the range [0, total_slots), and so
950ed02bdaSArd Biesheuvel 	 * it corresponds with exactly one of the suitable slots we recorded
960ed02bdaSArd Biesheuvel 	 * when iterating over the memory map the first time around.
970ed02bdaSArd Biesheuvel 	 *
980ed02bdaSArd Biesheuvel 	 * So iterate over the memory map again, subtracting the number of
990ed02bdaSArd Biesheuvel 	 * slots of each entry at each iteration, until we have found the entry
1000ed02bdaSArd Biesheuvel 	 * that covers our chosen slot. Use the residual value of target_slot
1010ed02bdaSArd Biesheuvel 	 * to calculate the randomly chosen address, and allocate it directly
1020ed02bdaSArd Biesheuvel 	 * using EFI_ALLOCATE_ADDRESS.
1030ed02bdaSArd Biesheuvel 	 */
104*0b1d9debSArd Biesheuvel 	status = EFI_OUT_OF_RESOURCES;
105eab31265SArd Biesheuvel 	for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
106eab31265SArd Biesheuvel 		efi_memory_desc_t *md = (void *)map->map + map_offset;
1070ed02bdaSArd Biesheuvel 		efi_physical_addr_t target;
1080ed02bdaSArd Biesheuvel 		unsigned long pages;
1090ed02bdaSArd Biesheuvel 
110a6cfe03cSArd Biesheuvel 		if (total_mirrored_slots > 0 &&
111a6cfe03cSArd Biesheuvel 		    !(md->attribute & EFI_MEMORY_MORE_RELIABLE))
112a6cfe03cSArd Biesheuvel 			continue;
113a6cfe03cSArd Biesheuvel 
1140ed02bdaSArd Biesheuvel 		if (target_slot >= MD_NUM_SLOTS(md)) {
1150ed02bdaSArd Biesheuvel 			target_slot -= MD_NUM_SLOTS(md);
1160ed02bdaSArd Biesheuvel 			continue;
1170ed02bdaSArd Biesheuvel 		}
1180ed02bdaSArd Biesheuvel 
1190ed02bdaSArd Biesheuvel 		target = round_up(md->phys_addr, align) + target_slot * align;
120e1df73e2SArd Biesheuvel 		pages = size / EFI_PAGE_SIZE;
1210ed02bdaSArd Biesheuvel 
1220ed02bdaSArd Biesheuvel 		status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
1239cf42bcaSArd Biesheuvel 				     memory_type, pages, &target);
1240ed02bdaSArd Biesheuvel 		if (status == EFI_SUCCESS)
1250ed02bdaSArd Biesheuvel 			*addr = target;
1260ed02bdaSArd Biesheuvel 		break;
1270ed02bdaSArd Biesheuvel 	}
1280ed02bdaSArd Biesheuvel 
129eab31265SArd Biesheuvel 	efi_bs_call(free_pool, map);
1300ed02bdaSArd Biesheuvel 
1310ed02bdaSArd Biesheuvel 	return status;
1320ed02bdaSArd Biesheuvel }
133