1 /* 2 * Extensible Firmware Interface 3 * 4 * Based on Extensible Firmware Interface Specification version 0.9 5 * April 30, 1999 6 * 7 * Copyright (C) 1999 VA Linux Systems 8 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> 9 * Copyright (C) 1999-2003 Hewlett-Packard Co. 10 * David Mosberger-Tang <davidm@hpl.hp.com> 11 * Stephane Eranian <eranian@hpl.hp.com> 12 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P. 13 * Bjorn Helgaas <bjorn.helgaas@hp.com> 14 * 15 * All EFI Runtime Services are not implemented yet as EFI only 16 * supports physical mode addressing on SoftSDV. This is to be fixed 17 * in a future version. --drummond 1999-07-20 18 * 19 * Implemented EFI runtime services and virtual mode calls. --davidm 20 * 21 * Goutham Rao: <goutham.rao@intel.com> 22 * Skip non-WB memory and ignore empty memory ranges. 23 */ 24 #include <linux/module.h> 25 #include <linux/bootmem.h> 26 #include <linux/crash_dump.h> 27 #include <linux/kernel.h> 28 #include <linux/init.h> 29 #include <linux/types.h> 30 #include <linux/slab.h> 31 #include <linux/time.h> 32 #include <linux/efi.h> 33 #include <linux/kexec.h> 34 #include <linux/mm.h> 35 36 #include <asm/io.h> 37 #include <asm/kregs.h> 38 #include <asm/meminit.h> 39 #include <asm/pgtable.h> 40 #include <asm/processor.h> 41 #include <asm/mca.h> 42 #include <asm/setup.h> 43 #include <asm/tlbflush.h> 44 45 #define EFI_DEBUG 0 46 47 static __initdata unsigned long palo_phys; 48 49 static __initdata efi_config_table_type_t arch_tables[] = { 50 {PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID, "PALO", &palo_phys}, 51 {NULL_GUID, NULL, 0}, 52 }; 53 54 extern efi_status_t efi_call_phys (void *, ...); 55 56 static efi_runtime_services_t *runtime; 57 static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL; 58 59 #define efi_call_virt(f, args...) (*(f))(args) 60 61 #define STUB_GET_TIME(prefix, adjust_arg) \ 62 static efi_status_t \ 63 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \ 64 { \ 65 struct ia64_fpreg fr[6]; \ 66 efi_time_cap_t *atc = NULL; \ 67 efi_status_t ret; \ 68 \ 69 if (tc) \ 70 atc = adjust_arg(tc); \ 71 ia64_save_scratch_fpregs(fr); \ 72 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \ 73 adjust_arg(tm), atc); \ 74 ia64_load_scratch_fpregs(fr); \ 75 return ret; \ 76 } 77 78 #define STUB_SET_TIME(prefix, adjust_arg) \ 79 static efi_status_t \ 80 prefix##_set_time (efi_time_t *tm) \ 81 { \ 82 struct ia64_fpreg fr[6]; \ 83 efi_status_t ret; \ 84 \ 85 ia64_save_scratch_fpregs(fr); \ 86 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \ 87 adjust_arg(tm)); \ 88 ia64_load_scratch_fpregs(fr); \ 89 return ret; \ 90 } 91 92 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \ 93 static efi_status_t \ 94 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \ 95 efi_time_t *tm) \ 96 { \ 97 struct ia64_fpreg fr[6]; \ 98 efi_status_t ret; \ 99 \ 100 ia64_save_scratch_fpregs(fr); \ 101 ret = efi_call_##prefix( \ 102 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \ 103 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \ 104 ia64_load_scratch_fpregs(fr); \ 105 return ret; \ 106 } 107 108 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \ 109 static efi_status_t \ 110 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \ 111 { \ 112 struct ia64_fpreg fr[6]; \ 113 efi_time_t *atm = NULL; \ 114 efi_status_t ret; \ 115 \ 116 if (tm) \ 117 atm = adjust_arg(tm); \ 118 ia64_save_scratch_fpregs(fr); \ 119 ret = efi_call_##prefix( \ 120 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \ 121 enabled, atm); \ 122 ia64_load_scratch_fpregs(fr); \ 123 return ret; \ 124 } 125 126 #define STUB_GET_VARIABLE(prefix, adjust_arg) \ 127 static efi_status_t \ 128 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \ 129 unsigned long *data_size, void *data) \ 130 { \ 131 struct ia64_fpreg fr[6]; \ 132 u32 *aattr = NULL; \ 133 efi_status_t ret; \ 134 \ 135 if (attr) \ 136 aattr = adjust_arg(attr); \ 137 ia64_save_scratch_fpregs(fr); \ 138 ret = efi_call_##prefix( \ 139 (efi_get_variable_t *) __va(runtime->get_variable), \ 140 adjust_arg(name), adjust_arg(vendor), aattr, \ 141 adjust_arg(data_size), adjust_arg(data)); \ 142 ia64_load_scratch_fpregs(fr); \ 143 return ret; \ 144 } 145 146 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \ 147 static efi_status_t \ 148 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \ 149 efi_guid_t *vendor) \ 150 { \ 151 struct ia64_fpreg fr[6]; \ 152 efi_status_t ret; \ 153 \ 154 ia64_save_scratch_fpregs(fr); \ 155 ret = efi_call_##prefix( \ 156 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \ 157 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \ 158 ia64_load_scratch_fpregs(fr); \ 159 return ret; \ 160 } 161 162 #define STUB_SET_VARIABLE(prefix, adjust_arg) \ 163 static efi_status_t \ 164 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \ 165 u32 attr, unsigned long data_size, \ 166 void *data) \ 167 { \ 168 struct ia64_fpreg fr[6]; \ 169 efi_status_t ret; \ 170 \ 171 ia64_save_scratch_fpregs(fr); \ 172 ret = efi_call_##prefix( \ 173 (efi_set_variable_t *) __va(runtime->set_variable), \ 174 adjust_arg(name), adjust_arg(vendor), attr, data_size, \ 175 adjust_arg(data)); \ 176 ia64_load_scratch_fpregs(fr); \ 177 return ret; \ 178 } 179 180 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \ 181 static efi_status_t \ 182 prefix##_get_next_high_mono_count (u32 *count) \ 183 { \ 184 struct ia64_fpreg fr[6]; \ 185 efi_status_t ret; \ 186 \ 187 ia64_save_scratch_fpregs(fr); \ 188 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \ 189 __va(runtime->get_next_high_mono_count), \ 190 adjust_arg(count)); \ 191 ia64_load_scratch_fpregs(fr); \ 192 return ret; \ 193 } 194 195 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \ 196 static void \ 197 prefix##_reset_system (int reset_type, efi_status_t status, \ 198 unsigned long data_size, efi_char16_t *data) \ 199 { \ 200 struct ia64_fpreg fr[6]; \ 201 efi_char16_t *adata = NULL; \ 202 \ 203 if (data) \ 204 adata = adjust_arg(data); \ 205 \ 206 ia64_save_scratch_fpregs(fr); \ 207 efi_call_##prefix( \ 208 (efi_reset_system_t *) __va(runtime->reset_system), \ 209 reset_type, status, data_size, adata); \ 210 /* should not return, but just in case... */ \ 211 ia64_load_scratch_fpregs(fr); \ 212 } 213 214 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg)) 215 216 STUB_GET_TIME(phys, phys_ptr) 217 STUB_SET_TIME(phys, phys_ptr) 218 STUB_GET_WAKEUP_TIME(phys, phys_ptr) 219 STUB_SET_WAKEUP_TIME(phys, phys_ptr) 220 STUB_GET_VARIABLE(phys, phys_ptr) 221 STUB_GET_NEXT_VARIABLE(phys, phys_ptr) 222 STUB_SET_VARIABLE(phys, phys_ptr) 223 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr) 224 STUB_RESET_SYSTEM(phys, phys_ptr) 225 226 #define id(arg) arg 227 228 STUB_GET_TIME(virt, id) 229 STUB_SET_TIME(virt, id) 230 STUB_GET_WAKEUP_TIME(virt, id) 231 STUB_SET_WAKEUP_TIME(virt, id) 232 STUB_GET_VARIABLE(virt, id) 233 STUB_GET_NEXT_VARIABLE(virt, id) 234 STUB_SET_VARIABLE(virt, id) 235 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id) 236 STUB_RESET_SYSTEM(virt, id) 237 238 void 239 efi_gettimeofday (struct timespec *ts) 240 { 241 efi_time_t tm; 242 243 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) { 244 memset(ts, 0, sizeof(*ts)); 245 return; 246 } 247 248 ts->tv_sec = mktime(tm.year, tm.month, tm.day, 249 tm.hour, tm.minute, tm.second); 250 ts->tv_nsec = tm.nanosecond; 251 } 252 253 static int 254 is_memory_available (efi_memory_desc_t *md) 255 { 256 if (!(md->attribute & EFI_MEMORY_WB)) 257 return 0; 258 259 switch (md->type) { 260 case EFI_LOADER_CODE: 261 case EFI_LOADER_DATA: 262 case EFI_BOOT_SERVICES_CODE: 263 case EFI_BOOT_SERVICES_DATA: 264 case EFI_CONVENTIONAL_MEMORY: 265 return 1; 266 } 267 return 0; 268 } 269 270 typedef struct kern_memdesc { 271 u64 attribute; 272 u64 start; 273 u64 num_pages; 274 } kern_memdesc_t; 275 276 static kern_memdesc_t *kern_memmap; 277 278 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT) 279 280 static inline u64 281 kmd_end(kern_memdesc_t *kmd) 282 { 283 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT)); 284 } 285 286 static inline u64 287 efi_md_end(efi_memory_desc_t *md) 288 { 289 return (md->phys_addr + efi_md_size(md)); 290 } 291 292 static inline int 293 efi_wb(efi_memory_desc_t *md) 294 { 295 return (md->attribute & EFI_MEMORY_WB); 296 } 297 298 static inline int 299 efi_uc(efi_memory_desc_t *md) 300 { 301 return (md->attribute & EFI_MEMORY_UC); 302 } 303 304 static void 305 walk (efi_freemem_callback_t callback, void *arg, u64 attr) 306 { 307 kern_memdesc_t *k; 308 u64 start, end, voff; 309 310 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET; 311 for (k = kern_memmap; k->start != ~0UL; k++) { 312 if (k->attribute != attr) 313 continue; 314 start = PAGE_ALIGN(k->start); 315 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK; 316 if (start < end) 317 if ((*callback)(start + voff, end + voff, arg) < 0) 318 return; 319 } 320 } 321 322 /* 323 * Walk the EFI memory map and call CALLBACK once for each EFI memory 324 * descriptor that has memory that is available for OS use. 325 */ 326 void 327 efi_memmap_walk (efi_freemem_callback_t callback, void *arg) 328 { 329 walk(callback, arg, EFI_MEMORY_WB); 330 } 331 332 /* 333 * Walk the EFI memory map and call CALLBACK once for each EFI memory 334 * descriptor that has memory that is available for uncached allocator. 335 */ 336 void 337 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg) 338 { 339 walk(callback, arg, EFI_MEMORY_UC); 340 } 341 342 /* 343 * Look for the PAL_CODE region reported by EFI and map it using an 344 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor 345 * Abstraction Layer chapter 11 in ADAG 346 */ 347 void * 348 efi_get_pal_addr (void) 349 { 350 void *efi_map_start, *efi_map_end, *p; 351 efi_memory_desc_t *md; 352 u64 efi_desc_size; 353 int pal_code_count = 0; 354 u64 vaddr, mask; 355 356 efi_map_start = __va(ia64_boot_param->efi_memmap); 357 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 358 efi_desc_size = ia64_boot_param->efi_memdesc_size; 359 360 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 361 md = p; 362 if (md->type != EFI_PAL_CODE) 363 continue; 364 365 if (++pal_code_count > 1) { 366 printk(KERN_ERR "Too many EFI Pal Code memory ranges, " 367 "dropped @ %llx\n", md->phys_addr); 368 continue; 369 } 370 /* 371 * The only ITLB entry in region 7 that is used is the one 372 * installed by __start(). That entry covers a 64MB range. 373 */ 374 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1); 375 vaddr = PAGE_OFFSET + md->phys_addr; 376 377 /* 378 * We must check that the PAL mapping won't overlap with the 379 * kernel mapping. 380 * 381 * PAL code is guaranteed to be aligned on a power of 2 between 382 * 4k and 256KB and that only one ITR is needed to map it. This 383 * implies that the PAL code is always aligned on its size, 384 * i.e., the closest matching page size supported by the TLB. 385 * Therefore PAL code is guaranteed never to cross a 64MB unless 386 * it is bigger than 64MB (very unlikely!). So for now the 387 * following test is enough to determine whether or not we need 388 * a dedicated ITR for the PAL code. 389 */ 390 if ((vaddr & mask) == (KERNEL_START & mask)) { 391 printk(KERN_INFO "%s: no need to install ITR for PAL code\n", 392 __func__); 393 continue; 394 } 395 396 if (efi_md_size(md) > IA64_GRANULE_SIZE) 397 panic("Whoa! PAL code size bigger than a granule!"); 398 399 #if EFI_DEBUG 400 mask = ~((1 << IA64_GRANULE_SHIFT) - 1); 401 402 printk(KERN_INFO "CPU %d: mapping PAL code " 403 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n", 404 smp_processor_id(), md->phys_addr, 405 md->phys_addr + efi_md_size(md), 406 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE); 407 #endif 408 return __va(md->phys_addr); 409 } 410 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n", 411 __func__); 412 return NULL; 413 } 414 415 416 static u8 __init palo_checksum(u8 *buffer, u32 length) 417 { 418 u8 sum = 0; 419 u8 *end = buffer + length; 420 421 while (buffer < end) 422 sum = (u8) (sum + *(buffer++)); 423 424 return sum; 425 } 426 427 /* 428 * Parse and handle PALO table which is published at: 429 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf 430 */ 431 static void __init handle_palo(unsigned long phys_addr) 432 { 433 struct palo_table *palo = __va(phys_addr); 434 u8 checksum; 435 436 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) { 437 printk(KERN_INFO "PALO signature incorrect.\n"); 438 return; 439 } 440 441 checksum = palo_checksum((u8 *)palo, palo->length); 442 if (checksum) { 443 printk(KERN_INFO "PALO checksum incorrect.\n"); 444 return; 445 } 446 447 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO); 448 } 449 450 void 451 efi_map_pal_code (void) 452 { 453 void *pal_vaddr = efi_get_pal_addr (); 454 u64 psr; 455 456 if (!pal_vaddr) 457 return; 458 459 /* 460 * Cannot write to CRx with PSR.ic=1 461 */ 462 psr = ia64_clear_ic(); 463 ia64_itr(0x1, IA64_TR_PALCODE, 464 GRANULEROUNDDOWN((unsigned long) pal_vaddr), 465 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)), 466 IA64_GRANULE_SHIFT); 467 paravirt_dv_serialize_data(); 468 ia64_set_psr(psr); /* restore psr */ 469 } 470 471 void __init 472 efi_init (void) 473 { 474 void *efi_map_start, *efi_map_end; 475 efi_char16_t *c16; 476 u64 efi_desc_size; 477 char *cp, vendor[100] = "unknown"; 478 int i; 479 480 set_bit(EFI_BOOT, &efi.flags); 481 set_bit(EFI_64BIT, &efi.flags); 482 483 /* 484 * It's too early to be able to use the standard kernel command line 485 * support... 486 */ 487 for (cp = boot_command_line; *cp; ) { 488 if (memcmp(cp, "mem=", 4) == 0) { 489 mem_limit = memparse(cp + 4, &cp); 490 } else if (memcmp(cp, "max_addr=", 9) == 0) { 491 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); 492 } else if (memcmp(cp, "min_addr=", 9) == 0) { 493 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp)); 494 } else { 495 while (*cp != ' ' && *cp) 496 ++cp; 497 while (*cp == ' ') 498 ++cp; 499 } 500 } 501 if (min_addr != 0UL) 502 printk(KERN_INFO "Ignoring memory below %lluMB\n", 503 min_addr >> 20); 504 if (max_addr != ~0UL) 505 printk(KERN_INFO "Ignoring memory above %lluMB\n", 506 max_addr >> 20); 507 508 efi.systab = __va(ia64_boot_param->efi_systab); 509 510 /* 511 * Verify the EFI Table 512 */ 513 if (efi.systab == NULL) 514 panic("Whoa! Can't find EFI system table.\n"); 515 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 516 panic("Whoa! EFI system table signature incorrect\n"); 517 if ((efi.systab->hdr.revision >> 16) == 0) 518 printk(KERN_WARNING "Warning: EFI system table version " 519 "%d.%02d, expected 1.00 or greater\n", 520 efi.systab->hdr.revision >> 16, 521 efi.systab->hdr.revision & 0xffff); 522 523 /* Show what we know for posterity */ 524 c16 = __va(efi.systab->fw_vendor); 525 if (c16) { 526 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i) 527 vendor[i] = *c16++; 528 vendor[i] = '\0'; 529 } 530 531 printk(KERN_INFO "EFI v%u.%.02u by %s:", 532 efi.systab->hdr.revision >> 16, 533 efi.systab->hdr.revision & 0xffff, vendor); 534 535 set_bit(EFI_SYSTEM_TABLES, &efi.flags); 536 537 palo_phys = EFI_INVALID_TABLE_ADDR; 538 539 if (efi_config_init(arch_tables) != 0) 540 return; 541 542 if (palo_phys != EFI_INVALID_TABLE_ADDR) 543 handle_palo(palo_phys); 544 545 runtime = __va(efi.systab->runtime); 546 efi.get_time = phys_get_time; 547 efi.set_time = phys_set_time; 548 efi.get_wakeup_time = phys_get_wakeup_time; 549 efi.set_wakeup_time = phys_set_wakeup_time; 550 efi.get_variable = phys_get_variable; 551 efi.get_next_variable = phys_get_next_variable; 552 efi.set_variable = phys_set_variable; 553 efi.get_next_high_mono_count = phys_get_next_high_mono_count; 554 efi.reset_system = phys_reset_system; 555 556 efi_map_start = __va(ia64_boot_param->efi_memmap); 557 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 558 efi_desc_size = ia64_boot_param->efi_memdesc_size; 559 560 #if EFI_DEBUG 561 /* print EFI memory map: */ 562 { 563 efi_memory_desc_t *md; 564 void *p; 565 566 for (i = 0, p = efi_map_start; p < efi_map_end; 567 ++i, p += efi_desc_size) 568 { 569 const char *unit; 570 unsigned long size; 571 char buf[64]; 572 573 md = p; 574 size = md->num_pages << EFI_PAGE_SHIFT; 575 576 if ((size >> 40) > 0) { 577 size >>= 40; 578 unit = "TB"; 579 } else if ((size >> 30) > 0) { 580 size >>= 30; 581 unit = "GB"; 582 } else if ((size >> 20) > 0) { 583 size >>= 20; 584 unit = "MB"; 585 } else { 586 size >>= 10; 587 unit = "KB"; 588 } 589 590 printk("mem%02d: %s " 591 "range=[0x%016lx-0x%016lx) (%4lu%s)\n", 592 i, efi_md_typeattr_format(buf, sizeof(buf), md), 593 md->phys_addr, 594 md->phys_addr + efi_md_size(md), size, unit); 595 } 596 } 597 #endif 598 599 efi_map_pal_code(); 600 efi_enter_virtual_mode(); 601 } 602 603 void 604 efi_enter_virtual_mode (void) 605 { 606 void *efi_map_start, *efi_map_end, *p; 607 efi_memory_desc_t *md; 608 efi_status_t status; 609 u64 efi_desc_size; 610 611 efi_map_start = __va(ia64_boot_param->efi_memmap); 612 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 613 efi_desc_size = ia64_boot_param->efi_memdesc_size; 614 615 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 616 md = p; 617 if (md->attribute & EFI_MEMORY_RUNTIME) { 618 /* 619 * Some descriptors have multiple bits set, so the 620 * order of the tests is relevant. 621 */ 622 if (md->attribute & EFI_MEMORY_WB) { 623 md->virt_addr = (u64) __va(md->phys_addr); 624 } else if (md->attribute & EFI_MEMORY_UC) { 625 md->virt_addr = (u64) ioremap(md->phys_addr, 0); 626 } else if (md->attribute & EFI_MEMORY_WC) { 627 #if 0 628 md->virt_addr = ia64_remap(md->phys_addr, 629 (_PAGE_A | 630 _PAGE_P | 631 _PAGE_D | 632 _PAGE_MA_WC | 633 _PAGE_PL_0 | 634 _PAGE_AR_RW)); 635 #else 636 printk(KERN_INFO "EFI_MEMORY_WC mapping\n"); 637 md->virt_addr = (u64) ioremap(md->phys_addr, 0); 638 #endif 639 } else if (md->attribute & EFI_MEMORY_WT) { 640 #if 0 641 md->virt_addr = ia64_remap(md->phys_addr, 642 (_PAGE_A | 643 _PAGE_P | 644 _PAGE_D | 645 _PAGE_MA_WT | 646 _PAGE_PL_0 | 647 _PAGE_AR_RW)); 648 #else 649 printk(KERN_INFO "EFI_MEMORY_WT mapping\n"); 650 md->virt_addr = (u64) ioremap(md->phys_addr, 0); 651 #endif 652 } 653 } 654 } 655 656 status = efi_call_phys(__va(runtime->set_virtual_address_map), 657 ia64_boot_param->efi_memmap_size, 658 efi_desc_size, 659 ia64_boot_param->efi_memdesc_version, 660 ia64_boot_param->efi_memmap); 661 if (status != EFI_SUCCESS) { 662 printk(KERN_WARNING "warning: unable to switch EFI into " 663 "virtual mode (status=%lu)\n", status); 664 return; 665 } 666 667 set_bit(EFI_RUNTIME_SERVICES, &efi.flags); 668 669 /* 670 * Now that EFI is in virtual mode, we call the EFI functions more 671 * efficiently: 672 */ 673 efi.get_time = virt_get_time; 674 efi.set_time = virt_set_time; 675 efi.get_wakeup_time = virt_get_wakeup_time; 676 efi.set_wakeup_time = virt_set_wakeup_time; 677 efi.get_variable = virt_get_variable; 678 efi.get_next_variable = virt_get_next_variable; 679 efi.set_variable = virt_set_variable; 680 efi.get_next_high_mono_count = virt_get_next_high_mono_count; 681 efi.reset_system = virt_reset_system; 682 } 683 684 /* 685 * Walk the EFI memory map looking for the I/O port range. There can only be 686 * one entry of this type, other I/O port ranges should be described via ACPI. 687 */ 688 u64 689 efi_get_iobase (void) 690 { 691 void *efi_map_start, *efi_map_end, *p; 692 efi_memory_desc_t *md; 693 u64 efi_desc_size; 694 695 efi_map_start = __va(ia64_boot_param->efi_memmap); 696 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 697 efi_desc_size = ia64_boot_param->efi_memdesc_size; 698 699 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 700 md = p; 701 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) { 702 if (md->attribute & EFI_MEMORY_UC) 703 return md->phys_addr; 704 } 705 } 706 return 0; 707 } 708 709 static struct kern_memdesc * 710 kern_memory_descriptor (unsigned long phys_addr) 711 { 712 struct kern_memdesc *md; 713 714 for (md = kern_memmap; md->start != ~0UL; md++) { 715 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT)) 716 return md; 717 } 718 return NULL; 719 } 720 721 static efi_memory_desc_t * 722 efi_memory_descriptor (unsigned long phys_addr) 723 { 724 void *efi_map_start, *efi_map_end, *p; 725 efi_memory_desc_t *md; 726 u64 efi_desc_size; 727 728 efi_map_start = __va(ia64_boot_param->efi_memmap); 729 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 730 efi_desc_size = ia64_boot_param->efi_memdesc_size; 731 732 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 733 md = p; 734 735 if (phys_addr - md->phys_addr < efi_md_size(md)) 736 return md; 737 } 738 return NULL; 739 } 740 741 static int 742 efi_memmap_intersects (unsigned long phys_addr, unsigned long size) 743 { 744 void *efi_map_start, *efi_map_end, *p; 745 efi_memory_desc_t *md; 746 u64 efi_desc_size; 747 unsigned long end; 748 749 efi_map_start = __va(ia64_boot_param->efi_memmap); 750 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 751 efi_desc_size = ia64_boot_param->efi_memdesc_size; 752 753 end = phys_addr + size; 754 755 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 756 md = p; 757 if (md->phys_addr < end && efi_md_end(md) > phys_addr) 758 return 1; 759 } 760 return 0; 761 } 762 763 u32 764 efi_mem_type (unsigned long phys_addr) 765 { 766 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); 767 768 if (md) 769 return md->type; 770 return 0; 771 } 772 773 u64 774 efi_mem_attributes (unsigned long phys_addr) 775 { 776 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); 777 778 if (md) 779 return md->attribute; 780 return 0; 781 } 782 EXPORT_SYMBOL(efi_mem_attributes); 783 784 u64 785 efi_mem_attribute (unsigned long phys_addr, unsigned long size) 786 { 787 unsigned long end = phys_addr + size; 788 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr); 789 u64 attr; 790 791 if (!md) 792 return 0; 793 794 /* 795 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells 796 * the kernel that firmware needs this region mapped. 797 */ 798 attr = md->attribute & ~EFI_MEMORY_RUNTIME; 799 do { 800 unsigned long md_end = efi_md_end(md); 801 802 if (end <= md_end) 803 return attr; 804 805 md = efi_memory_descriptor(md_end); 806 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr) 807 return 0; 808 } while (md); 809 return 0; /* never reached */ 810 } 811 812 u64 813 kern_mem_attribute (unsigned long phys_addr, unsigned long size) 814 { 815 unsigned long end = phys_addr + size; 816 struct kern_memdesc *md; 817 u64 attr; 818 819 /* 820 * This is a hack for ioremap calls before we set up kern_memmap. 821 * Maybe we should do efi_memmap_init() earlier instead. 822 */ 823 if (!kern_memmap) { 824 attr = efi_mem_attribute(phys_addr, size); 825 if (attr & EFI_MEMORY_WB) 826 return EFI_MEMORY_WB; 827 return 0; 828 } 829 830 md = kern_memory_descriptor(phys_addr); 831 if (!md) 832 return 0; 833 834 attr = md->attribute; 835 do { 836 unsigned long md_end = kmd_end(md); 837 838 if (end <= md_end) 839 return attr; 840 841 md = kern_memory_descriptor(md_end); 842 if (!md || md->attribute != attr) 843 return 0; 844 } while (md); 845 return 0; /* never reached */ 846 } 847 EXPORT_SYMBOL(kern_mem_attribute); 848 849 int 850 valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size) 851 { 852 u64 attr; 853 854 /* 855 * /dev/mem reads and writes use copy_to_user(), which implicitly 856 * uses a granule-sized kernel identity mapping. It's really 857 * only safe to do this for regions in kern_memmap. For more 858 * details, see Documentation/ia64/aliasing.txt. 859 */ 860 attr = kern_mem_attribute(phys_addr, size); 861 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) 862 return 1; 863 return 0; 864 } 865 866 int 867 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size) 868 { 869 unsigned long phys_addr = pfn << PAGE_SHIFT; 870 u64 attr; 871 872 attr = efi_mem_attribute(phys_addr, size); 873 874 /* 875 * /dev/mem mmap uses normal user pages, so we don't need the entire 876 * granule, but the entire region we're mapping must support the same 877 * attribute. 878 */ 879 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC) 880 return 1; 881 882 /* 883 * Intel firmware doesn't tell us about all the MMIO regions, so 884 * in general we have to allow mmap requests. But if EFI *does* 885 * tell us about anything inside this region, we should deny it. 886 * The user can always map a smaller region to avoid the overlap. 887 */ 888 if (efi_memmap_intersects(phys_addr, size)) 889 return 0; 890 891 return 1; 892 } 893 894 pgprot_t 895 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size, 896 pgprot_t vma_prot) 897 { 898 unsigned long phys_addr = pfn << PAGE_SHIFT; 899 u64 attr; 900 901 /* 902 * For /dev/mem mmap, we use user mappings, but if the region is 903 * in kern_memmap (and hence may be covered by a kernel mapping), 904 * we must use the same attribute as the kernel mapping. 905 */ 906 attr = kern_mem_attribute(phys_addr, size); 907 if (attr & EFI_MEMORY_WB) 908 return pgprot_cacheable(vma_prot); 909 else if (attr & EFI_MEMORY_UC) 910 return pgprot_noncached(vma_prot); 911 912 /* 913 * Some chipsets don't support UC access to memory. If 914 * WB is supported, we prefer that. 915 */ 916 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB) 917 return pgprot_cacheable(vma_prot); 918 919 return pgprot_noncached(vma_prot); 920 } 921 922 int __init 923 efi_uart_console_only(void) 924 { 925 efi_status_t status; 926 char *s, name[] = "ConOut"; 927 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID; 928 efi_char16_t *utf16, name_utf16[32]; 929 unsigned char data[1024]; 930 unsigned long size = sizeof(data); 931 struct efi_generic_dev_path *hdr, *end_addr; 932 int uart = 0; 933 934 /* Convert to UTF-16 */ 935 utf16 = name_utf16; 936 s = name; 937 while (*s) 938 *utf16++ = *s++ & 0x7f; 939 *utf16 = 0; 940 941 status = efi.get_variable(name_utf16, &guid, NULL, &size, data); 942 if (status != EFI_SUCCESS) { 943 printk(KERN_ERR "No EFI %s variable?\n", name); 944 return 0; 945 } 946 947 hdr = (struct efi_generic_dev_path *) data; 948 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size); 949 while (hdr < end_addr) { 950 if (hdr->type == EFI_DEV_MSG && 951 hdr->sub_type == EFI_DEV_MSG_UART) 952 uart = 1; 953 else if (hdr->type == EFI_DEV_END_PATH || 954 hdr->type == EFI_DEV_END_PATH2) { 955 if (!uart) 956 return 0; 957 if (hdr->sub_type == EFI_DEV_END_ENTIRE) 958 return 1; 959 uart = 0; 960 } 961 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length); 962 } 963 printk(KERN_ERR "Malformed %s value\n", name); 964 return 0; 965 } 966 967 /* 968 * Look for the first granule aligned memory descriptor memory 969 * that is big enough to hold EFI memory map. Make sure this 970 * descriptor is atleast granule sized so it does not get trimmed 971 */ 972 struct kern_memdesc * 973 find_memmap_space (void) 974 { 975 u64 contig_low=0, contig_high=0; 976 u64 as = 0, ae; 977 void *efi_map_start, *efi_map_end, *p, *q; 978 efi_memory_desc_t *md, *pmd = NULL, *check_md; 979 u64 space_needed, efi_desc_size; 980 unsigned long total_mem = 0; 981 982 efi_map_start = __va(ia64_boot_param->efi_memmap); 983 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 984 efi_desc_size = ia64_boot_param->efi_memdesc_size; 985 986 /* 987 * Worst case: we need 3 kernel descriptors for each efi descriptor 988 * (if every entry has a WB part in the middle, and UC head and tail), 989 * plus one for the end marker. 990 */ 991 space_needed = sizeof(kern_memdesc_t) * 992 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1); 993 994 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { 995 md = p; 996 if (!efi_wb(md)) { 997 continue; 998 } 999 if (pmd == NULL || !efi_wb(pmd) || 1000 efi_md_end(pmd) != md->phys_addr) { 1001 contig_low = GRANULEROUNDUP(md->phys_addr); 1002 contig_high = efi_md_end(md); 1003 for (q = p + efi_desc_size; q < efi_map_end; 1004 q += efi_desc_size) { 1005 check_md = q; 1006 if (!efi_wb(check_md)) 1007 break; 1008 if (contig_high != check_md->phys_addr) 1009 break; 1010 contig_high = efi_md_end(check_md); 1011 } 1012 contig_high = GRANULEROUNDDOWN(contig_high); 1013 } 1014 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA) 1015 continue; 1016 1017 /* Round ends inward to granule boundaries */ 1018 as = max(contig_low, md->phys_addr); 1019 ae = min(contig_high, efi_md_end(md)); 1020 1021 /* keep within max_addr= and min_addr= command line arg */ 1022 as = max(as, min_addr); 1023 ae = min(ae, max_addr); 1024 if (ae <= as) 1025 continue; 1026 1027 /* avoid going over mem= command line arg */ 1028 if (total_mem + (ae - as) > mem_limit) 1029 ae -= total_mem + (ae - as) - mem_limit; 1030 1031 if (ae <= as) 1032 continue; 1033 1034 if (ae - as > space_needed) 1035 break; 1036 } 1037 if (p >= efi_map_end) 1038 panic("Can't allocate space for kernel memory descriptors"); 1039 1040 return __va(as); 1041 } 1042 1043 /* 1044 * Walk the EFI memory map and gather all memory available for kernel 1045 * to use. We can allocate partial granules only if the unavailable 1046 * parts exist, and are WB. 1047 */ 1048 unsigned long 1049 efi_memmap_init(u64 *s, u64 *e) 1050 { 1051 struct kern_memdesc *k, *prev = NULL; 1052 u64 contig_low=0, contig_high=0; 1053 u64 as, ae, lim; 1054 void *efi_map_start, *efi_map_end, *p, *q; 1055 efi_memory_desc_t *md, *pmd = NULL, *check_md; 1056 u64 efi_desc_size; 1057 unsigned long total_mem = 0; 1058 1059 k = kern_memmap = find_memmap_space(); 1060 1061 efi_map_start = __va(ia64_boot_param->efi_memmap); 1062 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 1063 efi_desc_size = ia64_boot_param->efi_memdesc_size; 1064 1065 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) { 1066 md = p; 1067 if (!efi_wb(md)) { 1068 if (efi_uc(md) && 1069 (md->type == EFI_CONVENTIONAL_MEMORY || 1070 md->type == EFI_BOOT_SERVICES_DATA)) { 1071 k->attribute = EFI_MEMORY_UC; 1072 k->start = md->phys_addr; 1073 k->num_pages = md->num_pages; 1074 k++; 1075 } 1076 continue; 1077 } 1078 if (pmd == NULL || !efi_wb(pmd) || 1079 efi_md_end(pmd) != md->phys_addr) { 1080 contig_low = GRANULEROUNDUP(md->phys_addr); 1081 contig_high = efi_md_end(md); 1082 for (q = p + efi_desc_size; q < efi_map_end; 1083 q += efi_desc_size) { 1084 check_md = q; 1085 if (!efi_wb(check_md)) 1086 break; 1087 if (contig_high != check_md->phys_addr) 1088 break; 1089 contig_high = efi_md_end(check_md); 1090 } 1091 contig_high = GRANULEROUNDDOWN(contig_high); 1092 } 1093 if (!is_memory_available(md)) 1094 continue; 1095 1096 /* 1097 * Round ends inward to granule boundaries 1098 * Give trimmings to uncached allocator 1099 */ 1100 if (md->phys_addr < contig_low) { 1101 lim = min(efi_md_end(md), contig_low); 1102 if (efi_uc(md)) { 1103 if (k > kern_memmap && 1104 (k-1)->attribute == EFI_MEMORY_UC && 1105 kmd_end(k-1) == md->phys_addr) { 1106 (k-1)->num_pages += 1107 (lim - md->phys_addr) 1108 >> EFI_PAGE_SHIFT; 1109 } else { 1110 k->attribute = EFI_MEMORY_UC; 1111 k->start = md->phys_addr; 1112 k->num_pages = (lim - md->phys_addr) 1113 >> EFI_PAGE_SHIFT; 1114 k++; 1115 } 1116 } 1117 as = contig_low; 1118 } else 1119 as = md->phys_addr; 1120 1121 if (efi_md_end(md) > contig_high) { 1122 lim = max(md->phys_addr, contig_high); 1123 if (efi_uc(md)) { 1124 if (lim == md->phys_addr && k > kern_memmap && 1125 (k-1)->attribute == EFI_MEMORY_UC && 1126 kmd_end(k-1) == md->phys_addr) { 1127 (k-1)->num_pages += md->num_pages; 1128 } else { 1129 k->attribute = EFI_MEMORY_UC; 1130 k->start = lim; 1131 k->num_pages = (efi_md_end(md) - lim) 1132 >> EFI_PAGE_SHIFT; 1133 k++; 1134 } 1135 } 1136 ae = contig_high; 1137 } else 1138 ae = efi_md_end(md); 1139 1140 /* keep within max_addr= and min_addr= command line arg */ 1141 as = max(as, min_addr); 1142 ae = min(ae, max_addr); 1143 if (ae <= as) 1144 continue; 1145 1146 /* avoid going over mem= command line arg */ 1147 if (total_mem + (ae - as) > mem_limit) 1148 ae -= total_mem + (ae - as) - mem_limit; 1149 1150 if (ae <= as) 1151 continue; 1152 if (prev && kmd_end(prev) == md->phys_addr) { 1153 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT; 1154 total_mem += ae - as; 1155 continue; 1156 } 1157 k->attribute = EFI_MEMORY_WB; 1158 k->start = as; 1159 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT; 1160 total_mem += ae - as; 1161 prev = k++; 1162 } 1163 k->start = ~0L; /* end-marker */ 1164 1165 /* reserve the memory we are using for kern_memmap */ 1166 *s = (u64)kern_memmap; 1167 *e = (u64)++k; 1168 1169 return total_mem; 1170 } 1171 1172 void 1173 efi_initialize_iomem_resources(struct resource *code_resource, 1174 struct resource *data_resource, 1175 struct resource *bss_resource) 1176 { 1177 struct resource *res; 1178 void *efi_map_start, *efi_map_end, *p; 1179 efi_memory_desc_t *md; 1180 u64 efi_desc_size; 1181 char *name; 1182 unsigned long flags; 1183 1184 efi_map_start = __va(ia64_boot_param->efi_memmap); 1185 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 1186 efi_desc_size = ia64_boot_param->efi_memdesc_size; 1187 1188 res = NULL; 1189 1190 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 1191 md = p; 1192 1193 if (md->num_pages == 0) /* should not happen */ 1194 continue; 1195 1196 flags = IORESOURCE_MEM | IORESOURCE_BUSY; 1197 switch (md->type) { 1198 1199 case EFI_MEMORY_MAPPED_IO: 1200 case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 1201 continue; 1202 1203 case EFI_LOADER_CODE: 1204 case EFI_LOADER_DATA: 1205 case EFI_BOOT_SERVICES_DATA: 1206 case EFI_BOOT_SERVICES_CODE: 1207 case EFI_CONVENTIONAL_MEMORY: 1208 if (md->attribute & EFI_MEMORY_WP) { 1209 name = "System ROM"; 1210 flags |= IORESOURCE_READONLY; 1211 } else if (md->attribute == EFI_MEMORY_UC) 1212 name = "Uncached RAM"; 1213 else 1214 name = "System RAM"; 1215 break; 1216 1217 case EFI_ACPI_MEMORY_NVS: 1218 name = "ACPI Non-volatile Storage"; 1219 break; 1220 1221 case EFI_UNUSABLE_MEMORY: 1222 name = "reserved"; 1223 flags |= IORESOURCE_DISABLED; 1224 break; 1225 1226 case EFI_RESERVED_TYPE: 1227 case EFI_RUNTIME_SERVICES_CODE: 1228 case EFI_RUNTIME_SERVICES_DATA: 1229 case EFI_ACPI_RECLAIM_MEMORY: 1230 default: 1231 name = "reserved"; 1232 break; 1233 } 1234 1235 if ((res = kzalloc(sizeof(struct resource), 1236 GFP_KERNEL)) == NULL) { 1237 printk(KERN_ERR 1238 "failed to allocate resource for iomem\n"); 1239 return; 1240 } 1241 1242 res->name = name; 1243 res->start = md->phys_addr; 1244 res->end = md->phys_addr + efi_md_size(md) - 1; 1245 res->flags = flags; 1246 1247 if (insert_resource(&iomem_resource, res) < 0) 1248 kfree(res); 1249 else { 1250 /* 1251 * We don't know which region contains 1252 * kernel data so we try it repeatedly and 1253 * let the resource manager test it. 1254 */ 1255 insert_resource(res, code_resource); 1256 insert_resource(res, data_resource); 1257 insert_resource(res, bss_resource); 1258 #ifdef CONFIG_KEXEC 1259 insert_resource(res, &efi_memmap_res); 1260 insert_resource(res, &boot_param_res); 1261 if (crashk_res.end > crashk_res.start) 1262 insert_resource(res, &crashk_res); 1263 #endif 1264 } 1265 } 1266 } 1267 1268 #ifdef CONFIG_KEXEC 1269 /* find a block of memory aligned to 64M exclude reserved regions 1270 rsvd_regions are sorted 1271 */ 1272 unsigned long __init 1273 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n) 1274 { 1275 int i; 1276 u64 start, end; 1277 u64 alignment = 1UL << _PAGE_SIZE_64M; 1278 void *efi_map_start, *efi_map_end, *p; 1279 efi_memory_desc_t *md; 1280 u64 efi_desc_size; 1281 1282 efi_map_start = __va(ia64_boot_param->efi_memmap); 1283 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 1284 efi_desc_size = ia64_boot_param->efi_memdesc_size; 1285 1286 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 1287 md = p; 1288 if (!efi_wb(md)) 1289 continue; 1290 start = ALIGN(md->phys_addr, alignment); 1291 end = efi_md_end(md); 1292 for (i = 0; i < n; i++) { 1293 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) { 1294 if (__pa(r[i].start) > start + size) 1295 return start; 1296 start = ALIGN(__pa(r[i].end), alignment); 1297 if (i < n-1 && 1298 __pa(r[i+1].start) < start + size) 1299 continue; 1300 else 1301 break; 1302 } 1303 } 1304 if (end > start + size) 1305 return start; 1306 } 1307 1308 printk(KERN_WARNING 1309 "Cannot reserve 0x%lx byte of memory for crashdump\n", size); 1310 return ~0UL; 1311 } 1312 #endif 1313 1314 #ifdef CONFIG_CRASH_DUMP 1315 /* locate the size find a the descriptor at a certain address */ 1316 unsigned long __init 1317 vmcore_find_descriptor_size (unsigned long address) 1318 { 1319 void *efi_map_start, *efi_map_end, *p; 1320 efi_memory_desc_t *md; 1321 u64 efi_desc_size; 1322 unsigned long ret = 0; 1323 1324 efi_map_start = __va(ia64_boot_param->efi_memmap); 1325 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size; 1326 efi_desc_size = ia64_boot_param->efi_memdesc_size; 1327 1328 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) { 1329 md = p; 1330 if (efi_wb(md) && md->type == EFI_LOADER_DATA 1331 && md->phys_addr == address) { 1332 ret = efi_md_size(md); 1333 break; 1334 } 1335 } 1336 1337 if (ret == 0) 1338 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n"); 1339 1340 return ret; 1341 } 1342 #endif 1343