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)
74 spin_lock(&efi_rt_lock);
75 local_irq_save(efi_rt_eflags);
78 * If I don't have PSE, I should just duplicate two entries in page
79 * directory. If I have PSE, I just need to duplicate one entry in
82 __asm__ __volatile__("movl %%cr4, %0":"=r"(cr4));
84 if (cr4 & X86_CR4_PSE) {
85 efi_bak_pg_dir_pointer[0].pgd =
86 swapper_pg_dir[pgd_index(0)].pgd;
87 swapper_pg_dir[0].pgd =
88 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
90 efi_bak_pg_dir_pointer[0].pgd =
91 swapper_pg_dir[pgd_index(0)].pgd;
92 efi_bak_pg_dir_pointer[1].pgd =
93 swapper_pg_dir[pgd_index(0x400000)].pgd;
94 swapper_pg_dir[pgd_index(0)].pgd =
95 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
96 temp = PAGE_OFFSET + 0x400000;
97 swapper_pg_dir[pgd_index(0x400000)].pgd =
98 swapper_pg_dir[pgd_index(temp)].pgd;
102 * After the lock is released, the original page table is restored.
106 cpu_gdt_descr[0].address = __pa(cpu_gdt_descr[0].address);
107 __asm__ __volatile__("lgdt %0":"=m"
108 (*(struct Xgt_desc_struct *) __pa(&cpu_gdt_descr[0])));
111 static void efi_call_phys_epilog(void)
115 cpu_gdt_descr[0].address =
116 (unsigned long) __va(cpu_gdt_descr[0].address);
117 __asm__ __volatile__("lgdt %0":"=m"(cpu_gdt_descr));
118 __asm__ __volatile__("movl %%cr4, %0":"=r"(cr4));
120 if (cr4 & X86_CR4_PSE) {
121 swapper_pg_dir[pgd_index(0)].pgd =
122 efi_bak_pg_dir_pointer[0].pgd;
124 swapper_pg_dir[pgd_index(0)].pgd =
125 efi_bak_pg_dir_pointer[0].pgd;
126 swapper_pg_dir[pgd_index(0x400000)].pgd =
127 efi_bak_pg_dir_pointer[1].pgd;
131 * After the lock is released, the original page table is restored.
135 local_irq_restore(efi_rt_eflags);
136 spin_unlock(&efi_rt_lock);
140 phys_efi_set_virtual_address_map(unsigned long memory_map_size,
141 unsigned long descriptor_size,
142 u32 descriptor_version,
143 efi_memory_desc_t *virtual_map)
147 efi_call_phys_prelog();
148 status = efi_call_phys(efi_phys.set_virtual_address_map,
149 memory_map_size, descriptor_size,
150 descriptor_version, virtual_map);
151 efi_call_phys_epilog();
156 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
160 efi_call_phys_prelog();
161 status = efi_call_phys(efi_phys.get_time, tm, tc);
162 efi_call_phys_epilog();
166 inline int efi_set_rtc_mmss(unsigned long nowtime)
168 int real_seconds, real_minutes;
173 spin_lock(&efi_rt_lock);
174 status = efi.get_time(&eft, &cap);
175 spin_unlock(&efi_rt_lock);
176 if (status != EFI_SUCCESS)
177 panic("Ooops, efitime: can't read time!\n");
178 real_seconds = nowtime % 60;
179 real_minutes = nowtime / 60;
181 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
185 eft.minute = real_minutes;
186 eft.second = real_seconds;
188 if (status != EFI_SUCCESS) {
189 printk("Ooops: efitime: can't read time!\n");
195 * This should only be used during kernel init and before runtime
196 * services have been remapped, therefore, we'll need to call in physical
197 * mode. Note, this call isn't used later, so mark it __init.
199 inline unsigned long __init efi_get_time(void)
205 status = phys_efi_get_time(&eft, &cap);
206 if (status != EFI_SUCCESS)
207 printk("Oops: efitime: can't read time status: 0x%lx\n",status);
209 return mktime(eft.year, eft.month, eft.day, eft.hour,
210 eft.minute, eft.second);
213 int is_available_memory(efi_memory_desc_t * md)
215 if (!(md->attribute & EFI_MEMORY_WB))
219 case EFI_LOADER_CODE:
220 case EFI_LOADER_DATA:
221 case EFI_BOOT_SERVICES_CODE:
222 case EFI_BOOT_SERVICES_DATA:
223 case EFI_CONVENTIONAL_MEMORY:
230 * We need to map the EFI memory map again after paging_init().
232 void __init efi_map_memmap(void)
236 memmap.map = (efi_memory_desc_t *)
237 bt_ioremap((unsigned long) memmap.phys_map,
238 (memmap.nr_map * sizeof(efi_memory_desc_t)));
240 if (memmap.map == NULL)
241 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
245 static void __init print_efi_memmap(void)
247 efi_memory_desc_t *md;
250 for (i = 0; i < memmap.nr_map; i++) {
252 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
253 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
254 i, md->type, md->attribute, md->phys_addr,
255 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
256 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
259 #endif /* EFI_DEBUG */
262 * Walks the EFI memory map and calls CALLBACK once for each EFI
263 * memory descriptor that has memory that is available for kernel use.
265 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
272 efi_memory_desc_t *md;
273 unsigned long start, end;
276 for (i = 0; i < memmap.nr_map; i++) {
279 if ((md->num_pages == 0) || (!is_available_memory(md)))
282 curr.start = md->phys_addr;
283 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
289 if (curr.start < prev.start)
290 printk(KERN_INFO PFX "Unordered memory map\n");
291 if (prev.end == curr.start)
295 (unsigned long) (PAGE_ALIGN(prev.start));
296 end = (unsigned long) (prev.end & PAGE_MASK);
298 && (*callback) (start, end, arg) < 0)
305 start = (unsigned long) PAGE_ALIGN(prev.start);
306 end = (unsigned long) (prev.end & PAGE_MASK);
308 (*callback) (start, end, arg);
312 void __init efi_init(void)
314 efi_config_table_t *config_tables;
315 efi_runtime_services_t *runtime;
317 char vendor[100] = "unknown";
318 unsigned long num_config_tables;
321 memset(&efi, 0, sizeof(efi) );
322 memset(&efi_phys, 0, sizeof(efi_phys));
324 efi_phys.systab = EFI_SYSTAB;
325 memmap.phys_map = EFI_MEMMAP;
326 memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
327 memmap.desc_version = EFI_MEMDESC_VERSION;
329 efi.systab = (efi_system_table_t *)
330 boot_ioremap((unsigned long) efi_phys.systab,
331 sizeof(efi_system_table_t));
333 * Verify the EFI Table
335 if (efi.systab == NULL)
336 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
337 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
338 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
339 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
341 "Warning: EFI system table major version mismatch: "
342 "got %d.%02d, expected %d.%02d\n",
343 efi.systab->hdr.revision >> 16,
344 efi.systab->hdr.revision & 0xffff,
345 EFI_SYSTEM_TABLE_REVISION >> 16,
346 EFI_SYSTEM_TABLE_REVISION & 0xffff);
348 * Grab some details from the system table
350 num_config_tables = efi.systab->nr_tables;
351 config_tables = (efi_config_table_t *)efi.systab->tables;
352 runtime = efi.systab->runtime;
355 * Show what we know for posterity
357 c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
359 for (i = 0; i < sizeof(vendor) && *c16; ++i)
363 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
365 printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
366 efi.systab->hdr.revision >> 16,
367 efi.systab->hdr.revision & 0xffff, vendor);
370 * Let's see what config tables the firmware passed to us.
372 config_tables = (efi_config_table_t *)
373 boot_ioremap((unsigned long) config_tables,
374 num_config_tables * sizeof(efi_config_table_t));
376 if (config_tables == NULL)
377 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
379 for (i = 0; i < num_config_tables; i++) {
380 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
381 efi.mps = (void *)config_tables[i].table;
382 printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
384 if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
385 efi.acpi20 = __va(config_tables[i].table);
386 printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
388 if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
389 efi.acpi = __va(config_tables[i].table);
390 printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
392 if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
393 efi.smbios = (void *) config_tables[i].table;
394 printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
396 if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
397 efi.hcdp = (void *)config_tables[i].table;
398 printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
400 if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
401 efi.uga = (void *)config_tables[i].table;
402 printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
408 * Check out the runtime services table. We need to map
409 * the runtime services table so that we can grab the physical
410 * address of several of the EFI runtime functions, needed to
411 * set the firmware into virtual mode.
414 runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
416 sizeof(efi_runtime_services_t));
417 if (runtime != NULL) {
419 * We will only need *early* access to the following
420 * two EFI runtime services before set_virtual_address_map
423 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
424 efi_phys.set_virtual_address_map =
425 (efi_set_virtual_address_map_t *)
426 runtime->set_virtual_address_map;
428 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
430 /* Map the EFI memory map for use until paging_init() */
432 memmap.map = (efi_memory_desc_t *)
433 boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
435 if (memmap.map == NULL)
436 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
438 if (EFI_MEMDESC_SIZE != sizeof(efi_memory_desc_t)) {
439 printk(KERN_WARNING PFX "Warning! Kernel-defined memdesc doesn't "
440 "match the one from EFI!\n");
448 * This function will switch the EFI runtime services to virtual mode.
449 * Essentially, look through the EFI memmap and map every region that
450 * has the runtime attribute bit set in its memory descriptor and update
451 * that memory descriptor with the virtual address obtained from ioremap().
452 * This enables the runtime services to be called without having to
453 * thunk back into physical mode for every invocation.
456 void __init efi_enter_virtual_mode(void)
458 efi_memory_desc_t *md;
464 for (i = 0; i < memmap.nr_map; i++) {
467 if (md->attribute & EFI_MEMORY_RUNTIME) {
469 (unsigned long)ioremap(md->phys_addr,
470 md->num_pages << EFI_PAGE_SHIFT);
471 if (!(unsigned long)md->virt_addr) {
472 printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
473 (unsigned long)md->phys_addr);
476 if (((unsigned long)md->phys_addr <=
477 (unsigned long)efi_phys.systab) &&
478 ((unsigned long)efi_phys.systab <
480 ((unsigned long)md->num_pages <<
484 addr = md->virt_addr - md->phys_addr +
485 (unsigned long)efi_phys.systab;
486 efi.systab = (efi_system_table_t *)addr;
494 status = phys_efi_set_virtual_address_map(
495 sizeof(efi_memory_desc_t) * memmap.nr_map,
496 sizeof(efi_memory_desc_t),
500 if (status != EFI_SUCCESS) {
501 printk (KERN_ALERT "You are screwed! "
502 "Unable to switch EFI into virtual mode "
503 "(status=%lx)\n", status);
504 panic("EFI call to SetVirtualAddressMap() failed!");
508 * Now that EFI is in virtual mode, update the function
509 * pointers in the runtime service table to the new virtual addresses.
512 efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
513 efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
514 efi.get_wakeup_time = (efi_get_wakeup_time_t *)
515 efi.systab->runtime->get_wakeup_time;
516 efi.set_wakeup_time = (efi_set_wakeup_time_t *)
517 efi.systab->runtime->set_wakeup_time;
518 efi.get_variable = (efi_get_variable_t *)
519 efi.systab->runtime->get_variable;
520 efi.get_next_variable = (efi_get_next_variable_t *)
521 efi.systab->runtime->get_next_variable;
522 efi.set_variable = (efi_set_variable_t *)
523 efi.systab->runtime->set_variable;
524 efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
525 efi.systab->runtime->get_next_high_mono_count;
526 efi.reset_system = (efi_reset_system_t *)
527 efi.systab->runtime->reset_system;
531 efi_initialize_iomem_resources(struct resource *code_resource,
532 struct resource *data_resource)
534 struct resource *res;
535 efi_memory_desc_t *md;
538 for (i = 0; i < memmap.nr_map; i++) {
541 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
544 res = alloc_bootmem_low(sizeof(struct resource));
546 case EFI_RESERVED_TYPE:
547 res->name = "Reserved Memory";
549 case EFI_LOADER_CODE:
550 res->name = "Loader Code";
552 case EFI_LOADER_DATA:
553 res->name = "Loader Data";
555 case EFI_BOOT_SERVICES_DATA:
556 res->name = "BootServices Data";
558 case EFI_BOOT_SERVICES_CODE:
559 res->name = "BootServices Code";
561 case EFI_RUNTIME_SERVICES_CODE:
562 res->name = "Runtime Service Code";
564 case EFI_RUNTIME_SERVICES_DATA:
565 res->name = "Runtime Service Data";
567 case EFI_CONVENTIONAL_MEMORY:
568 res->name = "Conventional Memory";
570 case EFI_UNUSABLE_MEMORY:
571 res->name = "Unusable Memory";
573 case EFI_ACPI_RECLAIM_MEMORY:
574 res->name = "ACPI Reclaim";
576 case EFI_ACPI_MEMORY_NVS:
577 res->name = "ACPI NVS";
579 case EFI_MEMORY_MAPPED_IO:
580 res->name = "Memory Mapped IO";
582 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
583 res->name = "Memory Mapped IO Port Space";
586 res->name = "Reserved";
589 res->start = md->phys_addr;
590 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
591 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
592 if (request_resource(&iomem_resource, res) < 0)
593 printk(KERN_ERR PFX "Failed to allocate res %s : 0x%lx-0x%lx\n",
594 res->name, res->start, res->end);
596 * We don't know which region contains kernel data so we try
597 * it repeatedly and let the resource manager test it.
599 if (md->type == EFI_CONVENTIONAL_MEMORY) {
600 request_resource(res, code_resource);
601 request_resource(res, data_resource);
603 request_resource(res, &crashk_res);
610 * Convenience functions to obtain memory types and attributes
613 u32 efi_mem_type(unsigned long phys_addr)
615 efi_memory_desc_t *md;
618 for (i = 0; i < memmap.nr_map; i++) {
620 if ((md->phys_addr <= phys_addr) && (phys_addr <
621 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
627 u64 efi_mem_attributes(unsigned long phys_addr)
629 efi_memory_desc_t *md;
632 for (i = 0; i < memmap.nr_map; i++) {
634 if ((md->phys_addr <= phys_addr) && (phys_addr <
635 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
636 return md->attribute;