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