blob: 12112ab4fd40f59404f8b2e71eadd0b238ae9fc7 [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>
David Howells0d01ff22013-04-11 23:51:01 +010030#include <linux/slab.h>
Huang, Ying5b836832008-01-30 13:31:19 +010031
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>
Huang, Ying5b836832008-01-30 13:31:19 +010037#include <asm/proto.h>
38#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080039#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090040#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010041#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000042#include <asm/time.h>
Huang, Ying5b836832008-01-30 13:31:19 +010043
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060044static pgd_t *save_pgd __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010045static unsigned long efi_flags __initdata;
46
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010047/*
48 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
49 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
50 */
51static u64 efi_va = -4 * (1UL << 30);
52#define EFI_VA_END (-68 * (1UL << 30))
53
54/*
55 * Scratch space used for switching the pagetable in the EFI stub
56 */
57struct efi_scratch {
58 u64 r15;
59 u64 prev_cr3;
60 pgd_t *efi_pgt;
61 bool use_pgd;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000062 u64 phys_stack;
63} __packed;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010064
Matthew Garrett9cd2b072011-05-05 15:19:43 -040065static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010066{
67 efi_memory_desc_t *md;
68 void *p;
69
Huang, Yinga2172e22008-01-30 13:33:55 +010070 if (!(__supported_pte_mask & _PAGE_NX))
71 return;
72
Matthew Garrett916f6762011-05-25 09:53:13 -040073 /* Make EFI service code area executable */
Huang, Ying5b836832008-01-30 13:31:19 +010074 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
75 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -040076 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
77 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040078 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010079 }
80}
81
82void __init efi_call_phys_prelog(void)
83{
84 unsigned long vaddress;
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060085 int pgd;
86 int n_pgds;
Huang, Ying5b836832008-01-30 13:31:19 +010087
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010088 if (!efi_enabled(EFI_OLD_MEMMAP))
89 return;
90
Matthew Garrett9cd2b072011-05-05 15:19:43 -040091 early_code_mapping_set_exec(1);
Huang, Ying4de0d4a2008-02-13 17:22:41 +080092 local_irq_save(efi_flags);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060093
94 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
95 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
96
97 for (pgd = 0; pgd < n_pgds; pgd++) {
98 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
99 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
100 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
101 }
Huang, Ying5b836832008-01-30 13:31:19 +0100102 __flush_tlb_all();
103}
104
105void __init efi_call_phys_epilog(void)
106{
107 /*
108 * After the lock is released, the original page table is restored.
109 */
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600110 int pgd;
111 int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100112
113 if (!efi_enabled(EFI_OLD_MEMMAP))
114 return;
115
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600116 for (pgd = 0; pgd < n_pgds; pgd++)
117 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
118 kfree(save_pgd);
Huang, Ying5b836832008-01-30 13:31:19 +0100119 __flush_tlb_all();
120 local_irq_restore(efi_flags);
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400121 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100122}
Keith Packarde1ad7832011-12-11 16:12:42 -0800123
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100124/*
125 * Add low kernel mappings for passing arguments to EFI functions.
126 */
127void efi_sync_low_kernel_mappings(void)
128{
129 unsigned num_pgds;
130 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
131
132 if (efi_enabled(EFI_OLD_MEMMAP))
133 return;
134
135 num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
136
137 memcpy(pgd + pgd_index(PAGE_OFFSET),
138 init_mm.pgd + pgd_index(PAGE_OFFSET),
139 sizeof(pgd_t) * num_pgds);
140}
141
142void efi_setup_page_tables(void)
143{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000144 unsigned long text;
145 unsigned npages;
146 struct page *page;
147
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100148 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
149
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000150 if (efi_enabled(EFI_OLD_MEMMAP))
151 return;
152
153 efi_scratch.use_pgd = true;
154
155 /*
156 * When making calls to the firmware everything needs to be 1:1
157 * mapped and addressable with 32-bit pointers. Map the kernel
158 * text and allocate a new stack because we can't rely on the
159 * stack pointer being < 4GB.
160 */
161 if (!IS_ENABLED(CONFIG_EFI_MIXED))
162 return;
163
164 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
165 if (!page)
166 panic("Unable to allocate EFI runtime stack < 4GB\n");
167
168 efi_scratch.phys_stack = virt_to_phys(page_address(page));
169 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
170
171 npages = (_end - _text) >> PAGE_SHIFT;
172 text = __pa(_text);
173
174 if (kernel_map_pages_in_pgd(__va(efi_scratch.efi_pgt),
175 text >> PAGE_SHIFT, text, npages, 0)) {
176 pr_err("Failed to map kernel text 1:1\n");
177 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100178}
179
180static void __init __map_region(efi_memory_desc_t *md, u64 va)
181{
182 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
Dave Young2da6e572013-12-20 18:02:13 +0800183 unsigned long pf = 0;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100184
185 if (!(md->attribute & EFI_MEMORY_WB))
186 pf |= _PAGE_PCD;
187
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100188 if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
189 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
190 md->phys_addr, va);
191}
192
193void __init efi_map_region(efi_memory_desc_t *md)
194{
195 unsigned long size = md->num_pages << PAGE_SHIFT;
196 u64 pa = md->phys_addr;
197
198 if (efi_enabled(EFI_OLD_MEMMAP))
199 return old_map_region(md);
200
201 /*
202 * Make sure the 1:1 mappings are present as a catch-all for b0rked
203 * firmware which doesn't update all internal pointers after switching
204 * to virtual mode and would otherwise crap on us.
205 */
206 __map_region(md, md->phys_addr);
207
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000208 /*
209 * Enforce the 1:1 mapping as the default virtual address when
210 * booting in EFI mixed mode, because even though we may be
211 * running a 64-bit kernel, the firmware may only be 32-bit.
212 */
213 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
214 md->virt_addr = md->phys_addr;
215 return;
216 }
217
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100218 efi_va -= size;
219
220 /* Is PA 2M-aligned? */
221 if (!(pa & (PMD_SIZE - 1))) {
222 efi_va &= PMD_MASK;
223 } else {
224 u64 pa_offset = pa & (PMD_SIZE - 1);
225 u64 prev_va = efi_va;
226
227 /* get us the same offset within this 2M page */
228 efi_va = (efi_va & PMD_MASK) + pa_offset;
229
230 if (efi_va > prev_va)
231 efi_va -= PMD_SIZE;
232 }
233
234 if (efi_va < EFI_VA_END) {
235 pr_warn(FW_WARN "VA address range overflow!\n");
236 return;
237 }
238
239 /* Do the VA map */
240 __map_region(md, efi_va);
241 md->virt_addr = efi_va;
242}
243
Dave Young3b266492013-12-20 18:02:14 +0800244/*
245 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
246 * md->virt_addr is the original virtual address which had been mapped in kexec
247 * 1st kernel.
248 */
249void __init efi_map_region_fixed(efi_memory_desc_t *md)
250{
251 __map_region(md, md->virt_addr);
252}
253
Keith Packarde1ad7832011-12-11 16:12:42 -0800254void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100255 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800256{
257 unsigned long last_map_pfn;
258
259 if (type == EFI_MEMORY_MAPPED_IO)
260 return ioremap(phys_addr, size);
261
262 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
263 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
264 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100265 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800266 }
267
Matt Fleming3e8fa262012-10-19 13:25:46 +0100268 if (!(attribute & EFI_MEMORY_WB))
269 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
270
Keith Packarde1ad7832011-12-11 16:12:42 -0800271 return (void __iomem *)__va(phys_addr);
272}
Dave Young1fec0532013-12-20 18:02:19 +0800273
274void __init parse_efi_setup(u64 phys_addr, u32 data_len)
275{
276 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800277}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100278
279void __init efi_runtime_mkexec(void)
280{
281 if (!efi_enabled(EFI_OLD_MEMMAP))
282 return;
283
284 if (__supported_pte_mask & _PAGE_NX)
285 runtime_code_page_mkexec();
286}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000287
288#ifdef CONFIG_EFI_MIXED
289extern efi_status_t efi64_thunk(u32, ...);
290
291#define runtime_service32(func) \
292({ \
293 u32 table = (u32)(unsigned long)efi.systab; \
294 u32 *rt, *___f; \
295 \
296 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
297 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
298 *___f; \
299})
300
301/*
302 * Switch to the EFI page tables early so that we can access the 1:1
303 * runtime services mappings which are not mapped in any other page
304 * tables. This function must be called before runtime_service32().
305 *
306 * Also, disable interrupts because the IDT points to 64-bit handlers,
307 * which aren't going to function correctly when we switch to 32-bit.
308 */
309#define efi_thunk(f, ...) \
310({ \
311 efi_status_t __s; \
312 unsigned long flags; \
313 u32 func; \
314 \
315 efi_sync_low_kernel_mappings(); \
316 local_irq_save(flags); \
317 \
318 efi_scratch.prev_cr3 = read_cr3(); \
319 write_cr3((unsigned long)efi_scratch.efi_pgt); \
320 __flush_tlb_all(); \
321 \
322 func = runtime_service32(f); \
323 __s = efi64_thunk(func, __VA_ARGS__); \
324 \
325 write_cr3(efi_scratch.prev_cr3); \
326 __flush_tlb_all(); \
327 local_irq_restore(flags); \
328 \
329 __s; \
330})
331
332efi_status_t efi_thunk_set_virtual_address_map(
333 void *phys_set_virtual_address_map,
334 unsigned long memory_map_size,
335 unsigned long descriptor_size,
336 u32 descriptor_version,
337 efi_memory_desc_t *virtual_map)
338{
339 efi_status_t status;
340 unsigned long flags;
341 u32 func;
342
343 efi_sync_low_kernel_mappings();
344 local_irq_save(flags);
345
346 efi_scratch.prev_cr3 = read_cr3();
347 write_cr3((unsigned long)efi_scratch.efi_pgt);
348 __flush_tlb_all();
349
350 func = (u32)(unsigned long)phys_set_virtual_address_map;
351 status = efi64_thunk(func, memory_map_size, descriptor_size,
352 descriptor_version, virtual_map);
353
354 write_cr3(efi_scratch.prev_cr3);
355 __flush_tlb_all();
356 local_irq_restore(flags);
357
358 return status;
359}
360
361static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
362{
363 efi_status_t status;
364 u32 phys_tm, phys_tc;
365
366 spin_lock(&rtc_lock);
367
368 phys_tm = virt_to_phys(tm);
369 phys_tc = virt_to_phys(tc);
370
371 status = efi_thunk(get_time, phys_tm, phys_tc);
372
373 spin_unlock(&rtc_lock);
374
375 return status;
376}
377
378static efi_status_t efi_thunk_set_time(efi_time_t *tm)
379{
380 efi_status_t status;
381 u32 phys_tm;
382
383 spin_lock(&rtc_lock);
384
385 phys_tm = virt_to_phys(tm);
386
387 status = efi_thunk(set_time, phys_tm);
388
389 spin_unlock(&rtc_lock);
390
391 return status;
392}
393
394static efi_status_t
395efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
396 efi_time_t *tm)
397{
398 efi_status_t status;
399 u32 phys_enabled, phys_pending, phys_tm;
400
401 spin_lock(&rtc_lock);
402
403 phys_enabled = virt_to_phys(enabled);
404 phys_pending = virt_to_phys(pending);
405 phys_tm = virt_to_phys(tm);
406
407 status = efi_thunk(get_wakeup_time, phys_enabled,
408 phys_pending, phys_tm);
409
410 spin_unlock(&rtc_lock);
411
412 return status;
413}
414
415static efi_status_t
416efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
417{
418 efi_status_t status;
419 u32 phys_tm;
420
421 spin_lock(&rtc_lock);
422
423 phys_tm = virt_to_phys(tm);
424
425 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
426
427 spin_unlock(&rtc_lock);
428
429 return status;
430}
431
432
433static efi_status_t
434efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
435 u32 *attr, unsigned long *data_size, void *data)
436{
437 efi_status_t status;
438 u32 phys_name, phys_vendor, phys_attr;
439 u32 phys_data_size, phys_data;
440
441 phys_data_size = virt_to_phys(data_size);
442 phys_vendor = virt_to_phys(vendor);
443 phys_name = virt_to_phys(name);
444 phys_attr = virt_to_phys(attr);
445 phys_data = virt_to_phys(data);
446
447 status = efi_thunk(get_variable, phys_name, phys_vendor,
448 phys_attr, phys_data_size, phys_data);
449
450 return status;
451}
452
453static efi_status_t
454efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
455 u32 attr, unsigned long data_size, void *data)
456{
457 u32 phys_name, phys_vendor, phys_data;
458 efi_status_t status;
459
460 phys_name = virt_to_phys(name);
461 phys_vendor = virt_to_phys(vendor);
462 phys_data = virt_to_phys(data);
463
464 /* If data_size is > sizeof(u32) we've got problems */
465 status = efi_thunk(set_variable, phys_name, phys_vendor,
466 attr, data_size, phys_data);
467
468 return status;
469}
470
471static efi_status_t
472efi_thunk_get_next_variable(unsigned long *name_size,
473 efi_char16_t *name,
474 efi_guid_t *vendor)
475{
476 efi_status_t status;
477 u32 phys_name_size, phys_name, phys_vendor;
478
479 phys_name_size = virt_to_phys(name_size);
480 phys_vendor = virt_to_phys(vendor);
481 phys_name = virt_to_phys(name);
482
483 status = efi_thunk(get_next_variable, phys_name_size,
484 phys_name, phys_vendor);
485
486 return status;
487}
488
489static efi_status_t
490efi_thunk_get_next_high_mono_count(u32 *count)
491{
492 efi_status_t status;
493 u32 phys_count;
494
495 phys_count = virt_to_phys(count);
496 status = efi_thunk(get_next_high_mono_count, phys_count);
497
498 return status;
499}
500
501static void
502efi_thunk_reset_system(int reset_type, efi_status_t status,
503 unsigned long data_size, efi_char16_t *data)
504{
505 u32 phys_data;
506
507 phys_data = virt_to_phys(data);
508
509 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
510}
511
512static efi_status_t
513efi_thunk_update_capsule(efi_capsule_header_t **capsules,
514 unsigned long count, unsigned long sg_list)
515{
516 /*
517 * To properly support this function we would need to repackage
518 * 'capsules' because the firmware doesn't understand 64-bit
519 * pointers.
520 */
521 return EFI_UNSUPPORTED;
522}
523
524static efi_status_t
525efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
526 u64 *remaining_space,
527 u64 *max_variable_size)
528{
529 efi_status_t status;
530 u32 phys_storage, phys_remaining, phys_max;
531
532 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
533 return EFI_UNSUPPORTED;
534
535 phys_storage = virt_to_phys(storage_space);
536 phys_remaining = virt_to_phys(remaining_space);
537 phys_max = virt_to_phys(max_variable_size);
538
539 status = efi_thunk(query_variable_info, phys_storage,
540 phys_remaining, phys_max);
541
542 return status;
543}
544
545static efi_status_t
546efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
547 unsigned long count, u64 *max_size,
548 int *reset_type)
549{
550 /*
551 * To properly support this function we would need to repackage
552 * 'capsules' because the firmware doesn't understand 64-bit
553 * pointers.
554 */
555 return EFI_UNSUPPORTED;
556}
557
558void efi_thunk_runtime_setup(void)
559{
560 efi.get_time = efi_thunk_get_time;
561 efi.set_time = efi_thunk_set_time;
562 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
563 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
564 efi.get_variable = efi_thunk_get_variable;
565 efi.get_next_variable = efi_thunk_get_next_variable;
566 efi.set_variable = efi_thunk_set_variable;
567 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
568 efi.reset_system = efi_thunk_reset_system;
569 efi.query_variable_info = efi_thunk_query_variable_info;
570 efi.update_capsule = efi_thunk_update_capsule;
571 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
572}
573#endif /* CONFIG_EFI_MIXED */