Merge commit 'v2.6.31.12' into mini2440-stable-v2.6.31
[linux-2.6/mini2440.git] / arch / x86 / kernel / efi.c
blobfe26ba3e3451872c083c10b6ccf62651fcd762a0
1 /*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
5 * Copyright (C) 1999 VA Linux Systems
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
8 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * Stephane Eranian <eranian@hpl.hp.com>
10 * Copyright (C) 2005-2008 Intel Co.
11 * Fenghua Yu <fenghua.yu@intel.com>
12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@intel.com>
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
19 * All EFI Runtime Services are not implemented yet as EFI only
20 * supports physical mode addressing on SoftSDV. This is to be fixed
21 * in a future version. --drummond 1999-07-20
23 * Implemented EFI runtime services and virtual mode calls. --davidm
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/efi.h>
32 #include <linux/bootmem.h>
33 #include <linux/spinlock.h>
34 #include <linux/uaccess.h>
35 #include <linux/time.h>
36 #include <linux/io.h>
37 #include <linux/reboot.h>
38 #include <linux/bcd.h>
40 #include <asm/setup.h>
41 #include <asm/efi.h>
42 #include <asm/time.h>
43 #include <asm/cacheflush.h>
44 #include <asm/tlbflush.h>
46 #define EFI_DEBUG 1
47 #define PFX "EFI: "
49 int efi_enabled;
50 EXPORT_SYMBOL(efi_enabled);
52 struct efi efi;
53 EXPORT_SYMBOL(efi);
55 struct efi_memory_map memmap;
57 static struct efi efi_phys __initdata;
58 static efi_system_table_t efi_systab __initdata;
60 static int __init setup_noefi(char *arg)
62 efi_enabled = 0;
63 return 0;
65 early_param("noefi", setup_noefi);
67 int add_efi_memmap;
68 EXPORT_SYMBOL(add_efi_memmap);
70 static int __init setup_add_efi_memmap(char *arg)
72 add_efi_memmap = 1;
73 return 0;
75 early_param("add_efi_memmap", setup_add_efi_memmap);
78 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
80 return efi_call_virt2(get_time, tm, tc);
83 static efi_status_t virt_efi_set_time(efi_time_t *tm)
85 return efi_call_virt1(set_time, tm);
88 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
89 efi_bool_t *pending,
90 efi_time_t *tm)
92 return efi_call_virt3(get_wakeup_time,
93 enabled, pending, tm);
96 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
98 return efi_call_virt2(set_wakeup_time,
99 enabled, tm);
102 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
103 efi_guid_t *vendor,
104 u32 *attr,
105 unsigned long *data_size,
106 void *data)
108 return efi_call_virt5(get_variable,
109 name, vendor, attr,
110 data_size, data);
113 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
114 efi_char16_t *name,
115 efi_guid_t *vendor)
117 return efi_call_virt3(get_next_variable,
118 name_size, name, vendor);
121 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
122 efi_guid_t *vendor,
123 unsigned long attr,
124 unsigned long data_size,
125 void *data)
127 return efi_call_virt5(set_variable,
128 name, vendor, attr,
129 data_size, data);
132 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
134 return efi_call_virt1(get_next_high_mono_count, count);
137 static void virt_efi_reset_system(int reset_type,
138 efi_status_t status,
139 unsigned long data_size,
140 efi_char16_t *data)
142 efi_call_virt4(reset_system, reset_type, status,
143 data_size, data);
146 static efi_status_t virt_efi_set_virtual_address_map(
147 unsigned long memory_map_size,
148 unsigned long descriptor_size,
149 u32 descriptor_version,
150 efi_memory_desc_t *virtual_map)
152 return efi_call_virt4(set_virtual_address_map,
153 memory_map_size, descriptor_size,
154 descriptor_version, virtual_map);
157 static efi_status_t __init phys_efi_set_virtual_address_map(
158 unsigned long memory_map_size,
159 unsigned long descriptor_size,
160 u32 descriptor_version,
161 efi_memory_desc_t *virtual_map)
163 efi_status_t status;
165 efi_call_phys_prelog();
166 status = efi_call_phys4(efi_phys.set_virtual_address_map,
167 memory_map_size, descriptor_size,
168 descriptor_version, virtual_map);
169 efi_call_phys_epilog();
170 return status;
173 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
174 efi_time_cap_t *tc)
176 efi_status_t status;
178 efi_call_phys_prelog();
179 status = efi_call_phys2(efi_phys.get_time, tm, tc);
180 efi_call_phys_epilog();
181 return status;
184 int efi_set_rtc_mmss(unsigned long nowtime)
186 int real_seconds, real_minutes;
187 efi_status_t status;
188 efi_time_t eft;
189 efi_time_cap_t cap;
191 status = efi.get_time(&eft, &cap);
192 if (status != EFI_SUCCESS) {
193 printk(KERN_ERR "Oops: efitime: can't read time!\n");
194 return -1;
197 real_seconds = nowtime % 60;
198 real_minutes = nowtime / 60;
199 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
200 real_minutes += 30;
201 real_minutes %= 60;
202 eft.minute = real_minutes;
203 eft.second = real_seconds;
205 status = efi.set_time(&eft);
206 if (status != EFI_SUCCESS) {
207 printk(KERN_ERR "Oops: efitime: can't write time!\n");
208 return -1;
210 return 0;
213 unsigned long efi_get_time(void)
215 efi_status_t status;
216 efi_time_t eft;
217 efi_time_cap_t cap;
219 status = efi.get_time(&eft, &cap);
220 if (status != EFI_SUCCESS)
221 printk(KERN_ERR "Oops: efitime: can't read time!\n");
223 return mktime(eft.year, eft.month, eft.day, eft.hour,
224 eft.minute, eft.second);
228 * Tell the kernel about the EFI memory map. This might include
229 * more than the max 128 entries that can fit in the e820 legacy
230 * (zeropage) memory map.
233 static void __init do_add_efi_memmap(void)
235 void *p;
237 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
238 efi_memory_desc_t *md = p;
239 unsigned long long start = md->phys_addr;
240 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
241 int e820_type;
243 switch (md->type) {
244 case EFI_LOADER_CODE:
245 case EFI_LOADER_DATA:
246 case EFI_BOOT_SERVICES_CODE:
247 case EFI_BOOT_SERVICES_DATA:
248 case EFI_CONVENTIONAL_MEMORY:
249 if (md->attribute & EFI_MEMORY_WB)
250 e820_type = E820_RAM;
251 else
252 e820_type = E820_RESERVED;
253 break;
254 case EFI_ACPI_RECLAIM_MEMORY:
255 e820_type = E820_ACPI;
256 break;
257 case EFI_ACPI_MEMORY_NVS:
258 e820_type = E820_NVS;
259 break;
260 case EFI_UNUSABLE_MEMORY:
261 e820_type = E820_UNUSABLE;
262 break;
263 default:
265 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
266 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
267 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
269 e820_type = E820_RESERVED;
270 break;
272 e820_add_region(start, size, e820_type);
274 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
277 void __init efi_reserve_early(void)
279 unsigned long pmap;
281 #ifdef CONFIG_X86_32
282 pmap = boot_params.efi_info.efi_memmap;
283 #else
284 pmap = (boot_params.efi_info.efi_memmap |
285 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
286 #endif
287 memmap.phys_map = (void *)pmap;
288 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
289 boot_params.efi_info.efi_memdesc_size;
290 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
291 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
292 reserve_early(pmap, pmap + memmap.nr_map * memmap.desc_size,
293 "EFI memmap");
296 #if EFI_DEBUG
297 static void __init print_efi_memmap(void)
299 efi_memory_desc_t *md;
300 void *p;
301 int i;
303 for (p = memmap.map, i = 0;
304 p < memmap.map_end;
305 p += memmap.desc_size, i++) {
306 md = p;
307 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
308 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
309 i, md->type, md->attribute, md->phys_addr,
310 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
311 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
314 #endif /* EFI_DEBUG */
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 int i = 0;
323 void *tmp;
325 #ifdef CONFIG_X86_32
326 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
327 #else
328 efi_phys.systab = (efi_system_table_t *)
329 (boot_params.efi_info.efi_systab |
330 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
331 #endif
333 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
334 sizeof(efi_system_table_t));
335 if (efi.systab == NULL)
336 printk(KERN_ERR "Couldn't map the EFI system table!\n");
337 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
338 early_iounmap(efi.systab, sizeof(efi_system_table_t));
339 efi.systab = &efi_systab;
342 * Verify the EFI Table
344 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
345 printk(KERN_ERR "EFI system table signature incorrect!\n");
346 if ((efi.systab->hdr.revision >> 16) == 0)
347 printk(KERN_ERR "Warning: EFI system table version "
348 "%d.%02d, expected 1.00 or greater!\n",
349 efi.systab->hdr.revision >> 16,
350 efi.systab->hdr.revision & 0xffff);
353 * Show what we know for posterity
355 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
356 if (c16) {
357 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
358 vendor[i] = *c16++;
359 vendor[i] = '\0';
360 } else
361 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
362 early_iounmap(tmp, 2);
364 printk(KERN_INFO "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 = early_ioremap(
372 efi.systab->tables,
373 efi.systab->nr_tables * sizeof(efi_config_table_t));
374 if (config_tables == NULL)
375 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
377 printk(KERN_INFO);
378 for (i = 0; i < efi.systab->nr_tables; i++) {
379 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
380 efi.mps = config_tables[i].table;
381 printk(" MPS=0x%lx ", config_tables[i].table);
382 } else if (!efi_guidcmp(config_tables[i].guid,
383 ACPI_20_TABLE_GUID)) {
384 efi.acpi20 = config_tables[i].table;
385 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
386 } else if (!efi_guidcmp(config_tables[i].guid,
387 ACPI_TABLE_GUID)) {
388 efi.acpi = config_tables[i].table;
389 printk(" ACPI=0x%lx ", config_tables[i].table);
390 } else if (!efi_guidcmp(config_tables[i].guid,
391 SMBIOS_TABLE_GUID)) {
392 efi.smbios = config_tables[i].table;
393 printk(" SMBIOS=0x%lx ", config_tables[i].table);
394 #ifdef CONFIG_X86_UV
395 } else if (!efi_guidcmp(config_tables[i].guid,
396 UV_SYSTEM_TABLE_GUID)) {
397 efi.uv_systab = config_tables[i].table;
398 printk(" UVsystab=0x%lx ", config_tables[i].table);
399 #endif
400 } else if (!efi_guidcmp(config_tables[i].guid,
401 HCDP_TABLE_GUID)) {
402 efi.hcdp = config_tables[i].table;
403 printk(" HCDP=0x%lx ", config_tables[i].table);
404 } else if (!efi_guidcmp(config_tables[i].guid,
405 UGA_IO_PROTOCOL_GUID)) {
406 efi.uga = config_tables[i].table;
407 printk(" UGA=0x%lx ", config_tables[i].table);
410 printk("\n");
411 early_iounmap(config_tables,
412 efi.systab->nr_tables * sizeof(efi_config_table_t));
415 * Check out the runtime services table. We need to map
416 * the runtime services table so that we can grab the physical
417 * address of several of the EFI runtime functions, needed to
418 * set the firmware into virtual mode.
420 runtime = early_ioremap((unsigned long)efi.systab->runtime,
421 sizeof(efi_runtime_services_t));
422 if (runtime != NULL) {
424 * We will only need *early* access to the following
425 * two EFI runtime services before set_virtual_address_map
426 * is invoked.
428 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
429 efi_phys.set_virtual_address_map =
430 (efi_set_virtual_address_map_t *)
431 runtime->set_virtual_address_map;
433 * Make efi_get_time can be called before entering
434 * virtual mode.
436 efi.get_time = phys_efi_get_time;
437 } else
438 printk(KERN_ERR "Could not map the EFI runtime service "
439 "table!\n");
440 early_iounmap(runtime, sizeof(efi_runtime_services_t));
442 /* Map the EFI memory map */
443 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
444 memmap.nr_map * memmap.desc_size);
445 if (memmap.map == NULL)
446 printk(KERN_ERR "Could not map the EFI memory map!\n");
447 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
449 if (memmap.desc_size != sizeof(efi_memory_desc_t))
450 printk(KERN_WARNING
451 "Kernel-defined memdesc doesn't match the one from EFI!\n");
453 if (add_efi_memmap)
454 do_add_efi_memmap();
456 /* Setup for EFI runtime service */
457 reboot_type = BOOT_EFI;
459 #if EFI_DEBUG
460 print_efi_memmap();
461 #endif
464 static void __init runtime_code_page_mkexec(void)
466 efi_memory_desc_t *md;
467 void *p;
468 u64 addr, npages;
470 /* Make EFI runtime service code area executable */
471 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
472 md = p;
474 if (md->type != EFI_RUNTIME_SERVICES_CODE)
475 continue;
477 addr = md->virt_addr;
478 npages = md->num_pages;
479 memrange_efi_to_native(&addr, &npages);
480 set_memory_x(addr, npages);
485 * This function will switch the EFI runtime services to virtual mode.
486 * Essentially, look through the EFI memmap and map every region that
487 * has the runtime attribute bit set in its memory descriptor and update
488 * that memory descriptor with the virtual address obtained from ioremap().
489 * This enables the runtime services to be called without having to
490 * thunk back into physical mode for every invocation.
492 void __init efi_enter_virtual_mode(void)
494 efi_memory_desc_t *md;
495 efi_status_t status;
496 unsigned long size;
497 u64 end, systab, addr, npages, end_pfn;
498 void *p, *va;
500 efi.systab = NULL;
501 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
502 md = p;
503 if (!(md->attribute & EFI_MEMORY_RUNTIME))
504 continue;
506 size = md->num_pages << EFI_PAGE_SHIFT;
507 end = md->phys_addr + size;
509 end_pfn = PFN_UP(end);
510 if (end_pfn <= max_low_pfn_mapped
511 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
512 && end_pfn <= max_pfn_mapped))
513 va = __va(md->phys_addr);
514 else
515 va = efi_ioremap(md->phys_addr, size, md->type);
517 md->virt_addr = (u64) (unsigned long) va;
519 if (!va) {
520 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
521 (unsigned long long)md->phys_addr);
522 continue;
525 if (!(md->attribute & EFI_MEMORY_WB)) {
526 addr = md->virt_addr;
527 npages = md->num_pages;
528 memrange_efi_to_native(&addr, &npages);
529 set_memory_uc(addr, npages);
532 systab = (u64) (unsigned long) efi_phys.systab;
533 if (md->phys_addr <= systab && systab < end) {
534 systab += md->virt_addr - md->phys_addr;
535 efi.systab = (efi_system_table_t *) (unsigned long) systab;
539 BUG_ON(!efi.systab);
541 status = phys_efi_set_virtual_address_map(
542 memmap.desc_size * memmap.nr_map,
543 memmap.desc_size,
544 memmap.desc_version,
545 memmap.phys_map);
547 if (status != EFI_SUCCESS) {
548 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
549 "(status=%lx)!\n", status);
550 panic("EFI call to SetVirtualAddressMap() failed!");
554 * Now that EFI is in virtual mode, update the function
555 * pointers in the runtime service table to the new virtual addresses.
557 * Call EFI services through wrapper functions.
559 efi.get_time = virt_efi_get_time;
560 efi.set_time = virt_efi_set_time;
561 efi.get_wakeup_time = virt_efi_get_wakeup_time;
562 efi.set_wakeup_time = virt_efi_set_wakeup_time;
563 efi.get_variable = virt_efi_get_variable;
564 efi.get_next_variable = virt_efi_get_next_variable;
565 efi.set_variable = virt_efi_set_variable;
566 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
567 efi.reset_system = virt_efi_reset_system;
568 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
569 if (__supported_pte_mask & _PAGE_NX)
570 runtime_code_page_mkexec();
571 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
572 memmap.map = NULL;
576 * Convenience functions to obtain memory types and attributes
578 u32 efi_mem_type(unsigned long phys_addr)
580 efi_memory_desc_t *md;
581 void *p;
583 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
584 md = p;
585 if ((md->phys_addr <= phys_addr) &&
586 (phys_addr < (md->phys_addr +
587 (md->num_pages << EFI_PAGE_SHIFT))))
588 return md->type;
590 return 0;
593 u64 efi_mem_attributes(unsigned long phys_addr)
595 efi_memory_desc_t *md;
596 void *p;
598 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
599 md = p;
600 if ((md->phys_addr <= phys_addr) &&
601 (phys_addr < (md->phys_addr +
602 (md->num_pages << EFI_PAGE_SHIFT))))
603 return md->attribute;
605 return 0;