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
= (unsigned long)__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
) - 1) && *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 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
385 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
386 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
387 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
388 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
389 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
390 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
391 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
393 for (i
= 0; i
< num_config_tables
; i
++) {
394 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
395 efi
.mps
= config_tables
[i
].table
;
396 printk(KERN_INFO
" MPS=0x%lx ", config_tables
[i
].table
);
398 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
399 efi
.acpi20
= config_tables
[i
].table
;
400 printk(KERN_INFO
" ACPI 2.0=0x%lx ", config_tables
[i
].table
);
402 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
403 efi
.acpi
= config_tables
[i
].table
;
404 printk(KERN_INFO
" ACPI=0x%lx ", config_tables
[i
].table
);
406 if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
407 efi
.smbios
= config_tables
[i
].table
;
408 printk(KERN_INFO
" SMBIOS=0x%lx ", config_tables
[i
].table
);
410 if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
411 efi
.hcdp
= config_tables
[i
].table
;
412 printk(KERN_INFO
" HCDP=0x%lx ", config_tables
[i
].table
);
414 if (efi_guidcmp(config_tables
[i
].guid
, UGA_IO_PROTOCOL_GUID
) == 0) {
415 efi
.uga
= config_tables
[i
].table
;
416 printk(KERN_INFO
" UGA=0x%lx ", config_tables
[i
].table
);
422 * Check out the runtime services table. We need to map
423 * the runtime services table so that we can grab the physical
424 * address of several of the EFI runtime functions, needed to
425 * set the firmware into virtual mode.
428 runtime
= (efi_runtime_services_t
*) boot_ioremap((unsigned long)
430 sizeof(efi_runtime_services_t
));
431 if (runtime
!= NULL
) {
433 * We will only need *early* access to the following
434 * two EFI runtime services before set_virtual_address_map
437 efi_phys
.get_time
= (efi_get_time_t
*) runtime
->get_time
;
438 efi_phys
.set_virtual_address_map
=
439 (efi_set_virtual_address_map_t
*)
440 runtime
->set_virtual_address_map
;
442 printk(KERN_ERR PFX
"Could not map the runtime service table!\n");
444 /* Map the EFI memory map for use until paging_init() */
445 memmap
.map
= boot_ioremap((unsigned long) EFI_MEMMAP
, EFI_MEMMAP_SIZE
);
446 if (memmap
.map
== NULL
)
447 printk(KERN_ERR PFX
"Could not map the EFI memory map!\n");
449 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
456 static inline void __init
check_range_for_systab(efi_memory_desc_t
*md
)
458 if (((unsigned long)md
->phys_addr
<= (unsigned long)efi_phys
.systab
) &&
459 ((unsigned long)efi_phys
.systab
< md
->phys_addr
+
460 ((unsigned long)md
->num_pages
<< EFI_PAGE_SHIFT
))) {
463 addr
= md
->virt_addr
- md
->phys_addr
+
464 (unsigned long)efi_phys
.systab
;
465 efi
.systab
= (efi_system_table_t
*)addr
;
470 * This function will switch the EFI runtime services to virtual mode.
471 * Essentially, look through the EFI memmap and map every region that
472 * has the runtime attribute bit set in its memory descriptor and update
473 * that memory descriptor with the virtual address obtained from ioremap().
474 * This enables the runtime services to be called without having to
475 * thunk back into physical mode for every invocation.
478 void __init
efi_enter_virtual_mode(void)
480 efi_memory_desc_t
*md
;
486 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
489 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
492 md
->virt_addr
= (unsigned long)ioremap(md
->phys_addr
,
493 md
->num_pages
<< EFI_PAGE_SHIFT
);
494 if (!(unsigned long)md
->virt_addr
) {
495 printk(KERN_ERR PFX
"ioremap of 0x%lX failed\n",
496 (unsigned long)md
->phys_addr
);
498 /* update the virtual address of the EFI system table */
499 check_range_for_systab(md
);
505 status
= phys_efi_set_virtual_address_map(
506 memmap
.desc_size
* memmap
.nr_map
,
511 if (status
!= EFI_SUCCESS
) {
512 printk (KERN_ALERT
"You are screwed! "
513 "Unable to switch EFI into virtual mode "
514 "(status=%lx)\n", status
);
515 panic("EFI call to SetVirtualAddressMap() failed!");
519 * Now that EFI is in virtual mode, update the function
520 * pointers in the runtime service table to the new virtual addresses.
523 efi
.get_time
= (efi_get_time_t
*) efi
.systab
->runtime
->get_time
;
524 efi
.set_time
= (efi_set_time_t
*) efi
.systab
->runtime
->set_time
;
525 efi
.get_wakeup_time
= (efi_get_wakeup_time_t
*)
526 efi
.systab
->runtime
->get_wakeup_time
;
527 efi
.set_wakeup_time
= (efi_set_wakeup_time_t
*)
528 efi
.systab
->runtime
->set_wakeup_time
;
529 efi
.get_variable
= (efi_get_variable_t
*)
530 efi
.systab
->runtime
->get_variable
;
531 efi
.get_next_variable
= (efi_get_next_variable_t
*)
532 efi
.systab
->runtime
->get_next_variable
;
533 efi
.set_variable
= (efi_set_variable_t
*)
534 efi
.systab
->runtime
->set_variable
;
535 efi
.get_next_high_mono_count
= (efi_get_next_high_mono_count_t
*)
536 efi
.systab
->runtime
->get_next_high_mono_count
;
537 efi
.reset_system
= (efi_reset_system_t
*)
538 efi
.systab
->runtime
->reset_system
;
542 efi_initialize_iomem_resources(struct resource
*code_resource
,
543 struct resource
*data_resource
)
545 struct resource
*res
;
546 efi_memory_desc_t
*md
;
549 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
552 if ((md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
)) >
555 res
= kzalloc(sizeof(struct resource
), GFP_ATOMIC
);
557 case EFI_RESERVED_TYPE
:
558 res
->name
= "Reserved Memory";
560 case EFI_LOADER_CODE
:
561 res
->name
= "Loader Code";
563 case EFI_LOADER_DATA
:
564 res
->name
= "Loader Data";
566 case EFI_BOOT_SERVICES_DATA
:
567 res
->name
= "BootServices Data";
569 case EFI_BOOT_SERVICES_CODE
:
570 res
->name
= "BootServices Code";
572 case EFI_RUNTIME_SERVICES_CODE
:
573 res
->name
= "Runtime Service Code";
575 case EFI_RUNTIME_SERVICES_DATA
:
576 res
->name
= "Runtime Service Data";
578 case EFI_CONVENTIONAL_MEMORY
:
579 res
->name
= "Conventional Memory";
581 case EFI_UNUSABLE_MEMORY
:
582 res
->name
= "Unusable Memory";
584 case EFI_ACPI_RECLAIM_MEMORY
:
585 res
->name
= "ACPI Reclaim";
587 case EFI_ACPI_MEMORY_NVS
:
588 res
->name
= "ACPI NVS";
590 case EFI_MEMORY_MAPPED_IO
:
591 res
->name
= "Memory Mapped IO";
593 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
594 res
->name
= "Memory Mapped IO Port Space";
597 res
->name
= "Reserved";
600 res
->start
= md
->phys_addr
;
601 res
->end
= res
->start
+ ((md
->num_pages
<< EFI_PAGE_SHIFT
) - 1);
602 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
603 if (request_resource(&iomem_resource
, res
) < 0)
604 printk(KERN_ERR PFX
"Failed to allocate res %s : 0x%lx-0x%lx\n",
605 res
->name
, res
->start
, res
->end
);
607 * We don't know which region contains kernel data so we try
608 * it repeatedly and let the resource manager test it.
610 if (md
->type
== EFI_CONVENTIONAL_MEMORY
) {
611 request_resource(res
, code_resource
);
612 request_resource(res
, data_resource
);
614 request_resource(res
, &crashk_res
);
621 * Convenience functions to obtain memory types and attributes
624 u32
efi_mem_type(unsigned long phys_addr
)
626 efi_memory_desc_t
*md
;
629 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
631 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
632 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
638 u64
efi_mem_attributes(unsigned long phys_addr
)
640 efi_memory_desc_t
*md
;
643 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
645 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
646 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
647 return md
->attribute
;