blob: 424a8f57e15534d74d17823c84715a1161fad475 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PPC64 (POWER4) Huge TLB Page Support for Kernel.
3 *
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
5 *
6 * Based on the IA-32 version:
7 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
8 */
9
10#include <linux/init.h>
11#include <linux/fs.h>
12#include <linux/mm.h>
13#include <linux/hugetlb.h>
14#include <linux/pagemap.h>
15#include <linux/smp_lock.h>
16#include <linux/slab.h>
17#include <linux/err.h>
18#include <linux/sysctl.h>
19#include <asm/mman.h>
20#include <asm/pgalloc.h>
21#include <asm/tlb.h>
22#include <asm/tlbflush.h>
23#include <asm/mmu_context.h>
24#include <asm/machdep.h>
25#include <asm/cputable.h>
26#include <asm/tlb.h>
27
28#include <linux/sysctl.h>
29
David Gibsonc594ada2005-08-11 16:55:21 +100030#define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
31#define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
32
David Gibsonf10a04c2006-04-28 15:02:51 +100033#ifdef CONFIG_PPC_64K_PAGES
34#define HUGEPTE_INDEX_SIZE (PMD_SHIFT-HPAGE_SHIFT)
35#else
36#define HUGEPTE_INDEX_SIZE (PUD_SHIFT-HPAGE_SHIFT)
37#endif
38#define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
39#define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << HUGEPTE_INDEX_SIZE)
40
41#define HUGEPD_SHIFT (HPAGE_SHIFT + HUGEPTE_INDEX_SIZE)
42#define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
43#define HUGEPD_MASK (~(HUGEPD_SIZE-1))
44
45#define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
46
47/* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
48 * will choke on pointers to hugepte tables, which is handy for
49 * catching screwups early. */
50#define HUGEPD_OK 0x1
51
52typedef struct { unsigned long pd; } hugepd_t;
53
54#define hugepd_none(hpd) ((hpd).pd == 0)
55
56static inline pte_t *hugepd_page(hugepd_t hpd)
57{
58 BUG_ON(!(hpd.pd & HUGEPD_OK));
59 return (pte_t *)(hpd.pd & ~HUGEPD_OK);
60}
61
62static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr)
63{
64 unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1));
65 pte_t *dir = hugepd_page(*hpdp);
66
67 return dir + idx;
68}
69
70static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
71 unsigned long address)
72{
73 pte_t *new = kmem_cache_alloc(huge_pgtable_cache,
74 GFP_KERNEL|__GFP_REPEAT);
75
76 if (! new)
77 return -ENOMEM;
78
79 spin_lock(&mm->page_table_lock);
80 if (!hugepd_none(*hpdp))
81 kmem_cache_free(huge_pgtable_cache, new);
82 else
83 hpdp->pd = (unsigned long)new | HUGEPD_OK;
84 spin_unlock(&mm->page_table_lock);
85 return 0;
86}
87
David Gibsone28f7fa2005-08-05 19:39:06 +100088/* Modelled after find_linux_pte() */
89pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
David Gibsone28f7fa2005-08-05 19:39:06 +100091 pgd_t *pg;
92 pud_t *pu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 BUG_ON(! in_hugepage_area(mm->context, addr));
95
David Gibsone28f7fa2005-08-05 19:39:06 +100096 addr &= HPAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Gibsone28f7fa2005-08-05 19:39:06 +100098 pg = pgd_offset(mm, addr);
99 if (!pgd_none(*pg)) {
100 pu = pud_offset(pg, addr);
101 if (!pud_none(*pu)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100102#ifdef CONFIG_PPC_64K_PAGES
David Gibsonf10a04c2006-04-28 15:02:51 +1000103 pmd_t *pm;
104 pm = pmd_offset(pu, addr);
105 if (!pmd_none(*pm))
106 return hugepte_offset((hugepd_t *)pm, addr);
107#else
108 return hugepte_offset((hugepd_t *)pu, addr);
109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 }
112
David Gibsone28f7fa2005-08-05 19:39:06 +1000113 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
David Gibson63551ae2005-06-21 17:14:44 -0700116pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
David Gibsone28f7fa2005-08-05 19:39:06 +1000118 pgd_t *pg;
119 pud_t *pu;
David Gibsonf10a04c2006-04-28 15:02:51 +1000120 hugepd_t *hpdp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 BUG_ON(! in_hugepage_area(mm->context, addr));
123
David Gibsone28f7fa2005-08-05 19:39:06 +1000124 addr &= HPAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David Gibsone28f7fa2005-08-05 19:39:06 +1000126 pg = pgd_offset(mm, addr);
127 pu = pud_alloc(mm, pg, addr);
128
129 if (pu) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100130#ifdef CONFIG_PPC_64K_PAGES
David Gibsonf10a04c2006-04-28 15:02:51 +1000131 pmd_t *pm;
132 pm = pmd_alloc(mm, pu, addr);
133 if (pm)
134 hpdp = (hugepd_t *)pm;
135#else
136 hpdp = (hugepd_t *)pu;
137#endif
David Gibsone28f7fa2005-08-05 19:39:06 +1000138 }
139
David Gibsonf10a04c2006-04-28 15:02:51 +1000140 if (! hpdp)
141 return NULL;
142
143 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr))
144 return NULL;
145
146 return hugepte_offset(hpdp, addr);
147}
148
Chen, Kenneth W39dde652006-12-06 20:32:03 -0800149int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
150{
151 return 0;
152}
153
David Gibsonf10a04c2006-04-28 15:02:51 +1000154static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
155{
156 pte_t *hugepte = hugepd_page(*hpdp);
157
158 hpdp->pd = 0;
159 tlb->need_flush = 1;
160 pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
Adam Litkec9169f82006-08-18 11:22:21 -0700161 PGF_CACHENUM_MASK));
David Gibsonf10a04c2006-04-28 15:02:51 +1000162}
163
164#ifdef CONFIG_PPC_64K_PAGES
165static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
166 unsigned long addr, unsigned long end,
167 unsigned long floor, unsigned long ceiling)
168{
169 pmd_t *pmd;
170 unsigned long next;
171 unsigned long start;
172
173 start = addr;
174 pmd = pmd_offset(pud, addr);
175 do {
176 next = pmd_addr_end(addr, end);
177 if (pmd_none(*pmd))
178 continue;
179 free_hugepte_range(tlb, (hugepd_t *)pmd);
180 } while (pmd++, addr = next, addr != end);
181
182 start &= PUD_MASK;
183 if (start < floor)
184 return;
185 if (ceiling) {
186 ceiling &= PUD_MASK;
187 if (!ceiling)
188 return;
189 }
190 if (end - 1 > ceiling - 1)
191 return;
192
193 pmd = pmd_offset(pud, start);
194 pud_clear(pud);
195 pmd_free_tlb(tlb, pmd);
196}
197#endif
198
199static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
200 unsigned long addr, unsigned long end,
201 unsigned long floor, unsigned long ceiling)
202{
203 pud_t *pud;
204 unsigned long next;
205 unsigned long start;
206
207 start = addr;
208 pud = pud_offset(pgd, addr);
209 do {
210 next = pud_addr_end(addr, end);
211#ifdef CONFIG_PPC_64K_PAGES
212 if (pud_none_or_clear_bad(pud))
213 continue;
214 hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
215#else
216 if (pud_none(*pud))
217 continue;
218 free_hugepte_range(tlb, (hugepd_t *)pud);
219#endif
220 } while (pud++, addr = next, addr != end);
221
222 start &= PGDIR_MASK;
223 if (start < floor)
224 return;
225 if (ceiling) {
226 ceiling &= PGDIR_MASK;
227 if (!ceiling)
228 return;
229 }
230 if (end - 1 > ceiling - 1)
231 return;
232
233 pud = pud_offset(pgd, start);
234 pgd_clear(pgd);
235 pud_free_tlb(tlb, pud);
236}
237
238/*
239 * This function frees user-level page tables of a process.
240 *
241 * Must be called with pagetable lock held.
242 */
243void hugetlb_free_pgd_range(struct mmu_gather **tlb,
244 unsigned long addr, unsigned long end,
245 unsigned long floor, unsigned long ceiling)
246{
247 pgd_t *pgd;
248 unsigned long next;
249 unsigned long start;
250
251 /*
252 * Comments below take from the normal free_pgd_range(). They
253 * apply here too. The tests against HUGEPD_MASK below are
254 * essential, because we *don't* test for this at the bottom
255 * level. Without them we'll attempt to free a hugepte table
256 * when we unmap just part of it, even if there are other
257 * active mappings using it.
258 *
259 * The next few lines have given us lots of grief...
260 *
261 * Why are we testing HUGEPD* at this top level? Because
262 * often there will be no work to do at all, and we'd prefer
263 * not to go all the way down to the bottom just to discover
264 * that.
265 *
266 * Why all these "- 1"s? Because 0 represents both the bottom
267 * of the address space and the top of it (using -1 for the
268 * top wouldn't help much: the masks would do the wrong thing).
269 * The rule is that addr 0 and floor 0 refer to the bottom of
270 * the address space, but end 0 and ceiling 0 refer to the top
271 * Comparisons need to use "end - 1" and "ceiling - 1" (though
272 * that end 0 case should be mythical).
273 *
274 * Wherever addr is brought up or ceiling brought down, we
275 * must be careful to reject "the opposite 0" before it
276 * confuses the subsequent tests. But what about where end is
277 * brought down by HUGEPD_SIZE below? no, end can't go down to
278 * 0 there.
279 *
280 * Whereas we round start (addr) and ceiling down, by different
281 * masks at different levels, in order to test whether a table
282 * now has no other vmas using it, so can be freed, we don't
283 * bother to round floor or end up - the tests don't need that.
284 */
285
286 addr &= HUGEPD_MASK;
287 if (addr < floor) {
288 addr += HUGEPD_SIZE;
289 if (!addr)
290 return;
291 }
292 if (ceiling) {
293 ceiling &= HUGEPD_MASK;
294 if (!ceiling)
295 return;
296 }
297 if (end - 1 > ceiling - 1)
298 end -= HUGEPD_SIZE;
299 if (addr > end - 1)
300 return;
301
302 start = addr;
303 pgd = pgd_offset((*tlb)->mm, addr);
304 do {
305 BUG_ON(! in_hugepage_area((*tlb)->mm->context, addr));
306 next = pgd_addr_end(addr, end);
307 if (pgd_none_or_clear_bad(pgd))
308 continue;
309 hugetlb_free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
310 } while (pgd++, addr = next, addr != end);
David Gibsone28f7fa2005-08-05 19:39:06 +1000311}
312
David Gibsone28f7fa2005-08-05 19:39:06 +1000313void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
314 pte_t *ptep, pte_t pte)
315{
David Gibsone28f7fa2005-08-05 19:39:06 +1000316 if (pte_present(*ptep)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100317 /* We open-code pte_clear because we need to pass the right
318 * argument to hpte_update (huge / !huge)
319 */
320 unsigned long old = pte_update(ptep, ~0UL);
321 if (old & _PAGE_HASHPTE)
322 hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
David Gibsone28f7fa2005-08-05 19:39:06 +1000323 flush_tlb_pending();
324 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100325 *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
David Gibsone28f7fa2005-08-05 19:39:06 +1000326}
327
328pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
329 pte_t *ptep)
330{
331 unsigned long old = pte_update(ptep, ~0UL);
David Gibsone28f7fa2005-08-05 19:39:06 +1000332
333 if (old & _PAGE_HASHPTE)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100334 hpte_update(mm, addr & HPAGE_MASK, ptep, old, 1);
335 *ptep = __pte(0);
David Gibsone28f7fa2005-08-05 19:39:06 +1000336
337 return __pte(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
David Gibson23ed6cb2005-12-09 16:45:17 +1100340struct slb_flush_info {
341 struct mm_struct *mm;
342 u16 newareas;
343};
344
David Gibsonc594ada2005-08-11 16:55:21 +1000345static void flush_low_segments(void *parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David Gibson23ed6cb2005-12-09 16:45:17 +1100347 struct slb_flush_info *fi = parm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long i;
349
David Gibson23ed6cb2005-12-09 16:45:17 +1100350 BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_LOW_AREAS);
351
352 if (current->active_mm != fi->mm)
353 return;
354
355 /* Only need to do anything if this CPU is working in the same
356 * mm as the one which has changed */
357
358 /* update the paca copy of the context struct */
359 get_paca()->context = current->active_mm->context;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 asm volatile("isync" : : : "memory");
David Gibsonc594ada2005-08-11 16:55:21 +1000362 for (i = 0; i < NUM_LOW_AREAS; i++) {
David Gibson23ed6cb2005-12-09 16:45:17 +1100363 if (! (fi->newareas & (1U << i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 continue;
David Gibson14b34662005-09-06 14:59:47 +1000365 asm volatile("slbie %0"
366 : : "r" ((i << SID_SHIFT) | SLBIE_C));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 asm volatile("isync" : : : "memory");
369}
370
David Gibsonc594ada2005-08-11 16:55:21 +1000371static void flush_high_segments(void *parm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
David Gibson23ed6cb2005-12-09 16:45:17 +1100373 struct slb_flush_info *fi = parm;
David Gibsonc594ada2005-08-11 16:55:21 +1000374 unsigned long i, j;
375
David Gibson23ed6cb2005-12-09 16:45:17 +1100376
377 BUILD_BUG_ON((sizeof(fi->newareas)*8) != NUM_HIGH_AREAS);
378
379 if (current->active_mm != fi->mm)
380 return;
381
382 /* Only need to do anything if this CPU is working in the same
383 * mm as the one which has changed */
384
385 /* update the paca copy of the context struct */
386 get_paca()->context = current->active_mm->context;
387
David Gibsonc594ada2005-08-11 16:55:21 +1000388 asm volatile("isync" : : : "memory");
David Gibsonc594ada2005-08-11 16:55:21 +1000389 for (i = 0; i < NUM_HIGH_AREAS; i++) {
David Gibson23ed6cb2005-12-09 16:45:17 +1100390 if (! (fi->newareas & (1U << i)))
David Gibsonc594ada2005-08-11 16:55:21 +1000391 continue;
392 for (j = 0; j < (1UL << (HTLB_AREA_SHIFT-SID_SHIFT)); j++)
393 asm volatile("slbie %0"
David Gibson14b34662005-09-06 14:59:47 +1000394 :: "r" (((i << HTLB_AREA_SHIFT)
David Gibson23ed6cb2005-12-09 16:45:17 +1100395 + (j << SID_SHIFT)) | SLBIE_C));
David Gibsonc594ada2005-08-11 16:55:21 +1000396 }
David Gibsonc594ada2005-08-11 16:55:21 +1000397 asm volatile("isync" : : : "memory");
398}
399
400static int prepare_low_area_for_htlb(struct mm_struct *mm, unsigned long area)
401{
402 unsigned long start = area << SID_SHIFT;
403 unsigned long end = (area+1) << SID_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Gibsonc594ada2005-08-11 16:55:21 +1000406 BUG_ON(area >= NUM_LOW_AREAS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Check no VMAs are in the region */
409 vma = find_vma(mm, start);
410 if (vma && (vma->vm_start < end))
411 return -EBUSY;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414}
415
David Gibsonc594ada2005-08-11 16:55:21 +1000416static int prepare_high_area_for_htlb(struct mm_struct *mm, unsigned long area)
417{
418 unsigned long start = area << HTLB_AREA_SHIFT;
419 unsigned long end = (area+1) << HTLB_AREA_SHIFT;
420 struct vm_area_struct *vma;
421
422 BUG_ON(area >= NUM_HIGH_AREAS);
423
David Gibson7d24f0b2005-11-07 00:57:52 -0800424 /* Hack, so that each addresses is controlled by exactly one
425 * of the high or low area bitmaps, the first high area starts
426 * at 4GB, not 0 */
427 if (start == 0)
428 start = 0x100000000UL;
429
David Gibsonc594ada2005-08-11 16:55:21 +1000430 /* Check no VMAs are in the region */
431 vma = find_vma(mm, start);
432 if (vma && (vma->vm_start < end))
433 return -EBUSY;
434
435 return 0;
436}
437
438static int open_low_hpage_areas(struct mm_struct *mm, u16 newareas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 unsigned long i;
David Gibson23ed6cb2005-12-09 16:45:17 +1100441 struct slb_flush_info fi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Gibsonc594ada2005-08-11 16:55:21 +1000443 BUILD_BUG_ON((sizeof(newareas)*8) != NUM_LOW_AREAS);
444 BUILD_BUG_ON((sizeof(mm->context.low_htlb_areas)*8) != NUM_LOW_AREAS);
445
446 newareas &= ~(mm->context.low_htlb_areas);
447 if (! newareas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0; /* The segments we want are already open */
449
David Gibsonc594ada2005-08-11 16:55:21 +1000450 for (i = 0; i < NUM_LOW_AREAS; i++)
451 if ((1 << i) & newareas)
452 if (prepare_low_area_for_htlb(mm, i) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EBUSY;
454
David Gibsonc594ada2005-08-11 16:55:21 +1000455 mm->context.low_htlb_areas |= newareas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* the context change must make it to memory before the flush,
458 * so that further SLB misses do the right thing. */
459 mb();
David Gibson23ed6cb2005-12-09 16:45:17 +1100460
461 fi.mm = mm;
462 fi.newareas = newareas;
463 on_each_cpu(flush_low_segments, &fi, 0, 1);
David Gibsonc594ada2005-08-11 16:55:21 +1000464
465 return 0;
466}
467
468static int open_high_hpage_areas(struct mm_struct *mm, u16 newareas)
469{
David Gibson23ed6cb2005-12-09 16:45:17 +1100470 struct slb_flush_info fi;
David Gibsonc594ada2005-08-11 16:55:21 +1000471 unsigned long i;
472
473 BUILD_BUG_ON((sizeof(newareas)*8) != NUM_HIGH_AREAS);
474 BUILD_BUG_ON((sizeof(mm->context.high_htlb_areas)*8)
475 != NUM_HIGH_AREAS);
476
477 newareas &= ~(mm->context.high_htlb_areas);
478 if (! newareas)
479 return 0; /* The areas we want are already open */
480
481 for (i = 0; i < NUM_HIGH_AREAS; i++)
482 if ((1 << i) & newareas)
483 if (prepare_high_area_for_htlb(mm, i) != 0)
484 return -EBUSY;
485
486 mm->context.high_htlb_areas |= newareas;
487
David Gibsonc594ada2005-08-11 16:55:21 +1000488 /* the context change must make it to memory before the flush,
489 * so that further SLB misses do the right thing. */
490 mb();
David Gibson23ed6cb2005-12-09 16:45:17 +1100491
492 fi.mm = mm;
493 fi.newareas = newareas;
494 on_each_cpu(flush_high_segments, &fi, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 return 0;
497}
498
Hugh Dickins68589bc2006-11-14 02:03:32 -0800499int prepare_hugepage_range(unsigned long addr, unsigned long len, pgoff_t pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
David Gibson5e391dc2005-11-23 13:37:45 -0800501 int err = 0;
David Gibsonc594ada2005-08-11 16:55:21 +1000502
Hugh Dickins68589bc2006-11-14 02:03:32 -0800503 if (pgoff & (~HPAGE_MASK >> PAGE_SHIFT))
504 return -EINVAL;
505 if (len & ~HPAGE_MASK)
506 return -EINVAL;
507 if (addr & ~HPAGE_MASK)
David Gibsonc594ada2005-08-11 16:55:21 +1000508 return -EINVAL;
509
David Gibson5e391dc2005-11-23 13:37:45 -0800510 if (addr < 0x100000000UL)
David Gibsonc594ada2005-08-11 16:55:21 +1000511 err = open_low_hpage_areas(current->mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 LOW_ESID_MASK(addr, len));
David Gibson9a94c572005-11-24 13:34:56 +1100513 if ((addr + len) > 0x100000000UL)
David Gibsonc594ada2005-08-11 16:55:21 +1000514 err = open_high_hpage_areas(current->mm,
515 HTLB_AREA_MASK(addr, len));
516 if (err) {
517 printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
518 " failed (lowmask: 0x%04hx, highmask: 0x%04hx)\n",
519 addr, len,
520 LOW_ESID_MASK(addr, len), HTLB_AREA_MASK(addr, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return err;
522 }
523
David Gibsonc594ada2005-08-11 16:55:21 +1000524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527struct page *
528follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
529{
530 pte_t *ptep;
531 struct page *page;
532
533 if (! in_hugepage_area(mm->context, address))
534 return ERR_PTR(-EINVAL);
535
536 ptep = huge_pte_offset(mm, address);
537 page = pte_page(*ptep);
538 if (page)
539 page += (address % HPAGE_SIZE) / PAGE_SIZE;
540
541 return page;
542}
543
544int pmd_huge(pmd_t pmd)
545{
546 return 0;
547}
548
549struct page *
550follow_huge_pmd(struct mm_struct *mm, unsigned long address,
551 pmd_t *pmd, int write)
552{
553 BUG();
554 return NULL;
555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/* Because we have an exclusive hugepage region which lies within the
558 * normal user address space, we have to take special measures to make
559 * non-huge mmap()s evade the hugepage reserved regions. */
560unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
561 unsigned long len, unsigned long pgoff,
562 unsigned long flags)
563{
564 struct mm_struct *mm = current->mm;
565 struct vm_area_struct *vma;
566 unsigned long start_addr;
567
568 if (len > TASK_SIZE)
569 return -ENOMEM;
570
571 if (addr) {
572 addr = PAGE_ALIGN(addr);
573 vma = find_vma(mm, addr);
574 if (((TASK_SIZE - len) >= addr)
575 && (!vma || (addr+len) <= vma->vm_start)
576 && !is_hugepage_only_range(mm, addr,len))
577 return addr;
578 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700579 if (len > mm->cached_hole_size) {
580 start_addr = addr = mm->free_area_cache;
581 } else {
582 start_addr = addr = TASK_UNMAPPED_BASE;
583 mm->cached_hole_size = 0;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586full_search:
587 vma = find_vma(mm, addr);
588 while (TASK_SIZE - len >= addr) {
589 BUG_ON(vma && (addr >= vma->vm_end));
590
591 if (touches_hugepage_low_range(mm, addr, len)) {
592 addr = ALIGN(addr+1, 1<<SID_SHIFT);
593 vma = find_vma(mm, addr);
594 continue;
595 }
David Gibsonc594ada2005-08-11 16:55:21 +1000596 if (touches_hugepage_high_range(mm, addr, len)) {
597 addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 vma = find_vma(mm, addr);
599 continue;
600 }
601 if (!vma || addr + len <= vma->vm_start) {
602 /*
603 * Remember the place where we stopped the search:
604 */
605 mm->free_area_cache = addr + len;
606 return addr;
607 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700608 if (addr + mm->cached_hole_size < vma->vm_start)
609 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 addr = vma->vm_end;
611 vma = vma->vm_next;
612 }
613
614 /* Make sure we didn't miss any holes */
615 if (start_addr != TASK_UNMAPPED_BASE) {
616 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700617 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 goto full_search;
619 }
620 return -ENOMEM;
621}
622
623/*
624 * This mmap-allocator allocates new areas top-down from below the
625 * stack's low limit (the base):
626 *
627 * Because we have an exclusive hugepage region which lies within the
628 * normal user address space, we have to take special measures to make
629 * non-huge mmap()s evade the hugepage reserved regions.
630 */
631unsigned long
632arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
633 const unsigned long len, const unsigned long pgoff,
634 const unsigned long flags)
635{
636 struct vm_area_struct *vma, *prev_vma;
637 struct mm_struct *mm = current->mm;
638 unsigned long base = mm->mmap_base, addr = addr0;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700639 unsigned long largest_hole = mm->cached_hole_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 int first_time = 1;
641
642 /* requested length too big for entire address space */
643 if (len > TASK_SIZE)
644 return -ENOMEM;
645
646 /* dont allow allocations above current base */
647 if (mm->free_area_cache > base)
648 mm->free_area_cache = base;
649
650 /* requesting a specific address */
651 if (addr) {
652 addr = PAGE_ALIGN(addr);
653 vma = find_vma(mm, addr);
654 if (TASK_SIZE - len >= addr &&
655 (!vma || addr + len <= vma->vm_start)
656 && !is_hugepage_only_range(mm, addr,len))
657 return addr;
658 }
659
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700660 if (len <= largest_hole) {
661 largest_hole = 0;
662 mm->free_area_cache = base;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664try_again:
665 /* make sure it can fit in the remaining address space */
666 if (mm->free_area_cache < len)
667 goto fail;
668
669 /* either no address requested or cant fit in requested address hole */
670 addr = (mm->free_area_cache - len) & PAGE_MASK;
671 do {
672hugepage_recheck:
673 if (touches_hugepage_low_range(mm, addr, len)) {
674 addr = (addr & ((~0) << SID_SHIFT)) - len;
675 goto hugepage_recheck;
David Gibsonc594ada2005-08-11 16:55:21 +1000676 } else if (touches_hugepage_high_range(mm, addr, len)) {
677 addr = (addr & ((~0UL) << HTLB_AREA_SHIFT)) - len;
678 goto hugepage_recheck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 /*
682 * Lookup failure means no vma is above this address,
683 * i.e. return with success:
684 */
685 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
686 return addr;
687
688 /*
689 * new region fits between prev_vma->vm_end and
690 * vma->vm_start, use it:
691 */
692 if (addr+len <= vma->vm_start &&
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700693 (!prev_vma || (addr >= prev_vma->vm_end))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* remember the address as a hint for next time */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700695 mm->cached_hole_size = largest_hole;
696 return (mm->free_area_cache = addr);
697 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* pull free_area_cache down to the first hole */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700699 if (mm->free_area_cache == vma->vm_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mm->free_area_cache = vma->vm_start;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700701 mm->cached_hole_size = largest_hole;
702 }
703 }
704
705 /* remember the largest hole we saw so far */
706 if (addr + largest_hole < vma->vm_start)
707 largest_hole = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* try just below the current vma->vm_start */
710 addr = vma->vm_start-len;
711 } while (len <= vma->vm_start);
712
713fail:
714 /*
715 * if hint left us with no space for the requested
716 * mapping then try again:
717 */
718 if (first_time) {
719 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700720 largest_hole = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 first_time = 0;
722 goto try_again;
723 }
724 /*
725 * A failed mmap() very likely causes application failure,
726 * so fall back to the bottom-up function here. This scenario
727 * can happen with large stack limits and large mmap()
728 * allocations.
729 */
730 mm->free_area_cache = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700731 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
733 /*
734 * Restore the topdown base:
735 */
736 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700737 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 return addr;
740}
741
David Gibson456752f2005-11-24 14:16:15 +1100742static int htlb_check_hinted_area(unsigned long addr, unsigned long len)
743{
744 struct vm_area_struct *vma;
745
746 vma = find_vma(current->mm, addr);
747 if (!vma || ((addr + len) <= vma->vm_start))
748 return 0;
749
750 return -ENOMEM;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
754{
755 unsigned long addr = 0;
756 struct vm_area_struct *vma;
757
758 vma = find_vma(current->mm, addr);
759 while (addr + len <= 0x100000000UL) {
760 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
761
762 if (! __within_hugepage_low_range(addr, len, segmask)) {
763 addr = ALIGN(addr+1, 1<<SID_SHIFT);
764 vma = find_vma(current->mm, addr);
765 continue;
766 }
767
768 if (!vma || (addr + len) <= vma->vm_start)
769 return addr;
770 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
771 /* Depending on segmask this might not be a confirmed
772 * hugepage region, so the ALIGN could have skipped
773 * some VMAs */
774 vma = find_vma(current->mm, addr);
775 }
776
777 return -ENOMEM;
778}
779
David Gibsonc594ada2005-08-11 16:55:21 +1000780static unsigned long htlb_get_high_area(unsigned long len, u16 areamask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
David Gibsonc594ada2005-08-11 16:55:21 +1000782 unsigned long addr = 0x100000000UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct vm_area_struct *vma;
784
785 vma = find_vma(current->mm, addr);
David Gibsonc594ada2005-08-11 16:55:21 +1000786 while (addr + len <= TASK_SIZE_USER64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
David Gibsonc594ada2005-08-11 16:55:21 +1000788
789 if (! __within_hugepage_high_range(addr, len, areamask)) {
790 addr = ALIGN(addr+1, 1UL<<HTLB_AREA_SHIFT);
791 vma = find_vma(current->mm, addr);
792 continue;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (!vma || (addr + len) <= vma->vm_start)
796 return addr;
797 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
David Gibsonc594ada2005-08-11 16:55:21 +1000798 /* Depending on segmask this might not be a confirmed
799 * hugepage region, so the ALIGN could have skipped
800 * some VMAs */
801 vma = find_vma(current->mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 return -ENOMEM;
805}
806
807unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
808 unsigned long len, unsigned long pgoff,
809 unsigned long flags)
810{
David Gibsonc594ada2005-08-11 16:55:21 +1000811 int lastshift;
812 u16 areamask, curareas;
813
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100814 if (HPAGE_SHIFT == 0)
815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (len & ~HPAGE_MASK)
817 return -EINVAL;
818
819 if (!cpu_has_feature(CPU_FTR_16M_PAGE))
820 return -EINVAL;
821
David Gibson456752f2005-11-24 14:16:15 +1100822 /* Paranoia, caller should have dealt with this */
823 BUG_ON((addr + len) < addr);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (test_thread_flag(TIF_32BIT)) {
David Gibson456752f2005-11-24 14:16:15 +1100826 /* Paranoia, caller should have dealt with this */
827 BUG_ON((addr + len) > 0x100000000UL);
828
David Gibsonc594ada2005-08-11 16:55:21 +1000829 curareas = current->mm->context.low_htlb_areas;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
David Gibson456752f2005-11-24 14:16:15 +1100831 /* First see if we can use the hint address */
832 if (addr && (htlb_check_hinted_area(addr, len) == 0)) {
833 areamask = LOW_ESID_MASK(addr, len);
834 if (open_low_hpage_areas(current->mm, areamask) == 0)
835 return addr;
836 }
837
838 /* Next see if we can map in the existing low areas */
David Gibsonc594ada2005-08-11 16:55:21 +1000839 addr = htlb_get_low_area(len, curareas);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (addr != -ENOMEM)
841 return addr;
842
David Gibson456752f2005-11-24 14:16:15 +1100843 /* Finally go looking for areas to open */
David Gibsonc594ada2005-08-11 16:55:21 +1000844 lastshift = 0;
845 for (areamask = LOW_ESID_MASK(0x100000000UL-len, len);
846 ! lastshift; areamask >>=1) {
847 if (areamask & 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 lastshift = 1;
849
David Gibsonc594ada2005-08-11 16:55:21 +1000850 addr = htlb_get_low_area(len, curareas | areamask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if ((addr != -ENOMEM)
David Gibsonc594ada2005-08-11 16:55:21 +1000852 && open_low_hpage_areas(current->mm, areamask) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return addr;
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
David Gibsonc594ada2005-08-11 16:55:21 +1000856 curareas = current->mm->context.high_htlb_areas;
857
David Gibson456752f2005-11-24 14:16:15 +1100858 /* First see if we can use the hint address */
859 /* We discourage 64-bit processes from doing hugepage
860 * mappings below 4GB (must use MAP_FIXED) */
861 if ((addr >= 0x100000000UL)
862 && (htlb_check_hinted_area(addr, len) == 0)) {
863 areamask = HTLB_AREA_MASK(addr, len);
864 if (open_high_hpage_areas(current->mm, areamask) == 0)
865 return addr;
866 }
867
868 /* Next see if we can map in the existing high areas */
David Gibsonc594ada2005-08-11 16:55:21 +1000869 addr = htlb_get_high_area(len, curareas);
870 if (addr != -ENOMEM)
871 return addr;
872
David Gibson456752f2005-11-24 14:16:15 +1100873 /* Finally go looking for areas to open */
David Gibsonc594ada2005-08-11 16:55:21 +1000874 lastshift = 0;
875 for (areamask = HTLB_AREA_MASK(TASK_SIZE_USER64-len, len);
876 ! lastshift; areamask >>=1) {
877 if (areamask & 1)
878 lastshift = 1;
879
880 addr = htlb_get_high_area(len, curareas | areamask);
881 if ((addr != -ENOMEM)
882 && open_high_hpage_areas(current->mm, areamask) == 0)
883 return addr;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
David Gibsonc594ada2005-08-11 16:55:21 +1000886 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
887 " enough areas\n");
888 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
David Gibsoncbf52af2005-12-09 14:20:52 +1100891/*
892 * Called by asm hashtable.S for doing lazy icache flush
893 */
894static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
895 pte_t pte, int trap)
896{
897 struct page *page;
898 int i;
899
900 if (!pfn_valid(pte_pfn(pte)))
901 return rflags;
902
903 page = pte_page(pte);
904
905 /* page is dirty */
906 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
907 if (trap == 0x400) {
908 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
909 __flush_dcache_icache(page_address(page+i));
910 set_bit(PG_arch_1, &page->flags);
911 } else {
912 rflags |= HPTE_R_N;
913 }
914 }
915 return rflags;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918int hash_huge_page(struct mm_struct *mm, unsigned long access,
David Gibsoncbf52af2005-12-09 14:20:52 +1100919 unsigned long ea, unsigned long vsid, int local,
920 unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 pte_t *ptep;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100923 unsigned long old_pte, new_pte;
924 unsigned long va, rflags, pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 long slot;
926 int err = 1;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 ptep = huge_pte_offset(mm, ea);
929
930 /* Search the Linux page table for a match with va */
931 va = (vsid << 28) | (ea & 0x0fffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /*
934 * If no pte found or not present, send the problem up to
935 * do_page_fault
936 */
937 if (unlikely(!ptep || pte_none(*ptep)))
938 goto out;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * Check the user's access rights to the page. If access should be
942 * prevented then send the problem up to do_page_fault.
943 */
944 if (unlikely(access & ~pte_val(*ptep)))
945 goto out;
946 /*
947 * At this point, we have a pte (old_pte) which can be used to build
948 * or update an HPTE. There are 2 cases:
949 *
950 * 1. There is a valid (present) pte with no associated HPTE (this is
951 * the most common case)
952 * 2. There is a valid (present) pte with an associated HPTE. The
953 * current values of the pp bits in the HPTE prevent access
954 * because we are doing software DIRTY bit management and the
955 * page is currently not DIRTY.
956 */
957
958
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100959 do {
960 old_pte = pte_val(*ptep);
961 if (old_pte & _PAGE_BUSY)
962 goto out;
963 new_pte = old_pte | _PAGE_BUSY |
964 _PAGE_ACCESSED | _PAGE_HASHPTE;
965 } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
966 old_pte, new_pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100968 rflags = 0x2 | (!(new_pte & _PAGE_RW));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100970 rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
David Gibsoncbf52af2005-12-09 14:20:52 +1100971 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
972 /* No CPU has hugepages but lacks no execute, so we
973 * don't need to worry about that case */
974 rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
975 trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Check if pte already has an hpte (case 2) */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100978 if (unlikely(old_pte & _PAGE_HASHPTE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* There MIGHT be an HPTE for this pte */
980 unsigned long hash, slot;
981
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100982 hash = hpt_hash(va, HPAGE_SHIFT);
983 if (old_pte & _PAGE_F_SECOND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 hash = ~hash;
985 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100986 slot += (old_pte & _PAGE_F_GIX) >> 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Benjamin Herrenschmidt325c82a2005-12-08 16:51:44 +1100988 if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
989 local) == -1)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100990 old_pte &= ~_PAGE_HPTEFLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100993 if (likely(!(old_pte & _PAGE_HASHPTE))) {
994 unsigned long hash = hpt_hash(va, HPAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unsigned long hpte_group;
996
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100997 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999repeat:
1000 hpte_group = ((hash & htab_hash_mask) *
1001 HPTES_PER_GROUP) & ~0x7UL;
1002
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001003 /* clear HPTE slot informations in new PTE */
1004 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* Add in WIMG bits */
1007 /* XXX We should store these in the pte */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001008 /* --BenH: I think they are ... */
David Gibson96e28442005-07-13 01:11:42 -07001009 rflags |= _PAGE_COHERENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001011 /* Insert into the hash table, primary slot */
1012 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
1013 mmu_huge_psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Primary is full, try the secondary */
1016 if (unlikely(slot == -1)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001017 new_pte |= _PAGE_F_SECOND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 hpte_group = ((~hash & htab_hash_mask) *
1019 HPTES_PER_GROUP) & ~0x7UL;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001020 slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
Benjamin Herrenschmidt67b10812005-09-23 13:24:07 -07001021 HPTE_V_SECONDARY,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001022 mmu_huge_psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (slot == -1) {
1024 if (mftb() & 0x1)
Benjamin Herrenschmidt67b10812005-09-23 13:24:07 -07001025 hpte_group = ((hash & htab_hash_mask) *
1026 HPTES_PER_GROUP)&~0x7UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 ppc_md.hpte_remove(hpte_group);
1029 goto repeat;
1030 }
1031 }
1032
1033 if (unlikely(slot == -2))
1034 panic("hash_huge_page: pte_insert failed\n");
1035
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001036 new_pte |= (slot << 12) & _PAGE_F_GIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001039 /*
Hugh Dickins01edcd82005-11-23 13:37:39 -08001040 * No need to use ldarx/stdcx here
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001041 */
1042 *ptep = __pte(new_pte & ~_PAGE_BUSY);
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 err = 0;
1045
1046 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return err;
1048}
David Gibsonf10a04c2006-04-28 15:02:51 +10001049
1050static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
1051{
1052 memset(addr, 0, kmem_cache_size(cache));
1053}
1054
1055static int __init hugetlbpage_init(void)
1056{
1057 if (!cpu_has_feature(CPU_FTR_16M_PAGE))
1058 return -ENODEV;
1059
1060 huge_pgtable_cache = kmem_cache_create("hugepte_cache",
1061 HUGEPTE_TABLE_SIZE,
1062 HUGEPTE_TABLE_SIZE,
1063 SLAB_HWCACHE_ALIGN |
1064 SLAB_MUST_HWCACHE_ALIGN,
1065 zero_ctor, NULL);
1066 if (! huge_pgtable_cache)
1067 panic("hugetlbpage_init(): could not create hugepte cache\n");
1068
1069 return 0;
1070}
1071
1072module_init(hugetlbpage_init);