usb: xhci-plat: properly handle probe deferral for devm_clk_get()
[linux/fpc-iii.git] / arch / powerpc / mm / hash_utils_64.c
blobf4acba25fa5e223209e86677c513bcf0c9e75ba3
1 /*
2 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
3 * {mikejc|engebret}@us.ibm.com
5 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
7 * SMP scalability work:
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 * Module name: htab.c
12 * Description:
13 * PowerPC Hashed Page Table functions
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
21 #undef DEBUG
22 #undef DEBUG_LOW
24 #include <linux/spinlock.h>
25 #include <linux/errno.h>
26 #include <linux/sched.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stat.h>
29 #include <linux/sysctl.h>
30 #include <linux/export.h>
31 #include <linux/ctype.h>
32 #include <linux/cache.h>
33 #include <linux/init.h>
34 #include <linux/signal.h>
35 #include <linux/memblock.h>
36 #include <linux/context_tracking.h>
38 #include <asm/processor.h>
39 #include <asm/pgtable.h>
40 #include <asm/mmu.h>
41 #include <asm/mmu_context.h>
42 #include <asm/page.h>
43 #include <asm/types.h>
44 #include <asm/uaccess.h>
45 #include <asm/machdep.h>
46 #include <asm/prom.h>
47 #include <asm/tlbflush.h>
48 #include <asm/io.h>
49 #include <asm/eeh.h>
50 #include <asm/tlb.h>
51 #include <asm/cacheflush.h>
52 #include <asm/cputable.h>
53 #include <asm/sections.h>
54 #include <asm/copro.h>
55 #include <asm/udbg.h>
56 #include <asm/code-patching.h>
57 #include <asm/fadump.h>
58 #include <asm/firmware.h>
59 #include <asm/tm.h>
60 #include <asm/trace.h>
62 #ifdef DEBUG
63 #define DBG(fmt...) udbg_printf(fmt)
64 #else
65 #define DBG(fmt...)
66 #endif
68 #ifdef DEBUG_LOW
69 #define DBG_LOW(fmt...) udbg_printf(fmt)
70 #else
71 #define DBG_LOW(fmt...)
72 #endif
74 #define KB (1024)
75 #define MB (1024*KB)
76 #define GB (1024L*MB)
79 * Note: pte --> Linux PTE
80 * HPTE --> PowerPC Hashed Page Table Entry
82 * Execution context:
83 * htab_initialize is called with the MMU off (of course), but
84 * the kernel has been copied down to zero so it can directly
85 * reference global data. At this point it is very difficult
86 * to print debug info.
90 #ifdef CONFIG_U3_DART
91 extern unsigned long dart_tablebase;
92 #endif /* CONFIG_U3_DART */
94 static unsigned long _SDR1;
95 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
96 EXPORT_SYMBOL_GPL(mmu_psize_defs);
98 struct hash_pte *htab_address;
99 unsigned long htab_size_bytes;
100 unsigned long htab_hash_mask;
101 EXPORT_SYMBOL_GPL(htab_hash_mask);
102 int mmu_linear_psize = MMU_PAGE_4K;
103 EXPORT_SYMBOL_GPL(mmu_linear_psize);
104 int mmu_virtual_psize = MMU_PAGE_4K;
105 int mmu_vmalloc_psize = MMU_PAGE_4K;
106 #ifdef CONFIG_SPARSEMEM_VMEMMAP
107 int mmu_vmemmap_psize = MMU_PAGE_4K;
108 #endif
109 int mmu_io_psize = MMU_PAGE_4K;
110 int mmu_kernel_ssize = MMU_SEGSIZE_256M;
111 EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
112 int mmu_highuser_ssize = MMU_SEGSIZE_256M;
113 u16 mmu_slb_size = 64;
114 EXPORT_SYMBOL_GPL(mmu_slb_size);
115 #ifdef CONFIG_PPC_64K_PAGES
116 int mmu_ci_restrictions;
117 #endif
118 #ifdef CONFIG_DEBUG_PAGEALLOC
119 static u8 *linear_map_hash_slots;
120 static unsigned long linear_map_hash_count;
121 static DEFINE_SPINLOCK(linear_map_hash_lock);
122 #endif /* CONFIG_DEBUG_PAGEALLOC */
124 /* There are definitions of page sizes arrays to be used when none
125 * is provided by the firmware.
128 /* Pre-POWER4 CPUs (4k pages only)
130 static struct mmu_psize_def mmu_psize_defaults_old[] = {
131 [MMU_PAGE_4K] = {
132 .shift = 12,
133 .sllp = 0,
134 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
135 .avpnm = 0,
136 .tlbiel = 0,
140 /* POWER4, GPUL, POWER5
142 * Support for 16Mb large pages
144 static struct mmu_psize_def mmu_psize_defaults_gp[] = {
145 [MMU_PAGE_4K] = {
146 .shift = 12,
147 .sllp = 0,
148 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
149 .avpnm = 0,
150 .tlbiel = 1,
152 [MMU_PAGE_16M] = {
153 .shift = 24,
154 .sllp = SLB_VSID_L,
155 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
156 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
157 .avpnm = 0x1UL,
158 .tlbiel = 0,
163 * 'R' and 'C' update notes:
164 * - Under pHyp or KVM, the updatepp path will not set C, thus it *will*
165 * create writeable HPTEs without C set, because the hcall H_PROTECT
166 * that we use in that case will not update C
167 * - The above is however not a problem, because we also don't do that
168 * fancy "no flush" variant of eviction and we use H_REMOVE which will
169 * do the right thing and thus we don't have the race I described earlier
171 * - Under bare metal, we do have the race, so we need R and C set
172 * - We make sure R is always set and never lost
173 * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping
175 unsigned long htab_convert_pte_flags(unsigned long pteflags)
177 unsigned long rflags = 0;
179 /* _PAGE_EXEC -> NOEXEC */
180 if ((pteflags & _PAGE_EXEC) == 0)
181 rflags |= HPTE_R_N;
183 * PP bits:
184 * Linux uses slb key 0 for kernel and 1 for user.
185 * kernel areas are mapped with PP=00
186 * and there is no kernel RO (_PAGE_KERNEL_RO).
187 * User area is mapped with PP=0x2 for read/write
188 * or PP=0x3 for read-only (including writeable but clean pages).
190 if (pteflags & _PAGE_USER) {
191 rflags |= 0x2;
192 if (!((pteflags & _PAGE_RW) && (pteflags & _PAGE_DIRTY)))
193 rflags |= 0x1;
196 * We can't allow hardware to update hpte bits. Hence always
197 * set 'R' bit and set 'C' if it is a write fault
198 * Memory coherence is always enabled
200 rflags |= HPTE_R_R | HPTE_R_M;
202 if (pteflags & _PAGE_DIRTY)
203 rflags |= HPTE_R_C;
205 * Add in WIG bits
207 if (pteflags & _PAGE_WRITETHRU)
208 rflags |= HPTE_R_W;
209 if (pteflags & _PAGE_NO_CACHE)
210 rflags |= HPTE_R_I;
211 if (pteflags & _PAGE_GUARDED)
212 rflags |= HPTE_R_G;
214 return rflags;
217 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
218 unsigned long pstart, unsigned long prot,
219 int psize, int ssize)
221 unsigned long vaddr, paddr;
222 unsigned int step, shift;
223 int ret = 0;
225 shift = mmu_psize_defs[psize].shift;
226 step = 1 << shift;
228 prot = htab_convert_pte_flags(prot);
230 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
231 vstart, vend, pstart, prot, psize, ssize);
233 for (vaddr = vstart, paddr = pstart; vaddr < vend;
234 vaddr += step, paddr += step) {
235 unsigned long hash, hpteg;
236 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
237 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
238 unsigned long tprot = prot;
241 * If we hit a bad address return error.
243 if (!vsid)
244 return -1;
245 /* Make kernel text executable */
246 if (overlaps_kernel_text(vaddr, vaddr + step))
247 tprot &= ~HPTE_R_N;
249 /* Make kvm guest trampolines executable */
250 if (overlaps_kvm_tmp(vaddr, vaddr + step))
251 tprot &= ~HPTE_R_N;
254 * If relocatable, check if it overlaps interrupt vectors that
255 * are copied down to real 0. For relocatable kernel
256 * (e.g. kdump case) we copy interrupt vectors down to real
257 * address 0. Mark that region as executable. This is
258 * because on p8 system with relocation on exception feature
259 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
260 * in order to execute the interrupt handlers in virtual
261 * mode the vector region need to be marked as executable.
263 if ((PHYSICAL_START > MEMORY_START) &&
264 overlaps_interrupt_vector_text(vaddr, vaddr + step))
265 tprot &= ~HPTE_R_N;
267 hash = hpt_hash(vpn, shift, ssize);
268 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
270 BUG_ON(!ppc_md.hpte_insert);
271 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
272 HPTE_V_BOLTED, psize, psize, ssize);
274 if (ret < 0)
275 break;
277 #ifdef CONFIG_DEBUG_PAGEALLOC
278 if (debug_pagealloc_enabled() &&
279 (paddr >> PAGE_SHIFT) < linear_map_hash_count)
280 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
281 #endif /* CONFIG_DEBUG_PAGEALLOC */
283 return ret < 0 ? ret : 0;
286 int htab_remove_mapping(unsigned long vstart, unsigned long vend,
287 int psize, int ssize)
289 unsigned long vaddr;
290 unsigned int step, shift;
291 int rc;
292 int ret = 0;
294 shift = mmu_psize_defs[psize].shift;
295 step = 1 << shift;
297 if (!ppc_md.hpte_removebolted)
298 return -ENODEV;
300 for (vaddr = vstart; vaddr < vend; vaddr += step) {
301 rc = ppc_md.hpte_removebolted(vaddr, psize, ssize);
302 if (rc == -ENOENT) {
303 ret = -ENOENT;
304 continue;
306 if (rc < 0)
307 return rc;
310 return ret;
313 static int __init htab_dt_scan_seg_sizes(unsigned long node,
314 const char *uname, int depth,
315 void *data)
317 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
318 const __be32 *prop;
319 int size = 0;
321 /* We are scanning "cpu" nodes only */
322 if (type == NULL || strcmp(type, "cpu") != 0)
323 return 0;
325 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
326 if (prop == NULL)
327 return 0;
328 for (; size >= 4; size -= 4, ++prop) {
329 if (be32_to_cpu(prop[0]) == 40) {
330 DBG("1T segment support detected\n");
331 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
332 return 1;
335 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
336 return 0;
339 static void __init htab_init_seg_sizes(void)
341 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
344 static int __init get_idx_from_shift(unsigned int shift)
346 int idx = -1;
348 switch (shift) {
349 case 0xc:
350 idx = MMU_PAGE_4K;
351 break;
352 case 0x10:
353 idx = MMU_PAGE_64K;
354 break;
355 case 0x14:
356 idx = MMU_PAGE_1M;
357 break;
358 case 0x18:
359 idx = MMU_PAGE_16M;
360 break;
361 case 0x22:
362 idx = MMU_PAGE_16G;
363 break;
365 return idx;
368 static int __init htab_dt_scan_page_sizes(unsigned long node,
369 const char *uname, int depth,
370 void *data)
372 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
373 const __be32 *prop;
374 int size = 0;
376 /* We are scanning "cpu" nodes only */
377 if (type == NULL || strcmp(type, "cpu") != 0)
378 return 0;
380 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
381 if (!prop)
382 return 0;
384 pr_info("Page sizes from device-tree:\n");
385 size /= 4;
386 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
387 while(size > 0) {
388 unsigned int base_shift = be32_to_cpu(prop[0]);
389 unsigned int slbenc = be32_to_cpu(prop[1]);
390 unsigned int lpnum = be32_to_cpu(prop[2]);
391 struct mmu_psize_def *def;
392 int idx, base_idx;
394 size -= 3; prop += 3;
395 base_idx = get_idx_from_shift(base_shift);
396 if (base_idx < 0) {
397 /* skip the pte encoding also */
398 prop += lpnum * 2; size -= lpnum * 2;
399 continue;
401 def = &mmu_psize_defs[base_idx];
402 if (base_idx == MMU_PAGE_16M)
403 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
405 def->shift = base_shift;
406 if (base_shift <= 23)
407 def->avpnm = 0;
408 else
409 def->avpnm = (1 << (base_shift - 23)) - 1;
410 def->sllp = slbenc;
412 * We don't know for sure what's up with tlbiel, so
413 * for now we only set it for 4K and 64K pages
415 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
416 def->tlbiel = 1;
417 else
418 def->tlbiel = 0;
420 while (size > 0 && lpnum) {
421 unsigned int shift = be32_to_cpu(prop[0]);
422 int penc = be32_to_cpu(prop[1]);
424 prop += 2; size -= 2;
425 lpnum--;
427 idx = get_idx_from_shift(shift);
428 if (idx < 0)
429 continue;
431 if (penc == -1)
432 pr_err("Invalid penc for base_shift=%d "
433 "shift=%d\n", base_shift, shift);
435 def->penc[idx] = penc;
436 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
437 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
438 base_shift, shift, def->sllp,
439 def->avpnm, def->tlbiel, def->penc[idx]);
443 return 1;
446 #ifdef CONFIG_HUGETLB_PAGE
447 /* Scan for 16G memory blocks that have been set aside for huge pages
448 * and reserve those blocks for 16G huge pages.
450 static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
451 const char *uname, int depth,
452 void *data) {
453 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
454 const __be64 *addr_prop;
455 const __be32 *page_count_prop;
456 unsigned int expected_pages;
457 long unsigned int phys_addr;
458 long unsigned int block_size;
460 /* We are scanning "memory" nodes only */
461 if (type == NULL || strcmp(type, "memory") != 0)
462 return 0;
464 /* This property is the log base 2 of the number of virtual pages that
465 * will represent this memory block. */
466 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
467 if (page_count_prop == NULL)
468 return 0;
469 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
470 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
471 if (addr_prop == NULL)
472 return 0;
473 phys_addr = be64_to_cpu(addr_prop[0]);
474 block_size = be64_to_cpu(addr_prop[1]);
475 if (block_size != (16 * GB))
476 return 0;
477 printk(KERN_INFO "Huge page(16GB) memory: "
478 "addr = 0x%lX size = 0x%lX pages = %d\n",
479 phys_addr, block_size, expected_pages);
480 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
481 memblock_reserve(phys_addr, block_size * expected_pages);
482 add_gpage(phys_addr, block_size, expected_pages);
484 return 0;
486 #endif /* CONFIG_HUGETLB_PAGE */
488 static void mmu_psize_set_default_penc(void)
490 int bpsize, apsize;
491 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
492 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
493 mmu_psize_defs[bpsize].penc[apsize] = -1;
496 #ifdef CONFIG_PPC_64K_PAGES
498 static bool might_have_hea(void)
501 * The HEA ethernet adapter requires awareness of the
502 * GX bus. Without that awareness we can easily assume
503 * we will never see an HEA ethernet device.
505 #ifdef CONFIG_IBMEBUS
506 return !cpu_has_feature(CPU_FTR_ARCH_207S);
507 #else
508 return false;
509 #endif
512 #endif /* #ifdef CONFIG_PPC_64K_PAGES */
514 static void __init htab_init_page_sizes(void)
516 int rc;
518 /* se the invalid penc to -1 */
519 mmu_psize_set_default_penc();
521 /* Default to 4K pages only */
522 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
523 sizeof(mmu_psize_defaults_old));
526 * Try to find the available page sizes in the device-tree
528 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
529 if (rc != 0) /* Found */
530 goto found;
533 * Not in the device-tree, let's fallback on known size
534 * list for 16M capable GP & GR
536 if (mmu_has_feature(MMU_FTR_16M_PAGE))
537 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
538 sizeof(mmu_psize_defaults_gp));
539 found:
540 if (!debug_pagealloc_enabled()) {
542 * Pick a size for the linear mapping. Currently, we only
543 * support 16M, 1M and 4K which is the default
545 if (mmu_psize_defs[MMU_PAGE_16M].shift)
546 mmu_linear_psize = MMU_PAGE_16M;
547 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
548 mmu_linear_psize = MMU_PAGE_1M;
551 #ifdef CONFIG_PPC_64K_PAGES
553 * Pick a size for the ordinary pages. Default is 4K, we support
554 * 64K for user mappings and vmalloc if supported by the processor.
555 * We only use 64k for ioremap if the processor
556 * (and firmware) support cache-inhibited large pages.
557 * If not, we use 4k and set mmu_ci_restrictions so that
558 * hash_page knows to switch processes that use cache-inhibited
559 * mappings to 4k pages.
561 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
562 mmu_virtual_psize = MMU_PAGE_64K;
563 mmu_vmalloc_psize = MMU_PAGE_64K;
564 if (mmu_linear_psize == MMU_PAGE_4K)
565 mmu_linear_psize = MMU_PAGE_64K;
566 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
568 * When running on pSeries using 64k pages for ioremap
569 * would stop us accessing the HEA ethernet. So if we
570 * have the chance of ever seeing one, stay at 4k.
572 if (!might_have_hea() || !machine_is(pseries))
573 mmu_io_psize = MMU_PAGE_64K;
574 } else
575 mmu_ci_restrictions = 1;
577 #endif /* CONFIG_PPC_64K_PAGES */
579 #ifdef CONFIG_SPARSEMEM_VMEMMAP
580 /* We try to use 16M pages for vmemmap if that is supported
581 * and we have at least 1G of RAM at boot
583 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
584 memblock_phys_mem_size() >= 0x40000000)
585 mmu_vmemmap_psize = MMU_PAGE_16M;
586 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
587 mmu_vmemmap_psize = MMU_PAGE_64K;
588 else
589 mmu_vmemmap_psize = MMU_PAGE_4K;
590 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
592 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
593 "virtual = %d, io = %d"
594 #ifdef CONFIG_SPARSEMEM_VMEMMAP
595 ", vmemmap = %d"
596 #endif
597 "\n",
598 mmu_psize_defs[mmu_linear_psize].shift,
599 mmu_psize_defs[mmu_virtual_psize].shift,
600 mmu_psize_defs[mmu_io_psize].shift
601 #ifdef CONFIG_SPARSEMEM_VMEMMAP
602 ,mmu_psize_defs[mmu_vmemmap_psize].shift
603 #endif
606 #ifdef CONFIG_HUGETLB_PAGE
607 /* Reserve 16G huge page memory sections for huge pages */
608 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
609 #endif /* CONFIG_HUGETLB_PAGE */
612 static int __init htab_dt_scan_pftsize(unsigned long node,
613 const char *uname, int depth,
614 void *data)
616 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
617 const __be32 *prop;
619 /* We are scanning "cpu" nodes only */
620 if (type == NULL || strcmp(type, "cpu") != 0)
621 return 0;
623 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
624 if (prop != NULL) {
625 /* pft_size[0] is the NUMA CEC cookie */
626 ppc64_pft_size = be32_to_cpu(prop[1]);
627 return 1;
629 return 0;
632 unsigned htab_shift_for_mem_size(unsigned long mem_size)
634 unsigned memshift = __ilog2(mem_size);
635 unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
636 unsigned pteg_shift;
638 /* round mem_size up to next power of 2 */
639 if ((1UL << memshift) < mem_size)
640 memshift += 1;
642 /* aim for 2 pages / pteg */
643 pteg_shift = memshift - (pshift + 1);
646 * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
647 * size permitted by the architecture.
649 return max(pteg_shift + 7, 18U);
652 static unsigned long __init htab_get_table_size(void)
654 /* If hash size isn't already provided by the platform, we try to
655 * retrieve it from the device-tree. If it's not there neither, we
656 * calculate it now based on the total RAM size
658 if (ppc64_pft_size == 0)
659 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
660 if (ppc64_pft_size)
661 return 1UL << ppc64_pft_size;
663 return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
666 #ifdef CONFIG_MEMORY_HOTPLUG
667 int create_section_mapping(unsigned long start, unsigned long end)
669 int rc = htab_bolt_mapping(start, end, __pa(start),
670 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
671 mmu_kernel_ssize);
673 if (rc < 0) {
674 int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
675 mmu_kernel_ssize);
676 BUG_ON(rc2 && (rc2 != -ENOENT));
678 return rc;
681 int remove_section_mapping(unsigned long start, unsigned long end)
683 int rc = htab_remove_mapping(start, end, mmu_linear_psize,
684 mmu_kernel_ssize);
685 WARN_ON(rc < 0);
686 return rc;
688 #endif /* CONFIG_MEMORY_HOTPLUG */
690 static void __init htab_initialize(void)
692 unsigned long table;
693 unsigned long pteg_count;
694 unsigned long prot;
695 unsigned long base = 0, size = 0, limit;
696 struct memblock_region *reg;
698 DBG(" -> htab_initialize()\n");
700 /* Initialize segment sizes */
701 htab_init_seg_sizes();
703 /* Initialize page sizes */
704 htab_init_page_sizes();
706 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
707 mmu_kernel_ssize = MMU_SEGSIZE_1T;
708 mmu_highuser_ssize = MMU_SEGSIZE_1T;
709 printk(KERN_INFO "Using 1TB segments\n");
713 * Calculate the required size of the htab. We want the number of
714 * PTEGs to equal one half the number of real pages.
716 htab_size_bytes = htab_get_table_size();
717 pteg_count = htab_size_bytes >> 7;
719 htab_hash_mask = pteg_count - 1;
721 if (firmware_has_feature(FW_FEATURE_LPAR)) {
722 /* Using a hypervisor which owns the htab */
723 htab_address = NULL;
724 _SDR1 = 0;
725 #ifdef CONFIG_FA_DUMP
727 * If firmware assisted dump is active firmware preserves
728 * the contents of htab along with entire partition memory.
729 * Clear the htab if firmware assisted dump is active so
730 * that we dont end up using old mappings.
732 if (is_fadump_active() && ppc_md.hpte_clear_all)
733 ppc_md.hpte_clear_all();
734 #endif
735 } else {
736 /* Find storage for the HPT. Must be contiguous in
737 * the absolute address space. On cell we want it to be
738 * in the first 2 Gig so we can use it for IOMMU hacks.
740 if (machine_is(cell))
741 limit = 0x80000000;
742 else
743 limit = MEMBLOCK_ALLOC_ANYWHERE;
745 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
747 DBG("Hash table allocated at %lx, size: %lx\n", table,
748 htab_size_bytes);
750 htab_address = __va(table);
752 /* htab absolute addr + encoded htabsize */
753 _SDR1 = table + __ilog2(pteg_count) - 11;
755 /* Initialize the HPT with no entries */
756 memset((void *)table, 0, htab_size_bytes);
758 /* Set SDR1 */
759 mtspr(SPRN_SDR1, _SDR1);
762 prot = pgprot_val(PAGE_KERNEL);
764 #ifdef CONFIG_DEBUG_PAGEALLOC
765 if (debug_pagealloc_enabled()) {
766 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
767 linear_map_hash_slots = __va(memblock_alloc_base(
768 linear_map_hash_count, 1, ppc64_rma_size));
769 memset(linear_map_hash_slots, 0, linear_map_hash_count);
771 #endif /* CONFIG_DEBUG_PAGEALLOC */
773 /* On U3 based machines, we need to reserve the DART area and
774 * _NOT_ map it to avoid cache paradoxes as it's remapped non
775 * cacheable later on
778 /* create bolted the linear mapping in the hash table */
779 for_each_memblock(memory, reg) {
780 base = (unsigned long)__va(reg->base);
781 size = reg->size;
783 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
784 base, size, prot);
786 #ifdef CONFIG_U3_DART
787 /* Do not map the DART space. Fortunately, it will be aligned
788 * in such a way that it will not cross two memblock regions and
789 * will fit within a single 16Mb page.
790 * The DART space is assumed to be a full 16Mb region even if
791 * we only use 2Mb of that space. We will use more of it later
792 * for AGP GART. We have to use a full 16Mb large page.
794 DBG("DART base: %lx\n", dart_tablebase);
796 if (dart_tablebase != 0 && dart_tablebase >= base
797 && dart_tablebase < (base + size)) {
798 unsigned long dart_table_end = dart_tablebase + 16 * MB;
799 if (base != dart_tablebase)
800 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
801 __pa(base), prot,
802 mmu_linear_psize,
803 mmu_kernel_ssize));
804 if ((base + size) > dart_table_end)
805 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
806 base + size,
807 __pa(dart_table_end),
808 prot,
809 mmu_linear_psize,
810 mmu_kernel_ssize));
811 continue;
813 #endif /* CONFIG_U3_DART */
814 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
815 prot, mmu_linear_psize, mmu_kernel_ssize));
817 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
820 * If we have a memory_limit and we've allocated TCEs then we need to
821 * explicitly map the TCE area at the top of RAM. We also cope with the
822 * case that the TCEs start below memory_limit.
823 * tce_alloc_start/end are 16MB aligned so the mapping should work
824 * for either 4K or 16MB pages.
826 if (tce_alloc_start) {
827 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
828 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
830 if (base + size >= tce_alloc_start)
831 tce_alloc_start = base + size + 1;
833 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
834 __pa(tce_alloc_start), prot,
835 mmu_linear_psize, mmu_kernel_ssize));
839 DBG(" <- htab_initialize()\n");
841 #undef KB
842 #undef MB
844 void __init early_init_mmu(void)
846 /* Initialize the MMU Hash table and create the linear mapping
847 * of memory. Has to be done before SLB initialization as this is
848 * currently where the page size encoding is obtained.
850 htab_initialize();
852 /* Initialize SLB management */
853 slb_initialize();
856 #ifdef CONFIG_SMP
857 void early_init_mmu_secondary(void)
859 /* Initialize hash table for that CPU */
860 if (!firmware_has_feature(FW_FEATURE_LPAR))
861 mtspr(SPRN_SDR1, _SDR1);
863 /* Initialize SLB */
864 slb_initialize();
866 #endif /* CONFIG_SMP */
869 * Called by asm hashtable.S for doing lazy icache flush
871 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
873 struct page *page;
875 if (!pfn_valid(pte_pfn(pte)))
876 return pp;
878 page = pte_page(pte);
880 /* page is dirty */
881 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
882 if (trap == 0x400) {
883 flush_dcache_icache_page(page);
884 set_bit(PG_arch_1, &page->flags);
885 } else
886 pp |= HPTE_R_N;
888 return pp;
891 #ifdef CONFIG_PPC_MM_SLICES
892 static unsigned int get_paca_psize(unsigned long addr)
894 u64 lpsizes;
895 unsigned char *hpsizes;
896 unsigned long index, mask_index;
898 if (addr < SLICE_LOW_TOP) {
899 lpsizes = get_paca()->mm_ctx_low_slices_psize;
900 index = GET_LOW_SLICE_INDEX(addr);
901 return (lpsizes >> (index * 4)) & 0xF;
903 hpsizes = get_paca()->mm_ctx_high_slices_psize;
904 index = GET_HIGH_SLICE_INDEX(addr);
905 mask_index = index & 0x1;
906 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
909 #else
910 unsigned int get_paca_psize(unsigned long addr)
912 return get_paca()->mm_ctx_user_psize;
914 #endif
917 * Demote a segment to using 4k pages.
918 * For now this makes the whole process use 4k pages.
920 #ifdef CONFIG_PPC_64K_PAGES
921 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
923 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
924 return;
925 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
926 copro_flush_all_slbs(mm);
927 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
929 copy_mm_to_paca(&mm->context);
930 slb_flush_and_rebolt();
933 #endif /* CONFIG_PPC_64K_PAGES */
935 #ifdef CONFIG_PPC_SUBPAGE_PROT
937 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
938 * Userspace sets the subpage permissions using the subpage_prot system call.
940 * Result is 0: full permissions, _PAGE_RW: read-only,
941 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
943 static int subpage_protection(struct mm_struct *mm, unsigned long ea)
945 struct subpage_prot_table *spt = &mm->context.spt;
946 u32 spp = 0;
947 u32 **sbpm, *sbpp;
949 if (ea >= spt->maxaddr)
950 return 0;
951 if (ea < 0x100000000UL) {
952 /* addresses below 4GB use spt->low_prot */
953 sbpm = spt->low_prot;
954 } else {
955 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
956 if (!sbpm)
957 return 0;
959 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
960 if (!sbpp)
961 return 0;
962 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
964 /* extract 2-bit bitfield for this 4k subpage */
965 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
967 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
968 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
969 return spp;
972 #else /* CONFIG_PPC_SUBPAGE_PROT */
973 static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
975 return 0;
977 #endif
979 void hash_failure_debug(unsigned long ea, unsigned long access,
980 unsigned long vsid, unsigned long trap,
981 int ssize, int psize, int lpsize, unsigned long pte)
983 if (!printk_ratelimit())
984 return;
985 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
986 ea, access, current->comm);
987 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
988 trap, vsid, ssize, psize, lpsize, pte);
991 static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
992 int psize, bool user_region)
994 if (user_region) {
995 if (psize != get_paca_psize(ea)) {
996 copy_mm_to_paca(&mm->context);
997 slb_flush_and_rebolt();
999 } else if (get_paca()->vmalloc_sllp !=
1000 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
1001 get_paca()->vmalloc_sllp =
1002 mmu_psize_defs[mmu_vmalloc_psize].sllp;
1003 slb_vmalloc_update();
1007 /* Result code is:
1008 * 0 - handled
1009 * 1 - normal page fault
1010 * -1 - critical hash insertion error
1011 * -2 - access not permitted by subpage protection mechanism
1013 int hash_page_mm(struct mm_struct *mm, unsigned long ea,
1014 unsigned long access, unsigned long trap,
1015 unsigned long flags)
1017 bool is_thp;
1018 enum ctx_state prev_state = exception_enter();
1019 pgd_t *pgdir;
1020 unsigned long vsid;
1021 pte_t *ptep;
1022 unsigned hugeshift;
1023 const struct cpumask *tmp;
1024 int rc, user_region = 0;
1025 int psize, ssize;
1027 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1028 ea, access, trap);
1029 trace_hash_fault(ea, access, trap);
1031 /* Get region & vsid */
1032 switch (REGION_ID(ea)) {
1033 case USER_REGION_ID:
1034 user_region = 1;
1035 if (! mm) {
1036 DBG_LOW(" user region with no mm !\n");
1037 rc = 1;
1038 goto bail;
1040 psize = get_slice_psize(mm, ea);
1041 ssize = user_segment_size(ea);
1042 vsid = get_vsid(mm->context.id, ea, ssize);
1043 break;
1044 case VMALLOC_REGION_ID:
1045 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
1046 if (ea < VMALLOC_END)
1047 psize = mmu_vmalloc_psize;
1048 else
1049 psize = mmu_io_psize;
1050 ssize = mmu_kernel_ssize;
1051 break;
1052 default:
1053 /* Not a valid range
1054 * Send the problem up to do_page_fault
1056 rc = 1;
1057 goto bail;
1059 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
1061 /* Bad address. */
1062 if (!vsid) {
1063 DBG_LOW("Bad address!\n");
1064 rc = 1;
1065 goto bail;
1067 /* Get pgdir */
1068 pgdir = mm->pgd;
1069 if (pgdir == NULL) {
1070 rc = 1;
1071 goto bail;
1074 /* Check CPU locality */
1075 tmp = cpumask_of(smp_processor_id());
1076 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
1077 flags |= HPTE_LOCAL_UPDATE;
1079 #ifndef CONFIG_PPC_64K_PAGES
1080 /* If we use 4K pages and our psize is not 4K, then we might
1081 * be hitting a special driver mapping, and need to align the
1082 * address before we fetch the PTE.
1084 * It could also be a hugepage mapping, in which case this is
1085 * not necessary, but it's not harmful, either.
1087 if (psize != MMU_PAGE_4K)
1088 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1089 #endif /* CONFIG_PPC_64K_PAGES */
1091 /* Get PTE and page size from page tables */
1092 ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
1093 if (ptep == NULL || !pte_present(*ptep)) {
1094 DBG_LOW(" no PTE !\n");
1095 rc = 1;
1096 goto bail;
1099 /* Add _PAGE_PRESENT to the required access perm */
1100 access |= _PAGE_PRESENT;
1102 /* Pre-check access permissions (will be re-checked atomically
1103 * in __hash_page_XX but this pre-check is a fast path
1105 if (access & ~pte_val(*ptep)) {
1106 DBG_LOW(" no access !\n");
1107 rc = 1;
1108 goto bail;
1111 if (hugeshift) {
1112 if (is_thp)
1113 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
1114 trap, flags, ssize, psize);
1115 #ifdef CONFIG_HUGETLB_PAGE
1116 else
1117 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
1118 flags, ssize, hugeshift, psize);
1119 #else
1120 else {
1122 * if we have hugeshift, and is not transhuge with
1123 * hugetlb disabled, something is really wrong.
1125 rc = 1;
1126 WARN_ON(1);
1128 #endif
1129 if (current->mm == mm)
1130 check_paca_psize(ea, mm, psize, user_region);
1132 goto bail;
1135 #ifndef CONFIG_PPC_64K_PAGES
1136 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1137 #else
1138 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1139 pte_val(*(ptep + PTRS_PER_PTE)));
1140 #endif
1141 /* Do actual hashing */
1142 #ifdef CONFIG_PPC_64K_PAGES
1143 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
1144 if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
1145 demote_segment_4k(mm, ea);
1146 psize = MMU_PAGE_4K;
1149 /* If this PTE is non-cacheable and we have restrictions on
1150 * using non cacheable large pages, then we switch to 4k
1152 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
1153 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
1154 if (user_region) {
1155 demote_segment_4k(mm, ea);
1156 psize = MMU_PAGE_4K;
1157 } else if (ea < VMALLOC_END) {
1159 * some driver did a non-cacheable mapping
1160 * in vmalloc space, so switch vmalloc
1161 * to 4k pages
1163 printk(KERN_ALERT "Reducing vmalloc segment "
1164 "to 4kB pages because of "
1165 "non-cacheable mapping\n");
1166 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
1167 copro_flush_all_slbs(mm);
1171 #endif /* CONFIG_PPC_64K_PAGES */
1173 if (current->mm == mm)
1174 check_paca_psize(ea, mm, psize, user_region);
1176 #ifdef CONFIG_PPC_64K_PAGES
1177 if (psize == MMU_PAGE_64K)
1178 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1179 flags, ssize);
1180 else
1181 #endif /* CONFIG_PPC_64K_PAGES */
1183 int spp = subpage_protection(mm, ea);
1184 if (access & spp)
1185 rc = -2;
1186 else
1187 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
1188 flags, ssize, spp);
1191 /* Dump some info in case of hash insertion failure, they should
1192 * never happen so it is really useful to know if/when they do
1194 if (rc == -1)
1195 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
1196 psize, pte_val(*ptep));
1197 #ifndef CONFIG_PPC_64K_PAGES
1198 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1199 #else
1200 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1201 pte_val(*(ptep + PTRS_PER_PTE)));
1202 #endif
1203 DBG_LOW(" -> rc=%d\n", rc);
1205 bail:
1206 exception_exit(prev_state);
1207 return rc;
1209 EXPORT_SYMBOL_GPL(hash_page_mm);
1211 int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
1212 unsigned long dsisr)
1214 unsigned long flags = 0;
1215 struct mm_struct *mm = current->mm;
1217 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1218 mm = &init_mm;
1220 if (dsisr & DSISR_NOHPTE)
1221 flags |= HPTE_NOHPTE_UPDATE;
1223 return hash_page_mm(mm, ea, access, trap, flags);
1225 EXPORT_SYMBOL_GPL(hash_page);
1227 int __hash_page(unsigned long ea, unsigned long msr, unsigned long trap,
1228 unsigned long dsisr)
1230 unsigned long access = _PAGE_PRESENT;
1231 unsigned long flags = 0;
1232 struct mm_struct *mm = current->mm;
1234 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1235 mm = &init_mm;
1237 if (dsisr & DSISR_NOHPTE)
1238 flags |= HPTE_NOHPTE_UPDATE;
1240 if (dsisr & DSISR_ISSTORE)
1241 access |= _PAGE_RW;
1243 * We need to set the _PAGE_USER bit if MSR_PR is set or if we are
1244 * accessing a userspace segment (even from the kernel). We assume
1245 * kernel addresses always have the high bit set.
1247 if ((msr & MSR_PR) || (REGION_ID(ea) == USER_REGION_ID))
1248 access |= _PAGE_USER;
1250 if (trap == 0x400)
1251 access |= _PAGE_EXEC;
1253 return hash_page_mm(mm, ea, access, trap, flags);
1256 void hash_preload(struct mm_struct *mm, unsigned long ea,
1257 unsigned long access, unsigned long trap)
1259 int hugepage_shift;
1260 unsigned long vsid;
1261 pgd_t *pgdir;
1262 pte_t *ptep;
1263 unsigned long flags;
1264 int rc, ssize, update_flags = 0;
1266 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1268 #ifdef CONFIG_PPC_MM_SLICES
1269 /* We only prefault standard pages for now */
1270 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
1271 return;
1272 #endif
1274 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1275 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1277 /* Get Linux PTE if available */
1278 pgdir = mm->pgd;
1279 if (pgdir == NULL)
1280 return;
1282 /* Get VSID */
1283 ssize = user_segment_size(ea);
1284 vsid = get_vsid(mm->context.id, ea, ssize);
1285 if (!vsid)
1286 return;
1288 * Hash doesn't like irqs. Walking linux page table with irq disabled
1289 * saves us from holding multiple locks.
1291 local_irq_save(flags);
1294 * THP pages use update_mmu_cache_pmd. We don't do
1295 * hash preload there. Hence can ignore THP here
1297 ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
1298 if (!ptep)
1299 goto out_exit;
1301 WARN_ON(hugepage_shift);
1302 #ifdef CONFIG_PPC_64K_PAGES
1303 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
1304 * a 64K kernel), then we don't preload, hash_page() will take
1305 * care of it once we actually try to access the page.
1306 * That way we don't have to duplicate all of the logic for segment
1307 * page size demotion here
1309 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
1310 goto out_exit;
1311 #endif /* CONFIG_PPC_64K_PAGES */
1313 /* Is that local to this CPU ? */
1314 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1315 update_flags |= HPTE_LOCAL_UPDATE;
1317 /* Hash it in */
1318 #ifdef CONFIG_PPC_64K_PAGES
1319 if (mm->context.user_psize == MMU_PAGE_64K)
1320 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1321 update_flags, ssize);
1322 else
1323 #endif /* CONFIG_PPC_64K_PAGES */
1324 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
1325 ssize, subpage_protection(mm, ea));
1327 /* Dump some info in case of hash insertion failure, they should
1328 * never happen so it is really useful to know if/when they do
1330 if (rc == -1)
1331 hash_failure_debug(ea, access, vsid, trap, ssize,
1332 mm->context.user_psize,
1333 mm->context.user_psize,
1334 pte_val(*ptep));
1335 out_exit:
1336 local_irq_restore(flags);
1339 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
1340 * do not forget to update the assembly call site !
1342 void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
1343 unsigned long flags)
1345 unsigned long hash, index, shift, hidx, slot;
1346 int local = flags & HPTE_LOCAL_UPDATE;
1348 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1349 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1350 hash = hpt_hash(vpn, shift, ssize);
1351 hidx = __rpte_to_hidx(pte, index);
1352 if (hidx & _PTEIDX_SECONDARY)
1353 hash = ~hash;
1354 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1355 slot += hidx & _PTEIDX_GROUP_IX;
1356 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
1358 * We use same base page size and actual psize, because we don't
1359 * use these functions for hugepage
1361 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
1362 } pte_iterate_hashed_end();
1364 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1365 /* Transactions are not aborted by tlbiel, only tlbie.
1366 * Without, syncing a page back to a block device w/ PIO could pick up
1367 * transactional data (bad!) so we force an abort here. Before the
1368 * sync the page will be made read-only, which will flush_hash_page.
1369 * BIG ISSUE here: if the kernel uses a page from userspace without
1370 * unmapping it first, it may see the speculated version.
1372 if (local && cpu_has_feature(CPU_FTR_TM) &&
1373 current->thread.regs &&
1374 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1375 tm_enable();
1376 tm_abort(TM_CAUSE_TLBI);
1378 #endif
1381 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1382 void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
1383 pmd_t *pmdp, unsigned int psize, int ssize,
1384 unsigned long flags)
1386 int i, max_hpte_count, valid;
1387 unsigned long s_addr;
1388 unsigned char *hpte_slot_array;
1389 unsigned long hidx, shift, vpn, hash, slot;
1390 int local = flags & HPTE_LOCAL_UPDATE;
1392 s_addr = addr & HPAGE_PMD_MASK;
1393 hpte_slot_array = get_hpte_slot_array(pmdp);
1395 * IF we try to do a HUGE PTE update after a withdraw is done.
1396 * we will find the below NULL. This happens when we do
1397 * split_huge_page_pmd
1399 if (!hpte_slot_array)
1400 return;
1402 if (ppc_md.hugepage_invalidate) {
1403 ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
1404 psize, ssize, local);
1405 goto tm_abort;
1408 * No bluk hpte removal support, invalidate each entry
1410 shift = mmu_psize_defs[psize].shift;
1411 max_hpte_count = HPAGE_PMD_SIZE >> shift;
1412 for (i = 0; i < max_hpte_count; i++) {
1414 * 8 bits per each hpte entries
1415 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
1417 valid = hpte_valid(hpte_slot_array, i);
1418 if (!valid)
1419 continue;
1420 hidx = hpte_hash_index(hpte_slot_array, i);
1422 /* get the vpn */
1423 addr = s_addr + (i * (1ul << shift));
1424 vpn = hpt_vpn(addr, vsid, ssize);
1425 hash = hpt_hash(vpn, shift, ssize);
1426 if (hidx & _PTEIDX_SECONDARY)
1427 hash = ~hash;
1429 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1430 slot += hidx & _PTEIDX_GROUP_IX;
1431 ppc_md.hpte_invalidate(slot, vpn, psize,
1432 MMU_PAGE_16M, ssize, local);
1434 tm_abort:
1435 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1436 /* Transactions are not aborted by tlbiel, only tlbie.
1437 * Without, syncing a page back to a block device w/ PIO could pick up
1438 * transactional data (bad!) so we force an abort here. Before the
1439 * sync the page will be made read-only, which will flush_hash_page.
1440 * BIG ISSUE here: if the kernel uses a page from userspace without
1441 * unmapping it first, it may see the speculated version.
1443 if (local && cpu_has_feature(CPU_FTR_TM) &&
1444 current->thread.regs &&
1445 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1446 tm_enable();
1447 tm_abort(TM_CAUSE_TLBI);
1449 #endif
1450 return;
1452 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1454 void flush_hash_range(unsigned long number, int local)
1456 if (ppc_md.flush_hash_range)
1457 ppc_md.flush_hash_range(number, local);
1458 else {
1459 int i;
1460 struct ppc64_tlb_batch *batch =
1461 this_cpu_ptr(&ppc64_tlb_batch);
1463 for (i = 0; i < number; i++)
1464 flush_hash_page(batch->vpn[i], batch->pte[i],
1465 batch->psize, batch->ssize, local);
1470 * low_hash_fault is called when we the low level hash code failed
1471 * to instert a PTE due to an hypervisor error
1473 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1475 enum ctx_state prev_state = exception_enter();
1477 if (user_mode(regs)) {
1478 #ifdef CONFIG_PPC_SUBPAGE_PROT
1479 if (rc == -2)
1480 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1481 else
1482 #endif
1483 _exception(SIGBUS, regs, BUS_ADRERR, address);
1484 } else
1485 bad_page_fault(regs, address, SIGBUS);
1487 exception_exit(prev_state);
1490 long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1491 unsigned long pa, unsigned long rflags,
1492 unsigned long vflags, int psize, int ssize)
1494 unsigned long hpte_group;
1495 long slot;
1497 repeat:
1498 hpte_group = ((hash & htab_hash_mask) *
1499 HPTES_PER_GROUP) & ~0x7UL;
1501 /* Insert into the hash table, primary slot */
1502 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
1503 psize, psize, ssize);
1505 /* Primary is full, try the secondary */
1506 if (unlikely(slot == -1)) {
1507 hpte_group = ((~hash & htab_hash_mask) *
1508 HPTES_PER_GROUP) & ~0x7UL;
1509 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1510 vflags | HPTE_V_SECONDARY,
1511 psize, psize, ssize);
1512 if (slot == -1) {
1513 if (mftb() & 0x1)
1514 hpte_group = ((hash & htab_hash_mask) *
1515 HPTES_PER_GROUP)&~0x7UL;
1517 ppc_md.hpte_remove(hpte_group);
1518 goto repeat;
1522 return slot;
1525 #ifdef CONFIG_DEBUG_PAGEALLOC
1526 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1528 unsigned long hash;
1529 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1530 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1531 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
1532 long ret;
1534 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1536 /* Don't create HPTE entries for bad address */
1537 if (!vsid)
1538 return;
1540 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1541 HPTE_V_BOLTED,
1542 mmu_linear_psize, mmu_kernel_ssize);
1544 BUG_ON (ret < 0);
1545 spin_lock(&linear_map_hash_lock);
1546 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1547 linear_map_hash_slots[lmi] = ret | 0x80;
1548 spin_unlock(&linear_map_hash_lock);
1551 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1553 unsigned long hash, hidx, slot;
1554 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1555 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1557 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1558 spin_lock(&linear_map_hash_lock);
1559 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1560 hidx = linear_map_hash_slots[lmi] & 0x7f;
1561 linear_map_hash_slots[lmi] = 0;
1562 spin_unlock(&linear_map_hash_lock);
1563 if (hidx & _PTEIDX_SECONDARY)
1564 hash = ~hash;
1565 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1566 slot += hidx & _PTEIDX_GROUP_IX;
1567 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1568 mmu_kernel_ssize, 0);
1571 void __kernel_map_pages(struct page *page, int numpages, int enable)
1573 unsigned long flags, vaddr, lmi;
1574 int i;
1576 local_irq_save(flags);
1577 for (i = 0; i < numpages; i++, page++) {
1578 vaddr = (unsigned long)page_address(page);
1579 lmi = __pa(vaddr) >> PAGE_SHIFT;
1580 if (lmi >= linear_map_hash_count)
1581 continue;
1582 if (enable)
1583 kernel_map_linear_page(vaddr, lmi);
1584 else
1585 kernel_unmap_linear_page(vaddr, lmi);
1587 local_irq_restore(flags);
1589 #endif /* CONFIG_DEBUG_PAGEALLOC */
1591 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
1592 phys_addr_t first_memblock_size)
1594 /* We don't currently support the first MEMBLOCK not mapping 0
1595 * physical on those processors
1597 BUG_ON(first_memblock_base != 0);
1599 /* On LPAR systems, the first entry is our RMA region,
1600 * non-LPAR 64-bit hash MMU systems don't have a limitation
1601 * on real mode access, but using the first entry works well
1602 * enough. We also clamp it to 1G to avoid some funky things
1603 * such as RTAS bugs etc...
1605 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1607 /* Finally limit subsequent allocations */
1608 memblock_set_current_limit(ppc64_rma_size);