[CPUFREQ] powernow-k8.c: fix a check-after-use
[usb.git] / arch / i386 / kernel / efi.c
blob9202b67c4b2e5cc925b0670d0b3e804dc719abbc
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/config.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/mm.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>
36 #include <asm/io.h>
37 #include <asm/page.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40 #include <asm/desc.h>
41 #include <asm/tlbflush.h>
43 #define EFI_DEBUG 0
44 #define PFX "EFI: "
46 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
48 struct efi efi;
49 EXPORT_SYMBOL(efi);
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)
71 unsigned long cr4;
72 unsigned long temp;
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
83 * page directory.
85 cr4 = read_cr4();
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;
92 } else {
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.
107 local_flush_tlb();
109 cpu_gdt_descr->address = __pa(cpu_gdt_descr->address);
110 load_gdt(cpu_gdt_descr);
113 static void efi_call_phys_epilog(void)
115 unsigned long cr4;
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);
121 cr4 = read_cr4();
123 if (cr4 & X86_CR4_PSE) {
124 swapper_pg_dir[pgd_index(0)].pgd =
125 efi_bak_pg_dir_pointer[0].pgd;
126 } else {
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.
136 local_flush_tlb();
138 local_irq_restore(efi_rt_eflags);
139 spin_unlock(&efi_rt_lock);
142 static efi_status_t
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)
148 efi_status_t status;
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();
155 return status;
158 static efi_status_t
159 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
161 efi_status_t status;
163 efi_call_phys_prelog();
164 status = efi_call_phys(efi_phys.get_time, tm, tc);
165 efi_call_phys_epilog();
166 return status;
169 inline int efi_set_rtc_mmss(unsigned long nowtime)
171 int real_seconds, real_minutes;
172 efi_status_t status;
173 efi_time_t eft;
174 efi_time_cap_t cap;
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)
185 real_minutes += 30;
186 real_minutes %= 60;
188 eft.minute = real_minutes;
189 eft.second = real_seconds;
191 if (status != EFI_SUCCESS) {
192 printk("Ooops: efitime: can't read time!\n");
193 return -1;
195 return 0;
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)
204 efi_status_t status;
205 efi_time_t eft;
206 efi_time_cap_t cap;
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))
219 return 0;
221 switch (md->type) {
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:
227 return 1;
229 return 0;
233 * We need to map the EFI memory map again after paging_init().
235 void __init efi_map_memmap(void)
237 memmap.map = NULL;
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);
247 #if EFI_DEBUG
248 static void __init print_efi_memmap(void)
250 efi_memory_desc_t *md;
251 void *p;
252 int i;
254 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
255 md = p;
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)
271 int prev_valid = 0;
272 struct range {
273 unsigned long start;
274 unsigned long end;
275 } prev, curr;
276 efi_memory_desc_t *md;
277 unsigned long start, end;
278 void *p;
280 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
281 md = p;
283 if ((md->num_pages == 0) || (!is_available_memory(md)))
284 continue;
286 curr.start = md->phys_addr;
287 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
289 if (!prev_valid) {
290 prev = curr;
291 prev_valid = 1;
292 } else {
293 if (curr.start < prev.start)
294 printk(KERN_INFO PFX "Unordered memory map\n");
295 if (prev.end == curr.start)
296 prev.end = curr.end;
297 else {
298 start =
299 (unsigned long) (PAGE_ALIGN(prev.start));
300 end = (unsigned long) (prev.end & PAGE_MASK);
301 if ((end > start)
302 && (*callback) (start, end, arg) < 0)
303 return;
304 prev = curr;
308 if (prev_valid) {
309 start = (unsigned long) PAGE_ALIGN(prev.start);
310 end = (unsigned long) (prev.end & PAGE_MASK);
311 if (end > start)
312 (*callback) (start, end, arg);
316 void __init efi_init(void)
318 efi_config_table_t *config_tables;
319 efi_runtime_services_t *runtime;
320 efi_char16_t *c16;
321 char vendor[100] = "unknown";
322 unsigned long num_config_tables;
323 int i = 0;
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)
345 printk(KERN_ERR PFX
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);
363 if (c16) {
364 for (i = 0; i < (sizeof(vendor) - 1) && *c16; ++i)
365 vendor[i] = *c16++;
366 vendor[i] = '\0';
367 } else
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);
397 } else
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);
401 } else
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);
405 } else
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);
409 } else
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);
413 } else
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);
419 printk("\n");
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)
429 runtime,
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
435 * is invoked.
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;
441 } else
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);
451 #if EFI_DEBUG
452 print_efi_memmap();
453 #endif
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))) {
461 unsigned long addr;
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;
481 efi_status_t status;
482 void *p;
484 efi.systab = NULL;
486 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
487 md = p;
489 if (!(md->attribute & EFI_MEMORY_RUNTIME))
490 continue;
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);
502 if (!efi.systab)
503 BUG();
505 status = phys_efi_set_virtual_address_map(
506 memmap.desc_size * memmap.nr_map,
507 memmap.desc_size,
508 memmap.desc_version,
509 memmap.phys_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;
541 void __init
542 efi_initialize_iomem_resources(struct resource *code_resource,
543 struct resource *data_resource)
545 struct resource *res;
546 efi_memory_desc_t *md;
547 void *p;
549 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
550 md = p;
552 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
553 0x100000000ULL)
554 continue;
555 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
556 switch (md->type) {
557 case EFI_RESERVED_TYPE:
558 res->name = "Reserved Memory";
559 break;
560 case EFI_LOADER_CODE:
561 res->name = "Loader Code";
562 break;
563 case EFI_LOADER_DATA:
564 res->name = "Loader Data";
565 break;
566 case EFI_BOOT_SERVICES_DATA:
567 res->name = "BootServices Data";
568 break;
569 case EFI_BOOT_SERVICES_CODE:
570 res->name = "BootServices Code";
571 break;
572 case EFI_RUNTIME_SERVICES_CODE:
573 res->name = "Runtime Service Code";
574 break;
575 case EFI_RUNTIME_SERVICES_DATA:
576 res->name = "Runtime Service Data";
577 break;
578 case EFI_CONVENTIONAL_MEMORY:
579 res->name = "Conventional Memory";
580 break;
581 case EFI_UNUSABLE_MEMORY:
582 res->name = "Unusable Memory";
583 break;
584 case EFI_ACPI_RECLAIM_MEMORY:
585 res->name = "ACPI Reclaim";
586 break;
587 case EFI_ACPI_MEMORY_NVS:
588 res->name = "ACPI NVS";
589 break;
590 case EFI_MEMORY_MAPPED_IO:
591 res->name = "Memory Mapped IO";
592 break;
593 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
594 res->name = "Memory Mapped IO Port Space";
595 break;
596 default:
597 res->name = "Reserved";
598 break;
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);
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;