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/ctype.h>
31 #include <linux/cache.h>
32 #include <linux/init.h>
33 #include <linux/signal.h>
34 #include <linux/lmb.h>
36 #include <asm/processor.h>
37 #include <asm/pgtable.h>
39 #include <asm/mmu_context.h>
41 #include <asm/types.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/machdep.h>
46 #include <asm/abs_addr.h>
47 #include <asm/tlbflush.h>
51 #include <asm/cacheflush.h>
52 #include <asm/cputable.h>
53 #include <asm/sections.h>
58 #define DBG(fmt...) udbg_printf(fmt)
64 #define DBG_LOW(fmt...) udbg_printf(fmt)
66 #define DBG_LOW(fmt...)
73 * Note: pte --> Linux PTE
74 * HPTE --> PowerPC Hashed Page Table Entry
77 * htab_initialize is called with the MMU off (of course), but
78 * the kernel has been copied down to zero so it can directly
79 * reference global data. At this point it is very difficult
80 * to print debug info.
85 extern unsigned long dart_tablebase
;
86 #endif /* CONFIG_U3_DART */
88 static unsigned long _SDR1
;
89 struct mmu_psize_def mmu_psize_defs
[MMU_PAGE_COUNT
];
91 struct hash_pte
*htab_address
;
92 unsigned long htab_size_bytes
;
93 unsigned long htab_hash_mask
;
94 int mmu_linear_psize
= MMU_PAGE_4K
;
95 int mmu_virtual_psize
= MMU_PAGE_4K
;
96 int mmu_vmalloc_psize
= MMU_PAGE_4K
;
97 int mmu_io_psize
= MMU_PAGE_4K
;
98 int mmu_kernel_ssize
= MMU_SEGSIZE_256M
;
99 int mmu_highuser_ssize
= MMU_SEGSIZE_256M
;
100 u16 mmu_slb_size
= 64;
101 #ifdef CONFIG_HUGETLB_PAGE
102 int mmu_huge_psize
= MMU_PAGE_16M
;
103 unsigned int HPAGE_SHIFT
;
105 #ifdef CONFIG_PPC_64K_PAGES
106 int mmu_ci_restrictions
;
108 #ifdef CONFIG_DEBUG_PAGEALLOC
109 static u8
*linear_map_hash_slots
;
110 static unsigned long linear_map_hash_count
;
111 static DEFINE_SPINLOCK(linear_map_hash_lock
);
112 #endif /* CONFIG_DEBUG_PAGEALLOC */
114 /* There are definitions of page sizes arrays to be used when none
115 * is provided by the firmware.
118 /* Pre-POWER4 CPUs (4k pages only)
120 struct mmu_psize_def mmu_psize_defaults_old
[] = {
130 /* POWER4, GPUL, POWER5
132 * Support for 16Mb large pages
134 struct mmu_psize_def mmu_psize_defaults_gp
[] = {
152 int htab_bolt_mapping(unsigned long vstart
, unsigned long vend
,
153 unsigned long pstart
, unsigned long mode
,
154 int psize
, int ssize
)
156 unsigned long vaddr
, paddr
;
157 unsigned int step
, shift
;
158 unsigned long tmp_mode
;
161 shift
= mmu_psize_defs
[psize
].shift
;
164 for (vaddr
= vstart
, paddr
= pstart
; vaddr
< vend
;
165 vaddr
+= step
, paddr
+= step
) {
166 unsigned long hash
, hpteg
;
167 unsigned long vsid
= get_kernel_vsid(vaddr
, ssize
);
168 unsigned long va
= hpt_va(vaddr
, vsid
, ssize
);
172 /* Make non-kernel text non-executable */
173 if (!in_kernel_text(vaddr
))
174 tmp_mode
= mode
| HPTE_R_N
;
176 hash
= hpt_hash(va
, shift
, ssize
);
177 hpteg
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
);
179 DBG("htab_bolt_mapping: calling %p\n", ppc_md
.hpte_insert
);
181 BUG_ON(!ppc_md
.hpte_insert
);
182 ret
= ppc_md
.hpte_insert(hpteg
, va
, paddr
,
183 tmp_mode
, HPTE_V_BOLTED
, psize
, ssize
);
187 #ifdef CONFIG_DEBUG_PAGEALLOC
188 if ((paddr
>> PAGE_SHIFT
) < linear_map_hash_count
)
189 linear_map_hash_slots
[paddr
>> PAGE_SHIFT
] = ret
| 0x80;
190 #endif /* CONFIG_DEBUG_PAGEALLOC */
192 return ret
< 0 ? ret
: 0;
195 static void htab_remove_mapping(unsigned long vstart
, unsigned long vend
,
196 int psize
, int ssize
)
199 unsigned int step
, shift
;
201 shift
= mmu_psize_defs
[psize
].shift
;
204 if (!ppc_md
.hpte_removebolted
) {
205 printk("Sub-arch doesn't implement hpte_removebolted\n");
209 for (vaddr
= vstart
; vaddr
< vend
; vaddr
+= step
)
210 ppc_md
.hpte_removebolted(vaddr
, psize
, ssize
);
213 static int __init
htab_dt_scan_seg_sizes(unsigned long node
,
214 const char *uname
, int depth
,
217 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
219 unsigned long size
= 0;
221 /* We are scanning "cpu" nodes only */
222 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
225 prop
= (u32
*)of_get_flat_dt_prop(node
, "ibm,processor-segment-sizes",
229 for (; size
>= 4; size
-= 4, ++prop
) {
231 DBG("1T segment support detected\n");
232 cur_cpu_spec
->cpu_features
|= CPU_FTR_1T_SEGMENT
;
236 cur_cpu_spec
->cpu_features
&= ~CPU_FTR_NO_SLBIE_B
;
240 static void __init
htab_init_seg_sizes(void)
242 of_scan_flat_dt(htab_dt_scan_seg_sizes
, NULL
);
245 static int __init
htab_dt_scan_page_sizes(unsigned long node
,
246 const char *uname
, int depth
,
249 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
251 unsigned long size
= 0;
253 /* We are scanning "cpu" nodes only */
254 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
257 prop
= (u32
*)of_get_flat_dt_prop(node
,
258 "ibm,segment-page-sizes", &size
);
260 DBG("Page sizes from device-tree:\n");
262 cur_cpu_spec
->cpu_features
&= ~(CPU_FTR_16M_PAGE
);
264 unsigned int shift
= prop
[0];
265 unsigned int slbenc
= prop
[1];
266 unsigned int lpnum
= prop
[2];
267 unsigned int lpenc
= 0;
268 struct mmu_psize_def
*def
;
271 size
-= 3; prop
+= 3;
272 while(size
> 0 && lpnum
) {
273 if (prop
[0] == shift
)
275 prop
+= 2; size
-= 2;
290 cur_cpu_spec
->cpu_features
|= CPU_FTR_16M_PAGE
;
298 def
= &mmu_psize_defs
[idx
];
303 def
->avpnm
= (1 << (shift
- 23)) - 1;
306 /* We don't know for sure what's up with tlbiel, so
307 * for now we only set it for 4K and 64K pages
309 if (idx
== MMU_PAGE_4K
|| idx
== MMU_PAGE_64K
)
314 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
315 "tlbiel=%d, penc=%d\n",
316 idx
, shift
, def
->sllp
, def
->avpnm
, def
->tlbiel
,
324 static void __init
htab_init_page_sizes(void)
328 /* Default to 4K pages only */
329 memcpy(mmu_psize_defs
, mmu_psize_defaults_old
,
330 sizeof(mmu_psize_defaults_old
));
333 * Try to find the available page sizes in the device-tree
335 rc
= of_scan_flat_dt(htab_dt_scan_page_sizes
, NULL
);
336 if (rc
!= 0) /* Found */
340 * Not in the device-tree, let's fallback on known size
341 * list for 16M capable GP & GR
343 if (cpu_has_feature(CPU_FTR_16M_PAGE
))
344 memcpy(mmu_psize_defs
, mmu_psize_defaults_gp
,
345 sizeof(mmu_psize_defaults_gp
));
347 #ifndef CONFIG_DEBUG_PAGEALLOC
349 * Pick a size for the linear mapping. Currently, we only support
350 * 16M, 1M and 4K which is the default
352 if (mmu_psize_defs
[MMU_PAGE_16M
].shift
)
353 mmu_linear_psize
= MMU_PAGE_16M
;
354 else if (mmu_psize_defs
[MMU_PAGE_1M
].shift
)
355 mmu_linear_psize
= MMU_PAGE_1M
;
356 #endif /* CONFIG_DEBUG_PAGEALLOC */
358 #ifdef CONFIG_PPC_64K_PAGES
360 * Pick a size for the ordinary pages. Default is 4K, we support
361 * 64K for user mappings and vmalloc if supported by the processor.
362 * We only use 64k for ioremap if the processor
363 * (and firmware) support cache-inhibited large pages.
364 * If not, we use 4k and set mmu_ci_restrictions so that
365 * hash_page knows to switch processes that use cache-inhibited
366 * mappings to 4k pages.
368 if (mmu_psize_defs
[MMU_PAGE_64K
].shift
) {
369 mmu_virtual_psize
= MMU_PAGE_64K
;
370 mmu_vmalloc_psize
= MMU_PAGE_64K
;
371 if (mmu_linear_psize
== MMU_PAGE_4K
)
372 mmu_linear_psize
= MMU_PAGE_64K
;
373 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE
))
374 mmu_io_psize
= MMU_PAGE_64K
;
376 mmu_ci_restrictions
= 1;
378 #endif /* CONFIG_PPC_64K_PAGES */
380 printk(KERN_DEBUG
"Page orders: linear mapping = %d, "
381 "virtual = %d, io = %d\n",
382 mmu_psize_defs
[mmu_linear_psize
].shift
,
383 mmu_psize_defs
[mmu_virtual_psize
].shift
,
384 mmu_psize_defs
[mmu_io_psize
].shift
);
386 #ifdef CONFIG_HUGETLB_PAGE
387 /* Init large page size. Currently, we pick 16M or 1M depending
388 * on what is available
390 if (mmu_psize_defs
[MMU_PAGE_16M
].shift
)
391 set_huge_psize(MMU_PAGE_16M
);
392 /* With 4k/4level pagetables, we can't (for now) cope with a
393 * huge page size < PMD_SIZE */
394 else if (mmu_psize_defs
[MMU_PAGE_1M
].shift
)
395 set_huge_psize(MMU_PAGE_1M
);
396 #endif /* CONFIG_HUGETLB_PAGE */
399 static int __init
htab_dt_scan_pftsize(unsigned long node
,
400 const char *uname
, int depth
,
403 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
406 /* We are scanning "cpu" nodes only */
407 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
410 prop
= (u32
*)of_get_flat_dt_prop(node
, "ibm,pft-size", NULL
);
412 /* pft_size[0] is the NUMA CEC cookie */
413 ppc64_pft_size
= prop
[1];
419 static unsigned long __init
htab_get_table_size(void)
421 unsigned long mem_size
, rnd_mem_size
, pteg_count
;
423 /* If hash size isn't already provided by the platform, we try to
424 * retrieve it from the device-tree. If it's not there neither, we
425 * calculate it now based on the total RAM size
427 if (ppc64_pft_size
== 0)
428 of_scan_flat_dt(htab_dt_scan_pftsize
, NULL
);
430 return 1UL << ppc64_pft_size
;
432 /* round mem_size up to next power of 2 */
433 mem_size
= lmb_phys_mem_size();
434 rnd_mem_size
= 1UL << __ilog2(mem_size
);
435 if (rnd_mem_size
< mem_size
)
439 pteg_count
= max(rnd_mem_size
>> (12 + 1), 1UL << 11);
441 return pteg_count
<< 7;
444 #ifdef CONFIG_MEMORY_HOTPLUG
445 void create_section_mapping(unsigned long start
, unsigned long end
)
447 BUG_ON(htab_bolt_mapping(start
, end
, __pa(start
),
448 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_COHERENT
| PP_RWXX
,
449 mmu_linear_psize
, mmu_kernel_ssize
));
452 void remove_section_mapping(unsigned long start
, unsigned long end
)
454 htab_remove_mapping(start
, end
, mmu_linear_psize
, mmu_kernel_ssize
);
456 #endif /* CONFIG_MEMORY_HOTPLUG */
458 static inline void make_bl(unsigned int *insn_addr
, void *func
)
460 unsigned long funcp
= *((unsigned long *)func
);
461 int offset
= funcp
- (unsigned long)insn_addr
;
463 *insn_addr
= (unsigned int)(0x48000001 | (offset
& 0x03fffffc));
464 flush_icache_range((unsigned long)insn_addr
, 4+
465 (unsigned long)insn_addr
);
468 static void __init
htab_finish_init(void)
470 extern unsigned int *htab_call_hpte_insert1
;
471 extern unsigned int *htab_call_hpte_insert2
;
472 extern unsigned int *htab_call_hpte_remove
;
473 extern unsigned int *htab_call_hpte_updatepp
;
475 #ifdef CONFIG_PPC_HAS_HASH_64K
476 extern unsigned int *ht64_call_hpte_insert1
;
477 extern unsigned int *ht64_call_hpte_insert2
;
478 extern unsigned int *ht64_call_hpte_remove
;
479 extern unsigned int *ht64_call_hpte_updatepp
;
481 make_bl(ht64_call_hpte_insert1
, ppc_md
.hpte_insert
);
482 make_bl(ht64_call_hpte_insert2
, ppc_md
.hpte_insert
);
483 make_bl(ht64_call_hpte_remove
, ppc_md
.hpte_remove
);
484 make_bl(ht64_call_hpte_updatepp
, ppc_md
.hpte_updatepp
);
485 #endif /* CONFIG_PPC_HAS_HASH_64K */
487 make_bl(htab_call_hpte_insert1
, ppc_md
.hpte_insert
);
488 make_bl(htab_call_hpte_insert2
, ppc_md
.hpte_insert
);
489 make_bl(htab_call_hpte_remove
, ppc_md
.hpte_remove
);
490 make_bl(htab_call_hpte_updatepp
, ppc_md
.hpte_updatepp
);
493 void __init
htab_initialize(void)
496 unsigned long pteg_count
;
497 unsigned long mode_rw
;
498 unsigned long base
= 0, size
= 0, limit
;
501 extern unsigned long tce_alloc_start
, tce_alloc_end
;
503 DBG(" -> htab_initialize()\n");
505 /* Initialize segment sizes */
506 htab_init_seg_sizes();
508 /* Initialize page sizes */
509 htab_init_page_sizes();
511 if (cpu_has_feature(CPU_FTR_1T_SEGMENT
)) {
512 mmu_kernel_ssize
= MMU_SEGSIZE_1T
;
513 mmu_highuser_ssize
= MMU_SEGSIZE_1T
;
514 printk(KERN_INFO
"Using 1TB segments\n");
518 * Calculate the required size of the htab. We want the number of
519 * PTEGs to equal one half the number of real pages.
521 htab_size_bytes
= htab_get_table_size();
522 pteg_count
= htab_size_bytes
>> 7;
524 htab_hash_mask
= pteg_count
- 1;
526 if (firmware_has_feature(FW_FEATURE_LPAR
)) {
527 /* Using a hypervisor which owns the htab */
531 /* Find storage for the HPT. Must be contiguous in
532 * the absolute address space. On cell we want it to be
533 * in the first 2 Gig so we can use it for IOMMU hacks.
535 if (machine_is(cell
))
540 table
= lmb_alloc_base(htab_size_bytes
, htab_size_bytes
, limit
);
542 DBG("Hash table allocated at %lx, size: %lx\n", table
,
545 htab_address
= abs_to_virt(table
);
547 /* htab absolute addr + encoded htabsize */
548 _SDR1
= table
+ __ilog2(pteg_count
) - 11;
550 /* Initialize the HPT with no entries */
551 memset((void *)table
, 0, htab_size_bytes
);
554 mtspr(SPRN_SDR1
, _SDR1
);
557 mode_rw
= _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_COHERENT
| PP_RWXX
;
559 #ifdef CONFIG_DEBUG_PAGEALLOC
560 linear_map_hash_count
= lmb_end_of_DRAM() >> PAGE_SHIFT
;
561 linear_map_hash_slots
= __va(lmb_alloc_base(linear_map_hash_count
,
563 memset(linear_map_hash_slots
, 0, linear_map_hash_count
);
564 #endif /* CONFIG_DEBUG_PAGEALLOC */
566 /* On U3 based machines, we need to reserve the DART area and
567 * _NOT_ map it to avoid cache paradoxes as it's remapped non
571 /* create bolted the linear mapping in the hash table */
572 for (i
=0; i
< lmb
.memory
.cnt
; i
++) {
573 base
= (unsigned long)__va(lmb
.memory
.region
[i
].base
);
574 size
= lmb
.memory
.region
[i
].size
;
576 DBG("creating mapping for region: %lx : %lx\n", base
, size
);
578 #ifdef CONFIG_U3_DART
579 /* Do not map the DART space. Fortunately, it will be aligned
580 * in such a way that it will not cross two lmb regions and
581 * will fit within a single 16Mb page.
582 * The DART space is assumed to be a full 16Mb region even if
583 * we only use 2Mb of that space. We will use more of it later
584 * for AGP GART. We have to use a full 16Mb large page.
586 DBG("DART base: %lx\n", dart_tablebase
);
588 if (dart_tablebase
!= 0 && dart_tablebase
>= base
589 && dart_tablebase
< (base
+ size
)) {
590 unsigned long dart_table_end
= dart_tablebase
+ 16 * MB
;
591 if (base
!= dart_tablebase
)
592 BUG_ON(htab_bolt_mapping(base
, dart_tablebase
,
596 if ((base
+ size
) > dart_table_end
)
597 BUG_ON(htab_bolt_mapping(dart_tablebase
+16*MB
,
599 __pa(dart_table_end
),
605 #endif /* CONFIG_U3_DART */
606 BUG_ON(htab_bolt_mapping(base
, base
+ size
, __pa(base
),
607 mode_rw
, mmu_linear_psize
, mmu_kernel_ssize
));
611 * If we have a memory_limit and we've allocated TCEs then we need to
612 * explicitly map the TCE area at the top of RAM. We also cope with the
613 * case that the TCEs start below memory_limit.
614 * tce_alloc_start/end are 16MB aligned so the mapping should work
615 * for either 4K or 16MB pages.
617 if (tce_alloc_start
) {
618 tce_alloc_start
= (unsigned long)__va(tce_alloc_start
);
619 tce_alloc_end
= (unsigned long)__va(tce_alloc_end
);
621 if (base
+ size
>= tce_alloc_start
)
622 tce_alloc_start
= base
+ size
+ 1;
624 BUG_ON(htab_bolt_mapping(tce_alloc_start
, tce_alloc_end
,
625 __pa(tce_alloc_start
), mode_rw
,
626 mmu_linear_psize
, mmu_kernel_ssize
));
631 DBG(" <- htab_initialize()\n");
636 void htab_initialize_secondary(void)
638 if (!firmware_has_feature(FW_FEATURE_LPAR
))
639 mtspr(SPRN_SDR1
, _SDR1
);
643 * Called by asm hashtable.S for doing lazy icache flush
645 unsigned int hash_page_do_lazy_icache(unsigned int pp
, pte_t pte
, int trap
)
649 if (!pfn_valid(pte_pfn(pte
)))
652 page
= pte_page(pte
);
655 if (!test_bit(PG_arch_1
, &page
->flags
) && !PageReserved(page
)) {
657 __flush_dcache_icache(page_address(page
));
658 set_bit(PG_arch_1
, &page
->flags
);
666 * Demote a segment to using 4k pages.
667 * For now this makes the whole process use 4k pages.
669 #ifdef CONFIG_PPC_64K_PAGES
670 void demote_segment_4k(struct mm_struct
*mm
, unsigned long addr
)
672 if (mm
->context
.user_psize
== MMU_PAGE_4K
)
674 slice_set_user_psize(mm
, MMU_PAGE_4K
);
675 #ifdef CONFIG_SPU_BASE
676 spu_flush_all_slbs(mm
);
678 if (get_paca()->context
.user_psize
!= MMU_PAGE_4K
) {
679 get_paca()->context
= mm
->context
;
680 slb_flush_and_rebolt();
683 #endif /* CONFIG_PPC_64K_PAGES */
685 #ifdef CONFIG_PPC_SUBPAGE_PROT
687 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
688 * Userspace sets the subpage permissions using the subpage_prot system call.
690 * Result is 0: full permissions, _PAGE_RW: read-only,
691 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
693 static int subpage_protection(pgd_t
*pgdir
, unsigned long ea
)
695 struct subpage_prot_table
*spt
= pgd_subpage_prot(pgdir
);
699 if (ea
>= spt
->maxaddr
)
701 if (ea
< 0x100000000) {
702 /* addresses below 4GB use spt->low_prot */
703 sbpm
= spt
->low_prot
;
705 sbpm
= spt
->protptrs
[ea
>> SBP_L3_SHIFT
];
709 sbpp
= sbpm
[(ea
>> SBP_L2_SHIFT
) & (SBP_L2_COUNT
- 1)];
712 spp
= sbpp
[(ea
>> PAGE_SHIFT
) & (SBP_L1_COUNT
- 1)];
714 /* extract 2-bit bitfield for this 4k subpage */
715 spp
>>= 30 - 2 * ((ea
>> 12) & 0xf);
717 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
718 spp
= ((spp
& 2) ? _PAGE_USER
: 0) | ((spp
& 1) ? _PAGE_RW
: 0);
722 #else /* CONFIG_PPC_SUBPAGE_PROT */
723 static inline int subpage_protection(pgd_t
*pgdir
, unsigned long ea
)
731 * 1 - normal page fault
732 * -1 - critical hash insertion error
733 * -2 - access not permitted by subpage protection mechanism
735 int hash_page(unsigned long ea
, unsigned long access
, unsigned long trap
)
739 struct mm_struct
*mm
;
742 int rc
, user_region
= 0, local
= 0;
745 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
748 if ((ea
& ~REGION_MASK
) >= PGTABLE_RANGE
) {
749 DBG_LOW(" out of pgtable range !\n");
753 /* Get region & vsid */
754 switch (REGION_ID(ea
)) {
759 DBG_LOW(" user region with no mm !\n");
762 #ifdef CONFIG_PPC_MM_SLICES
763 psize
= get_slice_psize(mm
, ea
);
765 psize
= mm
->context
.user_psize
;
767 ssize
= user_segment_size(ea
);
768 vsid
= get_vsid(mm
->context
.id
, ea
, ssize
);
770 case VMALLOC_REGION_ID
:
772 vsid
= get_kernel_vsid(ea
, mmu_kernel_ssize
);
773 if (ea
< VMALLOC_END
)
774 psize
= mmu_vmalloc_psize
;
776 psize
= mmu_io_psize
;
777 ssize
= mmu_kernel_ssize
;
781 * Send the problem up to do_page_fault
785 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm
, mm
->pgd
, vsid
);
792 /* Check CPU locality */
793 tmp
= cpumask_of_cpu(smp_processor_id());
794 if (user_region
&& cpus_equal(mm
->cpu_vm_mask
, tmp
))
797 #ifdef CONFIG_HUGETLB_PAGE
798 /* Handle hugepage regions */
799 if (HPAGE_SHIFT
&& psize
== mmu_huge_psize
) {
800 DBG_LOW(" -> huge page !\n");
801 return hash_huge_page(mm
, access
, ea
, vsid
, local
, trap
);
803 #endif /* CONFIG_HUGETLB_PAGE */
805 #ifndef CONFIG_PPC_64K_PAGES
806 /* If we use 4K pages and our psize is not 4K, then we are hitting
807 * a special driver mapping, we need to align the address before
810 if (psize
!= MMU_PAGE_4K
)
811 ea
&= ~((1ul << mmu_psize_defs
[psize
].shift
) - 1);
812 #endif /* CONFIG_PPC_64K_PAGES */
814 /* Get PTE and page size from page tables */
815 ptep
= find_linux_pte(pgdir
, ea
);
816 if (ptep
== NULL
|| !pte_present(*ptep
)) {
817 DBG_LOW(" no PTE !\n");
821 #ifndef CONFIG_PPC_64K_PAGES
822 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep
));
824 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep
),
825 pte_val(*(ptep
+ PTRS_PER_PTE
)));
827 /* Pre-check access permissions (will be re-checked atomically
828 * in __hash_page_XX but this pre-check is a fast path
830 if (access
& ~pte_val(*ptep
)) {
831 DBG_LOW(" no access !\n");
835 /* Do actual hashing */
836 #ifdef CONFIG_PPC_64K_PAGES
837 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
838 if (pte_val(*ptep
) & _PAGE_4K_PFN
) {
839 demote_segment_4k(mm
, ea
);
843 /* If this PTE is non-cacheable and we have restrictions on
844 * using non cacheable large pages, then we switch to 4k
846 if (mmu_ci_restrictions
&& psize
== MMU_PAGE_64K
&&
847 (pte_val(*ptep
) & _PAGE_NO_CACHE
)) {
849 demote_segment_4k(mm
, ea
);
851 } else if (ea
< VMALLOC_END
) {
853 * some driver did a non-cacheable mapping
854 * in vmalloc space, so switch vmalloc
857 printk(KERN_ALERT
"Reducing vmalloc segment "
858 "to 4kB pages because of "
859 "non-cacheable mapping\n");
860 psize
= mmu_vmalloc_psize
= MMU_PAGE_4K
;
861 #ifdef CONFIG_SPU_BASE
862 spu_flush_all_slbs(mm
);
867 if (psize
!= get_paca()->context
.user_psize
) {
868 get_paca()->context
= mm
->context
;
869 slb_flush_and_rebolt();
871 } else if (get_paca()->vmalloc_sllp
!=
872 mmu_psize_defs
[mmu_vmalloc_psize
].sllp
) {
873 get_paca()->vmalloc_sllp
=
874 mmu_psize_defs
[mmu_vmalloc_psize
].sllp
;
875 slb_vmalloc_update();
877 #endif /* CONFIG_PPC_64K_PAGES */
879 #ifdef CONFIG_PPC_HAS_HASH_64K
880 if (psize
== MMU_PAGE_64K
)
881 rc
= __hash_page_64K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
);
883 #endif /* CONFIG_PPC_HAS_HASH_64K */
885 int spp
= subpage_protection(pgdir
, ea
);
889 rc
= __hash_page_4K(ea
, access
, vsid
, ptep
, trap
,
893 #ifndef CONFIG_PPC_64K_PAGES
894 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep
));
896 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep
),
897 pte_val(*(ptep
+ PTRS_PER_PTE
)));
899 DBG_LOW(" -> rc=%d\n", rc
);
902 EXPORT_SYMBOL_GPL(hash_page
);
904 void hash_preload(struct mm_struct
*mm
, unsigned long ea
,
905 unsigned long access
, unsigned long trap
)
915 BUG_ON(REGION_ID(ea
) != USER_REGION_ID
);
917 #ifdef CONFIG_PPC_MM_SLICES
918 /* We only prefault standard pages for now */
919 if (unlikely(get_slice_psize(mm
, ea
) != mm
->context
.user_psize
))
923 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
924 " trap=%lx\n", mm
, mm
->pgd
, ea
, access
, trap
);
926 /* Get Linux PTE if available */
930 ptep
= find_linux_pte(pgdir
, ea
);
934 #ifdef CONFIG_PPC_64K_PAGES
935 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
936 * a 64K kernel), then we don't preload, hash_page() will take
937 * care of it once we actually try to access the page.
938 * That way we don't have to duplicate all of the logic for segment
939 * page size demotion here
941 if (pte_val(*ptep
) & (_PAGE_4K_PFN
| _PAGE_NO_CACHE
))
943 #endif /* CONFIG_PPC_64K_PAGES */
946 ssize
= user_segment_size(ea
);
947 vsid
= get_vsid(mm
->context
.id
, ea
, ssize
);
949 /* Hash doesn't like irqs */
950 local_irq_save(flags
);
952 /* Is that local to this CPU ? */
953 mask
= cpumask_of_cpu(smp_processor_id());
954 if (cpus_equal(mm
->cpu_vm_mask
, mask
))
958 #ifdef CONFIG_PPC_HAS_HASH_64K
959 if (mm
->context
.user_psize
== MMU_PAGE_64K
)
960 __hash_page_64K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
);
962 #endif /* CONFIG_PPC_HAS_HASH_64K */
963 __hash_page_4K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
,
964 subpage_protection(pgdir
, ea
));
966 local_irq_restore(flags
);
969 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
970 * do not forget to update the assembly call site !
972 void flush_hash_page(unsigned long va
, real_pte_t pte
, int psize
, int ssize
,
975 unsigned long hash
, index
, shift
, hidx
, slot
;
977 DBG_LOW("flush_hash_page(va=%016x)\n", va
);
978 pte_iterate_hashed_subpages(pte
, psize
, va
, index
, shift
) {
979 hash
= hpt_hash(va
, shift
, ssize
);
980 hidx
= __rpte_to_hidx(pte
, index
);
981 if (hidx
& _PTEIDX_SECONDARY
)
983 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
984 slot
+= hidx
& _PTEIDX_GROUP_IX
;
985 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index
, slot
, hidx
);
986 ppc_md
.hpte_invalidate(slot
, va
, psize
, ssize
, local
);
987 } pte_iterate_hashed_end();
990 void flush_hash_range(unsigned long number
, int local
)
992 if (ppc_md
.flush_hash_range
)
993 ppc_md
.flush_hash_range(number
, local
);
996 struct ppc64_tlb_batch
*batch
=
997 &__get_cpu_var(ppc64_tlb_batch
);
999 for (i
= 0; i
< number
; i
++)
1000 flush_hash_page(batch
->vaddr
[i
], batch
->pte
[i
],
1001 batch
->psize
, batch
->ssize
, local
);
1006 * low_hash_fault is called when we the low level hash code failed
1007 * to instert a PTE due to an hypervisor error
1009 void low_hash_fault(struct pt_regs
*regs
, unsigned long address
, int rc
)
1011 if (user_mode(regs
)) {
1012 #ifdef CONFIG_PPC_SUBPAGE_PROT
1014 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, address
);
1017 _exception(SIGBUS
, regs
, BUS_ADRERR
, address
);
1019 bad_page_fault(regs
, address
, SIGBUS
);
1022 #ifdef CONFIG_DEBUG_PAGEALLOC
1023 static void kernel_map_linear_page(unsigned long vaddr
, unsigned long lmi
)
1025 unsigned long hash
, hpteg
;
1026 unsigned long vsid
= get_kernel_vsid(vaddr
, mmu_kernel_ssize
);
1027 unsigned long va
= hpt_va(vaddr
, vsid
, mmu_kernel_ssize
);
1028 unsigned long mode
= _PAGE_ACCESSED
| _PAGE_DIRTY
|
1029 _PAGE_COHERENT
| PP_RWXX
| HPTE_R_N
;
1032 hash
= hpt_hash(va
, PAGE_SHIFT
, mmu_kernel_ssize
);
1033 hpteg
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
);
1035 ret
= ppc_md
.hpte_insert(hpteg
, va
, __pa(vaddr
),
1036 mode
, HPTE_V_BOLTED
,
1037 mmu_linear_psize
, mmu_kernel_ssize
);
1039 spin_lock(&linear_map_hash_lock
);
1040 BUG_ON(linear_map_hash_slots
[lmi
] & 0x80);
1041 linear_map_hash_slots
[lmi
] = ret
| 0x80;
1042 spin_unlock(&linear_map_hash_lock
);
1045 static void kernel_unmap_linear_page(unsigned long vaddr
, unsigned long lmi
)
1047 unsigned long hash
, hidx
, slot
;
1048 unsigned long vsid
= get_kernel_vsid(vaddr
, mmu_kernel_ssize
);
1049 unsigned long va
= hpt_va(vaddr
, vsid
, mmu_kernel_ssize
);
1051 hash
= hpt_hash(va
, PAGE_SHIFT
, mmu_kernel_ssize
);
1052 spin_lock(&linear_map_hash_lock
);
1053 BUG_ON(!(linear_map_hash_slots
[lmi
] & 0x80));
1054 hidx
= linear_map_hash_slots
[lmi
] & 0x7f;
1055 linear_map_hash_slots
[lmi
] = 0;
1056 spin_unlock(&linear_map_hash_lock
);
1057 if (hidx
& _PTEIDX_SECONDARY
)
1059 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
1060 slot
+= hidx
& _PTEIDX_GROUP_IX
;
1061 ppc_md
.hpte_invalidate(slot
, va
, mmu_linear_psize
, mmu_kernel_ssize
, 0);
1064 void kernel_map_pages(struct page
*page
, int numpages
, int enable
)
1066 unsigned long flags
, vaddr
, lmi
;
1069 local_irq_save(flags
);
1070 for (i
= 0; i
< numpages
; i
++, page
++) {
1071 vaddr
= (unsigned long)page_address(page
);
1072 lmi
= __pa(vaddr
) >> PAGE_SHIFT
;
1073 if (lmi
>= linear_map_hash_count
)
1076 kernel_map_linear_page(vaddr
, lmi
);
1078 kernel_unmap_linear_page(vaddr
, lmi
);
1080 local_irq_restore(flags
);
1082 #endif /* CONFIG_DEBUG_PAGEALLOC */