blob: c82b53f336adff2a90a5d35575a09136d92476e6 [file] [log] [blame]
Alexander Graf8623f922016-03-04 01:10:04 +01001/*
2 * EFI application memory management
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9/* #define DEBUG_EFI */
10
11#include <common.h>
12#include <efi_loader.h>
13#include <malloc.h>
14#include <asm/global_data.h>
15#include <libfdt_env.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21struct efi_mem_list {
22 struct list_head link;
23 struct efi_mem_desc desc;
24};
25
26/* This list contains all memory map items */
27LIST_HEAD(efi_mem);
28
29/*
30 * Unmaps all memory occupied by the carve_desc region from the
31 * list entry pointed to by map.
32 *
33 * Returns 1 if carving was performed or 0 if the regions don't overlap.
34 * Returns -1 if it would affect non-RAM regions but overlap_only_ram is set.
35 * Carving is only guaranteed to complete when all regions return 0.
36 */
37static int efi_mem_carve_out(struct efi_mem_list *map,
38 struct efi_mem_desc *carve_desc,
39 bool overlap_only_ram)
40{
41 struct efi_mem_list *newmap;
42 struct efi_mem_desc *map_desc = &map->desc;
43 uint64_t map_start = map_desc->physical_start;
44 uint64_t map_end = map_start + (map_desc->num_pages << EFI_PAGE_SHIFT);
45 uint64_t carve_start = carve_desc->physical_start;
46 uint64_t carve_end = carve_start +
47 (carve_desc->num_pages << EFI_PAGE_SHIFT);
48
49 /* check whether we're overlapping */
50 if ((carve_end <= map_start) || (carve_start >= map_end))
51 return 0;
52
53 /* We're overlapping with non-RAM, warn the caller if desired */
54 if (overlap_only_ram && (map_desc->type != EFI_CONVENTIONAL_MEMORY))
55 return -1;
56
57 /* Sanitize carve_start and carve_end to lie within our bounds */
58 carve_start = max(carve_start, map_start);
59 carve_end = min(carve_end, map_end);
60
61 /* Carving at the beginning of our map? Just move it! */
62 if (carve_start == map_start) {
63 if (map_end == carve_end) {
64 /* Full overlap, just remove map */
65 list_del(&map->link);
66 }
67
68 map_desc->physical_start = carve_end;
69 map_desc->num_pages = (map_end - carve_end) >> EFI_PAGE_SHIFT;
70 return 1;
71 }
72
73 /*
74 * Overlapping maps, just split the list map at carve_start,
75 * it will get moved or removed in the next iteration.
76 *
77 * [ map_desc |__carve_start__| newmap ]
78 */
79
80 /* Create a new map from [ carve_start ... map_end ] */
81 newmap = calloc(1, sizeof(*newmap));
82 newmap->desc = map->desc;
83 newmap->desc.physical_start = carve_start;
84 newmap->desc.num_pages = (map_end - carve_start) >> EFI_PAGE_SHIFT;
85 list_add_tail(&newmap->link, &efi_mem);
86
87 /* Shrink the map to [ map_start ... carve_start ] */
88 map_desc->num_pages = (carve_start - map_start) >> EFI_PAGE_SHIFT;
89
90 return 1;
91}
92
93uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
94 bool overlap_only_ram)
95{
96 struct list_head *lhandle;
97 struct efi_mem_list *newlist;
98 bool do_carving;
99
100 if (!pages)
101 return start;
102
103 newlist = calloc(1, sizeof(*newlist));
104 newlist->desc.type = memory_type;
105 newlist->desc.physical_start = start;
106 newlist->desc.virtual_start = start;
107 newlist->desc.num_pages = pages;
108
109 switch (memory_type) {
110 case EFI_RUNTIME_SERVICES_CODE:
111 case EFI_RUNTIME_SERVICES_DATA:
112 newlist->desc.attribute = (1 << EFI_MEMORY_WB_SHIFT) |
113 (1ULL << EFI_MEMORY_RUNTIME_SHIFT);
114 break;
115 case EFI_MMAP_IO:
116 newlist->desc.attribute = 1ULL << EFI_MEMORY_RUNTIME_SHIFT;
117 break;
118 default:
119 newlist->desc.attribute = 1 << EFI_MEMORY_WB_SHIFT;
120 break;
121 }
122
123 /* Add our new map */
124 do {
125 do_carving = false;
126 list_for_each(lhandle, &efi_mem) {
127 struct efi_mem_list *lmem;
128 int r;
129
130 lmem = list_entry(lhandle, struct efi_mem_list, link);
131 r = efi_mem_carve_out(lmem, &newlist->desc,
132 overlap_only_ram);
133 if (r < 0) {
134 return 0;
135 } else if (r) {
136 do_carving = true;
137 break;
138 }
139 }
140 } while (do_carving);
141
142 /* Add our new map */
143 list_add_tail(&newlist->link, &efi_mem);
144
145 return start;
146}
147
148static uint64_t efi_find_free_memory(uint64_t len, uint64_t max_addr)
149{
150 struct list_head *lhandle;
151
152 list_for_each(lhandle, &efi_mem) {
153 struct efi_mem_list *lmem = list_entry(lhandle,
154 struct efi_mem_list, link);
155 struct efi_mem_desc *desc = &lmem->desc;
156 uint64_t desc_len = desc->num_pages << EFI_PAGE_SHIFT;
157 uint64_t desc_end = desc->physical_start + desc_len;
158 uint64_t curmax = min(max_addr, desc_end);
159 uint64_t ret = curmax - len;
160
161 /* We only take memory from free RAM */
162 if (desc->type != EFI_CONVENTIONAL_MEMORY)
163 continue;
164
165 /* Out of bounds for max_addr */
166 if ((ret + len) > max_addr)
167 continue;
168
169 /* Out of bounds for upper map limit */
170 if ((ret + len) > desc_end)
171 continue;
172
173 /* Out of bounds for lower map limit */
174 if (ret < desc->physical_start)
175 continue;
176
177 /* Return the highest address in this map within bounds */
178 return ret;
179 }
180
181 return 0;
182}
183
184efi_status_t efi_allocate_pages(int type, int memory_type,
185 unsigned long pages, uint64_t *memory)
186{
187 u64 len = pages << EFI_PAGE_SHIFT;
188 efi_status_t r = EFI_SUCCESS;
189 uint64_t addr;
190
191 switch (type) {
192 case 0:
193 /* Any page */
194 addr = efi_find_free_memory(len, gd->ram_top);
195 if (!addr) {
196 r = EFI_NOT_FOUND;
197 break;
198 }
199 break;
200 case 1:
201 /* Max address */
202 addr = efi_find_free_memory(len, *memory);
203 if (!addr) {
204 r = EFI_NOT_FOUND;
205 break;
206 }
207 break;
208 case 2:
209 /* Exact address, reserve it. The addr is already in *memory. */
210 addr = *memory;
211 break;
212 default:
213 /* UEFI doesn't specify other allocation types */
214 r = EFI_INVALID_PARAMETER;
215 break;
216 }
217
218 if (r == EFI_SUCCESS) {
219 uint64_t ret;
220
221 /* Reserve that map in our memory maps */
222 ret = efi_add_memory_map(addr, pages, memory_type, true);
223 if (ret == addr) {
224 *memory = addr;
225 } else {
226 /* Map would overlap, bail out */
227 r = EFI_OUT_OF_RESOURCES;
228 }
229 }
230
231 return r;
232}
233
234void *efi_alloc(uint64_t len, int memory_type)
235{
236 uint64_t ret = 0;
237 uint64_t pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
238 efi_status_t r;
239
240 r = efi_allocate_pages(0, memory_type, pages, &ret);
241 if (r == EFI_SUCCESS)
242 return (void*)(uintptr_t)ret;
243
244 return NULL;
245}
246
247efi_status_t efi_free_pages(uint64_t memory, unsigned long pages)
248{
249 /* We don't free, let's cross our fingers we have plenty RAM */
250 return EFI_SUCCESS;
251}
252
253efi_status_t efi_get_memory_map(unsigned long *memory_map_size,
254 struct efi_mem_desc *memory_map,
255 unsigned long *map_key,
256 unsigned long *descriptor_size,
257 uint32_t *descriptor_version)
258{
259 ulong map_size = 0;
260 struct list_head *lhandle;
261
262 list_for_each(lhandle, &efi_mem)
263 map_size += sizeof(struct efi_mem_desc);
264
265 *memory_map_size = map_size;
266
267 if (descriptor_size)
268 *descriptor_size = sizeof(struct efi_mem_desc);
269
270 if (*memory_map_size < map_size)
271 return EFI_BUFFER_TOO_SMALL;
272
273 /* Copy list into array */
274 if (memory_map) {
275 list_for_each(lhandle, &efi_mem) {
276 struct efi_mem_list *lmem;
277
278 lmem = list_entry(lhandle, struct efi_mem_list, link);
279 *memory_map = lmem->desc;
280 memory_map++;
281 }
282 }
283
284 return EFI_SUCCESS;
285}
286
287int efi_memory_init(void)
288{
289 uint64_t runtime_start, runtime_end, runtime_pages;
290 uint64_t uboot_start, uboot_pages;
291 uint64_t uboot_stack_size = 16 * 1024 * 1024;
292 int i;
293
294 /* Add RAM */
295 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
296 u64 ram_start = gd->bd->bi_dram[i].start;
297 u64 ram_size = gd->bd->bi_dram[i].size;
298 u64 start = (ram_start + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
299 u64 pages = (ram_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
300
301 efi_add_memory_map(start, pages, EFI_CONVENTIONAL_MEMORY,
302 false);
303 }
304
305 /* Add U-Boot */
306 uboot_start = (gd->start_addr_sp - uboot_stack_size) & ~EFI_PAGE_MASK;
307 uboot_pages = (gd->ram_top - uboot_start) >> EFI_PAGE_SHIFT;
308 efi_add_memory_map(uboot_start, uboot_pages, EFI_LOADER_DATA, false);
309
310 /* Add Runtime Services */
311 runtime_start = (ulong)&__efi_runtime_start & ~EFI_PAGE_MASK;
312 runtime_end = (ulong)&__efi_runtime_stop;
313 runtime_end = (runtime_end + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;
314 runtime_pages = (runtime_end - runtime_start) >> EFI_PAGE_SHIFT;
315 efi_add_memory_map(runtime_start, runtime_pages,
316 EFI_RUNTIME_SERVICES_CODE, false);
317
318 return 0;
319}