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>
28 #include <linux/sysctl.h>
30 #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
31 #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
33 #ifdef CONFIG_PPC_64K_PAGES
34 #define HUGEPTE_INDEX_SIZE (PMD_SHIFT-HPAGE_SHIFT)
36 #define HUGEPTE_INDEX_SIZE (PUD_SHIFT-HPAGE_SHIFT)
38 #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
39 #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << HUGEPTE_INDEX_SIZE)
41 #define HUGEPD_SHIFT (HPAGE_SHIFT + HUGEPTE_INDEX_SIZE)
42 #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
43 #define HUGEPD_MASK (~(HUGEPD_SIZE-1))
45 #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
47 /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
48 * will choke on pointers to hugepte tables, which is handy for
49 * catching screwups early. */
52 typedef struct { unsigned long pd
; } hugepd_t
;
54 #define hugepd_none(hpd) ((hpd).pd == 0)
56 static inline pte_t
*hugepd_page(hugepd_t hpd
)
58 BUG_ON(!(hpd
.pd
& HUGEPD_OK
));
59 return (pte_t
*)(hpd
.pd
& ~HUGEPD_OK
);
62 static inline pte_t
*hugepte_offset(hugepd_t
*hpdp
, unsigned long addr
)
64 unsigned long idx
= ((addr
>> HPAGE_SHIFT
) & (PTRS_PER_HUGEPTE
-1));
65 pte_t
*dir
= hugepd_page(*hpdp
);
70 static int __hugepte_alloc(struct mm_struct
*mm
, hugepd_t
*hpdp
,
71 unsigned long address
)
73 pte_t
*new = kmem_cache_alloc(huge_pgtable_cache
,
74 GFP_KERNEL
|__GFP_REPEAT
);
79 spin_lock(&mm
->page_table_lock
);
80 if (!hugepd_none(*hpdp
))
81 kmem_cache_free(huge_pgtable_cache
, new);
83 hpdp
->pd
= (unsigned long)new | HUGEPD_OK
;
84 spin_unlock(&mm
->page_table_lock
);
88 /* Modelled after find_linux_pte() */
89 pte_t
*huge_pte_offset(struct mm_struct
*mm
, unsigned long addr
)
94 BUG_ON(get_slice_psize(mm
, addr
) != mmu_huge_psize
);
98 pg
= pgd_offset(mm
, addr
);
100 pu
= pud_offset(pg
, addr
);
101 if (!pud_none(*pu
)) {
102 #ifdef CONFIG_PPC_64K_PAGES
104 pm
= pmd_offset(pu
, addr
);
106 return hugepte_offset((hugepd_t
*)pm
, addr
);
108 return hugepte_offset((hugepd_t
*)pu
, addr
);
116 pte_t
*huge_pte_alloc(struct mm_struct
*mm
, unsigned long addr
)
120 hugepd_t
*hpdp
= NULL
;
122 BUG_ON(get_slice_psize(mm
, addr
) != mmu_huge_psize
);
126 pg
= pgd_offset(mm
, addr
);
127 pu
= pud_alloc(mm
, pg
, addr
);
130 #ifdef CONFIG_PPC_64K_PAGES
132 pm
= pmd_alloc(mm
, pu
, addr
);
134 hpdp
= (hugepd_t
*)pm
;
136 hpdp
= (hugepd_t
*)pu
;
143 if (hugepd_none(*hpdp
) && __hugepte_alloc(mm
, hpdp
, addr
))
146 return hugepte_offset(hpdp
, addr
);
149 int huge_pmd_unshare(struct mm_struct
*mm
, unsigned long *addr
, pte_t
*ptep
)
154 static void free_hugepte_range(struct mmu_gather
*tlb
, hugepd_t
*hpdp
)
156 pte_t
*hugepte
= hugepd_page(*hpdp
);
160 pgtable_free_tlb(tlb
, pgtable_free_cache(hugepte
, HUGEPTE_CACHE_NUM
,
164 #ifdef CONFIG_PPC_64K_PAGES
165 static void hugetlb_free_pmd_range(struct mmu_gather
*tlb
, pud_t
*pud
,
166 unsigned long addr
, unsigned long end
,
167 unsigned long floor
, unsigned long ceiling
)
174 pmd
= pmd_offset(pud
, addr
);
176 next
= pmd_addr_end(addr
, end
);
179 free_hugepte_range(tlb
, (hugepd_t
*)pmd
);
180 } while (pmd
++, addr
= next
, addr
!= end
);
190 if (end
- 1 > ceiling
- 1)
193 pmd
= pmd_offset(pud
, start
);
195 pmd_free_tlb(tlb
, pmd
);
199 static void hugetlb_free_pud_range(struct mmu_gather
*tlb
, pgd_t
*pgd
,
200 unsigned long addr
, unsigned long end
,
201 unsigned long floor
, unsigned long ceiling
)
208 pud
= pud_offset(pgd
, addr
);
210 next
= pud_addr_end(addr
, end
);
211 #ifdef CONFIG_PPC_64K_PAGES
212 if (pud_none_or_clear_bad(pud
))
214 hugetlb_free_pmd_range(tlb
, pud
, addr
, next
, floor
, ceiling
);
218 free_hugepte_range(tlb
, (hugepd_t
*)pud
);
220 } while (pud
++, addr
= next
, addr
!= end
);
226 ceiling
&= PGDIR_MASK
;
230 if (end
- 1 > ceiling
- 1)
233 pud
= pud_offset(pgd
, start
);
235 pud_free_tlb(tlb
, pud
);
239 * This function frees user-level page tables of a process.
241 * Must be called with pagetable lock held.
243 void hugetlb_free_pgd_range(struct mmu_gather
**tlb
,
244 unsigned long addr
, unsigned long end
,
245 unsigned long floor
, unsigned long ceiling
)
252 * Comments below take from the normal free_pgd_range(). They
253 * apply here too. The tests against HUGEPD_MASK below are
254 * essential, because we *don't* test for this at the bottom
255 * level. Without them we'll attempt to free a hugepte table
256 * when we unmap just part of it, even if there are other
257 * active mappings using it.
259 * The next few lines have given us lots of grief...
261 * Why are we testing HUGEPD* at this top level? Because
262 * often there will be no work to do at all, and we'd prefer
263 * not to go all the way down to the bottom just to discover
266 * Why all these "- 1"s? Because 0 represents both the bottom
267 * of the address space and the top of it (using -1 for the
268 * top wouldn't help much: the masks would do the wrong thing).
269 * The rule is that addr 0 and floor 0 refer to the bottom of
270 * the address space, but end 0 and ceiling 0 refer to the top
271 * Comparisons need to use "end - 1" and "ceiling - 1" (though
272 * that end 0 case should be mythical).
274 * Wherever addr is brought up or ceiling brought down, we
275 * must be careful to reject "the opposite 0" before it
276 * confuses the subsequent tests. But what about where end is
277 * brought down by HUGEPD_SIZE below? no, end can't go down to
280 * Whereas we round start (addr) and ceiling down, by different
281 * masks at different levels, in order to test whether a table
282 * now has no other vmas using it, so can be freed, we don't
283 * bother to round floor or end up - the tests don't need that.
293 ceiling
&= HUGEPD_MASK
;
297 if (end
- 1 > ceiling
- 1)
303 pgd
= pgd_offset((*tlb
)->mm
, addr
);
305 BUG_ON(get_slice_psize((*tlb
)->mm
, addr
) != mmu_huge_psize
);
306 next
= pgd_addr_end(addr
, end
);
307 if (pgd_none_or_clear_bad(pgd
))
309 hugetlb_free_pud_range(*tlb
, pgd
, addr
, next
, floor
, ceiling
);
310 } while (pgd
++, addr
= next
, addr
!= end
);
313 void set_huge_pte_at(struct mm_struct
*mm
, unsigned long addr
,
314 pte_t
*ptep
, pte_t pte
)
316 if (pte_present(*ptep
)) {
317 /* We open-code pte_clear because we need to pass the right
318 * argument to hpte_need_flush (huge / !huge). Might not be
319 * necessary anymore if we make hpte_need_flush() get the
320 * page size from the slices
322 pte_update(mm
, addr
& HPAGE_MASK
, ptep
, ~0UL, 1);
324 *ptep
= __pte(pte_val(pte
) & ~_PAGE_HPTEFLAGS
);
327 pte_t
huge_ptep_get_and_clear(struct mm_struct
*mm
, unsigned long addr
,
330 unsigned long old
= pte_update(mm
, addr
, ptep
, ~0UL, 1);
335 follow_huge_addr(struct mm_struct
*mm
, unsigned long address
, int write
)
340 if (get_slice_psize(mm
, address
) != mmu_huge_psize
)
341 return ERR_PTR(-EINVAL
);
343 ptep
= huge_pte_offset(mm
, address
);
344 page
= pte_page(*ptep
);
346 page
+= (address
% HPAGE_SIZE
) / PAGE_SIZE
;
351 int pmd_huge(pmd_t pmd
)
357 follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
358 pmd_t
*pmd
, int write
)
365 unsigned long hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
366 unsigned long len
, unsigned long pgoff
,
369 return slice_get_unmapped_area(addr
, len
, flags
,
370 mmu_huge_psize
, 1, 0);
374 * Called by asm hashtable.S for doing lazy icache flush
376 static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags
,
382 if (!pfn_valid(pte_pfn(pte
)))
385 page
= pte_page(pte
);
388 if (!test_bit(PG_arch_1
, &page
->flags
) && !PageReserved(page
)) {
390 for (i
= 0; i
< (HPAGE_SIZE
/ PAGE_SIZE
); i
++)
391 __flush_dcache_icache(page_address(page
+i
));
392 set_bit(PG_arch_1
, &page
->flags
);
400 int hash_huge_page(struct mm_struct
*mm
, unsigned long access
,
401 unsigned long ea
, unsigned long vsid
, int local
,
405 unsigned long old_pte
, new_pte
;
406 unsigned long va
, rflags
, pa
;
410 ptep
= huge_pte_offset(mm
, ea
);
412 /* Search the Linux page table for a match with va */
413 va
= (vsid
<< 28) | (ea
& 0x0fffffff);
416 * If no pte found or not present, send the problem up to
419 if (unlikely(!ptep
|| pte_none(*ptep
)))
423 * Check the user's access rights to the page. If access should be
424 * prevented then send the problem up to do_page_fault.
426 if (unlikely(access
& ~pte_val(*ptep
)))
429 * At this point, we have a pte (old_pte) which can be used to build
430 * or update an HPTE. There are 2 cases:
432 * 1. There is a valid (present) pte with no associated HPTE (this is
433 * the most common case)
434 * 2. There is a valid (present) pte with an associated HPTE. The
435 * current values of the pp bits in the HPTE prevent access
436 * because we are doing software DIRTY bit management and the
437 * page is currently not DIRTY.
442 old_pte
= pte_val(*ptep
);
443 if (old_pte
& _PAGE_BUSY
)
445 new_pte
= old_pte
| _PAGE_BUSY
|
446 _PAGE_ACCESSED
| _PAGE_HASHPTE
;
447 } while(old_pte
!= __cmpxchg_u64((unsigned long *)ptep
,
450 rflags
= 0x2 | (!(new_pte
& _PAGE_RW
));
451 /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
452 rflags
|= ((new_pte
& _PAGE_EXEC
) ? 0 : HPTE_R_N
);
453 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE
))
454 /* No CPU has hugepages but lacks no execute, so we
455 * don't need to worry about that case */
456 rflags
= hash_huge_page_do_lazy_icache(rflags
, __pte(old_pte
),
459 /* Check if pte already has an hpte (case 2) */
460 if (unlikely(old_pte
& _PAGE_HASHPTE
)) {
461 /* There MIGHT be an HPTE for this pte */
462 unsigned long hash
, slot
;
464 hash
= hpt_hash(va
, HPAGE_SHIFT
);
465 if (old_pte
& _PAGE_F_SECOND
)
467 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
468 slot
+= (old_pte
& _PAGE_F_GIX
) >> 12;
470 if (ppc_md
.hpte_updatepp(slot
, rflags
, va
, mmu_huge_psize
,
472 old_pte
&= ~_PAGE_HPTEFLAGS
;
475 if (likely(!(old_pte
& _PAGE_HASHPTE
))) {
476 unsigned long hash
= hpt_hash(va
, HPAGE_SHIFT
);
477 unsigned long hpte_group
;
479 pa
= pte_pfn(__pte(old_pte
)) << PAGE_SHIFT
;
482 hpte_group
= ((hash
& htab_hash_mask
) *
483 HPTES_PER_GROUP
) & ~0x7UL
;
485 /* clear HPTE slot informations in new PTE */
486 new_pte
= (new_pte
& ~_PAGE_HPTEFLAGS
) | _PAGE_HASHPTE
;
488 /* Add in WIMG bits */
489 /* XXX We should store these in the pte */
490 /* --BenH: I think they are ... */
491 rflags
|= _PAGE_COHERENT
;
493 /* Insert into the hash table, primary slot */
494 slot
= ppc_md
.hpte_insert(hpte_group
, va
, pa
, rflags
, 0,
497 /* Primary is full, try the secondary */
498 if (unlikely(slot
== -1)) {
499 hpte_group
= ((~hash
& htab_hash_mask
) *
500 HPTES_PER_GROUP
) & ~0x7UL
;
501 slot
= ppc_md
.hpte_insert(hpte_group
, va
, pa
, rflags
,
506 hpte_group
= ((hash
& htab_hash_mask
) *
507 HPTES_PER_GROUP
)&~0x7UL
;
509 ppc_md
.hpte_remove(hpte_group
);
514 if (unlikely(slot
== -2))
515 panic("hash_huge_page: pte_insert failed\n");
517 new_pte
|= (slot
<< 12) & (_PAGE_F_SECOND
| _PAGE_F_GIX
);
521 * No need to use ldarx/stdcx here
523 *ptep
= __pte(new_pte
& ~_PAGE_BUSY
);
531 static void zero_ctor(void *addr
, struct kmem_cache
*cache
, unsigned long flags
)
533 memset(addr
, 0, kmem_cache_size(cache
));
536 static int __init
hugetlbpage_init(void)
538 if (!cpu_has_feature(CPU_FTR_16M_PAGE
))
541 huge_pgtable_cache
= kmem_cache_create("hugepte_cache",
546 if (! huge_pgtable_cache
)
547 panic("hugetlbpage_init(): could not create hugepte cache\n");
552 module_init(hugetlbpage_init
);