blob: ac0621a7ac3d23383ed6f284451c5bb7d960f977 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
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>
Huang, Ying5b836832008-01-30 13:31:19 +010036#include <asm/proto.h>
37#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080038#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090039#include <asm/fixmap.h>
Huang, Ying5b836832008-01-30 13:31:19 +010040
41static pgd_t save_pgd __initdata;
42static unsigned long efi_flags __initdata;
43
Huang, Ying5b836832008-01-30 13:31:19 +010044static void __init early_mapping_set_exec(unsigned long start,
45 unsigned long end,
46 int executable)
47{
Huang, Ying4de0d4a2008-02-13 17:22:41 +080048 unsigned long num_pages;
Huang, Ying5b836832008-01-30 13:31:19 +010049
Huang, Ying4de0d4a2008-02-13 17:22:41 +080050 start &= PMD_MASK;
51 end = (end + PMD_SIZE - 1) & PMD_MASK;
52 num_pages = (end - start) >> PAGE_SHIFT;
53 if (executable)
54 set_memory_x((unsigned long)__va(start), num_pages);
55 else
56 set_memory_nx((unsigned long)__va(start), num_pages);
Huang, Ying5b836832008-01-30 13:31:19 +010057}
58
59static void __init early_runtime_code_mapping_set_exec(int executable)
60{
61 efi_memory_desc_t *md;
62 void *p;
63
Huang, Yinga2172e22008-01-30 13:33:55 +010064 if (!(__supported_pte_mask & _PAGE_NX))
65 return;
66
Huang, Ying5b836832008-01-30 13:31:19 +010067 /* Make EFI runtime service code area executable */
68 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
69 md = p;
70 if (md->type == EFI_RUNTIME_SERVICES_CODE) {
71 unsigned long end;
Huang, Ying4de0d4a2008-02-13 17:22:41 +080072 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
Huang, Ying5b836832008-01-30 13:31:19 +010073 early_mapping_set_exec(md->phys_addr, end, executable);
74 }
75 }
76}
77
78void __init efi_call_phys_prelog(void)
79{
80 unsigned long vaddress;
81
Huang, Ying5b836832008-01-30 13:31:19 +010082 early_runtime_code_mapping_set_exec(1);
Huang, Ying4de0d4a2008-02-13 17:22:41 +080083 local_irq_save(efi_flags);
Huang, Ying5b836832008-01-30 13:31:19 +010084 vaddress = (unsigned long)__va(0x0UL);
Jeremy Fitzhardingeb899c5e2008-01-30 13:32:44 +010085 save_pgd = *pgd_offset_k(0x0UL);
Huang, Ying5b836832008-01-30 13:31:19 +010086 set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress));
87 __flush_tlb_all();
88}
89
90void __init efi_call_phys_epilog(void)
91{
92 /*
93 * After the lock is released, the original page table is restored.
94 */
95 set_pgd(pgd_offset_k(0x0UL), save_pgd);
Huang, Ying5b836832008-01-30 13:31:19 +010096 __flush_tlb_all();
97 local_irq_restore(efi_flags);
Huang, Ying4de0d4a2008-02-13 17:22:41 +080098 early_runtime_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +010099}
100
Paul Mackerras6a7bbd52009-08-03 22:38:10 +1000101void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
102 u32 type)
Huang, Ying5b836832008-01-30 13:31:19 +0100103{
Huang Yingdd39ecf2009-03-04 10:58:33 +0800104 unsigned long last_map_pfn;
Huang, Ying5b836832008-01-30 13:31:19 +0100105
Paul Mackerras6a7bbd52009-08-03 22:38:10 +1000106 if (type == EFI_MEMORY_MAPPED_IO)
107 return ioremap(phys_addr, size);
108
Huang Yingdd39ecf2009-03-04 10:58:33 +0800109 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
110 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size)
Huang, Ying5b836832008-01-30 13:31:19 +0100111 return NULL;
112
Huang Yingdd39ecf2009-03-04 10:58:33 +0800113 return (void __iomem *)__va(phys_addr);
Huang, Ying5b836832008-01-30 13:31:19 +0100114}