2 * PPC64 (POWER4) Huge TLB Page Support for Kernel.
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
6 * Based on the IA-32 version:
7 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
10 #include <linux/init.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/sysctl.h>
19 #include <asm/pgalloc.h>
21 #include <asm/tlbflush.h>
22 #include <asm/mmu_context.h>
23 #include <asm/machdep.h>
24 #include <asm/cputable.h>
27 #define HPAGE_SHIFT_64K 16
28 #define HPAGE_SHIFT_16M 24
30 #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
31 #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
33 unsigned int hugepte_shift
;
34 #define PTRS_PER_HUGEPTE (1 << hugepte_shift)
35 #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << hugepte_shift)
37 #define HUGEPD_SHIFT (HPAGE_SHIFT + hugepte_shift)
38 #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
39 #define HUGEPD_MASK (~(HUGEPD_SIZE-1))
41 #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
43 /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
44 * will choke on pointers to hugepte tables, which is handy for
45 * catching screwups early. */
48 typedef struct { unsigned long pd
; } hugepd_t
;
50 #define hugepd_none(hpd) ((hpd).pd == 0)
52 static inline pte_t
*hugepd_page(hugepd_t hpd
)
54 BUG_ON(!(hpd
.pd
& HUGEPD_OK
));
55 return (pte_t
*)(hpd
.pd
& ~HUGEPD_OK
);
58 static inline pte_t
*hugepte_offset(hugepd_t
*hpdp
, unsigned long addr
)
60 unsigned long idx
= ((addr
>> HPAGE_SHIFT
) & (PTRS_PER_HUGEPTE
-1));
61 pte_t
*dir
= hugepd_page(*hpdp
);
66 static int __hugepte_alloc(struct mm_struct
*mm
, hugepd_t
*hpdp
,
67 unsigned long address
)
69 pte_t
*new = kmem_cache_alloc(huge_pgtable_cache
,
70 GFP_KERNEL
|__GFP_REPEAT
);
75 spin_lock(&mm
->page_table_lock
);
76 if (!hugepd_none(*hpdp
))
77 kmem_cache_free(huge_pgtable_cache
, new);
79 hpdp
->pd
= (unsigned long)new | HUGEPD_OK
;
80 spin_unlock(&mm
->page_table_lock
);
84 /* Base page size affects how we walk hugetlb page tables */
85 #ifdef CONFIG_PPC_64K_PAGES
86 #define hpmd_offset(pud, addr) pmd_offset(pud, addr)
87 #define hpmd_alloc(mm, pud, addr) pmd_alloc(mm, pud, addr)
90 pmd_t
*hpmd_offset(pud_t
*pud
, unsigned long addr
)
92 if (HPAGE_SHIFT
== HPAGE_SHIFT_64K
)
93 return pmd_offset(pud
, addr
);
98 pmd_t
*hpmd_alloc(struct mm_struct
*mm
, pud_t
*pud
, unsigned long addr
)
100 if (HPAGE_SHIFT
== HPAGE_SHIFT_64K
)
101 return pmd_alloc(mm
, pud
, addr
);
103 return (pmd_t
*) pud
;
107 /* Modelled after find_linux_pte() */
108 pte_t
*huge_pte_offset(struct mm_struct
*mm
, unsigned long addr
)
114 BUG_ON(get_slice_psize(mm
, addr
) != mmu_huge_psize
);
118 pg
= pgd_offset(mm
, addr
);
119 if (!pgd_none(*pg
)) {
120 pu
= pud_offset(pg
, addr
);
121 if (!pud_none(*pu
)) {
122 pm
= hpmd_offset(pu
, addr
);
124 return hugepte_offset((hugepd_t
*)pm
, addr
);
131 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
132 unsigned long addr
, unsigned long sz
)
137 hugepd_t
*hpdp
= NULL
;
139 BUG_ON(get_slice_psize(mm
, addr
) != mmu_huge_psize
);
143 pg
= pgd_offset(mm
, addr
);
144 pu
= pud_alloc(mm
, pg
, addr
);
147 pm
= hpmd_alloc(mm
, pu
, addr
);
149 hpdp
= (hugepd_t
*)pm
;
155 if (hugepd_none(*hpdp
) && __hugepte_alloc(mm
, hpdp
, addr
))
158 return hugepte_offset(hpdp
, addr
);
161 int huge_pmd_unshare(struct mm_struct
*mm
, unsigned long *addr
, pte_t
*ptep
)
166 static void free_hugepte_range(struct mmu_gather
*tlb
, hugepd_t
*hpdp
)
168 pte_t
*hugepte
= hugepd_page(*hpdp
);
172 pgtable_free_tlb(tlb
, pgtable_free_cache(hugepte
, HUGEPTE_CACHE_NUM
,
176 static void hugetlb_free_pmd_range(struct mmu_gather
*tlb
, pud_t
*pud
,
177 unsigned long addr
, unsigned long end
,
178 unsigned long floor
, unsigned long ceiling
)
185 pmd
= pmd_offset(pud
, addr
);
187 next
= pmd_addr_end(addr
, end
);
190 free_hugepte_range(tlb
, (hugepd_t
*)pmd
);
191 } while (pmd
++, addr
= next
, addr
!= end
);
201 if (end
- 1 > ceiling
- 1)
204 pmd
= pmd_offset(pud
, start
);
206 pmd_free_tlb(tlb
, pmd
);
209 static void hugetlb_free_pud_range(struct mmu_gather
*tlb
, pgd_t
*pgd
,
210 unsigned long addr
, unsigned long end
,
211 unsigned long floor
, unsigned long ceiling
)
218 pud
= pud_offset(pgd
, addr
);
220 next
= pud_addr_end(addr
, end
);
221 #ifdef CONFIG_PPC_64K_PAGES
222 if (pud_none_or_clear_bad(pud
))
224 hugetlb_free_pmd_range(tlb
, pud
, addr
, next
, floor
, ceiling
);
226 if (HPAGE_SHIFT
== HPAGE_SHIFT_64K
) {
227 if (pud_none_or_clear_bad(pud
))
229 hugetlb_free_pmd_range(tlb
, pud
, addr
, next
, floor
, ceiling
);
233 free_hugepte_range(tlb
, (hugepd_t
*)pud
);
236 } while (pud
++, addr
= next
, addr
!= end
);
242 ceiling
&= PGDIR_MASK
;
246 if (end
- 1 > ceiling
- 1)
249 pud
= pud_offset(pgd
, start
);
251 pud_free_tlb(tlb
, pud
);
255 * This function frees user-level page tables of a process.
257 * Must be called with pagetable lock held.
259 void hugetlb_free_pgd_range(struct mmu_gather
*tlb
,
260 unsigned long addr
, unsigned long end
,
261 unsigned long floor
, unsigned long ceiling
)
268 * Comments below take from the normal free_pgd_range(). They
269 * apply here too. The tests against HUGEPD_MASK below are
270 * essential, because we *don't* test for this at the bottom
271 * level. Without them we'll attempt to free a hugepte table
272 * when we unmap just part of it, even if there are other
273 * active mappings using it.
275 * The next few lines have given us lots of grief...
277 * Why are we testing HUGEPD* at this top level? Because
278 * often there will be no work to do at all, and we'd prefer
279 * not to go all the way down to the bottom just to discover
282 * Why all these "- 1"s? Because 0 represents both the bottom
283 * of the address space and the top of it (using -1 for the
284 * top wouldn't help much: the masks would do the wrong thing).
285 * The rule is that addr 0 and floor 0 refer to the bottom of
286 * the address space, but end 0 and ceiling 0 refer to the top
287 * Comparisons need to use "end - 1" and "ceiling - 1" (though
288 * that end 0 case should be mythical).
290 * Wherever addr is brought up or ceiling brought down, we
291 * must be careful to reject "the opposite 0" before it
292 * confuses the subsequent tests. But what about where end is
293 * brought down by HUGEPD_SIZE below? no, end can't go down to
296 * Whereas we round start (addr) and ceiling down, by different
297 * masks at different levels, in order to test whether a table
298 * now has no other vmas using it, so can be freed, we don't
299 * bother to round floor or end up - the tests don't need that.
309 ceiling
&= HUGEPD_MASK
;
313 if (end
- 1 > ceiling
- 1)
319 pgd
= pgd_offset(tlb
->mm
, addr
);
321 BUG_ON(get_slice_psize(tlb
->mm
, addr
) != mmu_huge_psize
);
322 next
= pgd_addr_end(addr
, end
);
323 if (pgd_none_or_clear_bad(pgd
))
325 hugetlb_free_pud_range(tlb
, pgd
, addr
, next
, floor
, ceiling
);
326 } while (pgd
++, addr
= next
, addr
!= end
);
329 void set_huge_pte_at(struct mm_struct
*mm
, unsigned long addr
,
330 pte_t
*ptep
, pte_t pte
)
332 if (pte_present(*ptep
)) {
333 /* We open-code pte_clear because we need to pass the right
334 * argument to hpte_need_flush (huge / !huge). Might not be
335 * necessary anymore if we make hpte_need_flush() get the
336 * page size from the slices
338 pte_update(mm
, addr
& HPAGE_MASK
, ptep
, ~0UL, 1);
340 *ptep
= __pte(pte_val(pte
) & ~_PAGE_HPTEFLAGS
);
343 pte_t
huge_ptep_get_and_clear(struct mm_struct
*mm
, unsigned long addr
,
346 unsigned long old
= pte_update(mm
, addr
, ptep
, ~0UL, 1);
351 follow_huge_addr(struct mm_struct
*mm
, unsigned long address
, int write
)
356 if (get_slice_psize(mm
, address
) != mmu_huge_psize
)
357 return ERR_PTR(-EINVAL
);
359 ptep
= huge_pte_offset(mm
, address
);
360 page
= pte_page(*ptep
);
362 page
+= (address
% HPAGE_SIZE
) / PAGE_SIZE
;
367 int pmd_huge(pmd_t pmd
)
372 int pud_huge(pud_t pud
)
378 follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
379 pmd_t
*pmd
, int write
)
386 unsigned long hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
387 unsigned long len
, unsigned long pgoff
,
390 return slice_get_unmapped_area(addr
, len
, flags
,
391 mmu_huge_psize
, 1, 0);
395 * Called by asm hashtable.S for doing lazy icache flush
397 static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags
,
403 if (!pfn_valid(pte_pfn(pte
)))
406 page
= pte_page(pte
);
409 if (!test_bit(PG_arch_1
, &page
->flags
) && !PageReserved(page
)) {
411 for (i
= 0; i
< (HPAGE_SIZE
/ PAGE_SIZE
); i
++)
412 __flush_dcache_icache(page_address(page
+i
));
413 set_bit(PG_arch_1
, &page
->flags
);
421 int hash_huge_page(struct mm_struct
*mm
, unsigned long access
,
422 unsigned long ea
, unsigned long vsid
, int local
,
426 unsigned long old_pte
, new_pte
;
427 unsigned long va
, rflags
, pa
;
430 int ssize
= user_segment_size(ea
);
432 ptep
= huge_pte_offset(mm
, ea
);
434 /* Search the Linux page table for a match with va */
435 va
= hpt_va(ea
, vsid
, ssize
);
438 * If no pte found or not present, send the problem up to
441 if (unlikely(!ptep
|| pte_none(*ptep
)))
445 * Check the user's access rights to the page. If access should be
446 * prevented then send the problem up to do_page_fault.
448 if (unlikely(access
& ~pte_val(*ptep
)))
451 * At this point, we have a pte (old_pte) which can be used to build
452 * or update an HPTE. There are 2 cases:
454 * 1. There is a valid (present) pte with no associated HPTE (this is
455 * the most common case)
456 * 2. There is a valid (present) pte with an associated HPTE. The
457 * current values of the pp bits in the HPTE prevent access
458 * because we are doing software DIRTY bit management and the
459 * page is currently not DIRTY.
464 old_pte
= pte_val(*ptep
);
465 if (old_pte
& _PAGE_BUSY
)
467 new_pte
= old_pte
| _PAGE_BUSY
| _PAGE_ACCESSED
;
468 } while(old_pte
!= __cmpxchg_u64((unsigned long *)ptep
,
471 rflags
= 0x2 | (!(new_pte
& _PAGE_RW
));
472 /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
473 rflags
|= ((new_pte
& _PAGE_EXEC
) ? 0 : HPTE_R_N
);
474 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE
))
475 /* No CPU has hugepages but lacks no execute, so we
476 * don't need to worry about that case */
477 rflags
= hash_huge_page_do_lazy_icache(rflags
, __pte(old_pte
),
480 /* Check if pte already has an hpte (case 2) */
481 if (unlikely(old_pte
& _PAGE_HASHPTE
)) {
482 /* There MIGHT be an HPTE for this pte */
483 unsigned long hash
, slot
;
485 hash
= hpt_hash(va
, HPAGE_SHIFT
, ssize
);
486 if (old_pte
& _PAGE_F_SECOND
)
488 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
489 slot
+= (old_pte
& _PAGE_F_GIX
) >> 12;
491 if (ppc_md
.hpte_updatepp(slot
, rflags
, va
, mmu_huge_psize
,
493 old_pte
&= ~_PAGE_HPTEFLAGS
;
496 if (likely(!(old_pte
& _PAGE_HASHPTE
))) {
497 unsigned long hash
= hpt_hash(va
, HPAGE_SHIFT
, ssize
);
498 unsigned long hpte_group
;
500 pa
= pte_pfn(__pte(old_pte
)) << PAGE_SHIFT
;
503 hpte_group
= ((hash
& htab_hash_mask
) *
504 HPTES_PER_GROUP
) & ~0x7UL
;
506 /* clear HPTE slot informations in new PTE */
507 #ifdef CONFIG_PPC_64K_PAGES
508 new_pte
= (new_pte
& ~_PAGE_HPTEFLAGS
) | _PAGE_HPTE_SUB0
;
510 new_pte
= (new_pte
& ~_PAGE_HPTEFLAGS
) | _PAGE_HASHPTE
;
512 /* Add in WIMG bits */
513 rflags
|= (new_pte
& (_PAGE_WRITETHRU
| _PAGE_NO_CACHE
|
514 _PAGE_COHERENT
| _PAGE_GUARDED
));
516 /* Insert into the hash table, primary slot */
517 slot
= ppc_md
.hpte_insert(hpte_group
, va
, pa
, rflags
, 0,
518 mmu_huge_psize
, ssize
);
520 /* Primary is full, try the secondary */
521 if (unlikely(slot
== -1)) {
522 hpte_group
= ((~hash
& htab_hash_mask
) *
523 HPTES_PER_GROUP
) & ~0x7UL
;
524 slot
= ppc_md
.hpte_insert(hpte_group
, va
, pa
, rflags
,
526 mmu_huge_psize
, ssize
);
529 hpte_group
= ((hash
& htab_hash_mask
) *
530 HPTES_PER_GROUP
)&~0x7UL
;
532 ppc_md
.hpte_remove(hpte_group
);
537 if (unlikely(slot
== -2))
538 panic("hash_huge_page: pte_insert failed\n");
540 new_pte
|= (slot
<< 12) & (_PAGE_F_SECOND
| _PAGE_F_GIX
);
544 * No need to use ldarx/stdcx here
546 *ptep
= __pte(new_pte
& ~_PAGE_BUSY
);
554 void set_huge_psize(int psize
)
556 /* Check that it is a page size supported by the hardware and
557 * that it fits within pagetable limits. */
558 if (mmu_psize_defs
[psize
].shift
&& mmu_psize_defs
[psize
].shift
< SID_SHIFT
&&
559 (mmu_psize_defs
[psize
].shift
> MIN_HUGEPTE_SHIFT
||
560 mmu_psize_defs
[psize
].shift
== HPAGE_SHIFT_64K
)) {
561 HPAGE_SHIFT
= mmu_psize_defs
[psize
].shift
;
562 mmu_huge_psize
= psize
;
563 #ifdef CONFIG_PPC_64K_PAGES
564 hugepte_shift
= (PMD_SHIFT
-HPAGE_SHIFT
);
566 if (HPAGE_SHIFT
== HPAGE_SHIFT_64K
)
567 hugepte_shift
= (PMD_SHIFT
-HPAGE_SHIFT
);
569 hugepte_shift
= (PUD_SHIFT
-HPAGE_SHIFT
);
576 static int __init
hugepage_setup_sz(char *str
)
578 unsigned long long size
;
582 size
= memparse(str
, &str
);
586 #ifndef CONFIG_PPC_64K_PAGES
587 case HPAGE_SHIFT_64K
:
588 mmu_psize
= MMU_PAGE_64K
;
591 case HPAGE_SHIFT_16M
:
592 mmu_psize
= MMU_PAGE_16M
;
596 if (mmu_psize
>=0 && mmu_psize_defs
[mmu_psize
].shift
)
597 set_huge_psize(mmu_psize
);
599 printk(KERN_WARNING
"Invalid huge page size specified(%llu)\n", size
);
603 __setup("hugepagesz=", hugepage_setup_sz
);
605 static void zero_ctor(struct kmem_cache
*cache
, void *addr
)
607 memset(addr
, 0, kmem_cache_size(cache
));
610 static int __init
hugetlbpage_init(void)
612 if (!cpu_has_feature(CPU_FTR_16M_PAGE
))
615 huge_pgtable_cache
= kmem_cache_create("hugepte_cache",
620 if (! huge_pgtable_cache
)
621 panic("hugetlbpage_init(): could not create hugepte cache\n");
626 module_init(hugetlbpage_init
);