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