blob: b4e2f24a9b8f5c84e5ec24ad696fe27c21d9dc56 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Becky Bruce41151e72011-06-28 09:54:48 +00002 * PPC Huge TLB Page Support for Kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
Becky Bruce41151e72011-06-28 09:54:48 +00005 * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on the IA-32 version:
8 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
David Gibson883a3e52009-10-26 19:24:31 +000012#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/hugetlb.h>
Paul Mackerras342d3db2011-12-12 12:38:05 +000015#include <linux/export.h>
Becky Bruce41151e72011-06-28 09:54:48 +000016#include <linux/of_fdt.h>
17#include <linux/memblock.h>
18#include <linux/bootmem.h>
Kumar Gala13020be2011-11-24 09:40:07 +000019#include <linux/moduleparam.h>
David Gibson883a3e52009-10-26 19:24:31 +000020#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/pgalloc.h>
22#include <asm/tlb.h>
Becky Bruce41151e72011-06-28 09:54:48 +000023#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jon Tollefson91224342008-07-23 21:27:55 -070025#define PAGE_SHIFT_64K 16
26#define PAGE_SHIFT_16M 24
27#define PAGE_SHIFT_16G 34
Jon Tollefson4ec161c2008-01-04 09:59:50 +110028
Becky Bruce41151e72011-06-28 09:54:48 +000029unsigned int HPAGE_SHIFT;
30
31/*
32 * Tracks gpages after the device tree is scanned and before the
Becky Brucea6146882011-10-10 10:50:43 +000033 * huge_boot_pages list is ready. On non-Freescale implementations, this is
34 * just used to track 16G pages and so is a single array. FSL-based
35 * implementations may have more than one gpage size, so we need multiple
36 * arrays
Becky Bruce41151e72011-06-28 09:54:48 +000037 */
Becky Bruce881fde12011-10-10 10:50:40 +000038#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +000039#define MAX_NUMBER_GPAGES 128
40struct psize_gpages {
41 u64 gpage_list[MAX_NUMBER_GPAGES];
42 unsigned int nr_gpages;
43};
44static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT];
Becky Bruce881fde12011-10-10 10:50:40 +000045#else
46#define MAX_NUMBER_GPAGES 1024
47static u64 gpage_freearray[MAX_NUMBER_GPAGES];
48static unsigned nr_gpages;
Becky Bruce41151e72011-06-28 09:54:48 +000049#endif
David Gibsonf10a04c2006-04-28 15:02:51 +100050
David Gibsona4fe3ce2009-10-26 19:24:31 +000051#define hugepd_none(hpd) ((hpd).pd == 0)
52
David Gibsona4fe3ce2009-10-26 19:24:31 +000053pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, unsigned *shift)
David Gibsonf10a04c2006-04-28 15:02:51 +100054{
David Gibsona4fe3ce2009-10-26 19:24:31 +000055 pgd_t *pg;
56 pud_t *pu;
57 pmd_t *pm;
58 hugepd_t *hpdp = NULL;
59 unsigned pdshift = PGDIR_SHIFT;
60
61 if (shift)
62 *shift = 0;
63
64 pg = pgdir + pgd_index(ea);
65 if (is_hugepd(pg)) {
66 hpdp = (hugepd_t *)pg;
67 } else if (!pgd_none(*pg)) {
68 pdshift = PUD_SHIFT;
69 pu = pud_offset(pg, ea);
70 if (is_hugepd(pu))
71 hpdp = (hugepd_t *)pu;
72 else if (!pud_none(*pu)) {
73 pdshift = PMD_SHIFT;
74 pm = pmd_offset(pu, ea);
75 if (is_hugepd(pm))
76 hpdp = (hugepd_t *)pm;
77 else if (!pmd_none(*pm)) {
Becky Bruce41151e72011-06-28 09:54:48 +000078 return pte_offset_kernel(pm, ea);
David Gibsona4fe3ce2009-10-26 19:24:31 +000079 }
80 }
81 }
82
83 if (!hpdp)
84 return NULL;
85
86 if (shift)
87 *shift = hugepd_shift(*hpdp);
88 return hugepte_offset(hpdp, ea, pdshift);
89}
Paul Mackerras342d3db2011-12-12 12:38:05 +000090EXPORT_SYMBOL_GPL(find_linux_pte_or_hugepte);
David Gibsona4fe3ce2009-10-26 19:24:31 +000091
92pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
93{
94 return find_linux_pte_or_hugepte(mm->pgd, addr, NULL);
95}
96
97static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
98 unsigned long address, unsigned pdshift, unsigned pshift)
99{
Becky Bruce41151e72011-06-28 09:54:48 +0000100 struct kmem_cache *cachep;
101 pte_t *new;
102
Becky Bruce881fde12011-10-10 10:50:40 +0000103#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000104 int i;
105 int num_hugepd = 1 << (pshift - pdshift);
106 cachep = hugepte_cache;
Becky Bruce881fde12011-10-10 10:50:40 +0000107#else
108 cachep = PGT_CACHE(pdshift - pshift);
Becky Bruce41151e72011-06-28 09:54:48 +0000109#endif
110
111 new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
David Gibsonf10a04c2006-04-28 15:02:51 +1000112
David Gibsona4fe3ce2009-10-26 19:24:31 +0000113 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
114 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
115
David Gibsonf10a04c2006-04-28 15:02:51 +1000116 if (! new)
117 return -ENOMEM;
118
119 spin_lock(&mm->page_table_lock);
Becky Bruce881fde12011-10-10 10:50:40 +0000120#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000121 /*
122 * We have multiple higher-level entries that point to the same
123 * actual pte location. Fill in each as we go and backtrack on error.
124 * We need all of these so the DTLB pgtable walk code can find the
125 * right higher-level entry without knowing if it's a hugepage or not.
126 */
127 for (i = 0; i < num_hugepd; i++, hpdp++) {
128 if (unlikely(!hugepd_none(*hpdp)))
129 break;
130 else
Aneesh Kumar K.Vcf9427b2013-04-28 09:37:29 +0000131 /* We use the old format for PPC_FSL_BOOK3E */
Becky Bruce41151e72011-06-28 09:54:48 +0000132 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
133 }
134 /* If we bailed from the for loop early, an error occurred, clean up */
135 if (i < num_hugepd) {
136 for (i = i - 1 ; i >= 0; i--, hpdp--)
137 hpdp->pd = 0;
138 kmem_cache_free(cachep, new);
139 }
Becky Brucea1cd5412011-10-10 10:50:39 +0000140#else
141 if (!hugepd_none(*hpdp))
142 kmem_cache_free(cachep, new);
Aneesh Kumar K.Vcf9427b2013-04-28 09:37:29 +0000143 else {
144#ifdef CONFIG_PPC_BOOK3S_64
145 hpdp->pd = (unsigned long)new |
146 (shift_to_mmu_psize(pshift) << 2);
147#else
Becky Brucea1cd5412011-10-10 10:50:39 +0000148 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
Becky Bruce41151e72011-06-28 09:54:48 +0000149#endif
Aneesh Kumar K.Vcf9427b2013-04-28 09:37:29 +0000150 }
151#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000152 spin_unlock(&mm->page_table_lock);
153 return 0;
154}
155
Becky Brucea1cd5412011-10-10 10:50:39 +0000156/*
157 * These macros define how to determine which level of the page table holds
158 * the hpdp.
159 */
160#ifdef CONFIG_PPC_FSL_BOOK3E
161#define HUGEPD_PGD_SHIFT PGDIR_SHIFT
162#define HUGEPD_PUD_SHIFT PUD_SHIFT
163#else
164#define HUGEPD_PGD_SHIFT PUD_SHIFT
165#define HUGEPD_PUD_SHIFT PMD_SHIFT
166#endif
167
David Gibsona4fe3ce2009-10-26 19:24:31 +0000168pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
169{
170 pgd_t *pg;
171 pud_t *pu;
172 pmd_t *pm;
173 hugepd_t *hpdp = NULL;
174 unsigned pshift = __ffs(sz);
175 unsigned pdshift = PGDIR_SHIFT;
David Gibson0b264252008-09-05 11:49:54 +1000176
David Gibsona4fe3ce2009-10-26 19:24:31 +0000177 addr &= ~(sz-1);
178
179 pg = pgd_offset(mm, addr);
Becky Brucea1cd5412011-10-10 10:50:39 +0000180
181 if (pshift >= HUGEPD_PGD_SHIFT) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000182 hpdp = (hugepd_t *)pg;
183 } else {
184 pdshift = PUD_SHIFT;
185 pu = pud_alloc(mm, pg, addr);
Becky Brucea1cd5412011-10-10 10:50:39 +0000186 if (pshift >= HUGEPD_PUD_SHIFT) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000187 hpdp = (hugepd_t *)pu;
188 } else {
189 pdshift = PMD_SHIFT;
190 pm = pmd_alloc(mm, pu, addr);
191 hpdp = (hugepd_t *)pm;
192 }
193 }
194
195 if (!hpdp)
196 return NULL;
197
198 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
199
200 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
201 return NULL;
202
203 return hugepte_offset(hpdp, addr, pdshift);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100204}
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100205
Becky Bruce881fde12011-10-10 10:50:40 +0000206#ifdef CONFIG_PPC_FSL_BOOK3E
Jon Tollefson658013e2008-07-23 21:27:54 -0700207/* Build list of addresses of gigantic pages. This function is used in early
208 * boot before the buddy or bootmem allocator is setup.
209 */
Becky Bruce41151e72011-06-28 09:54:48 +0000210void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
211{
212 unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
213 int i;
214
215 if (addr == 0)
216 return;
217
218 gpage_freearray[idx].nr_gpages = number_of_pages;
219
220 for (i = 0; i < number_of_pages; i++) {
221 gpage_freearray[idx].gpage_list[i] = addr;
222 addr += page_size;
223 }
224}
225
226/*
227 * Moves the gigantic page addresses from the temporary list to the
228 * huge_boot_pages list.
229 */
230int alloc_bootmem_huge_page(struct hstate *hstate)
231{
232 struct huge_bootmem_page *m;
233 int idx = shift_to_mmu_psize(hstate->order + PAGE_SHIFT);
234 int nr_gpages = gpage_freearray[idx].nr_gpages;
235
236 if (nr_gpages == 0)
237 return 0;
238
239#ifdef CONFIG_HIGHMEM
240 /*
241 * If gpages can be in highmem we can't use the trick of storing the
242 * data structure in the page; allocate space for this
243 */
244 m = alloc_bootmem(sizeof(struct huge_bootmem_page));
245 m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
246#else
247 m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
248#endif
249
250 list_add(&m->list, &huge_boot_pages);
251 gpage_freearray[idx].nr_gpages = nr_gpages;
252 gpage_freearray[idx].gpage_list[nr_gpages] = 0;
253 m->hstate = hstate;
254
255 return 1;
256}
257/*
258 * Scan the command line hugepagesz= options for gigantic pages; store those in
259 * a list that we use to allocate the memory once all options are parsed.
260 */
261
262unsigned long gpage_npages[MMU_PAGE_COUNT];
263
Paul Gortmaker89528122012-05-07 10:32:22 -0400264static int __init do_gpage_early_setup(char *param, char *val,
265 const char *unused)
Becky Bruce41151e72011-06-28 09:54:48 +0000266{
267 static phys_addr_t size;
268 unsigned long npages;
269
270 /*
271 * The hugepagesz and hugepages cmdline options are interleaved. We
272 * use the size variable to keep track of whether or not this was done
273 * properly and skip over instances where it is incorrect. Other
274 * command-line parsing code will issue warnings, so we don't need to.
275 *
276 */
277 if ((strcmp(param, "default_hugepagesz") == 0) ||
278 (strcmp(param, "hugepagesz") == 0)) {
279 size = memparse(val, NULL);
280 } else if (strcmp(param, "hugepages") == 0) {
281 if (size != 0) {
282 if (sscanf(val, "%lu", &npages) <= 0)
283 npages = 0;
284 gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
285 size = 0;
286 }
287 }
288 return 0;
289}
290
291
292/*
293 * This function allocates physical space for pages that are larger than the
294 * buddy allocator can handle. We want to allocate these in highmem because
295 * the amount of lowmem is limited. This means that this function MUST be
296 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
297 * allocate to grab highmem.
298 */
299void __init reserve_hugetlb_gpages(void)
300{
301 static __initdata char cmdline[COMMAND_LINE_SIZE];
302 phys_addr_t size, base;
303 int i;
304
305 strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
Pawel Moll026cee02012-03-26 12:50:51 +1030306 parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0,
307 &do_gpage_early_setup);
Becky Bruce41151e72011-06-28 09:54:48 +0000308
309 /*
310 * Walk gpage list in reverse, allocating larger page sizes first.
311 * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
312 * When we reach the point in the list where pages are no longer
313 * considered gpages, we're done.
314 */
315 for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
316 if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
317 continue;
318 else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
319 break;
320
321 size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
322 base = memblock_alloc_base(size * gpage_npages[i], size,
323 MEMBLOCK_ALLOC_ANYWHERE);
324 add_gpage(base, size, gpage_npages[i]);
325 }
326}
327
Becky Bruce881fde12011-10-10 10:50:40 +0000328#else /* !PPC_FSL_BOOK3E */
Becky Bruce41151e72011-06-28 09:54:48 +0000329
330/* Build list of addresses of gigantic pages. This function is used in early
331 * boot before the buddy or bootmem allocator is setup.
332 */
333void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
Jon Tollefson658013e2008-07-23 21:27:54 -0700334{
335 if (!addr)
336 return;
337 while (number_of_pages > 0) {
338 gpage_freearray[nr_gpages] = addr;
339 nr_gpages++;
340 number_of_pages--;
341 addr += page_size;
342 }
343}
344
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700345/* Moves the gigantic page addresses from the temporary list to the
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700346 * huge_boot_pages list.
347 */
348int alloc_bootmem_huge_page(struct hstate *hstate)
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700349{
350 struct huge_bootmem_page *m;
351 if (nr_gpages == 0)
352 return 0;
353 m = phys_to_virt(gpage_freearray[--nr_gpages]);
354 gpage_freearray[nr_gpages] = 0;
355 list_add(&m->list, &huge_boot_pages);
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700356 m->hstate = hstate;
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700357 return 1;
358}
Becky Bruce41151e72011-06-28 09:54:48 +0000359#endif
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700360
Chen, Kenneth W39dde652006-12-06 20:32:03 -0800361int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
362{
363 return 0;
364}
365
Becky Bruce881fde12011-10-10 10:50:40 +0000366#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000367#define HUGEPD_FREELIST_SIZE \
368 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
369
370struct hugepd_freelist {
371 struct rcu_head rcu;
372 unsigned int index;
373 void *ptes[0];
374};
375
376static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
377
378static void hugepd_free_rcu_callback(struct rcu_head *head)
379{
380 struct hugepd_freelist *batch =
381 container_of(head, struct hugepd_freelist, rcu);
382 unsigned int i;
383
384 for (i = 0; i < batch->index; i++)
385 kmem_cache_free(hugepte_cache, batch->ptes[i]);
386
387 free_page((unsigned long)batch);
388}
389
390static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
391{
392 struct hugepd_freelist **batchp;
393
394 batchp = &__get_cpu_var(hugepd_freelist_cur);
395
396 if (atomic_read(&tlb->mm->mm_users) < 2 ||
397 cpumask_equal(mm_cpumask(tlb->mm),
398 cpumask_of(smp_processor_id()))) {
399 kmem_cache_free(hugepte_cache, hugepte);
400 return;
401 }
402
403 if (*batchp == NULL) {
404 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
405 (*batchp)->index = 0;
406 }
407
408 (*batchp)->ptes[(*batchp)->index++] = hugepte;
409 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
410 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
411 *batchp = NULL;
412 }
413}
414#endif
415
David Gibsona4fe3ce2009-10-26 19:24:31 +0000416static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
417 unsigned long start, unsigned long end,
418 unsigned long floor, unsigned long ceiling)
David Gibsonf10a04c2006-04-28 15:02:51 +1000419{
420 pte_t *hugepte = hugepd_page(*hpdp);
Becky Bruce41151e72011-06-28 09:54:48 +0000421 int i;
422
David Gibsona4fe3ce2009-10-26 19:24:31 +0000423 unsigned long pdmask = ~((1UL << pdshift) - 1);
Becky Bruce41151e72011-06-28 09:54:48 +0000424 unsigned int num_hugepd = 1;
425
Becky Bruce881fde12011-10-10 10:50:40 +0000426#ifdef CONFIG_PPC_FSL_BOOK3E
427 /* Note: On fsl the hpdp may be the first of several */
Becky Bruce41151e72011-06-28 09:54:48 +0000428 num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
Becky Bruce881fde12011-10-10 10:50:40 +0000429#else
430 unsigned int shift = hugepd_shift(*hpdp);
Becky Bruce41151e72011-06-28 09:54:48 +0000431#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000432
433 start &= pdmask;
434 if (start < floor)
435 return;
436 if (ceiling) {
437 ceiling &= pdmask;
438 if (! ceiling)
439 return;
440 }
441 if (end - 1 > ceiling - 1)
442 return;
David Gibsonf10a04c2006-04-28 15:02:51 +1000443
Becky Bruce41151e72011-06-28 09:54:48 +0000444 for (i = 0; i < num_hugepd; i++, hpdp++)
445 hpdp->pd = 0;
446
David Gibsonf10a04c2006-04-28 15:02:51 +1000447 tlb->need_flush = 1;
Becky Bruce881fde12011-10-10 10:50:40 +0000448
449#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000450 hugepd_free(tlb, hugepte);
Becky Bruce881fde12011-10-10 10:50:40 +0000451#else
452 pgtable_free_tlb(tlb, hugepte, pdshift - shift);
Becky Bruce41151e72011-06-28 09:54:48 +0000453#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000454}
455
David Gibsonf10a04c2006-04-28 15:02:51 +1000456static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
457 unsigned long addr, unsigned long end,
David Gibsona4fe3ce2009-10-26 19:24:31 +0000458 unsigned long floor, unsigned long ceiling)
David Gibsonf10a04c2006-04-28 15:02:51 +1000459{
460 pmd_t *pmd;
461 unsigned long next;
462 unsigned long start;
463
464 start = addr;
David Gibsonf10a04c2006-04-28 15:02:51 +1000465 do {
Becky Brucea1cd5412011-10-10 10:50:39 +0000466 pmd = pmd_offset(pud, addr);
David Gibsonf10a04c2006-04-28 15:02:51 +1000467 next = pmd_addr_end(addr, end);
468 if (pmd_none(*pmd))
469 continue;
Becky Brucea1cd5412011-10-10 10:50:39 +0000470#ifdef CONFIG_PPC_FSL_BOOK3E
471 /*
472 * Increment next by the size of the huge mapping since
473 * there may be more than one entry at this level for a
474 * single hugepage, but all of them point to
475 * the same kmem cache that holds the hugepte.
476 */
477 next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
478#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000479 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
480 addr, next, floor, ceiling);
Becky Brucea1cd5412011-10-10 10:50:39 +0000481 } while (addr = next, addr != end);
David Gibsonf10a04c2006-04-28 15:02:51 +1000482
483 start &= PUD_MASK;
484 if (start < floor)
485 return;
486 if (ceiling) {
487 ceiling &= PUD_MASK;
488 if (!ceiling)
489 return;
490 }
491 if (end - 1 > ceiling - 1)
492 return;
493
494 pmd = pmd_offset(pud, start);
495 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000496 pmd_free_tlb(tlb, pmd, start);
David Gibsonf10a04c2006-04-28 15:02:51 +1000497}
David Gibsonf10a04c2006-04-28 15:02:51 +1000498
499static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
500 unsigned long addr, unsigned long end,
501 unsigned long floor, unsigned long ceiling)
502{
503 pud_t *pud;
504 unsigned long next;
505 unsigned long start;
506
507 start = addr;
David Gibsonf10a04c2006-04-28 15:02:51 +1000508 do {
Becky Brucea1cd5412011-10-10 10:50:39 +0000509 pud = pud_offset(pgd, addr);
David Gibsonf10a04c2006-04-28 15:02:51 +1000510 next = pud_addr_end(addr, end);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000511 if (!is_hugepd(pud)) {
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100512 if (pud_none_or_clear_bad(pud))
513 continue;
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700514 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
David Gibsona4fe3ce2009-10-26 19:24:31 +0000515 ceiling);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100516 } else {
Becky Brucea1cd5412011-10-10 10:50:39 +0000517#ifdef CONFIG_PPC_FSL_BOOK3E
518 /*
519 * Increment next by the size of the huge mapping since
520 * there may be more than one entry at this level for a
521 * single hugepage, but all of them point to
522 * the same kmem cache that holds the hugepte.
523 */
524 next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
525#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000526 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
527 addr, next, floor, ceiling);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100528 }
Becky Brucea1cd5412011-10-10 10:50:39 +0000529 } while (addr = next, addr != end);
David Gibsonf10a04c2006-04-28 15:02:51 +1000530
531 start &= PGDIR_MASK;
532 if (start < floor)
533 return;
534 if (ceiling) {
535 ceiling &= PGDIR_MASK;
536 if (!ceiling)
537 return;
538 }
539 if (end - 1 > ceiling - 1)
540 return;
541
542 pud = pud_offset(pgd, start);
543 pgd_clear(pgd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000544 pud_free_tlb(tlb, pud, start);
David Gibsonf10a04c2006-04-28 15:02:51 +1000545}
546
547/*
548 * This function frees user-level page tables of a process.
549 *
550 * Must be called with pagetable lock held.
551 */
Jan Beulich42b77722008-07-23 21:27:10 -0700552void hugetlb_free_pgd_range(struct mmu_gather *tlb,
David Gibsonf10a04c2006-04-28 15:02:51 +1000553 unsigned long addr, unsigned long end,
554 unsigned long floor, unsigned long ceiling)
555{
556 pgd_t *pgd;
557 unsigned long next;
David Gibsonf10a04c2006-04-28 15:02:51 +1000558
559 /*
David Gibsona4fe3ce2009-10-26 19:24:31 +0000560 * Because there are a number of different possible pagetable
561 * layouts for hugepage ranges, we limit knowledge of how
562 * things should be laid out to the allocation path
563 * (huge_pte_alloc(), above). Everything else works out the
564 * structure as it goes from information in the hugepd
565 * pointers. That means that we can't here use the
566 * optimization used in the normal page free_pgd_range(), of
567 * checking whether we're actually covering a large enough
568 * range to have to do anything at the top level of the walk
569 * instead of at the bottom.
David Gibsonf10a04c2006-04-28 15:02:51 +1000570 *
David Gibsona4fe3ce2009-10-26 19:24:31 +0000571 * To make sense of this, you should probably go read the big
572 * block comment at the top of the normal free_pgd_range(),
573 * too.
David Gibsonf10a04c2006-04-28 15:02:51 +1000574 */
575
David Gibsonf10a04c2006-04-28 15:02:51 +1000576 do {
David Gibsonf10a04c2006-04-28 15:02:51 +1000577 next = pgd_addr_end(addr, end);
Becky Bruce41151e72011-06-28 09:54:48 +0000578 pgd = pgd_offset(tlb->mm, addr);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000579 if (!is_hugepd(pgd)) {
David Gibson0b264252008-09-05 11:49:54 +1000580 if (pgd_none_or_clear_bad(pgd))
581 continue;
582 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
583 } else {
Becky Bruce881fde12011-10-10 10:50:40 +0000584#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000585 /*
586 * Increment next by the size of the huge mapping since
Becky Bruce881fde12011-10-10 10:50:40 +0000587 * there may be more than one entry at the pgd level
588 * for a single hugepage, but all of them point to the
589 * same kmem cache that holds the hugepte.
Becky Bruce41151e72011-06-28 09:54:48 +0000590 */
591 next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
592#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000593 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
594 addr, next, floor, ceiling);
David Gibson0b264252008-09-05 11:49:54 +1000595 }
Becky Bruce41151e72011-06-28 09:54:48 +0000596 } while (addr = next, addr != end);
David Gibsone28f7fa2005-08-05 19:39:06 +1000597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599struct page *
600follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
601{
602 pte_t *ptep;
603 struct page *page;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000604 unsigned shift;
605 unsigned long mask;
606
607 ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700609 /* Verify it is a huge page else bail. */
David Gibsona4fe3ce2009-10-26 19:24:31 +0000610 if (!ptep || !shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return ERR_PTR(-EINVAL);
612
David Gibsona4fe3ce2009-10-26 19:24:31 +0000613 mask = (1UL << shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 page = pte_page(*ptep);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000615 if (page)
616 page += (address & mask) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 return page;
619}
620
621int pmd_huge(pmd_t pmd)
622{
623 return 0;
624}
625
Andi Kleenceb86872008-07-23 21:27:50 -0700626int pud_huge(pud_t pud)
627{
628 return 0;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631struct page *
632follow_huge_pmd(struct mm_struct *mm, unsigned long address,
633 pmd_t *pmd, int write)
634{
635 BUG();
636 return NULL;
637}
638
David Gibsona4fe3ce2009-10-26 19:24:31 +0000639static noinline int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
640 unsigned long end, int write, struct page **pages, int *nr)
641{
642 unsigned long mask;
643 unsigned long pte_end;
Andrea Arcangeli35267412011-11-02 13:37:15 -0700644 struct page *head, *page, *tail;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000645 pte_t pte;
646 int refs;
647
648 pte_end = (addr + sz) & ~(sz-1);
649 if (pte_end < end)
650 end = pte_end;
651
652 pte = *ptep;
653 mask = _PAGE_PRESENT | _PAGE_USER;
654 if (write)
655 mask |= _PAGE_RW;
656
657 if ((pte_val(pte) & mask) != mask)
658 return 0;
659
660 /* hugepages are never "special" */
661 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
662
663 refs = 0;
664 head = pte_page(pte);
665
666 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
Andrea Arcangeli35267412011-11-02 13:37:15 -0700667 tail = page;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000668 do {
669 VM_BUG_ON(compound_head(page) != head);
670 pages[*nr] = page;
671 (*nr)++;
672 page++;
673 refs++;
674 } while (addr += PAGE_SIZE, addr != end);
675
676 if (!page_cache_add_speculative(head, refs)) {
677 *nr -= refs;
678 return 0;
679 }
680
681 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
682 /* Could be optimized better */
Andrea Arcangeli85964682011-11-02 13:37:11 -0700683 *nr -= refs;
684 while (refs--)
Andrea Arcangeli405e44f2011-11-02 13:37:08 -0700685 put_page(head);
Andrea Arcangelicf592bf2011-11-02 13:37:19 -0700686 return 0;
687 }
688
689 /*
690 * Any tail page need their mapcount reference taken before we
691 * return.
692 */
693 while (refs--) {
694 if (PageTail(tail))
695 get_huge_page_tail(tail);
696 tail++;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000697 }
698
699 return 1;
700}
701
David Gibson39adfa52009-11-23 20:03:40 +0000702static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
703 unsigned long sz)
704{
705 unsigned long __boundary = (addr + sz) & ~(sz-1);
706 return (__boundary - 1 < end - 1) ? __boundary : end;
707}
708
David Gibsona4fe3ce2009-10-26 19:24:31 +0000709int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
710 unsigned long addr, unsigned long end,
711 int write, struct page **pages, int *nr)
712{
713 pte_t *ptep;
714 unsigned long sz = 1UL << hugepd_shift(*hugepd);
David Gibson39adfa52009-11-23 20:03:40 +0000715 unsigned long next;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000716
717 ptep = hugepte_offset(hugepd, addr, pdshift);
718 do {
David Gibson39adfa52009-11-23 20:03:40 +0000719 next = hugepte_addr_end(addr, end, sz);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000720 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
721 return 0;
David Gibson39adfa52009-11-23 20:03:40 +0000722 } while (ptep++, addr = next, addr != end);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000723
724 return 1;
725}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Becky Bruce76512952011-10-10 10:50:36 +0000727#ifdef CONFIG_PPC_MM_SLICES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
729 unsigned long len, unsigned long pgoff,
730 unsigned long flags)
731{
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700732 struct hstate *hstate = hstate_file(file);
733 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
Brian King48f797d2008-12-04 04:07:54 +0000734
Michel Lespinasse34d07172013-04-29 11:53:52 -0700735 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
Becky Bruce76512952011-10-10 10:50:36 +0000737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Mel Gorman33402892009-01-06 14:38:54 -0800739unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
740{
Paul Mackerras25c29f92011-09-20 19:58:10 +0000741#ifdef CONFIG_PPC_MM_SLICES
Mel Gorman33402892009-01-06 14:38:54 -0800742 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
743
744 return 1UL << mmu_psize_to_shift(psize);
Becky Bruce41151e72011-06-28 09:54:48 +0000745#else
746 if (!is_vm_hugetlb_page(vma))
747 return PAGE_SIZE;
748
749 return huge_page_size(hstate_vma(vma));
750#endif
751}
752
753static inline bool is_power_of_4(unsigned long x)
754{
755 if (is_power_of_2(x))
756 return (__ilog2(x) % 2) ? false : true;
757 return false;
Mel Gorman33402892009-01-06 14:38:54 -0800758}
759
David Gibsond1837cb2009-10-26 19:24:31 +0000760static int __init add_huge_page_size(unsigned long long size)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100761{
David Gibsond1837cb2009-10-26 19:24:31 +0000762 int shift = __ffs(size);
763 int mmu_psize;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000764
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100765 /* Check that it is a page size supported by the hardware and
David Gibsond1837cb2009-10-26 19:24:31 +0000766 * that it fits within pagetable and slice limits. */
Becky Bruce41151e72011-06-28 09:54:48 +0000767#ifdef CONFIG_PPC_FSL_BOOK3E
768 if ((size < PAGE_SIZE) || !is_power_of_4(size))
769 return -EINVAL;
770#else
David Gibsond1837cb2009-10-26 19:24:31 +0000771 if (!is_power_of_2(size)
772 || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
773 return -EINVAL;
Becky Bruce41151e72011-06-28 09:54:48 +0000774#endif
Jon Tollefson91224342008-07-23 21:27:55 -0700775
David Gibsond1837cb2009-10-26 19:24:31 +0000776 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
777 return -EINVAL;
778
779#ifdef CONFIG_SPU_FS_64K_LS
780 /* Disable support for 64K huge pages when 64K SPU local store
781 * support is enabled as the current implementation conflicts.
782 */
783 if (shift == PAGE_SHIFT_64K)
784 return -EINVAL;
785#endif /* CONFIG_SPU_FS_64K_LS */
786
787 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
788
789 /* Return if huge page size has already been setup */
790 if (size_to_hstate(size))
791 return 0;
792
793 hugetlb_add_hstate(shift - PAGE_SHIFT);
794
795 return 0;
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100796}
797
798static int __init hugepage_setup_sz(char *str)
799{
800 unsigned long long size;
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100801
802 size = memparse(str, &str);
803
David Gibsond1837cb2009-10-26 19:24:31 +0000804 if (add_huge_page_size(size) != 0)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100805 printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
806
807 return 1;
808}
809__setup("hugepagesz=", hugepage_setup_sz);
810
Becky Bruce881fde12011-10-10 10:50:40 +0000811#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000812struct kmem_cache *hugepte_cache;
813static int __init hugetlbpage_init(void)
814{
815 int psize;
816
817 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
818 unsigned shift;
819
820 if (!mmu_psize_defs[psize].shift)
821 continue;
822
823 shift = mmu_psize_to_shift(psize);
824
825 /* Don't treat normal page sizes as huge... */
826 if (shift != PAGE_SHIFT)
827 if (add_huge_page_size(1ULL << shift) < 0)
828 continue;
829 }
830
831 /*
832 * Create a kmem cache for hugeptes. The bottom bits in the pte have
833 * size information encoded in them, so align them to allow this
834 */
835 hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
836 HUGEPD_SHIFT_MASK + 1, 0, NULL);
837 if (hugepte_cache == NULL)
838 panic("%s: Unable to create kmem cache for hugeptes\n",
839 __func__);
840
841 /* Default hpage size = 4M */
842 if (mmu_psize_defs[MMU_PAGE_4M].shift)
843 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
844 else
845 panic("%s: Unable to set default huge page size\n", __func__);
846
847
848 return 0;
849}
850#else
David Gibsonf10a04c2006-04-28 15:02:51 +1000851static int __init hugetlbpage_init(void)
852{
David Gibsona4fe3ce2009-10-26 19:24:31 +0000853 int psize;
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700854
Matt Evans44ae3ab2011-04-06 19:48:50 +0000855 if (!mmu_has_feature(MMU_FTR_16M_PAGE))
David Gibsonf10a04c2006-04-28 15:02:51 +1000856 return -ENODEV;
Benjamin Herrenschmidt00df4382008-07-28 16:13:18 +1000857
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700858 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
David Gibsond1837cb2009-10-26 19:24:31 +0000859 unsigned shift;
860 unsigned pdshift;
861
862 if (!mmu_psize_defs[psize].shift)
863 continue;
864
865 shift = mmu_psize_to_shift(psize);
866
867 if (add_huge_page_size(1ULL << shift) < 0)
868 continue;
869
870 if (shift < PMD_SHIFT)
871 pdshift = PMD_SHIFT;
872 else if (shift < PUD_SHIFT)
873 pdshift = PUD_SHIFT;
874 else
875 pdshift = PGDIR_SHIFT;
876
877 pgtable_cache_add(pdshift - shift, NULL);
878 if (!PGT_CACHE(pdshift - shift))
879 panic("hugetlbpage_init(): could not create "
880 "pgtable cache for %d bit pagesize\n", shift);
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700881 }
David Gibsonf10a04c2006-04-28 15:02:51 +1000882
David Gibsond1837cb2009-10-26 19:24:31 +0000883 /* Set default large page size. Currently, we pick 16M or 1M
884 * depending on what is available
885 */
886 if (mmu_psize_defs[MMU_PAGE_16M].shift)
887 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
888 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
889 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
890
David Gibsonf10a04c2006-04-28 15:02:51 +1000891 return 0;
892}
Becky Bruce41151e72011-06-28 09:54:48 +0000893#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000894module_init(hugetlbpage_init);
David Gibson0895ecd2009-10-26 19:24:31 +0000895
896void flush_dcache_icache_hugepage(struct page *page)
897{
898 int i;
Becky Bruce41151e72011-06-28 09:54:48 +0000899 void *start;
David Gibson0895ecd2009-10-26 19:24:31 +0000900
901 BUG_ON(!PageCompound(page));
902
Becky Bruce41151e72011-06-28 09:54:48 +0000903 for (i = 0; i < (1UL << compound_order(page)); i++) {
904 if (!PageHighMem(page)) {
905 __flush_dcache_icache(page_address(page+i));
906 } else {
Cong Wang2480b202011-11-25 23:14:16 +0800907 start = kmap_atomic(page+i);
Becky Bruce41151e72011-06-28 09:54:48 +0000908 __flush_dcache_icache(start);
Cong Wang2480b202011-11-25 23:14:16 +0800909 kunmap_atomic(start);
Becky Bruce41151e72011-06-28 09:54:48 +0000910 }
911 }
David Gibson0895ecd2009-10-26 19:24:31 +0000912}