blob: 7ec6cfa01fb30d093e1c2fd7fd35e854e55a00d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 1.0
5 *
6 * Copyright (C) 1999 VA Linux Systems
7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8 * Copyright (C) 1999-2002 Hewlett-Packard Co.
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 * Stephane Eranian <eranian@hpl.hp.com>
11 *
12 * All EFI Runtime Services are not implemented yet as EFI only
13 * supports physical mode addressing on SoftSDV. This is to be fixed
14 * in a future version. --drummond 1999-07-20
15 *
16 * Implemented EFI runtime services and virtual mode calls. --davidm
17 *
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
20 */
21
22#include <linux/config.h>
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/mm.h>
26#include <linux/types.h>
27#include <linux/time.h>
28#include <linux/spinlock.h>
29#include <linux/bootmem.h>
30#include <linux/ioport.h>
31#include <linux/module.h>
32#include <linux/efi.h>
Eric W. Biederman1bc3b912005-06-25 14:58:01 -070033#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/setup.h>
36#include <asm/io.h>
37#include <asm/page.h>
38#include <asm/pgtable.h>
39#include <asm/processor.h>
40#include <asm/desc.h>
41#include <asm/tlbflush.h>
42
43#define EFI_DEBUG 0
44#define PFX "EFI: "
45
46extern efi_status_t asmlinkage efi_call_phys(void *, ...);
47
48struct efi efi;
49EXPORT_SYMBOL(efi);
maximilian attemsc41f5eb2005-04-16 15:25:53 -070050static struct efi efi_phys;
51struct efi_memory_map memmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * We require an early boot_ioremap mapping mechanism initially
55 */
56extern void * boot_ioremap(unsigned long, unsigned long);
57
58/*
59 * To make EFI call EFI runtime service in physical addressing mode we need
60 * prelog/epilog before/after the invocation to disable interrupt, to
61 * claim EFI runtime service handler exclusively and to duplicate a memory in
62 * low memory space say 0 - 3G.
63 */
64
65static unsigned long efi_rt_eflags;
66static DEFINE_SPINLOCK(efi_rt_lock);
67static pgd_t efi_bak_pg_dir_pointer[2];
68
69static void efi_call_phys_prelog(void)
70{
71 unsigned long cr4;
72 unsigned long temp;
Edgar Huceke8c3b5a2006-03-06 15:42:54 -080073 struct Xgt_desc_struct *cpu_gdt_descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 spin_lock(&efi_rt_lock);
76 local_irq_save(efi_rt_eflags);
77
Edgar Huceke8c3b5a2006-03-06 15:42:54 -080078 cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /*
81 * If I don't have PSE, I should just duplicate two entries in page
82 * directory. If I have PSE, I just need to duplicate one entry in
83 * page directory.
84 */
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070085 cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 if (cr4 & X86_CR4_PSE) {
88 efi_bak_pg_dir_pointer[0].pgd =
89 swapper_pg_dir[pgd_index(0)].pgd;
90 swapper_pg_dir[0].pgd =
91 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
92 } else {
93 efi_bak_pg_dir_pointer[0].pgd =
94 swapper_pg_dir[pgd_index(0)].pgd;
95 efi_bak_pg_dir_pointer[1].pgd =
96 swapper_pg_dir[pgd_index(0x400000)].pgd;
97 swapper_pg_dir[pgd_index(0)].pgd =
98 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
99 temp = PAGE_OFFSET + 0x400000;
100 swapper_pg_dir[pgd_index(0x400000)].pgd =
101 swapper_pg_dir[pgd_index(temp)].pgd;
102 }
103
104 /*
105 * After the lock is released, the original page table is restored.
106 */
107 local_flush_tlb();
108
Edgar Huceke8c3b5a2006-03-06 15:42:54 -0800109 cpu_gdt_descr->address = __pa(cpu_gdt_descr->address);
110 load_gdt(cpu_gdt_descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static void efi_call_phys_epilog(void)
114{
115 unsigned long cr4;
Edgar Huceke8c3b5a2006-03-06 15:42:54 -0800116 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Andrew Morton40780062006-03-22 00:07:35 -0800118 cpu_gdt_descr->address = (unsigned long)__va(cpu_gdt_descr->address);
Edgar Huceke8c3b5a2006-03-06 15:42:54 -0800119 load_gdt(cpu_gdt_descr);
James Bottomley2b932f62006-02-24 13:04:14 -0800120
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700121 cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (cr4 & X86_CR4_PSE) {
124 swapper_pg_dir[pgd_index(0)].pgd =
125 efi_bak_pg_dir_pointer[0].pgd;
126 } else {
127 swapper_pg_dir[pgd_index(0)].pgd =
128 efi_bak_pg_dir_pointer[0].pgd;
129 swapper_pg_dir[pgd_index(0x400000)].pgd =
130 efi_bak_pg_dir_pointer[1].pgd;
131 }
132
133 /*
134 * After the lock is released, the original page table is restored.
135 */
136 local_flush_tlb();
137
138 local_irq_restore(efi_rt_eflags);
139 spin_unlock(&efi_rt_lock);
140}
141
142static efi_status_t
143phys_efi_set_virtual_address_map(unsigned long memory_map_size,
144 unsigned long descriptor_size,
145 u32 descriptor_version,
146 efi_memory_desc_t *virtual_map)
147{
148 efi_status_t status;
149
150 efi_call_phys_prelog();
151 status = efi_call_phys(efi_phys.set_virtual_address_map,
152 memory_map_size, descriptor_size,
153 descriptor_version, virtual_map);
154 efi_call_phys_epilog();
155 return status;
156}
157
158static efi_status_t
159phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
160{
161 efi_status_t status;
162
163 efi_call_phys_prelog();
164 status = efi_call_phys(efi_phys.get_time, tm, tc);
165 efi_call_phys_epilog();
166 return status;
167}
168
169inline int efi_set_rtc_mmss(unsigned long nowtime)
170{
171 int real_seconds, real_minutes;
172 efi_status_t status;
173 efi_time_t eft;
174 efi_time_cap_t cap;
175
176 spin_lock(&efi_rt_lock);
177 status = efi.get_time(&eft, &cap);
178 spin_unlock(&efi_rt_lock);
179 if (status != EFI_SUCCESS)
180 panic("Ooops, efitime: can't read time!\n");
181 real_seconds = nowtime % 60;
182 real_minutes = nowtime / 60;
183
184 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
185 real_minutes += 30;
186 real_minutes %= 60;
187
188 eft.minute = real_minutes;
189 eft.second = real_seconds;
190
191 if (status != EFI_SUCCESS) {
192 printk("Ooops: efitime: can't read time!\n");
193 return -1;
194 }
195 return 0;
196}
197/*
198 * This should only be used during kernel init and before runtime
199 * services have been remapped, therefore, we'll need to call in physical
200 * mode. Note, this call isn't used later, so mark it __init.
201 */
202inline unsigned long __init efi_get_time(void)
203{
204 efi_status_t status;
205 efi_time_t eft;
206 efi_time_cap_t cap;
207
208 status = phys_efi_get_time(&eft, &cap);
209 if (status != EFI_SUCCESS)
210 printk("Oops: efitime: can't read time status: 0x%lx\n",status);
211
212 return mktime(eft.year, eft.month, eft.day, eft.hour,
213 eft.minute, eft.second);
214}
215
216int is_available_memory(efi_memory_desc_t * md)
217{
218 if (!(md->attribute & EFI_MEMORY_WB))
219 return 0;
220
221 switch (md->type) {
222 case EFI_LOADER_CODE:
223 case EFI_LOADER_DATA:
224 case EFI_BOOT_SERVICES_CODE:
225 case EFI_BOOT_SERVICES_DATA:
226 case EFI_CONVENTIONAL_MEMORY:
227 return 1;
228 }
229 return 0;
230}
231
232/*
233 * We need to map the EFI memory map again after paging_init().
234 */
235void __init efi_map_memmap(void)
236{
237 memmap.map = NULL;
238
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700239 memmap.map = bt_ioremap((unsigned long) memmap.phys_map,
240 (memmap.nr_map * memmap.desc_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (memmap.map == NULL)
242 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700243
244 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247#if EFI_DEBUG
248static void __init print_efi_memmap(void)
249{
250 efi_memory_desc_t *md;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700251 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int i;
253
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700254 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
255 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
257 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
258 i, md->type, md->attribute, md->phys_addr,
259 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
260 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
261 }
262}
263#endif /* EFI_DEBUG */
264
265/*
266 * Walks the EFI memory map and calls CALLBACK once for each EFI
267 * memory descriptor that has memory that is available for kernel use.
268 */
269void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
270{
271 int prev_valid = 0;
272 struct range {
273 unsigned long start;
274 unsigned long end;
275 } prev, curr;
276 efi_memory_desc_t *md;
277 unsigned long start, end;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700278 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700280 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
281 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if ((md->num_pages == 0) || (!is_available_memory(md)))
284 continue;
285
286 curr.start = md->phys_addr;
287 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
288
289 if (!prev_valid) {
290 prev = curr;
291 prev_valid = 1;
292 } else {
293 if (curr.start < prev.start)
294 printk(KERN_INFO PFX "Unordered memory map\n");
295 if (prev.end == curr.start)
296 prev.end = curr.end;
297 else {
298 start =
299 (unsigned long) (PAGE_ALIGN(prev.start));
300 end = (unsigned long) (prev.end & PAGE_MASK);
301 if ((end > start)
302 && (*callback) (start, end, arg) < 0)
303 return;
304 prev = curr;
305 }
306 }
307 }
308 if (prev_valid) {
309 start = (unsigned long) PAGE_ALIGN(prev.start);
310 end = (unsigned long) (prev.end & PAGE_MASK);
311 if (end > start)
312 (*callback) (start, end, arg);
313 }
314}
315
316void __init efi_init(void)
317{
318 efi_config_table_t *config_tables;
319 efi_runtime_services_t *runtime;
320 efi_char16_t *c16;
321 char vendor[100] = "unknown";
322 unsigned long num_config_tables;
323 int i = 0;
324
325 memset(&efi, 0, sizeof(efi) );
326 memset(&efi_phys, 0, sizeof(efi_phys));
327
328 efi_phys.systab = EFI_SYSTAB;
329 memmap.phys_map = EFI_MEMMAP;
330 memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
331 memmap.desc_version = EFI_MEMDESC_VERSION;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700332 memmap.desc_size = EFI_MEMDESC_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 efi.systab = (efi_system_table_t *)
335 boot_ioremap((unsigned long) efi_phys.systab,
336 sizeof(efi_system_table_t));
337 /*
338 * Verify the EFI Table
339 */
340 if (efi.systab == NULL)
341 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
342 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
343 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
344 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
345 printk(KERN_ERR PFX
346 "Warning: EFI system table major version mismatch: "
347 "got %d.%02d, expected %d.%02d\n",
348 efi.systab->hdr.revision >> 16,
349 efi.systab->hdr.revision & 0xffff,
350 EFI_SYSTEM_TABLE_REVISION >> 16,
351 EFI_SYSTEM_TABLE_REVISION & 0xffff);
352 /*
353 * Grab some details from the system table
354 */
355 num_config_tables = efi.systab->nr_tables;
356 config_tables = (efi_config_table_t *)efi.systab->tables;
357 runtime = efi.systab->runtime;
358
359 /*
360 * Show what we know for posterity
361 */
362 c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
363 if (c16) {
364 for (i = 0; i < sizeof(vendor) && *c16; ++i)
365 vendor[i] = *c16++;
366 vendor[i] = '\0';
367 } else
368 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
369
370 printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
371 efi.systab->hdr.revision >> 16,
372 efi.systab->hdr.revision & 0xffff, vendor);
373
374 /*
375 * Let's see what config tables the firmware passed to us.
376 */
377 config_tables = (efi_config_table_t *)
378 boot_ioremap((unsigned long) config_tables,
379 num_config_tables * sizeof(efi_config_table_t));
380
381 if (config_tables == NULL)
382 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
383
384 for (i = 0; i < num_config_tables; i++) {
385 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
386 efi.mps = (void *)config_tables[i].table;
387 printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
388 } else
389 if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
390 efi.acpi20 = __va(config_tables[i].table);
391 printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
392 } else
393 if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
394 efi.acpi = __va(config_tables[i].table);
395 printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
396 } else
397 if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
398 efi.smbios = (void *) config_tables[i].table;
399 printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
400 } else
401 if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
402 efi.hcdp = (void *)config_tables[i].table;
403 printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
404 } else
405 if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
406 efi.uga = (void *)config_tables[i].table;
407 printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
408 }
409 }
410 printk("\n");
411
412 /*
413 * Check out the runtime services table. We need to map
414 * the runtime services table so that we can grab the physical
415 * address of several of the EFI runtime functions, needed to
416 * set the firmware into virtual mode.
417 */
418
419 runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
420 runtime,
421 sizeof(efi_runtime_services_t));
422 if (runtime != NULL) {
423 /*
424 * We will only need *early* access to the following
425 * two EFI runtime services before set_virtual_address_map
426 * is invoked.
427 */
428 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
429 efi_phys.set_virtual_address_map =
430 (efi_set_virtual_address_map_t *)
431 runtime->set_virtual_address_map;
432 } else
433 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
434
435 /* Map the EFI memory map for use until paging_init() */
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700436 memmap.map = boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (memmap.map == NULL)
438 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
439
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700440 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#if EFI_DEBUG
443 print_efi_memmap();
444#endif
445}
446
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700447static inline void __init check_range_for_systab(efi_memory_desc_t *md)
448{
449 if (((unsigned long)md->phys_addr <= (unsigned long)efi_phys.systab) &&
450 ((unsigned long)efi_phys.systab < md->phys_addr +
451 ((unsigned long)md->num_pages << EFI_PAGE_SHIFT))) {
452 unsigned long addr;
453
454 addr = md->virt_addr - md->phys_addr +
455 (unsigned long)efi_phys.systab;
456 efi.systab = (efi_system_table_t *)addr;
457 }
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/*
461 * This function will switch the EFI runtime services to virtual mode.
462 * Essentially, look through the EFI memmap and map every region that
463 * has the runtime attribute bit set in its memory descriptor and update
464 * that memory descriptor with the virtual address obtained from ioremap().
465 * This enables the runtime services to be called without having to
466 * thunk back into physical mode for every invocation.
467 */
468
469void __init efi_enter_virtual_mode(void)
470{
471 efi_memory_desc_t *md;
472 efi_status_t status;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700473 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 efi.systab = NULL;
476
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700477 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
478 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700480 if (!(md->attribute & EFI_MEMORY_RUNTIME))
481 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700483 md->virt_addr = (unsigned long)ioremap(md->phys_addr,
484 md->num_pages << EFI_PAGE_SHIFT);
485 if (!(unsigned long)md->virt_addr) {
486 printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
487 (unsigned long)md->phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700489 /* update the virtual address of the EFI system table */
490 check_range_for_systab(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
493 if (!efi.systab)
494 BUG();
495
496 status = phys_efi_set_virtual_address_map(
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700497 memmap.desc_size * memmap.nr_map,
498 memmap.desc_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 memmap.desc_version,
500 memmap.phys_map);
501
502 if (status != EFI_SUCCESS) {
503 printk (KERN_ALERT "You are screwed! "
504 "Unable to switch EFI into virtual mode "
505 "(status=%lx)\n", status);
506 panic("EFI call to SetVirtualAddressMap() failed!");
507 }
508
509 /*
510 * Now that EFI is in virtual mode, update the function
511 * pointers in the runtime service table to the new virtual addresses.
512 */
513
514 efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
515 efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
516 efi.get_wakeup_time = (efi_get_wakeup_time_t *)
517 efi.systab->runtime->get_wakeup_time;
518 efi.set_wakeup_time = (efi_set_wakeup_time_t *)
519 efi.systab->runtime->set_wakeup_time;
520 efi.get_variable = (efi_get_variable_t *)
521 efi.systab->runtime->get_variable;
522 efi.get_next_variable = (efi_get_next_variable_t *)
523 efi.systab->runtime->get_next_variable;
524 efi.set_variable = (efi_set_variable_t *)
525 efi.systab->runtime->set_variable;
526 efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
527 efi.systab->runtime->get_next_high_mono_count;
528 efi.reset_system = (efi_reset_system_t *)
529 efi.systab->runtime->reset_system;
530}
531
532void __init
533efi_initialize_iomem_resources(struct resource *code_resource,
534 struct resource *data_resource)
535{
536 struct resource *res;
537 efi_memory_desc_t *md;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700538 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700540 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
541 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
544 0x100000000ULL)
545 continue;
Linus Torvaldsb408cbc2006-02-22 15:50:30 -0800546 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 switch (md->type) {
548 case EFI_RESERVED_TYPE:
549 res->name = "Reserved Memory";
550 break;
551 case EFI_LOADER_CODE:
552 res->name = "Loader Code";
553 break;
554 case EFI_LOADER_DATA:
555 res->name = "Loader Data";
556 break;
557 case EFI_BOOT_SERVICES_DATA:
558 res->name = "BootServices Data";
559 break;
560 case EFI_BOOT_SERVICES_CODE:
561 res->name = "BootServices Code";
562 break;
563 case EFI_RUNTIME_SERVICES_CODE:
564 res->name = "Runtime Service Code";
565 break;
566 case EFI_RUNTIME_SERVICES_DATA:
567 res->name = "Runtime Service Data";
568 break;
569 case EFI_CONVENTIONAL_MEMORY:
570 res->name = "Conventional Memory";
571 break;
572 case EFI_UNUSABLE_MEMORY:
573 res->name = "Unusable Memory";
574 break;
575 case EFI_ACPI_RECLAIM_MEMORY:
576 res->name = "ACPI Reclaim";
577 break;
578 case EFI_ACPI_MEMORY_NVS:
579 res->name = "ACPI NVS";
580 break;
581 case EFI_MEMORY_MAPPED_IO:
582 res->name = "Memory Mapped IO";
583 break;
584 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
585 res->name = "Memory Mapped IO Port Space";
586 break;
587 default:
588 res->name = "Reserved";
589 break;
590 }
591 res->start = md->phys_addr;
592 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
593 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
594 if (request_resource(&iomem_resource, res) < 0)
595 printk(KERN_ERR PFX "Failed to allocate res %s : 0x%lx-0x%lx\n",
596 res->name, res->start, res->end);
597 /*
598 * We don't know which region contains kernel data so we try
599 * it repeatedly and let the resource manager test it.
600 */
601 if (md->type == EFI_CONVENTIONAL_MEMORY) {
602 request_resource(res, code_resource);
603 request_resource(res, data_resource);
Eric W. Biederman1bc3b912005-06-25 14:58:01 -0700604#ifdef CONFIG_KEXEC
605 request_resource(res, &crashk_res);
606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 }
609}
610
611/*
612 * Convenience functions to obtain memory types and attributes
613 */
614
615u32 efi_mem_type(unsigned long phys_addr)
616{
617 efi_memory_desc_t *md;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700618 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700620 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
621 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if ((md->phys_addr <= phys_addr) && (phys_addr <
623 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
624 return md->type;
625 }
626 return 0;
627}
628
629u64 efi_mem_attributes(unsigned long phys_addr)
630{
631 efi_memory_desc_t *md;
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700632 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Matt Tolentino7ae65fd2005-09-03 15:56:27 -0700634 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
635 md = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if ((md->phys_addr <= phys_addr) && (phys_addr <
637 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
638 return md->attribute;
639 }
640 return 0;
641}