Linux 3.17-rc2
[linux/fpc-iii.git] / arch / powerpc / mm / hash_utils_64.c
blobdaee7f4e5a14ca0048a7dfee9f2f07921529fced
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/spu.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>
61 #ifdef DEBUG
62 #define DBG(fmt...) udbg_printf(fmt)
63 #else
64 #define DBG(fmt...)
65 #endif
67 #ifdef DEBUG_LOW
68 #define DBG_LOW(fmt...) udbg_printf(fmt)
69 #else
70 #define DBG_LOW(fmt...)
71 #endif
73 #define KB (1024)
74 #define MB (1024*KB)
75 #define GB (1024L*MB)
78 * Note: pte --> Linux PTE
79 * HPTE --> PowerPC Hashed Page Table Entry
81 * Execution context:
82 * htab_initialize is called with the MMU off (of course), but
83 * the kernel has been copied down to zero so it can directly
84 * reference global data. At this point it is very difficult
85 * to print debug info.
89 #ifdef CONFIG_U3_DART
90 extern unsigned long dart_tablebase;
91 #endif /* CONFIG_U3_DART */
93 static unsigned long _SDR1;
94 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
96 struct hash_pte *htab_address;
97 unsigned long htab_size_bytes;
98 unsigned long htab_hash_mask;
99 EXPORT_SYMBOL_GPL(htab_hash_mask);
100 int mmu_linear_psize = MMU_PAGE_4K;
101 int mmu_virtual_psize = MMU_PAGE_4K;
102 int mmu_vmalloc_psize = MMU_PAGE_4K;
103 #ifdef CONFIG_SPARSEMEM_VMEMMAP
104 int mmu_vmemmap_psize = MMU_PAGE_4K;
105 #endif
106 int mmu_io_psize = MMU_PAGE_4K;
107 int mmu_kernel_ssize = MMU_SEGSIZE_256M;
108 int mmu_highuser_ssize = MMU_SEGSIZE_256M;
109 u16 mmu_slb_size = 64;
110 EXPORT_SYMBOL_GPL(mmu_slb_size);
111 #ifdef CONFIG_PPC_64K_PAGES
112 int mmu_ci_restrictions;
113 #endif
114 #ifdef CONFIG_DEBUG_PAGEALLOC
115 static u8 *linear_map_hash_slots;
116 static unsigned long linear_map_hash_count;
117 static DEFINE_SPINLOCK(linear_map_hash_lock);
118 #endif /* CONFIG_DEBUG_PAGEALLOC */
120 /* There are definitions of page sizes arrays to be used when none
121 * is provided by the firmware.
124 /* Pre-POWER4 CPUs (4k pages only)
126 static struct mmu_psize_def mmu_psize_defaults_old[] = {
127 [MMU_PAGE_4K] = {
128 .shift = 12,
129 .sllp = 0,
130 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
131 .avpnm = 0,
132 .tlbiel = 0,
136 /* POWER4, GPUL, POWER5
138 * Support for 16Mb large pages
140 static struct mmu_psize_def mmu_psize_defaults_gp[] = {
141 [MMU_PAGE_4K] = {
142 .shift = 12,
143 .sllp = 0,
144 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
145 .avpnm = 0,
146 .tlbiel = 1,
148 [MMU_PAGE_16M] = {
149 .shift = 24,
150 .sllp = SLB_VSID_L,
151 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
152 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
153 .avpnm = 0x1UL,
154 .tlbiel = 0,
158 static unsigned long htab_convert_pte_flags(unsigned long pteflags)
160 unsigned long rflags = pteflags & 0x1fa;
162 /* _PAGE_EXEC -> NOEXEC */
163 if ((pteflags & _PAGE_EXEC) == 0)
164 rflags |= HPTE_R_N;
166 /* PP bits. PAGE_USER is already PP bit 0x2, so we only
167 * need to add in 0x1 if it's a read-only user page
169 if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
170 (pteflags & _PAGE_DIRTY)))
171 rflags |= 1;
173 * Always add "C" bit for perf. Memory coherence is always enabled
175 return rflags | HPTE_R_C | HPTE_R_M;
178 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
179 unsigned long pstart, unsigned long prot,
180 int psize, int ssize)
182 unsigned long vaddr, paddr;
183 unsigned int step, shift;
184 int ret = 0;
186 shift = mmu_psize_defs[psize].shift;
187 step = 1 << shift;
189 prot = htab_convert_pte_flags(prot);
191 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
192 vstart, vend, pstart, prot, psize, ssize);
194 for (vaddr = vstart, paddr = pstart; vaddr < vend;
195 vaddr += step, paddr += step) {
196 unsigned long hash, hpteg;
197 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
198 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
199 unsigned long tprot = prot;
202 * If we hit a bad address return error.
204 if (!vsid)
205 return -1;
206 /* Make kernel text executable */
207 if (overlaps_kernel_text(vaddr, vaddr + step))
208 tprot &= ~HPTE_R_N;
210 /* Make kvm guest trampolines executable */
211 if (overlaps_kvm_tmp(vaddr, vaddr + step))
212 tprot &= ~HPTE_R_N;
215 * If relocatable, check if it overlaps interrupt vectors that
216 * are copied down to real 0. For relocatable kernel
217 * (e.g. kdump case) we copy interrupt vectors down to real
218 * address 0. Mark that region as executable. This is
219 * because on p8 system with relocation on exception feature
220 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
221 * in order to execute the interrupt handlers in virtual
222 * mode the vector region need to be marked as executable.
224 if ((PHYSICAL_START > MEMORY_START) &&
225 overlaps_interrupt_vector_text(vaddr, vaddr + step))
226 tprot &= ~HPTE_R_N;
228 hash = hpt_hash(vpn, shift, ssize);
229 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
231 BUG_ON(!ppc_md.hpte_insert);
232 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
233 HPTE_V_BOLTED, psize, psize, ssize);
235 if (ret < 0)
236 break;
237 #ifdef CONFIG_DEBUG_PAGEALLOC
238 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
239 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
240 #endif /* CONFIG_DEBUG_PAGEALLOC */
242 return ret < 0 ? ret : 0;
245 #ifdef CONFIG_MEMORY_HOTPLUG
246 int htab_remove_mapping(unsigned long vstart, unsigned long vend,
247 int psize, int ssize)
249 unsigned long vaddr;
250 unsigned int step, shift;
252 shift = mmu_psize_defs[psize].shift;
253 step = 1 << shift;
255 if (!ppc_md.hpte_removebolted) {
256 printk(KERN_WARNING "Platform doesn't implement "
257 "hpte_removebolted\n");
258 return -EINVAL;
261 for (vaddr = vstart; vaddr < vend; vaddr += step)
262 ppc_md.hpte_removebolted(vaddr, psize, ssize);
264 return 0;
266 #endif /* CONFIG_MEMORY_HOTPLUG */
268 static int __init htab_dt_scan_seg_sizes(unsigned long node,
269 const char *uname, int depth,
270 void *data)
272 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
273 const __be32 *prop;
274 int size = 0;
276 /* We are scanning "cpu" nodes only */
277 if (type == NULL || strcmp(type, "cpu") != 0)
278 return 0;
280 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
281 if (prop == NULL)
282 return 0;
283 for (; size >= 4; size -= 4, ++prop) {
284 if (be32_to_cpu(prop[0]) == 40) {
285 DBG("1T segment support detected\n");
286 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
287 return 1;
290 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
291 return 0;
294 static void __init htab_init_seg_sizes(void)
296 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
299 static int __init get_idx_from_shift(unsigned int shift)
301 int idx = -1;
303 switch (shift) {
304 case 0xc:
305 idx = MMU_PAGE_4K;
306 break;
307 case 0x10:
308 idx = MMU_PAGE_64K;
309 break;
310 case 0x14:
311 idx = MMU_PAGE_1M;
312 break;
313 case 0x18:
314 idx = MMU_PAGE_16M;
315 break;
316 case 0x22:
317 idx = MMU_PAGE_16G;
318 break;
320 return idx;
323 static int __init htab_dt_scan_page_sizes(unsigned long node,
324 const char *uname, int depth,
325 void *data)
327 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
328 const __be32 *prop;
329 int size = 0;
331 /* We are scanning "cpu" nodes only */
332 if (type == NULL || strcmp(type, "cpu") != 0)
333 return 0;
335 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
336 if (prop != NULL) {
337 pr_info("Page sizes from device-tree:\n");
338 size /= 4;
339 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
340 while(size > 0) {
341 unsigned int base_shift = be32_to_cpu(prop[0]);
342 unsigned int slbenc = be32_to_cpu(prop[1]);
343 unsigned int lpnum = be32_to_cpu(prop[2]);
344 struct mmu_psize_def *def;
345 int idx, base_idx;
347 size -= 3; prop += 3;
348 base_idx = get_idx_from_shift(base_shift);
349 if (base_idx < 0) {
351 * skip the pte encoding also
353 prop += lpnum * 2; size -= lpnum * 2;
354 continue;
356 def = &mmu_psize_defs[base_idx];
357 if (base_idx == MMU_PAGE_16M)
358 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
360 def->shift = base_shift;
361 if (base_shift <= 23)
362 def->avpnm = 0;
363 else
364 def->avpnm = (1 << (base_shift - 23)) - 1;
365 def->sllp = slbenc;
367 * We don't know for sure what's up with tlbiel, so
368 * for now we only set it for 4K and 64K pages
370 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
371 def->tlbiel = 1;
372 else
373 def->tlbiel = 0;
375 while (size > 0 && lpnum) {
376 unsigned int shift = be32_to_cpu(prop[0]);
377 int penc = be32_to_cpu(prop[1]);
379 prop += 2; size -= 2;
380 lpnum--;
382 idx = get_idx_from_shift(shift);
383 if (idx < 0)
384 continue;
386 if (penc == -1)
387 pr_err("Invalid penc for base_shift=%d "
388 "shift=%d\n", base_shift, shift);
390 def->penc[idx] = penc;
391 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
392 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
393 base_shift, shift, def->sllp,
394 def->avpnm, def->tlbiel, def->penc[idx]);
397 return 1;
399 return 0;
402 #ifdef CONFIG_HUGETLB_PAGE
403 /* Scan for 16G memory blocks that have been set aside for huge pages
404 * and reserve those blocks for 16G huge pages.
406 static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
407 const char *uname, int depth,
408 void *data) {
409 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
410 const __be64 *addr_prop;
411 const __be32 *page_count_prop;
412 unsigned int expected_pages;
413 long unsigned int phys_addr;
414 long unsigned int block_size;
416 /* We are scanning "memory" nodes only */
417 if (type == NULL || strcmp(type, "memory") != 0)
418 return 0;
420 /* This property is the log base 2 of the number of virtual pages that
421 * will represent this memory block. */
422 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
423 if (page_count_prop == NULL)
424 return 0;
425 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
426 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
427 if (addr_prop == NULL)
428 return 0;
429 phys_addr = be64_to_cpu(addr_prop[0]);
430 block_size = be64_to_cpu(addr_prop[1]);
431 if (block_size != (16 * GB))
432 return 0;
433 printk(KERN_INFO "Huge page(16GB) memory: "
434 "addr = 0x%lX size = 0x%lX pages = %d\n",
435 phys_addr, block_size, expected_pages);
436 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
437 memblock_reserve(phys_addr, block_size * expected_pages);
438 add_gpage(phys_addr, block_size, expected_pages);
440 return 0;
442 #endif /* CONFIG_HUGETLB_PAGE */
444 static void mmu_psize_set_default_penc(void)
446 int bpsize, apsize;
447 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
448 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
449 mmu_psize_defs[bpsize].penc[apsize] = -1;
452 #ifdef CONFIG_PPC_64K_PAGES
454 static bool might_have_hea(void)
457 * The HEA ethernet adapter requires awareness of the
458 * GX bus. Without that awareness we can easily assume
459 * we will never see an HEA ethernet device.
461 #ifdef CONFIG_IBMEBUS
462 return !cpu_has_feature(CPU_FTR_ARCH_207S);
463 #else
464 return false;
465 #endif
468 #endif /* #ifdef CONFIG_PPC_64K_PAGES */
470 static void __init htab_init_page_sizes(void)
472 int rc;
474 /* se the invalid penc to -1 */
475 mmu_psize_set_default_penc();
477 /* Default to 4K pages only */
478 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
479 sizeof(mmu_psize_defaults_old));
482 * Try to find the available page sizes in the device-tree
484 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
485 if (rc != 0) /* Found */
486 goto found;
489 * Not in the device-tree, let's fallback on known size
490 * list for 16M capable GP & GR
492 if (mmu_has_feature(MMU_FTR_16M_PAGE))
493 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
494 sizeof(mmu_psize_defaults_gp));
495 found:
496 #ifndef CONFIG_DEBUG_PAGEALLOC
498 * Pick a size for the linear mapping. Currently, we only support
499 * 16M, 1M and 4K which is the default
501 if (mmu_psize_defs[MMU_PAGE_16M].shift)
502 mmu_linear_psize = MMU_PAGE_16M;
503 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
504 mmu_linear_psize = MMU_PAGE_1M;
505 #endif /* CONFIG_DEBUG_PAGEALLOC */
507 #ifdef CONFIG_PPC_64K_PAGES
509 * Pick a size for the ordinary pages. Default is 4K, we support
510 * 64K for user mappings and vmalloc if supported by the processor.
511 * We only use 64k for ioremap if the processor
512 * (and firmware) support cache-inhibited large pages.
513 * If not, we use 4k and set mmu_ci_restrictions so that
514 * hash_page knows to switch processes that use cache-inhibited
515 * mappings to 4k pages.
517 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
518 mmu_virtual_psize = MMU_PAGE_64K;
519 mmu_vmalloc_psize = MMU_PAGE_64K;
520 if (mmu_linear_psize == MMU_PAGE_4K)
521 mmu_linear_psize = MMU_PAGE_64K;
522 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
524 * When running on pSeries using 64k pages for ioremap
525 * would stop us accessing the HEA ethernet. So if we
526 * have the chance of ever seeing one, stay at 4k.
528 if (!might_have_hea() || !machine_is(pseries))
529 mmu_io_psize = MMU_PAGE_64K;
530 } else
531 mmu_ci_restrictions = 1;
533 #endif /* CONFIG_PPC_64K_PAGES */
535 #ifdef CONFIG_SPARSEMEM_VMEMMAP
536 /* We try to use 16M pages for vmemmap if that is supported
537 * and we have at least 1G of RAM at boot
539 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
540 memblock_phys_mem_size() >= 0x40000000)
541 mmu_vmemmap_psize = MMU_PAGE_16M;
542 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
543 mmu_vmemmap_psize = MMU_PAGE_64K;
544 else
545 mmu_vmemmap_psize = MMU_PAGE_4K;
546 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
548 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
549 "virtual = %d, io = %d"
550 #ifdef CONFIG_SPARSEMEM_VMEMMAP
551 ", vmemmap = %d"
552 #endif
553 "\n",
554 mmu_psize_defs[mmu_linear_psize].shift,
555 mmu_psize_defs[mmu_virtual_psize].shift,
556 mmu_psize_defs[mmu_io_psize].shift
557 #ifdef CONFIG_SPARSEMEM_VMEMMAP
558 ,mmu_psize_defs[mmu_vmemmap_psize].shift
559 #endif
562 #ifdef CONFIG_HUGETLB_PAGE
563 /* Reserve 16G huge page memory sections for huge pages */
564 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
565 #endif /* CONFIG_HUGETLB_PAGE */
568 static int __init htab_dt_scan_pftsize(unsigned long node,
569 const char *uname, int depth,
570 void *data)
572 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
573 const __be32 *prop;
575 /* We are scanning "cpu" nodes only */
576 if (type == NULL || strcmp(type, "cpu") != 0)
577 return 0;
579 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
580 if (prop != NULL) {
581 /* pft_size[0] is the NUMA CEC cookie */
582 ppc64_pft_size = be32_to_cpu(prop[1]);
583 return 1;
585 return 0;
588 static unsigned long __init htab_get_table_size(void)
590 unsigned long mem_size, rnd_mem_size, pteg_count, psize;
592 /* If hash size isn't already provided by the platform, we try to
593 * retrieve it from the device-tree. If it's not there neither, we
594 * calculate it now based on the total RAM size
596 if (ppc64_pft_size == 0)
597 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
598 if (ppc64_pft_size)
599 return 1UL << ppc64_pft_size;
601 /* round mem_size up to next power of 2 */
602 mem_size = memblock_phys_mem_size();
603 rnd_mem_size = 1UL << __ilog2(mem_size);
604 if (rnd_mem_size < mem_size)
605 rnd_mem_size <<= 1;
607 /* # pages / 2 */
608 psize = mmu_psize_defs[mmu_virtual_psize].shift;
609 pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
611 return pteg_count << 7;
614 #ifdef CONFIG_MEMORY_HOTPLUG
615 int create_section_mapping(unsigned long start, unsigned long end)
617 return htab_bolt_mapping(start, end, __pa(start),
618 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
619 mmu_kernel_ssize);
622 int remove_section_mapping(unsigned long start, unsigned long end)
624 return htab_remove_mapping(start, end, mmu_linear_psize,
625 mmu_kernel_ssize);
627 #endif /* CONFIG_MEMORY_HOTPLUG */
629 extern u32 htab_call_hpte_insert1[];
630 extern u32 htab_call_hpte_insert2[];
631 extern u32 htab_call_hpte_remove[];
632 extern u32 htab_call_hpte_updatepp[];
633 extern u32 ht64_call_hpte_insert1[];
634 extern u32 ht64_call_hpte_insert2[];
635 extern u32 ht64_call_hpte_remove[];
636 extern u32 ht64_call_hpte_updatepp[];
638 static void __init htab_finish_init(void)
640 #ifdef CONFIG_PPC_HAS_HASH_64K
641 patch_branch(ht64_call_hpte_insert1,
642 ppc_function_entry(ppc_md.hpte_insert),
643 BRANCH_SET_LINK);
644 patch_branch(ht64_call_hpte_insert2,
645 ppc_function_entry(ppc_md.hpte_insert),
646 BRANCH_SET_LINK);
647 patch_branch(ht64_call_hpte_remove,
648 ppc_function_entry(ppc_md.hpte_remove),
649 BRANCH_SET_LINK);
650 patch_branch(ht64_call_hpte_updatepp,
651 ppc_function_entry(ppc_md.hpte_updatepp),
652 BRANCH_SET_LINK);
653 #endif /* CONFIG_PPC_HAS_HASH_64K */
655 patch_branch(htab_call_hpte_insert1,
656 ppc_function_entry(ppc_md.hpte_insert),
657 BRANCH_SET_LINK);
658 patch_branch(htab_call_hpte_insert2,
659 ppc_function_entry(ppc_md.hpte_insert),
660 BRANCH_SET_LINK);
661 patch_branch(htab_call_hpte_remove,
662 ppc_function_entry(ppc_md.hpte_remove),
663 BRANCH_SET_LINK);
664 patch_branch(htab_call_hpte_updatepp,
665 ppc_function_entry(ppc_md.hpte_updatepp),
666 BRANCH_SET_LINK);
669 static void __init htab_initialize(void)
671 unsigned long table;
672 unsigned long pteg_count;
673 unsigned long prot;
674 unsigned long base = 0, size = 0, limit;
675 struct memblock_region *reg;
677 DBG(" -> htab_initialize()\n");
679 /* Initialize segment sizes */
680 htab_init_seg_sizes();
682 /* Initialize page sizes */
683 htab_init_page_sizes();
685 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
686 mmu_kernel_ssize = MMU_SEGSIZE_1T;
687 mmu_highuser_ssize = MMU_SEGSIZE_1T;
688 printk(KERN_INFO "Using 1TB segments\n");
692 * Calculate the required size of the htab. We want the number of
693 * PTEGs to equal one half the number of real pages.
695 htab_size_bytes = htab_get_table_size();
696 pteg_count = htab_size_bytes >> 7;
698 htab_hash_mask = pteg_count - 1;
700 if (firmware_has_feature(FW_FEATURE_LPAR)) {
701 /* Using a hypervisor which owns the htab */
702 htab_address = NULL;
703 _SDR1 = 0;
704 #ifdef CONFIG_FA_DUMP
706 * If firmware assisted dump is active firmware preserves
707 * the contents of htab along with entire partition memory.
708 * Clear the htab if firmware assisted dump is active so
709 * that we dont end up using old mappings.
711 if (is_fadump_active() && ppc_md.hpte_clear_all)
712 ppc_md.hpte_clear_all();
713 #endif
714 } else {
715 /* Find storage for the HPT. Must be contiguous in
716 * the absolute address space. On cell we want it to be
717 * in the first 2 Gig so we can use it for IOMMU hacks.
719 if (machine_is(cell))
720 limit = 0x80000000;
721 else
722 limit = MEMBLOCK_ALLOC_ANYWHERE;
724 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
726 DBG("Hash table allocated at %lx, size: %lx\n", table,
727 htab_size_bytes);
729 htab_address = __va(table);
731 /* htab absolute addr + encoded htabsize */
732 _SDR1 = table + __ilog2(pteg_count) - 11;
734 /* Initialize the HPT with no entries */
735 memset((void *)table, 0, htab_size_bytes);
737 /* Set SDR1 */
738 mtspr(SPRN_SDR1, _SDR1);
741 prot = pgprot_val(PAGE_KERNEL);
743 #ifdef CONFIG_DEBUG_PAGEALLOC
744 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
745 linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
746 1, ppc64_rma_size));
747 memset(linear_map_hash_slots, 0, linear_map_hash_count);
748 #endif /* CONFIG_DEBUG_PAGEALLOC */
750 /* On U3 based machines, we need to reserve the DART area and
751 * _NOT_ map it to avoid cache paradoxes as it's remapped non
752 * cacheable later on
755 /* create bolted the linear mapping in the hash table */
756 for_each_memblock(memory, reg) {
757 base = (unsigned long)__va(reg->base);
758 size = reg->size;
760 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
761 base, size, prot);
763 #ifdef CONFIG_U3_DART
764 /* Do not map the DART space. Fortunately, it will be aligned
765 * in such a way that it will not cross two memblock regions and
766 * will fit within a single 16Mb page.
767 * The DART space is assumed to be a full 16Mb region even if
768 * we only use 2Mb of that space. We will use more of it later
769 * for AGP GART. We have to use a full 16Mb large page.
771 DBG("DART base: %lx\n", dart_tablebase);
773 if (dart_tablebase != 0 && dart_tablebase >= base
774 && dart_tablebase < (base + size)) {
775 unsigned long dart_table_end = dart_tablebase + 16 * MB;
776 if (base != dart_tablebase)
777 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
778 __pa(base), prot,
779 mmu_linear_psize,
780 mmu_kernel_ssize));
781 if ((base + size) > dart_table_end)
782 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
783 base + size,
784 __pa(dart_table_end),
785 prot,
786 mmu_linear_psize,
787 mmu_kernel_ssize));
788 continue;
790 #endif /* CONFIG_U3_DART */
791 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
792 prot, mmu_linear_psize, mmu_kernel_ssize));
794 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
797 * If we have a memory_limit and we've allocated TCEs then we need to
798 * explicitly map the TCE area at the top of RAM. We also cope with the
799 * case that the TCEs start below memory_limit.
800 * tce_alloc_start/end are 16MB aligned so the mapping should work
801 * for either 4K or 16MB pages.
803 if (tce_alloc_start) {
804 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
805 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
807 if (base + size >= tce_alloc_start)
808 tce_alloc_start = base + size + 1;
810 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
811 __pa(tce_alloc_start), prot,
812 mmu_linear_psize, mmu_kernel_ssize));
815 htab_finish_init();
817 DBG(" <- htab_initialize()\n");
819 #undef KB
820 #undef MB
822 void __init early_init_mmu(void)
824 /* Initialize the MMU Hash table and create the linear mapping
825 * of memory. Has to be done before SLB initialization as this is
826 * currently where the page size encoding is obtained.
828 htab_initialize();
830 /* Initialize SLB management */
831 slb_initialize();
834 #ifdef CONFIG_SMP
835 void early_init_mmu_secondary(void)
837 /* Initialize hash table for that CPU */
838 if (!firmware_has_feature(FW_FEATURE_LPAR))
839 mtspr(SPRN_SDR1, _SDR1);
841 /* Initialize SLB */
842 slb_initialize();
844 #endif /* CONFIG_SMP */
847 * Called by asm hashtable.S for doing lazy icache flush
849 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
851 struct page *page;
853 if (!pfn_valid(pte_pfn(pte)))
854 return pp;
856 page = pte_page(pte);
858 /* page is dirty */
859 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
860 if (trap == 0x400) {
861 flush_dcache_icache_page(page);
862 set_bit(PG_arch_1, &page->flags);
863 } else
864 pp |= HPTE_R_N;
866 return pp;
869 #ifdef CONFIG_PPC_MM_SLICES
870 unsigned int get_paca_psize(unsigned long addr)
872 u64 lpsizes;
873 unsigned char *hpsizes;
874 unsigned long index, mask_index;
876 if (addr < SLICE_LOW_TOP) {
877 lpsizes = get_paca()->context.low_slices_psize;
878 index = GET_LOW_SLICE_INDEX(addr);
879 return (lpsizes >> (index * 4)) & 0xF;
881 hpsizes = get_paca()->context.high_slices_psize;
882 index = GET_HIGH_SLICE_INDEX(addr);
883 mask_index = index & 0x1;
884 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
887 #else
888 unsigned int get_paca_psize(unsigned long addr)
890 return get_paca()->context.user_psize;
892 #endif
895 * Demote a segment to using 4k pages.
896 * For now this makes the whole process use 4k pages.
898 #ifdef CONFIG_PPC_64K_PAGES
899 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
901 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
902 return;
903 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
904 #ifdef CONFIG_SPU_BASE
905 spu_flush_all_slbs(mm);
906 #endif
907 if (get_paca_psize(addr) != MMU_PAGE_4K) {
908 get_paca()->context = mm->context;
909 slb_flush_and_rebolt();
912 #endif /* CONFIG_PPC_64K_PAGES */
914 #ifdef CONFIG_PPC_SUBPAGE_PROT
916 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
917 * Userspace sets the subpage permissions using the subpage_prot system call.
919 * Result is 0: full permissions, _PAGE_RW: read-only,
920 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
922 static int subpage_protection(struct mm_struct *mm, unsigned long ea)
924 struct subpage_prot_table *spt = &mm->context.spt;
925 u32 spp = 0;
926 u32 **sbpm, *sbpp;
928 if (ea >= spt->maxaddr)
929 return 0;
930 if (ea < 0x100000000UL) {
931 /* addresses below 4GB use spt->low_prot */
932 sbpm = spt->low_prot;
933 } else {
934 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
935 if (!sbpm)
936 return 0;
938 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
939 if (!sbpp)
940 return 0;
941 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
943 /* extract 2-bit bitfield for this 4k subpage */
944 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
946 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
947 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
948 return spp;
951 #else /* CONFIG_PPC_SUBPAGE_PROT */
952 static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
954 return 0;
956 #endif
958 void hash_failure_debug(unsigned long ea, unsigned long access,
959 unsigned long vsid, unsigned long trap,
960 int ssize, int psize, int lpsize, unsigned long pte)
962 if (!printk_ratelimit())
963 return;
964 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
965 ea, access, current->comm);
966 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
967 trap, vsid, ssize, psize, lpsize, pte);
970 static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
971 int psize, bool user_region)
973 if (user_region) {
974 if (psize != get_paca_psize(ea)) {
975 get_paca()->context = mm->context;
976 slb_flush_and_rebolt();
978 } else if (get_paca()->vmalloc_sllp !=
979 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
980 get_paca()->vmalloc_sllp =
981 mmu_psize_defs[mmu_vmalloc_psize].sllp;
982 slb_vmalloc_update();
986 /* Result code is:
987 * 0 - handled
988 * 1 - normal page fault
989 * -1 - critical hash insertion error
990 * -2 - access not permitted by subpage protection mechanism
992 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
994 enum ctx_state prev_state = exception_enter();
995 pgd_t *pgdir;
996 unsigned long vsid;
997 struct mm_struct *mm;
998 pte_t *ptep;
999 unsigned hugeshift;
1000 const struct cpumask *tmp;
1001 int rc, user_region = 0, local = 0;
1002 int psize, ssize;
1004 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1005 ea, access, trap);
1007 /* Get region & vsid */
1008 switch (REGION_ID(ea)) {
1009 case USER_REGION_ID:
1010 user_region = 1;
1011 mm = current->mm;
1012 if (! mm) {
1013 DBG_LOW(" user region with no mm !\n");
1014 rc = 1;
1015 goto bail;
1017 psize = get_slice_psize(mm, ea);
1018 ssize = user_segment_size(ea);
1019 vsid = get_vsid(mm->context.id, ea, ssize);
1020 break;
1021 case VMALLOC_REGION_ID:
1022 mm = &init_mm;
1023 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
1024 if (ea < VMALLOC_END)
1025 psize = mmu_vmalloc_psize;
1026 else
1027 psize = mmu_io_psize;
1028 ssize = mmu_kernel_ssize;
1029 break;
1030 default:
1031 /* Not a valid range
1032 * Send the problem up to do_page_fault
1034 rc = 1;
1035 goto bail;
1037 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
1039 /* Bad address. */
1040 if (!vsid) {
1041 DBG_LOW("Bad address!\n");
1042 rc = 1;
1043 goto bail;
1045 /* Get pgdir */
1046 pgdir = mm->pgd;
1047 if (pgdir == NULL) {
1048 rc = 1;
1049 goto bail;
1052 /* Check CPU locality */
1053 tmp = cpumask_of(smp_processor_id());
1054 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
1055 local = 1;
1057 #ifndef CONFIG_PPC_64K_PAGES
1058 /* If we use 4K pages and our psize is not 4K, then we might
1059 * be hitting a special driver mapping, and need to align the
1060 * address before we fetch the PTE.
1062 * It could also be a hugepage mapping, in which case this is
1063 * not necessary, but it's not harmful, either.
1065 if (psize != MMU_PAGE_4K)
1066 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1067 #endif /* CONFIG_PPC_64K_PAGES */
1069 /* Get PTE and page size from page tables */
1070 ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
1071 if (ptep == NULL || !pte_present(*ptep)) {
1072 DBG_LOW(" no PTE !\n");
1073 rc = 1;
1074 goto bail;
1077 /* Add _PAGE_PRESENT to the required access perm */
1078 access |= _PAGE_PRESENT;
1080 /* Pre-check access permissions (will be re-checked atomically
1081 * in __hash_page_XX but this pre-check is a fast path
1083 if (access & ~pte_val(*ptep)) {
1084 DBG_LOW(" no access !\n");
1085 rc = 1;
1086 goto bail;
1089 if (hugeshift) {
1090 if (pmd_trans_huge(*(pmd_t *)ptep))
1091 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
1092 trap, local, ssize, psize);
1093 #ifdef CONFIG_HUGETLB_PAGE
1094 else
1095 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
1096 local, ssize, hugeshift, psize);
1097 #else
1098 else {
1100 * if we have hugeshift, and is not transhuge with
1101 * hugetlb disabled, something is really wrong.
1103 rc = 1;
1104 WARN_ON(1);
1106 #endif
1107 check_paca_psize(ea, mm, psize, user_region);
1109 goto bail;
1112 #ifndef CONFIG_PPC_64K_PAGES
1113 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1114 #else
1115 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1116 pte_val(*(ptep + PTRS_PER_PTE)));
1117 #endif
1118 /* Do actual hashing */
1119 #ifdef CONFIG_PPC_64K_PAGES
1120 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
1121 if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
1122 demote_segment_4k(mm, ea);
1123 psize = MMU_PAGE_4K;
1126 /* If this PTE is non-cacheable and we have restrictions on
1127 * using non cacheable large pages, then we switch to 4k
1129 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
1130 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
1131 if (user_region) {
1132 demote_segment_4k(mm, ea);
1133 psize = MMU_PAGE_4K;
1134 } else if (ea < VMALLOC_END) {
1136 * some driver did a non-cacheable mapping
1137 * in vmalloc space, so switch vmalloc
1138 * to 4k pages
1140 printk(KERN_ALERT "Reducing vmalloc segment "
1141 "to 4kB pages because of "
1142 "non-cacheable mapping\n");
1143 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
1144 #ifdef CONFIG_SPU_BASE
1145 spu_flush_all_slbs(mm);
1146 #endif
1150 check_paca_psize(ea, mm, psize, user_region);
1151 #endif /* CONFIG_PPC_64K_PAGES */
1153 #ifdef CONFIG_PPC_HAS_HASH_64K
1154 if (psize == MMU_PAGE_64K)
1155 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
1156 else
1157 #endif /* CONFIG_PPC_HAS_HASH_64K */
1159 int spp = subpage_protection(mm, ea);
1160 if (access & spp)
1161 rc = -2;
1162 else
1163 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
1164 local, ssize, spp);
1167 /* Dump some info in case of hash insertion failure, they should
1168 * never happen so it is really useful to know if/when they do
1170 if (rc == -1)
1171 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
1172 psize, pte_val(*ptep));
1173 #ifndef CONFIG_PPC_64K_PAGES
1174 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1175 #else
1176 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1177 pte_val(*(ptep + PTRS_PER_PTE)));
1178 #endif
1179 DBG_LOW(" -> rc=%d\n", rc);
1181 bail:
1182 exception_exit(prev_state);
1183 return rc;
1185 EXPORT_SYMBOL_GPL(hash_page);
1187 void hash_preload(struct mm_struct *mm, unsigned long ea,
1188 unsigned long access, unsigned long trap)
1190 int hugepage_shift;
1191 unsigned long vsid;
1192 pgd_t *pgdir;
1193 pte_t *ptep;
1194 unsigned long flags;
1195 int rc, ssize, local = 0;
1197 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1199 #ifdef CONFIG_PPC_MM_SLICES
1200 /* We only prefault standard pages for now */
1201 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
1202 return;
1203 #endif
1205 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1206 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1208 /* Get Linux PTE if available */
1209 pgdir = mm->pgd;
1210 if (pgdir == NULL)
1211 return;
1213 /* Get VSID */
1214 ssize = user_segment_size(ea);
1215 vsid = get_vsid(mm->context.id, ea, ssize);
1216 if (!vsid)
1217 return;
1219 * Hash doesn't like irqs. Walking linux page table with irq disabled
1220 * saves us from holding multiple locks.
1222 local_irq_save(flags);
1225 * THP pages use update_mmu_cache_pmd. We don't do
1226 * hash preload there. Hence can ignore THP here
1228 ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugepage_shift);
1229 if (!ptep)
1230 goto out_exit;
1232 WARN_ON(hugepage_shift);
1233 #ifdef CONFIG_PPC_64K_PAGES
1234 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
1235 * a 64K kernel), then we don't preload, hash_page() will take
1236 * care of it once we actually try to access the page.
1237 * That way we don't have to duplicate all of the logic for segment
1238 * page size demotion here
1240 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
1241 goto out_exit;
1242 #endif /* CONFIG_PPC_64K_PAGES */
1244 /* Is that local to this CPU ? */
1245 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
1246 local = 1;
1248 /* Hash it in */
1249 #ifdef CONFIG_PPC_HAS_HASH_64K
1250 if (mm->context.user_psize == MMU_PAGE_64K)
1251 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
1252 else
1253 #endif /* CONFIG_PPC_HAS_HASH_64K */
1254 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
1255 subpage_protection(mm, ea));
1257 /* Dump some info in case of hash insertion failure, they should
1258 * never happen so it is really useful to know if/when they do
1260 if (rc == -1)
1261 hash_failure_debug(ea, access, vsid, trap, ssize,
1262 mm->context.user_psize,
1263 mm->context.user_psize,
1264 pte_val(*ptep));
1265 out_exit:
1266 local_irq_restore(flags);
1269 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
1270 * do not forget to update the assembly call site !
1272 void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
1273 int local)
1275 unsigned long hash, index, shift, hidx, slot;
1277 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1278 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1279 hash = hpt_hash(vpn, shift, ssize);
1280 hidx = __rpte_to_hidx(pte, index);
1281 if (hidx & _PTEIDX_SECONDARY)
1282 hash = ~hash;
1283 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1284 slot += hidx & _PTEIDX_GROUP_IX;
1285 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
1287 * We use same base page size and actual psize, because we don't
1288 * use these functions for hugepage
1290 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
1291 } pte_iterate_hashed_end();
1293 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1294 /* Transactions are not aborted by tlbiel, only tlbie.
1295 * Without, syncing a page back to a block device w/ PIO could pick up
1296 * transactional data (bad!) so we force an abort here. Before the
1297 * sync the page will be made read-only, which will flush_hash_page.
1298 * BIG ISSUE here: if the kernel uses a page from userspace without
1299 * unmapping it first, it may see the speculated version.
1301 if (local && cpu_has_feature(CPU_FTR_TM) &&
1302 current->thread.regs &&
1303 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1304 tm_enable();
1305 tm_abort(TM_CAUSE_TLBI);
1307 #endif
1310 void flush_hash_range(unsigned long number, int local)
1312 if (ppc_md.flush_hash_range)
1313 ppc_md.flush_hash_range(number, local);
1314 else {
1315 int i;
1316 struct ppc64_tlb_batch *batch =
1317 &__get_cpu_var(ppc64_tlb_batch);
1319 for (i = 0; i < number; i++)
1320 flush_hash_page(batch->vpn[i], batch->pte[i],
1321 batch->psize, batch->ssize, local);
1326 * low_hash_fault is called when we the low level hash code failed
1327 * to instert a PTE due to an hypervisor error
1329 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1331 enum ctx_state prev_state = exception_enter();
1333 if (user_mode(regs)) {
1334 #ifdef CONFIG_PPC_SUBPAGE_PROT
1335 if (rc == -2)
1336 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1337 else
1338 #endif
1339 _exception(SIGBUS, regs, BUS_ADRERR, address);
1340 } else
1341 bad_page_fault(regs, address, SIGBUS);
1343 exception_exit(prev_state);
1346 long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1347 unsigned long pa, unsigned long rflags,
1348 unsigned long vflags, int psize, int ssize)
1350 unsigned long hpte_group;
1351 long slot;
1353 repeat:
1354 hpte_group = ((hash & htab_hash_mask) *
1355 HPTES_PER_GROUP) & ~0x7UL;
1357 /* Insert into the hash table, primary slot */
1358 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
1359 psize, psize, ssize);
1361 /* Primary is full, try the secondary */
1362 if (unlikely(slot == -1)) {
1363 hpte_group = ((~hash & htab_hash_mask) *
1364 HPTES_PER_GROUP) & ~0x7UL;
1365 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1366 vflags | HPTE_V_SECONDARY,
1367 psize, psize, ssize);
1368 if (slot == -1) {
1369 if (mftb() & 0x1)
1370 hpte_group = ((hash & htab_hash_mask) *
1371 HPTES_PER_GROUP)&~0x7UL;
1373 ppc_md.hpte_remove(hpte_group);
1374 goto repeat;
1378 return slot;
1381 #ifdef CONFIG_DEBUG_PAGEALLOC
1382 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1384 unsigned long hash;
1385 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1386 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1387 unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
1388 long ret;
1390 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1392 /* Don't create HPTE entries for bad address */
1393 if (!vsid)
1394 return;
1396 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1397 HPTE_V_BOLTED,
1398 mmu_linear_psize, mmu_kernel_ssize);
1400 BUG_ON (ret < 0);
1401 spin_lock(&linear_map_hash_lock);
1402 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1403 linear_map_hash_slots[lmi] = ret | 0x80;
1404 spin_unlock(&linear_map_hash_lock);
1407 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1409 unsigned long hash, hidx, slot;
1410 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1411 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
1413 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
1414 spin_lock(&linear_map_hash_lock);
1415 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1416 hidx = linear_map_hash_slots[lmi] & 0x7f;
1417 linear_map_hash_slots[lmi] = 0;
1418 spin_unlock(&linear_map_hash_lock);
1419 if (hidx & _PTEIDX_SECONDARY)
1420 hash = ~hash;
1421 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1422 slot += hidx & _PTEIDX_GROUP_IX;
1423 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1424 mmu_kernel_ssize, 0);
1427 void kernel_map_pages(struct page *page, int numpages, int enable)
1429 unsigned long flags, vaddr, lmi;
1430 int i;
1432 local_irq_save(flags);
1433 for (i = 0; i < numpages; i++, page++) {
1434 vaddr = (unsigned long)page_address(page);
1435 lmi = __pa(vaddr) >> PAGE_SHIFT;
1436 if (lmi >= linear_map_hash_count)
1437 continue;
1438 if (enable)
1439 kernel_map_linear_page(vaddr, lmi);
1440 else
1441 kernel_unmap_linear_page(vaddr, lmi);
1443 local_irq_restore(flags);
1445 #endif /* CONFIG_DEBUG_PAGEALLOC */
1447 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
1448 phys_addr_t first_memblock_size)
1450 /* We don't currently support the first MEMBLOCK not mapping 0
1451 * physical on those processors
1453 BUG_ON(first_memblock_base != 0);
1455 /* On LPAR systems, the first entry is our RMA region,
1456 * non-LPAR 64-bit hash MMU systems don't have a limitation
1457 * on real mode access, but using the first entry works well
1458 * enough. We also clamp it to 1G to avoid some funky things
1459 * such as RTAS bugs etc...
1461 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1463 /* Finally limit subsequent allocations */
1464 memblock_set_current_limit(ppc64_rma_size);