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