powerpc: New hugepage directory format
[linux-2.6-block.git] / arch / powerpc / mm / hugetlbpage.c
CommitLineData
1da177e4 1/*
41151e77 2 * PPC Huge TLB Page Support for Kernel.
1da177e4
LT
3 *
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
41151e77 5 * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
1da177e4
LT
6 *
7 * Based on the IA-32 version:
8 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
9 */
10
1da177e4 11#include <linux/mm.h>
883a3e52 12#include <linux/io.h>
5a0e3ad6 13#include <linux/slab.h>
1da177e4 14#include <linux/hugetlb.h>
342d3db7 15#include <linux/export.h>
41151e77
BB
16#include <linux/of_fdt.h>
17#include <linux/memblock.h>
18#include <linux/bootmem.h>
13020be8 19#include <linux/moduleparam.h>
883a3e52 20#include <asm/pgtable.h>
1da177e4
LT
21#include <asm/pgalloc.h>
22#include <asm/tlb.h>
41151e77 23#include <asm/setup.h>
1da177e4 24
91224346
JT
25#define PAGE_SHIFT_64K 16
26#define PAGE_SHIFT_16M 24
27#define PAGE_SHIFT_16G 34
4ec161cf 28
41151e77 29unsigned int HPAGE_SHIFT;
ec4b2c0c 30
41151e77
BB
31/*
32 * Tracks gpages after the device tree is scanned and before the
a6146888
BB
33 * huge_boot_pages list is ready. On non-Freescale implementations, this is
34 * just used to track 16G pages and so is a single array. FSL-based
35 * implementations may have more than one gpage size, so we need multiple
36 * arrays
41151e77 37 */
881fde1d 38#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
39#define MAX_NUMBER_GPAGES 128
40struct psize_gpages {
41 u64 gpage_list[MAX_NUMBER_GPAGES];
42 unsigned int nr_gpages;
43};
44static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT];
881fde1d
BB
45#else
46#define MAX_NUMBER_GPAGES 1024
47static u64 gpage_freearray[MAX_NUMBER_GPAGES];
48static unsigned nr_gpages;
41151e77 49#endif
f10a04c0 50
a4fe3ce7
DG
51#define hugepd_none(hpd) ((hpd).pd == 0)
52
a4fe3ce7
DG
53pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, unsigned *shift)
54{
55 pgd_t *pg;
56 pud_t *pu;
57 pmd_t *pm;
58 hugepd_t *hpdp = NULL;
59 unsigned pdshift = PGDIR_SHIFT;
60
61 if (shift)
62 *shift = 0;
63
64 pg = pgdir + pgd_index(ea);
65 if (is_hugepd(pg)) {
66 hpdp = (hugepd_t *)pg;
67 } else if (!pgd_none(*pg)) {
68 pdshift = PUD_SHIFT;
69 pu = pud_offset(pg, ea);
70 if (is_hugepd(pu))
71 hpdp = (hugepd_t *)pu;
72 else if (!pud_none(*pu)) {
73 pdshift = PMD_SHIFT;
74 pm = pmd_offset(pu, ea);
75 if (is_hugepd(pm))
76 hpdp = (hugepd_t *)pm;
77 else if (!pmd_none(*pm)) {
41151e77 78 return pte_offset_kernel(pm, ea);
a4fe3ce7
DG
79 }
80 }
81 }
82
83 if (!hpdp)
84 return NULL;
85
86 if (shift)
87 *shift = hugepd_shift(*hpdp);
88 return hugepte_offset(hpdp, ea, pdshift);
89}
342d3db7 90EXPORT_SYMBOL_GPL(find_linux_pte_or_hugepte);
a4fe3ce7
DG
91
92pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
93{
94 return find_linux_pte_or_hugepte(mm->pgd, addr, NULL);
95}
96
f10a04c0 97static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
a4fe3ce7 98 unsigned long address, unsigned pdshift, unsigned pshift)
f10a04c0 99{
41151e77
BB
100 struct kmem_cache *cachep;
101 pte_t *new;
102
881fde1d 103#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
104 int i;
105 int num_hugepd = 1 << (pshift - pdshift);
106 cachep = hugepte_cache;
881fde1d
BB
107#else
108 cachep = PGT_CACHE(pdshift - pshift);
41151e77
BB
109#endif
110
111 new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
f10a04c0 112
a4fe3ce7
DG
113 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
114 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
115
f10a04c0
DG
116 if (! new)
117 return -ENOMEM;
118
119 spin_lock(&mm->page_table_lock);
881fde1d 120#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
121 /*
122 * We have multiple higher-level entries that point to the same
123 * actual pte location. Fill in each as we go and backtrack on error.
124 * We need all of these so the DTLB pgtable walk code can find the
125 * right higher-level entry without knowing if it's a hugepage or not.
126 */
127 for (i = 0; i < num_hugepd; i++, hpdp++) {
128 if (unlikely(!hugepd_none(*hpdp)))
129 break;
130 else
cf9427b8 131 /* We use the old format for PPC_FSL_BOOK3E */
41151e77
BB
132 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
133 }
134 /* If we bailed from the for loop early, an error occurred, clean up */
135 if (i < num_hugepd) {
136 for (i = i - 1 ; i >= 0; i--, hpdp--)
137 hpdp->pd = 0;
138 kmem_cache_free(cachep, new);
139 }
a1cd5419
BB
140#else
141 if (!hugepd_none(*hpdp))
142 kmem_cache_free(cachep, new);
cf9427b8
AK
143 else {
144#ifdef CONFIG_PPC_BOOK3S_64
145 hpdp->pd = (unsigned long)new |
146 (shift_to_mmu_psize(pshift) << 2);
147#else
a1cd5419 148 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
cf9427b8
AK
149#endif
150 }
41151e77 151#endif
f10a04c0
DG
152 spin_unlock(&mm->page_table_lock);
153 return 0;
154}
155
a1cd5419
BB
156/*
157 * These macros define how to determine which level of the page table holds
158 * the hpdp.
159 */
160#ifdef CONFIG_PPC_FSL_BOOK3E
161#define HUGEPD_PGD_SHIFT PGDIR_SHIFT
162#define HUGEPD_PUD_SHIFT PUD_SHIFT
163#else
164#define HUGEPD_PGD_SHIFT PUD_SHIFT
165#define HUGEPD_PUD_SHIFT PMD_SHIFT
166#endif
167
a4fe3ce7 168pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
0b26425c 169{
a4fe3ce7
DG
170 pgd_t *pg;
171 pud_t *pu;
172 pmd_t *pm;
173 hugepd_t *hpdp = NULL;
174 unsigned pshift = __ffs(sz);
175 unsigned pdshift = PGDIR_SHIFT;
176
177 addr &= ~(sz-1);
178
179 pg = pgd_offset(mm, addr);
a1cd5419
BB
180
181 if (pshift >= HUGEPD_PGD_SHIFT) {
a4fe3ce7
DG
182 hpdp = (hugepd_t *)pg;
183 } else {
184 pdshift = PUD_SHIFT;
185 pu = pud_alloc(mm, pg, addr);
a1cd5419 186 if (pshift >= HUGEPD_PUD_SHIFT) {
a4fe3ce7
DG
187 hpdp = (hugepd_t *)pu;
188 } else {
189 pdshift = PMD_SHIFT;
190 pm = pmd_alloc(mm, pu, addr);
191 hpdp = (hugepd_t *)pm;
192 }
193 }
194
195 if (!hpdp)
196 return NULL;
197
198 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
199
200 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
201 return NULL;
202
203 return hugepte_offset(hpdp, addr, pdshift);
4ec161cf 204}
4ec161cf 205
881fde1d 206#ifdef CONFIG_PPC_FSL_BOOK3E
658013e9
JT
207/* Build list of addresses of gigantic pages. This function is used in early
208 * boot before the buddy or bootmem allocator is setup.
209 */
41151e77
BB
210void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
211{
212 unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
213 int i;
214
215 if (addr == 0)
216 return;
217
218 gpage_freearray[idx].nr_gpages = number_of_pages;
219
220 for (i = 0; i < number_of_pages; i++) {
221 gpage_freearray[idx].gpage_list[i] = addr;
222 addr += page_size;
223 }
224}
225
226/*
227 * Moves the gigantic page addresses from the temporary list to the
228 * huge_boot_pages list.
229 */
230int alloc_bootmem_huge_page(struct hstate *hstate)
231{
232 struct huge_bootmem_page *m;
233 int idx = shift_to_mmu_psize(hstate->order + PAGE_SHIFT);
234 int nr_gpages = gpage_freearray[idx].nr_gpages;
235
236 if (nr_gpages == 0)
237 return 0;
238
239#ifdef CONFIG_HIGHMEM
240 /*
241 * If gpages can be in highmem we can't use the trick of storing the
242 * data structure in the page; allocate space for this
243 */
244 m = alloc_bootmem(sizeof(struct huge_bootmem_page));
245 m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
246#else
247 m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
248#endif
249
250 list_add(&m->list, &huge_boot_pages);
251 gpage_freearray[idx].nr_gpages = nr_gpages;
252 gpage_freearray[idx].gpage_list[nr_gpages] = 0;
253 m->hstate = hstate;
254
255 return 1;
256}
257/*
258 * Scan the command line hugepagesz= options for gigantic pages; store those in
259 * a list that we use to allocate the memory once all options are parsed.
260 */
261
262unsigned long gpage_npages[MMU_PAGE_COUNT];
263
89528127
PG
264static int __init do_gpage_early_setup(char *param, char *val,
265 const char *unused)
41151e77
BB
266{
267 static phys_addr_t size;
268 unsigned long npages;
269
270 /*
271 * The hugepagesz and hugepages cmdline options are interleaved. We
272 * use the size variable to keep track of whether or not this was done
273 * properly and skip over instances where it is incorrect. Other
274 * command-line parsing code will issue warnings, so we don't need to.
275 *
276 */
277 if ((strcmp(param, "default_hugepagesz") == 0) ||
278 (strcmp(param, "hugepagesz") == 0)) {
279 size = memparse(val, NULL);
280 } else if (strcmp(param, "hugepages") == 0) {
281 if (size != 0) {
282 if (sscanf(val, "%lu", &npages) <= 0)
283 npages = 0;
284 gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
285 size = 0;
286 }
287 }
288 return 0;
289}
290
291
292/*
293 * This function allocates physical space for pages that are larger than the
294 * buddy allocator can handle. We want to allocate these in highmem because
295 * the amount of lowmem is limited. This means that this function MUST be
296 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
297 * allocate to grab highmem.
298 */
299void __init reserve_hugetlb_gpages(void)
300{
301 static __initdata char cmdline[COMMAND_LINE_SIZE];
302 phys_addr_t size, base;
303 int i;
304
305 strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
026cee00
PM
306 parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0,
307 &do_gpage_early_setup);
41151e77
BB
308
309 /*
310 * Walk gpage list in reverse, allocating larger page sizes first.
311 * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
312 * When we reach the point in the list where pages are no longer
313 * considered gpages, we're done.
314 */
315 for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
316 if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
317 continue;
318 else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
319 break;
320
321 size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
322 base = memblock_alloc_base(size * gpage_npages[i], size,
323 MEMBLOCK_ALLOC_ANYWHERE);
324 add_gpage(base, size, gpage_npages[i]);
325 }
326}
327
881fde1d 328#else /* !PPC_FSL_BOOK3E */
41151e77
BB
329
330/* Build list of addresses of gigantic pages. This function is used in early
331 * boot before the buddy or bootmem allocator is setup.
332 */
333void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
658013e9
JT
334{
335 if (!addr)
336 return;
337 while (number_of_pages > 0) {
338 gpage_freearray[nr_gpages] = addr;
339 nr_gpages++;
340 number_of_pages--;
341 addr += page_size;
342 }
343}
344
ec4b2c0c 345/* Moves the gigantic page addresses from the temporary list to the
0d9ea754
JT
346 * huge_boot_pages list.
347 */
348int alloc_bootmem_huge_page(struct hstate *hstate)
ec4b2c0c
JT
349{
350 struct huge_bootmem_page *m;
351 if (nr_gpages == 0)
352 return 0;
353 m = phys_to_virt(gpage_freearray[--nr_gpages]);
354 gpage_freearray[nr_gpages] = 0;
355 list_add(&m->list, &huge_boot_pages);
0d9ea754 356 m->hstate = hstate;
ec4b2c0c
JT
357 return 1;
358}
41151e77 359#endif
ec4b2c0c 360
39dde65c
CK
361int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
362{
363 return 0;
364}
365
881fde1d 366#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
367#define HUGEPD_FREELIST_SIZE \
368 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
369
370struct hugepd_freelist {
371 struct rcu_head rcu;
372 unsigned int index;
373 void *ptes[0];
374};
375
376static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
377
378static void hugepd_free_rcu_callback(struct rcu_head *head)
379{
380 struct hugepd_freelist *batch =
381 container_of(head, struct hugepd_freelist, rcu);
382 unsigned int i;
383
384 for (i = 0; i < batch->index; i++)
385 kmem_cache_free(hugepte_cache, batch->ptes[i]);
386
387 free_page((unsigned long)batch);
388}
389
390static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
391{
392 struct hugepd_freelist **batchp;
393
394 batchp = &__get_cpu_var(hugepd_freelist_cur);
395
396 if (atomic_read(&tlb->mm->mm_users) < 2 ||
397 cpumask_equal(mm_cpumask(tlb->mm),
398 cpumask_of(smp_processor_id()))) {
399 kmem_cache_free(hugepte_cache, hugepte);
400 return;
401 }
402
403 if (*batchp == NULL) {
404 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
405 (*batchp)->index = 0;
406 }
407
408 (*batchp)->ptes[(*batchp)->index++] = hugepte;
409 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
410 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
411 *batchp = NULL;
412 }
413}
414#endif
415
a4fe3ce7
DG
416static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
417 unsigned long start, unsigned long end,
418 unsigned long floor, unsigned long ceiling)
f10a04c0
DG
419{
420 pte_t *hugepte = hugepd_page(*hpdp);
41151e77
BB
421 int i;
422
a4fe3ce7 423 unsigned long pdmask = ~((1UL << pdshift) - 1);
41151e77
BB
424 unsigned int num_hugepd = 1;
425
881fde1d
BB
426#ifdef CONFIG_PPC_FSL_BOOK3E
427 /* Note: On fsl the hpdp may be the first of several */
41151e77 428 num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
881fde1d
BB
429#else
430 unsigned int shift = hugepd_shift(*hpdp);
41151e77 431#endif
a4fe3ce7
DG
432
433 start &= pdmask;
434 if (start < floor)
435 return;
436 if (ceiling) {
437 ceiling &= pdmask;
438 if (! ceiling)
439 return;
440 }
441 if (end - 1 > ceiling - 1)
442 return;
f10a04c0 443
41151e77
BB
444 for (i = 0; i < num_hugepd; i++, hpdp++)
445 hpdp->pd = 0;
446
f10a04c0 447 tlb->need_flush = 1;
881fde1d
BB
448
449#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77 450 hugepd_free(tlb, hugepte);
881fde1d
BB
451#else
452 pgtable_free_tlb(tlb, hugepte, pdshift - shift);
41151e77 453#endif
f10a04c0
DG
454}
455
f10a04c0
DG
456static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
457 unsigned long addr, unsigned long end,
a4fe3ce7 458 unsigned long floor, unsigned long ceiling)
f10a04c0
DG
459{
460 pmd_t *pmd;
461 unsigned long next;
462 unsigned long start;
463
464 start = addr;
f10a04c0 465 do {
a1cd5419 466 pmd = pmd_offset(pud, addr);
f10a04c0
DG
467 next = pmd_addr_end(addr, end);
468 if (pmd_none(*pmd))
469 continue;
a1cd5419
BB
470#ifdef CONFIG_PPC_FSL_BOOK3E
471 /*
472 * Increment next by the size of the huge mapping since
473 * there may be more than one entry at this level for a
474 * single hugepage, but all of them point to
475 * the same kmem cache that holds the hugepte.
476 */
477 next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
478#endif
a4fe3ce7
DG
479 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
480 addr, next, floor, ceiling);
a1cd5419 481 } while (addr = next, addr != end);
f10a04c0
DG
482
483 start &= PUD_MASK;
484 if (start < floor)
485 return;
486 if (ceiling) {
487 ceiling &= PUD_MASK;
488 if (!ceiling)
489 return;
1da177e4 490 }
f10a04c0
DG
491 if (end - 1 > ceiling - 1)
492 return;
1da177e4 493
f10a04c0
DG
494 pmd = pmd_offset(pud, start);
495 pud_clear(pud);
9e1b32ca 496 pmd_free_tlb(tlb, pmd, start);
f10a04c0 497}
f10a04c0
DG
498
499static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
500 unsigned long addr, unsigned long end,
501 unsigned long floor, unsigned long ceiling)
502{
503 pud_t *pud;
504 unsigned long next;
505 unsigned long start;
506
507 start = addr;
f10a04c0 508 do {
a1cd5419 509 pud = pud_offset(pgd, addr);
f10a04c0 510 next = pud_addr_end(addr, end);
a4fe3ce7 511 if (!is_hugepd(pud)) {
4ec161cf
JT
512 if (pud_none_or_clear_bad(pud))
513 continue;
0d9ea754 514 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
a4fe3ce7 515 ceiling);
4ec161cf 516 } else {
a1cd5419
BB
517#ifdef CONFIG_PPC_FSL_BOOK3E
518 /*
519 * Increment next by the size of the huge mapping since
520 * there may be more than one entry at this level for a
521 * single hugepage, but all of them point to
522 * the same kmem cache that holds the hugepte.
523 */
524 next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
525#endif
a4fe3ce7
DG
526 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
527 addr, next, floor, ceiling);
4ec161cf 528 }
a1cd5419 529 } while (addr = next, addr != end);
f10a04c0
DG
530
531 start &= PGDIR_MASK;
532 if (start < floor)
533 return;
534 if (ceiling) {
535 ceiling &= PGDIR_MASK;
536 if (!ceiling)
537 return;
538 }
539 if (end - 1 > ceiling - 1)
540 return;
541
542 pud = pud_offset(pgd, start);
543 pgd_clear(pgd);
9e1b32ca 544 pud_free_tlb(tlb, pud, start);
f10a04c0
DG
545}
546
547/*
548 * This function frees user-level page tables of a process.
549 *
550 * Must be called with pagetable lock held.
551 */
42b77728 552void hugetlb_free_pgd_range(struct mmu_gather *tlb,
f10a04c0
DG
553 unsigned long addr, unsigned long end,
554 unsigned long floor, unsigned long ceiling)
555{
556 pgd_t *pgd;
557 unsigned long next;
f10a04c0
DG
558
559 /*
a4fe3ce7
DG
560 * Because there are a number of different possible pagetable
561 * layouts for hugepage ranges, we limit knowledge of how
562 * things should be laid out to the allocation path
563 * (huge_pte_alloc(), above). Everything else works out the
564 * structure as it goes from information in the hugepd
565 * pointers. That means that we can't here use the
566 * optimization used in the normal page free_pgd_range(), of
567 * checking whether we're actually covering a large enough
568 * range to have to do anything at the top level of the walk
569 * instead of at the bottom.
f10a04c0 570 *
a4fe3ce7
DG
571 * To make sense of this, you should probably go read the big
572 * block comment at the top of the normal free_pgd_range(),
573 * too.
f10a04c0 574 */
f10a04c0 575
f10a04c0 576 do {
f10a04c0 577 next = pgd_addr_end(addr, end);
41151e77 578 pgd = pgd_offset(tlb->mm, addr);
a4fe3ce7 579 if (!is_hugepd(pgd)) {
0b26425c
DG
580 if (pgd_none_or_clear_bad(pgd))
581 continue;
582 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
583 } else {
881fde1d 584#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
585 /*
586 * Increment next by the size of the huge mapping since
881fde1d
BB
587 * there may be more than one entry at the pgd level
588 * for a single hugepage, but all of them point to the
589 * same kmem cache that holds the hugepte.
41151e77
BB
590 */
591 next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
592#endif
a4fe3ce7
DG
593 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
594 addr, next, floor, ceiling);
0b26425c 595 }
41151e77 596 } while (addr = next, addr != end);
1da177e4
LT
597}
598
1da177e4
LT
599struct page *
600follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
601{
602 pte_t *ptep;
603 struct page *page;
a4fe3ce7
DG
604 unsigned shift;
605 unsigned long mask;
606
607 ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
1da177e4 608
0d9ea754 609 /* Verify it is a huge page else bail. */
a4fe3ce7 610 if (!ptep || !shift)
1da177e4
LT
611 return ERR_PTR(-EINVAL);
612
a4fe3ce7 613 mask = (1UL << shift) - 1;
1da177e4 614 page = pte_page(*ptep);
a4fe3ce7
DG
615 if (page)
616 page += (address & mask) / PAGE_SIZE;
1da177e4
LT
617
618 return page;
619}
620
621int pmd_huge(pmd_t pmd)
622{
623 return 0;
624}
625
ceb86879
AK
626int pud_huge(pud_t pud)
627{
628 return 0;
629}
630
1da177e4
LT
631struct page *
632follow_huge_pmd(struct mm_struct *mm, unsigned long address,
633 pmd_t *pmd, int write)
634{
635 BUG();
636 return NULL;
637}
638
a4fe3ce7
DG
639static noinline int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
640 unsigned long end, int write, struct page **pages, int *nr)
641{
642 unsigned long mask;
643 unsigned long pte_end;
3526741f 644 struct page *head, *page, *tail;
a4fe3ce7
DG
645 pte_t pte;
646 int refs;
647
648 pte_end = (addr + sz) & ~(sz-1);
649 if (pte_end < end)
650 end = pte_end;
651
652 pte = *ptep;
653 mask = _PAGE_PRESENT | _PAGE_USER;
654 if (write)
655 mask |= _PAGE_RW;
656
657 if ((pte_val(pte) & mask) != mask)
658 return 0;
659
660 /* hugepages are never "special" */
661 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
662
663 refs = 0;
664 head = pte_page(pte);
665
666 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
3526741f 667 tail = page;
a4fe3ce7
DG
668 do {
669 VM_BUG_ON(compound_head(page) != head);
670 pages[*nr] = page;
671 (*nr)++;
672 page++;
673 refs++;
674 } while (addr += PAGE_SIZE, addr != end);
675
676 if (!page_cache_add_speculative(head, refs)) {
677 *nr -= refs;
678 return 0;
679 }
680
681 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
682 /* Could be optimized better */
85964684
AA
683 *nr -= refs;
684 while (refs--)
405e44f2 685 put_page(head);
cf592bf7
AA
686 return 0;
687 }
688
689 /*
690 * Any tail page need their mapcount reference taken before we
691 * return.
692 */
693 while (refs--) {
694 if (PageTail(tail))
695 get_huge_page_tail(tail);
696 tail++;
a4fe3ce7
DG
697 }
698
699 return 1;
700}
701
39adfa54
DG
702static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
703 unsigned long sz)
704{
705 unsigned long __boundary = (addr + sz) & ~(sz-1);
706 return (__boundary - 1 < end - 1) ? __boundary : end;
707}
708
a4fe3ce7
DG
709int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
710 unsigned long addr, unsigned long end,
711 int write, struct page **pages, int *nr)
712{
713 pte_t *ptep;
714 unsigned long sz = 1UL << hugepd_shift(*hugepd);
39adfa54 715 unsigned long next;
a4fe3ce7
DG
716
717 ptep = hugepte_offset(hugepd, addr, pdshift);
718 do {
39adfa54 719 next = hugepte_addr_end(addr, end, sz);
a4fe3ce7
DG
720 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
721 return 0;
39adfa54 722 } while (ptep++, addr = next, addr != end);
a4fe3ce7
DG
723
724 return 1;
725}
1da177e4 726
76512959 727#ifdef CONFIG_PPC_MM_SLICES
1da177e4
LT
728unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
729 unsigned long len, unsigned long pgoff,
730 unsigned long flags)
731{
0d9ea754
JT
732 struct hstate *hstate = hstate_file(file);
733 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
48f797de 734
34d07177 735 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
1da177e4 736}
76512959 737#endif
1da177e4 738
3340289d
MG
739unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
740{
25c29f9e 741#ifdef CONFIG_PPC_MM_SLICES
3340289d
MG
742 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
743
744 return 1UL << mmu_psize_to_shift(psize);
41151e77
BB
745#else
746 if (!is_vm_hugetlb_page(vma))
747 return PAGE_SIZE;
748
749 return huge_page_size(hstate_vma(vma));
750#endif
751}
752
753static inline bool is_power_of_4(unsigned long x)
754{
755 if (is_power_of_2(x))
756 return (__ilog2(x) % 2) ? false : true;
757 return false;
3340289d
MG
758}
759
d1837cba 760static int __init add_huge_page_size(unsigned long long size)
4ec161cf 761{
d1837cba
DG
762 int shift = __ffs(size);
763 int mmu_psize;
a4fe3ce7 764
4ec161cf 765 /* Check that it is a page size supported by the hardware and
d1837cba 766 * that it fits within pagetable and slice limits. */
41151e77
BB
767#ifdef CONFIG_PPC_FSL_BOOK3E
768 if ((size < PAGE_SIZE) || !is_power_of_4(size))
769 return -EINVAL;
770#else
d1837cba
DG
771 if (!is_power_of_2(size)
772 || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
773 return -EINVAL;
41151e77 774#endif
91224346 775
d1837cba
DG
776 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
777 return -EINVAL;
778
779#ifdef CONFIG_SPU_FS_64K_LS
780 /* Disable support for 64K huge pages when 64K SPU local store
781 * support is enabled as the current implementation conflicts.
782 */
783 if (shift == PAGE_SHIFT_64K)
784 return -EINVAL;
785#endif /* CONFIG_SPU_FS_64K_LS */
786
787 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
788
789 /* Return if huge page size has already been setup */
790 if (size_to_hstate(size))
791 return 0;
792
793 hugetlb_add_hstate(shift - PAGE_SHIFT);
794
795 return 0;
4ec161cf
JT
796}
797
798static int __init hugepage_setup_sz(char *str)
799{
800 unsigned long long size;
4ec161cf
JT
801
802 size = memparse(str, &str);
803
d1837cba 804 if (add_huge_page_size(size) != 0)
4ec161cf
JT
805 printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
806
807 return 1;
808}
809__setup("hugepagesz=", hugepage_setup_sz);
810
881fde1d 811#ifdef CONFIG_PPC_FSL_BOOK3E
41151e77
BB
812struct kmem_cache *hugepte_cache;
813static int __init hugetlbpage_init(void)
814{
815 int psize;
816
817 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
818 unsigned shift;
819
820 if (!mmu_psize_defs[psize].shift)
821 continue;
822
823 shift = mmu_psize_to_shift(psize);
824
825 /* Don't treat normal page sizes as huge... */
826 if (shift != PAGE_SHIFT)
827 if (add_huge_page_size(1ULL << shift) < 0)
828 continue;
829 }
830
831 /*
832 * Create a kmem cache for hugeptes. The bottom bits in the pte have
833 * size information encoded in them, so align them to allow this
834 */
835 hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
836 HUGEPD_SHIFT_MASK + 1, 0, NULL);
837 if (hugepte_cache == NULL)
838 panic("%s: Unable to create kmem cache for hugeptes\n",
839 __func__);
840
841 /* Default hpage size = 4M */
842 if (mmu_psize_defs[MMU_PAGE_4M].shift)
843 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
844 else
845 panic("%s: Unable to set default huge page size\n", __func__);
846
847
848 return 0;
849}
850#else
f10a04c0
DG
851static int __init hugetlbpage_init(void)
852{
a4fe3ce7 853 int psize;
0d9ea754 854
44ae3ab3 855 if (!mmu_has_feature(MMU_FTR_16M_PAGE))
f10a04c0 856 return -ENODEV;
00df438e 857
d1837cba
DG
858 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
859 unsigned shift;
860 unsigned pdshift;
0d9ea754 861
d1837cba
DG
862 if (!mmu_psize_defs[psize].shift)
863 continue;
00df438e 864
d1837cba
DG
865 shift = mmu_psize_to_shift(psize);
866
867 if (add_huge_page_size(1ULL << shift) < 0)
868 continue;
869
870 if (shift < PMD_SHIFT)
871 pdshift = PMD_SHIFT;
872 else if (shift < PUD_SHIFT)
873 pdshift = PUD_SHIFT;
874 else
875 pdshift = PGDIR_SHIFT;
876
877 pgtable_cache_add(pdshift - shift, NULL);
878 if (!PGT_CACHE(pdshift - shift))
879 panic("hugetlbpage_init(): could not create "
880 "pgtable cache for %d bit pagesize\n", shift);
0d9ea754 881 }
f10a04c0 882
d1837cba
DG
883 /* Set default large page size. Currently, we pick 16M or 1M
884 * depending on what is available
885 */
886 if (mmu_psize_defs[MMU_PAGE_16M].shift)
887 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
888 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
889 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
890
f10a04c0
DG
891 return 0;
892}
41151e77 893#endif
f10a04c0 894module_init(hugetlbpage_init);
0895ecda
DG
895
896void flush_dcache_icache_hugepage(struct page *page)
897{
898 int i;
41151e77 899 void *start;
0895ecda
DG
900
901 BUG_ON(!PageCompound(page));
902
41151e77
BB
903 for (i = 0; i < (1UL << compound_order(page)); i++) {
904 if (!PageHighMem(page)) {
905 __flush_dcache_icache(page_address(page+i));
906 } else {
2480b208 907 start = kmap_atomic(page+i);
41151e77 908 __flush_dcache_icache(start);
2480b208 909 kunmap_atomic(start);
41151e77
BB
910 }
911 }
0895ecda 912}