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