vfs: remove unused wrapper block_page_mkwrite()
[linux/fpc-iii.git] / arch / x86 / platform / efi / efi_64.c
bloba0ac0f9c307f661c8b3ed08c4ca6d23507772e36
1 /*
2 * x86_64 specific EFI support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
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>
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.
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 #include <linux/slab.h>
32 #include <asm/setup.h>
33 #include <asm/page.h>
34 #include <asm/e820.h>
35 #include <asm/pgtable.h>
36 #include <asm/tlbflush.h>
37 #include <asm/proto.h>
38 #include <asm/efi.h>
39 #include <asm/cacheflush.h>
40 #include <asm/fixmap.h>
41 #include <asm/realmode.h>
42 #include <asm/time.h>
45 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
46 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
48 static u64 efi_va = EFI_VA_START;
51 * Scratch space used for switching the pagetable in the EFI stub
53 struct efi_scratch {
54 u64 r15;
55 u64 prev_cr3;
56 pgd_t *efi_pgt;
57 bool use_pgd;
58 u64 phys_stack;
59 } __packed;
61 static void __init early_code_mapping_set_exec(int executable)
63 efi_memory_desc_t *md;
64 void *p;
66 if (!(__supported_pte_mask & _PAGE_NX))
67 return;
69 /* Make EFI service code area executable */
70 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
71 md = p;
72 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
73 md->type == EFI_BOOT_SERVICES_CODE)
74 efi_set_executable(md, executable);
78 pgd_t * __init efi_call_phys_prolog(void)
80 unsigned long vaddress;
81 pgd_t *save_pgd;
83 int pgd;
84 int n_pgds;
86 if (!efi_enabled(EFI_OLD_MEMMAP))
87 return NULL;
89 early_code_mapping_set_exec(1);
91 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
92 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
94 for (pgd = 0; pgd < n_pgds; pgd++) {
95 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
96 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
97 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
99 __flush_tlb_all();
101 return save_pgd;
104 void __init efi_call_phys_epilog(pgd_t *save_pgd)
107 * After the lock is released, the original page table is restored.
109 int pgd_idx;
110 int nr_pgds;
112 if (!save_pgd)
113 return;
115 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
117 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
118 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
120 kfree(save_pgd);
122 __flush_tlb_all();
123 early_code_mapping_set_exec(0);
127 * Add low kernel mappings for passing arguments to EFI functions.
129 void efi_sync_low_kernel_mappings(void)
131 unsigned num_pgds;
132 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
134 if (efi_enabled(EFI_OLD_MEMMAP))
135 return;
137 num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
139 memcpy(pgd + pgd_index(PAGE_OFFSET),
140 init_mm.pgd + pgd_index(PAGE_OFFSET),
141 sizeof(pgd_t) * num_pgds);
144 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
146 unsigned long text;
147 struct page *page;
148 unsigned npages;
149 pgd_t *pgd;
151 if (efi_enabled(EFI_OLD_MEMMAP))
152 return 0;
154 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
155 pgd = __va(efi_scratch.efi_pgt);
158 * It can happen that the physical address of new_memmap lands in memory
159 * which is not mapped in the EFI page table. Therefore we need to go
160 * and ident-map those pages containing the map before calling
161 * phys_efi_set_virtual_address_map().
163 if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
164 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
165 return 1;
168 efi_scratch.use_pgd = true;
171 * When making calls to the firmware everything needs to be 1:1
172 * mapped and addressable with 32-bit pointers. Map the kernel
173 * text and allocate a new stack because we can't rely on the
174 * stack pointer being < 4GB.
176 if (!IS_ENABLED(CONFIG_EFI_MIXED))
177 return 0;
179 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
180 if (!page)
181 panic("Unable to allocate EFI runtime stack < 4GB\n");
183 efi_scratch.phys_stack = virt_to_phys(page_address(page));
184 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
186 npages = (_end - _text) >> PAGE_SHIFT;
187 text = __pa(_text);
189 if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
190 pr_err("Failed to map kernel text 1:1\n");
191 return 1;
194 return 0;
197 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
199 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
201 kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
204 static void __init __map_region(efi_memory_desc_t *md, u64 va)
206 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
207 unsigned long pf = 0;
209 if (!(md->attribute & EFI_MEMORY_WB))
210 pf |= _PAGE_PCD;
212 if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
213 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
214 md->phys_addr, va);
217 void __init efi_map_region(efi_memory_desc_t *md)
219 unsigned long size = md->num_pages << PAGE_SHIFT;
220 u64 pa = md->phys_addr;
222 if (efi_enabled(EFI_OLD_MEMMAP))
223 return old_map_region(md);
226 * Make sure the 1:1 mappings are present as a catch-all for b0rked
227 * firmware which doesn't update all internal pointers after switching
228 * to virtual mode and would otherwise crap on us.
230 __map_region(md, md->phys_addr);
233 * Enforce the 1:1 mapping as the default virtual address when
234 * booting in EFI mixed mode, because even though we may be
235 * running a 64-bit kernel, the firmware may only be 32-bit.
237 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
238 md->virt_addr = md->phys_addr;
239 return;
242 efi_va -= size;
244 /* Is PA 2M-aligned? */
245 if (!(pa & (PMD_SIZE - 1))) {
246 efi_va &= PMD_MASK;
247 } else {
248 u64 pa_offset = pa & (PMD_SIZE - 1);
249 u64 prev_va = efi_va;
251 /* get us the same offset within this 2M page */
252 efi_va = (efi_va & PMD_MASK) + pa_offset;
254 if (efi_va > prev_va)
255 efi_va -= PMD_SIZE;
258 if (efi_va < EFI_VA_END) {
259 pr_warn(FW_WARN "VA address range overflow!\n");
260 return;
263 /* Do the VA map */
264 __map_region(md, efi_va);
265 md->virt_addr = efi_va;
269 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
270 * md->virt_addr is the original virtual address which had been mapped in kexec
271 * 1st kernel.
273 void __init efi_map_region_fixed(efi_memory_desc_t *md)
275 __map_region(md, md->virt_addr);
278 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
279 u32 type, u64 attribute)
281 unsigned long last_map_pfn;
283 if (type == EFI_MEMORY_MAPPED_IO)
284 return ioremap(phys_addr, size);
286 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
287 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
288 unsigned long top = last_map_pfn << PAGE_SHIFT;
289 efi_ioremap(top, size - (top - phys_addr), type, attribute);
292 if (!(attribute & EFI_MEMORY_WB))
293 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
295 return (void __iomem *)__va(phys_addr);
298 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
300 efi_setup = phys_addr + sizeof(struct setup_data);
303 void __init efi_runtime_mkexec(void)
305 if (!efi_enabled(EFI_OLD_MEMMAP))
306 return;
308 if (__supported_pte_mask & _PAGE_NX)
309 runtime_code_page_mkexec();
312 void __init efi_dump_pagetable(void)
314 #ifdef CONFIG_EFI_PGT_DUMP
315 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
317 ptdump_walk_pgd_level(NULL, pgd);
318 #endif
321 #ifdef CONFIG_EFI_MIXED
322 extern efi_status_t efi64_thunk(u32, ...);
324 #define runtime_service32(func) \
325 ({ \
326 u32 table = (u32)(unsigned long)efi.systab; \
327 u32 *rt, *___f; \
329 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
330 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
331 *___f; \
335 * Switch to the EFI page tables early so that we can access the 1:1
336 * runtime services mappings which are not mapped in any other page
337 * tables. This function must be called before runtime_service32().
339 * Also, disable interrupts because the IDT points to 64-bit handlers,
340 * which aren't going to function correctly when we switch to 32-bit.
342 #define efi_thunk(f, ...) \
343 ({ \
344 efi_status_t __s; \
345 unsigned long flags; \
346 u32 func; \
348 efi_sync_low_kernel_mappings(); \
349 local_irq_save(flags); \
351 efi_scratch.prev_cr3 = read_cr3(); \
352 write_cr3((unsigned long)efi_scratch.efi_pgt); \
353 __flush_tlb_all(); \
355 func = runtime_service32(f); \
356 __s = efi64_thunk(func, __VA_ARGS__); \
358 write_cr3(efi_scratch.prev_cr3); \
359 __flush_tlb_all(); \
360 local_irq_restore(flags); \
362 __s; \
365 efi_status_t efi_thunk_set_virtual_address_map(
366 void *phys_set_virtual_address_map,
367 unsigned long memory_map_size,
368 unsigned long descriptor_size,
369 u32 descriptor_version,
370 efi_memory_desc_t *virtual_map)
372 efi_status_t status;
373 unsigned long flags;
374 u32 func;
376 efi_sync_low_kernel_mappings();
377 local_irq_save(flags);
379 efi_scratch.prev_cr3 = read_cr3();
380 write_cr3((unsigned long)efi_scratch.efi_pgt);
381 __flush_tlb_all();
383 func = (u32)(unsigned long)phys_set_virtual_address_map;
384 status = efi64_thunk(func, memory_map_size, descriptor_size,
385 descriptor_version, virtual_map);
387 write_cr3(efi_scratch.prev_cr3);
388 __flush_tlb_all();
389 local_irq_restore(flags);
391 return status;
394 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
396 efi_status_t status;
397 u32 phys_tm, phys_tc;
399 spin_lock(&rtc_lock);
401 phys_tm = virt_to_phys(tm);
402 phys_tc = virt_to_phys(tc);
404 status = efi_thunk(get_time, phys_tm, phys_tc);
406 spin_unlock(&rtc_lock);
408 return status;
411 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
413 efi_status_t status;
414 u32 phys_tm;
416 spin_lock(&rtc_lock);
418 phys_tm = virt_to_phys(tm);
420 status = efi_thunk(set_time, phys_tm);
422 spin_unlock(&rtc_lock);
424 return status;
427 static efi_status_t
428 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
429 efi_time_t *tm)
431 efi_status_t status;
432 u32 phys_enabled, phys_pending, phys_tm;
434 spin_lock(&rtc_lock);
436 phys_enabled = virt_to_phys(enabled);
437 phys_pending = virt_to_phys(pending);
438 phys_tm = virt_to_phys(tm);
440 status = efi_thunk(get_wakeup_time, phys_enabled,
441 phys_pending, phys_tm);
443 spin_unlock(&rtc_lock);
445 return status;
448 static efi_status_t
449 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
451 efi_status_t status;
452 u32 phys_tm;
454 spin_lock(&rtc_lock);
456 phys_tm = virt_to_phys(tm);
458 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
460 spin_unlock(&rtc_lock);
462 return status;
466 static efi_status_t
467 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
468 u32 *attr, unsigned long *data_size, void *data)
470 efi_status_t status;
471 u32 phys_name, phys_vendor, phys_attr;
472 u32 phys_data_size, phys_data;
474 phys_data_size = virt_to_phys(data_size);
475 phys_vendor = virt_to_phys(vendor);
476 phys_name = virt_to_phys(name);
477 phys_attr = virt_to_phys(attr);
478 phys_data = virt_to_phys(data);
480 status = efi_thunk(get_variable, phys_name, phys_vendor,
481 phys_attr, phys_data_size, phys_data);
483 return status;
486 static efi_status_t
487 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
488 u32 attr, unsigned long data_size, void *data)
490 u32 phys_name, phys_vendor, phys_data;
491 efi_status_t status;
493 phys_name = virt_to_phys(name);
494 phys_vendor = virt_to_phys(vendor);
495 phys_data = virt_to_phys(data);
497 /* If data_size is > sizeof(u32) we've got problems */
498 status = efi_thunk(set_variable, phys_name, phys_vendor,
499 attr, data_size, phys_data);
501 return status;
504 static efi_status_t
505 efi_thunk_get_next_variable(unsigned long *name_size,
506 efi_char16_t *name,
507 efi_guid_t *vendor)
509 efi_status_t status;
510 u32 phys_name_size, phys_name, phys_vendor;
512 phys_name_size = virt_to_phys(name_size);
513 phys_vendor = virt_to_phys(vendor);
514 phys_name = virt_to_phys(name);
516 status = efi_thunk(get_next_variable, phys_name_size,
517 phys_name, phys_vendor);
519 return status;
522 static efi_status_t
523 efi_thunk_get_next_high_mono_count(u32 *count)
525 efi_status_t status;
526 u32 phys_count;
528 phys_count = virt_to_phys(count);
529 status = efi_thunk(get_next_high_mono_count, phys_count);
531 return status;
534 static void
535 efi_thunk_reset_system(int reset_type, efi_status_t status,
536 unsigned long data_size, efi_char16_t *data)
538 u32 phys_data;
540 phys_data = virt_to_phys(data);
542 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
545 static efi_status_t
546 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
547 unsigned long count, unsigned long sg_list)
550 * To properly support this function we would need to repackage
551 * 'capsules' because the firmware doesn't understand 64-bit
552 * pointers.
554 return EFI_UNSUPPORTED;
557 static efi_status_t
558 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
559 u64 *remaining_space,
560 u64 *max_variable_size)
562 efi_status_t status;
563 u32 phys_storage, phys_remaining, phys_max;
565 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
566 return EFI_UNSUPPORTED;
568 phys_storage = virt_to_phys(storage_space);
569 phys_remaining = virt_to_phys(remaining_space);
570 phys_max = virt_to_phys(max_variable_size);
572 status = efi_thunk(query_variable_info, attr, phys_storage,
573 phys_remaining, phys_max);
575 return status;
578 static efi_status_t
579 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
580 unsigned long count, u64 *max_size,
581 int *reset_type)
584 * To properly support this function we would need to repackage
585 * 'capsules' because the firmware doesn't understand 64-bit
586 * pointers.
588 return EFI_UNSUPPORTED;
591 void efi_thunk_runtime_setup(void)
593 efi.get_time = efi_thunk_get_time;
594 efi.set_time = efi_thunk_set_time;
595 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
596 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
597 efi.get_variable = efi_thunk_get_variable;
598 efi.get_next_variable = efi_thunk_get_next_variable;
599 efi.set_variable = efi_thunk_set_variable;
600 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
601 efi.reset_system = efi_thunk_reset_system;
602 efi.query_variable_info = efi_thunk_query_variable_info;
603 efi.update_capsule = efi_thunk_update_capsule;
604 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
606 #endif /* CONFIG_EFI_MIXED */