• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/moduleparam.h>
19 #include <linux/swap.h>
20 #include <linux/swapops.h>
21 #include <linux/kmemleak.h>
22 #include <asm/pgalloc.h>
23 #include <asm/tlb.h>
24 #include <asm/setup.h>
25 #include <asm/hugetlb.h>
26 #include <asm/pte-walk.h>
27 
28 bool hugetlb_disabled = false;
29 
30 #define hugepd_none(hpd)	(hpd_val(hpd) == 0)
31 
32 #define PTE_T_ORDER	(__builtin_ffs(sizeof(pte_basic_t)) - \
33 			 __builtin_ffs(sizeof(void *)))
34 
huge_pte_offset(struct mm_struct * mm,unsigned long addr,unsigned long sz)35 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
36 {
37 	/*
38 	 * Only called for hugetlbfs pages, hence can ignore THP and the
39 	 * irq disabled walk.
40 	 */
41 	return __find_linux_pte(mm->pgd, addr, NULL, NULL);
42 }
43 
__hugepte_alloc(struct mm_struct * mm,hugepd_t * hpdp,unsigned long address,unsigned int pdshift,unsigned int pshift,spinlock_t * ptl)44 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
45 			   unsigned long address, unsigned int pdshift,
46 			   unsigned int pshift, spinlock_t *ptl)
47 {
48 	struct kmem_cache *cachep;
49 	pte_t *new;
50 	int i;
51 	int num_hugepd;
52 
53 	if (pshift >= pdshift) {
54 		cachep = PGT_CACHE(PTE_T_ORDER);
55 		num_hugepd = 1 << (pshift - pdshift);
56 	} else {
57 		cachep = PGT_CACHE(pdshift - pshift);
58 		num_hugepd = 1;
59 	}
60 
61 	if (!cachep) {
62 		WARN_ONCE(1, "No page table cache created for hugetlb tables");
63 		return -ENOMEM;
64 	}
65 
66 	new = kmem_cache_alloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
67 
68 	BUG_ON(pshift > HUGEPD_SHIFT_MASK);
69 	BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
70 
71 	if (!new)
72 		return -ENOMEM;
73 
74 	/*
75 	 * Make sure other cpus find the hugepd set only after a
76 	 * properly initialized page table is visible to them.
77 	 * For more details look for comment in __pte_alloc().
78 	 */
79 	smp_wmb();
80 
81 	spin_lock(ptl);
82 	/*
83 	 * We have multiple higher-level entries that point to the same
84 	 * actual pte location.  Fill in each as we go and backtrack on error.
85 	 * We need all of these so the DTLB pgtable walk code can find the
86 	 * right higher-level entry without knowing if it's a hugepage or not.
87 	 */
88 	for (i = 0; i < num_hugepd; i++, hpdp++) {
89 		if (unlikely(!hugepd_none(*hpdp)))
90 			break;
91 		hugepd_populate(hpdp, new, pshift);
92 	}
93 	/* If we bailed from the for loop early, an error occurred, clean up */
94 	if (i < num_hugepd) {
95 		for (i = i - 1 ; i >= 0; i--, hpdp--)
96 			*hpdp = __hugepd(0);
97 		kmem_cache_free(cachep, new);
98 	} else {
99 		kmemleak_ignore(new);
100 	}
101 	spin_unlock(ptl);
102 	return 0;
103 }
104 
105 /*
106  * At this point we do the placement change only for BOOK3S 64. This would
107  * possibly work on other subarchs.
108  */
huge_pte_alloc(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,unsigned long sz)109 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
110 		      unsigned long addr, unsigned long sz)
111 {
112 	pgd_t *pg;
113 	p4d_t *p4;
114 	pud_t *pu;
115 	pmd_t *pm;
116 	hugepd_t *hpdp = NULL;
117 	unsigned pshift = __ffs(sz);
118 	unsigned pdshift = PGDIR_SHIFT;
119 	spinlock_t *ptl;
120 
121 	addr &= ~(sz-1);
122 	pg = pgd_offset(mm, addr);
123 	p4 = p4d_offset(pg, addr);
124 
125 #ifdef CONFIG_PPC_BOOK3S_64
126 	if (pshift == PGDIR_SHIFT)
127 		/* 16GB huge page */
128 		return (pte_t *) p4;
129 	else if (pshift > PUD_SHIFT) {
130 		/*
131 		 * We need to use hugepd table
132 		 */
133 		ptl = &mm->page_table_lock;
134 		hpdp = (hugepd_t *)p4;
135 	} else {
136 		pdshift = PUD_SHIFT;
137 		pu = pud_alloc(mm, p4, addr);
138 		if (!pu)
139 			return NULL;
140 		if (pshift == PUD_SHIFT)
141 			return (pte_t *)pu;
142 		else if (pshift > PMD_SHIFT) {
143 			ptl = pud_lockptr(mm, pu);
144 			hpdp = (hugepd_t *)pu;
145 		} else {
146 			pdshift = PMD_SHIFT;
147 			pm = pmd_alloc(mm, pu, addr);
148 			if (!pm)
149 				return NULL;
150 			if (pshift == PMD_SHIFT)
151 				/* 16MB hugepage */
152 				return (pte_t *)pm;
153 			else {
154 				ptl = pmd_lockptr(mm, pm);
155 				hpdp = (hugepd_t *)pm;
156 			}
157 		}
158 	}
159 #else
160 	if (pshift >= PGDIR_SHIFT) {
161 		ptl = &mm->page_table_lock;
162 		hpdp = (hugepd_t *)p4;
163 	} else {
164 		pdshift = PUD_SHIFT;
165 		pu = pud_alloc(mm, p4, addr);
166 		if (!pu)
167 			return NULL;
168 		if (pshift >= PUD_SHIFT) {
169 			ptl = pud_lockptr(mm, pu);
170 			hpdp = (hugepd_t *)pu;
171 		} else {
172 			pdshift = PMD_SHIFT;
173 			pm = pmd_alloc(mm, pu, addr);
174 			if (!pm)
175 				return NULL;
176 			ptl = pmd_lockptr(mm, pm);
177 			hpdp = (hugepd_t *)pm;
178 		}
179 	}
180 #endif
181 	if (!hpdp)
182 		return NULL;
183 
184 	if (IS_ENABLED(CONFIG_PPC_8xx) && pshift < PMD_SHIFT)
185 		return pte_alloc_map(mm, (pmd_t *)hpdp, addr);
186 
187 	BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
188 
189 	if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr,
190 						  pdshift, pshift, ptl))
191 		return NULL;
192 
193 	return hugepte_offset(*hpdp, addr, pdshift);
194 }
195 
196 #ifdef CONFIG_PPC_BOOK3S_64
197 /*
198  * Tracks gpages after the device tree is scanned and before the
199  * huge_boot_pages list is ready on pseries.
200  */
201 #define MAX_NUMBER_GPAGES	1024
202 __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
203 __initdata static unsigned nr_gpages;
204 
205 /*
206  * Build list of addresses of gigantic pages.  This function is used in early
207  * boot before the buddy allocator is setup.
208  */
pseries_add_gpage(u64 addr,u64 page_size,unsigned long number_of_pages)209 void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
210 {
211 	if (!addr)
212 		return;
213 	while (number_of_pages > 0) {
214 		gpage_freearray[nr_gpages] = addr;
215 		nr_gpages++;
216 		number_of_pages--;
217 		addr += page_size;
218 	}
219 }
220 
pseries_alloc_bootmem_huge_page(struct hstate * hstate)221 int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
222 {
223 	struct huge_bootmem_page *m;
224 	if (nr_gpages == 0)
225 		return 0;
226 	m = phys_to_virt(gpage_freearray[--nr_gpages]);
227 	gpage_freearray[nr_gpages] = 0;
228 	list_add(&m->list, &huge_boot_pages);
229 	m->hstate = hstate;
230 	return 1;
231 }
232 #endif
233 
234 
alloc_bootmem_huge_page(struct hstate * h)235 int __init alloc_bootmem_huge_page(struct hstate *h)
236 {
237 
238 #ifdef CONFIG_PPC_BOOK3S_64
239 	if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
240 		return pseries_alloc_bootmem_huge_page(h);
241 #endif
242 	return __alloc_bootmem_huge_page(h);
243 }
244 
245 #ifndef CONFIG_PPC_BOOK3S_64
246 #define HUGEPD_FREELIST_SIZE \
247 	((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
248 
249 struct hugepd_freelist {
250 	struct rcu_head	rcu;
251 	unsigned int index;
252 	void *ptes[];
253 };
254 
255 static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
256 
hugepd_free_rcu_callback(struct rcu_head * head)257 static void hugepd_free_rcu_callback(struct rcu_head *head)
258 {
259 	struct hugepd_freelist *batch =
260 		container_of(head, struct hugepd_freelist, rcu);
261 	unsigned int i;
262 
263 	for (i = 0; i < batch->index; i++)
264 		kmem_cache_free(PGT_CACHE(PTE_T_ORDER), batch->ptes[i]);
265 
266 	free_page((unsigned long)batch);
267 }
268 
hugepd_free(struct mmu_gather * tlb,void * hugepte)269 static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
270 {
271 	struct hugepd_freelist **batchp;
272 
273 	batchp = &get_cpu_var(hugepd_freelist_cur);
274 
275 	if (atomic_read(&tlb->mm->mm_users) < 2 ||
276 	    mm_is_thread_local(tlb->mm)) {
277 		kmem_cache_free(PGT_CACHE(PTE_T_ORDER), hugepte);
278 		put_cpu_var(hugepd_freelist_cur);
279 		return;
280 	}
281 
282 	if (*batchp == NULL) {
283 		*batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
284 		(*batchp)->index = 0;
285 	}
286 
287 	(*batchp)->ptes[(*batchp)->index++] = hugepte;
288 	if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
289 		call_rcu(&(*batchp)->rcu, hugepd_free_rcu_callback);
290 		*batchp = NULL;
291 	}
292 	put_cpu_var(hugepd_freelist_cur);
293 }
294 #else
hugepd_free(struct mmu_gather * tlb,void * hugepte)295 static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
296 #endif
297 
free_hugepd_range(struct mmu_gather * tlb,hugepd_t * hpdp,int pdshift,unsigned long start,unsigned long end,unsigned long floor,unsigned long ceiling)298 static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
299 			      unsigned long start, unsigned long end,
300 			      unsigned long floor, unsigned long ceiling)
301 {
302 	pte_t *hugepte = hugepd_page(*hpdp);
303 	int i;
304 
305 	unsigned long pdmask = ~((1UL << pdshift) - 1);
306 	unsigned int num_hugepd = 1;
307 	unsigned int shift = hugepd_shift(*hpdp);
308 
309 	/* Note: On fsl the hpdp may be the first of several */
310 	if (shift > pdshift)
311 		num_hugepd = 1 << (shift - pdshift);
312 
313 	start &= pdmask;
314 	if (start < floor)
315 		return;
316 	if (ceiling) {
317 		ceiling &= pdmask;
318 		if (! ceiling)
319 			return;
320 	}
321 	if (end - 1 > ceiling - 1)
322 		return;
323 
324 	for (i = 0; i < num_hugepd; i++, hpdp++)
325 		*hpdp = __hugepd(0);
326 
327 	if (shift >= pdshift)
328 		hugepd_free(tlb, hugepte);
329 	else
330 		pgtable_free_tlb(tlb, hugepte,
331 				 get_hugepd_cache_index(pdshift - shift));
332 }
333 
hugetlb_free_pte_range(struct mmu_gather * tlb,pmd_t * pmd,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)334 static void hugetlb_free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
335 				   unsigned long addr, unsigned long end,
336 				   unsigned long floor, unsigned long ceiling)
337 {
338 	unsigned long start = addr;
339 	pgtable_t token = pmd_pgtable(*pmd);
340 
341 	start &= PMD_MASK;
342 	if (start < floor)
343 		return;
344 	if (ceiling) {
345 		ceiling &= PMD_MASK;
346 		if (!ceiling)
347 			return;
348 	}
349 	if (end - 1 > ceiling - 1)
350 		return;
351 
352 	pmd_clear(pmd);
353 	pte_free_tlb(tlb, token, addr);
354 	mm_dec_nr_ptes(tlb->mm);
355 }
356 
hugetlb_free_pmd_range(struct mmu_gather * tlb,pud_t * pud,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)357 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
358 				   unsigned long addr, unsigned long end,
359 				   unsigned long floor, unsigned long ceiling)
360 {
361 	pmd_t *pmd;
362 	unsigned long next;
363 	unsigned long start;
364 
365 	start = addr;
366 	do {
367 		unsigned long more;
368 
369 		pmd = pmd_offset(pud, addr);
370 		next = pmd_addr_end(addr, end);
371 		if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
372 			if (pmd_none_or_clear_bad(pmd))
373 				continue;
374 
375 			/*
376 			 * if it is not hugepd pointer, we should already find
377 			 * it cleared.
378 			 */
379 			WARN_ON(!IS_ENABLED(CONFIG_PPC_8xx));
380 
381 			hugetlb_free_pte_range(tlb, pmd, addr, end, floor, ceiling);
382 
383 			continue;
384 		}
385 		/*
386 		 * Increment next by the size of the huge mapping since
387 		 * there may be more than one entry at this level for a
388 		 * single hugepage, but all of them point to
389 		 * the same kmem cache that holds the hugepte.
390 		 */
391 		more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
392 		if (more > next)
393 			next = more;
394 
395 		free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
396 				  addr, next, floor, ceiling);
397 	} while (addr = next, addr != end);
398 
399 	start &= PUD_MASK;
400 	if (start < floor)
401 		return;
402 	if (ceiling) {
403 		ceiling &= PUD_MASK;
404 		if (!ceiling)
405 			return;
406 	}
407 	if (end - 1 > ceiling - 1)
408 		return;
409 
410 	pmd = pmd_offset(pud, start);
411 	pud_clear(pud);
412 	pmd_free_tlb(tlb, pmd, start);
413 	mm_dec_nr_pmds(tlb->mm);
414 }
415 
hugetlb_free_pud_range(struct mmu_gather * tlb,p4d_t * p4d,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)416 static void hugetlb_free_pud_range(struct mmu_gather *tlb, p4d_t *p4d,
417 				   unsigned long addr, unsigned long end,
418 				   unsigned long floor, unsigned long ceiling)
419 {
420 	pud_t *pud;
421 	unsigned long next;
422 	unsigned long start;
423 
424 	start = addr;
425 	do {
426 		pud = pud_offset(p4d, addr);
427 		next = pud_addr_end(addr, end);
428 		if (!is_hugepd(__hugepd(pud_val(*pud)))) {
429 			if (pud_none_or_clear_bad(pud))
430 				continue;
431 			hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
432 					       ceiling);
433 		} else {
434 			unsigned long more;
435 			/*
436 			 * Increment next by the size of the huge mapping since
437 			 * there may be more than one entry at this level for a
438 			 * single hugepage, but all of them point to
439 			 * the same kmem cache that holds the hugepte.
440 			 */
441 			more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
442 			if (more > next)
443 				next = more;
444 
445 			free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
446 					  addr, next, floor, ceiling);
447 		}
448 	} while (addr = next, addr != end);
449 
450 	start &= PGDIR_MASK;
451 	if (start < floor)
452 		return;
453 	if (ceiling) {
454 		ceiling &= PGDIR_MASK;
455 		if (!ceiling)
456 			return;
457 	}
458 	if (end - 1 > ceiling - 1)
459 		return;
460 
461 	pud = pud_offset(p4d, start);
462 	p4d_clear(p4d);
463 	pud_free_tlb(tlb, pud, start);
464 	mm_dec_nr_puds(tlb->mm);
465 }
466 
467 /*
468  * This function frees user-level page tables of a process.
469  */
hugetlb_free_pgd_range(struct mmu_gather * tlb,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)470 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
471 			    unsigned long addr, unsigned long end,
472 			    unsigned long floor, unsigned long ceiling)
473 {
474 	pgd_t *pgd;
475 	p4d_t *p4d;
476 	unsigned long next;
477 
478 	/*
479 	 * Because there are a number of different possible pagetable
480 	 * layouts for hugepage ranges, we limit knowledge of how
481 	 * things should be laid out to the allocation path
482 	 * (huge_pte_alloc(), above).  Everything else works out the
483 	 * structure as it goes from information in the hugepd
484 	 * pointers.  That means that we can't here use the
485 	 * optimization used in the normal page free_pgd_range(), of
486 	 * checking whether we're actually covering a large enough
487 	 * range to have to do anything at the top level of the walk
488 	 * instead of at the bottom.
489 	 *
490 	 * To make sense of this, you should probably go read the big
491 	 * block comment at the top of the normal free_pgd_range(),
492 	 * too.
493 	 */
494 
495 	do {
496 		next = pgd_addr_end(addr, end);
497 		pgd = pgd_offset(tlb->mm, addr);
498 		p4d = p4d_offset(pgd, addr);
499 		if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
500 			if (p4d_none_or_clear_bad(p4d))
501 				continue;
502 			hugetlb_free_pud_range(tlb, p4d, addr, next, floor, ceiling);
503 		} else {
504 			unsigned long more;
505 			/*
506 			 * Increment next by the size of the huge mapping since
507 			 * there may be more than one entry at the pgd level
508 			 * for a single hugepage, but all of them point to the
509 			 * same kmem cache that holds the hugepte.
510 			 */
511 			more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
512 			if (more > next)
513 				next = more;
514 
515 			free_hugepd_range(tlb, (hugepd_t *)p4d, PGDIR_SHIFT,
516 					  addr, next, floor, ceiling);
517 		}
518 	} while (addr = next, addr != end);
519 }
520 
follow_huge_pd(struct vm_area_struct * vma,unsigned long address,hugepd_t hpd,int flags,int pdshift)521 struct page *follow_huge_pd(struct vm_area_struct *vma,
522 			    unsigned long address, hugepd_t hpd,
523 			    int flags, int pdshift)
524 {
525 	pte_t *ptep;
526 	spinlock_t *ptl;
527 	struct page *page = NULL;
528 	unsigned long mask;
529 	int shift = hugepd_shift(hpd);
530 	struct mm_struct *mm = vma->vm_mm;
531 
532 retry:
533 	/*
534 	 * hugepage directory entries are protected by mm->page_table_lock
535 	 * Use this instead of huge_pte_lockptr
536 	 */
537 	ptl = &mm->page_table_lock;
538 	spin_lock(ptl);
539 
540 	ptep = hugepte_offset(hpd, address, pdshift);
541 	if (pte_present(*ptep)) {
542 		mask = (1UL << shift) - 1;
543 		page = pte_page(*ptep);
544 		page += ((address & mask) >> PAGE_SHIFT);
545 		if (flags & FOLL_GET)
546 			get_page(page);
547 	} else {
548 		if (is_hugetlb_entry_migration(*ptep)) {
549 			spin_unlock(ptl);
550 			__migration_entry_wait(mm, ptep, ptl);
551 			goto retry;
552 		}
553 	}
554 	spin_unlock(ptl);
555 	return page;
556 }
557 
558 #ifdef CONFIG_PPC_MM_SLICES
hugetlb_get_unmapped_area(struct file * file,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)559 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
560 					unsigned long len, unsigned long pgoff,
561 					unsigned long flags)
562 {
563 	struct hstate *hstate = hstate_file(file);
564 	int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
565 
566 #ifdef CONFIG_PPC_RADIX_MMU
567 	if (radix_enabled())
568 		return radix__hugetlb_get_unmapped_area(file, addr, len,
569 						       pgoff, flags);
570 #endif
571 	return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
572 }
573 #endif
574 
vma_mmu_pagesize(struct vm_area_struct * vma)575 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
576 {
577 	/* With radix we don't use slice, so derive it from vma*/
578 	if (IS_ENABLED(CONFIG_PPC_MM_SLICES) && !radix_enabled()) {
579 		unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
580 
581 		return 1UL << mmu_psize_to_shift(psize);
582 	}
583 	return vma_kernel_pagesize(vma);
584 }
585 
arch_hugetlb_valid_size(unsigned long size)586 bool __init arch_hugetlb_valid_size(unsigned long size)
587 {
588 	int shift = __ffs(size);
589 	int mmu_psize;
590 
591 	/* Check that it is a page size supported by the hardware and
592 	 * that it fits within pagetable and slice limits. */
593 	if (size <= PAGE_SIZE || !is_power_of_2(size))
594 		return false;
595 
596 	mmu_psize = check_and_get_huge_psize(shift);
597 	if (mmu_psize < 0)
598 		return false;
599 
600 	BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
601 
602 	return true;
603 }
604 
add_huge_page_size(unsigned long long size)605 static int __init add_huge_page_size(unsigned long long size)
606 {
607 	int shift = __ffs(size);
608 
609 	if (!arch_hugetlb_valid_size((unsigned long)size))
610 		return -EINVAL;
611 
612 	hugetlb_add_hstate(shift - PAGE_SHIFT);
613 	return 0;
614 }
615 
hugetlbpage_init(void)616 static int __init hugetlbpage_init(void)
617 {
618 	bool configured = false;
619 	int psize;
620 
621 	if (hugetlb_disabled) {
622 		pr_info("HugeTLB support is disabled!\n");
623 		return 0;
624 	}
625 
626 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled() &&
627 	    !mmu_has_feature(MMU_FTR_16M_PAGE))
628 		return -ENODEV;
629 
630 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
631 		unsigned shift;
632 		unsigned pdshift;
633 
634 		if (!mmu_psize_defs[psize].shift)
635 			continue;
636 
637 		shift = mmu_psize_to_shift(psize);
638 
639 #ifdef CONFIG_PPC_BOOK3S_64
640 		if (shift > PGDIR_SHIFT)
641 			continue;
642 		else if (shift > PUD_SHIFT)
643 			pdshift = PGDIR_SHIFT;
644 		else if (shift > PMD_SHIFT)
645 			pdshift = PUD_SHIFT;
646 		else
647 			pdshift = PMD_SHIFT;
648 #else
649 		if (shift < PUD_SHIFT)
650 			pdshift = PMD_SHIFT;
651 		else if (shift < PGDIR_SHIFT)
652 			pdshift = PUD_SHIFT;
653 		else
654 			pdshift = PGDIR_SHIFT;
655 #endif
656 
657 		if (add_huge_page_size(1ULL << shift) < 0)
658 			continue;
659 		/*
660 		 * if we have pdshift and shift value same, we don't
661 		 * use pgt cache for hugepd.
662 		 */
663 		if (pdshift > shift) {
664 			if (!IS_ENABLED(CONFIG_PPC_8xx))
665 				pgtable_cache_add(pdshift - shift);
666 		} else if (IS_ENABLED(CONFIG_PPC_FSL_BOOK3E) ||
667 			   IS_ENABLED(CONFIG_PPC_8xx)) {
668 			pgtable_cache_add(PTE_T_ORDER);
669 		}
670 
671 		configured = true;
672 	}
673 
674 	if (configured) {
675 		if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE))
676 			hugetlbpage_init_default();
677 	} else
678 		pr_info("Failed to initialize. Disabling HugeTLB");
679 
680 	return 0;
681 }
682 
683 arch_initcall(hugetlbpage_init);
684 
flush_dcache_icache_hugepage(struct page * page)685 void flush_dcache_icache_hugepage(struct page *page)
686 {
687 	int i;
688 	void *start;
689 
690 	BUG_ON(!PageCompound(page));
691 
692 	for (i = 0; i < compound_nr(page); i++) {
693 		if (!PageHighMem(page)) {
694 			__flush_dcache_icache(page_address(page+i));
695 		} else {
696 			start = kmap_atomic(page+i);
697 			__flush_dcache_icache(start);
698 			kunmap_atomic(start);
699 		}
700 	}
701 }
702 
gigantic_hugetlb_cma_reserve(void)703 void __init gigantic_hugetlb_cma_reserve(void)
704 {
705 	unsigned long order = 0;
706 
707 	if (radix_enabled())
708 		order = PUD_SHIFT - PAGE_SHIFT;
709 	else if (!firmware_has_feature(FW_FEATURE_LPAR) && mmu_psize_defs[MMU_PAGE_16G].shift)
710 		/*
711 		 * For pseries we do use ibm,expected#pages for reserving 16G pages.
712 		 */
713 		order = mmu_psize_to_shift(MMU_PAGE_16G) - PAGE_SHIFT;
714 
715 	if (order) {
716 		VM_WARN_ON(order < MAX_ORDER);
717 		hugetlb_cma_reserve(order);
718 	}
719 }
720