Merge branch 'packaging' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek...
[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/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                                           unsigned long 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_get_next_high_mono_count(u32 *count)
135 {
136         return efi_call_virt1(get_next_high_mono_count, count);
137 }
138
139 static void virt_efi_reset_system(int reset_type,
140                                   efi_status_t status,
141                                   unsigned long data_size,
142                                   efi_char16_t *data)
143 {
144         efi_call_virt4(reset_system, reset_type, status,
145                        data_size, data);
146 }
147
148 static efi_status_t __init phys_efi_set_virtual_address_map(
149         unsigned long memory_map_size,
150         unsigned long descriptor_size,
151         u32 descriptor_version,
152         efi_memory_desc_t *virtual_map)
153 {
154         efi_status_t status;
155
156         efi_call_phys_prelog();
157         status = efi_call_phys4(efi_phys.set_virtual_address_map,
158                                 memory_map_size, descriptor_size,
159                                 descriptor_version, virtual_map);
160         efi_call_phys_epilog();
161         return status;
162 }
163
164 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
165                                              efi_time_cap_t *tc)
166 {
167         efi_status_t status;
168
169         efi_call_phys_prelog();
170         status = efi_call_phys2(efi_phys.get_time, tm, tc);
171         efi_call_phys_epilog();
172         return status;
173 }
174
175 int efi_set_rtc_mmss(unsigned long nowtime)
176 {
177         int real_seconds, real_minutes;
178         efi_status_t    status;
179         efi_time_t      eft;
180         efi_time_cap_t  cap;
181
182         status = efi.get_time(&eft, &cap);
183         if (status != EFI_SUCCESS) {
184                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
185                 return -1;
186         }
187
188         real_seconds = nowtime % 60;
189         real_minutes = nowtime / 60;
190         if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
191                 real_minutes += 30;
192         real_minutes %= 60;
193         eft.minute = real_minutes;
194         eft.second = real_seconds;
195
196         status = efi.set_time(&eft);
197         if (status != EFI_SUCCESS) {
198                 printk(KERN_ERR "Oops: efitime: can't write time!\n");
199                 return -1;
200         }
201         return 0;
202 }
203
204 unsigned long efi_get_time(void)
205 {
206         efi_status_t status;
207         efi_time_t eft;
208         efi_time_cap_t cap;
209
210         status = efi.get_time(&eft, &cap);
211         if (status != EFI_SUCCESS)
212                 printk(KERN_ERR "Oops: efitime: can't read time!\n");
213
214         return mktime(eft.year, eft.month, eft.day, eft.hour,
215                       eft.minute, eft.second);
216 }
217
218 /*
219  * Tell the kernel about the EFI memory map.  This might include
220  * more than the max 128 entries that can fit in the e820 legacy
221  * (zeropage) memory map.
222  */
223
224 static void __init do_add_efi_memmap(void)
225 {
226         void *p;
227
228         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
229                 efi_memory_desc_t *md = p;
230                 unsigned long long start = md->phys_addr;
231                 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
232                 int e820_type;
233
234                 switch (md->type) {
235                 case EFI_LOADER_CODE:
236                 case EFI_LOADER_DATA:
237                 case EFI_BOOT_SERVICES_CODE:
238                 case EFI_BOOT_SERVICES_DATA:
239                 case EFI_CONVENTIONAL_MEMORY:
240                         if (md->attribute & EFI_MEMORY_WB)
241                                 e820_type = E820_RAM;
242                         else
243                                 e820_type = E820_RESERVED;
244                         break;
245                 case EFI_ACPI_RECLAIM_MEMORY:
246                         e820_type = E820_ACPI;
247                         break;
248                 case EFI_ACPI_MEMORY_NVS:
249                         e820_type = E820_NVS;
250                         break;
251                 case EFI_UNUSABLE_MEMORY:
252                         e820_type = E820_UNUSABLE;
253                         break;
254                 default:
255                         /*
256                          * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
257                          * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
258                          * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
259                          */
260                         e820_type = E820_RESERVED;
261                         break;
262                 }
263                 e820_add_region(start, size, e820_type);
264         }
265         sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
266 }
267
268 void __init efi_memblock_x86_reserve_range(void)
269 {
270         unsigned long pmap;
271
272 #ifdef CONFIG_X86_32
273         pmap = boot_params.efi_info.efi_memmap;
274 #else
275         pmap = (boot_params.efi_info.efi_memmap |
276                 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
277 #endif
278         memmap.phys_map = (void *)pmap;
279         memmap.nr_map = boot_params.efi_info.efi_memmap_size /
280                 boot_params.efi_info.efi_memdesc_size;
281         memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
282         memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
283         memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
284                       "EFI memmap");
285 }
286
287 #if EFI_DEBUG
288 static void __init print_efi_memmap(void)
289 {
290         efi_memory_desc_t *md;
291         void *p;
292         int i;
293
294         for (p = memmap.map, i = 0;
295              p < memmap.map_end;
296              p += memmap.desc_size, i++) {
297                 md = p;
298                 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
299                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
300                         i, md->type, md->attribute, md->phys_addr,
301                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
302                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
303         }
304 }
305 #endif  /*  EFI_DEBUG  */
306
307 void __init efi_init(void)
308 {
309         efi_config_table_t *config_tables;
310         efi_runtime_services_t *runtime;
311         efi_char16_t *c16;
312         char vendor[100] = "unknown";
313         int i = 0;
314         void *tmp;
315
316 #ifdef CONFIG_X86_32
317         efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
318 #else
319         efi_phys.systab = (efi_system_table_t *)
320                 (boot_params.efi_info.efi_systab |
321                  ((__u64)boot_params.efi_info.efi_systab_hi<<32));
322 #endif
323
324         efi.systab = early_ioremap((unsigned long)efi_phys.systab,
325                                    sizeof(efi_system_table_t));
326         if (efi.systab == NULL)
327                 printk(KERN_ERR "Couldn't map the EFI system table!\n");
328         memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
329         early_iounmap(efi.systab, sizeof(efi_system_table_t));
330         efi.systab = &efi_systab;
331
332         /*
333          * Verify the EFI Table
334          */
335         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
336                 printk(KERN_ERR "EFI system table signature incorrect!\n");
337         if ((efi.systab->hdr.revision >> 16) == 0)
338                 printk(KERN_ERR "Warning: EFI system table version "
339                        "%d.%02d, expected 1.00 or greater!\n",
340                        efi.systab->hdr.revision >> 16,
341                        efi.systab->hdr.revision & 0xffff);
342
343         /*
344          * Show what we know for posterity
345          */
346         c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
347         if (c16) {
348                 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
349                         vendor[i] = *c16++;
350                 vendor[i] = '\0';
351         } else
352                 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
353         early_iounmap(tmp, 2);
354
355         printk(KERN_INFO "EFI v%u.%.02u by %s\n",
356                efi.systab->hdr.revision >> 16,
357                efi.systab->hdr.revision & 0xffff, vendor);
358
359         /*
360          * Let's see what config tables the firmware passed to us.
361          */
362         config_tables = early_ioremap(
363                 efi.systab->tables,
364                 efi.systab->nr_tables * sizeof(efi_config_table_t));
365         if (config_tables == NULL)
366                 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
367
368         printk(KERN_INFO);
369         for (i = 0; i < efi.systab->nr_tables; i++) {
370                 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
371                         efi.mps = config_tables[i].table;
372                         printk(" MPS=0x%lx ", config_tables[i].table);
373                 } else if (!efi_guidcmp(config_tables[i].guid,
374                                         ACPI_20_TABLE_GUID)) {
375                         efi.acpi20 = config_tables[i].table;
376                         printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
377                 } else if (!efi_guidcmp(config_tables[i].guid,
378                                         ACPI_TABLE_GUID)) {
379                         efi.acpi = config_tables[i].table;
380                         printk(" ACPI=0x%lx ", config_tables[i].table);
381                 } else if (!efi_guidcmp(config_tables[i].guid,
382                                         SMBIOS_TABLE_GUID)) {
383                         efi.smbios = config_tables[i].table;
384                         printk(" SMBIOS=0x%lx ", config_tables[i].table);
385 #ifdef CONFIG_X86_UV
386                 } else if (!efi_guidcmp(config_tables[i].guid,
387                                         UV_SYSTEM_TABLE_GUID)) {
388                         efi.uv_systab = config_tables[i].table;
389                         printk(" UVsystab=0x%lx ", config_tables[i].table);
390 #endif
391                 } else if (!efi_guidcmp(config_tables[i].guid,
392                                         HCDP_TABLE_GUID)) {
393                         efi.hcdp = config_tables[i].table;
394                         printk(" HCDP=0x%lx ", config_tables[i].table);
395                 } else if (!efi_guidcmp(config_tables[i].guid,
396                                         UGA_IO_PROTOCOL_GUID)) {
397                         efi.uga = config_tables[i].table;
398                         printk(" UGA=0x%lx ", config_tables[i].table);
399                 }
400         }
401         printk("\n");
402         early_iounmap(config_tables,
403                           efi.systab->nr_tables * sizeof(efi_config_table_t));
404
405         /*
406          * Check out the runtime services table. We need to map
407          * the runtime services table so that we can grab the physical
408          * address of several of the EFI runtime functions, needed to
409          * set the firmware into virtual mode.
410          */
411         runtime = early_ioremap((unsigned long)efi.systab->runtime,
412                                 sizeof(efi_runtime_services_t));
413         if (runtime != NULL) {
414                 /*
415                  * We will only need *early* access to the following
416                  * two EFI runtime services before set_virtual_address_map
417                  * is invoked.
418                  */
419                 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
420                 efi_phys.set_virtual_address_map =
421                         (efi_set_virtual_address_map_t *)
422                         runtime->set_virtual_address_map;
423                 /*
424                  * Make efi_get_time can be called before entering
425                  * virtual mode.
426                  */
427                 efi.get_time = phys_efi_get_time;
428         } else
429                 printk(KERN_ERR "Could not map the EFI runtime service "
430                        "table!\n");
431         early_iounmap(runtime, sizeof(efi_runtime_services_t));
432
433         /* Map the EFI memory map */
434         memmap.map = early_ioremap((unsigned long)memmap.phys_map,
435                                    memmap.nr_map * memmap.desc_size);
436         if (memmap.map == NULL)
437                 printk(KERN_ERR "Could not map the EFI memory map!\n");
438         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
439
440         if (memmap.desc_size != sizeof(efi_memory_desc_t))
441                 printk(KERN_WARNING
442                   "Kernel-defined memdesc doesn't match the one from EFI!\n");
443
444         if (add_efi_memmap)
445                 do_add_efi_memmap();
446
447 #ifdef CONFIG_X86_32
448         x86_platform.get_wallclock = efi_get_time;
449         x86_platform.set_wallclock = efi_set_rtc_mmss;
450 #endif
451
452         /* Setup for EFI runtime service */
453         reboot_type = BOOT_EFI;
454
455 #if EFI_DEBUG
456         print_efi_memmap();
457 #endif
458 }
459
460 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
461 {
462         u64 addr, npages;
463
464         addr = md->virt_addr;
465         npages = md->num_pages;
466
467         memrange_efi_to_native(&addr, &npages);
468
469         if (executable)
470                 set_memory_x(addr, npages);
471         else
472                 set_memory_nx(addr, npages);
473 }
474
475 static void __init runtime_code_page_mkexec(void)
476 {
477         efi_memory_desc_t *md;
478         void *p;
479
480         /* Make EFI runtime service code area executable */
481         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
482                 md = p;
483
484                 if (md->type != EFI_RUNTIME_SERVICES_CODE)
485                         continue;
486
487                 efi_set_executable(md, true);
488         }
489 }
490
491 /*
492  * This function will switch the EFI runtime services to virtual mode.
493  * Essentially, look through the EFI memmap and map every region that
494  * has the runtime attribute bit set in its memory descriptor and update
495  * that memory descriptor with the virtual address obtained from ioremap().
496  * This enables the runtime services to be called without having to
497  * thunk back into physical mode for every invocation.
498  */
499 void __init efi_enter_virtual_mode(void)
500 {
501         efi_memory_desc_t *md, *prev_md = NULL;
502         efi_status_t status;
503         unsigned long size;
504         u64 end, systab, addr, npages, end_pfn;
505         void *p, *va, *new_memmap = NULL;
506         int count = 0;
507
508         efi.systab = NULL;
509
510         /* Merge contiguous regions of the same type and attribute */
511         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
512                 u64 prev_size;
513                 md = p;
514
515                 if (!prev_md) {
516                         prev_md = md;
517                         continue;
518                 }
519
520                 if (prev_md->type != md->type ||
521                     prev_md->attribute != md->attribute) {
522                         prev_md = md;
523                         continue;
524                 }
525
526                 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
527
528                 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
529                         prev_md->num_pages += md->num_pages;
530                         md->type = EFI_RESERVED_TYPE;
531                         md->attribute = 0;
532                         continue;
533                 }
534                 prev_md = md;
535         }
536
537         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
538                 md = p;
539                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
540                         continue;
541
542                 size = md->num_pages << EFI_PAGE_SHIFT;
543                 end = md->phys_addr + size;
544
545                 end_pfn = PFN_UP(end);
546                 if (end_pfn <= max_low_pfn_mapped
547                     || (end_pfn > (1UL << (32 - PAGE_SHIFT))
548                         && end_pfn <= max_pfn_mapped))
549                         va = __va(md->phys_addr);
550                 else
551                         va = efi_ioremap(md->phys_addr, size, md->type);
552
553                 md->virt_addr = (u64) (unsigned long) va;
554
555                 if (!va) {
556                         printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
557                                (unsigned long long)md->phys_addr);
558                         continue;
559                 }
560
561                 if (!(md->attribute & EFI_MEMORY_WB)) {
562                         addr = md->virt_addr;
563                         npages = md->num_pages;
564                         memrange_efi_to_native(&addr, &npages);
565                         set_memory_uc(addr, npages);
566                 }
567
568                 systab = (u64) (unsigned long) efi_phys.systab;
569                 if (md->phys_addr <= systab && systab < end) {
570                         systab += md->virt_addr - md->phys_addr;
571                         efi.systab = (efi_system_table_t *) (unsigned long) systab;
572                 }
573                 new_memmap = krealloc(new_memmap,
574                                       (count + 1) * memmap.desc_size,
575                                       GFP_KERNEL);
576                 memcpy(new_memmap + (count * memmap.desc_size), md,
577                        memmap.desc_size);
578                 count++;
579         }
580
581         BUG_ON(!efi.systab);
582
583         status = phys_efi_set_virtual_address_map(
584                 memmap.desc_size * count,
585                 memmap.desc_size,
586                 memmap.desc_version,
587                 (efi_memory_desc_t *)__pa(new_memmap));
588
589         if (status != EFI_SUCCESS) {
590                 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
591                        "(status=%lx)!\n", status);
592                 panic("EFI call to SetVirtualAddressMap() failed!");
593         }
594
595         /*
596          * Now that EFI is in virtual mode, update the function
597          * pointers in the runtime service table to the new virtual addresses.
598          *
599          * Call EFI services through wrapper functions.
600          */
601         efi.get_time = virt_efi_get_time;
602         efi.set_time = virt_efi_set_time;
603         efi.get_wakeup_time = virt_efi_get_wakeup_time;
604         efi.set_wakeup_time = virt_efi_set_wakeup_time;
605         efi.get_variable = virt_efi_get_variable;
606         efi.get_next_variable = virt_efi_get_next_variable;
607         efi.set_variable = virt_efi_set_variable;
608         efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
609         efi.reset_system = virt_efi_reset_system;
610         efi.set_virtual_address_map = NULL;
611         if (__supported_pte_mask & _PAGE_NX)
612                 runtime_code_page_mkexec();
613         early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
614         memmap.map = NULL;
615         kfree(new_memmap);
616 }
617
618 /*
619  * Convenience functions to obtain memory types and attributes
620  */
621 u32 efi_mem_type(unsigned long phys_addr)
622 {
623         efi_memory_desc_t *md;
624         void *p;
625
626         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
627                 md = p;
628                 if ((md->phys_addr <= phys_addr) &&
629                     (phys_addr < (md->phys_addr +
630                                   (md->num_pages << EFI_PAGE_SHIFT))))
631                         return md->type;
632         }
633         return 0;
634 }
635
636 u64 efi_mem_attributes(unsigned long phys_addr)
637 {
638         efi_memory_desc_t *md;
639         void *p;
640
641         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
642                 md = p;
643                 if ((md->phys_addr <= phys_addr) &&
644                     (phys_addr < (md->phys_addr +
645                                   (md->num_pages << EFI_PAGE_SHIFT))))
646                         return md->attribute;
647         }
648         return 0;
649 }