sched: s/sched_latency/sched_min_granularity
[usb.git] / arch / powerpc / mm / hugetlbpage.c
blob4835f73af3044b7b57d6451cfc7c777a3806f1eb
1 /*
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>
8 */
10 #include <linux/init.h>
11 #include <linux/fs.h>
12 #include <linux/mm.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>
18 #include <asm/mman.h>
19 #include <asm/pgalloc.h>
20 #include <asm/tlb.h>
21 #include <asm/tlbflush.h>
22 #include <asm/mmu_context.h>
23 #include <asm/machdep.h>
24 #include <asm/cputable.h>
25 #include <asm/tlb.h>
26 #include <asm/spu.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)
35 #else
36 #define HUGEPTE_INDEX_SIZE (PUD_SHIFT-HPAGE_SHIFT)
37 #endif
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. */
50 #define HUGEPD_OK 0x1
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);
67 return dir + idx;
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);
76 if (! new)
77 return -ENOMEM;
79 spin_lock(&mm->page_table_lock);
80 if (!hugepd_none(*hpdp))
81 kmem_cache_free(huge_pgtable_cache, new);
82 else
83 hpdp->pd = (unsigned long)new | HUGEPD_OK;
84 spin_unlock(&mm->page_table_lock);
85 return 0;
88 /* Modelled after find_linux_pte() */
89 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
91 pgd_t *pg;
92 pud_t *pu;
94 BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
96 addr &= HPAGE_MASK;
98 pg = pgd_offset(mm, addr);
99 if (!pgd_none(*pg)) {
100 pu = pud_offset(pg, addr);
101 if (!pud_none(*pu)) {
102 #ifdef CONFIG_PPC_64K_PAGES
103 pmd_t *pm;
104 pm = pmd_offset(pu, addr);
105 if (!pmd_none(*pm))
106 return hugepte_offset((hugepd_t *)pm, addr);
107 #else
108 return hugepte_offset((hugepd_t *)pu, addr);
109 #endif
113 return NULL;
116 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
118 pgd_t *pg;
119 pud_t *pu;
120 hugepd_t *hpdp = NULL;
122 BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
124 addr &= HPAGE_MASK;
126 pg = pgd_offset(mm, addr);
127 pu = pud_alloc(mm, pg, addr);
129 if (pu) {
130 #ifdef CONFIG_PPC_64K_PAGES
131 pmd_t *pm;
132 pm = pmd_alloc(mm, pu, addr);
133 if (pm)
134 hpdp = (hugepd_t *)pm;
135 #else
136 hpdp = (hugepd_t *)pu;
137 #endif
140 if (! hpdp)
141 return NULL;
143 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr))
144 return NULL;
146 return hugepte_offset(hpdp, addr);
149 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
151 return 0;
154 static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
156 pte_t *hugepte = hugepd_page(*hpdp);
158 hpdp->pd = 0;
159 tlb->need_flush = 1;
160 pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
161 PGF_CACHENUM_MASK));
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)
169 pmd_t *pmd;
170 unsigned long next;
171 unsigned long start;
173 start = addr;
174 pmd = pmd_offset(pud, addr);
175 do {
176 next = pmd_addr_end(addr, end);
177 if (pmd_none(*pmd))
178 continue;
179 free_hugepte_range(tlb, (hugepd_t *)pmd);
180 } while (pmd++, addr = next, addr != end);
182 start &= PUD_MASK;
183 if (start < floor)
184 return;
185 if (ceiling) {
186 ceiling &= PUD_MASK;
187 if (!ceiling)
188 return;
190 if (end - 1 > ceiling - 1)
191 return;
193 pmd = pmd_offset(pud, start);
194 pud_clear(pud);
195 pmd_free_tlb(tlb, pmd);
197 #endif
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)
203 pud_t *pud;
204 unsigned long next;
205 unsigned long start;
207 start = addr;
208 pud = pud_offset(pgd, addr);
209 do {
210 next = pud_addr_end(addr, end);
211 #ifdef CONFIG_PPC_64K_PAGES
212 if (pud_none_or_clear_bad(pud))
213 continue;
214 hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
215 #else
216 if (pud_none(*pud))
217 continue;
218 free_hugepte_range(tlb, (hugepd_t *)pud);
219 #endif
220 } while (pud++, addr = next, addr != end);
222 start &= PGDIR_MASK;
223 if (start < floor)
224 return;
225 if (ceiling) {
226 ceiling &= PGDIR_MASK;
227 if (!ceiling)
228 return;
230 if (end - 1 > ceiling - 1)
231 return;
233 pud = pud_offset(pgd, start);
234 pgd_clear(pgd);
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)
247 pgd_t *pgd;
248 unsigned long next;
249 unsigned long start;
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
264 * that.
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
278 * 0 there.
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.
286 addr &= HUGEPD_MASK;
287 if (addr < floor) {
288 addr += HUGEPD_SIZE;
289 if (!addr)
290 return;
292 if (ceiling) {
293 ceiling &= HUGEPD_MASK;
294 if (!ceiling)
295 return;
297 if (end - 1 > ceiling - 1)
298 end -= HUGEPD_SIZE;
299 if (addr > end - 1)
300 return;
302 start = addr;
303 pgd = pgd_offset((*tlb)->mm, addr);
304 do {
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))
308 continue;
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,
328 pte_t *ptep)
330 unsigned long old = pte_update(mm, addr, ptep, ~0UL, 1);
331 return __pte(old);
334 struct page *
335 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
337 pte_t *ptep;
338 struct page *page;
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);
345 if (page)
346 page += (address % HPAGE_SIZE) / PAGE_SIZE;
348 return page;
351 int pmd_huge(pmd_t pmd)
353 return 0;
356 struct page *
357 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
358 pmd_t *pmd, int write)
360 BUG();
361 return NULL;
365 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
366 unsigned long len, unsigned long pgoff,
367 unsigned long flags)
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,
377 pte_t pte, int trap)
379 struct page *page;
380 int i;
382 if (!pfn_valid(pte_pfn(pte)))
383 return rflags;
385 page = pte_page(pte);
387 /* page is dirty */
388 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
389 if (trap == 0x400) {
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);
393 } else {
394 rflags |= HPTE_R_N;
397 return rflags;
400 int hash_huge_page(struct mm_struct *mm, unsigned long access,
401 unsigned long ea, unsigned long vsid, int local,
402 unsigned long trap)
404 pte_t *ptep;
405 unsigned long old_pte, new_pte;
406 unsigned long va, rflags, pa;
407 long slot;
408 int err = 1;
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
417 * do_page_fault
419 if (unlikely(!ptep || pte_none(*ptep)))
420 goto out;
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)))
427 goto out;
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.
441 do {
442 old_pte = pte_val(*ptep);
443 if (old_pte & _PAGE_BUSY)
444 goto out;
445 new_pte = old_pte | _PAGE_BUSY |
446 _PAGE_ACCESSED | _PAGE_HASHPTE;
447 } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
448 old_pte, new_pte));
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),
457 trap);
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)
466 hash = ~hash;
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,
471 local) == -1)
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;
481 repeat:
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,
495 mmu_huge_psize);
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,
502 HPTE_V_SECONDARY,
503 mmu_huge_psize);
504 if (slot == -1) {
505 if (mftb() & 0x1)
506 hpte_group = ((hash & htab_hash_mask) *
507 HPTES_PER_GROUP)&~0x7UL;
509 ppc_md.hpte_remove(hpte_group);
510 goto repeat;
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);
525 err = 0;
527 out:
528 return err;
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))
539 return -ENODEV;
541 huge_pgtable_cache = kmem_cache_create("hugepte_cache",
542 HUGEPTE_TABLE_SIZE,
543 HUGEPTE_TABLE_SIZE,
545 zero_ctor);
546 if (! huge_pgtable_cache)
547 panic("hugetlbpage_init(): could not create hugepte cache\n");
549 return 0;
552 module_init(hugetlbpage_init);