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 PAGE_SHIFT_64K 16
28 #define PAGE_SHIFT_16M 24
29 #define PAGE_SHIFT_16G 34
31 #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
32 #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
33 #define MAX_NUMBER_GPAGES 1024
35 /* Tracks the 16G pages after the device tree is scanned and before the
36 * huge_boot_pages list is ready. */
37 static unsigned long gpage_freearray[MAX_NUMBER_GPAGES];
38 static unsigned nr_gpages;
40 /* Array of valid huge page sizes - non-zero value(hugepte_shift) is
41 * stored for the huge page sizes that are valid.
43 unsigned int mmu_huge_psizes[MMU_PAGE_COUNT] = { }; /* initialize all to 0 */
45 #define hugepte_shift mmu_huge_psizes
46 #define PTRS_PER_HUGEPTE(psize) (1 << hugepte_shift[psize])
47 #define HUGEPTE_TABLE_SIZE(psize) (sizeof(pte_t) << hugepte_shift[psize])
49 #define HUGEPD_SHIFT(psize) (mmu_psize_to_shift(psize) \
50 + hugepte_shift[psize])
51 #define HUGEPD_SIZE(psize) (1UL << HUGEPD_SHIFT(psize))
52 #define HUGEPD_MASK(psize) (~(HUGEPD_SIZE(psize)-1))
54 /* Subtract one from array size because we don't need a cache for 4K since
55 * is not a huge page size */
56 #define huge_pgtable_cache(psize) (pgtable_cache[HUGEPTE_CACHE_NUM \
58 #define HUGEPTE_CACHE_NAME(psize) (huge_pgtable_cache_name[psize])
60 static const char *huge_pgtable_cache_name[MMU_PAGE_COUNT] = {
61 "unused_4K", "hugepte_cache_64K", "unused_64K_AP",
62 "hugepte_cache_1M", "hugepte_cache_16M", "hugepte_cache_16G"
65 /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
66 * will choke on pointers to hugepte tables, which is handy for
67 * catching screwups early. */
70 typedef struct { unsigned long pd; } hugepd_t;
72 #define hugepd_none(hpd) ((hpd).pd == 0)
74 static inline int shift_to_mmu_psize(unsigned int shift)
77 #ifndef CONFIG_PPC_64K_PAGES
89 static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
91 if (mmu_psize_defs[mmu_psize].shift)
92 return mmu_psize_defs[mmu_psize].shift;
96 static inline pte_t *hugepd_page(hugepd_t hpd)
98 BUG_ON(!(hpd.pd & HUGEPD_OK));
99 return (pte_t *)(hpd.pd & ~HUGEPD_OK);
102 static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr,
103 struct hstate *hstate)
105 unsigned int shift = huge_page_shift(hstate);
106 int psize = shift_to_mmu_psize(shift);
107 unsigned long idx = ((addr >> shift) & (PTRS_PER_HUGEPTE(psize)-1));
108 pte_t *dir = hugepd_page(*hpdp);
113 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
114 unsigned long address, unsigned int psize)
116 pte_t *new = kmem_cache_zalloc(huge_pgtable_cache(psize),
117 GFP_KERNEL|__GFP_REPEAT);
122 spin_lock(&mm->page_table_lock);
123 if (!hugepd_none(*hpdp))
124 kmem_cache_free(huge_pgtable_cache(psize), new);
126 hpdp->pd = (unsigned long)new | HUGEPD_OK;
127 spin_unlock(&mm->page_table_lock);
132 static pud_t *hpud_offset(pgd_t *pgd, unsigned long addr, struct hstate *hstate)
134 if (huge_page_shift(hstate) < PUD_SHIFT)
135 return pud_offset(pgd, addr);
137 return (pud_t *) pgd;
139 static pud_t *hpud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long addr,
140 struct hstate *hstate)
142 if (huge_page_shift(hstate) < PUD_SHIFT)
143 return pud_alloc(mm, pgd, addr);
145 return (pud_t *) pgd;
147 static pmd_t *hpmd_offset(pud_t *pud, unsigned long addr, struct hstate *hstate)
149 if (huge_page_shift(hstate) < PMD_SHIFT)
150 return pmd_offset(pud, addr);
152 return (pmd_t *) pud;
154 static pmd_t *hpmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long addr,
155 struct hstate *hstate)
157 if (huge_page_shift(hstate) < PMD_SHIFT)
158 return pmd_alloc(mm, pud, addr);
160 return (pmd_t *) pud;
163 /* Build list of addresses of gigantic pages. This function is used in early
164 * boot before the buddy or bootmem allocator is setup.
166 void add_gpage(unsigned long addr, unsigned long page_size,
167 unsigned long number_of_pages)
171 while (number_of_pages > 0) {
172 gpage_freearray[nr_gpages] = addr;
179 /* Moves the gigantic page addresses from the temporary list to the
180 * huge_boot_pages list.
182 int alloc_bootmem_huge_page(struct hstate *hstate)
184 struct huge_bootmem_page *m;
187 m = phys_to_virt(gpage_freearray[--nr_gpages]);
188 gpage_freearray[nr_gpages] = 0;
189 list_add(&m->list, &huge_boot_pages);
195 /* Modelled after find_linux_pte() */
196 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
205 struct hstate *hstate;
206 psize = get_slice_psize(mm, addr);
207 shift = mmu_psize_to_shift(psize);
208 sz = ((1UL) << shift);
209 hstate = size_to_hstate(sz);
211 addr &= hstate->mask;
213 pg = pgd_offset(mm, addr);
214 if (!pgd_none(*pg)) {
215 pu = hpud_offset(pg, addr, hstate);
216 if (!pud_none(*pu)) {
217 pm = hpmd_offset(pu, addr, hstate);
219 return hugepte_offset((hugepd_t *)pm, addr,
227 pte_t *huge_pte_alloc(struct mm_struct *mm,
228 unsigned long addr, unsigned long sz)
233 hugepd_t *hpdp = NULL;
234 struct hstate *hstate;
236 hstate = size_to_hstate(sz);
238 psize = get_slice_psize(mm, addr);
239 BUG_ON(!mmu_huge_psizes[psize]);
241 addr &= hstate->mask;
243 pg = pgd_offset(mm, addr);
244 pu = hpud_alloc(mm, pg, addr, hstate);
247 pm = hpmd_alloc(mm, pu, addr, hstate);
249 hpdp = (hugepd_t *)pm;
255 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, psize))
258 return hugepte_offset(hpdp, addr, hstate);
261 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
266 static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp,
269 pte_t *hugepte = hugepd_page(*hpdp);
273 pgtable_free_tlb(tlb, pgtable_free_cache(hugepte,
274 HUGEPTE_CACHE_NUM+psize-1,
278 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
279 unsigned long addr, unsigned long end,
280 unsigned long floor, unsigned long ceiling,
288 pmd = pmd_offset(pud, addr);
290 next = pmd_addr_end(addr, end);
293 free_hugepte_range(tlb, (hugepd_t *)pmd, psize);
294 } while (pmd++, addr = next, addr != end);
304 if (end - 1 > ceiling - 1)
307 pmd = pmd_offset(pud, start);
309 pmd_free_tlb(tlb, pmd);
312 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
313 unsigned long addr, unsigned long end,
314 unsigned long floor, unsigned long ceiling)
320 unsigned int psize = get_slice_psize(tlb->mm, addr);
321 shift = mmu_psize_to_shift(psize);
324 pud = pud_offset(pgd, addr);
326 next = pud_addr_end(addr, end);
327 if (shift < PMD_SHIFT) {
328 if (pud_none_or_clear_bad(pud))
330 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
335 free_hugepte_range(tlb, (hugepd_t *)pud, psize);
337 } while (pud++, addr = next, addr != end);
343 ceiling &= PGDIR_MASK;
347 if (end - 1 > ceiling - 1)
350 pud = pud_offset(pgd, start);
352 pud_free_tlb(tlb, pud);
356 * This function frees user-level page tables of a process.
358 * Must be called with pagetable lock held.
360 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
361 unsigned long addr, unsigned long end,
362 unsigned long floor, unsigned long ceiling)
369 * Comments below take from the normal free_pgd_range(). They
370 * apply here too. The tests against HUGEPD_MASK below are
371 * essential, because we *don't* test for this at the bottom
372 * level. Without them we'll attempt to free a hugepte table
373 * when we unmap just part of it, even if there are other
374 * active mappings using it.
376 * The next few lines have given us lots of grief...
378 * Why are we testing HUGEPD* at this top level? Because
379 * often there will be no work to do at all, and we'd prefer
380 * not to go all the way down to the bottom just to discover
383 * Why all these "- 1"s? Because 0 represents both the bottom
384 * of the address space and the top of it (using -1 for the
385 * top wouldn't help much: the masks would do the wrong thing).
386 * The rule is that addr 0 and floor 0 refer to the bottom of
387 * the address space, but end 0 and ceiling 0 refer to the top
388 * Comparisons need to use "end - 1" and "ceiling - 1" (though
389 * that end 0 case should be mythical).
391 * Wherever addr is brought up or ceiling brought down, we
392 * must be careful to reject "the opposite 0" before it
393 * confuses the subsequent tests. But what about where end is
394 * brought down by HUGEPD_SIZE below? no, end can't go down to
397 * Whereas we round start (addr) and ceiling down, by different
398 * masks at different levels, in order to test whether a table
399 * now has no other vmas using it, so can be freed, we don't
400 * bother to round floor or end up - the tests don't need that.
402 unsigned int psize = get_slice_psize(tlb->mm, addr);
404 addr &= HUGEPD_MASK(psize);
406 addr += HUGEPD_SIZE(psize);
411 ceiling &= HUGEPD_MASK(psize);
415 if (end - 1 > ceiling - 1)
416 end -= HUGEPD_SIZE(psize);
421 pgd = pgd_offset(tlb->mm, addr);
423 psize = get_slice_psize(tlb->mm, addr);
424 BUG_ON(!mmu_huge_psizes[psize]);
425 next = pgd_addr_end(addr, end);
426 if (mmu_psize_to_shift(psize) < PUD_SHIFT) {
427 if (pgd_none_or_clear_bad(pgd))
429 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
433 free_hugepte_range(tlb, (hugepd_t *)pgd, psize);
435 } while (pgd++, addr = next, addr != end);
438 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
439 pte_t *ptep, pte_t pte)
441 if (pte_present(*ptep)) {
442 /* We open-code pte_clear because we need to pass the right
443 * argument to hpte_need_flush (huge / !huge). Might not be
444 * necessary anymore if we make hpte_need_flush() get the
445 * page size from the slices
447 unsigned int psize = get_slice_psize(mm, addr);
448 unsigned int shift = mmu_psize_to_shift(psize);
449 unsigned long sz = ((1UL) << shift);
450 struct hstate *hstate = size_to_hstate(sz);
451 pte_update(mm, addr & hstate->mask, ptep, ~0UL, 1);
453 *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
456 pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
459 unsigned long old = pte_update(mm, addr, ptep, ~0UL, 1);
464 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
468 unsigned int mmu_psize = get_slice_psize(mm, address);
470 /* Verify it is a huge page else bail. */
471 if (!mmu_huge_psizes[mmu_psize])
472 return ERR_PTR(-EINVAL);
474 ptep = huge_pte_offset(mm, address);
475 page = pte_page(*ptep);
477 unsigned int shift = mmu_psize_to_shift(mmu_psize);
478 unsigned long sz = ((1UL) << shift);
479 page += (address % sz) / PAGE_SIZE;
485 int pmd_huge(pmd_t pmd)
490 int pud_huge(pud_t pud)
496 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
497 pmd_t *pmd, int write)
504 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
505 unsigned long len, unsigned long pgoff,
508 struct hstate *hstate = hstate_file(file);
509 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
511 if (!mmu_huge_psizes[mmu_psize])
513 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1, 0);
517 * Called by asm hashtable.S for doing lazy icache flush
519 static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
520 pte_t pte, int trap, unsigned long sz)
525 if (!pfn_valid(pte_pfn(pte)))
528 page = pte_page(pte);
531 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
533 for (i = 0; i < (sz / PAGE_SIZE); i++)
534 __flush_dcache_icache(page_address(page+i));
535 set_bit(PG_arch_1, &page->flags);
543 int hash_huge_page(struct mm_struct *mm, unsigned long access,
544 unsigned long ea, unsigned long vsid, int local,
548 unsigned long old_pte, new_pte;
549 unsigned long va, rflags, pa, sz;
552 int ssize = user_segment_size(ea);
553 unsigned int mmu_psize;
555 mmu_psize = get_slice_psize(mm, ea);
557 if (!mmu_huge_psizes[mmu_psize])
559 ptep = huge_pte_offset(mm, ea);
561 /* Search the Linux page table for a match with va */
562 va = hpt_va(ea, vsid, ssize);
565 * If no pte found or not present, send the problem up to
568 if (unlikely(!ptep || pte_none(*ptep)))
572 * Check the user's access rights to the page. If access should be
573 * prevented then send the problem up to do_page_fault.
575 if (unlikely(access & ~pte_val(*ptep)))
578 * At this point, we have a pte (old_pte) which can be used to build
579 * or update an HPTE. There are 2 cases:
581 * 1. There is a valid (present) pte with no associated HPTE (this is
582 * the most common case)
583 * 2. There is a valid (present) pte with an associated HPTE. The
584 * current values of the pp bits in the HPTE prevent access
585 * because we are doing software DIRTY bit management and the
586 * page is currently not DIRTY.
591 old_pte = pte_val(*ptep);
592 if (old_pte & _PAGE_BUSY)
594 new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
595 } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
598 rflags = 0x2 | (!(new_pte & _PAGE_RW));
599 /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
600 rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
601 shift = mmu_psize_to_shift(mmu_psize);
602 sz = ((1UL) << shift);
603 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
604 /* No CPU has hugepages but lacks no execute, so we
605 * don't need to worry about that case */
606 rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
609 /* Check if pte already has an hpte (case 2) */
610 if (unlikely(old_pte & _PAGE_HASHPTE)) {
611 /* There MIGHT be an HPTE for this pte */
612 unsigned long hash, slot;
614 hash = hpt_hash(va, shift, ssize);
615 if (old_pte & _PAGE_F_SECOND)
617 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
618 slot += (old_pte & _PAGE_F_GIX) >> 12;
620 if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_psize,
622 old_pte &= ~_PAGE_HPTEFLAGS;
625 if (likely(!(old_pte & _PAGE_HASHPTE))) {
626 unsigned long hash = hpt_hash(va, shift, ssize);
627 unsigned long hpte_group;
629 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
632 hpte_group = ((hash & htab_hash_mask) *
633 HPTES_PER_GROUP) & ~0x7UL;
635 /* clear HPTE slot informations in new PTE */
636 #ifdef CONFIG_PPC_64K_PAGES
637 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HPTE_SUB0;
639 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
641 /* Add in WIMG bits */
642 rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
643 _PAGE_COHERENT | _PAGE_GUARDED));
645 /* Insert into the hash table, primary slot */
646 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
649 /* Primary is full, try the secondary */
650 if (unlikely(slot == -1)) {
651 hpte_group = ((~hash & htab_hash_mask) *
652 HPTES_PER_GROUP) & ~0x7UL;
653 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
658 hpte_group = ((hash & htab_hash_mask) *
659 HPTES_PER_GROUP)&~0x7UL;
661 ppc_md.hpte_remove(hpte_group);
666 if (unlikely(slot == -2))
667 panic("hash_huge_page: pte_insert failed\n");
669 new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);
673 * No need to use ldarx/stdcx here
675 *ptep = __pte(new_pte & ~_PAGE_BUSY);
683 static void __init set_huge_psize(int psize)
685 /* Check that it is a page size supported by the hardware and
686 * that it fits within pagetable limits. */
687 if (mmu_psize_defs[psize].shift &&
688 mmu_psize_defs[psize].shift < SID_SHIFT_1T &&
689 (mmu_psize_defs[psize].shift > MIN_HUGEPTE_SHIFT ||
690 mmu_psize_defs[psize].shift == PAGE_SHIFT_64K ||
691 mmu_psize_defs[psize].shift == PAGE_SHIFT_16G)) {
692 /* Return if huge page size has already been setup or is the
693 * same as the base page size. */
694 if (mmu_huge_psizes[psize] ||
695 mmu_psize_defs[psize].shift == PAGE_SHIFT)
697 hugetlb_add_hstate(mmu_psize_defs[psize].shift - PAGE_SHIFT);
699 switch (mmu_psize_defs[psize].shift) {
701 /* We only allow 64k hpages with 4k base page,
702 * which was checked above, and always put them
704 hugepte_shift[psize] = PMD_SHIFT;
707 /* 16M pages can be at two different levels
708 * of pagestables based on base page size */
709 if (PAGE_SHIFT == PAGE_SHIFT_64K)
710 hugepte_shift[psize] = PMD_SHIFT;
711 else /* 4k base page */
712 hugepte_shift[psize] = PUD_SHIFT;
715 /* 16G pages are always at PGD level */
716 hugepte_shift[psize] = PGDIR_SHIFT;
719 hugepte_shift[psize] -= mmu_psize_defs[psize].shift;
721 hugepte_shift[psize] = 0;
724 static int __init hugepage_setup_sz(char *str)
726 unsigned long long size;
730 size = memparse(str, &str);
733 mmu_psize = shift_to_mmu_psize(shift);
734 if (mmu_psize >= 0 && mmu_psize_defs[mmu_psize].shift)
735 set_huge_psize(mmu_psize);
737 printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
741 __setup("hugepagesz=", hugepage_setup_sz);
743 static int __init hugetlbpage_init(void)
747 if (!cpu_has_feature(CPU_FTR_16M_PAGE))
750 /* Add supported huge page sizes. Need to change HUGE_MAX_HSTATE
751 * and adjust PTE_NONCACHE_NUM if the number of supported huge page
754 set_huge_psize(MMU_PAGE_16M);
755 set_huge_psize(MMU_PAGE_16G);
757 /* Temporarily disable support for 64K huge pages when 64K SPU local
758 * store support is enabled as the current implementation conflicts.
760 #ifndef CONFIG_SPU_FS_64K_LS
761 set_huge_psize(MMU_PAGE_64K);
764 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
765 if (mmu_huge_psizes[psize]) {
766 huge_pgtable_cache(psize) = kmem_cache_create(
767 HUGEPTE_CACHE_NAME(psize),
768 HUGEPTE_TABLE_SIZE(psize),
769 HUGEPTE_TABLE_SIZE(psize),
772 if (!huge_pgtable_cache(psize))
773 panic("hugetlbpage_init(): could not create %s"\
774 "\n", HUGEPTE_CACHE_NAME(psize));
781 module_init(hugetlbpage_init);