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