blob: f348c3d904046eaefd74b6cbdb2955535b602c0b [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
Jon Tollefson0d9ea752008-07-23 21:27:56 -070051static inline int shift_to_mmu_psize(unsigned int shift)
52{
David Gibsond1837cb2009-10-26 19:24:31 +000053 int psize;
54
55 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
56 if (mmu_psize_defs[psize].shift == shift)
57 return psize;
Jon Tollefson0d9ea752008-07-23 21:27:56 -070058 return -1;
59}
60
61static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
62{
63 if (mmu_psize_defs[mmu_psize].shift)
64 return mmu_psize_defs[mmu_psize].shift;
65 BUG();
66}
67
David Gibsona4fe3ce2009-10-26 19:24:31 +000068#define hugepd_none(hpd) ((hpd).pd == 0)
69
David Gibsona4fe3ce2009-10-26 19:24:31 +000070pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, unsigned *shift)
David Gibsonf10a04c2006-04-28 15:02:51 +100071{
David Gibsona4fe3ce2009-10-26 19:24:31 +000072 pgd_t *pg;
73 pud_t *pu;
74 pmd_t *pm;
75 hugepd_t *hpdp = NULL;
76 unsigned pdshift = PGDIR_SHIFT;
77
78 if (shift)
79 *shift = 0;
80
81 pg = pgdir + pgd_index(ea);
82 if (is_hugepd(pg)) {
83 hpdp = (hugepd_t *)pg;
84 } else if (!pgd_none(*pg)) {
85 pdshift = PUD_SHIFT;
86 pu = pud_offset(pg, ea);
87 if (is_hugepd(pu))
88 hpdp = (hugepd_t *)pu;
89 else if (!pud_none(*pu)) {
90 pdshift = PMD_SHIFT;
91 pm = pmd_offset(pu, ea);
92 if (is_hugepd(pm))
93 hpdp = (hugepd_t *)pm;
94 else if (!pmd_none(*pm)) {
Becky Bruce41151e72011-06-28 09:54:48 +000095 return pte_offset_kernel(pm, ea);
David Gibsona4fe3ce2009-10-26 19:24:31 +000096 }
97 }
98 }
99
100 if (!hpdp)
101 return NULL;
102
103 if (shift)
104 *shift = hugepd_shift(*hpdp);
105 return hugepte_offset(hpdp, ea, pdshift);
106}
Paul Mackerras342d3db2011-12-12 12:38:05 +0000107EXPORT_SYMBOL_GPL(find_linux_pte_or_hugepte);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000108
109pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
110{
111 return find_linux_pte_or_hugepte(mm->pgd, addr, NULL);
112}
113
114static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
115 unsigned long address, unsigned pdshift, unsigned pshift)
116{
Becky Bruce41151e72011-06-28 09:54:48 +0000117 struct kmem_cache *cachep;
118 pte_t *new;
119
Becky Bruce881fde12011-10-10 10:50:40 +0000120#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000121 int i;
122 int num_hugepd = 1 << (pshift - pdshift);
123 cachep = hugepte_cache;
Becky Bruce881fde12011-10-10 10:50:40 +0000124#else
125 cachep = PGT_CACHE(pdshift - pshift);
Becky Bruce41151e72011-06-28 09:54:48 +0000126#endif
127
128 new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT);
David Gibsonf10a04c2006-04-28 15:02:51 +1000129
David Gibsona4fe3ce2009-10-26 19:24:31 +0000130 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
131 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
132
David Gibsonf10a04c2006-04-28 15:02:51 +1000133 if (! new)
134 return -ENOMEM;
135
136 spin_lock(&mm->page_table_lock);
Becky Bruce881fde12011-10-10 10:50:40 +0000137#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000138 /*
139 * We have multiple higher-level entries that point to the same
140 * actual pte location. Fill in each as we go and backtrack on error.
141 * We need all of these so the DTLB pgtable walk code can find the
142 * right higher-level entry without knowing if it's a hugepage or not.
143 */
144 for (i = 0; i < num_hugepd; i++, hpdp++) {
145 if (unlikely(!hugepd_none(*hpdp)))
146 break;
147 else
148 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
149 }
150 /* If we bailed from the for loop early, an error occurred, clean up */
151 if (i < num_hugepd) {
152 for (i = i - 1 ; i >= 0; i--, hpdp--)
153 hpdp->pd = 0;
154 kmem_cache_free(cachep, new);
155 }
Becky Brucea1cd5412011-10-10 10:50:39 +0000156#else
157 if (!hugepd_none(*hpdp))
158 kmem_cache_free(cachep, new);
159 else
160 hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift;
Becky Bruce41151e72011-06-28 09:54:48 +0000161#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000162 spin_unlock(&mm->page_table_lock);
163 return 0;
164}
165
Becky Brucea1cd5412011-10-10 10:50:39 +0000166/*
167 * These macros define how to determine which level of the page table holds
168 * the hpdp.
169 */
170#ifdef CONFIG_PPC_FSL_BOOK3E
171#define HUGEPD_PGD_SHIFT PGDIR_SHIFT
172#define HUGEPD_PUD_SHIFT PUD_SHIFT
173#else
174#define HUGEPD_PGD_SHIFT PUD_SHIFT
175#define HUGEPD_PUD_SHIFT PMD_SHIFT
176#endif
177
David Gibsona4fe3ce2009-10-26 19:24:31 +0000178pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
179{
180 pgd_t *pg;
181 pud_t *pu;
182 pmd_t *pm;
183 hugepd_t *hpdp = NULL;
184 unsigned pshift = __ffs(sz);
185 unsigned pdshift = PGDIR_SHIFT;
David Gibson0b264252008-09-05 11:49:54 +1000186
David Gibsona4fe3ce2009-10-26 19:24:31 +0000187 addr &= ~(sz-1);
188
189 pg = pgd_offset(mm, addr);
Becky Brucea1cd5412011-10-10 10:50:39 +0000190
191 if (pshift >= HUGEPD_PGD_SHIFT) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000192 hpdp = (hugepd_t *)pg;
193 } else {
194 pdshift = PUD_SHIFT;
195 pu = pud_alloc(mm, pg, addr);
Becky Brucea1cd5412011-10-10 10:50:39 +0000196 if (pshift >= HUGEPD_PUD_SHIFT) {
David Gibsona4fe3ce2009-10-26 19:24:31 +0000197 hpdp = (hugepd_t *)pu;
198 } else {
199 pdshift = PMD_SHIFT;
200 pm = pmd_alloc(mm, pu, addr);
201 hpdp = (hugepd_t *)pm;
202 }
203 }
204
205 if (!hpdp)
206 return NULL;
207
208 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
209
210 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
211 return NULL;
212
213 return hugepte_offset(hpdp, addr, pdshift);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100214}
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100215
Becky Bruce881fde12011-10-10 10:50:40 +0000216#ifdef CONFIG_PPC_FSL_BOOK3E
Jon Tollefson658013e2008-07-23 21:27:54 -0700217/* Build list of addresses of gigantic pages. This function is used in early
218 * boot before the buddy or bootmem allocator is setup.
219 */
Becky Bruce41151e72011-06-28 09:54:48 +0000220void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
221{
222 unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
223 int i;
224
225 if (addr == 0)
226 return;
227
228 gpage_freearray[idx].nr_gpages = number_of_pages;
229
230 for (i = 0; i < number_of_pages; i++) {
231 gpage_freearray[idx].gpage_list[i] = addr;
232 addr += page_size;
233 }
234}
235
236/*
237 * Moves the gigantic page addresses from the temporary list to the
238 * huge_boot_pages list.
239 */
240int alloc_bootmem_huge_page(struct hstate *hstate)
241{
242 struct huge_bootmem_page *m;
243 int idx = shift_to_mmu_psize(hstate->order + PAGE_SHIFT);
244 int nr_gpages = gpage_freearray[idx].nr_gpages;
245
246 if (nr_gpages == 0)
247 return 0;
248
249#ifdef CONFIG_HIGHMEM
250 /*
251 * If gpages can be in highmem we can't use the trick of storing the
252 * data structure in the page; allocate space for this
253 */
254 m = alloc_bootmem(sizeof(struct huge_bootmem_page));
255 m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
256#else
257 m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
258#endif
259
260 list_add(&m->list, &huge_boot_pages);
261 gpage_freearray[idx].nr_gpages = nr_gpages;
262 gpage_freearray[idx].gpage_list[nr_gpages] = 0;
263 m->hstate = hstate;
264
265 return 1;
266}
267/*
268 * Scan the command line hugepagesz= options for gigantic pages; store those in
269 * a list that we use to allocate the memory once all options are parsed.
270 */
271
272unsigned long gpage_npages[MMU_PAGE_COUNT];
273
274static int __init do_gpage_early_setup(char *param, char *val)
275{
276 static phys_addr_t size;
277 unsigned long npages;
278
279 /*
280 * The hugepagesz and hugepages cmdline options are interleaved. We
281 * use the size variable to keep track of whether or not this was done
282 * properly and skip over instances where it is incorrect. Other
283 * command-line parsing code will issue warnings, so we don't need to.
284 *
285 */
286 if ((strcmp(param, "default_hugepagesz") == 0) ||
287 (strcmp(param, "hugepagesz") == 0)) {
288 size = memparse(val, NULL);
289 } else if (strcmp(param, "hugepages") == 0) {
290 if (size != 0) {
291 if (sscanf(val, "%lu", &npages) <= 0)
292 npages = 0;
293 gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
294 size = 0;
295 }
296 }
297 return 0;
298}
299
300
301/*
302 * This function allocates physical space for pages that are larger than the
303 * buddy allocator can handle. We want to allocate these in highmem because
304 * the amount of lowmem is limited. This means that this function MUST be
305 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
306 * allocate to grab highmem.
307 */
308void __init reserve_hugetlb_gpages(void)
309{
310 static __initdata char cmdline[COMMAND_LINE_SIZE];
311 phys_addr_t size, base;
312 int i;
313
314 strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
315 parse_args("hugetlb gpages", cmdline, NULL, 0, &do_gpage_early_setup);
316
317 /*
318 * Walk gpage list in reverse, allocating larger page sizes first.
319 * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
320 * When we reach the point in the list where pages are no longer
321 * considered gpages, we're done.
322 */
323 for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
324 if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
325 continue;
326 else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
327 break;
328
329 size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
330 base = memblock_alloc_base(size * gpage_npages[i], size,
331 MEMBLOCK_ALLOC_ANYWHERE);
332 add_gpage(base, size, gpage_npages[i]);
333 }
334}
335
Becky Bruce881fde12011-10-10 10:50:40 +0000336#else /* !PPC_FSL_BOOK3E */
Becky Bruce41151e72011-06-28 09:54:48 +0000337
338/* Build list of addresses of gigantic pages. This function is used in early
339 * boot before the buddy or bootmem allocator is setup.
340 */
341void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
Jon Tollefson658013e2008-07-23 21:27:54 -0700342{
343 if (!addr)
344 return;
345 while (number_of_pages > 0) {
346 gpage_freearray[nr_gpages] = addr;
347 nr_gpages++;
348 number_of_pages--;
349 addr += page_size;
350 }
351}
352
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700353/* Moves the gigantic page addresses from the temporary list to the
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700354 * huge_boot_pages list.
355 */
356int alloc_bootmem_huge_page(struct hstate *hstate)
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700357{
358 struct huge_bootmem_page *m;
359 if (nr_gpages == 0)
360 return 0;
361 m = phys_to_virt(gpage_freearray[--nr_gpages]);
362 gpage_freearray[nr_gpages] = 0;
363 list_add(&m->list, &huge_boot_pages);
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700364 m->hstate = hstate;
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700365 return 1;
366}
Becky Bruce41151e72011-06-28 09:54:48 +0000367#endif
Jon Tollefsonec4b2c02008-07-23 21:27:53 -0700368
Chen, Kenneth W39dde652006-12-06 20:32:03 -0800369int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
370{
371 return 0;
372}
373
Becky Bruce881fde12011-10-10 10:50:40 +0000374#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000375#define HUGEPD_FREELIST_SIZE \
376 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
377
378struct hugepd_freelist {
379 struct rcu_head rcu;
380 unsigned int index;
381 void *ptes[0];
382};
383
384static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
385
386static void hugepd_free_rcu_callback(struct rcu_head *head)
387{
388 struct hugepd_freelist *batch =
389 container_of(head, struct hugepd_freelist, rcu);
390 unsigned int i;
391
392 for (i = 0; i < batch->index; i++)
393 kmem_cache_free(hugepte_cache, batch->ptes[i]);
394
395 free_page((unsigned long)batch);
396}
397
398static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
399{
400 struct hugepd_freelist **batchp;
401
402 batchp = &__get_cpu_var(hugepd_freelist_cur);
403
404 if (atomic_read(&tlb->mm->mm_users) < 2 ||
405 cpumask_equal(mm_cpumask(tlb->mm),
406 cpumask_of(smp_processor_id()))) {
407 kmem_cache_free(hugepte_cache, hugepte);
408 return;
409 }
410
411 if (*batchp == NULL) {
412 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
413 (*batchp)->index = 0;
414 }
415
416 (*batchp)->ptes[(*batchp)->index++] = hugepte;
417 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
418 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
419 *batchp = NULL;
420 }
421}
422#endif
423
David Gibsona4fe3ce2009-10-26 19:24:31 +0000424static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
425 unsigned long start, unsigned long end,
426 unsigned long floor, unsigned long ceiling)
David Gibsonf10a04c2006-04-28 15:02:51 +1000427{
428 pte_t *hugepte = hugepd_page(*hpdp);
Becky Bruce41151e72011-06-28 09:54:48 +0000429 int i;
430
David Gibsona4fe3ce2009-10-26 19:24:31 +0000431 unsigned long pdmask = ~((1UL << pdshift) - 1);
Becky Bruce41151e72011-06-28 09:54:48 +0000432 unsigned int num_hugepd = 1;
433
Becky Bruce881fde12011-10-10 10:50:40 +0000434#ifdef CONFIG_PPC_FSL_BOOK3E
435 /* Note: On fsl the hpdp may be the first of several */
Becky Bruce41151e72011-06-28 09:54:48 +0000436 num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift));
Becky Bruce881fde12011-10-10 10:50:40 +0000437#else
438 unsigned int shift = hugepd_shift(*hpdp);
Becky Bruce41151e72011-06-28 09:54:48 +0000439#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000440
441 start &= pdmask;
442 if (start < floor)
443 return;
444 if (ceiling) {
445 ceiling &= pdmask;
446 if (! ceiling)
447 return;
448 }
449 if (end - 1 > ceiling - 1)
450 return;
David Gibsonf10a04c2006-04-28 15:02:51 +1000451
Becky Bruce41151e72011-06-28 09:54:48 +0000452 for (i = 0; i < num_hugepd; i++, hpdp++)
453 hpdp->pd = 0;
454
David Gibsonf10a04c2006-04-28 15:02:51 +1000455 tlb->need_flush = 1;
Becky Bruce881fde12011-10-10 10:50:40 +0000456
457#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000458 hugepd_free(tlb, hugepte);
Becky Bruce881fde12011-10-10 10:50:40 +0000459#else
460 pgtable_free_tlb(tlb, hugepte, pdshift - shift);
Becky Bruce41151e72011-06-28 09:54:48 +0000461#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000462}
463
David Gibsonf10a04c2006-04-28 15:02:51 +1000464static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
465 unsigned long addr, unsigned long end,
David Gibsona4fe3ce2009-10-26 19:24:31 +0000466 unsigned long floor, unsigned long ceiling)
David Gibsonf10a04c2006-04-28 15:02:51 +1000467{
468 pmd_t *pmd;
469 unsigned long next;
470 unsigned long start;
471
472 start = addr;
David Gibsonf10a04c2006-04-28 15:02:51 +1000473 do {
Becky Brucea1cd5412011-10-10 10:50:39 +0000474 pmd = pmd_offset(pud, addr);
David Gibsonf10a04c2006-04-28 15:02:51 +1000475 next = pmd_addr_end(addr, end);
476 if (pmd_none(*pmd))
477 continue;
Becky Brucea1cd5412011-10-10 10:50:39 +0000478#ifdef CONFIG_PPC_FSL_BOOK3E
479 /*
480 * Increment next by the size of the huge mapping since
481 * there may be more than one entry at this level for a
482 * single hugepage, but all of them point to
483 * the same kmem cache that holds the hugepte.
484 */
485 next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
486#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000487 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
488 addr, next, floor, ceiling);
Becky Brucea1cd5412011-10-10 10:50:39 +0000489 } while (addr = next, addr != end);
David Gibsonf10a04c2006-04-28 15:02:51 +1000490
491 start &= PUD_MASK;
492 if (start < floor)
493 return;
494 if (ceiling) {
495 ceiling &= PUD_MASK;
496 if (!ceiling)
497 return;
498 }
499 if (end - 1 > ceiling - 1)
500 return;
501
502 pmd = pmd_offset(pud, start);
503 pud_clear(pud);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000504 pmd_free_tlb(tlb, pmd, start);
David Gibsonf10a04c2006-04-28 15:02:51 +1000505}
David Gibsonf10a04c2006-04-28 15:02:51 +1000506
507static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
508 unsigned long addr, unsigned long end,
509 unsigned long floor, unsigned long ceiling)
510{
511 pud_t *pud;
512 unsigned long next;
513 unsigned long start;
514
515 start = addr;
David Gibsonf10a04c2006-04-28 15:02:51 +1000516 do {
Becky Brucea1cd5412011-10-10 10:50:39 +0000517 pud = pud_offset(pgd, addr);
David Gibsonf10a04c2006-04-28 15:02:51 +1000518 next = pud_addr_end(addr, end);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000519 if (!is_hugepd(pud)) {
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100520 if (pud_none_or_clear_bad(pud))
521 continue;
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700522 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
David Gibsona4fe3ce2009-10-26 19:24:31 +0000523 ceiling);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100524 } else {
Becky Brucea1cd5412011-10-10 10:50:39 +0000525#ifdef CONFIG_PPC_FSL_BOOK3E
526 /*
527 * Increment next by the size of the huge mapping since
528 * there may be more than one entry at this level for a
529 * single hugepage, but all of them point to
530 * the same kmem cache that holds the hugepte.
531 */
532 next = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
533#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000534 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
535 addr, next, floor, ceiling);
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100536 }
Becky Brucea1cd5412011-10-10 10:50:39 +0000537 } while (addr = next, addr != end);
David Gibsonf10a04c2006-04-28 15:02:51 +1000538
539 start &= PGDIR_MASK;
540 if (start < floor)
541 return;
542 if (ceiling) {
543 ceiling &= PGDIR_MASK;
544 if (!ceiling)
545 return;
546 }
547 if (end - 1 > ceiling - 1)
548 return;
549
550 pud = pud_offset(pgd, start);
551 pgd_clear(pgd);
Benjamin Herrenschmidt9e1b32c2009-07-22 15:44:28 +1000552 pud_free_tlb(tlb, pud, start);
David Gibsonf10a04c2006-04-28 15:02:51 +1000553}
554
555/*
556 * This function frees user-level page tables of a process.
557 *
558 * Must be called with pagetable lock held.
559 */
Jan Beulich42b77722008-07-23 21:27:10 -0700560void hugetlb_free_pgd_range(struct mmu_gather *tlb,
David Gibsonf10a04c2006-04-28 15:02:51 +1000561 unsigned long addr, unsigned long end,
562 unsigned long floor, unsigned long ceiling)
563{
564 pgd_t *pgd;
565 unsigned long next;
David Gibsonf10a04c2006-04-28 15:02:51 +1000566
567 /*
David Gibsona4fe3ce2009-10-26 19:24:31 +0000568 * Because there are a number of different possible pagetable
569 * layouts for hugepage ranges, we limit knowledge of how
570 * things should be laid out to the allocation path
571 * (huge_pte_alloc(), above). Everything else works out the
572 * structure as it goes from information in the hugepd
573 * pointers. That means that we can't here use the
574 * optimization used in the normal page free_pgd_range(), of
575 * checking whether we're actually covering a large enough
576 * range to have to do anything at the top level of the walk
577 * instead of at the bottom.
David Gibsonf10a04c2006-04-28 15:02:51 +1000578 *
David Gibsona4fe3ce2009-10-26 19:24:31 +0000579 * To make sense of this, you should probably go read the big
580 * block comment at the top of the normal free_pgd_range(),
581 * too.
David Gibsonf10a04c2006-04-28 15:02:51 +1000582 */
583
David Gibsonf10a04c2006-04-28 15:02:51 +1000584 do {
David Gibsonf10a04c2006-04-28 15:02:51 +1000585 next = pgd_addr_end(addr, end);
Becky Bruce41151e72011-06-28 09:54:48 +0000586 pgd = pgd_offset(tlb->mm, addr);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000587 if (!is_hugepd(pgd)) {
David Gibson0b264252008-09-05 11:49:54 +1000588 if (pgd_none_or_clear_bad(pgd))
589 continue;
590 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
591 } else {
Becky Bruce881fde12011-10-10 10:50:40 +0000592#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000593 /*
594 * Increment next by the size of the huge mapping since
Becky Bruce881fde12011-10-10 10:50:40 +0000595 * there may be more than one entry at the pgd level
596 * for a single hugepage, but all of them point to the
597 * same kmem cache that holds the hugepte.
Becky Bruce41151e72011-06-28 09:54:48 +0000598 */
599 next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
600#endif
David Gibsona4fe3ce2009-10-26 19:24:31 +0000601 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
602 addr, next, floor, ceiling);
David Gibson0b264252008-09-05 11:49:54 +1000603 }
Becky Bruce41151e72011-06-28 09:54:48 +0000604 } while (addr = next, addr != end);
David Gibsone28f7fa2005-08-05 19:39:06 +1000605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607struct page *
608follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
609{
610 pte_t *ptep;
611 struct page *page;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000612 unsigned shift;
613 unsigned long mask;
614
615 ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700617 /* Verify it is a huge page else bail. */
David Gibsona4fe3ce2009-10-26 19:24:31 +0000618 if (!ptep || !shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return ERR_PTR(-EINVAL);
620
David Gibsona4fe3ce2009-10-26 19:24:31 +0000621 mask = (1UL << shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 page = pte_page(*ptep);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000623 if (page)
624 page += (address & mask) / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return page;
627}
628
629int pmd_huge(pmd_t pmd)
630{
631 return 0;
632}
633
Andi Kleenceb86872008-07-23 21:27:50 -0700634int pud_huge(pud_t pud)
635{
636 return 0;
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639struct page *
640follow_huge_pmd(struct mm_struct *mm, unsigned long address,
641 pmd_t *pmd, int write)
642{
643 BUG();
644 return NULL;
645}
646
David Gibsona4fe3ce2009-10-26 19:24:31 +0000647static noinline int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
648 unsigned long end, int write, struct page **pages, int *nr)
649{
650 unsigned long mask;
651 unsigned long pte_end;
Andrea Arcangeli35267412011-11-02 13:37:15 -0700652 struct page *head, *page, *tail;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000653 pte_t pte;
654 int refs;
655
656 pte_end = (addr + sz) & ~(sz-1);
657 if (pte_end < end)
658 end = pte_end;
659
660 pte = *ptep;
661 mask = _PAGE_PRESENT | _PAGE_USER;
662 if (write)
663 mask |= _PAGE_RW;
664
665 if ((pte_val(pte) & mask) != mask)
666 return 0;
667
668 /* hugepages are never "special" */
669 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
670
671 refs = 0;
672 head = pte_page(pte);
673
674 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
Andrea Arcangeli35267412011-11-02 13:37:15 -0700675 tail = page;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000676 do {
677 VM_BUG_ON(compound_head(page) != head);
678 pages[*nr] = page;
679 (*nr)++;
680 page++;
681 refs++;
682 } while (addr += PAGE_SIZE, addr != end);
683
684 if (!page_cache_add_speculative(head, refs)) {
685 *nr -= refs;
686 return 0;
687 }
688
689 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
690 /* Could be optimized better */
Andrea Arcangeli85964682011-11-02 13:37:11 -0700691 *nr -= refs;
692 while (refs--)
Andrea Arcangeli405e44f2011-11-02 13:37:08 -0700693 put_page(head);
Andrea Arcangelicf592bf2011-11-02 13:37:19 -0700694 return 0;
695 }
696
697 /*
698 * Any tail page need their mapcount reference taken before we
699 * return.
700 */
701 while (refs--) {
702 if (PageTail(tail))
703 get_huge_page_tail(tail);
704 tail++;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000705 }
706
707 return 1;
708}
709
David Gibson39adfa52009-11-23 20:03:40 +0000710static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
711 unsigned long sz)
712{
713 unsigned long __boundary = (addr + sz) & ~(sz-1);
714 return (__boundary - 1 < end - 1) ? __boundary : end;
715}
716
David Gibsona4fe3ce2009-10-26 19:24:31 +0000717int gup_hugepd(hugepd_t *hugepd, unsigned pdshift,
718 unsigned long addr, unsigned long end,
719 int write, struct page **pages, int *nr)
720{
721 pte_t *ptep;
722 unsigned long sz = 1UL << hugepd_shift(*hugepd);
David Gibson39adfa52009-11-23 20:03:40 +0000723 unsigned long next;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000724
725 ptep = hugepte_offset(hugepd, addr, pdshift);
726 do {
David Gibson39adfa52009-11-23 20:03:40 +0000727 next = hugepte_addr_end(addr, end, sz);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000728 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
729 return 0;
David Gibson39adfa52009-11-23 20:03:40 +0000730 } while (ptep++, addr = next, addr != end);
David Gibsona4fe3ce2009-10-26 19:24:31 +0000731
732 return 1;
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Becky Bruce76512952011-10-10 10:50:36 +0000735#ifdef CONFIG_PPC_MM_SLICES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
737 unsigned long len, unsigned long pgoff,
738 unsigned long flags)
739{
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700740 struct hstate *hstate = hstate_file(file);
741 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
Brian King48f797d2008-12-04 04:07:54 +0000742
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700743 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
Becky Bruce76512952011-10-10 10:50:36 +0000745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Mel Gorman33402892009-01-06 14:38:54 -0800747unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
748{
Paul Mackerras25c29f92011-09-20 19:58:10 +0000749#ifdef CONFIG_PPC_MM_SLICES
Mel Gorman33402892009-01-06 14:38:54 -0800750 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
751
752 return 1UL << mmu_psize_to_shift(psize);
Becky Bruce41151e72011-06-28 09:54:48 +0000753#else
754 if (!is_vm_hugetlb_page(vma))
755 return PAGE_SIZE;
756
757 return huge_page_size(hstate_vma(vma));
758#endif
759}
760
761static inline bool is_power_of_4(unsigned long x)
762{
763 if (is_power_of_2(x))
764 return (__ilog2(x) % 2) ? false : true;
765 return false;
Mel Gorman33402892009-01-06 14:38:54 -0800766}
767
David Gibsond1837cb2009-10-26 19:24:31 +0000768static int __init add_huge_page_size(unsigned long long size)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100769{
David Gibsond1837cb2009-10-26 19:24:31 +0000770 int shift = __ffs(size);
771 int mmu_psize;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000772
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100773 /* Check that it is a page size supported by the hardware and
David Gibsond1837cb2009-10-26 19:24:31 +0000774 * that it fits within pagetable and slice limits. */
Becky Bruce41151e72011-06-28 09:54:48 +0000775#ifdef CONFIG_PPC_FSL_BOOK3E
776 if ((size < PAGE_SIZE) || !is_power_of_4(size))
777 return -EINVAL;
778#else
David Gibsond1837cb2009-10-26 19:24:31 +0000779 if (!is_power_of_2(size)
780 || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT))
781 return -EINVAL;
Becky Bruce41151e72011-06-28 09:54:48 +0000782#endif
Jon Tollefson91224342008-07-23 21:27:55 -0700783
David Gibsond1837cb2009-10-26 19:24:31 +0000784 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
785 return -EINVAL;
786
787#ifdef CONFIG_SPU_FS_64K_LS
788 /* Disable support for 64K huge pages when 64K SPU local store
789 * support is enabled as the current implementation conflicts.
790 */
791 if (shift == PAGE_SHIFT_64K)
792 return -EINVAL;
793#endif /* CONFIG_SPU_FS_64K_LS */
794
795 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
796
797 /* Return if huge page size has already been setup */
798 if (size_to_hstate(size))
799 return 0;
800
801 hugetlb_add_hstate(shift - PAGE_SHIFT);
802
803 return 0;
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100804}
805
806static int __init hugepage_setup_sz(char *str)
807{
808 unsigned long long size;
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100809
810 size = memparse(str, &str);
811
David Gibsond1837cb2009-10-26 19:24:31 +0000812 if (add_huge_page_size(size) != 0)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100813 printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
814
815 return 1;
816}
817__setup("hugepagesz=", hugepage_setup_sz);
818
Becky Bruce881fde12011-10-10 10:50:40 +0000819#ifdef CONFIG_PPC_FSL_BOOK3E
Becky Bruce41151e72011-06-28 09:54:48 +0000820struct kmem_cache *hugepte_cache;
821static int __init hugetlbpage_init(void)
822{
823 int psize;
824
825 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
826 unsigned shift;
827
828 if (!mmu_psize_defs[psize].shift)
829 continue;
830
831 shift = mmu_psize_to_shift(psize);
832
833 /* Don't treat normal page sizes as huge... */
834 if (shift != PAGE_SHIFT)
835 if (add_huge_page_size(1ULL << shift) < 0)
836 continue;
837 }
838
839 /*
840 * Create a kmem cache for hugeptes. The bottom bits in the pte have
841 * size information encoded in them, so align them to allow this
842 */
843 hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t),
844 HUGEPD_SHIFT_MASK + 1, 0, NULL);
845 if (hugepte_cache == NULL)
846 panic("%s: Unable to create kmem cache for hugeptes\n",
847 __func__);
848
849 /* Default hpage size = 4M */
850 if (mmu_psize_defs[MMU_PAGE_4M].shift)
851 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
852 else
853 panic("%s: Unable to set default huge page size\n", __func__);
854
855
856 return 0;
857}
858#else
David Gibsonf10a04c2006-04-28 15:02:51 +1000859static int __init hugetlbpage_init(void)
860{
David Gibsona4fe3ce2009-10-26 19:24:31 +0000861 int psize;
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700862
Matt Evans44ae3ab2011-04-06 19:48:50 +0000863 if (!mmu_has_feature(MMU_FTR_16M_PAGE))
David Gibsonf10a04c2006-04-28 15:02:51 +1000864 return -ENODEV;
Benjamin Herrenschmidt00df4382008-07-28 16:13:18 +1000865
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700866 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
David Gibsond1837cb2009-10-26 19:24:31 +0000867 unsigned shift;
868 unsigned pdshift;
869
870 if (!mmu_psize_defs[psize].shift)
871 continue;
872
873 shift = mmu_psize_to_shift(psize);
874
875 if (add_huge_page_size(1ULL << shift) < 0)
876 continue;
877
878 if (shift < PMD_SHIFT)
879 pdshift = PMD_SHIFT;
880 else if (shift < PUD_SHIFT)
881 pdshift = PUD_SHIFT;
882 else
883 pdshift = PGDIR_SHIFT;
884
885 pgtable_cache_add(pdshift - shift, NULL);
886 if (!PGT_CACHE(pdshift - shift))
887 panic("hugetlbpage_init(): could not create "
888 "pgtable cache for %d bit pagesize\n", shift);
Jon Tollefson0d9ea752008-07-23 21:27:56 -0700889 }
David Gibsonf10a04c2006-04-28 15:02:51 +1000890
David Gibsond1837cb2009-10-26 19:24:31 +0000891 /* Set default large page size. Currently, we pick 16M or 1M
892 * depending on what is available
893 */
894 if (mmu_psize_defs[MMU_PAGE_16M].shift)
895 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
896 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
897 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
898
David Gibsonf10a04c2006-04-28 15:02:51 +1000899 return 0;
900}
Becky Bruce41151e72011-06-28 09:54:48 +0000901#endif
David Gibsonf10a04c2006-04-28 15:02:51 +1000902module_init(hugetlbpage_init);
David Gibson0895ecd2009-10-26 19:24:31 +0000903
904void flush_dcache_icache_hugepage(struct page *page)
905{
906 int i;
Becky Bruce41151e72011-06-28 09:54:48 +0000907 void *start;
David Gibson0895ecd2009-10-26 19:24:31 +0000908
909 BUG_ON(!PageCompound(page));
910
Becky Bruce41151e72011-06-28 09:54:48 +0000911 for (i = 0; i < (1UL << compound_order(page)); i++) {
912 if (!PageHighMem(page)) {
913 __flush_dcache_icache(page_address(page+i));
914 } else {
915 start = kmap_atomic(page+i, KM_PPC_SYNC_ICACHE);
916 __flush_dcache_icache(start);
917 kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
918 }
919 }
David Gibson0895ecd2009-10-26 19:24:31 +0000920}