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