Merge branches 'release', 'cpuidle-2.6.25' and 'idle' into release
[linux-2.6] / arch / x86 / kernel / efi_64.c
1 /*
2  * x86_64 specific EFI support functions
3  * Based on Extensible Firmware Interface Specification version 1.0
4  *
5  * Copyright (C) 2005-2008 Intel Co.
6  *      Fenghua Yu <fenghua.yu@intel.com>
7  *      Bibo Mao <bibo.mao@intel.com>
8  *      Chandramouli Narayanan <mouli@linux.intel.com>
9  *      Huang Ying <ying.huang@intel.com>
10  *
11  * Code to convert EFI to E820 map has been implemented in elilo bootloader
12  * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
13  * is setup appropriately for EFI runtime code.
14  * - mouli 06/14/2007.
15  *
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/mm.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/bootmem.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/efi.h>
27 #include <linux/uaccess.h>
28 #include <linux/io.h>
29 #include <linux/reboot.h>
30
31 #include <asm/setup.h>
32 #include <asm/page.h>
33 #include <asm/e820.h>
34 #include <asm/pgtable.h>
35 #include <asm/tlbflush.h>
36 #include <asm/proto.h>
37 #include <asm/efi.h>
38
39 static pgd_t save_pgd __initdata;
40 static unsigned long efi_flags __initdata;
41
42 static void __init early_mapping_set_exec(unsigned long start,
43                                           unsigned long end,
44                                           int executable)
45 {
46         pte_t *kpte;
47         unsigned int level;
48
49         while (start < end) {
50                 kpte = lookup_address((unsigned long)__va(start), &level);
51                 BUG_ON(!kpte);
52                 if (executable)
53                         set_pte(kpte, pte_mkexec(*kpte));
54                 else
55                         set_pte(kpte, __pte((pte_val(*kpte) | _PAGE_NX) & \
56                                             __supported_pte_mask));
57                 if (level == PG_LEVEL_4K)
58                         start = (start + PAGE_SIZE) & PAGE_MASK;
59                 else
60                         start = (start + PMD_SIZE) & PMD_MASK;
61         }
62 }
63
64 static void __init early_runtime_code_mapping_set_exec(int executable)
65 {
66         efi_memory_desc_t *md;
67         void *p;
68
69         if (!(__supported_pte_mask & _PAGE_NX))
70                 return;
71
72         /* Make EFI runtime service code area executable */
73         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
74                 md = p;
75                 if (md->type == EFI_RUNTIME_SERVICES_CODE) {
76                         unsigned long end;
77                         end = md->phys_addr + (md->num_pages << PAGE_SHIFT);
78                         early_mapping_set_exec(md->phys_addr, end, executable);
79                 }
80         }
81 }
82
83 void __init efi_call_phys_prelog(void)
84 {
85         unsigned long vaddress;
86
87         local_irq_save(efi_flags);
88         early_runtime_code_mapping_set_exec(1);
89         vaddress = (unsigned long)__va(0x0UL);
90         save_pgd = *pgd_offset_k(0x0UL);
91         set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress));
92         __flush_tlb_all();
93 }
94
95 void __init efi_call_phys_epilog(void)
96 {
97         /*
98          * After the lock is released, the original page table is restored.
99          */
100         set_pgd(pgd_offset_k(0x0UL), save_pgd);
101         early_runtime_code_mapping_set_exec(0);
102         __flush_tlb_all();
103         local_irq_restore(efi_flags);
104 }
105
106 void __init efi_reserve_bootmem(void)
107 {
108         reserve_bootmem_generic((unsigned long)memmap.phys_map,
109                                 memmap.nr_map * memmap.desc_size);
110 }
111
112 void __iomem * __init efi_ioremap(unsigned long phys_addr, unsigned long size)
113 {
114         static unsigned pages_mapped;
115         unsigned i, pages;
116
117         /* phys_addr and size must be page aligned */
118         if ((phys_addr & ~PAGE_MASK) || (size & ~PAGE_MASK))
119                 return NULL;
120
121         pages = size >> PAGE_SHIFT;
122         if (pages_mapped + pages > MAX_EFI_IO_PAGES)
123                 return NULL;
124
125         for (i = 0; i < pages; i++) {
126                 __set_fixmap(FIX_EFI_IO_MAP_FIRST_PAGE - pages_mapped,
127                              phys_addr, PAGE_KERNEL);
128                 phys_addr += PAGE_SIZE;
129                 pages_mapped++;
130         }
131
132         return (void __iomem *)__fix_to_virt(FIX_EFI_IO_MAP_FIRST_PAGE - \
133                                              (pages_mapped - pages));
134 }