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/smp_lock.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/sysctl.h>
20 #include <asm/pgalloc.h>
22 #include <asm/tlbflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/machdep.h>
25 #include <asm/cputable.h>
29 #include <linux/sysctl.h>
31 #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
32 #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
34 #ifdef CONFIG_PPC_64K_PAGES
35 #define HUGEPTE_INDEX_SIZE (PMD_SHIFT-HPAGE_SHIFT)
37 #define HUGEPTE_INDEX_SIZE (PUD_SHIFT-HPAGE_SHIFT)
39 #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
40 #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << HUGEPTE_INDEX_SIZE)
42 #define HUGEPD_SHIFT (HPAGE_SHIFT + HUGEPTE_INDEX_SIZE)
43 #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
44 #define HUGEPD_MASK (~(HUGEPD_SIZE-1))
46 #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
48 /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
49 * will choke on pointers to hugepte tables, which is handy for
50 * catching screwups early. */
53 typedef struct { unsigned long pd; } hugepd_t;
55 #define hugepd_none(hpd) ((hpd).pd == 0)
57 static inline pte_t *hugepd_page(hugepd_t hpd)
59 BUG_ON(!(hpd.pd & HUGEPD_OK));
60 return (pte_t *)(hpd.pd & ~HUGEPD_OK);
63 static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr)
65 unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1));
66 pte_t *dir = hugepd_page(*hpdp);
71 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
72 unsigned long address)
74 pte_t *new = kmem_cache_alloc(huge_pgtable_cache,
75 GFP_KERNEL|__GFP_REPEAT);
80 spin_lock(&mm->page_table_lock);
81 if (!hugepd_none(*hpdp))
82 kmem_cache_free(huge_pgtable_cache, new);
84 hpdp->pd = (unsigned long)new | HUGEPD_OK;
85 spin_unlock(&mm->page_table_lock);
89 /* Modelled after find_linux_pte() */
90 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
95 BUG_ON(! in_hugepage_area(mm->context, addr));
99 pg = pgd_offset(mm, addr);
100 if (!pgd_none(*pg)) {
101 pu = pud_offset(pg, addr);
102 if (!pud_none(*pu)) {
103 #ifdef CONFIG_PPC_64K_PAGES
105 pm = pmd_offset(pu, addr);
107 return hugepte_offset((hugepd_t *)pm, addr);
109 return hugepte_offset((hugepd_t *)pu, addr);
117 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
121 hugepd_t *hpdp = NULL;
123 BUG_ON(! in_hugepage_area(mm->context, addr));
127 pg = pgd_offset(mm, addr);
128 pu = pud_alloc(mm, pg, addr);
131 #ifdef CONFIG_PPC_64K_PAGES
133 pm = pmd_alloc(mm, pu, addr);
135 hpdp = (hugepd_t *)pm;
137 hpdp = (hugepd_t *)pu;
144 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr))
147 return hugepte_offset(hpdp, addr);
150 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
155 static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
157 pte_t *hugepte = hugepd_page(*hpdp);
161 pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
165 #ifdef CONFIG_PPC_64K_PAGES
166 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
167 unsigned long addr, unsigned long end,
168 unsigned long floor, unsigned long ceiling)
175 pmd = pmd_offset(pud, addr);
177 next = pmd_addr_end(addr, end);
180 free_hugepte_range(tlb, (hugepd_t *)pmd);
181 } while (pmd++, addr = next, addr != end);
191 if (end - 1 > ceiling - 1)
194 pmd = pmd_offset(pud, start);
196 pmd_free_tlb(tlb, pmd);
200 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
201 unsigned long addr, unsigned long end,
202 unsigned long floor, unsigned long ceiling)
209 pud = pud_offset(pgd, addr);
211 next = pud_addr_end(addr, end);
212 #ifdef CONFIG_PPC_64K_PAGES
213 if (pud_none_or_clear_bad(pud))
215 hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
219 free_hugepte_range(tlb, (hugepd_t *)pud);
221 } while (pud++, addr = next, addr != end);
227 ceiling &= PGDIR_MASK;
231 if (end - 1 > ceiling - 1)
234 pud = pud_offset(pgd, start);
236 pud_free_tlb(tlb, pud);
240 * This function frees user-level page tables of a process.
242 * Must be called with pagetable lock held.
244 void hugetlb_free_pgd_range(struct mmu_gather **tlb,
245 unsigned long addr, unsigned long end,
246 unsigned long floor, unsigned long ceiling)
253 * Comments below take from the normal free_pgd_range(). They
254 * apply here too. The tests against HUGEPD_MASK below are
255 * essential, because we *don't* test for this at the bottom
256 * level. Without them we'll attempt to free a hugepte table
257 * when we unmap just part of it, even if there are other
258 * active mappings using it.
260 * The next few lines have given us lots of grief...
262 * Why are we testing HUGEPD* at this top level? Because
263 * often there will be no work to do at all, and we'd prefer
264 * not to go all the way down to the bottom just to discover
267 * Why all these "- 1"s? Because 0 represents both the bottom
268 * of the address space and the top of it (using -1 for the
269 * top wouldn't help much: the masks would do the wrong thing).
270 * The rule is that addr 0 and floor 0 refer to the bottom of
271 * the address space, but end 0 and ceiling 0 refer to the top
272 * Comparisons need to use "end - 1" and "ceiling - 1" (though
273 * that end 0 case should be mythical).
275 * Wherever addr is brought up or ceiling brought down, we
276 * must be careful to reject "the opposite 0" before it
277 * confuses the subsequent tests. But what about where end is
278 * brought down by HUGEPD_SIZE below? no, end can't go down to
281 * Whereas we round start (addr) and ceiling down, by different
282 * masks at different levels, in order to test whether a table
283 * now has no other vmas using it, so can be freed, we don't
284 * bother to round floor or end up - the tests don't need that.
294 ceiling &= HUGEPD_MASK;
298 if (end - 1 > ceiling - 1)
304 pgd = pgd_offset((*tlb)->mm, addr);
306 BUG_ON(! in_hugepage_area((*tlb)->mm->context, addr));
307 next = pgd_addr_end(addr, end);
308 if (pgd_none_or_clear_bad(pgd))
310 hugetlb_free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
311 } while (pgd++, addr = next, addr != end);
314 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
315 pte_t *ptep, pte_t pte)
317 if (pte_present(*ptep)) {
318 /* We open-code pte_clear because we need to pass the right
319 * argument to hpte_update (huge / !huge)
321 unsigned long old = pte_update(ptep, ~0UL);
322 if (old & _PAGE_HASHPTE)
323 hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
326 *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
329 pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
332 unsigned long old = pte_update(ptep, ~0UL);
334 if (old & _PAGE_HASHPTE)
335 hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
341 struct slb_flush_info {
342 struct mm_struct *mm;
346 static void flush_low_segments(void *parm)
348 struct slb_flush_info *fi = parm;
351 BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_LOW_AREAS);
353 if (current->active_mm != fi->mm)
356 /* Only need to do anything if this CPU is working in the same
357 * mm as the one which has changed */
359 /* update the paca copy of the context struct */
360 get_paca()->context = current->active_mm->context;
362 asm volatile("isync" : : : "memory");
363 for (i = 0; i < NUM_LOW_AREAS; i++) {
364 if (! (fi->newareas & (1U << i)))
366 asm volatile("slbie %0"
367 : : "r" ((i << SID_SHIFT) | SLBIE_C));
369 asm volatile("isync" : : : "memory");
372 static void flush_high_segments(void *parm)
374 struct slb_flush_info *fi = parm;
378 BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_HIGH_AREAS);
380 if (current->active_mm != fi->mm)
383 /* Only need to do anything if this CPU is working in the same
384 * mm as the one which has changed */
386 /* update the paca copy of the context struct */
387 get_paca()->context = current->active_mm->context;
389 asm volatile("isync" : : : "memory");
390 for (i = 0; i < NUM_HIGH_AREAS; i++) {
391 if (! (fi->newareas & (1U << i)))
393 for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++)
394 asm volatile("slbie %0"
395 :: "r" (((i << HTLB_AREA_SHIFT)
396 + (j << SID_SHIFT)) | SLBIE_C));
398 asm volatile("isync" : : : "memory");
401 static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area)
403 unsigned long start = area << SID_SHIFT;
404 unsigned long end = (area+1) << SID_SHIFT;
405 struct vm_area_struct *vma;
407 BUG_ON(area >= NUM_LOW_AREAS);
409 /* Check no VMAs are in the region */
410 vma = find_vma(mm, start);
411 if (vma && (vma->vm_start < end))
417 static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area)
419 unsigned long start = area << HTLB_AREA_SHIFT;
420 unsigned long end = (area+1) << HTLB_AREA_SHIFT;
421 struct vm_area_struct *vma;
423 BUG_ON(area >= NUM_HIGH_AREAS);
425 /* Hack, so that each addresses is controlled by exactly one
426 * of the high or low area bitmaps, the first high area starts
429 start = 0x100000000UL;
431 /* Check no VMAs are in the region */
432 vma = find_vma(mm, start);
433 if (vma && (vma->vm_start < end))
439 static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
442 struct slb_flush_info fi;
444 BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
445 BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
447 newareas &= ~(mm->context.low_htlb_areas);
449 return 0; /* The segments we want are already open */
451 for (i = 0; i < NUM_LOW_AREAS; i++)
452 if ((1 << i) & newareas)
453 if (prepare_low_area_for_htlb(mm, i) != 0)
456 mm->context.low_htlb_areas |= newareas;
458 /* the context change must make it to memory before the flush,
459 * so that further SLB misses do the right thing. */
463 fi.newareas = newareas;
464 on_each_cpu(flush_low_segments, &fi, 0, 1);
469 static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
471 struct slb_flush_info fi;
474 BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
475 BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
478 newareas &= ~(mm->context.high_htlb_areas);
480 return 0; /* The areas we want are already open */
482 for (i = 0; i < NUM_HIGH_AREAS; i++)
483 if ((1 << i) & newareas)
484 if (prepare_high_area_for_htlb(mm, i) != 0)
487 mm->context.high_htlb_areas |= newareas;
489 /* the context change must make it to memory before the flush,
490 * so that further SLB misses do the right thing. */
494 fi.newareas = newareas;
495 on_each_cpu(flush_high_segments, &fi, 0, 1);
500 int prepare_hugepage_range(unsigned long addr, unsigned long len, pgoff_t pgoff)
504 if (pgoff & (~HPAGE_MASK >> PAGE_SHIFT))
506 if (len & ~HPAGE_MASK)
508 if (addr & ~HPAGE_MASK)
511 if (addr < 0x100000000UL)
512 err = open_low_hpage_areas(current->mm,
513 LOW_ESID_MASK(addr, len));
514 if ((addr + len) > 0x100000000UL)
515 err = open_high_hpage_areas(current->mm,
516 HTLB_AREA_MASK(addr, len));
517 #ifdef CONFIG_SPE_BASE
518 spu_flush_all_slbs(current->mm);
521 printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
522 " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
524 LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
532 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
537 if (! in_hugepage_area(mm->context, address))
538 return ERR_PTR(-EINVAL);
540 ptep = huge_pte_offset(mm, address);
541 page = pte_page(*ptep);
543 page += (address % HPAGE_SIZE) / PAGE_SIZE;
548 int pmd_huge(pmd_t pmd)
554 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
555 pmd_t *pmd, int write)
561 /* Because we have an exclusive hugepage region which lies within the
562 * normal user address space, we have to take special measures to make
563 * non-huge mmap()s evade the hugepage reserved regions. */
564 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
565 unsigned long len, unsigned long pgoff,
568 struct mm_struct *mm = current->mm;
569 struct vm_area_struct *vma;
570 unsigned long start_addr;
576 addr = PAGE_ALIGN(addr);
577 vma = find_vma(mm, addr);
578 if (((TASK_SIZE - len) >= addr)
579 && (!vma || (addr+len) <= vma->vm_start)
580 && !is_hugepage_only_range(mm, addr,len))
583 if (len > mm->cached_hole_size) {
584 start_addr = addr = mm->free_area_cache;
586 start_addr = addr = TASK_UNMAPPED_BASE;
587 mm->cached_hole_size = 0;
591 vma = find_vma(mm, addr);
592 while (TASK_SIZE - len >= addr) {
593 BUG_ON(vma && (addr >= vma->vm_end));
595 if (touches_hugepage_low_range(mm, addr, len)) {
596 addr = ALIGN(addr+1, 1<<SID_SHIFT);
597 vma = find_vma(mm, addr);
600 if (touches_hugepage_high_range(mm, addr, len)) {
601 addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
602 vma = find_vma(mm, addr);
605 if (!vma || addr + len <= vma->vm_start) {
607 * Remember the place where we stopped the search:
609 mm->free_area_cache = addr + len;
612 if (addr + mm->cached_hole_size < vma->vm_start)
613 mm->cached_hole_size = vma->vm_start - addr;
618 /* Make sure we didn't miss any holes */
619 if (start_addr != TASK_UNMAPPED_BASE) {
620 start_addr = addr = TASK_UNMAPPED_BASE;
621 mm->cached_hole_size = 0;
628 * This mmap-allocator allocates new areas top-down from below the
629 * stack's low limit (the base):
631 * Because we have an exclusive hugepage region which lies within the
632 * normal user address space, we have to take special measures to make
633 * non-huge mmap()s evade the hugepage reserved regions.
636 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
637 const unsigned long len, const unsigned long pgoff,
638 const unsigned long flags)
640 struct vm_area_struct *vma, *prev_vma;
641 struct mm_struct *mm = current->mm;
642 unsigned long base = mm->mmap_base, addr = addr0;
643 unsigned long largest_hole = mm->cached_hole_size;
646 /* requested length too big for entire address space */
650 /* dont allow allocations above current base */
651 if (mm->free_area_cache > base)
652 mm->free_area_cache = base;
654 /* requesting a specific address */
656 addr = PAGE_ALIGN(addr);
657 vma = find_vma(mm, addr);
658 if (TASK_SIZE - len >= addr &&
659 (!vma || addr + len <= vma->vm_start)
660 && !is_hugepage_only_range(mm, addr,len))
664 if (len <= largest_hole) {
666 mm->free_area_cache = base;
669 /* make sure it can fit in the remaining address space */
670 if (mm->free_area_cache < len)
673 /* either no address requested or cant fit in requested address hole */
674 addr = (mm->free_area_cache - len) & PAGE_MASK;
677 if (touches_hugepage_low_range(mm, addr, len)) {
678 addr = (addr & ((~0) << SID_SHIFT)) - len;
679 goto hugepage_recheck;
680 } else if (touches_hugepage_high_range(mm, addr, len)) {
681 addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
682 goto hugepage_recheck;
686 * Lookup failure means no vma is above this address,
687 * i.e. return with success:
689 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
693 * new region fits between prev_vma->vm_end and
694 * vma->vm_start, use it:
696 if (addr+len <= vma->vm_start &&
697 (!prev_vma || (addr >= prev_vma->vm_end))) {
698 /* remember the address as a hint for next time */
699 mm->cached_hole_size = largest_hole;
700 return (mm->free_area_cache = addr);
702 /* pull free_area_cache down to the first hole */
703 if (mm->free_area_cache == vma->vm_end) {
704 mm->free_area_cache = vma->vm_start;
705 mm->cached_hole_size = largest_hole;
709 /* remember the largest hole we saw so far */
710 if (addr + largest_hole < vma->vm_start)
711 largest_hole = vma->vm_start - addr;
713 /* try just below the current vma->vm_start */
714 addr = vma->vm_start-len;
715 } while (len <= vma->vm_start);
719 * if hint left us with no space for the requested
720 * mapping then try again:
723 mm->free_area_cache = base;
729 * A failed mmap() very likely causes application failure,
730 * so fall back to the bottom-up function here. This scenario
731 * can happen with large stack limits and large mmap()
734 mm->free_area_cache = TASK_UNMAPPED_BASE;
735 mm->cached_hole_size = ~0UL;
736 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
738 * Restore the topdown base:
740 mm->free_area_cache = base;
741 mm->cached_hole_size = ~0UL;
746 static int htlb_check_hinted_area(unsigned long addr, unsigned long len)
748 struct vm_area_struct *vma;
750 vma = find_vma(current->mm, addr);
751 if (TASK_SIZE - len >= addr &&
752 (!vma || ((addr + len) <= vma->vm_start)))
758 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
760 unsigned long addr = 0;
761 struct vm_area_struct *vma;
763 vma = find_vma(current->mm, addr);
764 while (addr + len <= 0x100000000UL) {
765 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
767 if (! __within_hugepage_low_range(addr, len, segmask)) {
768 addr = ALIGN(addr+1, 1<<SID_SHIFT);
769 vma = find_vma(current->mm, addr);
773 if (!vma || (addr + len) <= vma->vm_start)
775 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
776 /* Depending on segmask this might not be a confirmed
777 * hugepage region, so the ALIGN could have skipped
779 vma = find_vma(current->mm, addr);
785 static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
787 unsigned long addr = 0x100000000UL;
788 struct vm_area_struct *vma;
790 vma = find_vma(current->mm, addr);
791 while (addr + len <= TASK_SIZE_USER64) {
792 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
794 if (! __within_hugepage_high_range(addr, len, areamask)) {
795 addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
796 vma = find_vma(current->mm, addr);
800 if (!vma || (addr + len) <= vma->vm_start)
802 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
803 /* Depending on segmask this might not be a confirmed
804 * hugepage region, so the ALIGN could have skipped
806 vma = find_vma(current->mm, addr);
812 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
813 unsigned long len, unsigned long pgoff,
817 u16 areamask, curareas;
819 if (HPAGE_SHIFT == 0)
821 if (len & ~HPAGE_MASK)
826 if (!cpu_has_feature(CPU_FTR_16M_PAGE))
829 /* Paranoia, caller should have dealt with this */
830 BUG_ON((addr + len) < addr);
832 if (test_thread_flag(TIF_32BIT)) {
833 curareas = current->mm->context.low_htlb_areas;
835 /* First see if we can use the hint address */
836 if (addr && (htlb_check_hinted_area(addr, len) == 0)) {
837 areamask = LOW_ESID_MASK(addr, len);
838 if (open_low_hpage_areas(current->mm, areamask) == 0)
842 /* Next see if we can map in the existing low areas */
843 addr = htlb_get_low_area(len, curareas);
847 /* Finally go looking for areas to open */
849 for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
850 ! lastshift; areamask >>=1) {
854 addr = htlb_get_low_area(len, curareas | areamask);
855 if ((addr != -ENOMEM)
856 && open_low_hpage_areas(current->mm, areamask) == 0)
860 curareas = current->mm->context.high_htlb_areas;
862 /* First see if we can use the hint address */
863 /* We discourage 64-bit processes from doing hugepage
864 * mappings below 4GB (must use MAP_FIXED) */
865 if ((addr >= 0x100000000UL)
866 && (htlb_check_hinted_area(addr, len) == 0)) {
867 areamask = HTLB_AREA_MASK(addr, len);
868 if (open_high_hpage_areas(current->mm, areamask) == 0)
872 /* Next see if we can map in the existing high areas */
873 addr = htlb_get_high_area(len, curareas);
877 /* Finally go looking for areas to open */
879 for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
880 ! lastshift; areamask >>=1) {
884 addr = htlb_get_high_area(len, curareas | areamask);
885 if ((addr != -ENOMEM)
886 && open_high_hpage_areas(current->mm, areamask) == 0)
890 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
896 * Called by asm hashtable.S for doing lazy icache flush
898 static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
904 if (!pfn_valid(pte_pfn(pte)))
907 page = pte_page(pte);
910 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
912 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
913 __flush_dcache_icache(page_address(page+i));
914 set_bit(PG_arch_1, &page->flags);
922 int hash_huge_page(struct mm_struct *mm, unsigned long access,
923 unsigned long ea, unsigned long vsid, int local,
927 unsigned long old_pte, new_pte;
928 unsigned long va, rflags, pa;
932 ptep = huge_pte_offset(mm, ea);
934 /* Search the Linux page table for a match with va */
935 va = (vsid << 28) | (ea & 0x0fffffff);
938 * If no pte found or not present, send the problem up to
941 if (unlikely(!ptep || pte_none(*ptep)))
945 * Check the user's access rights to the page. If access should be
946 * prevented then send the problem up to do_page_fault.
948 if (unlikely(access & ~pte_val(*ptep)))
951 * At this point, we have a pte (old_pte) which can be used to build
952 * or update an HPTE. There are 2 cases:
954 * 1. There is a valid (present) pte with no associated HPTE (this is
955 * the most common case)
956 * 2. There is a valid (present) pte with an associated HPTE. The
957 * current values of the pp bits in the HPTE prevent access
958 * because we are doing software DIRTY bit management and the
959 * page is currently not DIRTY.
964 old_pte = pte_val(*ptep);
965 if (old_pte & _PAGE_BUSY)
967 new_pte = old_pte | _PAGE_BUSY |
968 _PAGE_ACCESSED | _PAGE_HASHPTE;
969 } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
972 rflags = 0x2 | (!(new_pte & _PAGE_RW));
973 /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
974 rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
975 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
976 /* No CPU has hugepages but lacks no execute, so we
977 * don't need to worry about that case */
978 rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
981 /* Check if pte already has an hpte (case 2) */
982 if (unlikely(old_pte & _PAGE_HASHPTE)) {
983 /* There MIGHT be an HPTE for this pte */
984 unsigned long hash, slot;
986 hash = hpt_hash(va, HPAGE_SHIFT);
987 if (old_pte & _PAGE_F_SECOND)
989 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
990 slot += (old_pte & _PAGE_F_GIX) >> 12;
992 if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
994 old_pte &= ~_PAGE_HPTEFLAGS;
997 if (likely(!(old_pte & _PAGE_HASHPTE))) {
998 unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
999 unsigned long hpte_group;
1001 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
1004 hpte_group = ((hash & htab_hash_mask) *
1005 HPTES_PER_GROUP) & ~0x7UL;
1007 /* clear HPTE slot informations in new PTE */
1008 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
1010 /* Add in WIMG bits */
1011 /* XXX We should store these in the pte */
1012 /* --BenH: I think they are ... */
1013 rflags |= _PAGE_COHERENT;
1015 /* Insert into the hash table, primary slot */
1016 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
1019 /* Primary is full, try the secondary */
1020 if (unlikely(slot == -1)) {
1021 hpte_group = ((~hash & htab_hash_mask) *
1022 HPTES_PER_GROUP) & ~0x7UL;
1023 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
1028 hpte_group = ((hash & htab_hash_mask) *
1029 HPTES_PER_GROUP)&~0x7UL;
1031 ppc_md.hpte_remove(hpte_group);
1036 if (unlikely(slot == -2))
1037 panic("hash_huge_page: pte_insert failed\n");
1039 new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);
1043 * No need to use ldarx/stdcx here
1045 *ptep = __pte(new_pte & ~_PAGE_BUSY);
1053 static void zero_ctor(void *addr, struct kmem_cache *cache, unsigned long flags)
1055 memset(addr, 0, kmem_cache_size(cache));
1058 static int __init hugetlbpage_init(void)
1060 if (!cpu_has_feature(CPU_FTR_16M_PAGE))
1063 huge_pgtable_cache = kmem_cache_create("hugepte_cache",
1066 SLAB_HWCACHE_ALIGN |
1067 SLAB_MUST_HWCACHE_ALIGN,
1069 if (! huge_pgtable_cache)
1070 panic("hugetlbpage_init(): could not create hugepte cache\n");
1075 module_init(hugetlbpage_init);