1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/efi.h>
34#include <linux/export.h>
35#include <linux/bootmem.h>
36#include <linux/memblock.h>
37#include <linux/spinlock.h>
38#include <linux/uaccess.h>
39#include <linux/time.h>
40#include <linux/io.h>
41#include <linux/reboot.h>
42#include <linux/bcd.h>
43
44#include <asm/setup.h>
45#include <asm/efi.h>
46#include <asm/time.h>
47#include <asm/cacheflush.h>
48#include <asm/tlbflush.h>
49#include <asm/x86_init.h>
50
51#define EFI_DEBUG 1
52
53int efi_enabled;
54EXPORT_SYMBOL(efi_enabled);
55
56struct efi __read_mostly efi = {
57 .mps = EFI_INVALID_TABLE_ADDR,
58 .acpi = EFI_INVALID_TABLE_ADDR,
59 .acpi20 = EFI_INVALID_TABLE_ADDR,
60 .smbios = EFI_INVALID_TABLE_ADDR,
61 .sal_systab = EFI_INVALID_TABLE_ADDR,
62 .boot_info = EFI_INVALID_TABLE_ADDR,
63 .hcdp = EFI_INVALID_TABLE_ADDR,
64 .uga = EFI_INVALID_TABLE_ADDR,
65 .uv_systab = EFI_INVALID_TABLE_ADDR,
66};
67EXPORT_SYMBOL(efi);
68
69struct efi_memory_map memmap;
70
71bool efi_64bit;
72static bool efi_native;
73
74static struct efi efi_phys __initdata;
75static efi_system_table_t efi_systab __initdata;
76
77static int __init setup_noefi(char *arg)
78{
79 efi_enabled = 0;
80 return 0;
81}
82early_param("noefi", setup_noefi);
83
84int add_efi_memmap;
85EXPORT_SYMBOL(add_efi_memmap);
86
87static int __init setup_add_efi_memmap(char *arg)
88{
89 add_efi_memmap = 1;
90 return 0;
91}
92early_param("add_efi_memmap", setup_add_efi_memmap);
93
94
95static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
96{
97 unsigned long flags;
98 efi_status_t status;
99
100 spin_lock_irqsave(&rtc_lock, flags);
101 status = efi_call_virt2(get_time, tm, tc);
102 spin_unlock_irqrestore(&rtc_lock, flags);
103 return status;
104}
105
106static efi_status_t virt_efi_set_time(efi_time_t *tm)
107{
108 unsigned long flags;
109 efi_status_t status;
110
111 spin_lock_irqsave(&rtc_lock, flags);
112 status = efi_call_virt1(set_time, tm);
113 spin_unlock_irqrestore(&rtc_lock, flags);
114 return status;
115}
116
117static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
118 efi_bool_t *pending,
119 efi_time_t *tm)
120{
121 unsigned long flags;
122 efi_status_t status;
123
124 spin_lock_irqsave(&rtc_lock, flags);
125 status = efi_call_virt3(get_wakeup_time,
126 enabled, pending, tm);
127 spin_unlock_irqrestore(&rtc_lock, flags);
128 return status;
129}
130
131static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
132{
133 unsigned long flags;
134 efi_status_t status;
135
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt2(set_wakeup_time,
138 enabled, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
140 return status;
141}
142
143static efi_status_t virt_efi_get_variable(efi_char16_t *name,
144 efi_guid_t *vendor,
145 u32 *attr,
146 unsigned long *data_size,
147 void *data)
148{
149 return efi_call_virt5(get_variable,
150 name, vendor, attr,
151 data_size, data);
152}
153
154static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
155 efi_char16_t *name,
156 efi_guid_t *vendor)
157{
158 return efi_call_virt3(get_next_variable,
159 name_size, name, vendor);
160}
161
162static efi_status_t virt_efi_set_variable(efi_char16_t *name,
163 efi_guid_t *vendor,
164 u32 attr,
165 unsigned long data_size,
166 void *data)
167{
168 return efi_call_virt5(set_variable,
169 name, vendor, attr,
170 data_size, data);
171}
172
173static efi_status_t virt_efi_query_variable_info(u32 attr,
174 u64 *storage_space,
175 u64 *remaining_space,
176 u64 *max_variable_size)
177{
178 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
179 return EFI_UNSUPPORTED;
180
181 return efi_call_virt4(query_variable_info, attr, storage_space,
182 remaining_space, max_variable_size);
183}
184
185static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
186{
187 return efi_call_virt1(get_next_high_mono_count, count);
188}
189
190static void virt_efi_reset_system(int reset_type,
191 efi_status_t status,
192 unsigned long data_size,
193 efi_char16_t *data)
194{
195 efi_call_virt4(reset_system, reset_type, status,
196 data_size, data);
197}
198
199static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
200 unsigned long count,
201 unsigned long sg_list)
202{
203 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
204 return EFI_UNSUPPORTED;
205
206 return efi_call_virt3(update_capsule, capsules, count, sg_list);
207}
208
209static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
210 unsigned long count,
211 u64 *max_size,
212 int *reset_type)
213{
214 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
215 return EFI_UNSUPPORTED;
216
217 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
218 reset_type);
219}
220
221static efi_status_t __init phys_efi_set_virtual_address_map(
222 unsigned long memory_map_size,
223 unsigned long descriptor_size,
224 u32 descriptor_version,
225 efi_memory_desc_t *virtual_map)
226{
227 efi_status_t status;
228
229 efi_call_phys_prelog();
230 status = efi_call_phys4(efi_phys.set_virtual_address_map,
231 memory_map_size, descriptor_size,
232 descriptor_version, virtual_map);
233 efi_call_phys_epilog();
234 return status;
235}
236
237static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
238 efi_time_cap_t *tc)
239{
240 unsigned long flags;
241 efi_status_t status;
242
243 spin_lock_irqsave(&rtc_lock, flags);
244 efi_call_phys_prelog();
245 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
246 virt_to_phys(tc));
247 efi_call_phys_epilog();
248 spin_unlock_irqrestore(&rtc_lock, flags);
249 return status;
250}
251
252int efi_set_rtc_mmss(unsigned long nowtime)
253{
254 int real_seconds, real_minutes;
255 efi_status_t status;
256 efi_time_t eft;
257 efi_time_cap_t cap;
258
259 status = efi.get_time(&eft, &cap);
260 if (status != EFI_SUCCESS) {
261 pr_err("Oops: efitime: can't read time!\n");
262 return -1;
263 }
264
265 real_seconds = nowtime % 60;
266 real_minutes = nowtime / 60;
267 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
268 real_minutes += 30;
269 real_minutes %= 60;
270 eft.minute = real_minutes;
271 eft.second = real_seconds;
272
273 status = efi.set_time(&eft);
274 if (status != EFI_SUCCESS) {
275 pr_err("Oops: efitime: can't write time!\n");
276 return -1;
277 }
278 return 0;
279}
280
281unsigned long efi_get_time(void)
282{
283 efi_status_t status;
284 efi_time_t eft;
285 efi_time_cap_t cap;
286
287 status = efi.get_time(&eft, &cap);
288 if (status != EFI_SUCCESS)
289 pr_err("Oops: efitime: can't read time!\n");
290
291 return mktime(eft.year, eft.month, eft.day, eft.hour,
292 eft.minute, eft.second);
293}
294
295
296
297
298
299
300
301static void __init do_add_efi_memmap(void)
302{
303 void *p;
304
305 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
306 efi_memory_desc_t *md = p;
307 unsigned long long start = md->phys_addr;
308 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
309 int e820_type;
310
311 switch (md->type) {
312 case EFI_LOADER_CODE:
313 case EFI_LOADER_DATA:
314 case EFI_BOOT_SERVICES_CODE:
315 case EFI_BOOT_SERVICES_DATA:
316 case EFI_CONVENTIONAL_MEMORY:
317 if (md->attribute & EFI_MEMORY_WB)
318 e820_type = E820_RAM;
319 else
320 e820_type = E820_RESERVED;
321 break;
322 case EFI_ACPI_RECLAIM_MEMORY:
323 e820_type = E820_ACPI;
324 break;
325 case EFI_ACPI_MEMORY_NVS:
326 e820_type = E820_NVS;
327 break;
328 case EFI_UNUSABLE_MEMORY:
329 e820_type = E820_UNUSABLE;
330 break;
331 default:
332
333
334
335
336
337 e820_type = E820_RESERVED;
338 break;
339 }
340 e820_add_region(start, size, e820_type);
341 }
342 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
343}
344
345int __init efi_memblock_x86_reserve_range(void)
346{
347 unsigned long pmap;
348
349#ifdef CONFIG_X86_32
350
351 if (boot_params.efi_info.efi_memmap_hi) {
352 pr_err("Memory map is above 4GB, disabling EFI.\n");
353 return -EINVAL;
354 }
355 pmap = boot_params.efi_info.efi_memmap;
356#else
357 pmap = (boot_params.efi_info.efi_memmap |
358 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
359#endif
360 memmap.phys_map = (void *)pmap;
361 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
362 boot_params.efi_info.efi_memdesc_size;
363 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
364 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
365 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
366
367 return 0;
368}
369
370#if EFI_DEBUG
371static void __init print_efi_memmap(void)
372{
373 efi_memory_desc_t *md;
374 void *p;
375 int i;
376
377 for (p = memmap.map, i = 0;
378 p < memmap.map_end;
379 p += memmap.desc_size, i++) {
380 md = p;
381 pr_info("mem%02u: type=%u, attr=0x%llx, "
382 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
383 i, md->type, md->attribute, md->phys_addr,
384 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
385 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
386 }
387}
388#endif
389
390void __init efi_reserve_boot_services(void)
391{
392 void *p;
393
394 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
395 efi_memory_desc_t *md = p;
396 u64 start = md->phys_addr;
397 u64 size = md->num_pages << EFI_PAGE_SHIFT;
398
399 if (md->type != EFI_BOOT_SERVICES_CODE &&
400 md->type != EFI_BOOT_SERVICES_DATA)
401 continue;
402
403
404
405
406
407
408 if ((start+size >= virt_to_phys(_text)
409 && start <= virt_to_phys(_end)) ||
410 !e820_all_mapped(start, start+size, E820_RAM) ||
411 memblock_is_region_reserved(start, size)) {
412
413 md->num_pages = 0;
414 memblock_dbg("Could not reserve boot range "
415 "[0x%010llx-0x%010llx]\n",
416 start, start+size-1);
417 } else
418 memblock_reserve(start, size);
419 }
420}
421
422static void __init efi_free_boot_services(void)
423{
424 void *p;
425
426 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
427 efi_memory_desc_t *md = p;
428 unsigned long long start = md->phys_addr;
429 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
430
431 if (md->type != EFI_BOOT_SERVICES_CODE &&
432 md->type != EFI_BOOT_SERVICES_DATA)
433 continue;
434
435
436 if (!size)
437 continue;
438
439 free_bootmem_late(start, size);
440 }
441}
442
443static int __init efi_systab_init(void *phys)
444{
445 if (efi_64bit) {
446 efi_system_table_64_t *systab64;
447 u64 tmp = 0;
448
449 systab64 = early_ioremap((unsigned long)phys,
450 sizeof(*systab64));
451 if (systab64 == NULL) {
452 pr_err("Couldn't map the system table!\n");
453 return -ENOMEM;
454 }
455
456 efi_systab.hdr = systab64->hdr;
457 efi_systab.fw_vendor = systab64->fw_vendor;
458 tmp |= systab64->fw_vendor;
459 efi_systab.fw_revision = systab64->fw_revision;
460 efi_systab.con_in_handle = systab64->con_in_handle;
461 tmp |= systab64->con_in_handle;
462 efi_systab.con_in = systab64->con_in;
463 tmp |= systab64->con_in;
464 efi_systab.con_out_handle = systab64->con_out_handle;
465 tmp |= systab64->con_out_handle;
466 efi_systab.con_out = systab64->con_out;
467 tmp |= systab64->con_out;
468 efi_systab.stderr_handle = systab64->stderr_handle;
469 tmp |= systab64->stderr_handle;
470 efi_systab.stderr = systab64->stderr;
471 tmp |= systab64->stderr;
472 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
473 tmp |= systab64->runtime;
474 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
475 tmp |= systab64->boottime;
476 efi_systab.nr_tables = systab64->nr_tables;
477 efi_systab.tables = systab64->tables;
478 tmp |= systab64->tables;
479
480 early_iounmap(systab64, sizeof(*systab64));
481#ifdef CONFIG_X86_32
482 if (tmp >> 32) {
483 pr_err("EFI data located above 4GB, disabling EFI.\n");
484 return -EINVAL;
485 }
486#endif
487 } else {
488 efi_system_table_32_t *systab32;
489
490 systab32 = early_ioremap((unsigned long)phys,
491 sizeof(*systab32));
492 if (systab32 == NULL) {
493 pr_err("Couldn't map the system table!\n");
494 return -ENOMEM;
495 }
496
497 efi_systab.hdr = systab32->hdr;
498 efi_systab.fw_vendor = systab32->fw_vendor;
499 efi_systab.fw_revision = systab32->fw_revision;
500 efi_systab.con_in_handle = systab32->con_in_handle;
501 efi_systab.con_in = systab32->con_in;
502 efi_systab.con_out_handle = systab32->con_out_handle;
503 efi_systab.con_out = systab32->con_out;
504 efi_systab.stderr_handle = systab32->stderr_handle;
505 efi_systab.stderr = systab32->stderr;
506 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
507 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
508 efi_systab.nr_tables = systab32->nr_tables;
509 efi_systab.tables = systab32->tables;
510
511 early_iounmap(systab32, sizeof(*systab32));
512 }
513
514 efi.systab = &efi_systab;
515
516
517
518
519 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
520 pr_err("System table signature incorrect!\n");
521 return -EINVAL;
522 }
523 if ((efi.systab->hdr.revision >> 16) == 0)
524 pr_err("Warning: System table version "
525 "%d.%02d, expected 1.00 or greater!\n",
526 efi.systab->hdr.revision >> 16,
527 efi.systab->hdr.revision & 0xffff);
528
529 return 0;
530}
531
532static int __init efi_config_init(u64 tables, int nr_tables)
533{
534 void *config_tables, *tablep;
535 int i, sz;
536
537 if (efi_64bit)
538 sz = sizeof(efi_config_table_64_t);
539 else
540 sz = sizeof(efi_config_table_32_t);
541
542
543
544
545 config_tables = early_ioremap(tables, nr_tables * sz);
546 if (config_tables == NULL) {
547 pr_err("Could not map Configuration table!\n");
548 return -ENOMEM;
549 }
550
551 tablep = config_tables;
552 pr_info("");
553 for (i = 0; i < efi.systab->nr_tables; i++) {
554 efi_guid_t guid;
555 unsigned long table;
556
557 if (efi_64bit) {
558 u64 table64;
559 guid = ((efi_config_table_64_t *)tablep)->guid;
560 table64 = ((efi_config_table_64_t *)tablep)->table;
561 table = table64;
562#ifdef CONFIG_X86_32
563 if (table64 >> 32) {
564 pr_cont("\n");
565 pr_err("Table located above 4GB, disabling EFI.\n");
566 early_iounmap(config_tables,
567 efi.systab->nr_tables * sz);
568 return -EINVAL;
569 }
570#endif
571 } else {
572 guid = ((efi_config_table_32_t *)tablep)->guid;
573 table = ((efi_config_table_32_t *)tablep)->table;
574 }
575 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
576 efi.mps = table;
577 pr_cont(" MPS=0x%lx ", table);
578 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
579 efi.acpi20 = table;
580 pr_cont(" ACPI 2.0=0x%lx ", table);
581 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
582 efi.acpi = table;
583 pr_cont(" ACPI=0x%lx ", table);
584 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
585 efi.smbios = table;
586 pr_cont(" SMBIOS=0x%lx ", table);
587#ifdef CONFIG_X86_UV
588 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
589 efi.uv_systab = table;
590 pr_cont(" UVsystab=0x%lx ", table);
591#endif
592 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
593 efi.hcdp = table;
594 pr_cont(" HCDP=0x%lx ", table);
595 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
596 efi.uga = table;
597 pr_cont(" UGA=0x%lx ", table);
598 }
599 tablep += sz;
600 }
601 pr_cont("\n");
602 early_iounmap(config_tables, efi.systab->nr_tables * sz);
603 return 0;
604}
605
606static int __init efi_runtime_init(void)
607{
608 efi_runtime_services_t *runtime;
609
610
611
612
613
614
615
616 runtime = early_ioremap((unsigned long)efi.systab->runtime,
617 sizeof(efi_runtime_services_t));
618 if (!runtime) {
619 pr_err("Could not map the runtime service table!\n");
620 return -ENOMEM;
621 }
622
623
624
625
626
627 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
628 efi_phys.set_virtual_address_map =
629 (efi_set_virtual_address_map_t *)
630 runtime->set_virtual_address_map;
631
632
633
634
635 efi.get_time = phys_efi_get_time;
636 early_iounmap(runtime, sizeof(efi_runtime_services_t));
637
638 return 0;
639}
640
641static int __init efi_memmap_init(void)
642{
643
644 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
645 memmap.nr_map * memmap.desc_size);
646 if (memmap.map == NULL) {
647 pr_err("Could not map the memory map!\n");
648 return -ENOMEM;
649 }
650 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
651
652 if (add_efi_memmap)
653 do_add_efi_memmap();
654
655 return 0;
656}
657
658void __init efi_init(void)
659{
660 efi_char16_t *c16;
661 char vendor[100] = "unknown";
662 int i = 0;
663 void *tmp;
664
665#ifdef CONFIG_X86_32
666 if (boot_params.efi_info.efi_systab_hi ||
667 boot_params.efi_info.efi_memmap_hi) {
668 pr_info("Table located above 4GB, disabling EFI.\n");
669 efi_enabled = 0;
670 return;
671 }
672 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
673 efi_native = !efi_64bit;
674#else
675 efi_phys.systab = (efi_system_table_t *)
676 (boot_params.efi_info.efi_systab |
677 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
678 efi_native = efi_64bit;
679#endif
680
681 if (efi_systab_init(efi_phys.systab)) {
682 efi_enabled = 0;
683 return;
684 }
685
686
687
688
689 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
690 if (c16) {
691 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
692 vendor[i] = *c16++;
693 vendor[i] = '\0';
694 } else
695 pr_err("Could not map the firmware vendor!\n");
696 early_iounmap(tmp, 2);
697
698 pr_info("EFI v%u.%.02u by %s\n",
699 efi.systab->hdr.revision >> 16,
700 efi.systab->hdr.revision & 0xffff, vendor);
701
702 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) {
703 efi_enabled = 0;
704 return;
705 }
706
707
708
709
710
711
712 if (!efi_native)
713 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
714 else if (efi_runtime_init()) {
715 efi_enabled = 0;
716 return;
717 }
718
719 if (efi_memmap_init()) {
720 efi_enabled = 0;
721 return;
722 }
723#ifdef CONFIG_X86_32
724 if (efi_native) {
725 x86_platform.get_wallclock = efi_get_time;
726 x86_platform.set_wallclock = efi_set_rtc_mmss;
727 }
728#endif
729
730#if EFI_DEBUG
731 print_efi_memmap();
732#endif
733}
734
735void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
736{
737 u64 addr, npages;
738
739 addr = md->virt_addr;
740 npages = md->num_pages;
741
742 memrange_efi_to_native(&addr, &npages);
743
744 if (executable)
745 set_memory_x(addr, npages);
746 else
747 set_memory_nx(addr, npages);
748}
749
750static void __init runtime_code_page_mkexec(void)
751{
752 efi_memory_desc_t *md;
753 void *p;
754
755
756 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
757 md = p;
758
759 if (md->type != EFI_RUNTIME_SERVICES_CODE)
760 continue;
761
762 efi_set_executable(md, true);
763 }
764}
765
766
767
768
769
770
771
772
773
774void __init efi_enter_virtual_mode(void)
775{
776 efi_memory_desc_t *md, *prev_md = NULL;
777 efi_status_t status;
778 unsigned long size;
779 u64 end, systab, addr, npages, end_pfn;
780 void *p, *va, *new_memmap = NULL;
781 int count = 0;
782
783 efi.systab = NULL;
784
785
786
787
788
789
790 if (!efi_native)
791 goto out;
792
793
794 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
795 u64 prev_size;
796 md = p;
797
798 if (!prev_md) {
799 prev_md = md;
800 continue;
801 }
802
803 if (prev_md->type != md->type ||
804 prev_md->attribute != md->attribute) {
805 prev_md = md;
806 continue;
807 }
808
809 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
810
811 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
812 prev_md->num_pages += md->num_pages;
813 md->type = EFI_RESERVED_TYPE;
814 md->attribute = 0;
815 continue;
816 }
817 prev_md = md;
818 }
819
820 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
821 md = p;
822 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
823 md->type != EFI_BOOT_SERVICES_CODE &&
824 md->type != EFI_BOOT_SERVICES_DATA)
825 continue;
826
827 size = md->num_pages << EFI_PAGE_SHIFT;
828 end = md->phys_addr + size;
829
830 end_pfn = PFN_UP(end);
831 if (end_pfn <= max_low_pfn_mapped
832 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
833 && end_pfn <= max_pfn_mapped))
834 va = __va(md->phys_addr);
835 else
836 va = efi_ioremap(md->phys_addr, size, md->type);
837
838 md->virt_addr = (u64) (unsigned long) va;
839
840 if (!va) {
841 pr_err("ioremap of 0x%llX failed!\n",
842 (unsigned long long)md->phys_addr);
843 continue;
844 }
845
846 if (!(md->attribute & EFI_MEMORY_WB)) {
847 addr = md->virt_addr;
848 npages = md->num_pages;
849 memrange_efi_to_native(&addr, &npages);
850 set_memory_uc(addr, npages);
851 }
852
853 systab = (u64) (unsigned long) efi_phys.systab;
854 if (md->phys_addr <= systab && systab < end) {
855 systab += md->virt_addr - md->phys_addr;
856 efi.systab = (efi_system_table_t *) (unsigned long) systab;
857 }
858 new_memmap = krealloc(new_memmap,
859 (count + 1) * memmap.desc_size,
860 GFP_KERNEL);
861 memcpy(new_memmap + (count * memmap.desc_size), md,
862 memmap.desc_size);
863 count++;
864 }
865
866 BUG_ON(!efi.systab);
867
868 status = phys_efi_set_virtual_address_map(
869 memmap.desc_size * count,
870 memmap.desc_size,
871 memmap.desc_version,
872 (efi_memory_desc_t *)__pa(new_memmap));
873
874 if (status != EFI_SUCCESS) {
875 pr_alert("Unable to switch EFI into virtual mode "
876 "(status=%lx)!\n", status);
877 panic("EFI call to SetVirtualAddressMap() failed!");
878 }
879
880
881
882
883
884
885 efi_free_boot_services();
886
887
888
889
890
891
892
893 efi.get_time = virt_efi_get_time;
894 efi.set_time = virt_efi_set_time;
895 efi.get_wakeup_time = virt_efi_get_wakeup_time;
896 efi.set_wakeup_time = virt_efi_set_wakeup_time;
897 efi.get_variable = virt_efi_get_variable;
898 efi.get_next_variable = virt_efi_get_next_variable;
899 efi.set_variable = virt_efi_set_variable;
900 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
901 efi.reset_system = virt_efi_reset_system;
902 efi.set_virtual_address_map = NULL;
903 efi.query_variable_info = virt_efi_query_variable_info;
904 efi.update_capsule = virt_efi_update_capsule;
905 efi.query_capsule_caps = virt_efi_query_capsule_caps;
906 if (__supported_pte_mask & _PAGE_NX)
907 runtime_code_page_mkexec();
908
909out:
910 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
911 memmap.map = NULL;
912 kfree(new_memmap);
913}
914
915
916
917
918u32 efi_mem_type(unsigned long phys_addr)
919{
920 efi_memory_desc_t *md;
921 void *p;
922
923 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
924 md = p;
925 if ((md->phys_addr <= phys_addr) &&
926 (phys_addr < (md->phys_addr +
927 (md->num_pages << EFI_PAGE_SHIFT))))
928 return md->type;
929 }
930 return 0;
931}
932
933u64 efi_mem_attributes(unsigned long phys_addr)
934{
935 efi_memory_desc_t *md;
936 void *p;
937
938 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
939 md = p;
940 if ((md->phys_addr <= phys_addr) &&
941 (phys_addr < (md->phys_addr +
942 (md->num_pages << EFI_PAGE_SHIFT))))
943 return md->attribute;
944 }
945 return 0;
946}
947