1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PowerPC64 port by Mike Corrigan and Dave Engebretsen 4 * {mikejc|engebret}@us.ibm.com 5 * 6 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com> 7 * 8 * SMP scalability work: 9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM 10 * 11 * Module name: htab.c 12 * 13 * Description: 14 * PowerPC Hashed Page Table functions 15 */ 16 17 #undef DEBUG 18 #undef DEBUG_LOW 19 20 #define pr_fmt(fmt) "hash-mmu: " fmt 21 #include <linux/spinlock.h> 22 #include <linux/errno.h> 23 #include <linux/sched/mm.h> 24 #include <linux/proc_fs.h> 25 #include <linux/stat.h> 26 #include <linux/sysctl.h> 27 #include <linux/export.h> 28 #include <linux/ctype.h> 29 #include <linux/cache.h> 30 #include <linux/init.h> 31 #include <linux/signal.h> 32 #include <linux/memblock.h> 33 #include <linux/context_tracking.h> 34 #include <linux/libfdt.h> 35 #include <linux/pkeys.h> 36 #include <linux/hugetlb.h> 37 #include <linux/cpu.h> 38 #include <linux/pgtable.h> 39 40 #include <asm/debugfs.h> 41 #include <asm/interrupt.h> 42 #include <asm/processor.h> 43 #include <asm/mmu.h> 44 #include <asm/mmu_context.h> 45 #include <asm/page.h> 46 #include <asm/types.h> 47 #include <linux/uaccess.h> 48 #include <asm/machdep.h> 49 #include <asm/prom.h> 50 #include <asm/io.h> 51 #include <asm/eeh.h> 52 #include <asm/tlb.h> 53 #include <asm/cacheflush.h> 54 #include <asm/cputable.h> 55 #include <asm/sections.h> 56 #include <asm/copro.h> 57 #include <asm/udbg.h> 58 #include <asm/code-patching.h> 59 #include <asm/fadump.h> 60 #include <asm/firmware.h> 61 #include <asm/tm.h> 62 #include <asm/trace.h> 63 #include <asm/ps3.h> 64 #include <asm/pte-walk.h> 65 #include <asm/asm-prototypes.h> 66 #include <asm/ultravisor.h> 67 68 #include <mm/mmu_decl.h> 69 70 #include "internal.h" 71 72 73 #ifdef DEBUG 74 #define DBG(fmt...) udbg_printf(fmt) 75 #else 76 #define DBG(fmt...) 77 #endif 78 79 #ifdef DEBUG_LOW 80 #define DBG_LOW(fmt...) udbg_printf(fmt) 81 #else 82 #define DBG_LOW(fmt...) 83 #endif 84 85 #define KB (1024) 86 #define MB (1024*KB) 87 #define GB (1024L*MB) 88 89 /* 90 * Note: pte --> Linux PTE 91 * HPTE --> PowerPC Hashed Page Table Entry 92 * 93 * Execution context: 94 * htab_initialize is called with the MMU off (of course), but 95 * the kernel has been copied down to zero so it can directly 96 * reference global data. At this point it is very difficult 97 * to print debug info. 98 * 99 */ 100 101 static unsigned long _SDR1; 102 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT]; 103 EXPORT_SYMBOL_GPL(mmu_psize_defs); 104 105 u8 hpte_page_sizes[1 << LP_BITS]; 106 EXPORT_SYMBOL_GPL(hpte_page_sizes); 107 108 struct hash_pte *htab_address; 109 unsigned long htab_size_bytes; 110 unsigned long htab_hash_mask; 111 EXPORT_SYMBOL_GPL(htab_hash_mask); 112 int mmu_linear_psize = MMU_PAGE_4K; 113 EXPORT_SYMBOL_GPL(mmu_linear_psize); 114 int mmu_virtual_psize = MMU_PAGE_4K; 115 int mmu_vmalloc_psize = MMU_PAGE_4K; 116 EXPORT_SYMBOL_GPL(mmu_vmalloc_psize); 117 #ifdef CONFIG_SPARSEMEM_VMEMMAP 118 int mmu_vmemmap_psize = MMU_PAGE_4K; 119 #endif 120 int mmu_io_psize = MMU_PAGE_4K; 121 int mmu_kernel_ssize = MMU_SEGSIZE_256M; 122 EXPORT_SYMBOL_GPL(mmu_kernel_ssize); 123 int mmu_highuser_ssize = MMU_SEGSIZE_256M; 124 u16 mmu_slb_size = 64; 125 EXPORT_SYMBOL_GPL(mmu_slb_size); 126 #ifdef CONFIG_PPC_64K_PAGES 127 int mmu_ci_restrictions; 128 #endif 129 #ifdef CONFIG_DEBUG_PAGEALLOC 130 static u8 *linear_map_hash_slots; 131 static unsigned long linear_map_hash_count; 132 static DEFINE_SPINLOCK(linear_map_hash_lock); 133 #endif /* CONFIG_DEBUG_PAGEALLOC */ 134 struct mmu_hash_ops mmu_hash_ops; 135 EXPORT_SYMBOL(mmu_hash_ops); 136 137 /* 138 * These are definitions of page sizes arrays to be used when none 139 * is provided by the firmware. 140 */ 141 142 /* 143 * Fallback (4k pages only) 144 */ 145 static struct mmu_psize_def mmu_psize_defaults[] = { 146 [MMU_PAGE_4K] = { 147 .shift = 12, 148 .sllp = 0, 149 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1}, 150 .avpnm = 0, 151 .tlbiel = 0, 152 }, 153 }; 154 155 /* 156 * POWER4, GPUL, POWER5 157 * 158 * Support for 16Mb large pages 159 */ 160 static struct mmu_psize_def mmu_psize_defaults_gp[] = { 161 [MMU_PAGE_4K] = { 162 .shift = 12, 163 .sllp = 0, 164 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1}, 165 .avpnm = 0, 166 .tlbiel = 1, 167 }, 168 [MMU_PAGE_16M] = { 169 .shift = 24, 170 .sllp = SLB_VSID_L, 171 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0, 172 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 }, 173 .avpnm = 0x1UL, 174 .tlbiel = 0, 175 }, 176 }; 177 178 /* 179 * 'R' and 'C' update notes: 180 * - Under pHyp or KVM, the updatepp path will not set C, thus it *will* 181 * create writeable HPTEs without C set, because the hcall H_PROTECT 182 * that we use in that case will not update C 183 * - The above is however not a problem, because we also don't do that 184 * fancy "no flush" variant of eviction and we use H_REMOVE which will 185 * do the right thing and thus we don't have the race I described earlier 186 * 187 * - Under bare metal, we do have the race, so we need R and C set 188 * - We make sure R is always set and never lost 189 * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping 190 */ 191 unsigned long htab_convert_pte_flags(unsigned long pteflags, unsigned long flags) 192 { 193 unsigned long rflags = 0; 194 195 /* _PAGE_EXEC -> NOEXEC */ 196 if ((pteflags & _PAGE_EXEC) == 0) 197 rflags |= HPTE_R_N; 198 /* 199 * PPP bits: 200 * Linux uses slb key 0 for kernel and 1 for user. 201 * kernel RW areas are mapped with PPP=0b000 202 * User area is mapped with PPP=0b010 for read/write 203 * or PPP=0b011 for read-only (including writeable but clean pages). 204 */ 205 if (pteflags & _PAGE_PRIVILEGED) { 206 /* 207 * Kernel read only mapped with ppp bits 0b110 208 */ 209 if (!(pteflags & _PAGE_WRITE)) { 210 if (mmu_has_feature(MMU_FTR_KERNEL_RO)) 211 rflags |= (HPTE_R_PP0 | 0x2); 212 else 213 rflags |= 0x3; 214 } 215 } else { 216 if (pteflags & _PAGE_RWX) 217 rflags |= 0x2; 218 if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY))) 219 rflags |= 0x1; 220 } 221 /* 222 * We can't allow hardware to update hpte bits. Hence always 223 * set 'R' bit and set 'C' if it is a write fault 224 */ 225 rflags |= HPTE_R_R; 226 227 if (pteflags & _PAGE_DIRTY) 228 rflags |= HPTE_R_C; 229 /* 230 * Add in WIG bits 231 */ 232 233 if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT) 234 rflags |= HPTE_R_I; 235 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_NON_IDEMPOTENT) 236 rflags |= (HPTE_R_I | HPTE_R_G); 237 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO) 238 rflags |= (HPTE_R_W | HPTE_R_I | HPTE_R_M); 239 else 240 /* 241 * Add memory coherence if cache inhibited is not set 242 */ 243 rflags |= HPTE_R_M; 244 245 rflags |= pte_to_hpte_pkey_bits(pteflags, flags); 246 return rflags; 247 } 248 249 int htab_bolt_mapping(unsigned long vstart, unsigned long vend, 250 unsigned long pstart, unsigned long prot, 251 int psize, int ssize) 252 { 253 unsigned long vaddr, paddr; 254 unsigned int step, shift; 255 int ret = 0; 256 257 shift = mmu_psize_defs[psize].shift; 258 step = 1 << shift; 259 260 prot = htab_convert_pte_flags(prot, HPTE_USE_KERNEL_KEY); 261 262 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n", 263 vstart, vend, pstart, prot, psize, ssize); 264 265 /* Carefully map only the possible range */ 266 vaddr = ALIGN(vstart, step); 267 paddr = ALIGN(pstart, step); 268 vend = ALIGN_DOWN(vend, step); 269 270 for (; vaddr < vend; vaddr += step, paddr += step) { 271 unsigned long hash, hpteg; 272 unsigned long vsid = get_kernel_vsid(vaddr, ssize); 273 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize); 274 unsigned long tprot = prot; 275 bool secondary_hash = false; 276 277 /* 278 * If we hit a bad address return error. 279 */ 280 if (!vsid) 281 return -1; 282 /* Make kernel text executable */ 283 if (overlaps_kernel_text(vaddr, vaddr + step)) 284 tprot &= ~HPTE_R_N; 285 286 /* 287 * If relocatable, check if it overlaps interrupt vectors that 288 * are copied down to real 0. For relocatable kernel 289 * (e.g. kdump case) we copy interrupt vectors down to real 290 * address 0. Mark that region as executable. This is 291 * because on p8 system with relocation on exception feature 292 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence 293 * in order to execute the interrupt handlers in virtual 294 * mode the vector region need to be marked as executable. 295 */ 296 if ((PHYSICAL_START > MEMORY_START) && 297 overlaps_interrupt_vector_text(vaddr, vaddr + step)) 298 tprot &= ~HPTE_R_N; 299 300 hash = hpt_hash(vpn, shift, ssize); 301 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); 302 303 BUG_ON(!mmu_hash_ops.hpte_insert); 304 repeat: 305 ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot, 306 HPTE_V_BOLTED, psize, psize, 307 ssize); 308 if (ret == -1) { 309 /* 310 * Try to to keep bolted entries in primary. 311 * Remove non bolted entries and try insert again 312 */ 313 ret = mmu_hash_ops.hpte_remove(hpteg); 314 if (ret != -1) 315 ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot, 316 HPTE_V_BOLTED, psize, psize, 317 ssize); 318 if (ret == -1 && !secondary_hash) { 319 secondary_hash = true; 320 hpteg = ((~hash & htab_hash_mask) * HPTES_PER_GROUP); 321 goto repeat; 322 } 323 } 324 325 if (ret < 0) 326 break; 327 328 cond_resched(); 329 #ifdef CONFIG_DEBUG_PAGEALLOC 330 if (debug_pagealloc_enabled() && 331 (paddr >> PAGE_SHIFT) < linear_map_hash_count) 332 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80; 333 #endif /* CONFIG_DEBUG_PAGEALLOC */ 334 } 335 return ret < 0 ? ret : 0; 336 } 337 338 int htab_remove_mapping(unsigned long vstart, unsigned long vend, 339 int psize, int ssize) 340 { 341 unsigned long vaddr; 342 unsigned int step, shift; 343 int rc; 344 int ret = 0; 345 346 shift = mmu_psize_defs[psize].shift; 347 step = 1 << shift; 348 349 if (!mmu_hash_ops.hpte_removebolted) 350 return -ENODEV; 351 352 /* Unmap the full range specificied */ 353 vaddr = ALIGN_DOWN(vstart, step); 354 for (;vaddr < vend; vaddr += step) { 355 rc = mmu_hash_ops.hpte_removebolted(vaddr, psize, ssize); 356 if (rc == -ENOENT) { 357 ret = -ENOENT; 358 continue; 359 } 360 if (rc < 0) 361 return rc; 362 } 363 364 return ret; 365 } 366 367 static bool disable_1tb_segments = false; 368 369 static int __init parse_disable_1tb_segments(char *p) 370 { 371 disable_1tb_segments = true; 372 return 0; 373 } 374 early_param("disable_1tb_segments", parse_disable_1tb_segments); 375 376 static int __init htab_dt_scan_seg_sizes(unsigned long node, 377 const char *uname, int depth, 378 void *data) 379 { 380 const char *type = of_get_flat_dt_prop(node, "device_type", NULL); 381 const __be32 *prop; 382 int size = 0; 383 384 /* We are scanning "cpu" nodes only */ 385 if (type == NULL || strcmp(type, "cpu") != 0) 386 return 0; 387 388 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size); 389 if (prop == NULL) 390 return 0; 391 for (; size >= 4; size -= 4, ++prop) { 392 if (be32_to_cpu(prop[0]) == 40) { 393 DBG("1T segment support detected\n"); 394 395 if (disable_1tb_segments) { 396 DBG("1T segments disabled by command line\n"); 397 break; 398 } 399 400 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT; 401 return 1; 402 } 403 } 404 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B; 405 return 0; 406 } 407 408 static int __init get_idx_from_shift(unsigned int shift) 409 { 410 int idx = -1; 411 412 switch (shift) { 413 case 0xc: 414 idx = MMU_PAGE_4K; 415 break; 416 case 0x10: 417 idx = MMU_PAGE_64K; 418 break; 419 case 0x14: 420 idx = MMU_PAGE_1M; 421 break; 422 case 0x18: 423 idx = MMU_PAGE_16M; 424 break; 425 case 0x22: 426 idx = MMU_PAGE_16G; 427 break; 428 } 429 return idx; 430 } 431 432 static int __init htab_dt_scan_page_sizes(unsigned long node, 433 const char *uname, int depth, 434 void *data) 435 { 436 const char *type = of_get_flat_dt_prop(node, "device_type", NULL); 437 const __be32 *prop; 438 int size = 0; 439 440 /* We are scanning "cpu" nodes only */ 441 if (type == NULL || strcmp(type, "cpu") != 0) 442 return 0; 443 444 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size); 445 if (!prop) 446 return 0; 447 448 pr_info("Page sizes from device-tree:\n"); 449 size /= 4; 450 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE); 451 while(size > 0) { 452 unsigned int base_shift = be32_to_cpu(prop[0]); 453 unsigned int slbenc = be32_to_cpu(prop[1]); 454 unsigned int lpnum = be32_to_cpu(prop[2]); 455 struct mmu_psize_def *def; 456 int idx, base_idx; 457 458 size -= 3; prop += 3; 459 base_idx = get_idx_from_shift(base_shift); 460 if (base_idx < 0) { 461 /* skip the pte encoding also */ 462 prop += lpnum * 2; size -= lpnum * 2; 463 continue; 464 } 465 def = &mmu_psize_defs[base_idx]; 466 if (base_idx == MMU_PAGE_16M) 467 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE; 468 469 def->shift = base_shift; 470 if (base_shift <= 23) 471 def->avpnm = 0; 472 else 473 def->avpnm = (1 << (base_shift - 23)) - 1; 474 def->sllp = slbenc; 475 /* 476 * We don't know for sure what's up with tlbiel, so 477 * for now we only set it for 4K and 64K pages 478 */ 479 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K) 480 def->tlbiel = 1; 481 else 482 def->tlbiel = 0; 483 484 while (size > 0 && lpnum) { 485 unsigned int shift = be32_to_cpu(prop[0]); 486 int penc = be32_to_cpu(prop[1]); 487 488 prop += 2; size -= 2; 489 lpnum--; 490 491 idx = get_idx_from_shift(shift); 492 if (idx < 0) 493 continue; 494 495 if (penc == -1) 496 pr_err("Invalid penc for base_shift=%d " 497 "shift=%d\n", base_shift, shift); 498 499 def->penc[idx] = penc; 500 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx," 501 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n", 502 base_shift, shift, def->sllp, 503 def->avpnm, def->tlbiel, def->penc[idx]); 504 } 505 } 506 507 return 1; 508 } 509 510 #ifdef CONFIG_HUGETLB_PAGE 511 /* 512 * Scan for 16G memory blocks that have been set aside for huge pages 513 * and reserve those blocks for 16G huge pages. 514 */ 515 static int __init htab_dt_scan_hugepage_blocks(unsigned long node, 516 const char *uname, int depth, 517 void *data) { 518 const char *type = of_get_flat_dt_prop(node, "device_type", NULL); 519 const __be64 *addr_prop; 520 const __be32 *page_count_prop; 521 unsigned int expected_pages; 522 long unsigned int phys_addr; 523 long unsigned int block_size; 524 525 /* We are scanning "memory" nodes only */ 526 if (type == NULL || strcmp(type, "memory") != 0) 527 return 0; 528 529 /* 530 * This property is the log base 2 of the number of virtual pages that 531 * will represent this memory block. 532 */ 533 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL); 534 if (page_count_prop == NULL) 535 return 0; 536 expected_pages = (1 << be32_to_cpu(page_count_prop[0])); 537 addr_prop = of_get_flat_dt_prop(node, "reg", NULL); 538 if (addr_prop == NULL) 539 return 0; 540 phys_addr = be64_to_cpu(addr_prop[0]); 541 block_size = be64_to_cpu(addr_prop[1]); 542 if (block_size != (16 * GB)) 543 return 0; 544 printk(KERN_INFO "Huge page(16GB) memory: " 545 "addr = 0x%lX size = 0x%lX pages = %d\n", 546 phys_addr, block_size, expected_pages); 547 if (phys_addr + block_size * expected_pages <= memblock_end_of_DRAM()) { 548 memblock_reserve(phys_addr, block_size * expected_pages); 549 pseries_add_gpage(phys_addr, block_size, expected_pages); 550 } 551 return 0; 552 } 553 #endif /* CONFIG_HUGETLB_PAGE */ 554 555 static void mmu_psize_set_default_penc(void) 556 { 557 int bpsize, apsize; 558 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) 559 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++) 560 mmu_psize_defs[bpsize].penc[apsize] = -1; 561 } 562 563 #ifdef CONFIG_PPC_64K_PAGES 564 565 static bool might_have_hea(void) 566 { 567 /* 568 * The HEA ethernet adapter requires awareness of the 569 * GX bus. Without that awareness we can easily assume 570 * we will never see an HEA ethernet device. 571 */ 572 #ifdef CONFIG_IBMEBUS 573 return !cpu_has_feature(CPU_FTR_ARCH_207S) && 574 firmware_has_feature(FW_FEATURE_SPLPAR); 575 #else 576 return false; 577 #endif 578 } 579 580 #endif /* #ifdef CONFIG_PPC_64K_PAGES */ 581 582 static void __init htab_scan_page_sizes(void) 583 { 584 int rc; 585 586 /* se the invalid penc to -1 */ 587 mmu_psize_set_default_penc(); 588 589 /* Default to 4K pages only */ 590 memcpy(mmu_psize_defs, mmu_psize_defaults, 591 sizeof(mmu_psize_defaults)); 592 593 /* 594 * Try to find the available page sizes in the device-tree 595 */ 596 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL); 597 if (rc == 0 && early_mmu_has_feature(MMU_FTR_16M_PAGE)) { 598 /* 599 * Nothing in the device-tree, but the CPU supports 16M pages, 600 * so let's fallback on a known size list for 16M capable CPUs. 601 */ 602 memcpy(mmu_psize_defs, mmu_psize_defaults_gp, 603 sizeof(mmu_psize_defaults_gp)); 604 } 605 606 #ifdef CONFIG_HUGETLB_PAGE 607 if (!hugetlb_disabled && !early_radix_enabled() ) { 608 /* Reserve 16G huge page memory sections for huge pages */ 609 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL); 610 } 611 #endif /* CONFIG_HUGETLB_PAGE */ 612 } 613 614 /* 615 * Fill in the hpte_page_sizes[] array. 616 * We go through the mmu_psize_defs[] array looking for all the 617 * supported base/actual page size combinations. Each combination 618 * has a unique pagesize encoding (penc) value in the low bits of 619 * the LP field of the HPTE. For actual page sizes less than 1MB, 620 * some of the upper LP bits are used for RPN bits, meaning that 621 * we need to fill in several entries in hpte_page_sizes[]. 622 * 623 * In diagrammatic form, with r = RPN bits and z = page size bits: 624 * PTE LP actual page size 625 * rrrr rrrz >=8KB 626 * rrrr rrzz >=16KB 627 * rrrr rzzz >=32KB 628 * rrrr zzzz >=64KB 629 * ... 630 * 631 * The zzzz bits are implementation-specific but are chosen so that 632 * no encoding for a larger page size uses the same value in its 633 * low-order N bits as the encoding for the 2^(12+N) byte page size 634 * (if it exists). 635 */ 636 static void init_hpte_page_sizes(void) 637 { 638 long int ap, bp; 639 long int shift, penc; 640 641 for (bp = 0; bp < MMU_PAGE_COUNT; ++bp) { 642 if (!mmu_psize_defs[bp].shift) 643 continue; /* not a supported page size */ 644 for (ap = bp; ap < MMU_PAGE_COUNT; ++ap) { 645 penc = mmu_psize_defs[bp].penc[ap]; 646 if (penc == -1 || !mmu_psize_defs[ap].shift) 647 continue; 648 shift = mmu_psize_defs[ap].shift - LP_SHIFT; 649 if (shift <= 0) 650 continue; /* should never happen */ 651 /* 652 * For page sizes less than 1MB, this loop 653 * replicates the entry for all possible values 654 * of the rrrr bits. 655 */ 656 while (penc < (1 << LP_BITS)) { 657 hpte_page_sizes[penc] = (ap << 4) | bp; 658 penc += 1 << shift; 659 } 660 } 661 } 662 } 663 664 static void __init htab_init_page_sizes(void) 665 { 666 bool aligned = true; 667 init_hpte_page_sizes(); 668 669 if (!debug_pagealloc_enabled()) { 670 /* 671 * Pick a size for the linear mapping. Currently, we only 672 * support 16M, 1M and 4K which is the default 673 */ 674 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && 675 (unsigned long)_stext % 0x1000000) { 676 if (mmu_psize_defs[MMU_PAGE_16M].shift) 677 pr_warn("Kernel not 16M aligned, disabling 16M linear map alignment\n"); 678 aligned = false; 679 } 680 681 if (mmu_psize_defs[MMU_PAGE_16M].shift && aligned) 682 mmu_linear_psize = MMU_PAGE_16M; 683 else if (mmu_psize_defs[MMU_PAGE_1M].shift) 684 mmu_linear_psize = MMU_PAGE_1M; 685 } 686 687 #ifdef CONFIG_PPC_64K_PAGES 688 /* 689 * Pick a size for the ordinary pages. Default is 4K, we support 690 * 64K for user mappings and vmalloc if supported by the processor. 691 * We only use 64k for ioremap if the processor 692 * (and firmware) support cache-inhibited large pages. 693 * If not, we use 4k and set mmu_ci_restrictions so that 694 * hash_page knows to switch processes that use cache-inhibited 695 * mappings to 4k pages. 696 */ 697 if (mmu_psize_defs[MMU_PAGE_64K].shift) { 698 mmu_virtual_psize = MMU_PAGE_64K; 699 mmu_vmalloc_psize = MMU_PAGE_64K; 700 if (mmu_linear_psize == MMU_PAGE_4K) 701 mmu_linear_psize = MMU_PAGE_64K; 702 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) { 703 /* 704 * When running on pSeries using 64k pages for ioremap 705 * would stop us accessing the HEA ethernet. So if we 706 * have the chance of ever seeing one, stay at 4k. 707 */ 708 if (!might_have_hea()) 709 mmu_io_psize = MMU_PAGE_64K; 710 } else 711 mmu_ci_restrictions = 1; 712 } 713 #endif /* CONFIG_PPC_64K_PAGES */ 714 715 #ifdef CONFIG_SPARSEMEM_VMEMMAP 716 /* 717 * We try to use 16M pages for vmemmap if that is supported 718 * and we have at least 1G of RAM at boot 719 */ 720 if (mmu_psize_defs[MMU_PAGE_16M].shift && 721 memblock_phys_mem_size() >= 0x40000000) 722 mmu_vmemmap_psize = MMU_PAGE_16M; 723 else 724 mmu_vmemmap_psize = mmu_virtual_psize; 725 #endif /* CONFIG_SPARSEMEM_VMEMMAP */ 726 727 printk(KERN_DEBUG "Page orders: linear mapping = %d, " 728 "virtual = %d, io = %d" 729 #ifdef CONFIG_SPARSEMEM_VMEMMAP 730 ", vmemmap = %d" 731 #endif 732 "\n", 733 mmu_psize_defs[mmu_linear_psize].shift, 734 mmu_psize_defs[mmu_virtual_psize].shift, 735 mmu_psize_defs[mmu_io_psize].shift 736 #ifdef CONFIG_SPARSEMEM_VMEMMAP 737 ,mmu_psize_defs[mmu_vmemmap_psize].shift 738 #endif 739 ); 740 } 741 742 static int __init htab_dt_scan_pftsize(unsigned long node, 743 const char *uname, int depth, 744 void *data) 745 { 746 const char *type = of_get_flat_dt_prop(node, "device_type", NULL); 747 const __be32 *prop; 748 749 /* We are scanning "cpu" nodes only */ 750 if (type == NULL || strcmp(type, "cpu") != 0) 751 return 0; 752 753 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL); 754 if (prop != NULL) { 755 /* pft_size[0] is the NUMA CEC cookie */ 756 ppc64_pft_size = be32_to_cpu(prop[1]); 757 return 1; 758 } 759 return 0; 760 } 761 762 unsigned htab_shift_for_mem_size(unsigned long mem_size) 763 { 764 unsigned memshift = __ilog2(mem_size); 765 unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift; 766 unsigned pteg_shift; 767 768 /* round mem_size up to next power of 2 */ 769 if ((1UL << memshift) < mem_size) 770 memshift += 1; 771 772 /* aim for 2 pages / pteg */ 773 pteg_shift = memshift - (pshift + 1); 774 775 /* 776 * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab 777 * size permitted by the architecture. 778 */ 779 return max(pteg_shift + 7, 18U); 780 } 781 782 static unsigned long __init htab_get_table_size(void) 783 { 784 /* 785 * If hash size isn't already provided by the platform, we try to 786 * retrieve it from the device-tree. If it's not there neither, we 787 * calculate it now based on the total RAM size 788 */ 789 if (ppc64_pft_size == 0) 790 of_scan_flat_dt(htab_dt_scan_pftsize, NULL); 791 if (ppc64_pft_size) 792 return 1UL << ppc64_pft_size; 793 794 return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size()); 795 } 796 797 #ifdef CONFIG_MEMORY_HOTPLUG 798 static int resize_hpt_for_hotplug(unsigned long new_mem_size) 799 { 800 unsigned target_hpt_shift; 801 802 if (!mmu_hash_ops.resize_hpt) 803 return 0; 804 805 target_hpt_shift = htab_shift_for_mem_size(new_mem_size); 806 807 /* 808 * To avoid lots of HPT resizes if memory size is fluctuating 809 * across a boundary, we deliberately have some hysterisis 810 * here: we immediately increase the HPT size if the target 811 * shift exceeds the current shift, but we won't attempt to 812 * reduce unless the target shift is at least 2 below the 813 * current shift 814 */ 815 if (target_hpt_shift > ppc64_pft_size || 816 target_hpt_shift < ppc64_pft_size - 1) 817 return mmu_hash_ops.resize_hpt(target_hpt_shift); 818 819 return 0; 820 } 821 822 int hash__create_section_mapping(unsigned long start, unsigned long end, 823 int nid, pgprot_t prot) 824 { 825 int rc; 826 827 if (end >= H_VMALLOC_START) { 828 pr_warn("Outside the supported range\n"); 829 return -1; 830 } 831 832 resize_hpt_for_hotplug(memblock_phys_mem_size()); 833 834 rc = htab_bolt_mapping(start, end, __pa(start), 835 pgprot_val(prot), mmu_linear_psize, 836 mmu_kernel_ssize); 837 838 if (rc < 0) { 839 int rc2 = htab_remove_mapping(start, end, mmu_linear_psize, 840 mmu_kernel_ssize); 841 BUG_ON(rc2 && (rc2 != -ENOENT)); 842 } 843 return rc; 844 } 845 846 int hash__remove_section_mapping(unsigned long start, unsigned long end) 847 { 848 int rc = htab_remove_mapping(start, end, mmu_linear_psize, 849 mmu_kernel_ssize); 850 851 if (resize_hpt_for_hotplug(memblock_phys_mem_size()) == -ENOSPC) 852 pr_warn("Hash collision while resizing HPT\n"); 853 854 return rc; 855 } 856 #endif /* CONFIG_MEMORY_HOTPLUG */ 857 858 static void __init hash_init_partition_table(phys_addr_t hash_table, 859 unsigned long htab_size) 860 { 861 mmu_partition_table_init(); 862 863 /* 864 * PS field (VRMA page size) is not used for LPID 0, hence set to 0. 865 * For now, UPRT is 0 and we have no segment table. 866 */ 867 htab_size = __ilog2(htab_size) - 18; 868 mmu_partition_table_set_entry(0, hash_table | htab_size, 0, false); 869 pr_info("Partition table %p\n", partition_tb); 870 } 871 872 static void __init htab_initialize(void) 873 { 874 unsigned long table; 875 unsigned long pteg_count; 876 unsigned long prot; 877 phys_addr_t base = 0, size = 0, end; 878 u64 i; 879 880 DBG(" -> htab_initialize()\n"); 881 882 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) { 883 mmu_kernel_ssize = MMU_SEGSIZE_1T; 884 mmu_highuser_ssize = MMU_SEGSIZE_1T; 885 printk(KERN_INFO "Using 1TB segments\n"); 886 } 887 888 if (stress_slb_enabled) 889 static_branch_enable(&stress_slb_key); 890 891 /* 892 * Calculate the required size of the htab. We want the number of 893 * PTEGs to equal one half the number of real pages. 894 */ 895 htab_size_bytes = htab_get_table_size(); 896 pteg_count = htab_size_bytes >> 7; 897 898 htab_hash_mask = pteg_count - 1; 899 900 if (firmware_has_feature(FW_FEATURE_LPAR) || 901 firmware_has_feature(FW_FEATURE_PS3_LV1)) { 902 /* Using a hypervisor which owns the htab */ 903 htab_address = NULL; 904 _SDR1 = 0; 905 #ifdef CONFIG_FA_DUMP 906 /* 907 * If firmware assisted dump is active firmware preserves 908 * the contents of htab along with entire partition memory. 909 * Clear the htab if firmware assisted dump is active so 910 * that we dont end up using old mappings. 911 */ 912 if (is_fadump_active() && mmu_hash_ops.hpte_clear_all) 913 mmu_hash_ops.hpte_clear_all(); 914 #endif 915 } else { 916 unsigned long limit = MEMBLOCK_ALLOC_ANYWHERE; 917 918 #ifdef CONFIG_PPC_CELL 919 /* 920 * Cell may require the hash table down low when using the 921 * Axon IOMMU in order to fit the dynamic region over it, see 922 * comments in cell/iommu.c 923 */ 924 if (fdt_subnode_offset(initial_boot_params, 0, "axon") > 0) { 925 limit = 0x80000000; 926 pr_info("Hash table forced below 2G for Axon IOMMU\n"); 927 } 928 #endif /* CONFIG_PPC_CELL */ 929 930 table = memblock_phys_alloc_range(htab_size_bytes, 931 htab_size_bytes, 932 0, limit); 933 if (!table) 934 panic("ERROR: Failed to allocate %pa bytes below %pa\n", 935 &htab_size_bytes, &limit); 936 937 DBG("Hash table allocated at %lx, size: %lx\n", table, 938 htab_size_bytes); 939 940 htab_address = __va(table); 941 942 /* htab absolute addr + encoded htabsize */ 943 _SDR1 = table + __ilog2(htab_size_bytes) - 18; 944 945 /* Initialize the HPT with no entries */ 946 memset((void *)table, 0, htab_size_bytes); 947 948 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 949 /* Set SDR1 */ 950 mtspr(SPRN_SDR1, _SDR1); 951 else 952 hash_init_partition_table(table, htab_size_bytes); 953 } 954 955 prot = pgprot_val(PAGE_KERNEL); 956 957 #ifdef CONFIG_DEBUG_PAGEALLOC 958 if (debug_pagealloc_enabled()) { 959 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT; 960 linear_map_hash_slots = memblock_alloc_try_nid( 961 linear_map_hash_count, 1, MEMBLOCK_LOW_LIMIT, 962 ppc64_rma_size, NUMA_NO_NODE); 963 if (!linear_map_hash_slots) 964 panic("%s: Failed to allocate %lu bytes max_addr=%pa\n", 965 __func__, linear_map_hash_count, &ppc64_rma_size); 966 } 967 #endif /* CONFIG_DEBUG_PAGEALLOC */ 968 969 /* create bolted the linear mapping in the hash table */ 970 for_each_mem_range(i, &base, &end) { 971 size = end - base; 972 base = (unsigned long)__va(base); 973 974 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n", 975 base, size, prot); 976 977 if ((base + size) >= H_VMALLOC_START) { 978 pr_warn("Outside the supported range\n"); 979 continue; 980 } 981 982 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base), 983 prot, mmu_linear_psize, mmu_kernel_ssize)); 984 } 985 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE); 986 987 /* 988 * If we have a memory_limit and we've allocated TCEs then we need to 989 * explicitly map the TCE area at the top of RAM. We also cope with the 990 * case that the TCEs start below memory_limit. 991 * tce_alloc_start/end are 16MB aligned so the mapping should work 992 * for either 4K or 16MB pages. 993 */ 994 if (tce_alloc_start) { 995 tce_alloc_start = (unsigned long)__va(tce_alloc_start); 996 tce_alloc_end = (unsigned long)__va(tce_alloc_end); 997 998 if (base + size >= tce_alloc_start) 999 tce_alloc_start = base + size + 1; 1000 1001 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end, 1002 __pa(tce_alloc_start), prot, 1003 mmu_linear_psize, mmu_kernel_ssize)); 1004 } 1005 1006 1007 DBG(" <- htab_initialize()\n"); 1008 } 1009 #undef KB 1010 #undef MB 1011 1012 void __init hash__early_init_devtree(void) 1013 { 1014 /* Initialize segment sizes */ 1015 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL); 1016 1017 /* Initialize page sizes */ 1018 htab_scan_page_sizes(); 1019 } 1020 1021 static struct hash_mm_context init_hash_mm_context; 1022 void __init hash__early_init_mmu(void) 1023 { 1024 #ifndef CONFIG_PPC_64K_PAGES 1025 /* 1026 * We have code in __hash_page_4K() and elsewhere, which assumes it can 1027 * do the following: 1028 * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX); 1029 * 1030 * Where the slot number is between 0-15, and values of 8-15 indicate 1031 * the secondary bucket. For that code to work H_PAGE_F_SECOND and 1032 * H_PAGE_F_GIX must occupy four contiguous bits in the PTE, and 1033 * H_PAGE_F_SECOND must be placed above H_PAGE_F_GIX. Assert that here 1034 * with a BUILD_BUG_ON(). 1035 */ 1036 BUILD_BUG_ON(H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT + 3))); 1037 #endif /* CONFIG_PPC_64K_PAGES */ 1038 1039 htab_init_page_sizes(); 1040 1041 /* 1042 * initialize page table size 1043 */ 1044 __pte_frag_nr = H_PTE_FRAG_NR; 1045 __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT; 1046 __pmd_frag_nr = H_PMD_FRAG_NR; 1047 __pmd_frag_size_shift = H_PMD_FRAG_SIZE_SHIFT; 1048 1049 __pte_index_size = H_PTE_INDEX_SIZE; 1050 __pmd_index_size = H_PMD_INDEX_SIZE; 1051 __pud_index_size = H_PUD_INDEX_SIZE; 1052 __pgd_index_size = H_PGD_INDEX_SIZE; 1053 __pud_cache_index = H_PUD_CACHE_INDEX; 1054 __pte_table_size = H_PTE_TABLE_SIZE; 1055 __pmd_table_size = H_PMD_TABLE_SIZE; 1056 __pud_table_size = H_PUD_TABLE_SIZE; 1057 __pgd_table_size = H_PGD_TABLE_SIZE; 1058 /* 1059 * 4k use hugepd format, so for hash set then to 1060 * zero 1061 */ 1062 __pmd_val_bits = HASH_PMD_VAL_BITS; 1063 __pud_val_bits = HASH_PUD_VAL_BITS; 1064 __pgd_val_bits = HASH_PGD_VAL_BITS; 1065 1066 __kernel_virt_start = H_KERN_VIRT_START; 1067 __vmalloc_start = H_VMALLOC_START; 1068 __vmalloc_end = H_VMALLOC_END; 1069 __kernel_io_start = H_KERN_IO_START; 1070 __kernel_io_end = H_KERN_IO_END; 1071 vmemmap = (struct page *)H_VMEMMAP_START; 1072 ioremap_bot = IOREMAP_BASE; 1073 1074 #ifdef CONFIG_PCI 1075 pci_io_base = ISA_IO_BASE; 1076 #endif 1077 1078 /* Select appropriate backend */ 1079 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) 1080 ps3_early_mm_init(); 1081 else if (firmware_has_feature(FW_FEATURE_LPAR)) 1082 hpte_init_pseries(); 1083 else if (IS_ENABLED(CONFIG_PPC_NATIVE)) 1084 hpte_init_native(); 1085 1086 if (!mmu_hash_ops.hpte_insert) 1087 panic("hash__early_init_mmu: No MMU hash ops defined!\n"); 1088 1089 /* 1090 * Initialize the MMU Hash table and create the linear mapping 1091 * of memory. Has to be done before SLB initialization as this is 1092 * currently where the page size encoding is obtained. 1093 */ 1094 htab_initialize(); 1095 1096 init_mm.context.hash_context = &init_hash_mm_context; 1097 mm_ctx_set_slb_addr_limit(&init_mm.context, SLB_ADDR_LIMIT_DEFAULT); 1098 1099 pr_info("Initializing hash mmu with SLB\n"); 1100 /* Initialize SLB management */ 1101 slb_initialize(); 1102 1103 if (cpu_has_feature(CPU_FTR_ARCH_206) 1104 && cpu_has_feature(CPU_FTR_HVMODE)) 1105 tlbiel_all(); 1106 } 1107 1108 #ifdef CONFIG_SMP 1109 void hash__early_init_mmu_secondary(void) 1110 { 1111 /* Initialize hash table for that CPU */ 1112 if (!firmware_has_feature(FW_FEATURE_LPAR)) { 1113 1114 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 1115 mtspr(SPRN_SDR1, _SDR1); 1116 else 1117 set_ptcr_when_no_uv(__pa(partition_tb) | 1118 (PATB_SIZE_SHIFT - 12)); 1119 } 1120 /* Initialize SLB */ 1121 slb_initialize(); 1122 1123 if (cpu_has_feature(CPU_FTR_ARCH_206) 1124 && cpu_has_feature(CPU_FTR_HVMODE)) 1125 tlbiel_all(); 1126 1127 #ifdef CONFIG_PPC_MEM_KEYS 1128 if (mmu_has_feature(MMU_FTR_PKEY)) 1129 mtspr(SPRN_UAMOR, default_uamor); 1130 #endif 1131 } 1132 #endif /* CONFIG_SMP */ 1133 1134 /* 1135 * Called by asm hashtable.S for doing lazy icache flush 1136 */ 1137 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap) 1138 { 1139 struct page *page; 1140 1141 if (!pfn_valid(pte_pfn(pte))) 1142 return pp; 1143 1144 page = pte_page(pte); 1145 1146 /* page is dirty */ 1147 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) { 1148 if (trap == 0x400) { 1149 flush_dcache_icache_page(page); 1150 set_bit(PG_arch_1, &page->flags); 1151 } else 1152 pp |= HPTE_R_N; 1153 } 1154 return pp; 1155 } 1156 1157 #ifdef CONFIG_PPC_MM_SLICES 1158 static unsigned int get_paca_psize(unsigned long addr) 1159 { 1160 unsigned char *psizes; 1161 unsigned long index, mask_index; 1162 1163 if (addr < SLICE_LOW_TOP) { 1164 psizes = get_paca()->mm_ctx_low_slices_psize; 1165 index = GET_LOW_SLICE_INDEX(addr); 1166 } else { 1167 psizes = get_paca()->mm_ctx_high_slices_psize; 1168 index = GET_HIGH_SLICE_INDEX(addr); 1169 } 1170 mask_index = index & 0x1; 1171 return (psizes[index >> 1] >> (mask_index * 4)) & 0xF; 1172 } 1173 1174 #else 1175 unsigned int get_paca_psize(unsigned long addr) 1176 { 1177 return get_paca()->mm_ctx_user_psize; 1178 } 1179 #endif 1180 1181 /* 1182 * Demote a segment to using 4k pages. 1183 * For now this makes the whole process use 4k pages. 1184 */ 1185 #ifdef CONFIG_PPC_64K_PAGES 1186 void demote_segment_4k(struct mm_struct *mm, unsigned long addr) 1187 { 1188 if (get_slice_psize(mm, addr) == MMU_PAGE_4K) 1189 return; 1190 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K); 1191 copro_flush_all_slbs(mm); 1192 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) { 1193 1194 copy_mm_to_paca(mm); 1195 slb_flush_and_restore_bolted(); 1196 } 1197 } 1198 #endif /* CONFIG_PPC_64K_PAGES */ 1199 1200 #ifdef CONFIG_PPC_SUBPAGE_PROT 1201 /* 1202 * This looks up a 2-bit protection code for a 4k subpage of a 64k page. 1203 * Userspace sets the subpage permissions using the subpage_prot system call. 1204 * 1205 * Result is 0: full permissions, _PAGE_RW: read-only, 1206 * _PAGE_RWX: no access. 1207 */ 1208 static int subpage_protection(struct mm_struct *mm, unsigned long ea) 1209 { 1210 struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context); 1211 u32 spp = 0; 1212 u32 **sbpm, *sbpp; 1213 1214 if (!spt) 1215 return 0; 1216 1217 if (ea >= spt->maxaddr) 1218 return 0; 1219 if (ea < 0x100000000UL) { 1220 /* addresses below 4GB use spt->low_prot */ 1221 sbpm = spt->low_prot; 1222 } else { 1223 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT]; 1224 if (!sbpm) 1225 return 0; 1226 } 1227 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)]; 1228 if (!sbpp) 1229 return 0; 1230 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)]; 1231 1232 /* extract 2-bit bitfield for this 4k subpage */ 1233 spp >>= 30 - 2 * ((ea >> 12) & 0xf); 1234 1235 /* 1236 * 0 -> full premission 1237 * 1 -> Read only 1238 * 2 -> no access. 1239 * We return the flag that need to be cleared. 1240 */ 1241 spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0); 1242 return spp; 1243 } 1244 1245 #else /* CONFIG_PPC_SUBPAGE_PROT */ 1246 static inline int subpage_protection(struct mm_struct *mm, unsigned long ea) 1247 { 1248 return 0; 1249 } 1250 #endif 1251 1252 void hash_failure_debug(unsigned long ea, unsigned long access, 1253 unsigned long vsid, unsigned long trap, 1254 int ssize, int psize, int lpsize, unsigned long pte) 1255 { 1256 if (!printk_ratelimit()) 1257 return; 1258 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n", 1259 ea, access, current->comm); 1260 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n", 1261 trap, vsid, ssize, psize, lpsize, pte); 1262 } 1263 1264 static void check_paca_psize(unsigned long ea, struct mm_struct *mm, 1265 int psize, bool user_region) 1266 { 1267 if (user_region) { 1268 if (psize != get_paca_psize(ea)) { 1269 copy_mm_to_paca(mm); 1270 slb_flush_and_restore_bolted(); 1271 } 1272 } else if (get_paca()->vmalloc_sllp != 1273 mmu_psize_defs[mmu_vmalloc_psize].sllp) { 1274 get_paca()->vmalloc_sllp = 1275 mmu_psize_defs[mmu_vmalloc_psize].sllp; 1276 slb_vmalloc_update(); 1277 } 1278 } 1279 1280 /* 1281 * Result code is: 1282 * 0 - handled 1283 * 1 - normal page fault 1284 * -1 - critical hash insertion error 1285 * -2 - access not permitted by subpage protection mechanism 1286 */ 1287 int hash_page_mm(struct mm_struct *mm, unsigned long ea, 1288 unsigned long access, unsigned long trap, 1289 unsigned long flags) 1290 { 1291 bool is_thp; 1292 enum ctx_state prev_state = exception_enter(); 1293 pgd_t *pgdir; 1294 unsigned long vsid; 1295 pte_t *ptep; 1296 unsigned hugeshift; 1297 int rc, user_region = 0; 1298 int psize, ssize; 1299 1300 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n", 1301 ea, access, trap); 1302 trace_hash_fault(ea, access, trap); 1303 1304 /* Get region & vsid */ 1305 switch (get_region_id(ea)) { 1306 case USER_REGION_ID: 1307 user_region = 1; 1308 if (! mm) { 1309 DBG_LOW(" user region with no mm !\n"); 1310 rc = 1; 1311 goto bail; 1312 } 1313 psize = get_slice_psize(mm, ea); 1314 ssize = user_segment_size(ea); 1315 vsid = get_user_vsid(&mm->context, ea, ssize); 1316 break; 1317 case VMALLOC_REGION_ID: 1318 vsid = get_kernel_vsid(ea, mmu_kernel_ssize); 1319 psize = mmu_vmalloc_psize; 1320 ssize = mmu_kernel_ssize; 1321 flags |= HPTE_USE_KERNEL_KEY; 1322 break; 1323 1324 case IO_REGION_ID: 1325 vsid = get_kernel_vsid(ea, mmu_kernel_ssize); 1326 psize = mmu_io_psize; 1327 ssize = mmu_kernel_ssize; 1328 flags |= HPTE_USE_KERNEL_KEY; 1329 break; 1330 default: 1331 /* 1332 * Not a valid range 1333 * Send the problem up to do_page_fault() 1334 */ 1335 rc = 1; 1336 goto bail; 1337 } 1338 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid); 1339 1340 /* Bad address. */ 1341 if (!vsid) { 1342 DBG_LOW("Bad address!\n"); 1343 rc = 1; 1344 goto bail; 1345 } 1346 /* Get pgdir */ 1347 pgdir = mm->pgd; 1348 if (pgdir == NULL) { 1349 rc = 1; 1350 goto bail; 1351 } 1352 1353 /* Check CPU locality */ 1354 if (user_region && mm_is_thread_local(mm)) 1355 flags |= HPTE_LOCAL_UPDATE; 1356 1357 #ifndef CONFIG_PPC_64K_PAGES 1358 /* 1359 * If we use 4K pages and our psize is not 4K, then we might 1360 * be hitting a special driver mapping, and need to align the 1361 * address before we fetch the PTE. 1362 * 1363 * It could also be a hugepage mapping, in which case this is 1364 * not necessary, but it's not harmful, either. 1365 */ 1366 if (psize != MMU_PAGE_4K) 1367 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1); 1368 #endif /* CONFIG_PPC_64K_PAGES */ 1369 1370 /* Get PTE and page size from page tables */ 1371 ptep = find_linux_pte(pgdir, ea, &is_thp, &hugeshift); 1372 if (ptep == NULL || !pte_present(*ptep)) { 1373 DBG_LOW(" no PTE !\n"); 1374 rc = 1; 1375 goto bail; 1376 } 1377 1378 /* 1379 * Add _PAGE_PRESENT to the required access perm. If there are parallel 1380 * updates to the pte that can possibly clear _PAGE_PTE, catch that too. 1381 * 1382 * We can safely use the return pte address in rest of the function 1383 * because we do set H_PAGE_BUSY which prevents further updates to pte 1384 * from generic code. 1385 */ 1386 access |= _PAGE_PRESENT | _PAGE_PTE; 1387 1388 /* 1389 * Pre-check access permissions (will be re-checked atomically 1390 * in __hash_page_XX but this pre-check is a fast path 1391 */ 1392 if (!check_pte_access(access, pte_val(*ptep))) { 1393 DBG_LOW(" no access !\n"); 1394 rc = 1; 1395 goto bail; 1396 } 1397 1398 if (hugeshift) { 1399 if (is_thp) 1400 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep, 1401 trap, flags, ssize, psize); 1402 #ifdef CONFIG_HUGETLB_PAGE 1403 else 1404 rc = __hash_page_huge(ea, access, vsid, ptep, trap, 1405 flags, ssize, hugeshift, psize); 1406 #else 1407 else { 1408 /* 1409 * if we have hugeshift, and is not transhuge with 1410 * hugetlb disabled, something is really wrong. 1411 */ 1412 rc = 1; 1413 WARN_ON(1); 1414 } 1415 #endif 1416 if (current->mm == mm) 1417 check_paca_psize(ea, mm, psize, user_region); 1418 1419 goto bail; 1420 } 1421 1422 #ifndef CONFIG_PPC_64K_PAGES 1423 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep)); 1424 #else 1425 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep), 1426 pte_val(*(ptep + PTRS_PER_PTE))); 1427 #endif 1428 /* Do actual hashing */ 1429 #ifdef CONFIG_PPC_64K_PAGES 1430 /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */ 1431 if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) { 1432 demote_segment_4k(mm, ea); 1433 psize = MMU_PAGE_4K; 1434 } 1435 1436 /* 1437 * If this PTE is non-cacheable and we have restrictions on 1438 * using non cacheable large pages, then we switch to 4k 1439 */ 1440 if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) { 1441 if (user_region) { 1442 demote_segment_4k(mm, ea); 1443 psize = MMU_PAGE_4K; 1444 } else if (ea < VMALLOC_END) { 1445 /* 1446 * some driver did a non-cacheable mapping 1447 * in vmalloc space, so switch vmalloc 1448 * to 4k pages 1449 */ 1450 printk(KERN_ALERT "Reducing vmalloc segment " 1451 "to 4kB pages because of " 1452 "non-cacheable mapping\n"); 1453 psize = mmu_vmalloc_psize = MMU_PAGE_4K; 1454 copro_flush_all_slbs(mm); 1455 } 1456 } 1457 1458 #endif /* CONFIG_PPC_64K_PAGES */ 1459 1460 if (current->mm == mm) 1461 check_paca_psize(ea, mm, psize, user_region); 1462 1463 #ifdef CONFIG_PPC_64K_PAGES 1464 if (psize == MMU_PAGE_64K) 1465 rc = __hash_page_64K(ea, access, vsid, ptep, trap, 1466 flags, ssize); 1467 else 1468 #endif /* CONFIG_PPC_64K_PAGES */ 1469 { 1470 int spp = subpage_protection(mm, ea); 1471 if (access & spp) 1472 rc = -2; 1473 else 1474 rc = __hash_page_4K(ea, access, vsid, ptep, trap, 1475 flags, ssize, spp); 1476 } 1477 1478 /* 1479 * Dump some info in case of hash insertion failure, they should 1480 * never happen so it is really useful to know if/when they do 1481 */ 1482 if (rc == -1) 1483 hash_failure_debug(ea, access, vsid, trap, ssize, psize, 1484 psize, pte_val(*ptep)); 1485 #ifndef CONFIG_PPC_64K_PAGES 1486 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep)); 1487 #else 1488 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep), 1489 pte_val(*(ptep + PTRS_PER_PTE))); 1490 #endif 1491 DBG_LOW(" -> rc=%d\n", rc); 1492 1493 bail: 1494 exception_exit(prev_state); 1495 return rc; 1496 } 1497 EXPORT_SYMBOL_GPL(hash_page_mm); 1498 1499 int hash_page(unsigned long ea, unsigned long access, unsigned long trap, 1500 unsigned long dsisr) 1501 { 1502 unsigned long flags = 0; 1503 struct mm_struct *mm = current->mm; 1504 1505 if ((get_region_id(ea) == VMALLOC_REGION_ID) || 1506 (get_region_id(ea) == IO_REGION_ID)) 1507 mm = &init_mm; 1508 1509 if (dsisr & DSISR_NOHPTE) 1510 flags |= HPTE_NOHPTE_UPDATE; 1511 1512 return hash_page_mm(mm, ea, access, trap, flags); 1513 } 1514 EXPORT_SYMBOL_GPL(hash_page); 1515 1516 DECLARE_INTERRUPT_HANDLER_RET(__do_hash_fault); 1517 DEFINE_INTERRUPT_HANDLER_RET(__do_hash_fault) 1518 { 1519 unsigned long ea = regs->dar; 1520 unsigned long dsisr = regs->dsisr; 1521 unsigned long access = _PAGE_PRESENT | _PAGE_READ; 1522 unsigned long flags = 0; 1523 struct mm_struct *mm; 1524 unsigned int region_id; 1525 long err; 1526 1527 region_id = get_region_id(ea); 1528 if ((region_id == VMALLOC_REGION_ID) || (region_id == IO_REGION_ID)) 1529 mm = &init_mm; 1530 else 1531 mm = current->mm; 1532 1533 if (dsisr & DSISR_NOHPTE) 1534 flags |= HPTE_NOHPTE_UPDATE; 1535 1536 if (dsisr & DSISR_ISSTORE) 1537 access |= _PAGE_WRITE; 1538 /* 1539 * We set _PAGE_PRIVILEGED only when 1540 * kernel mode access kernel space. 1541 * 1542 * _PAGE_PRIVILEGED is NOT set 1543 * 1) when kernel mode access user space 1544 * 2) user space access kernel space. 1545 */ 1546 access |= _PAGE_PRIVILEGED; 1547 if (user_mode(regs) || (region_id == USER_REGION_ID)) 1548 access &= ~_PAGE_PRIVILEGED; 1549 1550 if (regs->trap == 0x400) 1551 access |= _PAGE_EXEC; 1552 1553 err = hash_page_mm(mm, ea, access, regs->trap, flags); 1554 if (unlikely(err < 0)) { 1555 // failed to instert a hash PTE due to an hypervisor error 1556 if (user_mode(regs)) { 1557 if (IS_ENABLED(CONFIG_PPC_SUBPAGE_PROT) && err == -2) 1558 _exception(SIGSEGV, regs, SEGV_ACCERR, ea); 1559 else 1560 _exception(SIGBUS, regs, BUS_ADRERR, ea); 1561 } else { 1562 bad_page_fault(regs, SIGBUS); 1563 } 1564 err = 0; 1565 } 1566 1567 return err; 1568 } 1569 1570 /* 1571 * The _RAW interrupt entry checks for the in_nmi() case before 1572 * running the full handler. 1573 */ 1574 DEFINE_INTERRUPT_HANDLER_RAW(do_hash_fault) 1575 { 1576 unsigned long dsisr = regs->dsisr; 1577 long err; 1578 1579 if (unlikely(dsisr & (DSISR_BAD_FAULT_64S | DSISR_KEYFAULT))) 1580 goto page_fault; 1581 1582 /* 1583 * If we are in an "NMI" (e.g., an interrupt when soft-disabled), then 1584 * don't call hash_page, just fail the fault. This is required to 1585 * prevent re-entrancy problems in the hash code, namely perf 1586 * interrupts hitting while something holds H_PAGE_BUSY, and taking a 1587 * hash fault. See the comment in hash_preload(). 1588 * 1589 * We come here as a result of a DSI at a point where we don't want 1590 * to call hash_page, such as when we are accessing memory (possibly 1591 * user memory) inside a PMU interrupt that occurred while interrupts 1592 * were soft-disabled. We want to invoke the exception handler for 1593 * the access, or panic if there isn't a handler. 1594 */ 1595 if (unlikely(in_nmi())) { 1596 do_bad_page_fault_segv(regs); 1597 return 0; 1598 } 1599 1600 err = __do_hash_fault(regs); 1601 if (err) { 1602 page_fault: 1603 err = do_page_fault(regs); 1604 } 1605 1606 return err; 1607 } 1608 1609 #ifdef CONFIG_PPC_MM_SLICES 1610 static bool should_hash_preload(struct mm_struct *mm, unsigned long ea) 1611 { 1612 int psize = get_slice_psize(mm, ea); 1613 1614 /* We only prefault standard pages for now */ 1615 if (unlikely(psize != mm_ctx_user_psize(&mm->context))) 1616 return false; 1617 1618 /* 1619 * Don't prefault if subpage protection is enabled for the EA. 1620 */ 1621 if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea))) 1622 return false; 1623 1624 return true; 1625 } 1626 #else 1627 static bool should_hash_preload(struct mm_struct *mm, unsigned long ea) 1628 { 1629 return true; 1630 } 1631 #endif 1632 1633 static void hash_preload(struct mm_struct *mm, pte_t *ptep, unsigned long ea, 1634 bool is_exec, unsigned long trap) 1635 { 1636 unsigned long vsid; 1637 pgd_t *pgdir; 1638 int rc, ssize, update_flags = 0; 1639 unsigned long access = _PAGE_PRESENT | _PAGE_READ | (is_exec ? _PAGE_EXEC : 0); 1640 unsigned long flags; 1641 1642 BUG_ON(get_region_id(ea) != USER_REGION_ID); 1643 1644 if (!should_hash_preload(mm, ea)) 1645 return; 1646 1647 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx," 1648 " trap=%lx\n", mm, mm->pgd, ea, access, trap); 1649 1650 /* Get Linux PTE if available */ 1651 pgdir = mm->pgd; 1652 if (pgdir == NULL) 1653 return; 1654 1655 /* Get VSID */ 1656 ssize = user_segment_size(ea); 1657 vsid = get_user_vsid(&mm->context, ea, ssize); 1658 if (!vsid) 1659 return; 1660 1661 #ifdef CONFIG_PPC_64K_PAGES 1662 /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on 1663 * a 64K kernel), then we don't preload, hash_page() will take 1664 * care of it once we actually try to access the page. 1665 * That way we don't have to duplicate all of the logic for segment 1666 * page size demotion here 1667 * Called with PTL held, hence can be sure the value won't change in 1668 * between. 1669 */ 1670 if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep)) 1671 return; 1672 #endif /* CONFIG_PPC_64K_PAGES */ 1673 1674 /* 1675 * __hash_page_* must run with interrupts off, as it sets the 1676 * H_PAGE_BUSY bit. It's possible for perf interrupts to hit at any 1677 * time and may take a hash fault reading the user stack, see 1678 * read_user_stack_slow() in the powerpc/perf code. 1679 * 1680 * If that takes a hash fault on the same page as we lock here, it 1681 * will bail out when seeing H_PAGE_BUSY set, and retry the access 1682 * leading to an infinite loop. 1683 * 1684 * Disabling interrupts here does not prevent perf interrupts, but it 1685 * will prevent them taking hash faults (see the NMI test in 1686 * do_hash_page), then read_user_stack's copy_from_user_nofault will 1687 * fail and perf will fall back to read_user_stack_slow(), which 1688 * walks the Linux page tables. 1689 * 1690 * Interrupts must also be off for the duration of the 1691 * mm_is_thread_local test and update, to prevent preempt running the 1692 * mm on another CPU (XXX: this may be racy vs kthread_use_mm). 1693 */ 1694 local_irq_save(flags); 1695 1696 /* Is that local to this CPU ? */ 1697 if (mm_is_thread_local(mm)) 1698 update_flags |= HPTE_LOCAL_UPDATE; 1699 1700 /* Hash it in */ 1701 #ifdef CONFIG_PPC_64K_PAGES 1702 if (mm_ctx_user_psize(&mm->context) == MMU_PAGE_64K) 1703 rc = __hash_page_64K(ea, access, vsid, ptep, trap, 1704 update_flags, ssize); 1705 else 1706 #endif /* CONFIG_PPC_64K_PAGES */ 1707 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags, 1708 ssize, subpage_protection(mm, ea)); 1709 1710 /* Dump some info in case of hash insertion failure, they should 1711 * never happen so it is really useful to know if/when they do 1712 */ 1713 if (rc == -1) 1714 hash_failure_debug(ea, access, vsid, trap, ssize, 1715 mm_ctx_user_psize(&mm->context), 1716 mm_ctx_user_psize(&mm->context), 1717 pte_val(*ptep)); 1718 1719 local_irq_restore(flags); 1720 } 1721 1722 /* 1723 * This is called at the end of handling a user page fault, when the 1724 * fault has been handled by updating a PTE in the linux page tables. 1725 * We use it to preload an HPTE into the hash table corresponding to 1726 * the updated linux PTE. 1727 * 1728 * This must always be called with the pte lock held. 1729 */ 1730 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, 1731 pte_t *ptep) 1732 { 1733 /* 1734 * We don't need to worry about _PAGE_PRESENT here because we are 1735 * called with either mm->page_table_lock held or ptl lock held 1736 */ 1737 unsigned long trap; 1738 bool is_exec; 1739 1740 if (radix_enabled()) 1741 return; 1742 1743 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */ 1744 if (!pte_young(*ptep) || address >= TASK_SIZE) 1745 return; 1746 1747 /* 1748 * We try to figure out if we are coming from an instruction 1749 * access fault and pass that down to __hash_page so we avoid 1750 * double-faulting on execution of fresh text. We have to test 1751 * for regs NULL since init will get here first thing at boot. 1752 * 1753 * We also avoid filling the hash if not coming from a fault. 1754 */ 1755 1756 trap = current->thread.regs ? TRAP(current->thread.regs) : 0UL; 1757 switch (trap) { 1758 case 0x300: 1759 is_exec = false; 1760 break; 1761 case 0x400: 1762 is_exec = true; 1763 break; 1764 default: 1765 return; 1766 } 1767 1768 hash_preload(vma->vm_mm, ptep, address, is_exec, trap); 1769 } 1770 1771 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1772 static inline void tm_flush_hash_page(int local) 1773 { 1774 /* 1775 * Transactions are not aborted by tlbiel, only tlbie. Without, syncing a 1776 * page back to a block device w/PIO could pick up transactional data 1777 * (bad!) so we force an abort here. Before the sync the page will be 1778 * made read-only, which will flush_hash_page. BIG ISSUE here: if the 1779 * kernel uses a page from userspace without unmapping it first, it may 1780 * see the speculated version. 1781 */ 1782 if (local && cpu_has_feature(CPU_FTR_TM) && current->thread.regs && 1783 MSR_TM_ACTIVE(current->thread.regs->msr)) { 1784 tm_enable(); 1785 tm_abort(TM_CAUSE_TLBI); 1786 } 1787 } 1788 #else 1789 static inline void tm_flush_hash_page(int local) 1790 { 1791 } 1792 #endif 1793 1794 /* 1795 * Return the global hash slot, corresponding to the given PTE, which contains 1796 * the HPTE. 1797 */ 1798 unsigned long pte_get_hash_gslot(unsigned long vpn, unsigned long shift, 1799 int ssize, real_pte_t rpte, unsigned int subpg_index) 1800 { 1801 unsigned long hash, gslot, hidx; 1802 1803 hash = hpt_hash(vpn, shift, ssize); 1804 hidx = __rpte_to_hidx(rpte, subpg_index); 1805 if (hidx & _PTEIDX_SECONDARY) 1806 hash = ~hash; 1807 gslot = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1808 gslot += hidx & _PTEIDX_GROUP_IX; 1809 return gslot; 1810 } 1811 1812 void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize, 1813 unsigned long flags) 1814 { 1815 unsigned long index, shift, gslot; 1816 int local = flags & HPTE_LOCAL_UPDATE; 1817 1818 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn); 1819 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { 1820 gslot = pte_get_hash_gslot(vpn, shift, ssize, pte, index); 1821 DBG_LOW(" sub %ld: gslot=%lx\n", index, gslot); 1822 /* 1823 * We use same base page size and actual psize, because we don't 1824 * use these functions for hugepage 1825 */ 1826 mmu_hash_ops.hpte_invalidate(gslot, vpn, psize, psize, 1827 ssize, local); 1828 } pte_iterate_hashed_end(); 1829 1830 tm_flush_hash_page(local); 1831 } 1832 1833 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1834 void flush_hash_hugepage(unsigned long vsid, unsigned long addr, 1835 pmd_t *pmdp, unsigned int psize, int ssize, 1836 unsigned long flags) 1837 { 1838 int i, max_hpte_count, valid; 1839 unsigned long s_addr; 1840 unsigned char *hpte_slot_array; 1841 unsigned long hidx, shift, vpn, hash, slot; 1842 int local = flags & HPTE_LOCAL_UPDATE; 1843 1844 s_addr = addr & HPAGE_PMD_MASK; 1845 hpte_slot_array = get_hpte_slot_array(pmdp); 1846 /* 1847 * IF we try to do a HUGE PTE update after a withdraw is done. 1848 * we will find the below NULL. This happens when we do 1849 * split_huge_pmd 1850 */ 1851 if (!hpte_slot_array) 1852 return; 1853 1854 if (mmu_hash_ops.hugepage_invalidate) { 1855 mmu_hash_ops.hugepage_invalidate(vsid, s_addr, hpte_slot_array, 1856 psize, ssize, local); 1857 goto tm_abort; 1858 } 1859 /* 1860 * No bluk hpte removal support, invalidate each entry 1861 */ 1862 shift = mmu_psize_defs[psize].shift; 1863 max_hpte_count = HPAGE_PMD_SIZE >> shift; 1864 for (i = 0; i < max_hpte_count; i++) { 1865 /* 1866 * 8 bits per each hpte entries 1867 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit] 1868 */ 1869 valid = hpte_valid(hpte_slot_array, i); 1870 if (!valid) 1871 continue; 1872 hidx = hpte_hash_index(hpte_slot_array, i); 1873 1874 /* get the vpn */ 1875 addr = s_addr + (i * (1ul << shift)); 1876 vpn = hpt_vpn(addr, vsid, ssize); 1877 hash = hpt_hash(vpn, shift, ssize); 1878 if (hidx & _PTEIDX_SECONDARY) 1879 hash = ~hash; 1880 1881 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1882 slot += hidx & _PTEIDX_GROUP_IX; 1883 mmu_hash_ops.hpte_invalidate(slot, vpn, psize, 1884 MMU_PAGE_16M, ssize, local); 1885 } 1886 tm_abort: 1887 tm_flush_hash_page(local); 1888 } 1889 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1890 1891 void flush_hash_range(unsigned long number, int local) 1892 { 1893 if (mmu_hash_ops.flush_hash_range) 1894 mmu_hash_ops.flush_hash_range(number, local); 1895 else { 1896 int i; 1897 struct ppc64_tlb_batch *batch = 1898 this_cpu_ptr(&ppc64_tlb_batch); 1899 1900 for (i = 0; i < number; i++) 1901 flush_hash_page(batch->vpn[i], batch->pte[i], 1902 batch->psize, batch->ssize, local); 1903 } 1904 } 1905 1906 long hpte_insert_repeating(unsigned long hash, unsigned long vpn, 1907 unsigned long pa, unsigned long rflags, 1908 unsigned long vflags, int psize, int ssize) 1909 { 1910 unsigned long hpte_group; 1911 long slot; 1912 1913 repeat: 1914 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1915 1916 /* Insert into the hash table, primary slot */ 1917 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, vflags, 1918 psize, psize, ssize); 1919 1920 /* Primary is full, try the secondary */ 1921 if (unlikely(slot == -1)) { 1922 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP; 1923 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 1924 vflags | HPTE_V_SECONDARY, 1925 psize, psize, ssize); 1926 if (slot == -1) { 1927 if (mftb() & 0x1) 1928 hpte_group = (hash & htab_hash_mask) * 1929 HPTES_PER_GROUP; 1930 1931 mmu_hash_ops.hpte_remove(hpte_group); 1932 goto repeat; 1933 } 1934 } 1935 1936 return slot; 1937 } 1938 1939 #ifdef CONFIG_DEBUG_PAGEALLOC 1940 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi) 1941 { 1942 unsigned long hash; 1943 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize); 1944 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize); 1945 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL), HPTE_USE_KERNEL_KEY); 1946 long ret; 1947 1948 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize); 1949 1950 /* Don't create HPTE entries for bad address */ 1951 if (!vsid) 1952 return; 1953 1954 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode, 1955 HPTE_V_BOLTED, 1956 mmu_linear_psize, mmu_kernel_ssize); 1957 1958 BUG_ON (ret < 0); 1959 spin_lock(&linear_map_hash_lock); 1960 BUG_ON(linear_map_hash_slots[lmi] & 0x80); 1961 linear_map_hash_slots[lmi] = ret | 0x80; 1962 spin_unlock(&linear_map_hash_lock); 1963 } 1964 1965 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi) 1966 { 1967 unsigned long hash, hidx, slot; 1968 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize); 1969 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize); 1970 1971 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize); 1972 spin_lock(&linear_map_hash_lock); 1973 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80)); 1974 hidx = linear_map_hash_slots[lmi] & 0x7f; 1975 linear_map_hash_slots[lmi] = 0; 1976 spin_unlock(&linear_map_hash_lock); 1977 if (hidx & _PTEIDX_SECONDARY) 1978 hash = ~hash; 1979 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; 1980 slot += hidx & _PTEIDX_GROUP_IX; 1981 mmu_hash_ops.hpte_invalidate(slot, vpn, mmu_linear_psize, 1982 mmu_linear_psize, 1983 mmu_kernel_ssize, 0); 1984 } 1985 1986 void __kernel_map_pages(struct page *page, int numpages, int enable) 1987 { 1988 unsigned long flags, vaddr, lmi; 1989 int i; 1990 1991 local_irq_save(flags); 1992 for (i = 0; i < numpages; i++, page++) { 1993 vaddr = (unsigned long)page_address(page); 1994 lmi = __pa(vaddr) >> PAGE_SHIFT; 1995 if (lmi >= linear_map_hash_count) 1996 continue; 1997 if (enable) 1998 kernel_map_linear_page(vaddr, lmi); 1999 else 2000 kernel_unmap_linear_page(vaddr, lmi); 2001 } 2002 local_irq_restore(flags); 2003 } 2004 #endif /* CONFIG_DEBUG_PAGEALLOC */ 2005 2006 void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base, 2007 phys_addr_t first_memblock_size) 2008 { 2009 /* 2010 * We don't currently support the first MEMBLOCK not mapping 0 2011 * physical on those processors 2012 */ 2013 BUG_ON(first_memblock_base != 0); 2014 2015 /* 2016 * On virtualized systems the first entry is our RMA region aka VRMA, 2017 * non-virtualized 64-bit hash MMU systems don't have a limitation 2018 * on real mode access. 2019 * 2020 * For guests on platforms before POWER9, we clamp the it limit to 1G 2021 * to avoid some funky things such as RTAS bugs etc... 2022 * 2023 * On POWER9 we limit to 1TB in case the host erroneously told us that 2024 * the RMA was >1TB. Effective address bits 0:23 are treated as zero 2025 * (meaning the access is aliased to zero i.e. addr = addr % 1TB) 2026 * for virtual real mode addressing and so it doesn't make sense to 2027 * have an area larger than 1TB as it can't be addressed. 2028 */ 2029 if (!early_cpu_has_feature(CPU_FTR_HVMODE)) { 2030 ppc64_rma_size = first_memblock_size; 2031 if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) 2032 ppc64_rma_size = min_t(u64, ppc64_rma_size, 0x40000000); 2033 else 2034 ppc64_rma_size = min_t(u64, ppc64_rma_size, 2035 1UL << SID_SHIFT_1T); 2036 2037 /* Finally limit subsequent allocations */ 2038 memblock_set_current_limit(ppc64_rma_size); 2039 } else { 2040 ppc64_rma_size = ULONG_MAX; 2041 } 2042 } 2043 2044 #ifdef CONFIG_DEBUG_FS 2045 2046 static int hpt_order_get(void *data, u64 *val) 2047 { 2048 *val = ppc64_pft_size; 2049 return 0; 2050 } 2051 2052 static int hpt_order_set(void *data, u64 val) 2053 { 2054 int ret; 2055 2056 if (!mmu_hash_ops.resize_hpt) 2057 return -ENODEV; 2058 2059 cpus_read_lock(); 2060 ret = mmu_hash_ops.resize_hpt(val); 2061 cpus_read_unlock(); 2062 2063 return ret; 2064 } 2065 2066 DEFINE_DEBUGFS_ATTRIBUTE(fops_hpt_order, hpt_order_get, hpt_order_set, "%llu\n"); 2067 2068 static int __init hash64_debugfs(void) 2069 { 2070 debugfs_create_file("hpt_order", 0600, powerpc_debugfs_root, NULL, 2071 &fops_hpt_order); 2072 return 0; 2073 } 2074 machine_device_initcall(pseries, hash64_debugfs); 2075 #endif /* CONFIG_DEBUG_FS */ 2076 2077 void __init print_system_hash_info(void) 2078 { 2079 pr_info("ppc64_pft_size = 0x%llx\n", ppc64_pft_size); 2080 2081 if (htab_hash_mask) 2082 pr_info("htab_hash_mask = 0x%lx\n", htab_hash_mask); 2083 } 2084