x86,efi: Implement efi_no_storage_paranoia parameter
[pandora-kernel.git] / arch / x86 / platform / efi / efi.c
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/export.h>
33 #include <linux/bootmem.h>
34 #include <linux/memblock.h>
35 #include <linux/spinlock.h>
36 #include <linux/uaccess.h>
37 #include <linux/time.h>
38 #include <linux/io.h>
39 #include <linux/reboot.h>
40 #include <linux/bcd.h>
41
42 #include <asm/setup.h>
43 #include <asm/efi.h>
44 #include <asm/time.h>
45 #include <asm/cacheflush.h>
46 #include <asm/tlbflush.h>
47 #include <asm/x86_init.h>
48
49 #define EFI_DEBUG       1
50 #define PFX             "EFI: "
51
52 struct efi __read_mostly efi = {
53         .mps        = EFI_INVALID_TABLE_ADDR,
54         .acpi       = EFI_INVALID_TABLE_ADDR,
55         .acpi20     = EFI_INVALID_TABLE_ADDR,
56         .smbios     = EFI_INVALID_TABLE_ADDR,
57         .sal_systab = EFI_INVALID_TABLE_ADDR,
58         .boot_info  = EFI_INVALID_TABLE_ADDR,
59         .hcdp       = EFI_INVALID_TABLE_ADDR,
60         .uga        = EFI_INVALID_TABLE_ADDR,
61         .uv_systab  = EFI_INVALID_TABLE_ADDR,
62 };
63 EXPORT_SYMBOL(efi);
64
65 struct efi_memory_map memmap;
66
67 static struct efi efi_phys __initdata;
68 static efi_system_table_t efi_systab __initdata;
69
70 static inline bool efi_is_native(void)
71 {
72         return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
73 }
74
75 unsigned long x86_efi_facility;
76
77 /*
78  * Returns 1 if 'facility' is enabled, 0 otherwise.
79  */
80 int efi_enabled(int facility)
81 {
82         return test_bit(facility, &x86_efi_facility) != 0;
83 }
84 EXPORT_SYMBOL(efi_enabled);
85
86 static bool disable_runtime = false;
87 static int __init setup_noefi(char *arg)
88 {
89         disable_runtime = true;
90         return 0;
91 }
92 early_param("noefi", setup_noefi);
93
94 int add_efi_memmap;
95 EXPORT_SYMBOL(add_efi_memmap);
96
97 static int __init setup_add_efi_memmap(char *arg)
98 {
99         add_efi_memmap = 1;
100         return 0;
101 }
102 early_param("add_efi_memmap", setup_add_efi_memmap);
103
104 static bool efi_no_storage_paranoia;
105
106 static int __init setup_storage_paranoia(char *arg)
107 {
108         efi_no_storage_paranoia = true;
109         return 0;
110 }
111 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
112
113
114 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
115 {
116         unsigned long flags;
117         efi_status_t status;
118
119         spin_lock_irqsave(&rtc_lock, flags);
120         status = efi_call_virt2(get_time, tm, tc);
121         spin_unlock_irqrestore(&rtc_lock, flags);
122         return status;
123 }
124
125 static efi_status_t virt_efi_set_time(efi_time_t *tm)
126 {
127         unsigned long flags;
128         efi_status_t status;
129
130         spin_lock_irqsave(&rtc_lock, flags);
131         status = efi_call_virt1(set_time, tm);
132         spin_unlock_irqrestore(&rtc_lock, flags);
133         return status;
134 }
135
136 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
137                                              efi_bool_t *pending,
138                                              efi_time_t *tm)
139 {
140         unsigned long flags;
141         efi_status_t status;
142
143         spin_lock_irqsave(&rtc_lock, flags);
144         status = efi_call_virt3(get_wakeup_time,
145                                 enabled, pending, tm);
146         spin_unlock_irqrestore(&rtc_lock, flags);
147         return status;
148 }
149
150 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
151 {
152         unsigned long flags;
153         efi_status_t status;
154
155         spin_lock_irqsave(&rtc_lock, flags);
156         status = efi_call_virt2(set_wakeup_time,
157                                 enabled, tm);
158         spin_unlock_irqrestore(&rtc_lock, flags);
159         return status;
160 }
161
162 static efi_status_t virt_efi_get_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(get_variable,
169                               name, vendor, attr,
170                               data_size, data);
171 }
172
173 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
174                                                efi_char16_t *name,
175                                                efi_guid_t *vendor)
176 {
177         return efi_call_virt3(get_next_variable,
178                               name_size, name, vendor);
179 }
180
181 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
182                                           efi_guid_t *vendor,
183                                           u32 attr,
184                                           unsigned long data_size,
185                                           void *data)
186 {
187         return efi_call_virt5(set_variable,
188                               name, vendor, attr,
189                               data_size, data);
190 }
191
192 static efi_status_t virt_efi_query_variable_info(u32 attr,
193                                                  u64 *storage_space,
194                                                  u64 *remaining_space,
195                                                  u64 *max_variable_size)
196 {
197         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
198                 return EFI_UNSUPPORTED;
199
200         return efi_call_virt4(query_variable_info, attr, storage_space,
201                               remaining_space, max_variable_size);
202 }
203
204 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
205 {
206         return efi_call_virt1(get_next_high_mono_count, count);
207 }
208
209 static void virt_efi_reset_system(int reset_type,
210                                   efi_status_t status,
211                                   unsigned long data_size,
212                                   efi_char16_t *data)
213 {
214         efi_call_virt4(reset_system, reset_type, status,
215                        data_size, data);
216 }
217
218 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
219                                             unsigned long count,
220                                             unsigned long sg_list)
221 {
222         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
223                 return EFI_UNSUPPORTED;
224
225         return efi_call_virt3(update_capsule, capsules, count, sg_list);
226 }
227
228 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
229                                                 unsigned long count,
230                                                 u64 *max_size,
231                                                 int *reset_type)
232 {
233         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
234                 return EFI_UNSUPPORTED;
235
236         return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
237                               reset_type);
238 }
239
240 static efi_status_t __init phys_efi_set_virtual_address_map(
241         unsigned long memory_map_size,
242         unsigned long descriptor_size,
243         u32 descriptor_version,
244         efi_memory_desc_t *virtual_map)
245 {
246         efi_status_t status;
247
248         efi_call_phys_prelog();
249         status = efi_call_phys4(efi_phys.set_virtual_address_map,
250                                 memory_map_size, descriptor_size,
251                                 descriptor_version, virtual_map);
252         efi_call_phys_epilog();
253         return status;
254 }
255
256 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
257                                              efi_time_cap_t *tc)
258 {
259         unsigned long flags;
260         efi_status_t status;
261
262         spin_lock_irqsave(&rtc_lock, flags);
263         efi_call_phys_prelog();
264         status = efi_call_phys2(efi_phys.get_time, tm, tc);
265         efi_call_phys_epilog();
266         spin_unlock_irqrestore(&rtc_lock, flags);
267         return status;
268 }
269
270 int efi_set_rtc_mmss(unsigned long nowtime)
271 {
272         int real_seconds, real_minutes;
273         efi_status_t    status;
274         efi_time_t      eft;
275         efi_time_cap_t  cap;
276
277         status = efi.get_time(&eft, &cap);
278         if (status != EFI_SUCCESS) {
279                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
280                 return -1;
281         }
282
283         real_seconds = nowtime % 60;
284         real_minutes = nowtime / 60;
285         if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
286                 real_minutes += 30;
287         real_minutes %= 60;
288         eft.minute = real_minutes;
289         eft.second = real_seconds;
290
291         status = efi.set_time(&eft);
292         if (status != EFI_SUCCESS) {
293                 printk(KERN_ERR "Oops: efitime: can't write time!\n");
294                 return -1;
295         }
296         return 0;
297 }
298
299 unsigned long efi_get_time(void)
300 {
301         efi_status_t status;
302         efi_time_t eft;
303         efi_time_cap_t cap;
304
305         status = efi.get_time(&eft, &cap);
306         if (status != EFI_SUCCESS)
307                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
308
309         return mktime(eft.year, eft.month, eft.day, eft.hour,
310                       eft.minute, eft.second);
311 }
312
313 /*
314  * Tell the kernel about the EFI memory map.  This might include
315  * more than the max 128 entries that can fit in the e820 legacy
316  * (zeropage) memory map.
317  */
318
319 static void __init do_add_efi_memmap(void)
320 {
321         void *p;
322
323         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
324                 efi_memory_desc_t *md = p;
325                 unsigned long long start = md->phys_addr;
326                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
327                 int e820_type;
328
329                 switch (md->type) {
330                 case EFI_LOADER_CODE:
331                 case EFI_LOADER_DATA:
332                 case EFI_BOOT_SERVICES_CODE:
333                 case EFI_BOOT_SERVICES_DATA:
334                 case EFI_CONVENTIONAL_MEMORY:
335                         if (md->attribute & EFI_MEMORY_WB)
336                                 e820_type = E820_RAM;
337                         else
338                                 e820_type = E820_RESERVED;
339                         break;
340                 case EFI_ACPI_RECLAIM_MEMORY:
341                         e820_type = E820_ACPI;
342                         break;
343                 case EFI_ACPI_MEMORY_NVS:
344                         e820_type = E820_NVS;
345                         break;
346                 case EFI_UNUSABLE_MEMORY:
347                         e820_type = E820_UNUSABLE;
348                         break;
349                 default:
350                         /*
351                          * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
352                          * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
353                          * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
354                          */
355                         e820_type = E820_RESERVED;
356                         break;
357                 }
358                 e820_add_region(start, size, e820_type);
359         }
360         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
361 }
362
363 void __init efi_memblock_x86_reserve_range(void)
364 {
365         unsigned long pmap;
366
367 #ifdef CONFIG_X86_32
368         pmap = boot_params.efi_info.efi_memmap;
369 #else
370         pmap = (boot_params.efi_info.efi_memmap |
371                 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
372 #endif
373         memmap.phys_map = (void *)pmap;
374         memmap.nr_map = boot_params.efi_info.efi_memmap_size /
375                 boot_params.efi_info.efi_memdesc_size;
376         memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
377         memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
378         memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
379                       "EFI memmap");
380 }
381
382 #if EFI_DEBUG
383 static void __init print_efi_memmap(void)
384 {
385         efi_memory_desc_t *md;
386         void *p;
387         int i;
388
389         for (p = memmap.map, i = 0;
390              p < memmap.map_end;
391              p += memmap.desc_size, i++) {
392                 md = p;
393                 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
394                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
395                         i, md->type, md->attribute, md->phys_addr,
396                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
397                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
398         }
399 }
400 #endif  /*  EFI_DEBUG  */
401
402 void __init efi_reserve_boot_services(void)
403 {
404         void *p;
405
406         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
407                 efi_memory_desc_t *md = p;
408                 u64 start = md->phys_addr;
409                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
410
411                 if (md->type != EFI_BOOT_SERVICES_CODE &&
412                     md->type != EFI_BOOT_SERVICES_DATA)
413                         continue;
414                 /* Only reserve where possible:
415                  * - Not within any already allocated areas
416                  * - Not over any memory area (really needed, if above?)
417                  * - Not within any part of the kernel
418                  * - Not the bios reserved area
419                 */
420                 if ((start+size >= virt_to_phys(_text)
421                                 && start <= virt_to_phys(_end)) ||
422                         !e820_all_mapped(start, start+size, E820_RAM) ||
423                         memblock_x86_check_reserved_size(&start, &size,
424                                                         1<<EFI_PAGE_SHIFT)) {
425                         /* Could not reserve, skip it */
426                         md->num_pages = 0;
427                         memblock_dbg(PFX "Could not reserve boot range "
428                                         "[0x%010llx-0x%010llx]\n",
429                                                 start, start+size-1);
430                 } else
431                         memblock_x86_reserve_range(start, start+size,
432                                                         "EFI Boot");
433         }
434 }
435
436 static void __init efi_free_boot_services(void)
437 {
438         void *p;
439
440         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
441                 efi_memory_desc_t *md = p;
442                 unsigned long long start = md->phys_addr;
443                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
444
445                 if (md->type != EFI_BOOT_SERVICES_CODE &&
446                     md->type != EFI_BOOT_SERVICES_DATA)
447                         continue;
448
449                 /* Could not reserve boot area */
450                 if (!size)
451                         continue;
452
453                 free_bootmem_late(start, size);
454         }
455 }
456
457 void __init efi_init(void)
458 {
459         efi_config_table_t *config_tables;
460         efi_runtime_services_t *runtime;
461         efi_char16_t *c16;
462         char vendor[100] = "unknown";
463         int i = 0;
464         void *tmp;
465
466         if (!efi_is_native())
467                 return;
468
469 #ifdef CONFIG_X86_32
470         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
471 #else
472         efi_phys.systab = (efi_system_table_t *)
473                 (boot_params.efi_info.efi_systab |
474                  ((__u64)boot_params.efi_info.efi_systab_hi<<32));
475 #endif
476
477         efi.systab = early_ioremap((unsigned long)efi_phys.systab,
478                                    sizeof(efi_system_table_t));
479         if (efi.systab == NULL)
480                 printk(KERN_ERR "Couldn't map the EFI system table!\n");
481         memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
482         early_iounmap(efi.systab, sizeof(efi_system_table_t));
483         efi.systab = &efi_systab;
484
485         /*
486          * Verify the EFI Table
487          */
488         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
489                 printk(KERN_ERR "EFI system table signature incorrect!\n");
490         if ((efi.systab->hdr.revision >> 16) == 0)
491                 printk(KERN_ERR "Warning: EFI system table version "
492                        "%d.%02d, expected 1.00 or greater!\n",
493                        efi.systab->hdr.revision >> 16,
494                        efi.systab->hdr.revision & 0xffff);
495
496         set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
497
498         /*
499          * Show what we know for posterity
500          */
501         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
502         if (c16) {
503                 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
504                         vendor[i] = *c16++;
505                 vendor[i] = '\0';
506         } else
507                 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
508         early_iounmap(tmp, 2);
509
510         printk(KERN_INFO "EFI v%u.%.02u by %s\n",
511                efi.systab->hdr.revision >> 16,
512                efi.systab->hdr.revision & 0xffff, vendor);
513
514         /*
515          * Let's see what config tables the firmware passed to us.
516          */
517         config_tables = early_ioremap(
518                 efi.systab->tables,
519                 efi.systab->nr_tables * sizeof(efi_config_table_t));
520         if (config_tables == NULL)
521                 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
522
523         printk(KERN_INFO);
524         for (i = 0; i < efi.systab->nr_tables; i++) {
525                 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
526                         efi.mps = config_tables[i].table;
527                         printk(" MPS=0x%lx ", config_tables[i].table);
528                 } else if (!efi_guidcmp(config_tables[i].guid,
529                                         ACPI_20_TABLE_GUID)) {
530                         efi.acpi20 = config_tables[i].table;
531                         printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
532                 } else if (!efi_guidcmp(config_tables[i].guid,
533                                         ACPI_TABLE_GUID)) {
534                         efi.acpi = config_tables[i].table;
535                         printk(" ACPI=0x%lx ", config_tables[i].table);
536                 } else if (!efi_guidcmp(config_tables[i].guid,
537                                         SMBIOS_TABLE_GUID)) {
538                         efi.smbios = config_tables[i].table;
539                         printk(" SMBIOS=0x%lx ", config_tables[i].table);
540 #ifdef CONFIG_X86_UV
541                 } else if (!efi_guidcmp(config_tables[i].guid,
542                                         UV_SYSTEM_TABLE_GUID)) {
543                         efi.uv_systab = config_tables[i].table;
544                         printk(" UVsystab=0x%lx ", config_tables[i].table);
545 #endif
546                 } else if (!efi_guidcmp(config_tables[i].guid,
547                                         HCDP_TABLE_GUID)) {
548                         efi.hcdp = config_tables[i].table;
549                         printk(" HCDP=0x%lx ", config_tables[i].table);
550                 } else if (!efi_guidcmp(config_tables[i].guid,
551                                         UGA_IO_PROTOCOL_GUID)) {
552                         efi.uga = config_tables[i].table;
553                         printk(" UGA=0x%lx ", config_tables[i].table);
554                 }
555         }
556         printk("\n");
557         early_iounmap(config_tables,
558                           efi.systab->nr_tables * sizeof(efi_config_table_t));
559
560         set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
561
562         if (!disable_runtime) {
563                 /*
564                  * Check out the runtime services table. We need to map
565                  * the runtime services table so that we can grab the physical
566                  * address of several of the EFI runtime functions, needed to
567                  * set the firmware into virtual mode.
568                  */
569                 runtime = early_ioremap((unsigned long)efi.systab->runtime,
570                                         sizeof(efi_runtime_services_t));
571                 if (runtime != NULL) {
572                         /*
573                          * We will only need *early* access to the following
574                          * two EFI runtime services before set_virtual_address_map
575                          * is invoked.
576                          */
577                         efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
578                         efi_phys.set_virtual_address_map =
579                                 (efi_set_virtual_address_map_t *)
580                                 runtime->set_virtual_address_map;
581                         /*
582                          * Make efi_get_time can be called before entering
583                          * virtual mode.
584                          */
585                         efi.get_time = phys_efi_get_time;
586                         
587                         set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
588                 } else
589                         printk(KERN_ERR "Could not map the EFI runtime service "
590                                "table!\n");
591                 early_iounmap(runtime, sizeof(efi_runtime_services_t));
592         }
593
594         /* Map the EFI memory map */
595         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
596                                    memmap.nr_map * memmap.desc_size);
597         if (memmap.map == NULL)
598                 printk(KERN_ERR "Could not map the EFI memory map!\n");
599         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
600
601         if (memmap.desc_size != sizeof(efi_memory_desc_t))
602                 printk(KERN_WARNING
603                   "Kernel-defined memdesc doesn't match the one from EFI!\n");
604
605         if (add_efi_memmap)
606                 do_add_efi_memmap();
607
608         set_bit(EFI_MEMMAP, &x86_efi_facility);
609
610 #ifdef CONFIG_X86_32
611         x86_platform.get_wallclock = efi_get_time;
612         x86_platform.set_wallclock = efi_set_rtc_mmss;
613 #endif
614
615 #if EFI_DEBUG
616         print_efi_memmap();
617 #endif
618 }
619
620 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
621 {
622         u64 addr, npages;
623
624         addr = md->virt_addr;
625         npages = md->num_pages;
626
627         memrange_efi_to_native(&addr, &npages);
628
629         if (executable)
630                 set_memory_x(addr, npages);
631         else
632                 set_memory_nx(addr, npages);
633 }
634
635 static void __init runtime_code_page_mkexec(void)
636 {
637         efi_memory_desc_t *md;
638         void *p;
639
640         /* Make EFI runtime service code area executable */
641         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
642                 md = p;
643
644                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
645                         continue;
646
647                 efi_set_executable(md, true);
648         }
649 }
650
651 /*
652  * This function will switch the EFI runtime services to virtual mode.
653  * Essentially, look through the EFI memmap and map every region that
654  * has the runtime attribute bit set in its memory descriptor and update
655  * that memory descriptor with the virtual address obtained from ioremap().
656  * This enables the runtime services to be called without having to
657  * thunk back into physical mode for every invocation.
658  */
659 void __init efi_enter_virtual_mode(void)
660 {
661         efi_memory_desc_t *md, *prev_md = NULL;
662         efi_status_t status;
663         unsigned long size;
664         u64 end, systab, addr, npages, end_pfn;
665         void *p, *va, *new_memmap = NULL;
666         int count = 0;
667
668         efi.systab = NULL;
669
670         /* Merge contiguous regions of the same type and attribute */
671         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
672                 u64 prev_size;
673                 md = p;
674
675                 if (!prev_md) {
676                         prev_md = md;
677                         continue;
678                 }
679
680                 if (prev_md->type != md->type ||
681                     prev_md->attribute != md->attribute) {
682                         prev_md = md;
683                         continue;
684                 }
685
686                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
687
688                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
689                         prev_md->num_pages += md->num_pages;
690                         md->type = EFI_RESERVED_TYPE;
691                         md->attribute = 0;
692                         continue;
693                 }
694                 prev_md = md;
695         }
696
697         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
698                 md = p;
699                 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
700                     md->type != EFI_BOOT_SERVICES_CODE &&
701                     md->type != EFI_BOOT_SERVICES_DATA)
702                         continue;
703
704                 size = md->num_pages << EFI_PAGE_SHIFT;
705                 end = md->phys_addr + size;
706
707                 end_pfn = PFN_UP(end);
708                 if (end_pfn <= max_low_pfn_mapped
709                     || (end_pfn > (1UL << (32 - PAGE_SHIFT))
710                         && end_pfn <= max_pfn_mapped))
711                         va = __va(md->phys_addr);
712                 else
713                         va = efi_ioremap(md->phys_addr, size, md->type);
714
715                 md->virt_addr = (u64) (unsigned long) va;
716
717                 if (!va) {
718                         printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
719                                (unsigned long long)md->phys_addr);
720                         continue;
721                 }
722
723                 if (!(md->attribute & EFI_MEMORY_WB)) {
724                         addr = md->virt_addr;
725                         npages = md->num_pages;
726                         memrange_efi_to_native(&addr, &npages);
727                         set_memory_uc(addr, npages);
728                 }
729
730                 systab = (u64) (unsigned long) efi_phys.systab;
731                 if (md->phys_addr <= systab && systab < end) {
732                         systab += md->virt_addr - md->phys_addr;
733                         efi.systab = (efi_system_table_t *) (unsigned long) systab;
734                 }
735                 new_memmap = krealloc(new_memmap,
736                                       (count + 1) * memmap.desc_size,
737                                       GFP_KERNEL);
738                 memcpy(new_memmap + (count * memmap.desc_size), md,
739                        memmap.desc_size);
740                 count++;
741         }
742
743         BUG_ON(!efi.systab);
744
745         status = phys_efi_set_virtual_address_map(
746                 memmap.desc_size * count,
747                 memmap.desc_size,
748                 memmap.desc_version,
749                 (efi_memory_desc_t *)__pa(new_memmap));
750
751         if (status != EFI_SUCCESS) {
752                 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
753                        "(status=%lx)!\n", status);
754                 panic("EFI call to SetVirtualAddressMap() failed!");
755         }
756
757         /*
758          * Thankfully, it does seem that no runtime services other than
759          * SetVirtualAddressMap() will touch boot services code, so we can
760          * get rid of it all at this point
761          */
762         efi_free_boot_services();
763
764         /*
765          * Now that EFI is in virtual mode, update the function
766          * pointers in the runtime service table to the new virtual addresses.
767          *
768          * Call EFI services through wrapper functions.
769          */
770         efi.runtime_version = efi_systab.hdr.revision;
771         efi.get_time = virt_efi_get_time;
772         efi.set_time = virt_efi_set_time;
773         efi.get_wakeup_time = virt_efi_get_wakeup_time;
774         efi.set_wakeup_time = virt_efi_set_wakeup_time;
775         efi.get_variable = virt_efi_get_variable;
776         efi.get_next_variable = virt_efi_get_next_variable;
777         efi.set_variable = virt_efi_set_variable;
778         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
779         efi.reset_system = virt_efi_reset_system;
780         efi.set_virtual_address_map = NULL;
781         efi.query_variable_info = virt_efi_query_variable_info;
782         efi.update_capsule = virt_efi_update_capsule;
783         efi.query_capsule_caps = virt_efi_query_capsule_caps;
784         if (__supported_pte_mask & _PAGE_NX)
785                 runtime_code_page_mkexec();
786         clear_bit(EFI_MEMMAP, &x86_efi_facility);
787         early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
788         memmap.map = NULL;
789         kfree(new_memmap);
790 }
791
792 /*
793  * Convenience functions to obtain memory types and attributes
794  */
795 u32 efi_mem_type(unsigned long phys_addr)
796 {
797         efi_memory_desc_t *md;
798         void *p;
799
800         if (!efi_enabled(EFI_MEMMAP))
801                 return 0;
802
803         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
804                 md = p;
805                 if ((md->phys_addr <= phys_addr) &&
806                     (phys_addr < (md->phys_addr +
807                                   (md->num_pages << EFI_PAGE_SHIFT))))
808                         return md->type;
809         }
810         return 0;
811 }
812
813 u64 efi_mem_attributes(unsigned long phys_addr)
814 {
815         efi_memory_desc_t *md;
816         void *p;
817
818         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
819                 md = p;
820                 if ((md->phys_addr <= phys_addr) &&
821                     (phys_addr < (md->phys_addr +
822                                   (md->num_pages << EFI_PAGE_SHIFT))))
823                         return md->attribute;
824         }
825         return 0;
826 }
827
828 /*
829  * Some firmware has serious problems when using more than 50% of the EFI
830  * variable store, i.e. it triggers bugs that can brick machines. Ensure that
831  * we never use more than this safe limit.
832  *
833  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
834  * store.
835  */
836 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
837 {
838         efi_status_t status;
839         u64 storage_size, remaining_size, max_size;
840
841         status = efi.query_variable_info(attributes, &storage_size,
842                                          &remaining_size, &max_size);
843         if (status != EFI_SUCCESS)
844                 return status;
845
846         if (!max_size && remaining_size > size)
847                 printk_once(KERN_ERR FW_BUG "Broken EFI implementation"
848                             " is returning MaxVariableSize=0\n");
849
850         if (!storage_size || size > remaining_size ||
851             (max_size && size > max_size))
852                 return EFI_OUT_OF_RESOURCES;
853
854         if (!efi_no_storage_paranoia &&
855             (remaining_size - size) < (storage_size / 2))
856                 return EFI_OUT_OF_RESOURCES;
857
858         return EFI_SUCCESS;
859 }
860 EXPORT_SYMBOL_GPL(efi_query_variable_store);