Linux 2.6.39-rc6
[deliverable/linux.git] / arch / x86 / platform / efi / efi.c
CommitLineData
5b83683f
HY
1/*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
4 *
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>
15 *
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
18 *
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
22 *
23 * Implemented EFI runtime services and virtual mode calls. --davidm
24 *
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
27 */
28
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/efi.h>
32#include <linux/bootmem.h>
a9ce6bc1 33#include <linux/memblock.h>
5b83683f
HY
34#include <linux/spinlock.h>
35#include <linux/uaccess.h>
36#include <linux/time.h>
37#include <linux/io.h>
38#include <linux/reboot.h>
39#include <linux/bcd.h>
40
41#include <asm/setup.h>
42#include <asm/efi.h>
43#include <asm/time.h>
a2172e25
HY
44#include <asm/cacheflush.h>
45#include <asm/tlbflush.h>
7bd867df 46#include <asm/x86_init.h>
5b83683f
HY
47
48#define EFI_DEBUG 1
49#define PFX "EFI: "
50
51int efi_enabled;
52EXPORT_SYMBOL(efi_enabled);
53
54struct efi efi;
55EXPORT_SYMBOL(efi);
56
57struct efi_memory_map memmap;
58
ecaea42e 59static struct efi efi_phys __initdata;
5b83683f
HY
60static efi_system_table_t efi_systab __initdata;
61
8b2cb7a8
HY
62static int __init setup_noefi(char *arg)
63{
64 efi_enabled = 0;
65 return 0;
66}
67early_param("noefi", setup_noefi);
68
200001eb
PJ
69int add_efi_memmap;
70EXPORT_SYMBOL(add_efi_memmap);
71
72static int __init setup_add_efi_memmap(char *arg)
73{
74 add_efi_memmap = 1;
75 return 0;
76}
77early_param("add_efi_memmap", setup_add_efi_memmap);
78
79
5b83683f
HY
80static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
81{
82 return efi_call_virt2(get_time, tm, tc);
83}
84
85static efi_status_t virt_efi_set_time(efi_time_t *tm)
86{
87 return efi_call_virt1(set_time, tm);
88}
89
90static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
91 efi_bool_t *pending,
92 efi_time_t *tm)
93{
94 return efi_call_virt3(get_wakeup_time,
95 enabled, pending, tm);
96}
97
98static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
99{
100 return efi_call_virt2(set_wakeup_time,
101 enabled, tm);
102}
103
104static efi_status_t virt_efi_get_variable(efi_char16_t *name,
105 efi_guid_t *vendor,
106 u32 *attr,
107 unsigned long *data_size,
108 void *data)
109{
110 return efi_call_virt5(get_variable,
111 name, vendor, attr,
112 data_size, data);
113}
114
115static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
116 efi_char16_t *name,
117 efi_guid_t *vendor)
118{
119 return efi_call_virt3(get_next_variable,
120 name_size, name, vendor);
121}
122
123static efi_status_t virt_efi_set_variable(efi_char16_t *name,
124 efi_guid_t *vendor,
125 unsigned long attr,
126 unsigned long data_size,
127 void *data)
128{
129 return efi_call_virt5(set_variable,
130 name, vendor, attr,
131 data_size, data);
132}
133
134static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
135{
136 return efi_call_virt1(get_next_high_mono_count, count);
137}
138
139static void virt_efi_reset_system(int reset_type,
140 efi_status_t status,
141 unsigned long data_size,
142 efi_char16_t *data)
143{
144 efi_call_virt4(reset_system, reset_type, status,
145 data_size, data);
146}
147
148static efi_status_t virt_efi_set_virtual_address_map(
149 unsigned long memory_map_size,
150 unsigned long descriptor_size,
151 u32 descriptor_version,
152 efi_memory_desc_t *virtual_map)
153{
154 return efi_call_virt4(set_virtual_address_map,
155 memory_map_size, descriptor_size,
156 descriptor_version, virtual_map);
157}
158
159static efi_status_t __init phys_efi_set_virtual_address_map(
160 unsigned long memory_map_size,
161 unsigned long descriptor_size,
162 u32 descriptor_version,
163 efi_memory_desc_t *virtual_map)
164{
165 efi_status_t status;
166
167 efi_call_phys_prelog();
168 status = efi_call_phys4(efi_phys.set_virtual_address_map,
169 memory_map_size, descriptor_size,
170 descriptor_version, virtual_map);
171 efi_call_phys_epilog();
172 return status;
173}
174
175static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
176 efi_time_cap_t *tc)
177{
178 efi_status_t status;
179
180 efi_call_phys_prelog();
181 status = efi_call_phys2(efi_phys.get_time, tm, tc);
182 efi_call_phys_epilog();
183 return status;
184}
185
186int efi_set_rtc_mmss(unsigned long nowtime)
187{
188 int real_seconds, real_minutes;
189 efi_status_t status;
190 efi_time_t eft;
191 efi_time_cap_t cap;
192
193 status = efi.get_time(&eft, &cap);
194 if (status != EFI_SUCCESS) {
195 printk(KERN_ERR "Oops: efitime: can't read time!\n");
196 return -1;
197 }
198
199 real_seconds = nowtime % 60;
200 real_minutes = nowtime / 60;
201 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
202 real_minutes += 30;
203 real_minutes %= 60;
204 eft.minute = real_minutes;
205 eft.second = real_seconds;
206
207 status = efi.set_time(&eft);
208 if (status != EFI_SUCCESS) {
209 printk(KERN_ERR "Oops: efitime: can't write time!\n");
210 return -1;
211 }
212 return 0;
213}
214
215unsigned long efi_get_time(void)
216{
217 efi_status_t status;
218 efi_time_t eft;
219 efi_time_cap_t cap;
220
221 status = efi.get_time(&eft, &cap);
222 if (status != EFI_SUCCESS)
223 printk(KERN_ERR "Oops: efitime: can't read time!\n");
224
225 return mktime(eft.year, eft.month, eft.day, eft.hour,
226 eft.minute, eft.second);
227}
228
69c91893
PJ
229/*
230 * Tell the kernel about the EFI memory map. This might include
231 * more than the max 128 entries that can fit in the e820 legacy
232 * (zeropage) memory map.
233 */
234
200001eb 235static void __init do_add_efi_memmap(void)
69c91893
PJ
236{
237 void *p;
238
239 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
240 efi_memory_desc_t *md = p;
241 unsigned long long start = md->phys_addr;
242 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
243 int e820_type;
244
e2a71476
CW
245 switch (md->type) {
246 case EFI_LOADER_CODE:
247 case EFI_LOADER_DATA:
248 case EFI_BOOT_SERVICES_CODE:
249 case EFI_BOOT_SERVICES_DATA:
250 case EFI_CONVENTIONAL_MEMORY:
251 if (md->attribute & EFI_MEMORY_WB)
252 e820_type = E820_RAM;
253 else
254 e820_type = E820_RESERVED;
255 break;
256 case EFI_ACPI_RECLAIM_MEMORY:
257 e820_type = E820_ACPI;
258 break;
259 case EFI_ACPI_MEMORY_NVS:
260 e820_type = E820_NVS;
261 break;
262 case EFI_UNUSABLE_MEMORY:
263 e820_type = E820_UNUSABLE;
264 break;
265 default:
266 /*
267 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
268 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
269 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
270 */
69c91893 271 e820_type = E820_RESERVED;
e2a71476
CW
272 break;
273 }
d0be6bde 274 e820_add_region(start, size, e820_type);
69c91893
PJ
275 }
276 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
277}
278
a9ce6bc1 279void __init efi_memblock_x86_reserve_range(void)
ecacf09f
HY
280{
281 unsigned long pmap;
282
05486fa7 283#ifdef CONFIG_X86_32
ecacf09f 284 pmap = boot_params.efi_info.efi_memmap;
05486fa7
PJ
285#else
286 pmap = (boot_params.efi_info.efi_memmap |
287 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
ecacf09f
HY
288#endif
289 memmap.phys_map = (void *)pmap;
290 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
291 boot_params.efi_info.efi_memdesc_size;
292 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
293 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
a9ce6bc1 294 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
ecacf09f
HY
295 "EFI memmap");
296}
297
5b83683f
HY
298#if EFI_DEBUG
299static void __init print_efi_memmap(void)
300{
301 efi_memory_desc_t *md;
302 void *p;
303 int i;
304
305 for (p = memmap.map, i = 0;
306 p < memmap.map_end;
307 p += memmap.desc_size, i++) {
308 md = p;
309 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
310 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
311 i, md->type, md->attribute, md->phys_addr,
312 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
313 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
314 }
315}
316#endif /* EFI_DEBUG */
317
318void __init efi_init(void)
319{
320 efi_config_table_t *config_tables;
321 efi_runtime_services_t *runtime;
322 efi_char16_t *c16;
323 char vendor[100] = "unknown";
324 int i = 0;
325 void *tmp;
326
05486fa7 327#ifdef CONFIG_X86_32
5b83683f 328 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
05486fa7
PJ
329#else
330 efi_phys.systab = (efi_system_table_t *)
331 (boot_params.efi_info.efi_systab |
332 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
5b83683f 333#endif
5b83683f 334
beacfaac
HY
335 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
336 sizeof(efi_system_table_t));
5b83683f
HY
337 if (efi.systab == NULL)
338 printk(KERN_ERR "Couldn't map the EFI system table!\n");
339 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
beacfaac 340 early_iounmap(efi.systab, sizeof(efi_system_table_t));
5b83683f
HY
341 efi.systab = &efi_systab;
342
343 /*
344 * Verify the EFI Table
345 */
346 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
347 printk(KERN_ERR "EFI system table signature incorrect!\n");
348 if ((efi.systab->hdr.revision >> 16) == 0)
349 printk(KERN_ERR "Warning: EFI system table version "
350 "%d.%02d, expected 1.00 or greater!\n",
351 efi.systab->hdr.revision >> 16,
352 efi.systab->hdr.revision & 0xffff);
353
354 /*
355 * Show what we know for posterity
356 */
beacfaac 357 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
5b83683f 358 if (c16) {
fdb8a427 359 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
5b83683f
HY
360 vendor[i] = *c16++;
361 vendor[i] = '\0';
362 } else
363 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
beacfaac 364 early_iounmap(tmp, 2);
5b83683f 365
3235dc3f 366 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
5b83683f
HY
367 efi.systab->hdr.revision >> 16,
368 efi.systab->hdr.revision & 0xffff, vendor);
369
370 /*
371 * Let's see what config tables the firmware passed to us.
372 */
beacfaac 373 config_tables = early_ioremap(
5b83683f
HY
374 efi.systab->tables,
375 efi.systab->nr_tables * sizeof(efi_config_table_t));
376 if (config_tables == NULL)
377 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
378
379 printk(KERN_INFO);
380 for (i = 0; i < efi.systab->nr_tables; i++) {
381 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
382 efi.mps = config_tables[i].table;
383 printk(" MPS=0x%lx ", config_tables[i].table);
384 } else if (!efi_guidcmp(config_tables[i].guid,
385 ACPI_20_TABLE_GUID)) {
386 efi.acpi20 = config_tables[i].table;
387 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
388 } else if (!efi_guidcmp(config_tables[i].guid,
389 ACPI_TABLE_GUID)) {
390 efi.acpi = config_tables[i].table;
391 printk(" ACPI=0x%lx ", config_tables[i].table);
392 } else if (!efi_guidcmp(config_tables[i].guid,
393 SMBIOS_TABLE_GUID)) {
394 efi.smbios = config_tables[i].table;
395 printk(" SMBIOS=0x%lx ", config_tables[i].table);
03b48632 396#ifdef CONFIG_X86_UV
a50f70b1
RA
397 } else if (!efi_guidcmp(config_tables[i].guid,
398 UV_SYSTEM_TABLE_GUID)) {
399 efi.uv_systab = config_tables[i].table;
400 printk(" UVsystab=0x%lx ", config_tables[i].table);
03b48632 401#endif
5b83683f
HY
402 } else if (!efi_guidcmp(config_tables[i].guid,
403 HCDP_TABLE_GUID)) {
404 efi.hcdp = config_tables[i].table;
405 printk(" HCDP=0x%lx ", config_tables[i].table);
406 } else if (!efi_guidcmp(config_tables[i].guid,
407 UGA_IO_PROTOCOL_GUID)) {
408 efi.uga = config_tables[i].table;
409 printk(" UGA=0x%lx ", config_tables[i].table);
410 }
411 }
412 printk("\n");
beacfaac 413 early_iounmap(config_tables,
5b83683f
HY
414 efi.systab->nr_tables * sizeof(efi_config_table_t));
415
416 /*
417 * Check out the runtime services table. We need to map
418 * the runtime services table so that we can grab the physical
419 * address of several of the EFI runtime functions, needed to
420 * set the firmware into virtual mode.
421 */
beacfaac
HY
422 runtime = early_ioremap((unsigned long)efi.systab->runtime,
423 sizeof(efi_runtime_services_t));
5b83683f
HY
424 if (runtime != NULL) {
425 /*
426 * We will only need *early* access to the following
427 * two EFI runtime services before set_virtual_address_map
428 * is invoked.
429 */
430 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
431 efi_phys.set_virtual_address_map =
432 (efi_set_virtual_address_map_t *)
433 runtime->set_virtual_address_map;
434 /*
435 * Make efi_get_time can be called before entering
436 * virtual mode.
437 */
438 efi.get_time = phys_efi_get_time;
439 } else
440 printk(KERN_ERR "Could not map the EFI runtime service "
441 "table!\n");
beacfaac 442 early_iounmap(runtime, sizeof(efi_runtime_services_t));
5b83683f
HY
443
444 /* Map the EFI memory map */
beacfaac
HY
445 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
446 memmap.nr_map * memmap.desc_size);
5b83683f
HY
447 if (memmap.map == NULL)
448 printk(KERN_ERR "Could not map the EFI memory map!\n");
449 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
175e438f 450
5b83683f 451 if (memmap.desc_size != sizeof(efi_memory_desc_t))
175e438f
RA
452 printk(KERN_WARNING
453 "Kernel-defined memdesc doesn't match the one from EFI!\n");
454
200001eb
PJ
455 if (add_efi_memmap)
456 do_add_efi_memmap();
5b83683f 457
772be899 458#ifdef CONFIG_X86_32
7bd867df
FT
459 x86_platform.get_wallclock = efi_get_time;
460 x86_platform.set_wallclock = efi_set_rtc_mmss;
772be899 461#endif
7bd867df 462
5b83683f
HY
463 /* Setup for EFI runtime service */
464 reboot_type = BOOT_EFI;
465
5b83683f
HY
466#if EFI_DEBUG
467 print_efi_memmap();
468#endif
469}
470
a2172e25
HY
471static void __init runtime_code_page_mkexec(void)
472{
473 efi_memory_desc_t *md;
a2172e25 474 void *p;
4a3575fd 475 u64 addr, npages;
a2172e25 476
a2172e25
HY
477 /* Make EFI runtime service code area executable */
478 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
479 md = p;
1c083eb2
HY
480
481 if (md->type != EFI_RUNTIME_SERVICES_CODE)
482 continue;
483
4a3575fd
HY
484 addr = md->virt_addr;
485 npages = md->num_pages;
486 memrange_efi_to_native(&addr, &npages);
487 set_memory_x(addr, npages);
a2172e25 488 }
a2172e25 489}
a2172e25 490
5b83683f
HY
491/*
492 * This function will switch the EFI runtime services to virtual mode.
493 * Essentially, look through the EFI memmap and map every region that
494 * has the runtime attribute bit set in its memory descriptor and update
495 * that memory descriptor with the virtual address obtained from ioremap().
496 * This enables the runtime services to be called without having to
497 * thunk back into physical mode for every invocation.
498 */
499void __init efi_enter_virtual_mode(void)
500{
501 efi_memory_desc_t *md;
502 efi_status_t status;
1c083eb2 503 unsigned long size;
dd39ecf5 504 u64 end, systab, addr, npages, end_pfn;
1c083eb2 505 void *p, *va;
5b83683f
HY
506
507 efi.systab = NULL;
508 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
509 md = p;
510 if (!(md->attribute & EFI_MEMORY_RUNTIME))
511 continue;
1c083eb2
HY
512
513 size = md->num_pages << EFI_PAGE_SHIFT;
514 end = md->phys_addr + size;
515
dd39ecf5
HY
516 end_pfn = PFN_UP(end);
517 if (end_pfn <= max_low_pfn_mapped
518 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
519 && end_pfn <= max_pfn_mapped))
1c083eb2 520 va = __va(md->phys_addr);
5b83683f 521 else
6a7bbd57 522 va = efi_ioremap(md->phys_addr, size, md->type);
1c083eb2 523
1c083eb2
HY
524 md->virt_addr = (u64) (unsigned long) va;
525
526 if (!va) {
5b83683f
HY
527 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
528 (unsigned long long)md->phys_addr);
1c083eb2
HY
529 continue;
530 }
531
4a3575fd
HY
532 if (!(md->attribute & EFI_MEMORY_WB)) {
533 addr = md->virt_addr;
534 npages = md->num_pages;
535 memrange_efi_to_native(&addr, &npages);
536 set_memory_uc(addr, npages);
537 }
e85f2051 538
1c083eb2
HY
539 systab = (u64) (unsigned long) efi_phys.systab;
540 if (md->phys_addr <= systab && systab < end) {
541 systab += md->virt_addr - md->phys_addr;
542 efi.systab = (efi_system_table_t *) (unsigned long) systab;
543 }
5b83683f
HY
544 }
545
546 BUG_ON(!efi.systab);
547
548 status = phys_efi_set_virtual_address_map(
549 memmap.desc_size * memmap.nr_map,
550 memmap.desc_size,
551 memmap.desc_version,
552 memmap.phys_map);
553
554 if (status != EFI_SUCCESS) {
555 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
556 "(status=%lx)!\n", status);
557 panic("EFI call to SetVirtualAddressMap() failed!");
558 }
559
560 /*
561 * Now that EFI is in virtual mode, update the function
562 * pointers in the runtime service table to the new virtual addresses.
563 *
564 * Call EFI services through wrapper functions.
565 */
566 efi.get_time = virt_efi_get_time;
567 efi.set_time = virt_efi_set_time;
568 efi.get_wakeup_time = virt_efi_get_wakeup_time;
569 efi.set_wakeup_time = virt_efi_set_wakeup_time;
570 efi.get_variable = virt_efi_get_variable;
571 efi.get_next_variable = virt_efi_get_next_variable;
572 efi.set_variable = virt_efi_set_variable;
573 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
574 efi.reset_system = virt_efi_reset_system;
575 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
4de0d4a6
HY
576 if (__supported_pte_mask & _PAGE_NX)
577 runtime_code_page_mkexec();
a3828064
HY
578 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
579 memmap.map = NULL;
5b83683f
HY
580}
581
582/*
583 * Convenience functions to obtain memory types and attributes
584 */
585u32 efi_mem_type(unsigned long phys_addr)
586{
587 efi_memory_desc_t *md;
588 void *p;
589
590 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
591 md = p;
592 if ((md->phys_addr <= phys_addr) &&
593 (phys_addr < (md->phys_addr +
594 (md->num_pages << EFI_PAGE_SHIFT))))
595 return md->type;
596 }
597 return 0;
598}
599
600u64 efi_mem_attributes(unsigned long phys_addr)
601{
602 efi_memory_desc_t *md;
603 void *p;
604
605 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
606 md = p;
607 if ((md->phys_addr <= phys_addr) &&
608 (phys_addr < (md->phys_addr +
609 (md->num_pages << EFI_PAGE_SHIFT))))
610 return md->attribute;
611 }
612 return 0;
613}
This page took 0.348276 seconds and 5 git commands to generate.