]> err.no Git - linux-2.6/blob - arch/x86/kernel/efi.c
x86 boot: add code to add BIOS provided EFI memory entries to kernel
[linux-2.6] / arch / x86 / kernel / 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/spinlock.h>
34 #include <linux/uaccess.h>
35 #include <linux/time.h>
36 #include <linux/io.h>
37 #include <linux/reboot.h>
38 #include <linux/bcd.h>
39
40 #include <asm/setup.h>
41 #include <asm/efi.h>
42 #include <asm/time.h>
43 #include <asm/cacheflush.h>
44 #include <asm/tlbflush.h>
45
46 #define EFI_DEBUG       1
47 #define PFX             "EFI: "
48
49 int efi_enabled;
50 EXPORT_SYMBOL(efi_enabled);
51
52 struct efi efi;
53 EXPORT_SYMBOL(efi);
54
55 struct efi_memory_map memmap;
56
57 static struct efi efi_phys __initdata;
58 static efi_system_table_t efi_systab __initdata;
59
60 static int __init setup_noefi(char *arg)
61 {
62         efi_enabled = 0;
63         return 0;
64 }
65 early_param("noefi", setup_noefi);
66
67 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
68 {
69         return efi_call_virt2(get_time, tm, tc);
70 }
71
72 static efi_status_t virt_efi_set_time(efi_time_t *tm)
73 {
74         return efi_call_virt1(set_time, tm);
75 }
76
77 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
78                                              efi_bool_t *pending,
79                                              efi_time_t *tm)
80 {
81         return efi_call_virt3(get_wakeup_time,
82                               enabled, pending, tm);
83 }
84
85 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
86 {
87         return efi_call_virt2(set_wakeup_time,
88                               enabled, tm);
89 }
90
91 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
92                                           efi_guid_t *vendor,
93                                           u32 *attr,
94                                           unsigned long *data_size,
95                                           void *data)
96 {
97         return efi_call_virt5(get_variable,
98                               name, vendor, attr,
99                               data_size, data);
100 }
101
102 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
103                                                efi_char16_t *name,
104                                                efi_guid_t *vendor)
105 {
106         return efi_call_virt3(get_next_variable,
107                               name_size, name, vendor);
108 }
109
110 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
111                                           efi_guid_t *vendor,
112                                           unsigned long attr,
113                                           unsigned long data_size,
114                                           void *data)
115 {
116         return efi_call_virt5(set_variable,
117                               name, vendor, attr,
118                               data_size, data);
119 }
120
121 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
122 {
123         return efi_call_virt1(get_next_high_mono_count, count);
124 }
125
126 static void virt_efi_reset_system(int reset_type,
127                                   efi_status_t status,
128                                   unsigned long data_size,
129                                   efi_char16_t *data)
130 {
131         efi_call_virt4(reset_system, reset_type, status,
132                        data_size, data);
133 }
134
135 static efi_status_t virt_efi_set_virtual_address_map(
136         unsigned long memory_map_size,
137         unsigned long descriptor_size,
138         u32 descriptor_version,
139         efi_memory_desc_t *virtual_map)
140 {
141         return efi_call_virt4(set_virtual_address_map,
142                               memory_map_size, descriptor_size,
143                               descriptor_version, virtual_map);
144 }
145
146 static efi_status_t __init phys_efi_set_virtual_address_map(
147         unsigned long memory_map_size,
148         unsigned long descriptor_size,
149         u32 descriptor_version,
150         efi_memory_desc_t *virtual_map)
151 {
152         efi_status_t status;
153
154         efi_call_phys_prelog();
155         status = efi_call_phys4(efi_phys.set_virtual_address_map,
156                                 memory_map_size, descriptor_size,
157                                 descriptor_version, virtual_map);
158         efi_call_phys_epilog();
159         return status;
160 }
161
162 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
163                                              efi_time_cap_t *tc)
164 {
165         efi_status_t status;
166
167         efi_call_phys_prelog();
168         status = efi_call_phys2(efi_phys.get_time, tm, tc);
169         efi_call_phys_epilog();
170         return status;
171 }
172
173 int efi_set_rtc_mmss(unsigned long nowtime)
174 {
175         int real_seconds, real_minutes;
176         efi_status_t    status;
177         efi_time_t      eft;
178         efi_time_cap_t  cap;
179
180         status = efi.get_time(&eft, &cap);
181         if (status != EFI_SUCCESS) {
182                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
183                 return -1;
184         }
185
186         real_seconds = nowtime % 60;
187         real_minutes = nowtime / 60;
188         if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
189                 real_minutes += 30;
190         real_minutes %= 60;
191         eft.minute = real_minutes;
192         eft.second = real_seconds;
193
194         status = efi.set_time(&eft);
195         if (status != EFI_SUCCESS) {
196                 printk(KERN_ERR "Oops: efitime: can't write time!\n");
197                 return -1;
198         }
199         return 0;
200 }
201
202 unsigned long efi_get_time(void)
203 {
204         efi_status_t status;
205         efi_time_t eft;
206         efi_time_cap_t cap;
207
208         status = efi.get_time(&eft, &cap);
209         if (status != EFI_SUCCESS)
210                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
211
212         return mktime(eft.year, eft.month, eft.day, eft.hour,
213                       eft.minute, eft.second);
214 }
215
216 /*
217  * Tell the kernel about the EFI memory map.  This might include
218  * more than the max 128 entries that can fit in the e820 legacy
219  * (zeropage) memory map.
220  */
221
222 static void __init add_efi_memmap(void)
223 {
224         void *p;
225
226         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
227                 efi_memory_desc_t *md = p;
228                 unsigned long long start = md->phys_addr;
229                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
230                 int e820_type;
231
232                 if (md->attribute & EFI_MEMORY_WB)
233                         e820_type = E820_RAM;
234                 else
235                         e820_type = E820_RESERVED;
236                 add_memory_region(start, size, e820_type);
237         }
238         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
239 }
240
241 #if EFI_DEBUG
242 static void __init print_efi_memmap(void)
243 {
244         efi_memory_desc_t *md;
245         void *p;
246         int i;
247
248         for (p = memmap.map, i = 0;
249              p < memmap.map_end;
250              p += memmap.desc_size, i++) {
251                 md = p;
252                 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
253                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
254                         i, md->type, md->attribute, md->phys_addr,
255                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
256                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
257         }
258 }
259 #endif  /*  EFI_DEBUG  */
260
261 void __init efi_init(void)
262 {
263         efi_config_table_t *config_tables;
264         efi_runtime_services_t *runtime;
265         efi_char16_t *c16;
266         char vendor[100] = "unknown";
267         int i = 0;
268         void *tmp;
269
270 #ifdef CONFIG_X86_32
271         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
272         memmap.phys_map = (void *)boot_params.efi_info.efi_memmap;
273 #else
274         efi_phys.systab = (efi_system_table_t *)
275                 (boot_params.efi_info.efi_systab |
276                  ((__u64)boot_params.efi_info.efi_systab_hi<<32));
277         memmap.phys_map = (void *)
278                 (boot_params.efi_info.efi_memmap |
279                  ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
280 #endif
281         memmap.nr_map = boot_params.efi_info.efi_memmap_size /
282                 boot_params.efi_info.efi_memdesc_size;
283         memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
284         memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
285
286         efi.systab = early_ioremap((unsigned long)efi_phys.systab,
287                                    sizeof(efi_system_table_t));
288         if (efi.systab == NULL)
289                 printk(KERN_ERR "Couldn't map the EFI system table!\n");
290         memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
291         early_iounmap(efi.systab, sizeof(efi_system_table_t));
292         efi.systab = &efi_systab;
293
294         /*
295          * Verify the EFI Table
296          */
297         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
298                 printk(KERN_ERR "EFI system table signature incorrect!\n");
299         if ((efi.systab->hdr.revision >> 16) == 0)
300                 printk(KERN_ERR "Warning: EFI system table version "
301                        "%d.%02d, expected 1.00 or greater!\n",
302                        efi.systab->hdr.revision >> 16,
303                        efi.systab->hdr.revision & 0xffff);
304
305         /*
306          * Show what we know for posterity
307          */
308         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
309         if (c16) {
310                 for (i = 0; i < sizeof(vendor) && *c16; ++i)
311                         vendor[i] = *c16++;
312                 vendor[i] = '\0';
313         } else
314                 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
315         early_iounmap(tmp, 2);
316
317         printk(KERN_INFO "EFI v%u.%.02u by %s \n",
318                efi.systab->hdr.revision >> 16,
319                efi.systab->hdr.revision & 0xffff, vendor);
320
321         /*
322          * Let's see what config tables the firmware passed to us.
323          */
324         config_tables = early_ioremap(
325                 efi.systab->tables,
326                 efi.systab->nr_tables * sizeof(efi_config_table_t));
327         if (config_tables == NULL)
328                 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
329
330         printk(KERN_INFO);
331         for (i = 0; i < efi.systab->nr_tables; i++) {
332                 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
333                         efi.mps = config_tables[i].table;
334                         printk(" MPS=0x%lx ", config_tables[i].table);
335                 } else if (!efi_guidcmp(config_tables[i].guid,
336                                         ACPI_20_TABLE_GUID)) {
337                         efi.acpi20 = config_tables[i].table;
338                         printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
339                 } else if (!efi_guidcmp(config_tables[i].guid,
340                                         ACPI_TABLE_GUID)) {
341                         efi.acpi = config_tables[i].table;
342                         printk(" ACPI=0x%lx ", config_tables[i].table);
343                 } else if (!efi_guidcmp(config_tables[i].guid,
344                                         SMBIOS_TABLE_GUID)) {
345                         efi.smbios = config_tables[i].table;
346                         printk(" SMBIOS=0x%lx ", config_tables[i].table);
347                 } else if (!efi_guidcmp(config_tables[i].guid,
348                                         HCDP_TABLE_GUID)) {
349                         efi.hcdp = config_tables[i].table;
350                         printk(" HCDP=0x%lx ", config_tables[i].table);
351                 } else if (!efi_guidcmp(config_tables[i].guid,
352                                         UGA_IO_PROTOCOL_GUID)) {
353                         efi.uga = config_tables[i].table;
354                         printk(" UGA=0x%lx ", config_tables[i].table);
355                 }
356         }
357         printk("\n");
358         early_iounmap(config_tables,
359                           efi.systab->nr_tables * sizeof(efi_config_table_t));
360
361         /*
362          * Check out the runtime services table. We need to map
363          * the runtime services table so that we can grab the physical
364          * address of several of the EFI runtime functions, needed to
365          * set the firmware into virtual mode.
366          */
367         runtime = early_ioremap((unsigned long)efi.systab->runtime,
368                                 sizeof(efi_runtime_services_t));
369         if (runtime != NULL) {
370                 /*
371                  * We will only need *early* access to the following
372                  * two EFI runtime services before set_virtual_address_map
373                  * is invoked.
374                  */
375                 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
376                 efi_phys.set_virtual_address_map =
377                         (efi_set_virtual_address_map_t *)
378                         runtime->set_virtual_address_map;
379                 /*
380                  * Make efi_get_time can be called before entering
381                  * virtual mode.
382                  */
383                 efi.get_time = phys_efi_get_time;
384         } else
385                 printk(KERN_ERR "Could not map the EFI runtime service "
386                        "table!\n");
387         early_iounmap(runtime, sizeof(efi_runtime_services_t));
388
389         /* Map the EFI memory map */
390         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
391                                    memmap.nr_map * memmap.desc_size);
392         if (memmap.map == NULL)
393                 printk(KERN_ERR "Could not map the EFI memory map!\n");
394         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
395         if (memmap.desc_size != sizeof(efi_memory_desc_t))
396                 printk(KERN_WARNING "Kernel-defined memdesc"
397                        "doesn't match the one from EFI!\n");
398         add_efi_memmap();
399
400         /* Setup for EFI runtime service */
401         reboot_type = BOOT_EFI;
402
403 #if EFI_DEBUG
404         print_efi_memmap();
405 #endif
406 }
407
408 static void __init runtime_code_page_mkexec(void)
409 {
410         efi_memory_desc_t *md;
411         void *p;
412         u64 addr, npages;
413
414         /* Make EFI runtime service code area executable */
415         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
416                 md = p;
417
418                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
419                         continue;
420
421                 addr = md->virt_addr;
422                 npages = md->num_pages;
423                 memrange_efi_to_native(&addr, &npages);
424                 set_memory_x(addr, npages);
425         }
426 }
427
428 /*
429  * This function will switch the EFI runtime services to virtual mode.
430  * Essentially, look through the EFI memmap and map every region that
431  * has the runtime attribute bit set in its memory descriptor and update
432  * that memory descriptor with the virtual address obtained from ioremap().
433  * This enables the runtime services to be called without having to
434  * thunk back into physical mode for every invocation.
435  */
436 void __init efi_enter_virtual_mode(void)
437 {
438         efi_memory_desc_t *md;
439         efi_status_t status;
440         unsigned long size;
441         u64 end, systab, addr, npages;
442         void *p, *va;
443
444         efi.systab = NULL;
445         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
446                 md = p;
447                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
448                         continue;
449
450                 size = md->num_pages << EFI_PAGE_SHIFT;
451                 end = md->phys_addr + size;
452
453                 if (PFN_UP(end) <= max_pfn_mapped)
454                         va = __va(md->phys_addr);
455                 else
456                         va = efi_ioremap(md->phys_addr, size);
457
458                 md->virt_addr = (u64) (unsigned long) va;
459
460                 if (!va) {
461                         printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
462                                (unsigned long long)md->phys_addr);
463                         continue;
464                 }
465
466                 if (!(md->attribute & EFI_MEMORY_WB)) {
467                         addr = md->virt_addr;
468                         npages = md->num_pages;
469                         memrange_efi_to_native(&addr, &npages);
470                         set_memory_uc(addr, npages);
471                 }
472
473                 systab = (u64) (unsigned long) efi_phys.systab;
474                 if (md->phys_addr <= systab && systab < end) {
475                         systab += md->virt_addr - md->phys_addr;
476                         efi.systab = (efi_system_table_t *) (unsigned long) systab;
477                 }
478         }
479
480         BUG_ON(!efi.systab);
481
482         status = phys_efi_set_virtual_address_map(
483                 memmap.desc_size * memmap.nr_map,
484                 memmap.desc_size,
485                 memmap.desc_version,
486                 memmap.phys_map);
487
488         if (status != EFI_SUCCESS) {
489                 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
490                        "(status=%lx)!\n", status);
491                 panic("EFI call to SetVirtualAddressMap() failed!");
492         }
493
494         /*
495          * Now that EFI is in virtual mode, update the function
496          * pointers in the runtime service table to the new virtual addresses.
497          *
498          * Call EFI services through wrapper functions.
499          */
500         efi.get_time = virt_efi_get_time;
501         efi.set_time = virt_efi_set_time;
502         efi.get_wakeup_time = virt_efi_get_wakeup_time;
503         efi.set_wakeup_time = virt_efi_set_wakeup_time;
504         efi.get_variable = virt_efi_get_variable;
505         efi.get_next_variable = virt_efi_get_next_variable;
506         efi.set_variable = virt_efi_set_variable;
507         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
508         efi.reset_system = virt_efi_reset_system;
509         efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
510         if (__supported_pte_mask & _PAGE_NX)
511                 runtime_code_page_mkexec();
512         early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
513         memmap.map = NULL;
514 }
515
516 /*
517  * Convenience functions to obtain memory types and attributes
518  */
519 u32 efi_mem_type(unsigned long phys_addr)
520 {
521         efi_memory_desc_t *md;
522         void *p;
523
524         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
525                 md = p;
526                 if ((md->phys_addr <= phys_addr) &&
527                     (phys_addr < (md->phys_addr +
528                                   (md->num_pages << EFI_PAGE_SHIFT))))
529                         return md->type;
530         }
531         return 0;
532 }
533
534 u64 efi_mem_attributes(unsigned long phys_addr)
535 {
536         efi_memory_desc_t *md;
537         void *p;
538
539         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
540                 md = p;
541                 if ((md->phys_addr <= phys_addr) &&
542                     (phys_addr < (md->phys_addr +
543                                   (md->num_pages << EFI_PAGE_SHIFT))))
544                         return md->attribute;
545         }
546         return 0;
547 }