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