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