memblock, x86: Replace memblock_x86_reserve/free_range() with generic ones
[linux-2.6-block.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
5b83683f
HY
148static efi_status_t __init phys_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 efi_status_t status;
155
156 efi_call_phys_prelog();
157 status = efi_call_phys4(efi_phys.set_virtual_address_map,
158 memory_map_size, descriptor_size,
159 descriptor_version, virtual_map);
160 efi_call_phys_epilog();
161 return status;
162}
163
164static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
165 efi_time_cap_t *tc)
166{
167 efi_status_t status;
168
169 efi_call_phys_prelog();
170 status = efi_call_phys2(efi_phys.get_time, tm, tc);
171 efi_call_phys_epilog();
172 return status;
173}
174
175int efi_set_rtc_mmss(unsigned long nowtime)
176{
177 int real_seconds, real_minutes;
178 efi_status_t status;
179 efi_time_t eft;
180 efi_time_cap_t cap;
181
182 status = efi.get_time(&eft, &cap);
183 if (status != EFI_SUCCESS) {
184 printk(KERN_ERR "Oops: efitime: can't read time!\n");
185 return -1;
186 }
187
188 real_seconds = nowtime % 60;
189 real_minutes = nowtime / 60;
190 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
191 real_minutes += 30;
192 real_minutes %= 60;
193 eft.minute = real_minutes;
194 eft.second = real_seconds;
195
196 status = efi.set_time(&eft);
197 if (status != EFI_SUCCESS) {
198 printk(KERN_ERR "Oops: efitime: can't write time!\n");
199 return -1;
200 }
201 return 0;
202}
203
204unsigned long efi_get_time(void)
205{
206 efi_status_t status;
207 efi_time_t eft;
208 efi_time_cap_t cap;
209
210 status = efi.get_time(&eft, &cap);
211 if (status != EFI_SUCCESS)
212 printk(KERN_ERR "Oops: efitime: can't read time!\n");
213
214 return mktime(eft.year, eft.month, eft.day, eft.hour,
215 eft.minute, eft.second);
216}
217
69c91893
PJ
218/*
219 * Tell the kernel about the EFI memory map. This might include
220 * more than the max 128 entries that can fit in the e820 legacy
221 * (zeropage) memory map.
222 */
223
200001eb 224static void __init do_add_efi_memmap(void)
69c91893
PJ
225{
226 void *p;
227
228 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
229 efi_memory_desc_t *md = p;
230 unsigned long long start = md->phys_addr;
231 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
232 int e820_type;
233
e2a71476
CW
234 switch (md->type) {
235 case EFI_LOADER_CODE:
236 case EFI_LOADER_DATA:
237 case EFI_BOOT_SERVICES_CODE:
238 case EFI_BOOT_SERVICES_DATA:
239 case EFI_CONVENTIONAL_MEMORY:
240 if (md->attribute & EFI_MEMORY_WB)
241 e820_type = E820_RAM;
242 else
243 e820_type = E820_RESERVED;
244 break;
245 case EFI_ACPI_RECLAIM_MEMORY:
246 e820_type = E820_ACPI;
247 break;
248 case EFI_ACPI_MEMORY_NVS:
249 e820_type = E820_NVS;
250 break;
251 case EFI_UNUSABLE_MEMORY:
252 e820_type = E820_UNUSABLE;
253 break;
254 default:
255 /*
256 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
257 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
258 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
259 */
69c91893 260 e820_type = E820_RESERVED;
e2a71476
CW
261 break;
262 }
d0be6bde 263 e820_add_region(start, size, e820_type);
69c91893
PJ
264 }
265 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
266}
267
a9ce6bc1 268void __init efi_memblock_x86_reserve_range(void)
ecacf09f
HY
269{
270 unsigned long pmap;
271
05486fa7 272#ifdef CONFIG_X86_32
ecacf09f 273 pmap = boot_params.efi_info.efi_memmap;
05486fa7
PJ
274#else
275 pmap = (boot_params.efi_info.efi_memmap |
276 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
ecacf09f
HY
277#endif
278 memmap.phys_map = (void *)pmap;
279 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
280 boot_params.efi_info.efi_memdesc_size;
281 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
282 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
24aa0788 283 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
ecacf09f
HY
284}
285
5b83683f
HY
286#if EFI_DEBUG
287static void __init print_efi_memmap(void)
288{
289 efi_memory_desc_t *md;
290 void *p;
291 int i;
292
293 for (p = memmap.map, i = 0;
294 p < memmap.map_end;
295 p += memmap.desc_size, i++) {
296 md = p;
297 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
298 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
299 i, md->type, md->attribute, md->phys_addr,
300 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
301 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
302 }
303}
304#endif /* EFI_DEBUG */
305
916f676f
MG
306void __init efi_reserve_boot_services(void)
307{
308 void *p;
309
310 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
311 efi_memory_desc_t *md = p;
7d68dc3f
ML
312 u64 start = md->phys_addr;
313 u64 size = md->num_pages << EFI_PAGE_SHIFT;
916f676f
MG
314
315 if (md->type != EFI_BOOT_SERVICES_CODE &&
316 md->type != EFI_BOOT_SERVICES_DATA)
317 continue;
7d68dc3f
ML
318 /* Only reserve where possible:
319 * - Not within any already allocated areas
320 * - Not over any memory area (really needed, if above?)
321 * - Not within any part of the kernel
322 * - Not the bios reserved area
323 */
324 if ((start+size >= virt_to_phys(_text)
325 && start <= virt_to_phys(_end)) ||
326 !e820_all_mapped(start, start+size, E820_RAM) ||
bf61549a 327 memblock_is_region_reserved(start, size)) {
7d68dc3f
ML
328 /* Could not reserve, skip it */
329 md->num_pages = 0;
330 memblock_dbg(PFX "Could not reserve boot range "
331 "[0x%010llx-0x%010llx]\n",
332 start, start+size-1);
333 } else
24aa0788 334 memblock_reserve(start, size);
916f676f
MG
335 }
336}
337
338static void __init efi_free_boot_services(void)
339{
340 void *p;
341
342 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
343 efi_memory_desc_t *md = p;
344 unsigned long long start = md->phys_addr;
345 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
346
347 if (md->type != EFI_BOOT_SERVICES_CODE &&
348 md->type != EFI_BOOT_SERVICES_DATA)
349 continue;
350
7d68dc3f
ML
351 /* Could not reserve boot area */
352 if (!size)
353 continue;
354
916f676f
MG
355 free_bootmem_late(start, size);
356 }
357}
358
5b83683f
HY
359void __init efi_init(void)
360{
361 efi_config_table_t *config_tables;
362 efi_runtime_services_t *runtime;
363 efi_char16_t *c16;
364 char vendor[100] = "unknown";
365 int i = 0;
366 void *tmp;
367
05486fa7 368#ifdef CONFIG_X86_32
5b83683f 369 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
05486fa7
PJ
370#else
371 efi_phys.systab = (efi_system_table_t *)
372 (boot_params.efi_info.efi_systab |
373 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
5b83683f 374#endif
5b83683f 375
beacfaac
HY
376 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
377 sizeof(efi_system_table_t));
5b83683f
HY
378 if (efi.systab == NULL)
379 printk(KERN_ERR "Couldn't map the EFI system table!\n");
380 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
beacfaac 381 early_iounmap(efi.systab, sizeof(efi_system_table_t));
5b83683f
HY
382 efi.systab = &efi_systab;
383
384 /*
385 * Verify the EFI Table
386 */
387 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
388 printk(KERN_ERR "EFI system table signature incorrect!\n");
389 if ((efi.systab->hdr.revision >> 16) == 0)
390 printk(KERN_ERR "Warning: EFI system table version "
391 "%d.%02d, expected 1.00 or greater!\n",
392 efi.systab->hdr.revision >> 16,
393 efi.systab->hdr.revision & 0xffff);
394
395 /*
396 * Show what we know for posterity
397 */
beacfaac 398 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
5b83683f 399 if (c16) {
fdb8a427 400 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
5b83683f
HY
401 vendor[i] = *c16++;
402 vendor[i] = '\0';
403 } else
404 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
beacfaac 405 early_iounmap(tmp, 2);
5b83683f 406
3235dc3f 407 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
5b83683f
HY
408 efi.systab->hdr.revision >> 16,
409 efi.systab->hdr.revision & 0xffff, vendor);
410
411 /*
412 * Let's see what config tables the firmware passed to us.
413 */
beacfaac 414 config_tables = early_ioremap(
5b83683f
HY
415 efi.systab->tables,
416 efi.systab->nr_tables * sizeof(efi_config_table_t));
417 if (config_tables == NULL)
418 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
419
420 printk(KERN_INFO);
421 for (i = 0; i < efi.systab->nr_tables; i++) {
422 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
423 efi.mps = config_tables[i].table;
424 printk(" MPS=0x%lx ", config_tables[i].table);
425 } else if (!efi_guidcmp(config_tables[i].guid,
426 ACPI_20_TABLE_GUID)) {
427 efi.acpi20 = config_tables[i].table;
428 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
429 } else if (!efi_guidcmp(config_tables[i].guid,
430 ACPI_TABLE_GUID)) {
431 efi.acpi = config_tables[i].table;
432 printk(" ACPI=0x%lx ", config_tables[i].table);
433 } else if (!efi_guidcmp(config_tables[i].guid,
434 SMBIOS_TABLE_GUID)) {
435 efi.smbios = config_tables[i].table;
436 printk(" SMBIOS=0x%lx ", config_tables[i].table);
03b48632 437#ifdef CONFIG_X86_UV
a50f70b1
RA
438 } else if (!efi_guidcmp(config_tables[i].guid,
439 UV_SYSTEM_TABLE_GUID)) {
440 efi.uv_systab = config_tables[i].table;
441 printk(" UVsystab=0x%lx ", config_tables[i].table);
03b48632 442#endif
5b83683f
HY
443 } else if (!efi_guidcmp(config_tables[i].guid,
444 HCDP_TABLE_GUID)) {
445 efi.hcdp = config_tables[i].table;
446 printk(" HCDP=0x%lx ", config_tables[i].table);
447 } else if (!efi_guidcmp(config_tables[i].guid,
448 UGA_IO_PROTOCOL_GUID)) {
449 efi.uga = config_tables[i].table;
450 printk(" UGA=0x%lx ", config_tables[i].table);
451 }
452 }
453 printk("\n");
beacfaac 454 early_iounmap(config_tables,
5b83683f
HY
455 efi.systab->nr_tables * sizeof(efi_config_table_t));
456
457 /*
458 * Check out the runtime services table. We need to map
459 * the runtime services table so that we can grab the physical
460 * address of several of the EFI runtime functions, needed to
461 * set the firmware into virtual mode.
462 */
beacfaac
HY
463 runtime = early_ioremap((unsigned long)efi.systab->runtime,
464 sizeof(efi_runtime_services_t));
5b83683f
HY
465 if (runtime != NULL) {
466 /*
467 * We will only need *early* access to the following
468 * two EFI runtime services before set_virtual_address_map
469 * is invoked.
470 */
471 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
472 efi_phys.set_virtual_address_map =
473 (efi_set_virtual_address_map_t *)
474 runtime->set_virtual_address_map;
475 /*
476 * Make efi_get_time can be called before entering
477 * virtual mode.
478 */
479 efi.get_time = phys_efi_get_time;
480 } else
481 printk(KERN_ERR "Could not map the EFI runtime service "
482 "table!\n");
beacfaac 483 early_iounmap(runtime, sizeof(efi_runtime_services_t));
5b83683f
HY
484
485 /* Map the EFI memory map */
beacfaac
HY
486 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
487 memmap.nr_map * memmap.desc_size);
5b83683f
HY
488 if (memmap.map == NULL)
489 printk(KERN_ERR "Could not map the EFI memory map!\n");
490 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
175e438f 491
5b83683f 492 if (memmap.desc_size != sizeof(efi_memory_desc_t))
175e438f
RA
493 printk(KERN_WARNING
494 "Kernel-defined memdesc doesn't match the one from EFI!\n");
495
200001eb
PJ
496 if (add_efi_memmap)
497 do_add_efi_memmap();
5b83683f 498
772be899 499#ifdef CONFIG_X86_32
7bd867df
FT
500 x86_platform.get_wallclock = efi_get_time;
501 x86_platform.set_wallclock = efi_set_rtc_mmss;
772be899 502#endif
7bd867df 503
5b83683f
HY
504#if EFI_DEBUG
505 print_efi_memmap();
506#endif
507}
508
9cd2b07c
MG
509void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
510{
511 u64 addr, npages;
512
513 addr = md->virt_addr;
514 npages = md->num_pages;
515
516 memrange_efi_to_native(&addr, &npages);
517
518 if (executable)
519 set_memory_x(addr, npages);
520 else
521 set_memory_nx(addr, npages);
522}
523
a2172e25
HY
524static void __init runtime_code_page_mkexec(void)
525{
526 efi_memory_desc_t *md;
a2172e25
HY
527 void *p;
528
a2172e25
HY
529 /* Make EFI runtime service code area executable */
530 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
531 md = p;
1c083eb2
HY
532
533 if (md->type != EFI_RUNTIME_SERVICES_CODE)
534 continue;
535
9cd2b07c 536 efi_set_executable(md, true);
a2172e25 537 }
a2172e25 538}
a2172e25 539
5b83683f
HY
540/*
541 * This function will switch the EFI runtime services to virtual mode.
542 * Essentially, look through the EFI memmap and map every region that
543 * has the runtime attribute bit set in its memory descriptor and update
544 * that memory descriptor with the virtual address obtained from ioremap().
545 * This enables the runtime services to be called without having to
546 * thunk back into physical mode for every invocation.
547 */
548void __init efi_enter_virtual_mode(void)
549{
202f9d0a 550 efi_memory_desc_t *md, *prev_md = NULL;
5b83683f 551 efi_status_t status;
1c083eb2 552 unsigned long size;
dd39ecf5 553 u64 end, systab, addr, npages, end_pfn;
7cb00b72
MG
554 void *p, *va, *new_memmap = NULL;
555 int count = 0;
5b83683f
HY
556
557 efi.systab = NULL;
202f9d0a
MG
558
559 /* Merge contiguous regions of the same type and attribute */
560 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
561 u64 prev_size;
562 md = p;
563
564 if (!prev_md) {
565 prev_md = md;
566 continue;
567 }
568
569 if (prev_md->type != md->type ||
570 prev_md->attribute != md->attribute) {
571 prev_md = md;
572 continue;
573 }
574
575 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
576
577 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
578 prev_md->num_pages += md->num_pages;
579 md->type = EFI_RESERVED_TYPE;
580 md->attribute = 0;
581 continue;
582 }
583 prev_md = md;
584 }
585
5b83683f
HY
586 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
587 md = p;
916f676f
MG
588 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
589 md->type != EFI_BOOT_SERVICES_CODE &&
590 md->type != EFI_BOOT_SERVICES_DATA)
5b83683f 591 continue;
1c083eb2
HY
592
593 size = md->num_pages << EFI_PAGE_SHIFT;
594 end = md->phys_addr + size;
595
dd39ecf5
HY
596 end_pfn = PFN_UP(end);
597 if (end_pfn <= max_low_pfn_mapped
598 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
599 && end_pfn <= max_pfn_mapped))
1c083eb2 600 va = __va(md->phys_addr);
5b83683f 601 else
6a7bbd57 602 va = efi_ioremap(md->phys_addr, size, md->type);
1c083eb2 603
1c083eb2
HY
604 md->virt_addr = (u64) (unsigned long) va;
605
606 if (!va) {
5b83683f
HY
607 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
608 (unsigned long long)md->phys_addr);
1c083eb2
HY
609 continue;
610 }
611
4a3575fd
HY
612 if (!(md->attribute & EFI_MEMORY_WB)) {
613 addr = md->virt_addr;
614 npages = md->num_pages;
615 memrange_efi_to_native(&addr, &npages);
616 set_memory_uc(addr, npages);
617 }
e85f2051 618
1c083eb2
HY
619 systab = (u64) (unsigned long) efi_phys.systab;
620 if (md->phys_addr <= systab && systab < end) {
621 systab += md->virt_addr - md->phys_addr;
622 efi.systab = (efi_system_table_t *) (unsigned long) systab;
623 }
7cb00b72
MG
624 new_memmap = krealloc(new_memmap,
625 (count + 1) * memmap.desc_size,
626 GFP_KERNEL);
627 memcpy(new_memmap + (count * memmap.desc_size), md,
628 memmap.desc_size);
629 count++;
5b83683f
HY
630 }
631
632 BUG_ON(!efi.systab);
633
634 status = phys_efi_set_virtual_address_map(
7cb00b72 635 memmap.desc_size * count,
5b83683f
HY
636 memmap.desc_size,
637 memmap.desc_version,
7cb00b72 638 (efi_memory_desc_t *)__pa(new_memmap));
5b83683f
HY
639
640 if (status != EFI_SUCCESS) {
641 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
642 "(status=%lx)!\n", status);
643 panic("EFI call to SetVirtualAddressMap() failed!");
644 }
645
916f676f
MG
646 /*
647 * Thankfully, it does seem that no runtime services other than
648 * SetVirtualAddressMap() will touch boot services code, so we can
649 * get rid of it all at this point
650 */
651 efi_free_boot_services();
652
5b83683f
HY
653 /*
654 * Now that EFI is in virtual mode, update the function
655 * pointers in the runtime service table to the new virtual addresses.
656 *
657 * Call EFI services through wrapper functions.
658 */
659 efi.get_time = virt_efi_get_time;
660 efi.set_time = virt_efi_set_time;
661 efi.get_wakeup_time = virt_efi_get_wakeup_time;
662 efi.set_wakeup_time = virt_efi_set_wakeup_time;
663 efi.get_variable = virt_efi_get_variable;
664 efi.get_next_variable = virt_efi_get_next_variable;
665 efi.set_variable = virt_efi_set_variable;
666 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
667 efi.reset_system = virt_efi_reset_system;
2b5e8ef3 668 efi.set_virtual_address_map = NULL;
4de0d4a6
HY
669 if (__supported_pte_mask & _PAGE_NX)
670 runtime_code_page_mkexec();
a3828064
HY
671 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
672 memmap.map = NULL;
7cb00b72 673 kfree(new_memmap);
5b83683f
HY
674}
675
676/*
677 * Convenience functions to obtain memory types and attributes
678 */
679u32 efi_mem_type(unsigned long phys_addr)
680{
681 efi_memory_desc_t *md;
682 void *p;
683
684 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
685 md = p;
686 if ((md->phys_addr <= phys_addr) &&
687 (phys_addr < (md->phys_addr +
688 (md->num_pages << EFI_PAGE_SHIFT))))
689 return md->type;
690 }
691 return 0;
692}
693
694u64 efi_mem_attributes(unsigned long phys_addr)
695{
696 efi_memory_desc_t *md;
697 void *p;
698
699 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
700 md = p;
701 if ((md->phys_addr <= phys_addr) &&
702 (phys_addr < (md->phys_addr +
703 (md->num_pages << EFI_PAGE_SHIFT))))
704 return md->attribute;
705 }
706 return 0;
707}