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/kernel.h>
23 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/time.h>
27 #include <linux/spinlock.h>
28 #include <linux/bootmem.h>
29 #include <linux/ioport.h>
30 #include <linux/module.h>
31 #include <linux/efi.h>
32 #include <linux/kexec.h>
34 #include <asm/setup.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
40 #include <asm/tlbflush.h>
45 extern efi_status_t asmlinkage
efi_call_phys(void *, ...);
49 static struct efi efi_phys
;
50 struct efi_memory_map memmap
;
53 * We require an early boot_ioremap mapping mechanism initially
55 extern void * boot_ioremap(unsigned long, unsigned long);
58 * To make EFI call EFI runtime service in physical addressing mode we need
59 * prelog/epilog before/after the invocation to disable interrupt, to
60 * claim EFI runtime service handler exclusively and to duplicate a memory in
61 * low memory space say 0 - 3G.
64 static unsigned long efi_rt_eflags
;
65 static DEFINE_SPINLOCK(efi_rt_lock
);
66 static pgd_t efi_bak_pg_dir_pointer
[2];
68 static void efi_call_phys_prelog(void)
72 struct Xgt_desc_struct
*cpu_gdt_descr
;
74 spin_lock(&efi_rt_lock
);
75 local_irq_save(efi_rt_eflags
);
77 cpu_gdt_descr
= &per_cpu(cpu_gdt_descr
, 0);
80 * If I don't have PSE, I should just duplicate two entries in page
81 * directory. If I have PSE, I just need to duplicate one entry in
86 if (cr4
& X86_CR4_PSE
) {
87 efi_bak_pg_dir_pointer
[0].pgd
=
88 swapper_pg_dir
[pgd_index(0)].pgd
;
89 swapper_pg_dir
[0].pgd
=
90 swapper_pg_dir
[pgd_index(PAGE_OFFSET
)].pgd
;
92 efi_bak_pg_dir_pointer
[0].pgd
=
93 swapper_pg_dir
[pgd_index(0)].pgd
;
94 efi_bak_pg_dir_pointer
[1].pgd
=
95 swapper_pg_dir
[pgd_index(0x400000)].pgd
;
96 swapper_pg_dir
[pgd_index(0)].pgd
=
97 swapper_pg_dir
[pgd_index(PAGE_OFFSET
)].pgd
;
98 temp
= PAGE_OFFSET
+ 0x400000;
99 swapper_pg_dir
[pgd_index(0x400000)].pgd
=
100 swapper_pg_dir
[pgd_index(temp
)].pgd
;
104 * After the lock is released, the original page table is restored.
108 cpu_gdt_descr
->address
= __pa(cpu_gdt_descr
->address
);
109 load_gdt(cpu_gdt_descr
);
112 static void efi_call_phys_epilog(void)
115 struct Xgt_desc_struct
*cpu_gdt_descr
= &per_cpu(cpu_gdt_descr
, 0);
117 cpu_gdt_descr
->address
= (unsigned long)__va(cpu_gdt_descr
->address
);
118 load_gdt(cpu_gdt_descr
);
122 if (cr4
& X86_CR4_PSE
) {
123 swapper_pg_dir
[pgd_index(0)].pgd
=
124 efi_bak_pg_dir_pointer
[0].pgd
;
126 swapper_pg_dir
[pgd_index(0)].pgd
=
127 efi_bak_pg_dir_pointer
[0].pgd
;
128 swapper_pg_dir
[pgd_index(0x400000)].pgd
=
129 efi_bak_pg_dir_pointer
[1].pgd
;
133 * After the lock is released, the original page table is restored.
137 local_irq_restore(efi_rt_eflags
);
138 spin_unlock(&efi_rt_lock
);
142 phys_efi_set_virtual_address_map(unsigned long memory_map_size
,
143 unsigned long descriptor_size
,
144 u32 descriptor_version
,
145 efi_memory_desc_t
*virtual_map
)
149 efi_call_phys_prelog();
150 status
= efi_call_phys(efi_phys
.set_virtual_address_map
,
151 memory_map_size
, descriptor_size
,
152 descriptor_version
, virtual_map
);
153 efi_call_phys_epilog();
158 phys_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
162 efi_call_phys_prelog();
163 status
= efi_call_phys(efi_phys
.get_time
, tm
, tc
);
164 efi_call_phys_epilog();
168 inline int efi_set_rtc_mmss(unsigned long nowtime
)
170 int real_seconds
, real_minutes
;
175 spin_lock(&efi_rt_lock
);
176 status
= efi
.get_time(&eft
, &cap
);
177 spin_unlock(&efi_rt_lock
);
178 if (status
!= EFI_SUCCESS
)
179 panic("Ooops, efitime: can't read time!\n");
180 real_seconds
= nowtime
% 60;
181 real_minutes
= nowtime
/ 60;
183 if (((abs(real_minutes
- eft
.minute
) + 15)/30) & 1)
187 eft
.minute
= real_minutes
;
188 eft
.second
= real_seconds
;
190 if (status
!= EFI_SUCCESS
) {
191 printk("Ooops: efitime: can't read time!\n");
197 * This should only be used during kernel init and before runtime
198 * services have been remapped, therefore, we'll need to call in physical
199 * mode. Note, this call isn't used later, so mark it __init.
201 inline unsigned long __init
efi_get_time(void)
207 status
= phys_efi_get_time(&eft
, &cap
);
208 if (status
!= EFI_SUCCESS
)
209 printk("Oops: efitime: can't read time status: 0x%lx\n",status
);
211 return mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
212 eft
.minute
, eft
.second
);
215 int is_available_memory(efi_memory_desc_t
* md
)
217 if (!(md
->attribute
& EFI_MEMORY_WB
))
221 case EFI_LOADER_CODE
:
222 case EFI_LOADER_DATA
:
223 case EFI_BOOT_SERVICES_CODE
:
224 case EFI_BOOT_SERVICES_DATA
:
225 case EFI_CONVENTIONAL_MEMORY
:
232 * We need to map the EFI memory map again after paging_init().
234 void __init
efi_map_memmap(void)
238 memmap
.map
= bt_ioremap((unsigned long) memmap
.phys_map
,
239 (memmap
.nr_map
* memmap
.desc_size
));
240 if (memmap
.map
== NULL
)
241 printk(KERN_ERR PFX
"Could not remap the EFI memmap!\n");
243 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
247 static void __init
print_efi_memmap(void)
249 efi_memory_desc_t
*md
;
253 for (p
= memmap
.map
, i
= 0; p
< memmap
.map_end
; p
+= memmap
.desc_size
, i
++) {
255 printk(KERN_INFO
"mem%02u: type=%u, attr=0x%llx, "
256 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
257 i
, md
->type
, md
->attribute
, md
->phys_addr
,
258 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
259 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
262 #endif /* EFI_DEBUG */
265 * Walks the EFI memory map and calls CALLBACK once for each EFI
266 * memory descriptor that has memory that is available for kernel use.
268 void efi_memmap_walk(efi_freemem_callback_t callback
, void *arg
)
275 efi_memory_desc_t
*md
;
276 unsigned long start
, end
;
279 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
282 if ((md
->num_pages
== 0) || (!is_available_memory(md
)))
285 curr
.start
= md
->phys_addr
;
286 curr
.end
= curr
.start
+ (md
->num_pages
<< EFI_PAGE_SHIFT
);
292 if (curr
.start
< prev
.start
)
293 printk(KERN_INFO PFX
"Unordered memory map\n");
294 if (prev
.end
== curr
.start
)
298 (unsigned long) (PAGE_ALIGN(prev
.start
));
299 end
= (unsigned long) (prev
.end
& PAGE_MASK
);
301 && (*callback
) (start
, end
, arg
) < 0)
308 start
= (unsigned long) PAGE_ALIGN(prev
.start
);
309 end
= (unsigned long) (prev
.end
& PAGE_MASK
);
311 (*callback
) (start
, end
, arg
);
315 void __init
efi_init(void)
317 efi_config_table_t
*config_tables
;
318 efi_runtime_services_t
*runtime
;
320 char vendor
[100] = "unknown";
321 unsigned long num_config_tables
;
324 memset(&efi
, 0, sizeof(efi
) );
325 memset(&efi_phys
, 0, sizeof(efi_phys
));
327 efi_phys
.systab
= EFI_SYSTAB
;
328 memmap
.phys_map
= EFI_MEMMAP
;
329 memmap
.nr_map
= EFI_MEMMAP_SIZE
/EFI_MEMDESC_SIZE
;
330 memmap
.desc_version
= EFI_MEMDESC_VERSION
;
331 memmap
.desc_size
= EFI_MEMDESC_SIZE
;
333 efi
.systab
= (efi_system_table_t
*)
334 boot_ioremap((unsigned long) efi_phys
.systab
,
335 sizeof(efi_system_table_t
));
337 * Verify the EFI Table
339 if (efi
.systab
== NULL
)
340 printk(KERN_ERR PFX
"Woah! Couldn't map the EFI system table.\n");
341 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
342 printk(KERN_ERR PFX
"Woah! EFI system table signature incorrect\n");
343 if ((efi
.systab
->hdr
.revision
^ EFI_SYSTEM_TABLE_REVISION
) >> 16 != 0)
345 "Warning: EFI system table major version mismatch: "
346 "got %d.%02d, expected %d.%02d\n",
347 efi
.systab
->hdr
.revision
>> 16,
348 efi
.systab
->hdr
.revision
& 0xffff,
349 EFI_SYSTEM_TABLE_REVISION
>> 16,
350 EFI_SYSTEM_TABLE_REVISION
& 0xffff);
352 * Grab some details from the system table
354 num_config_tables
= efi
.systab
->nr_tables
;
355 config_tables
= (efi_config_table_t
*)efi
.systab
->tables
;
356 runtime
= efi
.systab
->runtime
;
359 * Show what we know for posterity
361 c16
= (efi_char16_t
*) boot_ioremap(efi
.systab
->fw_vendor
, 2);
363 for (i
= 0; i
< (sizeof(vendor
) - 1) && *c16
; ++i
)
367 printk(KERN_ERR PFX
"Could not map the firmware vendor!\n");
369 printk(KERN_INFO PFX
"EFI v%u.%.02u by %s \n",
370 efi
.systab
->hdr
.revision
>> 16,
371 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
374 * Let's see what config tables the firmware passed to us.
376 config_tables
= (efi_config_table_t
*)
377 boot_ioremap((unsigned long) config_tables
,
378 num_config_tables
* sizeof(efi_config_table_t
));
380 if (config_tables
== NULL
)
381 printk(KERN_ERR PFX
"Could not map EFI Configuration Table!\n");
383 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
384 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
385 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
386 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
387 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
388 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
389 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
390 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
392 for (i
= 0; i
< num_config_tables
; i
++) {
393 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
394 efi
.mps
= config_tables
[i
].table
;
395 printk(KERN_INFO
" MPS=0x%lx ", config_tables
[i
].table
);
397 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
398 efi
.acpi20
= config_tables
[i
].table
;
399 printk(KERN_INFO
" ACPI 2.0=0x%lx ", config_tables
[i
].table
);
401 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
402 efi
.acpi
= config_tables
[i
].table
;
403 printk(KERN_INFO
" ACPI=0x%lx ", config_tables
[i
].table
);
405 if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
406 efi
.smbios
= config_tables
[i
].table
;
407 printk(KERN_INFO
" SMBIOS=0x%lx ", config_tables
[i
].table
);
409 if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
410 efi
.hcdp
= config_tables
[i
].table
;
411 printk(KERN_INFO
" HCDP=0x%lx ", config_tables
[i
].table
);
413 if (efi_guidcmp(config_tables
[i
].guid
, UGA_IO_PROTOCOL_GUID
) == 0) {
414 efi
.uga
= config_tables
[i
].table
;
415 printk(KERN_INFO
" UGA=0x%lx ", config_tables
[i
].table
);
421 * Check out the runtime services table. We need to map
422 * the runtime services table so that we can grab the physical
423 * address of several of the EFI runtime functions, needed to
424 * set the firmware into virtual mode.
427 runtime
= (efi_runtime_services_t
*) boot_ioremap((unsigned long)
429 sizeof(efi_runtime_services_t
));
430 if (runtime
!= NULL
) {
432 * We will only need *early* access to the following
433 * two EFI runtime services before set_virtual_address_map
436 efi_phys
.get_time
= (efi_get_time_t
*) runtime
->get_time
;
437 efi_phys
.set_virtual_address_map
=
438 (efi_set_virtual_address_map_t
*)
439 runtime
->set_virtual_address_map
;
441 printk(KERN_ERR PFX
"Could not map the runtime service table!\n");
443 /* Map the EFI memory map for use until paging_init() */
444 memmap
.map
= boot_ioremap((unsigned long) EFI_MEMMAP
, EFI_MEMMAP_SIZE
);
445 if (memmap
.map
== NULL
)
446 printk(KERN_ERR PFX
"Could not map the EFI memory map!\n");
448 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
455 static inline void __init
check_range_for_systab(efi_memory_desc_t
*md
)
457 if (((unsigned long)md
->phys_addr
<= (unsigned long)efi_phys
.systab
) &&
458 ((unsigned long)efi_phys
.systab
< md
->phys_addr
+
459 ((unsigned long)md
->num_pages
<< EFI_PAGE_SHIFT
))) {
462 addr
= md
->virt_addr
- md
->phys_addr
+
463 (unsigned long)efi_phys
.systab
;
464 efi
.systab
= (efi_system_table_t
*)addr
;
469 * This function will switch the EFI runtime services to virtual mode.
470 * Essentially, look through the EFI memmap and map every region that
471 * has the runtime attribute bit set in its memory descriptor and update
472 * that memory descriptor with the virtual address obtained from ioremap().
473 * This enables the runtime services to be called without having to
474 * thunk back into physical mode for every invocation.
477 void __init
efi_enter_virtual_mode(void)
479 efi_memory_desc_t
*md
;
485 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
488 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
491 md
->virt_addr
= (unsigned long)ioremap(md
->phys_addr
,
492 md
->num_pages
<< EFI_PAGE_SHIFT
);
493 if (!(unsigned long)md
->virt_addr
) {
494 printk(KERN_ERR PFX
"ioremap of 0x%lX failed\n",
495 (unsigned long)md
->phys_addr
);
497 /* update the virtual address of the EFI system table */
498 check_range_for_systab(md
);
504 status
= phys_efi_set_virtual_address_map(
505 memmap
.desc_size
* memmap
.nr_map
,
510 if (status
!= EFI_SUCCESS
) {
511 printk (KERN_ALERT
"You are screwed! "
512 "Unable to switch EFI into virtual mode "
513 "(status=%lx)\n", status
);
514 panic("EFI call to SetVirtualAddressMap() failed!");
518 * Now that EFI is in virtual mode, update the function
519 * pointers in the runtime service table to the new virtual addresses.
522 efi
.get_time
= (efi_get_time_t
*) efi
.systab
->runtime
->get_time
;
523 efi
.set_time
= (efi_set_time_t
*) efi
.systab
->runtime
->set_time
;
524 efi
.get_wakeup_time
= (efi_get_wakeup_time_t
*)
525 efi
.systab
->runtime
->get_wakeup_time
;
526 efi
.set_wakeup_time
= (efi_set_wakeup_time_t
*)
527 efi
.systab
->runtime
->set_wakeup_time
;
528 efi
.get_variable
= (efi_get_variable_t
*)
529 efi
.systab
->runtime
->get_variable
;
530 efi
.get_next_variable
= (efi_get_next_variable_t
*)
531 efi
.systab
->runtime
->get_next_variable
;
532 efi
.set_variable
= (efi_set_variable_t
*)
533 efi
.systab
->runtime
->set_variable
;
534 efi
.get_next_high_mono_count
= (efi_get_next_high_mono_count_t
*)
535 efi
.systab
->runtime
->get_next_high_mono_count
;
536 efi
.reset_system
= (efi_reset_system_t
*)
537 efi
.systab
->runtime
->reset_system
;
541 efi_initialize_iomem_resources(struct resource
*code_resource
,
542 struct resource
*data_resource
)
544 struct resource
*res
;
545 efi_memory_desc_t
*md
;
548 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
551 if ((md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
)) >
554 res
= kzalloc(sizeof(struct resource
), GFP_ATOMIC
);
556 case EFI_RESERVED_TYPE
:
557 res
->name
= "Reserved Memory";
559 case EFI_LOADER_CODE
:
560 res
->name
= "Loader Code";
562 case EFI_LOADER_DATA
:
563 res
->name
= "Loader Data";
565 case EFI_BOOT_SERVICES_DATA
:
566 res
->name
= "BootServices Data";
568 case EFI_BOOT_SERVICES_CODE
:
569 res
->name
= "BootServices Code";
571 case EFI_RUNTIME_SERVICES_CODE
:
572 res
->name
= "Runtime Service Code";
574 case EFI_RUNTIME_SERVICES_DATA
:
575 res
->name
= "Runtime Service Data";
577 case EFI_CONVENTIONAL_MEMORY
:
578 res
->name
= "Conventional Memory";
580 case EFI_UNUSABLE_MEMORY
:
581 res
->name
= "Unusable Memory";
583 case EFI_ACPI_RECLAIM_MEMORY
:
584 res
->name
= "ACPI Reclaim";
586 case EFI_ACPI_MEMORY_NVS
:
587 res
->name
= "ACPI NVS";
589 case EFI_MEMORY_MAPPED_IO
:
590 res
->name
= "Memory Mapped IO";
592 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
593 res
->name
= "Memory Mapped IO Port Space";
596 res
->name
= "Reserved";
599 res
->start
= md
->phys_addr
;
600 res
->end
= res
->start
+ ((md
->num_pages
<< EFI_PAGE_SHIFT
) - 1);
601 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
602 if (request_resource(&iomem_resource
, res
) < 0)
603 printk(KERN_ERR PFX
"Failed to allocate res %s : "
604 "0x%llx-0x%llx\n", res
->name
,
605 (unsigned long long)res
->start
,
606 (unsigned long long)res
->end
);
608 * We don't know which region contains kernel data so we try
609 * it repeatedly and let the resource manager test it.
611 if (md
->type
== EFI_CONVENTIONAL_MEMORY
) {
612 request_resource(res
, code_resource
);
613 request_resource(res
, data_resource
);
615 request_resource(res
, &crashk_res
);
622 * Convenience functions to obtain memory types and attributes
625 u32
efi_mem_type(unsigned long phys_addr
)
627 efi_memory_desc_t
*md
;
630 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
632 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
633 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
639 u64
efi_mem_attributes(unsigned long phys_addr
)
641 efi_memory_desc_t
*md
;
644 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
646 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
647 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
648 return md
->attribute
;