Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / x86 / kernel / efi.c
blob06a42ed75bb2be8ce80db4195006e80e7b26d63b
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 <<<<<<< HEAD:arch/x86/kernel/efi.c
58 struct efi efi_phys __initdata;
59 =======
60 static struct efi efi_phys __initdata;
61 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/efi.c
62 static efi_system_table_t efi_systab __initdata;
64 static int __init setup_noefi(char *arg)
66 efi_enabled = 0;
67 return 0;
69 early_param("noefi", setup_noefi);
71 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
73 return efi_call_virt2(get_time, tm, tc);
76 static efi_status_t virt_efi_set_time(efi_time_t *tm)
78 return efi_call_virt1(set_time, tm);
81 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
82 efi_bool_t *pending,
83 efi_time_t *tm)
85 return efi_call_virt3(get_wakeup_time,
86 enabled, pending, tm);
89 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
91 return efi_call_virt2(set_wakeup_time,
92 enabled, tm);
95 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
96 efi_guid_t *vendor,
97 u32 *attr,
98 unsigned long *data_size,
99 void *data)
101 return efi_call_virt5(get_variable,
102 name, vendor, attr,
103 data_size, data);
106 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
107 efi_char16_t *name,
108 efi_guid_t *vendor)
110 return efi_call_virt3(get_next_variable,
111 name_size, name, vendor);
114 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
115 efi_guid_t *vendor,
116 unsigned long attr,
117 unsigned long data_size,
118 void *data)
120 return efi_call_virt5(set_variable,
121 name, vendor, attr,
122 data_size, data);
125 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
127 return efi_call_virt1(get_next_high_mono_count, count);
130 static void virt_efi_reset_system(int reset_type,
131 efi_status_t status,
132 unsigned long data_size,
133 efi_char16_t *data)
135 efi_call_virt4(reset_system, reset_type, status,
136 data_size, data);
139 static efi_status_t virt_efi_set_virtual_address_map(
140 unsigned long memory_map_size,
141 unsigned long descriptor_size,
142 u32 descriptor_version,
143 efi_memory_desc_t *virtual_map)
145 return efi_call_virt4(set_virtual_address_map,
146 memory_map_size, descriptor_size,
147 descriptor_version, virtual_map);
150 static efi_status_t __init phys_efi_set_virtual_address_map(
151 unsigned long memory_map_size,
152 unsigned long descriptor_size,
153 u32 descriptor_version,
154 efi_memory_desc_t *virtual_map)
156 efi_status_t status;
158 efi_call_phys_prelog();
159 status = efi_call_phys4(efi_phys.set_virtual_address_map,
160 memory_map_size, descriptor_size,
161 descriptor_version, virtual_map);
162 efi_call_phys_epilog();
163 return status;
166 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
167 efi_time_cap_t *tc)
169 efi_status_t status;
171 efi_call_phys_prelog();
172 status = efi_call_phys2(efi_phys.get_time, tm, tc);
173 efi_call_phys_epilog();
174 return status;
177 int efi_set_rtc_mmss(unsigned long nowtime)
179 int real_seconds, real_minutes;
180 efi_status_t status;
181 efi_time_t eft;
182 efi_time_cap_t cap;
184 status = efi.get_time(&eft, &cap);
185 if (status != EFI_SUCCESS) {
186 printk(KERN_ERR "Oops: efitime: can't read time!\n");
187 return -1;
190 real_seconds = nowtime % 60;
191 real_minutes = nowtime / 60;
192 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
193 real_minutes += 30;
194 real_minutes %= 60;
195 eft.minute = real_minutes;
196 eft.second = real_seconds;
198 status = efi.set_time(&eft);
199 if (status != EFI_SUCCESS) {
200 printk(KERN_ERR "Oops: efitime: can't write time!\n");
201 return -1;
203 return 0;
206 unsigned long efi_get_time(void)
208 efi_status_t status;
209 efi_time_t eft;
210 efi_time_cap_t cap;
212 status = efi.get_time(&eft, &cap);
213 if (status != EFI_SUCCESS)
214 printk(KERN_ERR "Oops: efitime: can't read time!\n");
216 return mktime(eft.year, eft.month, eft.day, eft.hour,
217 eft.minute, eft.second);
220 #if EFI_DEBUG
221 static void __init print_efi_memmap(void)
223 efi_memory_desc_t *md;
224 void *p;
225 int i;
227 for (p = memmap.map, i = 0;
228 p < memmap.map_end;
229 p += memmap.desc_size, i++) {
230 md = p;
231 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
232 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
233 i, md->type, md->attribute, md->phys_addr,
234 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
235 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
238 #endif /* EFI_DEBUG */
240 void __init efi_init(void)
242 efi_config_table_t *config_tables;
243 efi_runtime_services_t *runtime;
244 efi_char16_t *c16;
245 char vendor[100] = "unknown";
246 int i = 0;
247 void *tmp;
249 #ifdef CONFIG_X86_32
250 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
251 memmap.phys_map = (void *)boot_params.efi_info.efi_memmap;
252 #else
253 efi_phys.systab = (efi_system_table_t *)
254 (boot_params.efi_info.efi_systab |
255 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
256 memmap.phys_map = (void *)
257 (boot_params.efi_info.efi_memmap |
258 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
259 #endif
260 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
261 boot_params.efi_info.efi_memdesc_size;
262 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
263 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
265 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
266 sizeof(efi_system_table_t));
267 if (efi.systab == NULL)
268 printk(KERN_ERR "Couldn't map the EFI system table!\n");
269 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
270 early_iounmap(efi.systab, sizeof(efi_system_table_t));
271 efi.systab = &efi_systab;
274 * Verify the EFI Table
276 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
277 printk(KERN_ERR "EFI system table signature incorrect!\n");
278 if ((efi.systab->hdr.revision >> 16) == 0)
279 printk(KERN_ERR "Warning: EFI system table version "
280 "%d.%02d, expected 1.00 or greater!\n",
281 efi.systab->hdr.revision >> 16,
282 efi.systab->hdr.revision & 0xffff);
285 * Show what we know for posterity
287 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
288 if (c16) {
289 for (i = 0; i < sizeof(vendor) && *c16; ++i)
290 vendor[i] = *c16++;
291 vendor[i] = '\0';
292 } else
293 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
294 early_iounmap(tmp, 2);
296 printk(KERN_INFO "EFI v%u.%.02u by %s \n",
297 efi.systab->hdr.revision >> 16,
298 efi.systab->hdr.revision & 0xffff, vendor);
301 * Let's see what config tables the firmware passed to us.
303 config_tables = early_ioremap(
304 efi.systab->tables,
305 efi.systab->nr_tables * sizeof(efi_config_table_t));
306 if (config_tables == NULL)
307 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
309 printk(KERN_INFO);
310 for (i = 0; i < efi.systab->nr_tables; i++) {
311 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
312 efi.mps = config_tables[i].table;
313 printk(" MPS=0x%lx ", config_tables[i].table);
314 } else if (!efi_guidcmp(config_tables[i].guid,
315 ACPI_20_TABLE_GUID)) {
316 efi.acpi20 = config_tables[i].table;
317 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
318 } else if (!efi_guidcmp(config_tables[i].guid,
319 ACPI_TABLE_GUID)) {
320 efi.acpi = config_tables[i].table;
321 printk(" ACPI=0x%lx ", config_tables[i].table);
322 } else if (!efi_guidcmp(config_tables[i].guid,
323 SMBIOS_TABLE_GUID)) {
324 efi.smbios = config_tables[i].table;
325 printk(" SMBIOS=0x%lx ", config_tables[i].table);
326 } else if (!efi_guidcmp(config_tables[i].guid,
327 HCDP_TABLE_GUID)) {
328 efi.hcdp = config_tables[i].table;
329 printk(" HCDP=0x%lx ", config_tables[i].table);
330 } else if (!efi_guidcmp(config_tables[i].guid,
331 UGA_IO_PROTOCOL_GUID)) {
332 efi.uga = config_tables[i].table;
333 printk(" UGA=0x%lx ", config_tables[i].table);
336 printk("\n");
337 early_iounmap(config_tables,
338 efi.systab->nr_tables * sizeof(efi_config_table_t));
341 * Check out the runtime services table. We need to map
342 * the runtime services table so that we can grab the physical
343 * address of several of the EFI runtime functions, needed to
344 * set the firmware into virtual mode.
346 runtime = early_ioremap((unsigned long)efi.systab->runtime,
347 sizeof(efi_runtime_services_t));
348 if (runtime != NULL) {
350 * We will only need *early* access to the following
351 * two EFI runtime services before set_virtual_address_map
352 * is invoked.
354 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
355 efi_phys.set_virtual_address_map =
356 (efi_set_virtual_address_map_t *)
357 runtime->set_virtual_address_map;
359 * Make efi_get_time can be called before entering
360 * virtual mode.
362 efi.get_time = phys_efi_get_time;
363 } else
364 printk(KERN_ERR "Could not map the EFI runtime service "
365 "table!\n");
366 early_iounmap(runtime, sizeof(efi_runtime_services_t));
368 /* Map the EFI memory map */
369 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
370 memmap.nr_map * memmap.desc_size);
371 if (memmap.map == NULL)
372 printk(KERN_ERR "Could not map the EFI memory map!\n");
373 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
374 if (memmap.desc_size != sizeof(efi_memory_desc_t))
375 printk(KERN_WARNING "Kernel-defined memdesc"
376 "doesn't match the one from EFI!\n");
378 /* Setup for EFI runtime service */
379 reboot_type = BOOT_EFI;
381 #if EFI_DEBUG
382 print_efi_memmap();
383 #endif
386 static void __init runtime_code_page_mkexec(void)
388 efi_memory_desc_t *md;
389 void *p;
391 /* Make EFI runtime service code area executable */
392 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
393 md = p;
395 if (md->type != EFI_RUNTIME_SERVICES_CODE)
396 continue;
398 set_memory_x(md->virt_addr, md->num_pages);
403 * This function will switch the EFI runtime services to virtual mode.
404 * Essentially, look through the EFI memmap and map every region that
405 * has the runtime attribute bit set in its memory descriptor and update
406 * that memory descriptor with the virtual address obtained from ioremap().
407 * This enables the runtime services to be called without having to
408 * thunk back into physical mode for every invocation.
410 void __init efi_enter_virtual_mode(void)
412 efi_memory_desc_t *md;
413 efi_status_t status;
414 unsigned long size;
415 u64 end, systab;
416 void *p, *va;
418 efi.systab = NULL;
419 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
420 md = p;
421 if (!(md->attribute & EFI_MEMORY_RUNTIME))
422 continue;
424 size = md->num_pages << EFI_PAGE_SHIFT;
425 end = md->phys_addr + size;
427 if ((end >> PAGE_SHIFT) <= max_pfn_mapped)
428 va = __va(md->phys_addr);
429 else
430 va = efi_ioremap(md->phys_addr, size);
432 md->virt_addr = (u64) (unsigned long) va;
434 if (!va) {
435 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
436 (unsigned long long)md->phys_addr);
437 continue;
440 if (!(md->attribute & EFI_MEMORY_WB))
441 set_memory_uc(md->virt_addr, md->num_pages);
443 systab = (u64) (unsigned long) efi_phys.systab;
444 if (md->phys_addr <= systab && systab < end) {
445 systab += md->virt_addr - md->phys_addr;
446 efi.systab = (efi_system_table_t *) (unsigned long) systab;
450 BUG_ON(!efi.systab);
452 status = phys_efi_set_virtual_address_map(
453 memmap.desc_size * memmap.nr_map,
454 memmap.desc_size,
455 memmap.desc_version,
456 memmap.phys_map);
458 if (status != EFI_SUCCESS) {
459 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
460 "(status=%lx)!\n", status);
461 panic("EFI call to SetVirtualAddressMap() failed!");
465 * Now that EFI is in virtual mode, update the function
466 * pointers in the runtime service table to the new virtual addresses.
468 * Call EFI services through wrapper functions.
470 efi.get_time = virt_efi_get_time;
471 efi.set_time = virt_efi_set_time;
472 efi.get_wakeup_time = virt_efi_get_wakeup_time;
473 efi.set_wakeup_time = virt_efi_set_wakeup_time;
474 efi.get_variable = virt_efi_get_variable;
475 efi.get_next_variable = virt_efi_get_next_variable;
476 efi.set_variable = virt_efi_set_variable;
477 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
478 efi.reset_system = virt_efi_reset_system;
479 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
480 if (__supported_pte_mask & _PAGE_NX)
481 runtime_code_page_mkexec();
482 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
483 memmap.map = NULL;
487 * Convenience functions to obtain memory types and attributes
489 u32 efi_mem_type(unsigned long phys_addr)
491 efi_memory_desc_t *md;
492 void *p;
494 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
495 md = p;
496 if ((md->phys_addr <= phys_addr) &&
497 (phys_addr < (md->phys_addr +
498 (md->num_pages << EFI_PAGE_SHIFT))))
499 return md->type;
501 return 0;
504 u64 efi_mem_attributes(unsigned long phys_addr)
506 efi_memory_desc_t *md;
507 void *p;
509 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
510 md = p;
511 if ((md->phys_addr <= phys_addr) &&
512 (phys_addr < (md->phys_addr +
513 (md->num_pages << EFI_PAGE_SHIFT))))
514 return md->attribute;
516 return 0;