Merge branch 'upstream'
[linux-2.6] / arch / i386 / kernel / efi.c
1 /*
2  * Extensible Firmware Interface
3  *
4  * Based on Extensible Firmware Interface Specification version 1.0
5  *
6  * Copyright (C) 1999 VA Linux Systems
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  * Copyright (C) 1999-2002 Hewlett-Packard Co.
9  *      David Mosberger-Tang <davidm@hpl.hp.com>
10  *      Stephane Eranian <eranian@hpl.hp.com>
11  *
12  * All EFI Runtime Services are not implemented yet as EFI only
13  * supports physical mode addressing on SoftSDV. This is to be fixed
14  * in a future version.  --drummond 1999-07-20
15  *
16  * Implemented EFI runtime services and virtual mode calls.  --davidm
17  *
18  * Goutham Rao: <goutham.rao@intel.com>
19  *      Skip non-WB memory and ignore empty memory ranges.
20  */
21
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/mm.h>
26 #include <linux/types.h>
27 #include <linux/time.h>
28 #include <linux/spinlock.h>
29 #include <linux/bootmem.h>
30 #include <linux/ioport.h>
31 #include <linux/module.h>
32 #include <linux/efi.h>
33 #include <linux/kexec.h>
34
35 #include <asm/setup.h>
36 #include <asm/io.h>
37 #include <asm/page.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40 #include <asm/desc.h>
41 #include <asm/tlbflush.h>
42
43 #define EFI_DEBUG       0
44 #define PFX             "EFI: "
45
46 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
47
48 struct efi efi;
49 EXPORT_SYMBOL(efi);
50 static struct efi efi_phys;
51 struct efi_memory_map memmap;
52
53 /*
54  * We require an early boot_ioremap mapping mechanism initially
55  */
56 extern void * boot_ioremap(unsigned long, unsigned long);
57
58 /*
59  * To make EFI call EFI runtime service in physical addressing mode we need
60  * prelog/epilog before/after the invocation to disable interrupt, to
61  * claim EFI runtime service handler exclusively and to duplicate a memory in
62  * low memory space say 0 - 3G.
63  */
64
65 static unsigned long efi_rt_eflags;
66 static DEFINE_SPINLOCK(efi_rt_lock);
67 static pgd_t efi_bak_pg_dir_pointer[2];
68
69 static void efi_call_phys_prelog(void)
70 {
71         unsigned long cr4;
72         unsigned long temp;
73         struct Xgt_desc_struct *cpu_gdt_descr;
74
75         spin_lock(&efi_rt_lock);
76         local_irq_save(efi_rt_eflags);
77
78         cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
79
80         /*
81          * If I don't have PSE, I should just duplicate two entries in page
82          * directory. If I have PSE, I just need to duplicate one entry in
83          * page directory.
84          */
85         cr4 = read_cr4();
86
87         if (cr4 & X86_CR4_PSE) {
88                 efi_bak_pg_dir_pointer[0].pgd =
89                     swapper_pg_dir[pgd_index(0)].pgd;
90                 swapper_pg_dir[0].pgd =
91                     swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
92         } else {
93                 efi_bak_pg_dir_pointer[0].pgd =
94                     swapper_pg_dir[pgd_index(0)].pgd;
95                 efi_bak_pg_dir_pointer[1].pgd =
96                     swapper_pg_dir[pgd_index(0x400000)].pgd;
97                 swapper_pg_dir[pgd_index(0)].pgd =
98                     swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
99                 temp = PAGE_OFFSET + 0x400000;
100                 swapper_pg_dir[pgd_index(0x400000)].pgd =
101                     swapper_pg_dir[pgd_index(temp)].pgd;
102         }
103
104         /*
105          * After the lock is released, the original page table is restored.
106          */
107         local_flush_tlb();
108
109         cpu_gdt_descr->address = __pa(cpu_gdt_descr->address);
110         load_gdt(cpu_gdt_descr);
111 }
112
113 static void efi_call_phys_epilog(void)
114 {
115         unsigned long cr4;
116         struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, 0);
117
118         cpu_gdt_descr->address = __va(cpu_gdt_descr->address);
119         load_gdt(cpu_gdt_descr);
120
121         cr4 = read_cr4();
122
123         if (cr4 & X86_CR4_PSE) {
124                 swapper_pg_dir[pgd_index(0)].pgd =
125                     efi_bak_pg_dir_pointer[0].pgd;
126         } else {
127                 swapper_pg_dir[pgd_index(0)].pgd =
128                     efi_bak_pg_dir_pointer[0].pgd;
129                 swapper_pg_dir[pgd_index(0x400000)].pgd =
130                     efi_bak_pg_dir_pointer[1].pgd;
131         }
132
133         /*
134          * After the lock is released, the original page table is restored.
135          */
136         local_flush_tlb();
137
138         local_irq_restore(efi_rt_eflags);
139         spin_unlock(&efi_rt_lock);
140 }
141
142 static efi_status_t
143 phys_efi_set_virtual_address_map(unsigned long memory_map_size,
144                                  unsigned long descriptor_size,
145                                  u32 descriptor_version,
146                                  efi_memory_desc_t *virtual_map)
147 {
148         efi_status_t status;
149
150         efi_call_phys_prelog();
151         status = efi_call_phys(efi_phys.set_virtual_address_map,
152                                      memory_map_size, descriptor_size,
153                                      descriptor_version, virtual_map);
154         efi_call_phys_epilog();
155         return status;
156 }
157
158 static efi_status_t
159 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
160 {
161         efi_status_t status;
162
163         efi_call_phys_prelog();
164         status = efi_call_phys(efi_phys.get_time, tm, tc);
165         efi_call_phys_epilog();
166         return status;
167 }
168
169 inline int efi_set_rtc_mmss(unsigned long nowtime)
170 {
171         int real_seconds, real_minutes;
172         efi_status_t    status;
173         efi_time_t      eft;
174         efi_time_cap_t  cap;
175
176         spin_lock(&efi_rt_lock);
177         status = efi.get_time(&eft, &cap);
178         spin_unlock(&efi_rt_lock);
179         if (status != EFI_SUCCESS)
180                 panic("Ooops, efitime: can't read time!\n");
181         real_seconds = nowtime % 60;
182         real_minutes = nowtime / 60;
183
184         if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
185                 real_minutes += 30;
186         real_minutes %= 60;
187
188         eft.minute = real_minutes;
189         eft.second = real_seconds;
190
191         if (status != EFI_SUCCESS) {
192                 printk("Ooops: efitime: can't read time!\n");
193                 return -1;
194         }
195         return 0;
196 }
197 /*
198  * This should only be used during kernel init and before runtime
199  * services have been remapped, therefore, we'll need to call in physical
200  * mode.  Note, this call isn't used later, so mark it __init.
201  */
202 inline unsigned long __init efi_get_time(void)
203 {
204         efi_status_t status;
205         efi_time_t eft;
206         efi_time_cap_t cap;
207
208         status = phys_efi_get_time(&eft, &cap);
209         if (status != EFI_SUCCESS)
210                 printk("Oops: efitime: can't read time status: 0x%lx\n",status);
211
212         return mktime(eft.year, eft.month, eft.day, eft.hour,
213                         eft.minute, eft.second);
214 }
215
216 int is_available_memory(efi_memory_desc_t * md)
217 {
218         if (!(md->attribute & EFI_MEMORY_WB))
219                 return 0;
220
221         switch (md->type) {
222                 case EFI_LOADER_CODE:
223                 case EFI_LOADER_DATA:
224                 case EFI_BOOT_SERVICES_CODE:
225                 case EFI_BOOT_SERVICES_DATA:
226                 case EFI_CONVENTIONAL_MEMORY:
227                         return 1;
228         }
229         return 0;
230 }
231
232 /*
233  * We need to map the EFI memory map again after paging_init().
234  */
235 void __init efi_map_memmap(void)
236 {
237         memmap.map = NULL;
238
239         memmap.map = bt_ioremap((unsigned long) memmap.phys_map,
240                         (memmap.nr_map * memmap.desc_size));
241         if (memmap.map == NULL)
242                 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
243
244         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
245 }
246
247 #if EFI_DEBUG
248 static void __init print_efi_memmap(void)
249 {
250         efi_memory_desc_t *md;
251         void *p;
252         int i;
253
254         for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
255                 md = p;
256                 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
257                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
258                         i, md->type, md->attribute, md->phys_addr,
259                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
260                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
261         }
262 }
263 #endif  /*  EFI_DEBUG  */
264
265 /*
266  * Walks the EFI memory map and calls CALLBACK once for each EFI
267  * memory descriptor that has memory that is available for kernel use.
268  */
269 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
270 {
271         int prev_valid = 0;
272         struct range {
273                 unsigned long start;
274                 unsigned long end;
275         } prev, curr;
276         efi_memory_desc_t *md;
277         unsigned long start, end;
278         void *p;
279
280         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
281                 md = p;
282
283                 if ((md->num_pages == 0) || (!is_available_memory(md)))
284                         continue;
285
286                 curr.start = md->phys_addr;
287                 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
288
289                 if (!prev_valid) {
290                         prev = curr;
291                         prev_valid = 1;
292                 } else {
293                         if (curr.start < prev.start)
294                                 printk(KERN_INFO PFX "Unordered memory map\n");
295                         if (prev.end == curr.start)
296                                 prev.end = curr.end;
297                         else {
298                                 start =
299                                     (unsigned long) (PAGE_ALIGN(prev.start));
300                                 end = (unsigned long) (prev.end & PAGE_MASK);
301                                 if ((end > start)
302                                     && (*callback) (start, end, arg) < 0)
303                                         return;
304                                 prev = curr;
305                         }
306                 }
307         }
308         if (prev_valid) {
309                 start = (unsigned long) PAGE_ALIGN(prev.start);
310                 end = (unsigned long) (prev.end & PAGE_MASK);
311                 if (end > start)
312                         (*callback) (start, end, arg);
313         }
314 }
315
316 void __init efi_init(void)
317 {
318         efi_config_table_t *config_tables;
319         efi_runtime_services_t *runtime;
320         efi_char16_t *c16;
321         char vendor[100] = "unknown";
322         unsigned long num_config_tables;
323         int i = 0;
324
325         memset(&efi, 0, sizeof(efi) );
326         memset(&efi_phys, 0, sizeof(efi_phys));
327
328         efi_phys.systab = EFI_SYSTAB;
329         memmap.phys_map = EFI_MEMMAP;
330         memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
331         memmap.desc_version = EFI_MEMDESC_VERSION;
332         memmap.desc_size = EFI_MEMDESC_SIZE;
333
334         efi.systab = (efi_system_table_t *)
335                 boot_ioremap((unsigned long) efi_phys.systab,
336                         sizeof(efi_system_table_t));
337         /*
338          * Verify the EFI Table
339          */
340         if (efi.systab == NULL)
341                 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
342         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
343                 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
344         if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
345                 printk(KERN_ERR PFX
346                        "Warning: EFI system table major version mismatch: "
347                        "got %d.%02d, expected %d.%02d\n",
348                        efi.systab->hdr.revision >> 16,
349                        efi.systab->hdr.revision & 0xffff,
350                        EFI_SYSTEM_TABLE_REVISION >> 16,
351                        EFI_SYSTEM_TABLE_REVISION & 0xffff);
352         /*
353          * Grab some details from the system table
354          */
355         num_config_tables = efi.systab->nr_tables;
356         config_tables = (efi_config_table_t *)efi.systab->tables;
357         runtime = efi.systab->runtime;
358
359         /*
360          * Show what we know for posterity
361          */
362         c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
363         if (c16) {
364                 for (i = 0; i < sizeof(vendor) && *c16; ++i)
365                         vendor[i] = *c16++;
366                 vendor[i] = '\0';
367         } else
368                 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
369
370         printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
371                efi.systab->hdr.revision >> 16,
372                efi.systab->hdr.revision & 0xffff, vendor);
373
374         /*
375          * Let's see what config tables the firmware passed to us.
376          */
377         config_tables = (efi_config_table_t *)
378                                 boot_ioremap((unsigned long) config_tables,
379                                 num_config_tables * sizeof(efi_config_table_t));
380
381         if (config_tables == NULL)
382                 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
383
384         for (i = 0; i < num_config_tables; i++) {
385                 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
386                         efi.mps = (void *)config_tables[i].table;
387                         printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
388                 } else
389                     if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
390                         efi.acpi20 = __va(config_tables[i].table);
391                         printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
392                 } else
393                     if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
394                         efi.acpi = __va(config_tables[i].table);
395                         printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
396                 } else
397                     if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
398                         efi.smbios = (void *) config_tables[i].table;
399                         printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
400                 } else
401                     if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
402                         efi.hcdp = (void *)config_tables[i].table;
403                         printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
404                 } else
405                     if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
406                         efi.uga = (void *)config_tables[i].table;
407                         printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
408                 }
409         }
410         printk("\n");
411
412         /*
413          * Check out the runtime services table. We need to map
414          * the runtime services table so that we can grab the physical
415          * address of several of the EFI runtime functions, needed to
416          * set the firmware into virtual mode.
417          */
418
419         runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
420                                                 runtime,
421                                                 sizeof(efi_runtime_services_t));
422         if (runtime != NULL) {
423                 /*
424                  * We will only need *early* access to the following
425                  * two EFI runtime services before set_virtual_address_map
426                  * is invoked.
427                  */
428                 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
429                 efi_phys.set_virtual_address_map =
430                         (efi_set_virtual_address_map_t *)
431                                 runtime->set_virtual_address_map;
432         } else
433                 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
434
435         /* Map the EFI memory map for use until paging_init() */
436         memmap.map = boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
437         if (memmap.map == NULL)
438                 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
439
440         memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
441
442 #if EFI_DEBUG
443         print_efi_memmap();
444 #endif
445 }
446
447 static inline void __init check_range_for_systab(efi_memory_desc_t *md)
448 {
449         if (((unsigned long)md->phys_addr <= (unsigned long)efi_phys.systab) &&
450                 ((unsigned long)efi_phys.systab < md->phys_addr +
451                 ((unsigned long)md->num_pages << EFI_PAGE_SHIFT))) {
452                 unsigned long addr;
453
454                 addr = md->virt_addr - md->phys_addr +
455                         (unsigned long)efi_phys.systab;
456                 efi.systab = (efi_system_table_t *)addr;
457         }
458 }
459
460 /*
461  * This function will switch the EFI runtime services to virtual mode.
462  * Essentially, look through the EFI memmap and map every region that
463  * has the runtime attribute bit set in its memory descriptor and update
464  * that memory descriptor with the virtual address obtained from ioremap().
465  * This enables the runtime services to be called without having to
466  * thunk back into physical mode for every invocation.
467  */
468
469 void __init efi_enter_virtual_mode(void)
470 {
471         efi_memory_desc_t *md;
472         efi_status_t status;
473         void *p;
474
475         efi.systab = NULL;
476
477         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
478                 md = p;
479
480                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
481                         continue;
482
483                 md->virt_addr = (unsigned long)ioremap(md->phys_addr,
484                         md->num_pages << EFI_PAGE_SHIFT);
485                 if (!(unsigned long)md->virt_addr) {
486                         printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
487                                 (unsigned long)md->phys_addr);
488                 }
489                 /* update the virtual address of the EFI system table */
490                 check_range_for_systab(md);
491         }
492
493         if (!efi.systab)
494                 BUG();
495
496         status = phys_efi_set_virtual_address_map(
497                         memmap.desc_size * memmap.nr_map,
498                         memmap.desc_size,
499                         memmap.desc_version,
500                         memmap.phys_map);
501
502         if (status != EFI_SUCCESS) {
503                 printk (KERN_ALERT "You are screwed! "
504                         "Unable to switch EFI into virtual mode "
505                         "(status=%lx)\n", status);
506                 panic("EFI call to SetVirtualAddressMap() failed!");
507         }
508
509         /*
510          * Now that EFI is in virtual mode, update the function
511          * pointers in the runtime service table to the new virtual addresses.
512          */
513
514         efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
515         efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
516         efi.get_wakeup_time = (efi_get_wakeup_time_t *)
517                                         efi.systab->runtime->get_wakeup_time;
518         efi.set_wakeup_time = (efi_set_wakeup_time_t *)
519                                         efi.systab->runtime->set_wakeup_time;
520         efi.get_variable = (efi_get_variable_t *)
521                                         efi.systab->runtime->get_variable;
522         efi.get_next_variable = (efi_get_next_variable_t *)
523                                         efi.systab->runtime->get_next_variable;
524         efi.set_variable = (efi_set_variable_t *)
525                                         efi.systab->runtime->set_variable;
526         efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
527                                         efi.systab->runtime->get_next_high_mono_count;
528         efi.reset_system = (efi_reset_system_t *)
529                                         efi.systab->runtime->reset_system;
530 }
531
532 void __init
533 efi_initialize_iomem_resources(struct resource *code_resource,
534                                struct resource *data_resource)
535 {
536         struct resource *res;
537         efi_memory_desc_t *md;
538         void *p;
539
540         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
541                 md = p;
542
543                 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
544                     0x100000000ULL)
545                         continue;
546                 res = alloc_bootmem_low(sizeof(struct resource));
547                 switch (md->type) {
548                 case EFI_RESERVED_TYPE:
549                         res->name = "Reserved Memory";
550                         break;
551                 case EFI_LOADER_CODE:
552                         res->name = "Loader Code";
553                         break;
554                 case EFI_LOADER_DATA:
555                         res->name = "Loader Data";
556                         break;
557                 case EFI_BOOT_SERVICES_DATA:
558                         res->name = "BootServices Data";
559                         break;
560                 case EFI_BOOT_SERVICES_CODE:
561                         res->name = "BootServices Code";
562                         break;
563                 case EFI_RUNTIME_SERVICES_CODE:
564                         res->name = "Runtime Service Code";
565                         break;
566                 case EFI_RUNTIME_SERVICES_DATA:
567                         res->name = "Runtime Service Data";
568                         break;
569                 case EFI_CONVENTIONAL_MEMORY:
570                         res->name = "Conventional Memory";
571                         break;
572                 case EFI_UNUSABLE_MEMORY:
573                         res->name = "Unusable Memory";
574                         break;
575                 case EFI_ACPI_RECLAIM_MEMORY:
576                         res->name = "ACPI Reclaim";
577                         break;
578                 case EFI_ACPI_MEMORY_NVS:
579                         res->name = "ACPI NVS";
580                         break;
581                 case EFI_MEMORY_MAPPED_IO:
582                         res->name = "Memory Mapped IO";
583                         break;
584                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
585                         res->name = "Memory Mapped IO Port Space";
586                         break;
587                 default:
588                         res->name = "Reserved";
589                         break;
590                 }
591                 res->start = md->phys_addr;
592                 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
593                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
594                 if (request_resource(&iomem_resource, res) < 0)
595                         printk(KERN_ERR PFX "Failed to allocate res %s : 0x%lx-0x%lx\n",
596                                 res->name, res->start, res->end);
597                 /*
598                  * We don't know which region contains kernel data so we try
599                  * it repeatedly and let the resource manager test it.
600                  */
601                 if (md->type == EFI_CONVENTIONAL_MEMORY) {
602                         request_resource(res, code_resource);
603                         request_resource(res, data_resource);
604 #ifdef CONFIG_KEXEC
605                         request_resource(res, &crashk_res);
606 #endif
607                 }
608         }
609 }
610
611 /*
612  * Convenience functions to obtain memory types and attributes
613  */
614
615 u32 efi_mem_type(unsigned long phys_addr)
616 {
617         efi_memory_desc_t *md;
618         void *p;
619
620         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
621                 md = p;
622                 if ((md->phys_addr <= phys_addr) && (phys_addr <
623                         (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
624                         return md->type;
625         }
626         return 0;
627 }
628
629 u64 efi_mem_attributes(unsigned long phys_addr)
630 {
631         efi_memory_desc_t *md;
632         void *p;
633
634         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
635                 md = p;
636                 if ((md->phys_addr <= phys_addr) && (phys_addr <
637                         (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
638                         return md->attribute;
639         }
640         return 0;
641 }