nds32: fix build error "relocation truncated to fit: R_NDS32_25_PCREL_RELA" when
[linux-2.6-microblaze.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 <linux/swap.h>
21 #include <linux/swapops.h>
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
24 #include <asm/tlb.h>
25 #include <asm/setup.h>
26 #include <asm/hugetlb.h>
27 #include <asm/pte-walk.h>
28
29
30 #ifdef CONFIG_HUGETLB_PAGE
31
32 #define PAGE_SHIFT_64K  16
33 #define PAGE_SHIFT_512K 19
34 #define PAGE_SHIFT_8M   23
35 #define PAGE_SHIFT_16M  24
36 #define PAGE_SHIFT_16G  34
37
38 bool hugetlb_disabled = false;
39
40 unsigned int HPAGE_SHIFT;
41 EXPORT_SYMBOL(HPAGE_SHIFT);
42
43 #define hugepd_none(hpd)        (hpd_val(hpd) == 0)
44
45 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
46 {
47         /*
48          * Only called for hugetlbfs pages, hence can ignore THP and the
49          * irq disabled walk.
50          */
51         return __find_linux_pte(mm->pgd, addr, NULL, NULL);
52 }
53
54 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
55                            unsigned long address, unsigned int pdshift,
56                            unsigned int pshift, spinlock_t *ptl)
57 {
58         struct kmem_cache *cachep;
59         pte_t *new;
60         int i;
61         int num_hugepd;
62
63         if (pshift >= pdshift) {
64                 cachep = hugepte_cache;
65                 num_hugepd = 1 << (pshift - pdshift);
66         } else {
67                 cachep = PGT_CACHE(pdshift - pshift);
68                 num_hugepd = 1;
69         }
70
71         new = kmem_cache_zalloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
72
73         BUG_ON(pshift > HUGEPD_SHIFT_MASK);
74         BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
75
76         if (! new)
77                 return -ENOMEM;
78
79         /*
80          * Make sure other cpus find the hugepd set only after a
81          * properly initialized page table is visible to them.
82          * For more details look for comment in __pte_alloc().
83          */
84         smp_wmb();
85
86         spin_lock(ptl);
87         /*
88          * We have multiple higher-level entries that point to the same
89          * actual pte location.  Fill in each as we go and backtrack on error.
90          * We need all of these so the DTLB pgtable walk code can find the
91          * right higher-level entry without knowing if it's a hugepage or not.
92          */
93         for (i = 0; i < num_hugepd; i++, hpdp++) {
94                 if (unlikely(!hugepd_none(*hpdp)))
95                         break;
96                 else {
97 #ifdef CONFIG_PPC_BOOK3S_64
98                         *hpdp = __hugepd(__pa(new) |
99                                          (shift_to_mmu_psize(pshift) << 2));
100 #elif defined(CONFIG_PPC_8xx)
101                         *hpdp = __hugepd(__pa(new) | _PMD_USER |
102                                          (pshift == PAGE_SHIFT_8M ? _PMD_PAGE_8M :
103                                           _PMD_PAGE_512K) | _PMD_PRESENT);
104 #else
105                         /* We use the old format for PPC_FSL_BOOK3E */
106                         *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift);
107 #endif
108                 }
109         }
110         /* If we bailed from the for loop early, an error occurred, clean up */
111         if (i < num_hugepd) {
112                 for (i = i - 1 ; i >= 0; i--, hpdp--)
113                         *hpdp = __hugepd(0);
114                 kmem_cache_free(cachep, new);
115         }
116         spin_unlock(ptl);
117         return 0;
118 }
119
120 /*
121  * These macros define how to determine which level of the page table holds
122  * the hpdp.
123  */
124 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
125 #define HUGEPD_PGD_SHIFT PGDIR_SHIFT
126 #define HUGEPD_PUD_SHIFT PUD_SHIFT
127 #endif
128
129 /*
130  * At this point we do the placement change only for BOOK3S 64. This would
131  * possibly work on other subarchs.
132  */
133 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
134 {
135         pgd_t *pg;
136         pud_t *pu;
137         pmd_t *pm;
138         hugepd_t *hpdp = NULL;
139         unsigned pshift = __ffs(sz);
140         unsigned pdshift = PGDIR_SHIFT;
141         spinlock_t *ptl;
142
143         addr &= ~(sz-1);
144         pg = pgd_offset(mm, addr);
145
146 #ifdef CONFIG_PPC_BOOK3S_64
147         if (pshift == PGDIR_SHIFT)
148                 /* 16GB huge page */
149                 return (pte_t *) pg;
150         else if (pshift > PUD_SHIFT) {
151                 /*
152                  * We need to use hugepd table
153                  */
154                 ptl = &mm->page_table_lock;
155                 hpdp = (hugepd_t *)pg;
156         } else {
157                 pdshift = PUD_SHIFT;
158                 pu = pud_alloc(mm, pg, addr);
159                 if (pshift == PUD_SHIFT)
160                         return (pte_t *)pu;
161                 else if (pshift > PMD_SHIFT) {
162                         ptl = pud_lockptr(mm, pu);
163                         hpdp = (hugepd_t *)pu;
164                 } else {
165                         pdshift = PMD_SHIFT;
166                         pm = pmd_alloc(mm, pu, addr);
167                         if (pshift == PMD_SHIFT)
168                                 /* 16MB hugepage */
169                                 return (pte_t *)pm;
170                         else {
171                                 ptl = pmd_lockptr(mm, pm);
172                                 hpdp = (hugepd_t *)pm;
173                         }
174                 }
175         }
176 #else
177         if (pshift >= HUGEPD_PGD_SHIFT) {
178                 ptl = &mm->page_table_lock;
179                 hpdp = (hugepd_t *)pg;
180         } else {
181                 pdshift = PUD_SHIFT;
182                 pu = pud_alloc(mm, pg, addr);
183                 if (pshift >= HUGEPD_PUD_SHIFT) {
184                         ptl = pud_lockptr(mm, pu);
185                         hpdp = (hugepd_t *)pu;
186                 } else {
187                         pdshift = PMD_SHIFT;
188                         pm = pmd_alloc(mm, pu, addr);
189                         ptl = pmd_lockptr(mm, pm);
190                         hpdp = (hugepd_t *)pm;
191                 }
192         }
193 #endif
194         if (!hpdp)
195                 return NULL;
196
197         BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
198
199         if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr,
200                                                   pdshift, pshift, ptl))
201                 return NULL;
202
203         return hugepte_offset(*hpdp, addr, pdshift);
204 }
205
206 #ifdef CONFIG_PPC_BOOK3S_64
207 /*
208  * Tracks gpages after the device tree is scanned and before the
209  * huge_boot_pages list is ready on pseries.
210  */
211 #define MAX_NUMBER_GPAGES       1024
212 __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
213 __initdata static unsigned nr_gpages;
214
215 /*
216  * Build list of addresses of gigantic pages.  This function is used in early
217  * boot before the buddy allocator is setup.
218  */
219 void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
220 {
221         if (!addr)
222                 return;
223         while (number_of_pages > 0) {
224                 gpage_freearray[nr_gpages] = addr;
225                 nr_gpages++;
226                 number_of_pages--;
227                 addr += page_size;
228         }
229 }
230
231 int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
232 {
233         struct huge_bootmem_page *m;
234         if (nr_gpages == 0)
235                 return 0;
236         m = phys_to_virt(gpage_freearray[--nr_gpages]);
237         gpage_freearray[nr_gpages] = 0;
238         list_add(&m->list, &huge_boot_pages);
239         m->hstate = hstate;
240         return 1;
241 }
242 #endif
243
244
245 int __init alloc_bootmem_huge_page(struct hstate *h)
246 {
247
248 #ifdef CONFIG_PPC_BOOK3S_64
249         if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
250                 return pseries_alloc_bootmem_huge_page(h);
251 #endif
252         return __alloc_bootmem_huge_page(h);
253 }
254
255 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
256 #define HUGEPD_FREELIST_SIZE \
257         ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
258
259 struct hugepd_freelist {
260         struct rcu_head rcu;
261         unsigned int index;
262         void *ptes[0];
263 };
264
265 static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
266
267 static void hugepd_free_rcu_callback(struct rcu_head *head)
268 {
269         struct hugepd_freelist *batch =
270                 container_of(head, struct hugepd_freelist, rcu);
271         unsigned int i;
272
273         for (i = 0; i < batch->index; i++)
274                 kmem_cache_free(hugepte_cache, batch->ptes[i]);
275
276         free_page((unsigned long)batch);
277 }
278
279 static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
280 {
281         struct hugepd_freelist **batchp;
282
283         batchp = &get_cpu_var(hugepd_freelist_cur);
284
285         if (atomic_read(&tlb->mm->mm_users) < 2 ||
286             mm_is_thread_local(tlb->mm)) {
287                 kmem_cache_free(hugepte_cache, hugepte);
288                 put_cpu_var(hugepd_freelist_cur);
289                 return;
290         }
291
292         if (*batchp == NULL) {
293                 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
294                 (*batchp)->index = 0;
295         }
296
297         (*batchp)->ptes[(*batchp)->index++] = hugepte;
298         if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
299                 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
300                 *batchp = NULL;
301         }
302         put_cpu_var(hugepd_freelist_cur);
303 }
304 #else
305 static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
306 #endif
307
308 static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
309                               unsigned long start, unsigned long end,
310                               unsigned long floor, unsigned long ceiling)
311 {
312         pte_t *hugepte = hugepd_page(*hpdp);
313         int i;
314
315         unsigned long pdmask = ~((1UL << pdshift) - 1);
316         unsigned int num_hugepd = 1;
317         unsigned int shift = hugepd_shift(*hpdp);
318
319         /* Note: On fsl the hpdp may be the first of several */
320         if (shift > pdshift)
321                 num_hugepd = 1 << (shift - pdshift);
322
323         start &= pdmask;
324         if (start < floor)
325                 return;
326         if (ceiling) {
327                 ceiling &= pdmask;
328                 if (! ceiling)
329                         return;
330         }
331         if (end - 1 > ceiling - 1)
332                 return;
333
334         for (i = 0; i < num_hugepd; i++, hpdp++)
335                 *hpdp = __hugepd(0);
336
337         if (shift >= pdshift)
338                 hugepd_free(tlb, hugepte);
339         else
340                 pgtable_free_tlb(tlb, hugepte, pdshift - shift);
341 }
342
343 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
344                                    unsigned long addr, unsigned long end,
345                                    unsigned long floor, unsigned long ceiling)
346 {
347         pmd_t *pmd;
348         unsigned long next;
349         unsigned long start;
350
351         start = addr;
352         do {
353                 unsigned long more;
354
355                 pmd = pmd_offset(pud, addr);
356                 next = pmd_addr_end(addr, end);
357                 if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
358                         /*
359                          * if it is not hugepd pointer, we should already find
360                          * it cleared.
361                          */
362                         WARN_ON(!pmd_none_or_clear_bad(pmd));
363                         continue;
364                 }
365                 /*
366                  * Increment next by the size of the huge mapping since
367                  * there may be more than one entry at this level for a
368                  * single hugepage, but all of them point to
369                  * the same kmem cache that holds the hugepte.
370                  */
371                 more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
372                 if (more > next)
373                         next = more;
374
375                 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
376                                   addr, next, floor, ceiling);
377         } while (addr = next, addr != end);
378
379         start &= PUD_MASK;
380         if (start < floor)
381                 return;
382         if (ceiling) {
383                 ceiling &= PUD_MASK;
384                 if (!ceiling)
385                         return;
386         }
387         if (end - 1 > ceiling - 1)
388                 return;
389
390         pmd = pmd_offset(pud, start);
391         pud_clear(pud);
392         pmd_free_tlb(tlb, pmd, start);
393         mm_dec_nr_pmds(tlb->mm);
394 }
395
396 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
397                                    unsigned long addr, unsigned long end,
398                                    unsigned long floor, unsigned long ceiling)
399 {
400         pud_t *pud;
401         unsigned long next;
402         unsigned long start;
403
404         start = addr;
405         do {
406                 pud = pud_offset(pgd, addr);
407                 next = pud_addr_end(addr, end);
408                 if (!is_hugepd(__hugepd(pud_val(*pud)))) {
409                         if (pud_none_or_clear_bad(pud))
410                                 continue;
411                         hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
412                                                ceiling);
413                 } else {
414                         unsigned long more;
415                         /*
416                          * Increment next by the size of the huge mapping since
417                          * there may be more than one entry at this level for a
418                          * single hugepage, but all of them point to
419                          * the same kmem cache that holds the hugepte.
420                          */
421                         more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
422                         if (more > next)
423                                 next = more;
424
425                         free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
426                                           addr, next, floor, ceiling);
427                 }
428         } while (addr = next, addr != end);
429
430         start &= PGDIR_MASK;
431         if (start < floor)
432                 return;
433         if (ceiling) {
434                 ceiling &= PGDIR_MASK;
435                 if (!ceiling)
436                         return;
437         }
438         if (end - 1 > ceiling - 1)
439                 return;
440
441         pud = pud_offset(pgd, start);
442         pgd_clear(pgd);
443         pud_free_tlb(tlb, pud, start);
444         mm_dec_nr_puds(tlb->mm);
445 }
446
447 /*
448  * This function frees user-level page tables of a process.
449  */
450 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
451                             unsigned long addr, unsigned long end,
452                             unsigned long floor, unsigned long ceiling)
453 {
454         pgd_t *pgd;
455         unsigned long next;
456
457         /*
458          * Because there are a number of different possible pagetable
459          * layouts for hugepage ranges, we limit knowledge of how
460          * things should be laid out to the allocation path
461          * (huge_pte_alloc(), above).  Everything else works out the
462          * structure as it goes from information in the hugepd
463          * pointers.  That means that we can't here use the
464          * optimization used in the normal page free_pgd_range(), of
465          * checking whether we're actually covering a large enough
466          * range to have to do anything at the top level of the walk
467          * instead of at the bottom.
468          *
469          * To make sense of this, you should probably go read the big
470          * block comment at the top of the normal free_pgd_range(),
471          * too.
472          */
473
474         do {
475                 next = pgd_addr_end(addr, end);
476                 pgd = pgd_offset(tlb->mm, addr);
477                 if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
478                         if (pgd_none_or_clear_bad(pgd))
479                                 continue;
480                         hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
481                 } else {
482                         unsigned long more;
483                         /*
484                          * Increment next by the size of the huge mapping since
485                          * there may be more than one entry at the pgd level
486                          * for a single hugepage, but all of them point to the
487                          * same kmem cache that holds the hugepte.
488                          */
489                         more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
490                         if (more > next)
491                                 next = more;
492
493                         free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
494                                           addr, next, floor, ceiling);
495                 }
496         } while (addr = next, addr != end);
497 }
498
499 struct page *follow_huge_pd(struct vm_area_struct *vma,
500                             unsigned long address, hugepd_t hpd,
501                             int flags, int pdshift)
502 {
503         pte_t *ptep;
504         spinlock_t *ptl;
505         struct page *page = NULL;
506         unsigned long mask;
507         int shift = hugepd_shift(hpd);
508         struct mm_struct *mm = vma->vm_mm;
509
510 retry:
511         /*
512          * hugepage directory entries are protected by mm->page_table_lock
513          * Use this instead of huge_pte_lockptr
514          */
515         ptl = &mm->page_table_lock;
516         spin_lock(ptl);
517
518         ptep = hugepte_offset(hpd, address, pdshift);
519         if (pte_present(*ptep)) {
520                 mask = (1UL << shift) - 1;
521                 page = pte_page(*ptep);
522                 page += ((address & mask) >> PAGE_SHIFT);
523                 if (flags & FOLL_GET)
524                         get_page(page);
525         } else {
526                 if (is_hugetlb_entry_migration(*ptep)) {
527                         spin_unlock(ptl);
528                         __migration_entry_wait(mm, ptep, ptl);
529                         goto retry;
530                 }
531         }
532         spin_unlock(ptl);
533         return page;
534 }
535
536 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
537                                       unsigned long sz)
538 {
539         unsigned long __boundary = (addr + sz) & ~(sz-1);
540         return (__boundary - 1 < end - 1) ? __boundary : end;
541 }
542
543 int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
544                 unsigned long end, int write, struct page **pages, int *nr)
545 {
546         pte_t *ptep;
547         unsigned long sz = 1UL << hugepd_shift(hugepd);
548         unsigned long next;
549
550         ptep = hugepte_offset(hugepd, addr, pdshift);
551         do {
552                 next = hugepte_addr_end(addr, end, sz);
553                 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
554                         return 0;
555         } while (ptep++, addr = next, addr != end);
556
557         return 1;
558 }
559
560 #ifdef CONFIG_PPC_MM_SLICES
561 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
562                                         unsigned long len, unsigned long pgoff,
563                                         unsigned long flags)
564 {
565         struct hstate *hstate = hstate_file(file);
566         int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
567
568 #ifdef CONFIG_PPC_RADIX_MMU
569         if (radix_enabled())
570                 return radix__hugetlb_get_unmapped_area(file, addr, len,
571                                                        pgoff, flags);
572 #endif
573         return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
574 }
575 #endif
576
577 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
578 {
579 #ifdef CONFIG_PPC_MM_SLICES
580         /* With radix we don't use slice, so derive it from vma*/
581         if (!radix_enabled()) {
582                 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
583
584                 return 1UL << mmu_psize_to_shift(psize);
585         }
586 #endif
587         return vma_kernel_pagesize(vma);
588 }
589
590 static inline bool is_power_of_4(unsigned long x)
591 {
592         if (is_power_of_2(x))
593                 return (__ilog2(x) % 2) ? false : true;
594         return false;
595 }
596
597 static int __init add_huge_page_size(unsigned long long size)
598 {
599         int shift = __ffs(size);
600         int mmu_psize;
601
602         /* Check that it is a page size supported by the hardware and
603          * that it fits within pagetable and slice limits. */
604         if (size <= PAGE_SIZE)
605                 return -EINVAL;
606 #if defined(CONFIG_PPC_FSL_BOOK3E)
607         if (!is_power_of_4(size))
608                 return -EINVAL;
609 #elif !defined(CONFIG_PPC_8xx)
610         if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
611                 return -EINVAL;
612 #endif
613
614         if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
615                 return -EINVAL;
616
617 #ifdef CONFIG_PPC_BOOK3S_64
618         /*
619          * We need to make sure that for different page sizes reported by
620          * firmware we only add hugetlb support for page sizes that can be
621          * supported by linux page table layout.
622          * For now we have
623          * Radix: 2M
624          * Hash: 16M and 16G
625          */
626         if (radix_enabled()) {
627                 if (mmu_psize != MMU_PAGE_2M) {
628                         if (cpu_has_feature(CPU_FTR_POWER9_DD1) ||
629                             (mmu_psize != MMU_PAGE_1G))
630                                 return -EINVAL;
631                 }
632         } else {
633                 if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
634                         return -EINVAL;
635         }
636 #endif
637
638         BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
639
640         /* Return if huge page size has already been setup */
641         if (size_to_hstate(size))
642                 return 0;
643
644         hugetlb_add_hstate(shift - PAGE_SHIFT);
645
646         return 0;
647 }
648
649 static int __init hugepage_setup_sz(char *str)
650 {
651         unsigned long long size;
652
653         size = memparse(str, &str);
654
655         if (add_huge_page_size(size) != 0) {
656                 hugetlb_bad_size();
657                 pr_err("Invalid huge page size specified(%llu)\n", size);
658         }
659
660         return 1;
661 }
662 __setup("hugepagesz=", hugepage_setup_sz);
663
664 struct kmem_cache *hugepte_cache;
665 static int __init hugetlbpage_init(void)
666 {
667         int psize;
668
669         if (hugetlb_disabled) {
670                 pr_info("HugeTLB support is disabled!\n");
671                 return 0;
672         }
673
674 #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
675         if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
676                 return -ENODEV;
677 #endif
678         for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
679                 unsigned shift;
680                 unsigned pdshift;
681
682                 if (!mmu_psize_defs[psize].shift)
683                         continue;
684
685                 shift = mmu_psize_to_shift(psize);
686
687 #ifdef CONFIG_PPC_BOOK3S_64
688                 if (shift > PGDIR_SHIFT)
689                         continue;
690                 else if (shift > PUD_SHIFT)
691                         pdshift = PGDIR_SHIFT;
692                 else if (shift > PMD_SHIFT)
693                         pdshift = PUD_SHIFT;
694                 else
695                         pdshift = PMD_SHIFT;
696 #else
697                 if (shift < HUGEPD_PUD_SHIFT)
698                         pdshift = PMD_SHIFT;
699                 else if (shift < HUGEPD_PGD_SHIFT)
700                         pdshift = PUD_SHIFT;
701                 else
702                         pdshift = PGDIR_SHIFT;
703 #endif
704
705                 if (add_huge_page_size(1ULL << shift) < 0)
706                         continue;
707                 /*
708                  * if we have pdshift and shift value same, we don't
709                  * use pgt cache for hugepd.
710                  */
711                 if (pdshift > shift)
712                         pgtable_cache_add(pdshift - shift, NULL);
713 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
714                 else if (!hugepte_cache) {
715                         /*
716                          * Create a kmem cache for hugeptes.  The bottom bits in
717                          * the pte have size information encoded in them, so
718                          * align them to allow this
719                          */
720                         hugepte_cache = kmem_cache_create("hugepte-cache",
721                                                           sizeof(pte_t),
722                                                           HUGEPD_SHIFT_MASK + 1,
723                                                           0, NULL);
724                         if (hugepte_cache == NULL)
725                                 panic("%s: Unable to create kmem cache "
726                                       "for hugeptes\n", __func__);
727
728                 }
729 #endif
730         }
731
732 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
733         /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */
734         if (mmu_psize_defs[MMU_PAGE_4M].shift)
735                 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
736         else if (mmu_psize_defs[MMU_PAGE_512K].shift)
737                 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
738 #else
739         /* Set default large page size. Currently, we pick 16M or 1M
740          * depending on what is available
741          */
742         if (mmu_psize_defs[MMU_PAGE_16M].shift)
743                 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
744         else if (mmu_psize_defs[MMU_PAGE_1M].shift)
745                 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
746         else if (mmu_psize_defs[MMU_PAGE_2M].shift)
747                 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
748 #endif
749         return 0;
750 }
751
752 arch_initcall(hugetlbpage_init);
753
754 void flush_dcache_icache_hugepage(struct page *page)
755 {
756         int i;
757         void *start;
758
759         BUG_ON(!PageCompound(page));
760
761         for (i = 0; i < (1UL << compound_order(page)); i++) {
762                 if (!PageHighMem(page)) {
763                         __flush_dcache_icache(page_address(page+i));
764                 } else {
765                         start = kmap_atomic(page+i);
766                         __flush_dcache_icache(start);
767                         kunmap_atomic(start);
768                 }
769         }
770 }
771
772 #endif /* CONFIG_HUGETLB_PAGE */
773
774 /*
775  * We have 4 cases for pgds and pmds:
776  * (1) invalid (all zeroes)
777  * (2) pointer to next table, as normal; bottom 6 bits == 0
778  * (3) leaf pte for huge page _PAGE_PTE set
779  * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
780  *
781  * So long as we atomically load page table pointers we are safe against teardown,
782  * we can follow the address down to the the page and take a ref on it.
783  * This function need to be called with interrupts disabled. We use this variant
784  * when we have MSR[EE] = 0 but the paca->irq_soft_mask = IRQS_ENABLED
785  */
786 pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
787                         bool *is_thp, unsigned *hpage_shift)
788 {
789         pgd_t pgd, *pgdp;
790         pud_t pud, *pudp;
791         pmd_t pmd, *pmdp;
792         pte_t *ret_pte;
793         hugepd_t *hpdp = NULL;
794         unsigned pdshift = PGDIR_SHIFT;
795
796         if (hpage_shift)
797                 *hpage_shift = 0;
798
799         if (is_thp)
800                 *is_thp = false;
801
802         pgdp = pgdir + pgd_index(ea);
803         pgd  = READ_ONCE(*pgdp);
804         /*
805          * Always operate on the local stack value. This make sure the
806          * value don't get updated by a parallel THP split/collapse,
807          * page fault or a page unmap. The return pte_t * is still not
808          * stable. So should be checked there for above conditions.
809          */
810         if (pgd_none(pgd))
811                 return NULL;
812         else if (pgd_huge(pgd)) {
813                 ret_pte = (pte_t *) pgdp;
814                 goto out;
815         } else if (is_hugepd(__hugepd(pgd_val(pgd))))
816                 hpdp = (hugepd_t *)&pgd;
817         else {
818                 /*
819                  * Even if we end up with an unmap, the pgtable will not
820                  * be freed, because we do an rcu free and here we are
821                  * irq disabled
822                  */
823                 pdshift = PUD_SHIFT;
824                 pudp = pud_offset(&pgd, ea);
825                 pud  = READ_ONCE(*pudp);
826
827                 if (pud_none(pud))
828                         return NULL;
829                 else if (pud_huge(pud)) {
830                         ret_pte = (pte_t *) pudp;
831                         goto out;
832                 } else if (is_hugepd(__hugepd(pud_val(pud))))
833                         hpdp = (hugepd_t *)&pud;
834                 else {
835                         pdshift = PMD_SHIFT;
836                         pmdp = pmd_offset(&pud, ea);
837                         pmd  = READ_ONCE(*pmdp);
838                         /*
839                          * A hugepage collapse is captured by pmd_none, because
840                          * it mark the pmd none and do a hpte invalidate.
841                          */
842                         if (pmd_none(pmd))
843                                 return NULL;
844
845                         if (pmd_trans_huge(pmd) || pmd_devmap(pmd)) {
846                                 if (is_thp)
847                                         *is_thp = true;
848                                 ret_pte = (pte_t *) pmdp;
849                                 goto out;
850                         }
851
852                         if (pmd_huge(pmd)) {
853                                 ret_pte = (pte_t *) pmdp;
854                                 goto out;
855                         } else if (is_hugepd(__hugepd(pmd_val(pmd))))
856                                 hpdp = (hugepd_t *)&pmd;
857                         else
858                                 return pte_offset_kernel(&pmd, ea);
859                 }
860         }
861         if (!hpdp)
862                 return NULL;
863
864         ret_pte = hugepte_offset(*hpdp, ea, pdshift);
865         pdshift = hugepd_shift(*hpdp);
866 out:
867         if (hpage_shift)
868                 *hpage_shift = pdshift;
869         return ret_pte;
870 }
871 EXPORT_SYMBOL_GPL(__find_linux_pte);
872
873 int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
874                 unsigned long end, int write, struct page **pages, int *nr)
875 {
876         unsigned long pte_end;
877         struct page *head, *page;
878         pte_t pte;
879         int refs;
880
881         pte_end = (addr + sz) & ~(sz-1);
882         if (pte_end < end)
883                 end = pte_end;
884
885         pte = READ_ONCE(*ptep);
886
887         if (!pte_access_permitted(pte, write))
888                 return 0;
889
890         /* hugepages are never "special" */
891         VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
892
893         refs = 0;
894         head = pte_page(pte);
895
896         page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
897         do {
898                 VM_BUG_ON(compound_head(page) != head);
899                 pages[*nr] = page;
900                 (*nr)++;
901                 page++;
902                 refs++;
903         } while (addr += PAGE_SIZE, addr != end);
904
905         if (!page_cache_add_speculative(head, refs)) {
906                 *nr -= refs;
907                 return 0;
908         }
909
910         if (unlikely(pte_val(pte) != pte_val(*ptep))) {
911                 /* Could be optimized better */
912                 *nr -= refs;
913                 while (refs--)
914                         put_page(head);
915                 return 0;
916         }
917
918         return 1;
919 }