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
)) {
375 * Don't use 64k pages for ioremap on pSeries, since
376 * that would stop us accessing the HEA ethernet.
378 if (!machine_is(pseries
))
379 mmu_io_psize
= MMU_PAGE_64K
;
381 mmu_ci_restrictions
= 1;
383 #endif /* CONFIG_PPC_64K_PAGES */
385 printk(KERN_DEBUG
"Page orders: linear mapping = %d, "
386 "virtual = %d, io = %d\n",
387 mmu_psize_defs
[mmu_linear_psize
].shift
,
388 mmu_psize_defs
[mmu_virtual_psize
].shift
,
389 mmu_psize_defs
[mmu_io_psize
].shift
);
391 #ifdef CONFIG_HUGETLB_PAGE
392 /* Init large page size. Currently, we pick 16M or 1M depending
393 * on what is available
395 if (mmu_psize_defs
[MMU_PAGE_16M
].shift
)
396 set_huge_psize(MMU_PAGE_16M
);
397 /* With 4k/4level pagetables, we can't (for now) cope with a
398 * huge page size < PMD_SIZE */
399 else if (mmu_psize_defs
[MMU_PAGE_1M
].shift
)
400 set_huge_psize(MMU_PAGE_1M
);
401 #endif /* CONFIG_HUGETLB_PAGE */
404 static int __init
htab_dt_scan_pftsize(unsigned long node
,
405 const char *uname
, int depth
,
408 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
411 /* We are scanning "cpu" nodes only */
412 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
415 prop
= (u32
*)of_get_flat_dt_prop(node
, "ibm,pft-size", NULL
);
417 /* pft_size[0] is the NUMA CEC cookie */
418 ppc64_pft_size
= prop
[1];
424 static unsigned long __init
htab_get_table_size(void)
426 unsigned long mem_size
, rnd_mem_size
, pteg_count
;
428 /* If hash size isn't already provided by the platform, we try to
429 * retrieve it from the device-tree. If it's not there neither, we
430 * calculate it now based on the total RAM size
432 if (ppc64_pft_size
== 0)
433 of_scan_flat_dt(htab_dt_scan_pftsize
, NULL
);
435 return 1UL << ppc64_pft_size
;
437 /* round mem_size up to next power of 2 */
438 mem_size
= lmb_phys_mem_size();
439 rnd_mem_size
= 1UL << __ilog2(mem_size
);
440 if (rnd_mem_size
< mem_size
)
444 pteg_count
= max(rnd_mem_size
>> (12 + 1), 1UL << 11);
446 return pteg_count
<< 7;
449 #ifdef CONFIG_MEMORY_HOTPLUG
450 void create_section_mapping(unsigned long start
, unsigned long end
)
452 BUG_ON(htab_bolt_mapping(start
, end
, __pa(start
),
453 _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_COHERENT
| PP_RWXX
,
454 mmu_linear_psize
, mmu_kernel_ssize
));
457 void remove_section_mapping(unsigned long start
, unsigned long end
)
459 htab_remove_mapping(start
, end
, mmu_linear_psize
, mmu_kernel_ssize
);
461 #endif /* CONFIG_MEMORY_HOTPLUG */
463 static inline void make_bl(unsigned int *insn_addr
, void *func
)
465 unsigned long funcp
= *((unsigned long *)func
);
466 int offset
= funcp
- (unsigned long)insn_addr
;
468 *insn_addr
= (unsigned int)(0x48000001 | (offset
& 0x03fffffc));
469 flush_icache_range((unsigned long)insn_addr
, 4+
470 (unsigned long)insn_addr
);
473 static void __init
htab_finish_init(void)
475 extern unsigned int *htab_call_hpte_insert1
;
476 extern unsigned int *htab_call_hpte_insert2
;
477 extern unsigned int *htab_call_hpte_remove
;
478 extern unsigned int *htab_call_hpte_updatepp
;
480 #ifdef CONFIG_PPC_HAS_HASH_64K
481 extern unsigned int *ht64_call_hpte_insert1
;
482 extern unsigned int *ht64_call_hpte_insert2
;
483 extern unsigned int *ht64_call_hpte_remove
;
484 extern unsigned int *ht64_call_hpte_updatepp
;
486 make_bl(ht64_call_hpte_insert1
, ppc_md
.hpte_insert
);
487 make_bl(ht64_call_hpte_insert2
, ppc_md
.hpte_insert
);
488 make_bl(ht64_call_hpte_remove
, ppc_md
.hpte_remove
);
489 make_bl(ht64_call_hpte_updatepp
, ppc_md
.hpte_updatepp
);
490 #endif /* CONFIG_PPC_HAS_HASH_64K */
492 make_bl(htab_call_hpte_insert1
, ppc_md
.hpte_insert
);
493 make_bl(htab_call_hpte_insert2
, ppc_md
.hpte_insert
);
494 make_bl(htab_call_hpte_remove
, ppc_md
.hpte_remove
);
495 make_bl(htab_call_hpte_updatepp
, ppc_md
.hpte_updatepp
);
498 void __init
htab_initialize(void)
501 unsigned long pteg_count
;
502 unsigned long mode_rw
;
503 unsigned long base
= 0, size
= 0, limit
;
506 extern unsigned long tce_alloc_start
, tce_alloc_end
;
508 DBG(" -> htab_initialize()\n");
510 /* Initialize segment sizes */
511 htab_init_seg_sizes();
513 /* Initialize page sizes */
514 htab_init_page_sizes();
516 if (cpu_has_feature(CPU_FTR_1T_SEGMENT
)) {
517 mmu_kernel_ssize
= MMU_SEGSIZE_1T
;
518 mmu_highuser_ssize
= MMU_SEGSIZE_1T
;
519 printk(KERN_INFO
"Using 1TB segments\n");
523 * Calculate the required size of the htab. We want the number of
524 * PTEGs to equal one half the number of real pages.
526 htab_size_bytes
= htab_get_table_size();
527 pteg_count
= htab_size_bytes
>> 7;
529 htab_hash_mask
= pteg_count
- 1;
531 if (firmware_has_feature(FW_FEATURE_LPAR
)) {
532 /* Using a hypervisor which owns the htab */
536 /* Find storage for the HPT. Must be contiguous in
537 * the absolute address space. On cell we want it to be
538 * in the first 2 Gig so we can use it for IOMMU hacks.
540 if (machine_is(cell
))
545 table
= lmb_alloc_base(htab_size_bytes
, htab_size_bytes
, limit
);
547 DBG("Hash table allocated at %lx, size: %lx\n", table
,
550 htab_address
= abs_to_virt(table
);
552 /* htab absolute addr + encoded htabsize */
553 _SDR1
= table
+ __ilog2(pteg_count
) - 11;
555 /* Initialize the HPT with no entries */
556 memset((void *)table
, 0, htab_size_bytes
);
559 mtspr(SPRN_SDR1
, _SDR1
);
562 mode_rw
= _PAGE_ACCESSED
| _PAGE_DIRTY
| _PAGE_COHERENT
| PP_RWXX
;
564 #ifdef CONFIG_DEBUG_PAGEALLOC
565 linear_map_hash_count
= lmb_end_of_DRAM() >> PAGE_SHIFT
;
566 linear_map_hash_slots
= __va(lmb_alloc_base(linear_map_hash_count
,
568 memset(linear_map_hash_slots
, 0, linear_map_hash_count
);
569 #endif /* CONFIG_DEBUG_PAGEALLOC */
571 /* On U3 based machines, we need to reserve the DART area and
572 * _NOT_ map it to avoid cache paradoxes as it's remapped non
576 /* create bolted the linear mapping in the hash table */
577 for (i
=0; i
< lmb
.memory
.cnt
; i
++) {
578 base
= (unsigned long)__va(lmb
.memory
.region
[i
].base
);
579 size
= lmb
.memory
.region
[i
].size
;
581 DBG("creating mapping for region: %lx : %lx\n", base
, size
);
583 #ifdef CONFIG_U3_DART
584 /* Do not map the DART space. Fortunately, it will be aligned
585 * in such a way that it will not cross two lmb regions and
586 * will fit within a single 16Mb page.
587 * The DART space is assumed to be a full 16Mb region even if
588 * we only use 2Mb of that space. We will use more of it later
589 * for AGP GART. We have to use a full 16Mb large page.
591 DBG("DART base: %lx\n", dart_tablebase
);
593 if (dart_tablebase
!= 0 && dart_tablebase
>= base
594 && dart_tablebase
< (base
+ size
)) {
595 unsigned long dart_table_end
= dart_tablebase
+ 16 * MB
;
596 if (base
!= dart_tablebase
)
597 BUG_ON(htab_bolt_mapping(base
, dart_tablebase
,
601 if ((base
+ size
) > dart_table_end
)
602 BUG_ON(htab_bolt_mapping(dart_tablebase
+16*MB
,
604 __pa(dart_table_end
),
610 #endif /* CONFIG_U3_DART */
611 BUG_ON(htab_bolt_mapping(base
, base
+ size
, __pa(base
),
612 mode_rw
, mmu_linear_psize
, mmu_kernel_ssize
));
616 * If we have a memory_limit and we've allocated TCEs then we need to
617 * explicitly map the TCE area at the top of RAM. We also cope with the
618 * case that the TCEs start below memory_limit.
619 * tce_alloc_start/end are 16MB aligned so the mapping should work
620 * for either 4K or 16MB pages.
622 if (tce_alloc_start
) {
623 tce_alloc_start
= (unsigned long)__va(tce_alloc_start
);
624 tce_alloc_end
= (unsigned long)__va(tce_alloc_end
);
626 if (base
+ size
>= tce_alloc_start
)
627 tce_alloc_start
= base
+ size
+ 1;
629 BUG_ON(htab_bolt_mapping(tce_alloc_start
, tce_alloc_end
,
630 __pa(tce_alloc_start
), mode_rw
,
631 mmu_linear_psize
, mmu_kernel_ssize
));
636 DBG(" <- htab_initialize()\n");
641 void htab_initialize_secondary(void)
643 if (!firmware_has_feature(FW_FEATURE_LPAR
))
644 mtspr(SPRN_SDR1
, _SDR1
);
648 * Called by asm hashtable.S for doing lazy icache flush
650 unsigned int hash_page_do_lazy_icache(unsigned int pp
, pte_t pte
, int trap
)
654 if (!pfn_valid(pte_pfn(pte
)))
657 page
= pte_page(pte
);
660 if (!test_bit(PG_arch_1
, &page
->flags
) && !PageReserved(page
)) {
662 __flush_dcache_icache(page_address(page
));
663 set_bit(PG_arch_1
, &page
->flags
);
671 * Demote a segment to using 4k pages.
672 * For now this makes the whole process use 4k pages.
674 #ifdef CONFIG_PPC_64K_PAGES
675 void demote_segment_4k(struct mm_struct
*mm
, unsigned long addr
)
677 if (mm
->context
.user_psize
== MMU_PAGE_4K
)
679 slice_set_user_psize(mm
, MMU_PAGE_4K
);
680 #ifdef CONFIG_SPU_BASE
681 spu_flush_all_slbs(mm
);
683 if (get_paca()->context
.user_psize
!= MMU_PAGE_4K
) {
684 get_paca()->context
= mm
->context
;
685 slb_flush_and_rebolt();
688 #endif /* CONFIG_PPC_64K_PAGES */
690 #ifdef CONFIG_PPC_SUBPAGE_PROT
692 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
693 * Userspace sets the subpage permissions using the subpage_prot system call.
695 * Result is 0: full permissions, _PAGE_RW: read-only,
696 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
698 static int subpage_protection(pgd_t
*pgdir
, unsigned long ea
)
700 struct subpage_prot_table
*spt
= pgd_subpage_prot(pgdir
);
704 if (ea
>= spt
->maxaddr
)
706 if (ea
< 0x100000000) {
707 /* addresses below 4GB use spt->low_prot */
708 sbpm
= spt
->low_prot
;
710 sbpm
= spt
->protptrs
[ea
>> SBP_L3_SHIFT
];
714 sbpp
= sbpm
[(ea
>> SBP_L2_SHIFT
) & (SBP_L2_COUNT
- 1)];
717 spp
= sbpp
[(ea
>> PAGE_SHIFT
) & (SBP_L1_COUNT
- 1)];
719 /* extract 2-bit bitfield for this 4k subpage */
720 spp
>>= 30 - 2 * ((ea
>> 12) & 0xf);
722 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
723 spp
= ((spp
& 2) ? _PAGE_USER
: 0) | ((spp
& 1) ? _PAGE_RW
: 0);
727 #else /* CONFIG_PPC_SUBPAGE_PROT */
728 static inline int subpage_protection(pgd_t
*pgdir
, unsigned long ea
)
736 * 1 - normal page fault
737 * -1 - critical hash insertion error
738 * -2 - access not permitted by subpage protection mechanism
740 int hash_page(unsigned long ea
, unsigned long access
, unsigned long trap
)
744 struct mm_struct
*mm
;
747 int rc
, user_region
= 0, local
= 0;
750 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
753 if ((ea
& ~REGION_MASK
) >= PGTABLE_RANGE
) {
754 DBG_LOW(" out of pgtable range !\n");
758 /* Get region & vsid */
759 switch (REGION_ID(ea
)) {
764 DBG_LOW(" user region with no mm !\n");
767 #ifdef CONFIG_PPC_MM_SLICES
768 psize
= get_slice_psize(mm
, ea
);
770 psize
= mm
->context
.user_psize
;
772 ssize
= user_segment_size(ea
);
773 vsid
= get_vsid(mm
->context
.id
, ea
, ssize
);
775 case VMALLOC_REGION_ID
:
777 vsid
= get_kernel_vsid(ea
, mmu_kernel_ssize
);
778 if (ea
< VMALLOC_END
)
779 psize
= mmu_vmalloc_psize
;
781 psize
= mmu_io_psize
;
782 ssize
= mmu_kernel_ssize
;
786 * Send the problem up to do_page_fault
790 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm
, mm
->pgd
, vsid
);
797 /* Check CPU locality */
798 tmp
= cpumask_of_cpu(smp_processor_id());
799 if (user_region
&& cpus_equal(mm
->cpu_vm_mask
, tmp
))
802 #ifdef CONFIG_HUGETLB_PAGE
803 /* Handle hugepage regions */
804 if (HPAGE_SHIFT
&& psize
== mmu_huge_psize
) {
805 DBG_LOW(" -> huge page !\n");
806 return hash_huge_page(mm
, access
, ea
, vsid
, local
, trap
);
808 #endif /* CONFIG_HUGETLB_PAGE */
810 #ifndef CONFIG_PPC_64K_PAGES
811 /* If we use 4K pages and our psize is not 4K, then we are hitting
812 * a special driver mapping, we need to align the address before
815 if (psize
!= MMU_PAGE_4K
)
816 ea
&= ~((1ul << mmu_psize_defs
[psize
].shift
) - 1);
817 #endif /* CONFIG_PPC_64K_PAGES */
819 /* Get PTE and page size from page tables */
820 ptep
= find_linux_pte(pgdir
, ea
);
821 if (ptep
== NULL
|| !pte_present(*ptep
)) {
822 DBG_LOW(" no PTE !\n");
826 #ifndef CONFIG_PPC_64K_PAGES
827 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep
));
829 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep
),
830 pte_val(*(ptep
+ PTRS_PER_PTE
)));
832 /* Pre-check access permissions (will be re-checked atomically
833 * in __hash_page_XX but this pre-check is a fast path
835 if (access
& ~pte_val(*ptep
)) {
836 DBG_LOW(" no access !\n");
840 /* Do actual hashing */
841 #ifdef CONFIG_PPC_64K_PAGES
842 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
843 if (pte_val(*ptep
) & _PAGE_4K_PFN
) {
844 demote_segment_4k(mm
, ea
);
848 /* If this PTE is non-cacheable and we have restrictions on
849 * using non cacheable large pages, then we switch to 4k
851 if (mmu_ci_restrictions
&& psize
== MMU_PAGE_64K
&&
852 (pte_val(*ptep
) & _PAGE_NO_CACHE
)) {
854 demote_segment_4k(mm
, ea
);
856 } else if (ea
< VMALLOC_END
) {
858 * some driver did a non-cacheable mapping
859 * in vmalloc space, so switch vmalloc
862 printk(KERN_ALERT
"Reducing vmalloc segment "
863 "to 4kB pages because of "
864 "non-cacheable mapping\n");
865 psize
= mmu_vmalloc_psize
= MMU_PAGE_4K
;
866 #ifdef CONFIG_SPU_BASE
867 spu_flush_all_slbs(mm
);
872 if (psize
!= get_paca()->context
.user_psize
) {
873 get_paca()->context
= mm
->context
;
874 slb_flush_and_rebolt();
876 } else if (get_paca()->vmalloc_sllp
!=
877 mmu_psize_defs
[mmu_vmalloc_psize
].sllp
) {
878 get_paca()->vmalloc_sllp
=
879 mmu_psize_defs
[mmu_vmalloc_psize
].sllp
;
880 slb_vmalloc_update();
882 #endif /* CONFIG_PPC_64K_PAGES */
884 #ifdef CONFIG_PPC_HAS_HASH_64K
885 if (psize
== MMU_PAGE_64K
)
886 rc
= __hash_page_64K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
);
888 #endif /* CONFIG_PPC_HAS_HASH_64K */
890 int spp
= subpage_protection(pgdir
, ea
);
894 rc
= __hash_page_4K(ea
, access
, vsid
, ptep
, trap
,
898 #ifndef CONFIG_PPC_64K_PAGES
899 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep
));
901 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep
),
902 pte_val(*(ptep
+ PTRS_PER_PTE
)));
904 DBG_LOW(" -> rc=%d\n", rc
);
907 EXPORT_SYMBOL_GPL(hash_page
);
909 void hash_preload(struct mm_struct
*mm
, unsigned long ea
,
910 unsigned long access
, unsigned long trap
)
920 BUG_ON(REGION_ID(ea
) != USER_REGION_ID
);
922 #ifdef CONFIG_PPC_MM_SLICES
923 /* We only prefault standard pages for now */
924 if (unlikely(get_slice_psize(mm
, ea
) != mm
->context
.user_psize
))
928 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
929 " trap=%lx\n", mm
, mm
->pgd
, ea
, access
, trap
);
931 /* Get Linux PTE if available */
935 ptep
= find_linux_pte(pgdir
, ea
);
939 #ifdef CONFIG_PPC_64K_PAGES
940 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
941 * a 64K kernel), then we don't preload, hash_page() will take
942 * care of it once we actually try to access the page.
943 * That way we don't have to duplicate all of the logic for segment
944 * page size demotion here
946 if (pte_val(*ptep
) & (_PAGE_4K_PFN
| _PAGE_NO_CACHE
))
948 #endif /* CONFIG_PPC_64K_PAGES */
951 ssize
= user_segment_size(ea
);
952 vsid
= get_vsid(mm
->context
.id
, ea
, ssize
);
954 /* Hash doesn't like irqs */
955 local_irq_save(flags
);
957 /* Is that local to this CPU ? */
958 mask
= cpumask_of_cpu(smp_processor_id());
959 if (cpus_equal(mm
->cpu_vm_mask
, mask
))
963 #ifdef CONFIG_PPC_HAS_HASH_64K
964 if (mm
->context
.user_psize
== MMU_PAGE_64K
)
965 __hash_page_64K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
);
967 #endif /* CONFIG_PPC_HAS_HASH_64K */
968 __hash_page_4K(ea
, access
, vsid
, ptep
, trap
, local
, ssize
,
969 subpage_protection(pgdir
, ea
));
971 local_irq_restore(flags
);
974 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
975 * do not forget to update the assembly call site !
977 void flush_hash_page(unsigned long va
, real_pte_t pte
, int psize
, int ssize
,
980 unsigned long hash
, index
, shift
, hidx
, slot
;
982 DBG_LOW("flush_hash_page(va=%016x)\n", va
);
983 pte_iterate_hashed_subpages(pte
, psize
, va
, index
, shift
) {
984 hash
= hpt_hash(va
, shift
, ssize
);
985 hidx
= __rpte_to_hidx(pte
, index
);
986 if (hidx
& _PTEIDX_SECONDARY
)
988 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
989 slot
+= hidx
& _PTEIDX_GROUP_IX
;
990 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index
, slot
, hidx
);
991 ppc_md
.hpte_invalidate(slot
, va
, psize
, ssize
, local
);
992 } pte_iterate_hashed_end();
995 void flush_hash_range(unsigned long number
, int local
)
997 if (ppc_md
.flush_hash_range
)
998 ppc_md
.flush_hash_range(number
, local
);
1001 struct ppc64_tlb_batch
*batch
=
1002 &__get_cpu_var(ppc64_tlb_batch
);
1004 for (i
= 0; i
< number
; i
++)
1005 flush_hash_page(batch
->vaddr
[i
], batch
->pte
[i
],
1006 batch
->psize
, batch
->ssize
, local
);
1011 * low_hash_fault is called when we the low level hash code failed
1012 * to instert a PTE due to an hypervisor error
1014 void low_hash_fault(struct pt_regs
*regs
, unsigned long address
, int rc
)
1016 if (user_mode(regs
)) {
1017 #ifdef CONFIG_PPC_SUBPAGE_PROT
1019 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, address
);
1022 _exception(SIGBUS
, regs
, BUS_ADRERR
, address
);
1024 bad_page_fault(regs
, address
, SIGBUS
);
1027 #ifdef CONFIG_DEBUG_PAGEALLOC
1028 static void kernel_map_linear_page(unsigned long vaddr
, unsigned long lmi
)
1030 unsigned long hash
, hpteg
;
1031 unsigned long vsid
= get_kernel_vsid(vaddr
, mmu_kernel_ssize
);
1032 unsigned long va
= hpt_va(vaddr
, vsid
, mmu_kernel_ssize
);
1033 unsigned long mode
= _PAGE_ACCESSED
| _PAGE_DIRTY
|
1034 _PAGE_COHERENT
| PP_RWXX
| HPTE_R_N
;
1037 hash
= hpt_hash(va
, PAGE_SHIFT
, mmu_kernel_ssize
);
1038 hpteg
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
);
1040 ret
= ppc_md
.hpte_insert(hpteg
, va
, __pa(vaddr
),
1041 mode
, HPTE_V_BOLTED
,
1042 mmu_linear_psize
, mmu_kernel_ssize
);
1044 spin_lock(&linear_map_hash_lock
);
1045 BUG_ON(linear_map_hash_slots
[lmi
] & 0x80);
1046 linear_map_hash_slots
[lmi
] = ret
| 0x80;
1047 spin_unlock(&linear_map_hash_lock
);
1050 static void kernel_unmap_linear_page(unsigned long vaddr
, unsigned long lmi
)
1052 unsigned long hash
, hidx
, slot
;
1053 unsigned long vsid
= get_kernel_vsid(vaddr
, mmu_kernel_ssize
);
1054 unsigned long va
= hpt_va(vaddr
, vsid
, mmu_kernel_ssize
);
1056 hash
= hpt_hash(va
, PAGE_SHIFT
, mmu_kernel_ssize
);
1057 spin_lock(&linear_map_hash_lock
);
1058 BUG_ON(!(linear_map_hash_slots
[lmi
] & 0x80));
1059 hidx
= linear_map_hash_slots
[lmi
] & 0x7f;
1060 linear_map_hash_slots
[lmi
] = 0;
1061 spin_unlock(&linear_map_hash_lock
);
1062 if (hidx
& _PTEIDX_SECONDARY
)
1064 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
1065 slot
+= hidx
& _PTEIDX_GROUP_IX
;
1066 ppc_md
.hpte_invalidate(slot
, va
, mmu_linear_psize
, mmu_kernel_ssize
, 0);
1069 void kernel_map_pages(struct page
*page
, int numpages
, int enable
)
1071 unsigned long flags
, vaddr
, lmi
;
1074 local_irq_save(flags
);
1075 for (i
= 0; i
< numpages
; i
++, page
++) {
1076 vaddr
= (unsigned long)page_address(page
);
1077 lmi
= __pa(vaddr
) >> PAGE_SHIFT
;
1078 if (lmi
>= linear_map_hash_count
)
1081 kernel_map_linear_page(vaddr
, lmi
);
1083 kernel_unmap_linear_page(vaddr
, lmi
);
1085 local_irq_restore(flags
);
1087 #endif /* CONFIG_DEBUG_PAGEALLOC */