2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 1.0
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>
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
16 * Implemented EFI runtime services and virtual mode calls. --davidm
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/init.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>
33 #include <linux/kexec.h>
35 #include <asm/setup.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
41 #include <asm/tlbflush.h>
46 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
50 static struct efi efi_phys;
51 struct efi_memory_map memmap;
54 * We require an early boot_ioremap mapping mechanism initially
56 extern void * boot_ioremap(unsigned long, unsigned long);
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.
65 static unsigned long efi_rt_eflags;
66 static DEFINE_SPINLOCK(efi_rt_lock);
67 static pgd_t efi_bak_pg_dir_pointer[2];
69 static void efi_call_phys_prelog(void)
73 struct Xgt_desc_struct *cpu_gdt_descr;
75 spin_lock(&efi_rt_lock);
76 local_irq_save(efi_rt_eflags);
78 cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
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
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;
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;
105 * After the lock is released, the original page table is restored.
109 cpu_gdt_descr->address = __pa(cpu_gdt_descr->address);
110 load_gdt(cpu_gdt_descr);
113 static void efi_call_phys_epilog(void)
116 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
118 cpu_gdt_descr->address = __va(cpu_gdt_descr->address);
119 load_gdt(cpu_gdt_descr);
123 if (cr4 & X86_CR4_PSE) {
124 swapper_pg_dir[pgd_index(0)].pgd =
125 efi_bak_pg_dir_pointer[0].pgd;
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;
134 * After the lock is released, the original page table is restored.
138 local_irq_restore(efi_rt_eflags);
139 spin_unlock(&efi_rt_lock);
143 phys_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)
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();
159 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
163 efi_call_phys_prelog();
164 status = efi_call_phys(efi_phys.get_time, tm, tc);
165 efi_call_phys_epilog();
169 inline int efi_set_rtc_mmss(unsigned long nowtime)
171 int real_seconds, real_minutes;
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;
184 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
188 eft.minute = real_minutes;
189 eft.second = real_seconds;
191 if (status != EFI_SUCCESS) {
192 printk("Ooops: efitime: can't read time!\n");
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.
202 inline unsigned long __init efi_get_time(void)
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);
212 return mktime(eft.year, eft.month, eft.day, eft.hour,
213 eft.minute, eft.second);
216 int is_available_memory(efi_memory_desc_t * md)
218 if (!(md->attribute & EFI_MEMORY_WB))
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:
233 * We need to map the EFI memory map again after paging_init().
235 void __init efi_map_memmap(void)
239 memmap.map = bt_ioremap((unsigned long) memmap.phys_map,
240 (memmap.nr_map * memmap.desc_size));
241 if (memmap.map == NULL)
242 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
244 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
248 static void __init print_efi_memmap(void)
250 efi_memory_desc_t *md;
254 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
256 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)));
263 #endif /* EFI_DEBUG */
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.
269 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
276 efi_memory_desc_t *md;
277 unsigned long start, end;
280 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
283 if ((md->num_pages == 0) || (!is_available_memory(md)))
286 curr.start = md->phys_addr;
287 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
293 if (curr.start < prev.start)
294 printk(KERN_INFO PFX "Unordered memory map\n");
295 if (prev.end == curr.start)
299 (unsigned long) (PAGE_ALIGN(prev.start));
300 end = (unsigned long) (prev.end & PAGE_MASK);
302 && (*callback) (start, end, arg) < 0)
309 start = (unsigned long) PAGE_ALIGN(prev.start);
310 end = (unsigned long) (prev.end & PAGE_MASK);
312 (*callback) (start, end, arg);
316 void __init efi_init(void)
318 efi_config_table_t *config_tables;
319 efi_runtime_services_t *runtime;
321 char vendor[100] = "unknown";
322 unsigned long num_config_tables;
325 memset(&efi, 0, sizeof(efi) );
326 memset(&efi_phys, 0, sizeof(efi_phys));
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;
332 memmap.desc_size = EFI_MEMDESC_SIZE;
334 efi.systab = (efi_system_table_t *)
335 boot_ioremap((unsigned long) efi_phys.systab,
336 sizeof(efi_system_table_t));
338 * Verify the EFI Table
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)
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);
353 * Grab some details from the system table
355 num_config_tables = efi.systab->nr_tables;
356 config_tables = (efi_config_table_t *)efi.systab->tables;
357 runtime = efi.systab->runtime;
360 * Show what we know for posterity
362 c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
364 for (i = 0; i < sizeof(vendor) && *c16; ++i)
368 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
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);
375 * Let's see what config tables the firmware passed to us.
377 config_tables = (efi_config_table_t *)
378 boot_ioremap((unsigned long) config_tables,
379 num_config_tables * sizeof(efi_config_table_t));
381 if (config_tables == NULL)
382 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
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);
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);
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);
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);
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);
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);
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.
419 runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
421 sizeof(efi_runtime_services_t));
422 if (runtime != NULL) {
424 * We will only need *early* access to the following
425 * two EFI runtime services before set_virtual_address_map
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;
433 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
435 /* Map the EFI memory map for use until paging_init() */
436 memmap.map = boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
437 if (memmap.map == NULL)
438 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
440 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
447 static inline void __init check_range_for_systab(efi_memory_desc_t *md)
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))) {
454 addr = md->virt_addr - md->phys_addr +
455 (unsigned long)efi_phys.systab;
456 efi.systab = (efi_system_table_t *)addr;
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.
469 void __init efi_enter_virtual_mode(void)
471 efi_memory_desc_t *md;
477 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
480 if (!(md->attribute & EFI_MEMORY_RUNTIME))
483 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);
489 /* update the virtual address of the EFI system table */
490 check_range_for_systab(md);
496 status = phys_efi_set_virtual_address_map(
497 memmap.desc_size * memmap.nr_map,
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!");
510 * Now that EFI is in virtual mode, update the function
511 * pointers in the runtime service table to the new virtual addresses.
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;
533 efi_initialize_iomem_resources(struct resource *code_resource,
534 struct resource *data_resource)
536 struct resource *res;
537 efi_memory_desc_t *md;
540 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
543 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
546 res = alloc_bootmem_low(sizeof(struct resource));
548 case EFI_RESERVED_TYPE:
549 res->name = "Reserved Memory";
551 case EFI_LOADER_CODE:
552 res->name = "Loader Code";
554 case EFI_LOADER_DATA:
555 res->name = "Loader Data";
557 case EFI_BOOT_SERVICES_DATA:
558 res->name = "BootServices Data";
560 case EFI_BOOT_SERVICES_CODE:
561 res->name = "BootServices Code";
563 case EFI_RUNTIME_SERVICES_CODE:
564 res->name = "Runtime Service Code";
566 case EFI_RUNTIME_SERVICES_DATA:
567 res->name = "Runtime Service Data";
569 case EFI_CONVENTIONAL_MEMORY:
570 res->name = "Conventional Memory";
572 case EFI_UNUSABLE_MEMORY:
573 res->name = "Unusable Memory";
575 case EFI_ACPI_RECLAIM_MEMORY:
576 res->name = "ACPI Reclaim";
578 case EFI_ACPI_MEMORY_NVS:
579 res->name = "ACPI NVS";
581 case EFI_MEMORY_MAPPED_IO:
582 res->name = "Memory Mapped IO";
584 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
585 res->name = "Memory Mapped IO Port Space";
588 res->name = "Reserved";
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);
598 * We don't know which region contains kernel data so we try
599 * it repeatedly and let the resource manager test it.
601 if (md->type == EFI_CONVENTIONAL_MEMORY) {
602 request_resource(res, code_resource);
603 request_resource(res, data_resource);
605 request_resource(res, &crashk_res);
612 * Convenience functions to obtain memory types and attributes
615 u32 efi_mem_type(unsigned long phys_addr)
617 efi_memory_desc_t *md;
620 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
622 if ((md->phys_addr <= phys_addr) && (phys_addr <
623 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
629 u64 efi_mem_attributes(unsigned long phys_addr)
631 efi_memory_desc_t *md;
634 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
636 if ((md->phys_addr <= phys_addr) && (phys_addr <
637 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
638 return md->attribute;