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