[PATCH] i386: call efi_get_time during suspend
[linux-2.6/linux-2.6-openrd.git] / arch / i386 / kernel / efi.c
blobb92c7f0a358aa79836a2ccbc56cd7bf386555a80
1 /*
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>
24 #include <linux/mm.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>
35 #include <asm/io.h>
36 #include <asm/page.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/desc.h>
40 #include <asm/tlbflush.h>
42 #define EFI_DEBUG 0
43 #define PFX "EFI: "
45 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
47 struct efi efi;
48 EXPORT_SYMBOL(efi);
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) __acquires(efi_rt_lock)
70 unsigned long cr4;
71 unsigned long temp;
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
82 * page directory.
84 cr4 = read_cr4();
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;
91 } else {
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.
106 local_flush_tlb();
108 cpu_gdt_descr->address = __pa(cpu_gdt_descr->address);
109 load_gdt(cpu_gdt_descr);
112 static void efi_call_phys_epilog(void) __releases(efi_rt_lock)
114 unsigned long cr4;
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);
120 cr4 = read_cr4();
122 if (cr4 & X86_CR4_PSE) {
123 swapper_pg_dir[pgd_index(0)].pgd =
124 efi_bak_pg_dir_pointer[0].pgd;
125 } else {
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.
135 local_flush_tlb();
137 local_irq_restore(efi_rt_eflags);
138 spin_unlock(&efi_rt_lock);
141 static efi_status_t
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)
147 efi_status_t status;
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();
154 return status;
157 static efi_status_t
158 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
160 efi_status_t status;
162 efi_call_phys_prelog();
163 status = efi_call_phys(efi_phys.get_time, tm, tc);
164 efi_call_phys_epilog();
165 return status;
168 inline int efi_set_rtc_mmss(unsigned long nowtime)
170 int real_seconds, real_minutes;
171 efi_status_t status;
172 efi_time_t eft;
173 efi_time_cap_t cap;
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)
184 real_minutes += 30;
185 real_minutes %= 60;
187 eft.minute = real_minutes;
188 eft.second = real_seconds;
190 if (status != EFI_SUCCESS) {
191 printk("Ooops: efitime: can't read time!\n");
192 return -1;
194 return 0;
197 * This is used during kernel init before runtime
198 * services have been remapped and also during suspend, therefore,
199 * we'll need to call both in physical and virtual modes.
201 inline unsigned long efi_get_time(void)
203 efi_status_t status;
204 efi_time_t eft;
205 efi_time_cap_t cap;
207 if (efi.get_time) {
208 /* if we are in virtual mode use remapped function */
209 status = efi.get_time(&eft, &cap);
210 } else {
211 /* we are in physical mode */
212 status = phys_efi_get_time(&eft, &cap);
215 if (status != EFI_SUCCESS)
216 printk("Oops: efitime: can't read time status: 0x%lx\n",status);
218 return mktime(eft.year, eft.month, eft.day, eft.hour,
219 eft.minute, eft.second);
222 int is_available_memory(efi_memory_desc_t * md)
224 if (!(md->attribute & EFI_MEMORY_WB))
225 return 0;
227 switch (md->type) {
228 case EFI_LOADER_CODE:
229 case EFI_LOADER_DATA:
230 case EFI_BOOT_SERVICES_CODE:
231 case EFI_BOOT_SERVICES_DATA:
232 case EFI_CONVENTIONAL_MEMORY:
233 return 1;
235 return 0;
239 * We need to map the EFI memory map again after paging_init().
241 void __init efi_map_memmap(void)
243 memmap.map = NULL;
245 memmap.map = bt_ioremap((unsigned long) memmap.phys_map,
246 (memmap.nr_map * memmap.desc_size));
247 if (memmap.map == NULL)
248 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
250 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
253 #if EFI_DEBUG
254 static void __init print_efi_memmap(void)
256 efi_memory_desc_t *md;
257 void *p;
258 int i;
260 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
261 md = p;
262 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
263 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
264 i, md->type, md->attribute, md->phys_addr,
265 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
266 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
269 #endif /* EFI_DEBUG */
272 * Walks the EFI memory map and calls CALLBACK once for each EFI
273 * memory descriptor that has memory that is available for kernel use.
275 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
277 int prev_valid = 0;
278 struct range {
279 unsigned long start;
280 unsigned long end;
281 } prev, curr;
282 efi_memory_desc_t *md;
283 unsigned long start, end;
284 void *p;
286 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
287 md = p;
289 if ((md->num_pages == 0) || (!is_available_memory(md)))
290 continue;
292 curr.start = md->phys_addr;
293 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
295 if (!prev_valid) {
296 prev = curr;
297 prev_valid = 1;
298 } else {
299 if (curr.start < prev.start)
300 printk(KERN_INFO PFX "Unordered memory map\n");
301 if (prev.end == curr.start)
302 prev.end = curr.end;
303 else {
304 start =
305 (unsigned long) (PAGE_ALIGN(prev.start));
306 end = (unsigned long) (prev.end & PAGE_MASK);
307 if ((end > start)
308 && (*callback) (start, end, arg) < 0)
309 return;
310 prev = curr;
314 if (prev_valid) {
315 start = (unsigned long) PAGE_ALIGN(prev.start);
316 end = (unsigned long) (prev.end & PAGE_MASK);
317 if (end > start)
318 (*callback) (start, end, arg);
322 void __init efi_init(void)
324 efi_config_table_t *config_tables;
325 efi_runtime_services_t *runtime;
326 efi_char16_t *c16;
327 char vendor[100] = "unknown";
328 unsigned long num_config_tables;
329 int i = 0;
331 memset(&efi, 0, sizeof(efi) );
332 memset(&efi_phys, 0, sizeof(efi_phys));
334 efi_phys.systab = EFI_SYSTAB;
335 memmap.phys_map = EFI_MEMMAP;
336 memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
337 memmap.desc_version = EFI_MEMDESC_VERSION;
338 memmap.desc_size = EFI_MEMDESC_SIZE;
340 efi.systab = (efi_system_table_t *)
341 boot_ioremap((unsigned long) efi_phys.systab,
342 sizeof(efi_system_table_t));
344 * Verify the EFI Table
346 if (efi.systab == NULL)
347 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
348 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
349 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
350 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
351 printk(KERN_ERR PFX
352 "Warning: EFI system table major version mismatch: "
353 "got %d.%02d, expected %d.%02d\n",
354 efi.systab->hdr.revision >> 16,
355 efi.systab->hdr.revision & 0xffff,
356 EFI_SYSTEM_TABLE_REVISION >> 16,
357 EFI_SYSTEM_TABLE_REVISION & 0xffff);
359 * Grab some details from the system table
361 num_config_tables = efi.systab->nr_tables;
362 config_tables = (efi_config_table_t *)efi.systab->tables;
363 runtime = efi.systab->runtime;
366 * Show what we know for posterity
368 c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
369 if (c16) {
370 for (i = 0; i < (sizeof(vendor) - 1) && *c16; ++i)
371 vendor[i] = *c16++;
372 vendor[i] = '\0';
373 } else
374 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
376 printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
377 efi.systab->hdr.revision >> 16,
378 efi.systab->hdr.revision & 0xffff, vendor);
381 * Let's see what config tables the firmware passed to us.
383 config_tables = (efi_config_table_t *)
384 boot_ioremap((unsigned long) config_tables,
385 num_config_tables * sizeof(efi_config_table_t));
387 if (config_tables == NULL)
388 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
390 efi.mps = EFI_INVALID_TABLE_ADDR;
391 efi.acpi = EFI_INVALID_TABLE_ADDR;
392 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
393 efi.smbios = EFI_INVALID_TABLE_ADDR;
394 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
395 efi.boot_info = EFI_INVALID_TABLE_ADDR;
396 efi.hcdp = EFI_INVALID_TABLE_ADDR;
397 efi.uga = EFI_INVALID_TABLE_ADDR;
399 for (i = 0; i < num_config_tables; i++) {
400 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
401 efi.mps = config_tables[i].table;
402 printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
403 } else
404 if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
405 efi.acpi20 = config_tables[i].table;
406 printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
407 } else
408 if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
409 efi.acpi = config_tables[i].table;
410 printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
411 } else
412 if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
413 efi.smbios = config_tables[i].table;
414 printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
415 } else
416 if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
417 efi.hcdp = config_tables[i].table;
418 printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
419 } else
420 if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
421 efi.uga = config_tables[i].table;
422 printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
425 printk("\n");
428 * Check out the runtime services table. We need to map
429 * the runtime services table so that we can grab the physical
430 * address of several of the EFI runtime functions, needed to
431 * set the firmware into virtual mode.
434 runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
435 runtime,
436 sizeof(efi_runtime_services_t));
437 if (runtime != NULL) {
439 * We will only need *early* access to the following
440 * two EFI runtime services before set_virtual_address_map
441 * is invoked.
443 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
444 efi_phys.set_virtual_address_map =
445 (efi_set_virtual_address_map_t *)
446 runtime->set_virtual_address_map;
447 } else
448 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
450 /* Map the EFI memory map for use until paging_init() */
451 memmap.map = boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
452 if (memmap.map == NULL)
453 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
455 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
457 #if EFI_DEBUG
458 print_efi_memmap();
459 #endif
462 static inline void __init check_range_for_systab(efi_memory_desc_t *md)
464 if (((unsigned long)md->phys_addr <= (unsigned long)efi_phys.systab) &&
465 ((unsigned long)efi_phys.systab < md->phys_addr +
466 ((unsigned long)md->num_pages << EFI_PAGE_SHIFT))) {
467 unsigned long addr;
469 addr = md->virt_addr - md->phys_addr +
470 (unsigned long)efi_phys.systab;
471 efi.systab = (efi_system_table_t *)addr;
476 * This function will switch the EFI runtime services to virtual mode.
477 * Essentially, look through the EFI memmap and map every region that
478 * has the runtime attribute bit set in its memory descriptor and update
479 * that memory descriptor with the virtual address obtained from ioremap().
480 * This enables the runtime services to be called without having to
481 * thunk back into physical mode for every invocation.
484 void __init efi_enter_virtual_mode(void)
486 efi_memory_desc_t *md;
487 efi_status_t status;
488 void *p;
490 efi.systab = NULL;
492 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
493 md = p;
495 if (!(md->attribute & EFI_MEMORY_RUNTIME))
496 continue;
498 md->virt_addr = (unsigned long)ioremap(md->phys_addr,
499 md->num_pages << EFI_PAGE_SHIFT);
500 if (!(unsigned long)md->virt_addr) {
501 printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
502 (unsigned long)md->phys_addr);
504 /* update the virtual address of the EFI system table */
505 check_range_for_systab(md);
508 BUG_ON(!efi.systab);
510 status = phys_efi_set_virtual_address_map(
511 memmap.desc_size * memmap.nr_map,
512 memmap.desc_size,
513 memmap.desc_version,
514 memmap.phys_map);
516 if (status != EFI_SUCCESS) {
517 printk (KERN_ALERT "You are screwed! "
518 "Unable to switch EFI into virtual mode "
519 "(status=%lx)\n", status);
520 panic("EFI call to SetVirtualAddressMap() failed!");
524 * Now that EFI is in virtual mode, update the function
525 * pointers in the runtime service table to the new virtual addresses.
528 efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
529 efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
530 efi.get_wakeup_time = (efi_get_wakeup_time_t *)
531 efi.systab->runtime->get_wakeup_time;
532 efi.set_wakeup_time = (efi_set_wakeup_time_t *)
533 efi.systab->runtime->set_wakeup_time;
534 efi.get_variable = (efi_get_variable_t *)
535 efi.systab->runtime->get_variable;
536 efi.get_next_variable = (efi_get_next_variable_t *)
537 efi.systab->runtime->get_next_variable;
538 efi.set_variable = (efi_set_variable_t *)
539 efi.systab->runtime->set_variable;
540 efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
541 efi.systab->runtime->get_next_high_mono_count;
542 efi.reset_system = (efi_reset_system_t *)
543 efi.systab->runtime->reset_system;
546 void __init
547 efi_initialize_iomem_resources(struct resource *code_resource,
548 struct resource *data_resource)
550 struct resource *res;
551 efi_memory_desc_t *md;
552 void *p;
554 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
555 md = p;
557 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
558 0x100000000ULL)
559 continue;
560 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
561 switch (md->type) {
562 case EFI_RESERVED_TYPE:
563 res->name = "Reserved Memory";
564 break;
565 case EFI_LOADER_CODE:
566 res->name = "Loader Code";
567 break;
568 case EFI_LOADER_DATA:
569 res->name = "Loader Data";
570 break;
571 case EFI_BOOT_SERVICES_DATA:
572 res->name = "BootServices Data";
573 break;
574 case EFI_BOOT_SERVICES_CODE:
575 res->name = "BootServices Code";
576 break;
577 case EFI_RUNTIME_SERVICES_CODE:
578 res->name = "Runtime Service Code";
579 break;
580 case EFI_RUNTIME_SERVICES_DATA:
581 res->name = "Runtime Service Data";
582 break;
583 case EFI_CONVENTIONAL_MEMORY:
584 res->name = "Conventional Memory";
585 break;
586 case EFI_UNUSABLE_MEMORY:
587 res->name = "Unusable Memory";
588 break;
589 case EFI_ACPI_RECLAIM_MEMORY:
590 res->name = "ACPI Reclaim";
591 break;
592 case EFI_ACPI_MEMORY_NVS:
593 res->name = "ACPI NVS";
594 break;
595 case EFI_MEMORY_MAPPED_IO:
596 res->name = "Memory Mapped IO";
597 break;
598 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
599 res->name = "Memory Mapped IO Port Space";
600 break;
601 default:
602 res->name = "Reserved";
603 break;
605 res->start = md->phys_addr;
606 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
607 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
608 if (request_resource(&iomem_resource, res) < 0)
609 printk(KERN_ERR PFX "Failed to allocate res %s : "
610 "0x%llx-0x%llx\n", res->name,
611 (unsigned long long)res->start,
612 (unsigned long long)res->end);
614 * We don't know which region contains kernel data so we try
615 * it repeatedly and let the resource manager test it.
617 if (md->type == EFI_CONVENTIONAL_MEMORY) {
618 request_resource(res, code_resource);
619 request_resource(res, data_resource);
620 #ifdef CONFIG_KEXEC
621 request_resource(res, &crashk_res);
622 #endif
628 * Convenience functions to obtain memory types and attributes
631 u32 efi_mem_type(unsigned long phys_addr)
633 efi_memory_desc_t *md;
634 void *p;
636 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
637 md = p;
638 if ((md->phys_addr <= phys_addr) && (phys_addr <
639 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
640 return md->type;
642 return 0;
645 u64 efi_mem_attributes(unsigned long phys_addr)
647 efi_memory_desc_t *md;
648 void *p;
650 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
651 md = p;
652 if ((md->phys_addr <= phys_addr) && (phys_addr <
653 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
654 return md->attribute;
656 return 0;