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
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.
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>
41 #include <asm/mmu_context.h>
43 #include <asm/types.h>
44 #include <asm/uaccess.h>
45 #include <asm/machdep.h>
47 #include <asm/tlbflush.h>
51 #include <asm/cacheflush.h>
52 #include <asm/cputable.h>
53 #include <asm/sections.h>
54 #include <asm/copro.h>
56 #include <asm/code-patching.h>
57 #include <asm/fadump.h>
58 #include <asm/firmware.h>
60 #include <asm/trace.h>
63 #define DBG(fmt...) udbg_printf(fmt)
69 #define DBG_LOW(fmt...) udbg_printf(fmt)
71 #define DBG_LOW(fmt...)
79 * Note: pte --> Linux PTE
80 * HPTE --> PowerPC Hashed Page Table Entry
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.
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;
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;
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[] = {
134 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
140 /* POWER4, GPUL, POWER5
142 * Support for 16Mb large pages
144 static struct mmu_psize_def mmu_psize_defaults_gp[] = {
148 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
155 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
156 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
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)
184 * Linux uses slb key 0 for kernel and 1 for user.
185 * kernel RW areas are mapped with PPP=0b000
186 * User area is mapped with PPP=0b010 for read/write
187 * or PPP=0b011 for read-only (including writeable but clean pages).
189 if (pteflags & _PAGE_PRIVILEGED) {
191 * Kernel read only mapped with ppp bits 0b110
193 if (!(pteflags & _PAGE_WRITE))
194 rflags |= (HPTE_R_PP0 | 0x2);
196 if (pteflags & _PAGE_RWX)
198 if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY)))
202 * We can't allow hardware to update hpte bits. Hence always
203 * set 'R' bit and set 'C' if it is a write fault
204 * Memory coherence is always enabled
206 rflags |= HPTE_R_R | HPTE_R_M;
208 if (pteflags & _PAGE_DIRTY)
214 if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT)
216 if ((pteflags & _PAGE_CACHE_CTL ) == _PAGE_NON_IDEMPOTENT)
217 rflags |= (HPTE_R_I | HPTE_R_G);
218 if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO)
219 rflags |= (HPTE_R_I | HPTE_R_W);
224 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
225 unsigned long pstart, unsigned long prot,
226 int psize, int ssize)
228 unsigned long vaddr, paddr;
229 unsigned int step, shift;
232 shift = mmu_psize_defs[psize].shift;
235 prot = htab_convert_pte_flags(prot);
237 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
238 vstart, vend, pstart, prot, psize, ssize);
240 for (vaddr = vstart, paddr = pstart; vaddr < vend;
241 vaddr += step, paddr += step) {
242 unsigned long hash, hpteg;
243 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
244 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
245 unsigned long tprot = prot;
248 * If we hit a bad address return error.
252 /* Make kernel text executable */
253 if (overlaps_kernel_text(vaddr, vaddr + step))
256 /* Make kvm guest trampolines executable */
257 if (overlaps_kvm_tmp(vaddr, vaddr + step))
261 * If relocatable, check if it overlaps interrupt vectors that
262 * are copied down to real 0. For relocatable kernel
263 * (e.g. kdump case) we copy interrupt vectors down to real
264 * address 0. Mark that region as executable. This is
265 * because on p8 system with relocation on exception feature
266 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
267 * in order to execute the interrupt handlers in virtual
268 * mode the vector region need to be marked as executable.
270 if ((PHYSICAL_START > MEMORY_START) &&
271 overlaps_interrupt_vector_text(vaddr, vaddr + step))
274 hash = hpt_hash(vpn, shift, ssize);
275 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
277 BUG_ON(!ppc_md.hpte_insert);
278 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
279 HPTE_V_BOLTED, psize, psize, ssize);
284 #ifdef CONFIG_DEBUG_PAGEALLOC
285 if (debug_pagealloc_enabled() &&
286 (paddr >> PAGE_SHIFT) < linear_map_hash_count)
287 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
288 #endif /* CONFIG_DEBUG_PAGEALLOC */
290 return ret < 0 ? ret : 0;
293 int htab_remove_mapping(unsigned long vstart, unsigned long vend,
294 int psize, int ssize)
297 unsigned int step, shift;
301 shift = mmu_psize_defs[psize].shift;
304 if (!ppc_md.hpte_removebolted)
307 for (vaddr = vstart; vaddr < vend; vaddr += step) {
308 rc = ppc_md.hpte_removebolted(vaddr, psize, ssize);
320 static int __init htab_dt_scan_seg_sizes(unsigned long node,
321 const char *uname, int depth,
324 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
328 /* We are scanning "cpu" nodes only */
329 if (type == NULL || strcmp(type, "cpu") != 0)
332 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
335 for (; size >= 4; size -= 4, ++prop) {
336 if (be32_to_cpu(prop[0]) == 40) {
337 DBG("1T segment support detected\n");
338 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
342 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
346 static void __init htab_init_seg_sizes(void)
348 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
351 static int __init get_idx_from_shift(unsigned int shift)
375 static int __init htab_dt_scan_page_sizes(unsigned long node,
376 const char *uname, int depth,
379 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
383 /* We are scanning "cpu" nodes only */
384 if (type == NULL || strcmp(type, "cpu") != 0)
387 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
391 pr_info("Page sizes from device-tree:\n");
393 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
395 unsigned int base_shift = be32_to_cpu(prop[0]);
396 unsigned int slbenc = be32_to_cpu(prop[1]);
397 unsigned int lpnum = be32_to_cpu(prop[2]);
398 struct mmu_psize_def *def;
401 size -= 3; prop += 3;
402 base_idx = get_idx_from_shift(base_shift);
404 /* skip the pte encoding also */
405 prop += lpnum * 2; size -= lpnum * 2;
408 def = &mmu_psize_defs[base_idx];
409 if (base_idx == MMU_PAGE_16M)
410 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
412 def->shift = base_shift;
413 if (base_shift <= 23)
416 def->avpnm = (1 << (base_shift - 23)) - 1;
419 * We don't know for sure what's up with tlbiel, so
420 * for now we only set it for 4K and 64K pages
422 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
427 while (size > 0 && lpnum) {
428 unsigned int shift = be32_to_cpu(prop[0]);
429 int penc = be32_to_cpu(prop[1]);
431 prop += 2; size -= 2;
434 idx = get_idx_from_shift(shift);
439 pr_err("Invalid penc for base_shift=%d "
440 "shift=%d\n", base_shift, shift);
442 def->penc[idx] = penc;
443 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
444 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
445 base_shift, shift, def->sllp,
446 def->avpnm, def->tlbiel, def->penc[idx]);
453 #ifdef CONFIG_HUGETLB_PAGE
454 /* Scan for 16G memory blocks that have been set aside for huge pages
455 * and reserve those blocks for 16G huge pages.
457 static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
458 const char *uname, int depth,
460 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
461 const __be64 *addr_prop;
462 const __be32 *page_count_prop;
463 unsigned int expected_pages;
464 long unsigned int phys_addr;
465 long unsigned int block_size;
467 /* We are scanning "memory" nodes only */
468 if (type == NULL || strcmp(type, "memory") != 0)
471 /* This property is the log base 2 of the number of virtual pages that
472 * will represent this memory block. */
473 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
474 if (page_count_prop == NULL)
476 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
477 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
478 if (addr_prop == NULL)
480 phys_addr = be64_to_cpu(addr_prop[0]);
481 block_size = be64_to_cpu(addr_prop[1]);
482 if (block_size != (16 * GB))
484 printk(KERN_INFO "Huge page(16GB) memory: "
485 "addr = 0x%lX size = 0x%lX pages = %d\n",
486 phys_addr, block_size, expected_pages);
487 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
488 memblock_reserve(phys_addr, block_size * expected_pages);
489 add_gpage(phys_addr, block_size, expected_pages);
493 #endif /* CONFIG_HUGETLB_PAGE */
495 static void mmu_psize_set_default_penc(void)
498 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
499 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
500 mmu_psize_defs[bpsize].penc[apsize] = -1;
503 #ifdef CONFIG_PPC_64K_PAGES
505 static bool might_have_hea(void)
508 * The HEA ethernet adapter requires awareness of the
509 * GX bus. Without that awareness we can easily assume
510 * we will never see an HEA ethernet device.
512 #ifdef CONFIG_IBMEBUS
513 return !cpu_has_feature(CPU_FTR_ARCH_207S);
519 #endif /* #ifdef CONFIG_PPC_64K_PAGES */
521 static void __init htab_init_page_sizes(void)
525 /* se the invalid penc to -1 */
526 mmu_psize_set_default_penc();
528 /* Default to 4K pages only */
529 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
530 sizeof(mmu_psize_defaults_old));
533 * Try to find the available page sizes in the device-tree
535 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
536 if (rc != 0) /* Found */
540 * Not in the device-tree, let's fallback on known size
541 * list for 16M capable GP & GR
543 if (mmu_has_feature(MMU_FTR_16M_PAGE))
544 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
545 sizeof(mmu_psize_defaults_gp));
547 if (!debug_pagealloc_enabled()) {
549 * Pick a size for the linear mapping. Currently, we only
550 * support 16M, 1M and 4K which is the default
552 if (mmu_psize_defs[MMU_PAGE_16M].shift)
553 mmu_linear_psize = MMU_PAGE_16M;
554 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
555 mmu_linear_psize = MMU_PAGE_1M;
558 #ifdef CONFIG_PPC_64K_PAGES
560 * Pick a size for the ordinary pages. Default is 4K, we support
561 * 64K for user mappings and vmalloc if supported by the processor.
562 * We only use 64k for ioremap if the processor
563 * (and firmware) support cache-inhibited large pages.
564 * If not, we use 4k and set mmu_ci_restrictions so that
565 * hash_page knows to switch processes that use cache-inhibited
566 * mappings to 4k pages.
568 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
569 mmu_virtual_psize = MMU_PAGE_64K;
570 mmu_vmalloc_psize = MMU_PAGE_64K;
571 if (mmu_linear_psize == MMU_PAGE_4K)
572 mmu_linear_psize = MMU_PAGE_64K;
573 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
575 * When running on pSeries using 64k pages for ioremap
576 * would stop us accessing the HEA ethernet. So if we
577 * have the chance of ever seeing one, stay at 4k.
579 if (!might_have_hea() || !machine_is(pseries))
580 mmu_io_psize = MMU_PAGE_64K;
582 mmu_ci_restrictions = 1;
584 #endif /* CONFIG_PPC_64K_PAGES */
586 #ifdef CONFIG_SPARSEMEM_VMEMMAP
587 /* We try to use 16M pages for vmemmap if that is supported
588 * and we have at least 1G of RAM at boot
590 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
591 memblock_phys_mem_size() >= 0x40000000)
592 mmu_vmemmap_psize = MMU_PAGE_16M;
593 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
594 mmu_vmemmap_psize = MMU_PAGE_64K;
596 mmu_vmemmap_psize = MMU_PAGE_4K;
597 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
599 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
600 "virtual = %d, io = %d"
601 #ifdef CONFIG_SPARSEMEM_VMEMMAP
605 mmu_psize_defs[mmu_linear_psize].shift,
606 mmu_psize_defs[mmu_virtual_psize].shift,
607 mmu_psize_defs[mmu_io_psize].shift
608 #ifdef CONFIG_SPARSEMEM_VMEMMAP
609 ,mmu_psize_defs[mmu_vmemmap_psize].shift
613 #ifdef CONFIG_HUGETLB_PAGE
614 /* Reserve 16G huge page memory sections for huge pages */
615 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
616 #endif /* CONFIG_HUGETLB_PAGE */
619 static int __init htab_dt_scan_pftsize(unsigned long node,
620 const char *uname, int depth,
623 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
626 /* We are scanning "cpu" nodes only */
627 if (type == NULL || strcmp(type, "cpu") != 0)
630 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
632 /* pft_size[0] is the NUMA CEC cookie */
633 ppc64_pft_size = be32_to_cpu(prop[1]);
639 unsigned htab_shift_for_mem_size(unsigned long mem_size)
641 unsigned memshift = __ilog2(mem_size);
642 unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
645 /* round mem_size up to next power of 2 */
646 if ((1UL << memshift) < mem_size)
649 /* aim for 2 pages / pteg */
650 pteg_shift = memshift - (pshift + 1);
653 * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
654 * size permitted by the architecture.
656 return max(pteg_shift + 7, 18U);
659 static unsigned long __init htab_get_table_size(void)
661 /* If hash size isn't already provided by the platform, we try to
662 * retrieve it from the device-tree. If it's not there neither, we
663 * calculate it now based on the total RAM size
665 if (ppc64_pft_size == 0)
666 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
668 return 1UL << ppc64_pft_size;
670 return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
673 #ifdef CONFIG_MEMORY_HOTPLUG
674 int create_section_mapping(unsigned long start, unsigned long end)
676 int rc = htab_bolt_mapping(start, end, __pa(start),
677 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
681 int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
683 BUG_ON(rc2 && (rc2 != -ENOENT));
688 int remove_section_mapping(unsigned long start, unsigned long end)
690 int rc = htab_remove_mapping(start, end, mmu_linear_psize,
695 #endif /* CONFIG_MEMORY_HOTPLUG */
697 static void __init hash_init_partition_table(phys_addr_t hash_table,
698 unsigned long pteg_count)
700 unsigned long ps_field;
701 unsigned long htab_size;
702 unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
705 * slb llp encoding for the page size used in VPM real mode.
706 * We can ignore that for lpid 0
709 htab_size = __ilog2(pteg_count) - 11;
711 BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 24), "Partition table size too large.");
712 partition_tb = __va(memblock_alloc_base(patb_size, patb_size,
713 MEMBLOCK_ALLOC_ANYWHERE));
715 /* Initialize the Partition Table with no entries */
716 memset((void *)partition_tb, 0, patb_size);
717 partition_tb->patb0 = cpu_to_be64(ps_field | hash_table | htab_size);
719 * FIXME!! This should be done via update_partition table
720 * For now UPRT is 0 for us.
722 partition_tb->patb1 = 0;
723 DBG("Partition table %p\n", partition_tb);
725 * update partition table control register,
728 mtspr(SPRN_PTCR, __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
732 static void __init htab_initialize(void)
735 unsigned long pteg_count;
737 unsigned long base = 0, size = 0, limit;
738 struct memblock_region *reg;
740 DBG(" -> htab_initialize()\n");
742 /* Initialize segment sizes */
743 htab_init_seg_sizes();
745 /* Initialize page sizes */
746 htab_init_page_sizes();
748 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
749 mmu_kernel_ssize = MMU_SEGSIZE_1T;
750 mmu_highuser_ssize = MMU_SEGSIZE_1T;
751 printk(KERN_INFO "Using 1TB segments\n");
755 * Calculate the required size of the htab. We want the number of
756 * PTEGs to equal one half the number of real pages.
758 htab_size_bytes = htab_get_table_size();
759 pteg_count = htab_size_bytes >> 7;
761 htab_hash_mask = pteg_count - 1;
763 if (firmware_has_feature(FW_FEATURE_LPAR)) {
764 /* Using a hypervisor which owns the htab */
767 #ifdef CONFIG_FA_DUMP
769 * If firmware assisted dump is active firmware preserves
770 * the contents of htab along with entire partition memory.
771 * Clear the htab if firmware assisted dump is active so
772 * that we dont end up using old mappings.
774 if (is_fadump_active() && ppc_md.hpte_clear_all)
775 ppc_md.hpte_clear_all();
778 /* Find storage for the HPT. Must be contiguous in
779 * the absolute address space. On cell we want it to be
780 * in the first 2 Gig so we can use it for IOMMU hacks.
782 if (machine_is(cell))
785 limit = MEMBLOCK_ALLOC_ANYWHERE;
787 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
789 DBG("Hash table allocated at %lx, size: %lx\n", table,
792 htab_address = __va(table);
794 /* htab absolute addr + encoded htabsize */
795 _SDR1 = table + __ilog2(pteg_count) - 11;
797 /* Initialize the HPT with no entries */
798 memset((void *)table, 0, htab_size_bytes);
800 if (!cpu_has_feature(CPU_FTR_ARCH_300))
802 mtspr(SPRN_SDR1, _SDR1);
804 hash_init_partition_table(table, pteg_count);
807 prot = pgprot_val(PAGE_KERNEL);
809 #ifdef CONFIG_DEBUG_PAGEALLOC
810 if (debug_pagealloc_enabled()) {
811 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
812 linear_map_hash_slots = __va(memblock_alloc_base(
813 linear_map_hash_count, 1, ppc64_rma_size));
814 memset(linear_map_hash_slots, 0, linear_map_hash_count);
816 #endif /* CONFIG_DEBUG_PAGEALLOC */
818 /* On U3 based machines, we need to reserve the DART area and
819 * _NOT_ map it to avoid cache paradoxes as it's remapped non
823 /* create bolted the linear mapping in the hash table */
824 for_each_memblock(memory, reg) {
825 base = (unsigned long)__va(reg->base);
828 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
831 #ifdef CONFIG_U3_DART
832 /* Do not map the DART space. Fortunately, it will be aligned
833 * in such a way that it will not cross two memblock regions and
834 * will fit within a single 16Mb page.
835 * The DART space is assumed to be a full 16Mb region even if
836 * we only use 2Mb of that space. We will use more of it later
837 * for AGP GART. We have to use a full 16Mb large page.
839 DBG("DART base: %lx\n", dart_tablebase);
841 if (dart_tablebase != 0 && dart_tablebase >= base
842 && dart_tablebase < (base + size)) {
843 unsigned long dart_table_end = dart_tablebase + 16 * MB;
844 if (base != dart_tablebase)
845 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
849 if ((base + size) > dart_table_end)
850 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
852 __pa(dart_table_end),
858 #endif /* CONFIG_U3_DART */
859 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
860 prot, mmu_linear_psize, mmu_kernel_ssize));
862 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
865 * If we have a memory_limit and we've allocated TCEs then we need to
866 * explicitly map the TCE area at the top of RAM. We also cope with the
867 * case that the TCEs start below memory_limit.
868 * tce_alloc_start/end are 16MB aligned so the mapping should work
869 * for either 4K or 16MB pages.
871 if (tce_alloc_start) {
872 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
873 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
875 if (base + size >= tce_alloc_start)
876 tce_alloc_start = base + size + 1;
878 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
879 __pa(tce_alloc_start), prot,
880 mmu_linear_psize, mmu_kernel_ssize));
884 DBG(" <- htab_initialize()\n");
889 void __init hash__early_init_mmu(void)
892 * initialize page table size
894 __pte_frag_nr = H_PTE_FRAG_NR;
895 __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
897 __pte_index_size = H_PTE_INDEX_SIZE;
898 __pmd_index_size = H_PMD_INDEX_SIZE;
899 __pud_index_size = H_PUD_INDEX_SIZE;
900 __pgd_index_size = H_PGD_INDEX_SIZE;
901 __pmd_cache_index = H_PMD_CACHE_INDEX;
902 __pte_table_size = H_PTE_TABLE_SIZE;
903 __pmd_table_size = H_PMD_TABLE_SIZE;
904 __pud_table_size = H_PUD_TABLE_SIZE;
905 __pgd_table_size = H_PGD_TABLE_SIZE;
907 * 4k use hugepd format, so for hash set then to
914 __kernel_virt_start = H_KERN_VIRT_START;
915 __kernel_virt_size = H_KERN_VIRT_SIZE;
916 __vmalloc_start = H_VMALLOC_START;
917 __vmalloc_end = H_VMALLOC_END;
918 vmemmap = (struct page *)H_VMEMMAP_BASE;
919 ioremap_bot = IOREMAP_BASE;
921 /* Initialize the MMU Hash table and create the linear mapping
922 * of memory. Has to be done before SLB initialization as this is
923 * currently where the page size encoding is obtained.
927 /* Initialize SLB management */
932 void hash__early_init_mmu_secondary(void)
934 /* Initialize hash table for that CPU */
935 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
936 if (!cpu_has_feature(CPU_FTR_ARCH_300))
937 mtspr(SPRN_SDR1, _SDR1);
940 __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
945 #endif /* CONFIG_SMP */
948 * Called by asm hashtable.S for doing lazy icache flush
950 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
954 if (!pfn_valid(pte_pfn(pte)))
957 page = pte_page(pte);
960 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
962 flush_dcache_icache_page(page);
963 set_bit(PG_arch_1, &page->flags);
970 #ifdef CONFIG_PPC_MM_SLICES
971 static unsigned int get_paca_psize(unsigned long addr)
974 unsigned char *hpsizes;
975 unsigned long index, mask_index;
977 if (addr < SLICE_LOW_TOP) {
978 lpsizes = get_paca()->mm_ctx_low_slices_psize;
979 index = GET_LOW_SLICE_INDEX(addr);
980 return (lpsizes >> (index * 4)) & 0xF;
982 hpsizes = get_paca()->mm_ctx_high_slices_psize;
983 index = GET_HIGH_SLICE_INDEX(addr);
984 mask_index = index & 0x1;
985 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
989 unsigned int get_paca_psize(unsigned long addr)
991 return get_paca()->mm_ctx_user_psize;
996 * Demote a segment to using 4k pages.
997 * For now this makes the whole process use 4k pages.
999 #ifdef CONFIG_PPC_64K_PAGES
1000 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
1002 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
1004 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
1005 copro_flush_all_slbs(mm);
1006 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
1008 copy_mm_to_paca(&mm->context);
1009 slb_flush_and_rebolt();
1012 #endif /* CONFIG_PPC_64K_PAGES */
1014 #ifdef CONFIG_PPC_SUBPAGE_PROT
1016 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
1017 * Userspace sets the subpage permissions using the subpage_prot system call.
1019 * Result is 0: full permissions, _PAGE_RW: read-only,
1020 * _PAGE_RWX: no access.
1022 static int subpage_protection(struct mm_struct *mm, unsigned long ea)
1024 struct subpage_prot_table *spt = &mm->context.spt;
1028 if (ea >= spt->maxaddr)
1030 if (ea < 0x100000000UL) {
1031 /* addresses below 4GB use spt->low_prot */
1032 sbpm = spt->low_prot;
1034 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
1038 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
1041 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
1043 /* extract 2-bit bitfield for this 4k subpage */
1044 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
1047 * 0 -> full premission
1050 * We return the flag that need to be cleared.
1052 spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0);
1056 #else /* CONFIG_PPC_SUBPAGE_PROT */
1057 static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
1063 void hash_failure_debug(unsigned long ea, unsigned long access,
1064 unsigned long vsid, unsigned long trap,
1065 int ssize, int psize, int lpsize, unsigned long pte)
1067 if (!printk_ratelimit())
1069 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
1070 ea, access, current->comm);
1071 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
1072 trap, vsid, ssize, psize, lpsize, pte);
1075 static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
1076 int psize, bool user_region)
1079 if (psize != get_paca_psize(ea)) {
1080 copy_mm_to_paca(&mm->context);
1081 slb_flush_and_rebolt();
1083 } else if (get_paca()->vmalloc_sllp !=
1084 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
1085 get_paca()->vmalloc_sllp =
1086 mmu_psize_defs[mmu_vmalloc_psize].sllp;
1087 slb_vmalloc_update();
1093 * 1 - normal page fault
1094 * -1 - critical hash insertion error
1095 * -2 - access not permitted by subpage protection mechanism
1097 int hash_page_mm(struct mm_struct *mm, unsigned long ea,
1098 unsigned long access, unsigned long trap,
1099 unsigned long flags)
1102 enum ctx_state prev_state = exception_enter();
1107 const struct cpumask *tmp;
1108 int rc, user_region = 0;
1111 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1113 trace_hash_fault(ea, access, trap);
1115 /* Get region & vsid */
1116 switch (REGION_ID(ea)) {
1117 case USER_REGION_ID:
1120 DBG_LOW(" user region with no mm !\n");
1124 psize = get_slice_psize(mm, ea);
1125 ssize = user_segment_size(ea);
1126 vsid = get_vsid(mm->context.id, ea, ssize);
1128 case VMALLOC_REGION_ID:
1129 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
1130 if (ea < VMALLOC_END)
1131 psize = mmu_vmalloc_psize;
1133 psize = mmu_io_psize;
1134 ssize = mmu_kernel_ssize;
1137 /* Not a valid range
1138 * Send the problem up to do_page_fault
1143 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
1147 DBG_LOW("Bad address!\n");
1153 if (pgdir == NULL) {
1158 /* Check CPU locality */
1159 tmp = cpumask_of(smp_processor_id());
1160 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
1161 flags |= HPTE_LOCAL_UPDATE;
1163 #ifndef CONFIG_PPC_64K_PAGES
1164 /* If we use 4K pages and our psize is not 4K, then we might
1165 * be hitting a special driver mapping, and need to align the
1166 * address before we fetch the PTE.
1168 * It could also be a hugepage mapping, in which case this is
1169 * not necessary, but it's not harmful, either.
1171 if (psize != MMU_PAGE_4K)
1172 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1173 #endif /* CONFIG_PPC_64K_PAGES */
1175 /* Get PTE and page size from page tables */
1176 ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
1177 if (ptep == NULL || !pte_present(*ptep)) {
1178 DBG_LOW(" no PTE !\n");
1183 /* Add _PAGE_PRESENT to the required access perm */
1184 access |= _PAGE_PRESENT;
1186 /* Pre-check access permissions (will be re-checked atomically
1187 * in __hash_page_XX but this pre-check is a fast path
1189 if (!check_pte_access(access, pte_val(*ptep))) {
1190 DBG_LOW(" no access !\n");
1197 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
1198 trap, flags, ssize, psize);
1199 #ifdef CONFIG_HUGETLB_PAGE
1201 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
1202 flags, ssize, hugeshift, psize);
1206 * if we have hugeshift, and is not transhuge with
1207 * hugetlb disabled, something is really wrong.
1213 if (current->mm == mm)
1214 check_paca_psize(ea, mm, psize, user_region);
1219 #ifndef CONFIG_PPC_64K_PAGES
1220 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1222 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1223 pte_val(*(ptep + PTRS_PER_PTE)));
1225 /* Do actual hashing */
1226 #ifdef CONFIG_PPC_64K_PAGES
1227 /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */
1228 if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
1229 demote_segment_4k(mm, ea);
1230 psize = MMU_PAGE_4K;
1233 /* If this PTE is non-cacheable and we have restrictions on
1234 * using non cacheable large pages, then we switch to 4k
1236 if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) {
1238 demote_segment_4k(mm, ea);
1239 psize = MMU_PAGE_4K;
1240 } else if (ea < VMALLOC_END) {
1242 * some driver did a non-cacheable mapping
1243 * in vmalloc space, so switch vmalloc
1246 printk(KERN_ALERT "Reducing vmalloc segment "
1247 "to 4kB pages because of "
1248 "non-cacheable mapping\n");
1249 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
1250 copro_flush_all_slbs(mm);
1254 #endif /* CONFIG_PPC_64K_PAGES */
1256 if (current->mm == mm)
1257 check_paca_psize(ea, mm, psize, user_region);
1259 #ifdef CONFIG_PPC_64K_PAGES
1260 if (psize == MMU_PAGE_64K)
1261 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1264 #endif /* CONFIG_PPC_64K_PAGES */
1266 int spp = subpage_protection(mm, ea);
1270 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
1274 /* Dump some info in case of hash insertion failure, they should
1275 * never happen so it is really useful to know if/when they do
1278 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
1279 psize, pte_val(*ptep));
1280 #ifndef CONFIG_PPC_64K_PAGES
1281 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1283 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1284 pte_val(*(ptep + PTRS_PER_PTE)));
1286 DBG_LOW(" -> rc=%d\n", rc);
1289 exception_exit(prev_state);
1292 EXPORT_SYMBOL_GPL(hash_page_mm);
1294 int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
1295 unsigned long dsisr)
1297 unsigned long flags = 0;
1298 struct mm_struct *mm = current->mm;
1300 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1303 if (dsisr & DSISR_NOHPTE)
1304 flags |= HPTE_NOHPTE_UPDATE;
1306 return hash_page_mm(mm, ea, access, trap, flags);
1308 EXPORT_SYMBOL_GPL(hash_page);
1310 int __hash_page(unsigned long ea, unsigned long msr, unsigned long trap,
1311 unsigned long dsisr)
1313 unsigned long access = _PAGE_PRESENT | _PAGE_READ;
1314 unsigned long flags = 0;
1315 struct mm_struct *mm = current->mm;
1317 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1320 if (dsisr & DSISR_NOHPTE)
1321 flags |= HPTE_NOHPTE_UPDATE;
1323 if (dsisr & DSISR_ISSTORE)
1324 access |= _PAGE_WRITE;
1326 * We set _PAGE_PRIVILEGED only when
1327 * kernel mode access kernel space.
1329 * _PAGE_PRIVILEGED is NOT set
1330 * 1) when kernel mode access user space
1331 * 2) user space access kernel space.
1333 access |= _PAGE_PRIVILEGED;
1334 if ((msr & MSR_PR) || (REGION_ID(ea) == USER_REGION_ID))
1335 access &= ~_PAGE_PRIVILEGED;
1338 access |= _PAGE_EXEC;
1340 return hash_page_mm(mm, ea, access, trap, flags);
1343 #ifdef CONFIG_PPC_MM_SLICES
1344 static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1346 int psize = get_slice_psize(mm, ea);
1348 /* We only prefault standard pages for now */
1349 if (unlikely(psize != mm->context.user_psize))
1353 * Don't prefault if subpage protection is enabled for the EA.
1355 if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea)))
1361 static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1367 void hash_preload(struct mm_struct *mm, unsigned long ea,
1368 unsigned long access, unsigned long trap)
1374 unsigned long flags;
1375 int rc, ssize, update_flags = 0;
1377 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1379 if (!should_hash_preload(mm, ea))
1382 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1383 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1385 /* Get Linux PTE if available */
1391 ssize = user_segment_size(ea);
1392 vsid = get_vsid(mm->context.id, ea, ssize);
1396 * Hash doesn't like irqs. Walking linux page table with irq disabled
1397 * saves us from holding multiple locks.
1399 local_irq_save(flags);
1402 * THP pages use update_mmu_cache_pmd. We don't do
1403 * hash preload there. Hence can ignore THP here
1405 ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
1409 WARN_ON(hugepage_shift);
1410 #ifdef CONFIG_PPC_64K_PAGES
1411 /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on
1412 * a 64K kernel), then we don't preload, hash_page() will take
1413 * care of it once we actually try to access the page.
1414 * That way we don't have to duplicate all of the logic for segment
1415 * page size demotion here
1417 if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep))
1419 #endif /* CONFIG_PPC_64K_PAGES */
1421 /* Is that local to this CPU ? */
1422 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1423 update_flags |= HPTE_LOCAL_UPDATE;
1426 #ifdef CONFIG_PPC_64K_PAGES
1427 if (mm->context.user_psize == MMU_PAGE_64K)
1428 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1429 update_flags, ssize);
1431 #endif /* CONFIG_PPC_64K_PAGES */
1432 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
1433 ssize, subpage_protection(mm, ea));
1435 /* Dump some info in case of hash insertion failure, they should
1436 * never happen so it is really useful to know if/when they do
1439 hash_failure_debug(ea, access, vsid, trap, ssize,
1440 mm->context.user_psize,
1441 mm->context.user_psize,
1444 local_irq_restore(flags);
1447 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
1448 * do not forget to update the assembly call site !
1450 void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
1451 unsigned long flags)
1453 unsigned long hash, index, shift, hidx, slot;
1454 int local = flags & HPTE_LOCAL_UPDATE;
1456 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1457 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1458 hash = hpt_hash(vpn, shift, ssize);
1459 hidx = __rpte_to_hidx(pte, index);
1460 if (hidx & _PTEIDX_SECONDARY)
1462 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1463 slot += hidx & _PTEIDX_GROUP_IX;
1464 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
1466 * We use same base page size and actual psize, because we don't
1467 * use these functions for hugepage
1469 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
1470 } pte_iterate_hashed_end();
1472 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1473 /* Transactions are not aborted by tlbiel, only tlbie.
1474 * Without, syncing a page back to a block device w/ PIO could pick up
1475 * transactional data (bad!) so we force an abort here. Before the
1476 * sync the page will be made read-only, which will flush_hash_page.
1477 * BIG ISSUE here: if the kernel uses a page from userspace without
1478 * unmapping it first, it may see the speculated version.
1480 if (local && cpu_has_feature(CPU_FTR_TM) &&
1481 current->thread.regs &&
1482 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1484 tm_abort(TM_CAUSE_TLBI);
1489 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1490 void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
1491 pmd_t *pmdp, unsigned int psize, int ssize,
1492 unsigned long flags)
1494 int i, max_hpte_count, valid;
1495 unsigned long s_addr;
1496 unsigned char *hpte_slot_array;
1497 unsigned long hidx, shift, vpn, hash, slot;
1498 int local = flags & HPTE_LOCAL_UPDATE;
1500 s_addr = addr & HPAGE_PMD_MASK;
1501 hpte_slot_array = get_hpte_slot_array(pmdp);
1503 * IF we try to do a HUGE PTE update after a withdraw is done.
1504 * we will find the below NULL. This happens when we do
1505 * split_huge_page_pmd
1507 if (!hpte_slot_array)
1510 if (ppc_md.hugepage_invalidate) {
1511 ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
1512 psize, ssize, local);
1516 * No bluk hpte removal support, invalidate each entry
1518 shift = mmu_psize_defs[psize].shift;
1519 max_hpte_count = HPAGE_PMD_SIZE >> shift;
1520 for (i = 0; i < max_hpte_count; i++) {
1522 * 8 bits per each hpte entries
1523 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
1525 valid = hpte_valid(hpte_slot_array, i);
1528 hidx = hpte_hash_index(hpte_slot_array, i);
1531 addr = s_addr + (i * (1ul << shift));
1532 vpn = hpt_vpn(addr, vsid, ssize);
1533 hash = hpt_hash(vpn, shift, ssize);
1534 if (hidx & _PTEIDX_SECONDARY)
1537 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1538 slot += hidx & _PTEIDX_GROUP_IX;
1539 ppc_md.hpte_invalidate(slot, vpn, psize,
1540 MMU_PAGE_16M, ssize, local);
1543 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1544 /* Transactions are not aborted by tlbiel, only tlbie.
1545 * Without, syncing a page back to a block device w/ PIO could pick up
1546 * transactional data (bad!) so we force an abort here. Before the
1547 * sync the page will be made read-only, which will flush_hash_page.
1548 * BIG ISSUE here: if the kernel uses a page from userspace without
1549 * unmapping it first, it may see the speculated version.
1551 if (local && cpu_has_feature(CPU_FTR_TM) &&
1552 current->thread.regs &&
1553 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1555 tm_abort(TM_CAUSE_TLBI);
1560 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1562 void flush_hash_range(unsigned long number, int local)
1564 if (ppc_md.flush_hash_range)
1565 ppc_md.flush_hash_range(number, local);
1568 struct ppc64_tlb_batch *batch =
1569 this_cpu_ptr(&ppc64_tlb_batch);
1571 for (i = 0; i < number; i++)
1572 flush_hash_page(batch->vpn[i], batch->pte[i],
1573 batch->psize, batch->ssize, local);
1578 * low_hash_fault is called when we the low level hash code failed
1579 * to instert a PTE due to an hypervisor error
1581 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1583 enum ctx_state prev_state = exception_enter();
1585 if (user_mode(regs)) {
1586 #ifdef CONFIG_PPC_SUBPAGE_PROT
1588 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1591 _exception(SIGBUS, regs, BUS_ADRERR, address);
1593 bad_page_fault(regs, address, SIGBUS);
1595 exception_exit(prev_state);
1598 long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1599 unsigned long pa, unsigned long rflags,
1600 unsigned long vflags, int psize, int ssize)
1602 unsigned long hpte_group;
1606 hpte_group = ((hash & htab_hash_mask) *
1607 HPTES_PER_GROUP) & ~0x7UL;
1609 /* Insert into the hash table, primary slot */
1610 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
1611 psize, psize, ssize);
1613 /* Primary is full, try the secondary */
1614 if (unlikely(slot == -1)) {
1615 hpte_group = ((~hash & htab_hash_mask) *
1616 HPTES_PER_GROUP) & ~0x7UL;
1617 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1618 vflags | HPTE_V_SECONDARY,
1619 psize, psize, ssize);
1622 hpte_group = ((hash & htab_hash_mask) *
1623 HPTES_PER_GROUP)&~0x7UL;
1625 ppc_md.hpte_remove(hpte_group);
1633 #ifdef CONFIG_DEBUG_PAGEALLOC
1634 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1637 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1638 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1639 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
1642 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1644 /* Don't create HPTE entries for bad address */
1648 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1650 mmu_linear_psize, mmu_kernel_ssize);
1653 spin_lock(&linear_map_hash_lock);
1654 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1655 linear_map_hash_slots[lmi] = ret | 0x80;
1656 spin_unlock(&linear_map_hash_lock);
1659 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1661 unsigned long hash, hidx, slot;
1662 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1663 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1665 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1666 spin_lock(&linear_map_hash_lock);
1667 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1668 hidx = linear_map_hash_slots[lmi] & 0x7f;
1669 linear_map_hash_slots[lmi] = 0;
1670 spin_unlock(&linear_map_hash_lock);
1671 if (hidx & _PTEIDX_SECONDARY)
1673 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1674 slot += hidx & _PTEIDX_GROUP_IX;
1675 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1676 mmu_kernel_ssize, 0);
1679 void __kernel_map_pages(struct page *page, int numpages, int enable)
1681 unsigned long flags, vaddr, lmi;
1684 local_irq_save(flags);
1685 for (i = 0; i < numpages; i++, page++) {
1686 vaddr = (unsigned long)page_address(page);
1687 lmi = __pa(vaddr) >> PAGE_SHIFT;
1688 if (lmi >= linear_map_hash_count)
1691 kernel_map_linear_page(vaddr, lmi);
1693 kernel_unmap_linear_page(vaddr, lmi);
1695 local_irq_restore(flags);
1697 #endif /* CONFIG_DEBUG_PAGEALLOC */
1699 void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
1700 phys_addr_t first_memblock_size)
1702 /* We don't currently support the first MEMBLOCK not mapping 0
1703 * physical on those processors
1705 BUG_ON(first_memblock_base != 0);
1707 /* On LPAR systems, the first entry is our RMA region,
1708 * non-LPAR 64-bit hash MMU systems don't have a limitation
1709 * on real mode access, but using the first entry works well
1710 * enough. We also clamp it to 1G to avoid some funky things
1711 * such as RTAS bugs etc...
1713 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1715 /* Finally limit subsequent allocations */
1716 memblock_set_current_limit(ppc64_rma_size);