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 #include <linux/slab.h> 31 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> 43 44 static pgd_t *save_pgd __initdata; 45 static unsigned long efi_flags __initdata; 46 47 /* 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 */ 51 static u64 efi_va = EFI_VA_START; 52 53 /* 54 * Scratch space used for switching the pagetable in the EFI stub 55 */ 56 struct efi_scratch { 57 u64 r15; 58 u64 prev_cr3; 59 pgd_t *efi_pgt; 60 bool use_pgd; 61 u64 phys_stack; 62 } __packed; 63 64 static void __init early_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 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 md->type == EFI_BOOT_SERVICES_CODE) 77 efi_set_executable(md, executable); 78 } 79 } 80 81 void __init efi_call_phys_prolog(void) 82 { 83 unsigned long vaddress; 84 int pgd; 85 int n_pgds; 86 87 if (!efi_enabled(EFI_OLD_MEMMAP)) 88 return; 89 90 early_code_mapping_set_exec(1); 91 local_irq_save(efi_flags); 92 93 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE); 94 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL); 95 96 for (pgd = 0; pgd < n_pgds; pgd++) { 97 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE); 98 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE); 99 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress)); 100 } 101 __flush_tlb_all(); 102 } 103 104 void __init efi_call_phys_epilog(void) 105 { 106 /* 107 * After the lock is released, the original page table is restored. 108 */ 109 int pgd; 110 int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE); 111 112 if (!efi_enabled(EFI_OLD_MEMMAP)) 113 return; 114 115 for (pgd = 0; pgd < n_pgds; pgd++) 116 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]); 117 kfree(save_pgd); 118 __flush_tlb_all(); 119 local_irq_restore(efi_flags); 120 early_code_mapping_set_exec(0); 121 } 122 123 /* 124 * Add low kernel mappings for passing arguments to EFI functions. 125 */ 126 void efi_sync_low_kernel_mappings(void) 127 { 128 unsigned num_pgds; 129 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd); 130 131 if (efi_enabled(EFI_OLD_MEMMAP)) 132 return; 133 134 num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET); 135 136 memcpy(pgd + pgd_index(PAGE_OFFSET), 137 init_mm.pgd + pgd_index(PAGE_OFFSET), 138 sizeof(pgd_t) * num_pgds); 139 } 140 141 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) 142 { 143 unsigned long text; 144 struct page *page; 145 unsigned npages; 146 pgd_t *pgd; 147 148 if (efi_enabled(EFI_OLD_MEMMAP)) 149 return 0; 150 151 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd; 152 pgd = __va(efi_scratch.efi_pgt); 153 154 /* 155 * It can happen that the physical address of new_memmap lands in memory 156 * which is not mapped in the EFI page table. Therefore we need to go 157 * and ident-map those pages containing the map before calling 158 * phys_efi_set_virtual_address_map(). 159 */ 160 if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) { 161 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap); 162 return 1; 163 } 164 165 efi_scratch.use_pgd = true; 166 167 /* 168 * When making calls to the firmware everything needs to be 1:1 169 * mapped and addressable with 32-bit pointers. Map the kernel 170 * text and allocate a new stack because we can't rely on the 171 * stack pointer being < 4GB. 172 */ 173 if (!IS_ENABLED(CONFIG_EFI_MIXED)) 174 return 0; 175 176 page = alloc_page(GFP_KERNEL|__GFP_DMA32); 177 if (!page) 178 panic("Unable to allocate EFI runtime stack < 4GB\n"); 179 180 efi_scratch.phys_stack = virt_to_phys(page_address(page)); 181 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */ 182 183 npages = (_end - _text) >> PAGE_SHIFT; 184 text = __pa(_text); 185 186 if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) { 187 pr_err("Failed to map kernel text 1:1\n"); 188 return 1; 189 } 190 191 return 0; 192 } 193 194 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages) 195 { 196 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd); 197 198 kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages); 199 } 200 201 static void __init __map_region(efi_memory_desc_t *md, u64 va) 202 { 203 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd); 204 unsigned long pf = 0; 205 206 if (!(md->attribute & EFI_MEMORY_WB)) 207 pf |= _PAGE_PCD; 208 209 if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf)) 210 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n", 211 md->phys_addr, va); 212 } 213 214 void __init efi_map_region(efi_memory_desc_t *md) 215 { 216 unsigned long size = md->num_pages << PAGE_SHIFT; 217 u64 pa = md->phys_addr; 218 219 if (efi_enabled(EFI_OLD_MEMMAP)) 220 return old_map_region(md); 221 222 /* 223 * Make sure the 1:1 mappings are present as a catch-all for b0rked 224 * firmware which doesn't update all internal pointers after switching 225 * to virtual mode and would otherwise crap on us. 226 */ 227 __map_region(md, md->phys_addr); 228 229 /* 230 * Enforce the 1:1 mapping as the default virtual address when 231 * booting in EFI mixed mode, because even though we may be 232 * running a 64-bit kernel, the firmware may only be 32-bit. 233 */ 234 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) { 235 md->virt_addr = md->phys_addr; 236 return; 237 } 238 239 efi_va -= size; 240 241 /* Is PA 2M-aligned? */ 242 if (!(pa & (PMD_SIZE - 1))) { 243 efi_va &= PMD_MASK; 244 } else { 245 u64 pa_offset = pa & (PMD_SIZE - 1); 246 u64 prev_va = efi_va; 247 248 /* get us the same offset within this 2M page */ 249 efi_va = (efi_va & PMD_MASK) + pa_offset; 250 251 if (efi_va > prev_va) 252 efi_va -= PMD_SIZE; 253 } 254 255 if (efi_va < EFI_VA_END) { 256 pr_warn(FW_WARN "VA address range overflow!\n"); 257 return; 258 } 259 260 /* Do the VA map */ 261 __map_region(md, efi_va); 262 md->virt_addr = efi_va; 263 } 264 265 /* 266 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges. 267 * md->virt_addr is the original virtual address which had been mapped in kexec 268 * 1st kernel. 269 */ 270 void __init efi_map_region_fixed(efi_memory_desc_t *md) 271 { 272 __map_region(md, md->virt_addr); 273 } 274 275 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size, 276 u32 type, u64 attribute) 277 { 278 unsigned long last_map_pfn; 279 280 if (type == EFI_MEMORY_MAPPED_IO) 281 return ioremap(phys_addr, size); 282 283 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size); 284 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) { 285 unsigned long top = last_map_pfn << PAGE_SHIFT; 286 efi_ioremap(top, size - (top - phys_addr), type, attribute); 287 } 288 289 if (!(attribute & EFI_MEMORY_WB)) 290 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size); 291 292 return (void __iomem *)__va(phys_addr); 293 } 294 295 void __init parse_efi_setup(u64 phys_addr, u32 data_len) 296 { 297 efi_setup = phys_addr + sizeof(struct setup_data); 298 } 299 300 void __init efi_runtime_mkexec(void) 301 { 302 if (!efi_enabled(EFI_OLD_MEMMAP)) 303 return; 304 305 if (__supported_pte_mask & _PAGE_NX) 306 runtime_code_page_mkexec(); 307 } 308 309 void __init efi_dump_pagetable(void) 310 { 311 #ifdef CONFIG_EFI_PGT_DUMP 312 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd); 313 314 ptdump_walk_pgd_level(NULL, pgd); 315 #endif 316 } 317 318 #ifdef CONFIG_EFI_MIXED 319 extern efi_status_t efi64_thunk(u32, ...); 320 321 #define runtime_service32(func) \ 322 ({ \ 323 u32 table = (u32)(unsigned long)efi.systab; \ 324 u32 *rt, *___f; \ 325 \ 326 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \ 327 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \ 328 *___f; \ 329 }) 330 331 /* 332 * Switch to the EFI page tables early so that we can access the 1:1 333 * runtime services mappings which are not mapped in any other page 334 * tables. This function must be called before runtime_service32(). 335 * 336 * Also, disable interrupts because the IDT points to 64-bit handlers, 337 * which aren't going to function correctly when we switch to 32-bit. 338 */ 339 #define efi_thunk(f, ...) \ 340 ({ \ 341 efi_status_t __s; \ 342 unsigned long flags; \ 343 u32 func; \ 344 \ 345 efi_sync_low_kernel_mappings(); \ 346 local_irq_save(flags); \ 347 \ 348 efi_scratch.prev_cr3 = read_cr3(); \ 349 write_cr3((unsigned long)efi_scratch.efi_pgt); \ 350 __flush_tlb_all(); \ 351 \ 352 func = runtime_service32(f); \ 353 __s = efi64_thunk(func, __VA_ARGS__); \ 354 \ 355 write_cr3(efi_scratch.prev_cr3); \ 356 __flush_tlb_all(); \ 357 local_irq_restore(flags); \ 358 \ 359 __s; \ 360 }) 361 362 efi_status_t efi_thunk_set_virtual_address_map( 363 void *phys_set_virtual_address_map, 364 unsigned long memory_map_size, 365 unsigned long descriptor_size, 366 u32 descriptor_version, 367 efi_memory_desc_t *virtual_map) 368 { 369 efi_status_t status; 370 unsigned long flags; 371 u32 func; 372 373 efi_sync_low_kernel_mappings(); 374 local_irq_save(flags); 375 376 efi_scratch.prev_cr3 = read_cr3(); 377 write_cr3((unsigned long)efi_scratch.efi_pgt); 378 __flush_tlb_all(); 379 380 func = (u32)(unsigned long)phys_set_virtual_address_map; 381 status = efi64_thunk(func, memory_map_size, descriptor_size, 382 descriptor_version, virtual_map); 383 384 write_cr3(efi_scratch.prev_cr3); 385 __flush_tlb_all(); 386 local_irq_restore(flags); 387 388 return status; 389 } 390 391 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc) 392 { 393 efi_status_t status; 394 u32 phys_tm, phys_tc; 395 396 spin_lock(&rtc_lock); 397 398 phys_tm = virt_to_phys(tm); 399 phys_tc = virt_to_phys(tc); 400 401 status = efi_thunk(get_time, phys_tm, phys_tc); 402 403 spin_unlock(&rtc_lock); 404 405 return status; 406 } 407 408 static efi_status_t efi_thunk_set_time(efi_time_t *tm) 409 { 410 efi_status_t status; 411 u32 phys_tm; 412 413 spin_lock(&rtc_lock); 414 415 phys_tm = virt_to_phys(tm); 416 417 status = efi_thunk(set_time, phys_tm); 418 419 spin_unlock(&rtc_lock); 420 421 return status; 422 } 423 424 static efi_status_t 425 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending, 426 efi_time_t *tm) 427 { 428 efi_status_t status; 429 u32 phys_enabled, phys_pending, phys_tm; 430 431 spin_lock(&rtc_lock); 432 433 phys_enabled = virt_to_phys(enabled); 434 phys_pending = virt_to_phys(pending); 435 phys_tm = virt_to_phys(tm); 436 437 status = efi_thunk(get_wakeup_time, phys_enabled, 438 phys_pending, phys_tm); 439 440 spin_unlock(&rtc_lock); 441 442 return status; 443 } 444 445 static efi_status_t 446 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) 447 { 448 efi_status_t status; 449 u32 phys_tm; 450 451 spin_lock(&rtc_lock); 452 453 phys_tm = virt_to_phys(tm); 454 455 status = efi_thunk(set_wakeup_time, enabled, phys_tm); 456 457 spin_unlock(&rtc_lock); 458 459 return status; 460 } 461 462 463 static efi_status_t 464 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor, 465 u32 *attr, unsigned long *data_size, void *data) 466 { 467 efi_status_t status; 468 u32 phys_name, phys_vendor, phys_attr; 469 u32 phys_data_size, phys_data; 470 471 phys_data_size = virt_to_phys(data_size); 472 phys_vendor = virt_to_phys(vendor); 473 phys_name = virt_to_phys(name); 474 phys_attr = virt_to_phys(attr); 475 phys_data = virt_to_phys(data); 476 477 status = efi_thunk(get_variable, phys_name, phys_vendor, 478 phys_attr, phys_data_size, phys_data); 479 480 return status; 481 } 482 483 static efi_status_t 484 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor, 485 u32 attr, unsigned long data_size, void *data) 486 { 487 u32 phys_name, phys_vendor, phys_data; 488 efi_status_t status; 489 490 phys_name = virt_to_phys(name); 491 phys_vendor = virt_to_phys(vendor); 492 phys_data = virt_to_phys(data); 493 494 /* If data_size is > sizeof(u32) we've got problems */ 495 status = efi_thunk(set_variable, phys_name, phys_vendor, 496 attr, data_size, phys_data); 497 498 return status; 499 } 500 501 static efi_status_t 502 efi_thunk_get_next_variable(unsigned long *name_size, 503 efi_char16_t *name, 504 efi_guid_t *vendor) 505 { 506 efi_status_t status; 507 u32 phys_name_size, phys_name, phys_vendor; 508 509 phys_name_size = virt_to_phys(name_size); 510 phys_vendor = virt_to_phys(vendor); 511 phys_name = virt_to_phys(name); 512 513 status = efi_thunk(get_next_variable, phys_name_size, 514 phys_name, phys_vendor); 515 516 return status; 517 } 518 519 static efi_status_t 520 efi_thunk_get_next_high_mono_count(u32 *count) 521 { 522 efi_status_t status; 523 u32 phys_count; 524 525 phys_count = virt_to_phys(count); 526 status = efi_thunk(get_next_high_mono_count, phys_count); 527 528 return status; 529 } 530 531 static void 532 efi_thunk_reset_system(int reset_type, efi_status_t status, 533 unsigned long data_size, efi_char16_t *data) 534 { 535 u32 phys_data; 536 537 phys_data = virt_to_phys(data); 538 539 efi_thunk(reset_system, reset_type, status, data_size, phys_data); 540 } 541 542 static efi_status_t 543 efi_thunk_update_capsule(efi_capsule_header_t **capsules, 544 unsigned long count, unsigned long sg_list) 545 { 546 /* 547 * To properly support this function we would need to repackage 548 * 'capsules' because the firmware doesn't understand 64-bit 549 * pointers. 550 */ 551 return EFI_UNSUPPORTED; 552 } 553 554 static efi_status_t 555 efi_thunk_query_variable_info(u32 attr, u64 *storage_space, 556 u64 *remaining_space, 557 u64 *max_variable_size) 558 { 559 efi_status_t status; 560 u32 phys_storage, phys_remaining, phys_max; 561 562 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 563 return EFI_UNSUPPORTED; 564 565 phys_storage = virt_to_phys(storage_space); 566 phys_remaining = virt_to_phys(remaining_space); 567 phys_max = virt_to_phys(max_variable_size); 568 569 status = efi_thunk(query_variable_info, attr, phys_storage, 570 phys_remaining, phys_max); 571 572 return status; 573 } 574 575 static efi_status_t 576 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules, 577 unsigned long count, u64 *max_size, 578 int *reset_type) 579 { 580 /* 581 * To properly support this function we would need to repackage 582 * 'capsules' because the firmware doesn't understand 64-bit 583 * pointers. 584 */ 585 return EFI_UNSUPPORTED; 586 } 587 588 void efi_thunk_runtime_setup(void) 589 { 590 efi.get_time = efi_thunk_get_time; 591 efi.set_time = efi_thunk_set_time; 592 efi.get_wakeup_time = efi_thunk_get_wakeup_time; 593 efi.set_wakeup_time = efi_thunk_set_wakeup_time; 594 efi.get_variable = efi_thunk_get_variable; 595 efi.get_next_variable = efi_thunk_get_next_variable; 596 efi.set_variable = efi_thunk_set_variable; 597 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count; 598 efi.reset_system = efi_thunk_reset_system; 599 efi.query_variable_info = efi_thunk_query_variable_info; 600 efi.update_capsule = efi_thunk_update_capsule; 601 efi.query_capsule_caps = efi_thunk_query_capsule_caps; 602 } 603 #endif /* CONFIG_EFI_MIXED */ 604