Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / kern / efi / mm.c
blobd7363699864aee1bbbcad867941cd8ddfb9a587e
1 /* mm.c - generic EFI memory management */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/misc.h>
21 #include <grub/mm.h>
22 #include <grub/efi/api.h>
23 #include <grub/efi/efi.h>
25 #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
26 ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
28 #define BYTES_TO_PAGES(bytes) ((bytes) >> 12)
29 #define PAGES_TO_BYTES(pages) ((pages) << 12)
31 /* The size of a memory map obtained from the firmware. This must be
32 a multiplier of 4KB. */
33 #define MEMORY_MAP_SIZE 0x3000
35 /* Maintain the list of allocated pages. */
36 struct allocated_page
38 grub_efi_physical_address_t addr;
39 grub_efi_uint64_t num_pages;
42 #define ALLOCATED_PAGES_SIZE 0x1000
43 #define MAX_ALLOCATED_PAGES \
44 (ALLOCATED_PAGES_SIZE / sizeof (struct allocated_page))
46 static struct allocated_page *allocated_pages = 0;
48 /* The minimum and maximum heap size for GRUB itself. */
49 #define MIN_HEAP_SIZE 0x100000
50 #define MAX_HEAP_SIZE (16 * 0x100000)
53 /* Allocate pages. Return the pointer to the first of allocated pages. */
54 void *
55 grub_efi_allocate_pages (grub_efi_physical_address_t address,
56 grub_efi_uintn_t pages)
58 grub_efi_allocate_type_t type;
59 grub_efi_status_t status;
60 grub_efi_boot_services_t *b;
62 #if GRUB_TARGET_SIZEOF_VOID_P < 8
63 /* Limit the memory access to less than 4GB for 32-bit platforms. */
64 if (address > 0xffffffff)
65 return 0;
67 if (address == 0)
69 type = GRUB_EFI_ALLOCATE_MAX_ADDRESS;
70 address = 0xffffffff;
72 else
73 type = GRUB_EFI_ALLOCATE_ADDRESS;
74 #else
75 if (address == 0)
76 type = GRUB_EFI_ALLOCATE_ANY_PAGES;
77 else
78 type = GRUB_EFI_ALLOCATE_ADDRESS;
79 #endif
81 b = grub_efi_system_table->boot_services;
82 status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_LOADER_DATA, pages, &address);
83 if (status != GRUB_EFI_SUCCESS)
84 return 0;
86 if (address == 0)
88 /* Uggh, the address 0 was allocated... This is too annoying,
89 so reallocate another one. */
90 address = 0xffffffff;
91 status = efi_call_4 (b->allocate_pages, type, GRUB_EFI_LOADER_DATA, pages, &address);
92 grub_efi_free_pages (0, pages);
93 if (status != GRUB_EFI_SUCCESS)
94 return 0;
97 if (allocated_pages)
99 unsigned i;
101 for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
102 if (allocated_pages[i].addr == 0)
104 allocated_pages[i].addr = address;
105 allocated_pages[i].num_pages = pages;
106 break;
109 if (i == MAX_ALLOCATED_PAGES)
110 grub_fatal ("too many page allocations");
113 return (void *) ((grub_addr_t) address);
116 /* Free pages starting from ADDRESS. */
117 void
118 grub_efi_free_pages (grub_efi_physical_address_t address,
119 grub_efi_uintn_t pages)
121 grub_efi_boot_services_t *b;
123 if (allocated_pages
124 && ((grub_efi_physical_address_t) ((grub_addr_t) allocated_pages)
125 != address))
127 unsigned i;
129 for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
130 if (allocated_pages[i].addr == address)
132 allocated_pages[i].addr = 0;
133 break;
137 b = grub_efi_system_table->boot_services;
138 efi_call_2 (b->free_pages, address, pages);
141 /* Get the memory map as defined in the EFI spec. Return 1 if successful,
142 return 0 if partial, or return -1 if an error occurs. */
144 grub_efi_get_memory_map (grub_efi_uintn_t *memory_map_size,
145 grub_efi_memory_descriptor_t *memory_map,
146 grub_efi_uintn_t *map_key,
147 grub_efi_uintn_t *descriptor_size,
148 grub_efi_uint32_t *descriptor_version)
150 grub_efi_status_t status;
151 grub_efi_boot_services_t *b;
152 grub_efi_uintn_t key;
153 grub_efi_uint32_t version;
155 /* Allow some parameters to be missing. */
156 if (! map_key)
157 map_key = &key;
158 if (! descriptor_version)
159 descriptor_version = &version;
161 b = grub_efi_system_table->boot_services;
162 status = efi_call_5 (b->get_memory_map, memory_map_size, memory_map, map_key,
163 descriptor_size, descriptor_version);
164 if (status == GRUB_EFI_SUCCESS)
165 return 1;
166 else if (status == GRUB_EFI_BUFFER_TOO_SMALL)
167 return 0;
168 else
169 return -1;
172 /* Sort the memory map in place. */
173 static void
174 sort_memory_map (grub_efi_memory_descriptor_t *memory_map,
175 grub_efi_uintn_t desc_size,
176 grub_efi_memory_descriptor_t *memory_map_end)
178 grub_efi_memory_descriptor_t *d1;
179 grub_efi_memory_descriptor_t *d2;
181 for (d1 = memory_map;
182 d1 < memory_map_end;
183 d1 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size))
185 grub_efi_memory_descriptor_t *max_desc = d1;
187 for (d2 = NEXT_MEMORY_DESCRIPTOR (d1, desc_size);
188 d2 < memory_map_end;
189 d2 = NEXT_MEMORY_DESCRIPTOR (d2, desc_size))
191 if (max_desc->num_pages < d2->num_pages)
192 max_desc = d2;
195 if (max_desc != d1)
197 grub_efi_memory_descriptor_t tmp;
199 tmp = *d1;
200 *d1 = *max_desc;
201 *max_desc = tmp;
206 /* Filter the descriptors. GRUB needs only available memory. */
207 static grub_efi_memory_descriptor_t *
208 filter_memory_map (grub_efi_memory_descriptor_t *memory_map,
209 grub_efi_memory_descriptor_t *filtered_memory_map,
210 grub_efi_uintn_t desc_size,
211 grub_efi_memory_descriptor_t *memory_map_end)
213 grub_efi_memory_descriptor_t *desc;
214 grub_efi_memory_descriptor_t *filtered_desc;
216 for (desc = memory_map, filtered_desc = filtered_memory_map;
217 desc < memory_map_end;
218 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
220 if (desc->type == GRUB_EFI_CONVENTIONAL_MEMORY
221 #if GRUB_TARGET_SIZEOF_VOID_P < 8
222 && desc->physical_start <= 0xffffffff
223 #endif
224 && desc->physical_start + PAGES_TO_BYTES (desc->num_pages) > 0x100000
225 && desc->num_pages != 0)
227 grub_memcpy (filtered_desc, desc, desc_size);
229 /* Avoid less than 1MB, because some loaders seem to be confused. */
230 if (desc->physical_start < 0x100000)
232 desc->num_pages -= BYTES_TO_PAGES (0x100000
233 - desc->physical_start);
234 desc->physical_start = 0x100000;
237 #if GRUB_TARGET_SIZEOF_VOID_P < 8
238 if (BYTES_TO_PAGES (filtered_desc->physical_start)
239 + filtered_desc->num_pages
240 > BYTES_TO_PAGES (0x100000000LL))
241 filtered_desc->num_pages
242 = (BYTES_TO_PAGES (0x100000000LL)
243 - BYTES_TO_PAGES (filtered_desc->physical_start));
244 #endif
246 if (filtered_desc->num_pages == 0)
247 continue;
249 filtered_desc = NEXT_MEMORY_DESCRIPTOR (filtered_desc, desc_size);
253 return filtered_desc;
256 /* Return the total number of pages. */
257 static grub_efi_uint64_t
258 get_total_pages (grub_efi_memory_descriptor_t *memory_map,
259 grub_efi_uintn_t desc_size,
260 grub_efi_memory_descriptor_t *memory_map_end)
262 grub_efi_memory_descriptor_t *desc;
263 grub_efi_uint64_t total = 0;
265 for (desc = memory_map;
266 desc < memory_map_end;
267 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
268 total += desc->num_pages;
270 return total;
273 /* Add memory regions. */
274 static void
275 add_memory_regions (grub_efi_memory_descriptor_t *memory_map,
276 grub_efi_uintn_t desc_size,
277 grub_efi_memory_descriptor_t *memory_map_end,
278 grub_efi_uint64_t required_pages)
280 grub_efi_memory_descriptor_t *desc;
282 for (desc = memory_map;
283 desc < memory_map_end;
284 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
286 grub_efi_uint64_t pages;
287 grub_efi_physical_address_t start;
288 void *addr;
290 start = desc->physical_start;
291 pages = desc->num_pages;
292 if (pages > required_pages)
294 start += PAGES_TO_BYTES (pages - required_pages);
295 pages = required_pages;
298 addr = grub_efi_allocate_pages (start, pages);
299 if (! addr)
300 grub_fatal ("cannot allocate conventional memory %p with %u pages",
301 (void *) ((grub_addr_t) start),
302 (unsigned) pages);
304 grub_mm_init_region (addr, PAGES_TO_BYTES (pages));
306 required_pages -= pages;
307 if (required_pages == 0)
308 break;
311 if (required_pages > 0)
312 grub_fatal ("too little memory");
315 #if 0
316 /* Print the memory map. */
317 static void
318 print_memory_map (grub_efi_memory_descriptor_t *memory_map,
319 grub_efi_uintn_t desc_size,
320 grub_efi_memory_descriptor_t *memory_map_end)
322 grub_efi_memory_descriptor_t *desc;
323 int i;
325 for (desc = memory_map, i = 0;
326 desc < memory_map_end;
327 desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size), i++)
329 grub_printf ("MD: t=%x, p=%llx, v=%llx, n=%llx, a=%llx\n",
330 desc->type, desc->physical_start, desc->virtual_start,
331 desc->num_pages, desc->attribute);
334 #endif
336 void
337 grub_efi_mm_init (void)
339 grub_efi_memory_descriptor_t *memory_map;
340 grub_efi_memory_descriptor_t *memory_map_end;
341 grub_efi_memory_descriptor_t *filtered_memory_map;
342 grub_efi_memory_descriptor_t *filtered_memory_map_end;
343 grub_efi_uintn_t map_size;
344 grub_efi_uintn_t desc_size;
345 grub_efi_uint64_t total_pages;
346 grub_efi_uint64_t required_pages;
348 /* First of all, allocate pages to maintain allocations. */
349 allocated_pages
350 = grub_efi_allocate_pages (0, BYTES_TO_PAGES (ALLOCATED_PAGES_SIZE));
351 if (! allocated_pages)
352 grub_fatal ("cannot allocate memory");
354 grub_memset (allocated_pages, 0, ALLOCATED_PAGES_SIZE);
356 /* Prepare a memory region to store two memory maps. */
357 memory_map = grub_efi_allocate_pages (0,
358 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
359 if (! memory_map)
360 grub_fatal ("cannot allocate memory");
362 filtered_memory_map = NEXT_MEMORY_DESCRIPTOR (memory_map, MEMORY_MAP_SIZE);
364 /* Obtain descriptors for available memory. */
365 map_size = MEMORY_MAP_SIZE;
367 if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
368 grub_fatal ("cannot get memory map");
370 memory_map_end = NEXT_MEMORY_DESCRIPTOR (memory_map, map_size);
372 filtered_memory_map_end = filter_memory_map (memory_map, filtered_memory_map,
373 desc_size, memory_map_end);
375 /* By default, request a quarter of the available memory. */
376 total_pages = get_total_pages (filtered_memory_map, desc_size,
377 filtered_memory_map_end);
378 required_pages = (total_pages >> 2);
379 if (required_pages < BYTES_TO_PAGES (MIN_HEAP_SIZE))
380 required_pages = BYTES_TO_PAGES (MIN_HEAP_SIZE);
381 else if (required_pages > BYTES_TO_PAGES (MAX_HEAP_SIZE))
382 required_pages = BYTES_TO_PAGES (MAX_HEAP_SIZE);
384 /* Sort the filtered descriptors, so that GRUB can allocate pages
385 from smaller regions. */
386 sort_memory_map (filtered_memory_map, desc_size, filtered_memory_map_end);
388 /* Allocate memory regions for GRUB's memory management. */
389 add_memory_regions (filtered_memory_map, desc_size,
390 filtered_memory_map_end, required_pages);
392 #if 0
393 /* For debug. */
394 map_size = MEMORY_MAP_SIZE;
396 if (grub_efi_get_memory_map (&map_size, memory_map, 0, &desc_size, 0) < 0)
397 grub_fatal ("cannot get memory map");
399 grub_printf ("printing memory map\n");
400 print_memory_map (memory_map, desc_size,
401 NEXT_MEMORY_DESCRIPTOR (memory_map, map_size));
402 grub_abort ();
403 #endif
405 /* Release the memory maps. */
406 grub_efi_free_pages ((grub_addr_t) memory_map,
407 2 * BYTES_TO_PAGES (MEMORY_MAP_SIZE));
410 void
411 grub_efi_mm_fini (void)
413 if (allocated_pages)
415 unsigned i;
417 for (i = 0; i < MAX_ALLOCATED_PAGES; i++)
419 struct allocated_page *p;
421 p = allocated_pages + i;
422 if (p->addr != 0)
423 grub_efi_free_pages ((grub_addr_t) p->addr, p->num_pages);
426 grub_efi_free_pages ((grub_addr_t) allocated_pages,
427 BYTES_TO_PAGES (ALLOCATED_PAGES_SIZE));