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>
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)
73 spin_lock(&efi_rt_lock
);
74 local_irq_save(efi_rt_eflags
);
77 * If I don't have PSE, I should just duplicate two entries in page
78 * directory. If I have PSE, I just need to duplicate one entry in
81 __asm__
__volatile__("movl %%cr4, %0":"=r"(cr4
));
83 if (cr4
& X86_CR4_PSE
) {
84 efi_bak_pg_dir_pointer
[0].pgd
=
85 swapper_pg_dir
[pgd_index(0)].pgd
;
86 swapper_pg_dir
[0].pgd
=
87 swapper_pg_dir
[pgd_index(PAGE_OFFSET
)].pgd
;
89 efi_bak_pg_dir_pointer
[0].pgd
=
90 swapper_pg_dir
[pgd_index(0)].pgd
;
91 efi_bak_pg_dir_pointer
[1].pgd
=
92 swapper_pg_dir
[pgd_index(0x400000)].pgd
;
93 swapper_pg_dir
[pgd_index(0)].pgd
=
94 swapper_pg_dir
[pgd_index(PAGE_OFFSET
)].pgd
;
95 temp
= PAGE_OFFSET
+ 0x400000;
96 swapper_pg_dir
[pgd_index(0x400000)].pgd
=
97 swapper_pg_dir
[pgd_index(temp
)].pgd
;
101 * After the lock is released, the original page table is restored.
105 cpu_gdt_descr
[0].address
= __pa(cpu_gdt_descr
[0].address
);
106 __asm__
__volatile__("lgdt %0":"=m"
107 (*(struct Xgt_desc_struct
*) __pa(&cpu_gdt_descr
[0])));
110 static void efi_call_phys_epilog(void)
114 cpu_gdt_descr
[0].address
=
115 (unsigned long) __va(cpu_gdt_descr
[0].address
);
116 __asm__
__volatile__("lgdt %0":"=m"(cpu_gdt_descr
));
117 __asm__
__volatile__("movl %%cr4, %0":"=r"(cr4
));
119 if (cr4
& X86_CR4_PSE
) {
120 swapper_pg_dir
[pgd_index(0)].pgd
=
121 efi_bak_pg_dir_pointer
[0].pgd
;
123 swapper_pg_dir
[pgd_index(0)].pgd
=
124 efi_bak_pg_dir_pointer
[0].pgd
;
125 swapper_pg_dir
[pgd_index(0x400000)].pgd
=
126 efi_bak_pg_dir_pointer
[1].pgd
;
130 * After the lock is released, the original page table is restored.
134 local_irq_restore(efi_rt_eflags
);
135 spin_unlock(&efi_rt_lock
);
139 phys_efi_set_virtual_address_map(unsigned long memory_map_size
,
140 unsigned long descriptor_size
,
141 u32 descriptor_version
,
142 efi_memory_desc_t
*virtual_map
)
146 efi_call_phys_prelog();
147 status
= efi_call_phys(efi_phys
.set_virtual_address_map
,
148 memory_map_size
, descriptor_size
,
149 descriptor_version
, virtual_map
);
150 efi_call_phys_epilog();
155 phys_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
159 efi_call_phys_prelog();
160 status
= efi_call_phys(efi_phys
.get_time
, tm
, tc
);
161 efi_call_phys_epilog();
165 inline int efi_set_rtc_mmss(unsigned long nowtime
)
167 int real_seconds
, real_minutes
;
172 spin_lock(&efi_rt_lock
);
173 status
= efi
.get_time(&eft
, &cap
);
174 spin_unlock(&efi_rt_lock
);
175 if (status
!= EFI_SUCCESS
)
176 panic("Ooops, efitime: can't read time!\n");
177 real_seconds
= nowtime
% 60;
178 real_minutes
= nowtime
/ 60;
180 if (((abs(real_minutes
- eft
.minute
) + 15)/30) & 1)
184 eft
.minute
= real_minutes
;
185 eft
.second
= real_seconds
;
187 if (status
!= EFI_SUCCESS
) {
188 printk("Ooops: efitime: can't read time!\n");
194 * This should only be used during kernel init and before runtime
195 * services have been remapped, therefore, we'll need to call in physical
196 * mode. Note, this call isn't used later, so mark it __init.
198 inline unsigned long __init
efi_get_time(void)
204 status
= phys_efi_get_time(&eft
, &cap
);
205 if (status
!= EFI_SUCCESS
)
206 printk("Oops: efitime: can't read time status: 0x%lx\n",status
);
208 return mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
209 eft
.minute
, eft
.second
);
212 int is_available_memory(efi_memory_desc_t
* md
)
214 if (!(md
->attribute
& EFI_MEMORY_WB
))
218 case EFI_LOADER_CODE
:
219 case EFI_LOADER_DATA
:
220 case EFI_BOOT_SERVICES_CODE
:
221 case EFI_BOOT_SERVICES_DATA
:
222 case EFI_CONVENTIONAL_MEMORY
:
229 * We need to map the EFI memory map again after paging_init().
231 void __init
efi_map_memmap(void)
235 memmap
.map
= (efi_memory_desc_t
*)
236 bt_ioremap((unsigned long) memmap
.phys_map
,
237 (memmap
.nr_map
* sizeof(efi_memory_desc_t
)));
239 if (memmap
.map
== NULL
)
240 printk(KERN_ERR PFX
"Could not remap the EFI memmap!\n");
244 static void __init
print_efi_memmap(void)
246 efi_memory_desc_t
*md
;
249 for (i
= 0; i
< memmap
.nr_map
; i
++) {
251 printk(KERN_INFO
"mem%02u: type=%u, attr=0x%llx, "
252 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
253 i
, md
->type
, md
->attribute
, md
->phys_addr
,
254 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
255 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
258 #endif /* EFI_DEBUG */
261 * Walks the EFI memory map and calls CALLBACK once for each EFI
262 * memory descriptor that has memory that is available for kernel use.
264 void efi_memmap_walk(efi_freemem_callback_t callback
, void *arg
)
271 efi_memory_desc_t
*md
;
272 unsigned long start
, end
;
275 for (i
= 0; i
< memmap
.nr_map
; i
++) {
278 if ((md
->num_pages
== 0) || (!is_available_memory(md
)))
281 curr
.start
= md
->phys_addr
;
282 curr
.end
= curr
.start
+ (md
->num_pages
<< EFI_PAGE_SHIFT
);
288 if (curr
.start
< prev
.start
)
289 printk(KERN_INFO PFX
"Unordered memory map\n");
290 if (prev
.end
== curr
.start
)
294 (unsigned long) (PAGE_ALIGN(prev
.start
));
295 end
= (unsigned long) (prev
.end
& PAGE_MASK
);
297 && (*callback
) (start
, end
, arg
) < 0)
304 start
= (unsigned long) PAGE_ALIGN(prev
.start
);
305 end
= (unsigned long) (prev
.end
& PAGE_MASK
);
307 (*callback
) (start
, end
, arg
);
311 void __init
efi_init(void)
313 efi_config_table_t
*config_tables
;
314 efi_runtime_services_t
*runtime
;
316 char vendor
[100] = "unknown";
317 unsigned long num_config_tables
;
320 memset(&efi
, 0, sizeof(efi
) );
321 memset(&efi_phys
, 0, sizeof(efi_phys
));
323 efi_phys
.systab
= EFI_SYSTAB
;
324 memmap
.phys_map
= EFI_MEMMAP
;
325 memmap
.nr_map
= EFI_MEMMAP_SIZE
/EFI_MEMDESC_SIZE
;
326 memmap
.desc_version
= EFI_MEMDESC_VERSION
;
328 efi
.systab
= (efi_system_table_t
*)
329 boot_ioremap((unsigned long) efi_phys
.systab
,
330 sizeof(efi_system_table_t
));
332 * Verify the EFI Table
334 if (efi
.systab
== NULL
)
335 printk(KERN_ERR PFX
"Woah! Couldn't map the EFI system table.\n");
336 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
337 printk(KERN_ERR PFX
"Woah! EFI system table signature incorrect\n");
338 if ((efi
.systab
->hdr
.revision
^ EFI_SYSTEM_TABLE_REVISION
) >> 16 != 0)
340 "Warning: EFI system table major version mismatch: "
341 "got %d.%02d, expected %d.%02d\n",
342 efi
.systab
->hdr
.revision
>> 16,
343 efi
.systab
->hdr
.revision
& 0xffff,
344 EFI_SYSTEM_TABLE_REVISION
>> 16,
345 EFI_SYSTEM_TABLE_REVISION
& 0xffff);
347 * Grab some details from the system table
349 num_config_tables
= efi
.systab
->nr_tables
;
350 config_tables
= (efi_config_table_t
*)efi
.systab
->tables
;
351 runtime
= efi
.systab
->runtime
;
354 * Show what we know for posterity
356 c16
= (efi_char16_t
*) boot_ioremap(efi
.systab
->fw_vendor
, 2);
358 for (i
= 0; i
< sizeof(vendor
) && *c16
; ++i
)
362 printk(KERN_ERR PFX
"Could not map the firmware vendor!\n");
364 printk(KERN_INFO PFX
"EFI v%u.%.02u by %s \n",
365 efi
.systab
->hdr
.revision
>> 16,
366 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
369 * Let's see what config tables the firmware passed to us.
371 config_tables
= (efi_config_table_t
*)
372 boot_ioremap((unsigned long) config_tables
,
373 num_config_tables
* sizeof(efi_config_table_t
));
375 if (config_tables
== NULL
)
376 printk(KERN_ERR PFX
"Could not map EFI Configuration Table!\n");
378 for (i
= 0; i
< num_config_tables
; i
++) {
379 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
380 efi
.mps
= (void *)config_tables
[i
].table
;
381 printk(KERN_INFO
" MPS=0x%lx ", config_tables
[i
].table
);
383 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
384 efi
.acpi20
= __va(config_tables
[i
].table
);
385 printk(KERN_INFO
" ACPI 2.0=0x%lx ", config_tables
[i
].table
);
387 if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
388 efi
.acpi
= __va(config_tables
[i
].table
);
389 printk(KERN_INFO
" ACPI=0x%lx ", config_tables
[i
].table
);
391 if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
392 efi
.smbios
= (void *) config_tables
[i
].table
;
393 printk(KERN_INFO
" SMBIOS=0x%lx ", config_tables
[i
].table
);
395 if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
396 efi
.hcdp
= (void *)config_tables
[i
].table
;
397 printk(KERN_INFO
" HCDP=0x%lx ", config_tables
[i
].table
);
399 if (efi_guidcmp(config_tables
[i
].guid
, UGA_IO_PROTOCOL_GUID
) == 0) {
400 efi
.uga
= (void *)config_tables
[i
].table
;
401 printk(KERN_INFO
" UGA=0x%lx ", config_tables
[i
].table
);
407 * Check out the runtime services table. We need to map
408 * the runtime services table so that we can grab the physical
409 * address of several of the EFI runtime functions, needed to
410 * set the firmware into virtual mode.
413 runtime
= (efi_runtime_services_t
*) boot_ioremap((unsigned long)
415 sizeof(efi_runtime_services_t
));
416 if (runtime
!= NULL
) {
418 * We will only need *early* access to the following
419 * two EFI runtime services before set_virtual_address_map
422 efi_phys
.get_time
= (efi_get_time_t
*) runtime
->get_time
;
423 efi_phys
.set_virtual_address_map
=
424 (efi_set_virtual_address_map_t
*)
425 runtime
->set_virtual_address_map
;
427 printk(KERN_ERR PFX
"Could not map the runtime service table!\n");
429 /* Map the EFI memory map for use until paging_init() */
431 memmap
.map
= (efi_memory_desc_t
*)
432 boot_ioremap((unsigned long) EFI_MEMMAP
, EFI_MEMMAP_SIZE
);
434 if (memmap
.map
== NULL
)
435 printk(KERN_ERR PFX
"Could not map the EFI memory map!\n");
437 if (EFI_MEMDESC_SIZE
!= sizeof(efi_memory_desc_t
)) {
438 printk(KERN_WARNING PFX
"Warning! Kernel-defined memdesc doesn't "
439 "match the one from EFI!\n");
447 * This function will switch the EFI runtime services to virtual mode.
448 * Essentially, look through the EFI memmap and map every region that
449 * has the runtime attribute bit set in its memory descriptor and update
450 * that memory descriptor with the virtual address obtained from ioremap().
451 * This enables the runtime services to be called without having to
452 * thunk back into physical mode for every invocation.
455 void __init
efi_enter_virtual_mode(void)
457 efi_memory_desc_t
*md
;
463 for (i
= 0; i
< memmap
.nr_map
; i
++) {
466 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
468 (unsigned long)ioremap(md
->phys_addr
,
469 md
->num_pages
<< EFI_PAGE_SHIFT
);
470 if (!(unsigned long)md
->virt_addr
) {
471 printk(KERN_ERR PFX
"ioremap of 0x%lX failed\n",
472 (unsigned long)md
->phys_addr
);
475 if (((unsigned long)md
->phys_addr
<=
476 (unsigned long)efi_phys
.systab
) &&
477 ((unsigned long)efi_phys
.systab
<
479 ((unsigned long)md
->num_pages
<<
483 addr
= md
->virt_addr
- md
->phys_addr
+
484 (unsigned long)efi_phys
.systab
;
485 efi
.systab
= (efi_system_table_t
*)addr
;
493 status
= phys_efi_set_virtual_address_map(
494 sizeof(efi_memory_desc_t
) * memmap
.nr_map
,
495 sizeof(efi_memory_desc_t
),
499 if (status
!= EFI_SUCCESS
) {
500 printk (KERN_ALERT
"You are screwed! "
501 "Unable to switch EFI into virtual mode "
502 "(status=%lx)\n", status
);
503 panic("EFI call to SetVirtualAddressMap() failed!");
507 * Now that EFI is in virtual mode, update the function
508 * pointers in the runtime service table to the new virtual addresses.
511 efi
.get_time
= (efi_get_time_t
*) efi
.systab
->runtime
->get_time
;
512 efi
.set_time
= (efi_set_time_t
*) efi
.systab
->runtime
->set_time
;
513 efi
.get_wakeup_time
= (efi_get_wakeup_time_t
*)
514 efi
.systab
->runtime
->get_wakeup_time
;
515 efi
.set_wakeup_time
= (efi_set_wakeup_time_t
*)
516 efi
.systab
->runtime
->set_wakeup_time
;
517 efi
.get_variable
= (efi_get_variable_t
*)
518 efi
.systab
->runtime
->get_variable
;
519 efi
.get_next_variable
= (efi_get_next_variable_t
*)
520 efi
.systab
->runtime
->get_next_variable
;
521 efi
.set_variable
= (efi_set_variable_t
*)
522 efi
.systab
->runtime
->set_variable
;
523 efi
.get_next_high_mono_count
= (efi_get_next_high_mono_count_t
*)
524 efi
.systab
->runtime
->get_next_high_mono_count
;
525 efi
.reset_system
= (efi_reset_system_t
*)
526 efi
.systab
->runtime
->reset_system
;
530 efi_initialize_iomem_resources(struct resource
*code_resource
,
531 struct resource
*data_resource
)
533 struct resource
*res
;
534 efi_memory_desc_t
*md
;
537 for (i
= 0; i
< memmap
.nr_map
; i
++) {
540 if ((md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
)) >
543 res
= alloc_bootmem_low(sizeof(struct resource
));
545 case EFI_RESERVED_TYPE
:
546 res
->name
= "Reserved Memory";
548 case EFI_LOADER_CODE
:
549 res
->name
= "Loader Code";
551 case EFI_LOADER_DATA
:
552 res
->name
= "Loader Data";
554 case EFI_BOOT_SERVICES_DATA
:
555 res
->name
= "BootServices Data";
557 case EFI_BOOT_SERVICES_CODE
:
558 res
->name
= "BootServices Code";
560 case EFI_RUNTIME_SERVICES_CODE
:
561 res
->name
= "Runtime Service Code";
563 case EFI_RUNTIME_SERVICES_DATA
:
564 res
->name
= "Runtime Service Data";
566 case EFI_CONVENTIONAL_MEMORY
:
567 res
->name
= "Conventional Memory";
569 case EFI_UNUSABLE_MEMORY
:
570 res
->name
= "Unusable Memory";
572 case EFI_ACPI_RECLAIM_MEMORY
:
573 res
->name
= "ACPI Reclaim";
575 case EFI_ACPI_MEMORY_NVS
:
576 res
->name
= "ACPI NVS";
578 case EFI_MEMORY_MAPPED_IO
:
579 res
->name
= "Memory Mapped IO";
581 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
582 res
->name
= "Memory Mapped IO Port Space";
585 res
->name
= "Reserved";
588 res
->start
= md
->phys_addr
;
589 res
->end
= res
->start
+ ((md
->num_pages
<< EFI_PAGE_SHIFT
) - 1);
590 res
->flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
591 if (request_resource(&iomem_resource
, res
) < 0)
592 printk(KERN_ERR PFX
"Failed to allocate res %s : 0x%lx-0x%lx\n",
593 res
->name
, res
->start
, res
->end
);
595 * We don't know which region contains kernel data so we try
596 * it repeatedly and let the resource manager test it.
598 if (md
->type
== EFI_CONVENTIONAL_MEMORY
) {
599 request_resource(res
, code_resource
);
600 request_resource(res
, data_resource
);
606 * Convenience functions to obtain memory types and attributes
609 u32
efi_mem_type(unsigned long phys_addr
)
611 efi_memory_desc_t
*md
;
614 for (i
= 0; i
< memmap
.nr_map
; i
++) {
616 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
617 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
623 u64
efi_mem_attributes(unsigned long phys_addr
)
625 efi_memory_desc_t
*md
;
628 for (i
= 0; i
< memmap
.nr_map
; i
++) {
630 if ((md
->phys_addr
<= phys_addr
) && (phys_addr
<
631 (md
->phys_addr
+ (md
-> num_pages
<< EFI_PAGE_SHIFT
)) ))
632 return md
->attribute
;