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