BUG_ON cleanups in arch/i386
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / kernel / efi.c
blob8b40648d0ef00f2570ea2f19e07da3e1f851d41f
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 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)
203 efi_status_t status;
204 efi_time_t eft;
205 efi_time_cap_t cap;
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))
218 return 0;
220 switch (md->type) {
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:
226 return 1;
228 return 0;
232 * We need to map the EFI memory map again after paging_init().
234 void __init efi_map_memmap(void)
236 memmap.map = NULL;
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);
246 #if EFI_DEBUG
247 static void __init print_efi_memmap(void)
249 efi_memory_desc_t *md;
250 void *p;
251 int i;
253 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
254 md = p;
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)
270 int prev_valid = 0;
271 struct range {
272 unsigned long start;
273 unsigned long end;
274 } prev, curr;
275 efi_memory_desc_t *md;
276 unsigned long start, end;
277 void *p;
279 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
280 md = p;
282 if ((md->num_pages == 0) || (!is_available_memory(md)))
283 continue;
285 curr.start = md->phys_addr;
286 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
288 if (!prev_valid) {
289 prev = curr;
290 prev_valid = 1;
291 } else {
292 if (curr.start < prev.start)
293 printk(KERN_INFO PFX "Unordered memory map\n");
294 if (prev.end == curr.start)
295 prev.end = curr.end;
296 else {
297 start =
298 (unsigned long) (PAGE_ALIGN(prev.start));
299 end = (unsigned long) (prev.end & PAGE_MASK);
300 if ((end > start)
301 && (*callback) (start, end, arg) < 0)
302 return;
303 prev = curr;
307 if (prev_valid) {
308 start = (unsigned long) PAGE_ALIGN(prev.start);
309 end = (unsigned long) (prev.end & PAGE_MASK);
310 if (end > start)
311 (*callback) (start, end, arg);
315 void __init efi_init(void)
317 efi_config_table_t *config_tables;
318 efi_runtime_services_t *runtime;
319 efi_char16_t *c16;
320 char vendor[100] = "unknown";
321 unsigned long num_config_tables;
322 int i = 0;
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)
344 printk(KERN_ERR PFX
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);
362 if (c16) {
363 for (i = 0; i < (sizeof(vendor) - 1) && *c16; ++i)
364 vendor[i] = *c16++;
365 vendor[i] = '\0';
366 } else
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);
396 } else
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);
400 } else
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);
404 } else
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);
408 } else
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);
412 } else
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);
418 printk("\n");
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)
428 runtime,
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
434 * is invoked.
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;
440 } else
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);
450 #if EFI_DEBUG
451 print_efi_memmap();
452 #endif
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))) {
460 unsigned long addr;
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;
480 efi_status_t status;
481 void *p;
483 efi.systab = NULL;
485 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
486 md = p;
488 if (!(md->attribute & EFI_MEMORY_RUNTIME))
489 continue;
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);
501 BUG_ON(!efi.systab);
503 status = phys_efi_set_virtual_address_map(
504 memmap.desc_size * memmap.nr_map,
505 memmap.desc_size,
506 memmap.desc_version,
507 memmap.phys_map);
509 if (status != EFI_SUCCESS) {
510 printk (KERN_ALERT "You are screwed! "
511 "Unable to switch EFI into virtual mode "
512 "(status=%lx)\n", status);
513 panic("EFI call to SetVirtualAddressMap() failed!");
517 * Now that EFI is in virtual mode, update the function
518 * pointers in the runtime service table to the new virtual addresses.
521 efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
522 efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
523 efi.get_wakeup_time = (efi_get_wakeup_time_t *)
524 efi.systab->runtime->get_wakeup_time;
525 efi.set_wakeup_time = (efi_set_wakeup_time_t *)
526 efi.systab->runtime->set_wakeup_time;
527 efi.get_variable = (efi_get_variable_t *)
528 efi.systab->runtime->get_variable;
529 efi.get_next_variable = (efi_get_next_variable_t *)
530 efi.systab->runtime->get_next_variable;
531 efi.set_variable = (efi_set_variable_t *)
532 efi.systab->runtime->set_variable;
533 efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
534 efi.systab->runtime->get_next_high_mono_count;
535 efi.reset_system = (efi_reset_system_t *)
536 efi.systab->runtime->reset_system;
539 void __init
540 efi_initialize_iomem_resources(struct resource *code_resource,
541 struct resource *data_resource)
543 struct resource *res;
544 efi_memory_desc_t *md;
545 void *p;
547 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
548 md = p;
550 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
551 0x100000000ULL)
552 continue;
553 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
554 switch (md->type) {
555 case EFI_RESERVED_TYPE:
556 res->name = "Reserved Memory";
557 break;
558 case EFI_LOADER_CODE:
559 res->name = "Loader Code";
560 break;
561 case EFI_LOADER_DATA:
562 res->name = "Loader Data";
563 break;
564 case EFI_BOOT_SERVICES_DATA:
565 res->name = "BootServices Data";
566 break;
567 case EFI_BOOT_SERVICES_CODE:
568 res->name = "BootServices Code";
569 break;
570 case EFI_RUNTIME_SERVICES_CODE:
571 res->name = "Runtime Service Code";
572 break;
573 case EFI_RUNTIME_SERVICES_DATA:
574 res->name = "Runtime Service Data";
575 break;
576 case EFI_CONVENTIONAL_MEMORY:
577 res->name = "Conventional Memory";
578 break;
579 case EFI_UNUSABLE_MEMORY:
580 res->name = "Unusable Memory";
581 break;
582 case EFI_ACPI_RECLAIM_MEMORY:
583 res->name = "ACPI Reclaim";
584 break;
585 case EFI_ACPI_MEMORY_NVS:
586 res->name = "ACPI NVS";
587 break;
588 case EFI_MEMORY_MAPPED_IO:
589 res->name = "Memory Mapped IO";
590 break;
591 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
592 res->name = "Memory Mapped IO Port Space";
593 break;
594 default:
595 res->name = "Reserved";
596 break;
598 res->start = md->phys_addr;
599 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
600 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
601 if (request_resource(&iomem_resource, res) < 0)
602 printk(KERN_ERR PFX "Failed to allocate res %s : "
603 "0x%llx-0x%llx\n", res->name,
604 (unsigned long long)res->start,
605 (unsigned long long)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);
613 #ifdef CONFIG_KEXEC
614 request_resource(res, &crashk_res);
615 #endif
621 * Convenience functions to obtain memory types and attributes
624 u32 efi_mem_type(unsigned long phys_addr)
626 efi_memory_desc_t *md;
627 void *p;
629 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
630 md = p;
631 if ((md->phys_addr <= phys_addr) && (phys_addr <
632 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
633 return md->type;
635 return 0;
638 u64 efi_mem_attributes(unsigned long phys_addr)
640 efi_memory_desc_t *md;
641 void *p;
643 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
644 md = p;
645 if ((md->phys_addr <= phys_addr) && (phys_addr <
646 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
647 return md->attribute;
649 return 0;