blob: 2b2003860615fbc5e24ef12e1c1fa3d855a1f09d [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
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060041static pgd_t *save_pgd __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010042static unsigned long efi_flags __initdata;
43
Matthew Garrett9cd2b072011-05-05 15:19:43 -040044static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010045{
46 efi_memory_desc_t *md;
47 void *p;
48
Huang, Yinga2172e22008-01-30 13:33:55 +010049 if (!(__supported_pte_mask & _PAGE_NX))
50 return;
51
Matthew Garrett916f6762011-05-25 09:53:13 -040052 /* Make EFI service code area executable */
Huang, Ying5b836832008-01-30 13:31:19 +010053 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
54 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -040055 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
56 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040057 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010058 }
59}
60
61void __init efi_call_phys_prelog(void)
62{
63 unsigned long vaddress;
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060064 int pgd;
65 int n_pgds;
Huang, Ying5b836832008-01-30 13:31:19 +010066
Matthew Garrett9cd2b072011-05-05 15:19:43 -040067 early_code_mapping_set_exec(1);
Huang, Ying4de0d4a2008-02-13 17:22:41 +080068 local_irq_save(efi_flags);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060069
70 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
71 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
72
73 for (pgd = 0; pgd < n_pgds; pgd++) {
74 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
75 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
76 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
77 }
Huang, Ying5b836832008-01-30 13:31:19 +010078 __flush_tlb_all();
79}
80
81void __init efi_call_phys_epilog(void)
82{
83 /*
84 * After the lock is released, the original page table is restored.
85 */
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060086 int pgd;
87 int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
88 for (pgd = 0; pgd < n_pgds; pgd++)
89 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
90 kfree(save_pgd);
Huang, Ying5b836832008-01-30 13:31:19 +010091 __flush_tlb_all();
92 local_irq_restore(efi_flags);
Matthew Garrett9cd2b072011-05-05 15:19:43 -040093 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +010094}
Keith Packarde1ad7832011-12-11 16:12:42 -080095
96void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +010097 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -080098{
99 unsigned long last_map_pfn;
100
101 if (type == EFI_MEMORY_MAPPED_IO)
102 return ioremap(phys_addr, size);
103
104 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
105 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
106 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100107 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800108 }
109
Matt Fleming3e8fa262012-10-19 13:25:46 +0100110 if (!(attribute & EFI_MEMORY_WB))
111 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
112
Keith Packarde1ad7832011-12-11 16:12:42 -0800113 return (void __iomem *)__va(phys_addr);
114}