Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/sysctl.h> |
| 18 | #include <asm/mman.h> |
| 19 | #include <asm/pgalloc.h> |
| 20 | #include <asm/tlb.h> |
| 21 | #include <asm/tlbflush.h> |
| 22 | #include <asm/mmu_context.h> |
| 23 | #include <asm/machdep.h> |
| 24 | #include <asm/cputable.h> |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 25 | #include <asm/spu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 27 | #define HPAGE_SHIFT_64K 16 |
| 28 | #define HPAGE_SHIFT_16M 24 |
| 29 | |
David Gibson | c594ada | 2005-08-11 16:55:21 +1000 | [diff] [blame] | 30 | #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT) |
| 31 | #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT) |
| 32 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 33 | unsigned int hugepte_shift; |
| 34 | #define PTRS_PER_HUGEPTE (1 << hugepte_shift) |
| 35 | #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << hugepte_shift) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 36 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 37 | #define HUGEPD_SHIFT (HPAGE_SHIFT + hugepte_shift) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 38 | #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT) |
| 39 | #define HUGEPD_MASK (~(HUGEPD_SIZE-1)) |
| 40 | |
| 41 | #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM]) |
| 42 | |
| 43 | /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad() |
| 44 | * will choke on pointers to hugepte tables, which is handy for |
| 45 | * catching screwups early. */ |
| 46 | #define HUGEPD_OK 0x1 |
| 47 | |
| 48 | typedef struct { unsigned long pd; } hugepd_t; |
| 49 | |
| 50 | #define hugepd_none(hpd) ((hpd).pd == 0) |
| 51 | |
| 52 | static inline pte_t *hugepd_page(hugepd_t hpd) |
| 53 | { |
| 54 | BUG_ON(!(hpd.pd & HUGEPD_OK)); |
| 55 | return (pte_t *)(hpd.pd & ~HUGEPD_OK); |
| 56 | } |
| 57 | |
| 58 | static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr) |
| 59 | { |
| 60 | unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1)); |
| 61 | pte_t *dir = hugepd_page(*hpdp); |
| 62 | |
| 63 | return dir + idx; |
| 64 | } |
| 65 | |
| 66 | static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp, |
| 67 | unsigned long address) |
| 68 | { |
| 69 | pte_t *new = kmem_cache_alloc(huge_pgtable_cache, |
| 70 | GFP_KERNEL|__GFP_REPEAT); |
| 71 | |
| 72 | if (! new) |
| 73 | return -ENOMEM; |
| 74 | |
| 75 | spin_lock(&mm->page_table_lock); |
| 76 | if (!hugepd_none(*hpdp)) |
| 77 | kmem_cache_free(huge_pgtable_cache, new); |
| 78 | else |
| 79 | hpdp->pd = (unsigned long)new | HUGEPD_OK; |
| 80 | spin_unlock(&mm->page_table_lock); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 84 | /* Base page size affects how we walk hugetlb page tables */ |
| 85 | #ifdef CONFIG_PPC_64K_PAGES |
| 86 | #define hpmd_offset(pud, addr) pmd_offset(pud, addr) |
| 87 | #define hpmd_alloc(mm, pud, addr) pmd_alloc(mm, pud, addr) |
| 88 | #else |
| 89 | static inline |
| 90 | pmd_t *hpmd_offset(pud_t *pud, unsigned long addr) |
| 91 | { |
| 92 | if (HPAGE_SHIFT == HPAGE_SHIFT_64K) |
| 93 | return pmd_offset(pud, addr); |
| 94 | else |
| 95 | return (pmd_t *) pud; |
| 96 | } |
| 97 | static inline |
| 98 | pmd_t *hpmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long addr) |
| 99 | { |
| 100 | if (HPAGE_SHIFT == HPAGE_SHIFT_64K) |
| 101 | return pmd_alloc(mm, pud, addr); |
| 102 | else |
| 103 | return (pmd_t *) pud; |
| 104 | } |
| 105 | #endif |
| 106 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 107 | /* Modelled after find_linux_pte() */ |
| 108 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 110 | pgd_t *pg; |
| 111 | pud_t *pu; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 112 | pmd_t *pm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 114 | BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 116 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 118 | pg = pgd_offset(mm, addr); |
| 119 | if (!pgd_none(*pg)) { |
| 120 | pu = pud_offset(pg, addr); |
| 121 | if (!pud_none(*pu)) { |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 122 | pm = hpmd_offset(pu, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 123 | if (!pmd_none(*pm)) |
| 124 | return hugepte_offset((hugepd_t *)pm, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 128 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame^] | 131 | pte_t *huge_pte_alloc(struct mm_struct *mm, |
| 132 | unsigned long addr, unsigned long sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 134 | pgd_t *pg; |
| 135 | pud_t *pu; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 136 | pmd_t *pm; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 137 | hugepd_t *hpdp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 139 | BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 141 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 143 | pg = pgd_offset(mm, addr); |
| 144 | pu = pud_alloc(mm, pg, addr); |
| 145 | |
| 146 | if (pu) { |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 147 | pm = hpmd_alloc(mm, pu, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 148 | if (pm) |
| 149 | hpdp = (hugepd_t *)pm; |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 150 | } |
| 151 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 152 | if (! hpdp) |
| 153 | return NULL; |
| 154 | |
| 155 | if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr)) |
| 156 | return NULL; |
| 157 | |
| 158 | return hugepte_offset(hpdp, addr); |
| 159 | } |
| 160 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 161 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) |
| 162 | { |
| 163 | return 0; |
| 164 | } |
| 165 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 166 | static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp) |
| 167 | { |
| 168 | pte_t *hugepte = hugepd_page(*hpdp); |
| 169 | |
| 170 | hpdp->pd = 0; |
| 171 | tlb->need_flush = 1; |
| 172 | pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM, |
Adam Litke | c9169f8 | 2006-08-18 11:22:21 -0700 | [diff] [blame] | 173 | PGF_CACHENUM_MASK)); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 174 | } |
| 175 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 176 | static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 177 | unsigned long addr, unsigned long end, |
| 178 | unsigned long floor, unsigned long ceiling) |
| 179 | { |
| 180 | pmd_t *pmd; |
| 181 | unsigned long next; |
| 182 | unsigned long start; |
| 183 | |
| 184 | start = addr; |
| 185 | pmd = pmd_offset(pud, addr); |
| 186 | do { |
| 187 | next = pmd_addr_end(addr, end); |
| 188 | if (pmd_none(*pmd)) |
| 189 | continue; |
| 190 | free_hugepte_range(tlb, (hugepd_t *)pmd); |
| 191 | } while (pmd++, addr = next, addr != end); |
| 192 | |
| 193 | start &= PUD_MASK; |
| 194 | if (start < floor) |
| 195 | return; |
| 196 | if (ceiling) { |
| 197 | ceiling &= PUD_MASK; |
| 198 | if (!ceiling) |
| 199 | return; |
| 200 | } |
| 201 | if (end - 1 > ceiling - 1) |
| 202 | return; |
| 203 | |
| 204 | pmd = pmd_offset(pud, start); |
| 205 | pud_clear(pud); |
| 206 | pmd_free_tlb(tlb, pmd); |
| 207 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 208 | |
| 209 | static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, |
| 210 | unsigned long addr, unsigned long end, |
| 211 | unsigned long floor, unsigned long ceiling) |
| 212 | { |
| 213 | pud_t *pud; |
| 214 | unsigned long next; |
| 215 | unsigned long start; |
| 216 | |
| 217 | start = addr; |
| 218 | pud = pud_offset(pgd, addr); |
| 219 | do { |
| 220 | next = pud_addr_end(addr, end); |
| 221 | #ifdef CONFIG_PPC_64K_PAGES |
| 222 | if (pud_none_or_clear_bad(pud)) |
| 223 | continue; |
| 224 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling); |
| 225 | #else |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 226 | if (HPAGE_SHIFT == HPAGE_SHIFT_64K) { |
| 227 | if (pud_none_or_clear_bad(pud)) |
| 228 | continue; |
| 229 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling); |
| 230 | } else { |
| 231 | if (pud_none(*pud)) |
| 232 | continue; |
| 233 | free_hugepte_range(tlb, (hugepd_t *)pud); |
| 234 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 235 | #endif |
| 236 | } while (pud++, addr = next, addr != end); |
| 237 | |
| 238 | start &= PGDIR_MASK; |
| 239 | if (start < floor) |
| 240 | return; |
| 241 | if (ceiling) { |
| 242 | ceiling &= PGDIR_MASK; |
| 243 | if (!ceiling) |
| 244 | return; |
| 245 | } |
| 246 | if (end - 1 > ceiling - 1) |
| 247 | return; |
| 248 | |
| 249 | pud = pud_offset(pgd, start); |
| 250 | pgd_clear(pgd); |
| 251 | pud_free_tlb(tlb, pud); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * This function frees user-level page tables of a process. |
| 256 | * |
| 257 | * Must be called with pagetable lock held. |
| 258 | */ |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 259 | void hugetlb_free_pgd_range(struct mmu_gather *tlb, |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 260 | unsigned long addr, unsigned long end, |
| 261 | unsigned long floor, unsigned long ceiling) |
| 262 | { |
| 263 | pgd_t *pgd; |
| 264 | unsigned long next; |
| 265 | unsigned long start; |
| 266 | |
| 267 | /* |
| 268 | * Comments below take from the normal free_pgd_range(). They |
| 269 | * apply here too. The tests against HUGEPD_MASK below are |
| 270 | * essential, because we *don't* test for this at the bottom |
| 271 | * level. Without them we'll attempt to free a hugepte table |
| 272 | * when we unmap just part of it, even if there are other |
| 273 | * active mappings using it. |
| 274 | * |
| 275 | * The next few lines have given us lots of grief... |
| 276 | * |
| 277 | * Why are we testing HUGEPD* at this top level? Because |
| 278 | * often there will be no work to do at all, and we'd prefer |
| 279 | * not to go all the way down to the bottom just to discover |
| 280 | * that. |
| 281 | * |
| 282 | * Why all these "- 1"s? Because 0 represents both the bottom |
| 283 | * of the address space and the top of it (using -1 for the |
| 284 | * top wouldn't help much: the masks would do the wrong thing). |
| 285 | * The rule is that addr 0 and floor 0 refer to the bottom of |
| 286 | * the address space, but end 0 and ceiling 0 refer to the top |
| 287 | * Comparisons need to use "end - 1" and "ceiling - 1" (though |
| 288 | * that end 0 case should be mythical). |
| 289 | * |
| 290 | * Wherever addr is brought up or ceiling brought down, we |
| 291 | * must be careful to reject "the opposite 0" before it |
| 292 | * confuses the subsequent tests. But what about where end is |
| 293 | * brought down by HUGEPD_SIZE below? no, end can't go down to |
| 294 | * 0 there. |
| 295 | * |
| 296 | * Whereas we round start (addr) and ceiling down, by different |
| 297 | * masks at different levels, in order to test whether a table |
| 298 | * now has no other vmas using it, so can be freed, we don't |
| 299 | * bother to round floor or end up - the tests don't need that. |
| 300 | */ |
| 301 | |
| 302 | addr &= HUGEPD_MASK; |
| 303 | if (addr < floor) { |
| 304 | addr += HUGEPD_SIZE; |
| 305 | if (!addr) |
| 306 | return; |
| 307 | } |
| 308 | if (ceiling) { |
| 309 | ceiling &= HUGEPD_MASK; |
| 310 | if (!ceiling) |
| 311 | return; |
| 312 | } |
| 313 | if (end - 1 > ceiling - 1) |
| 314 | end -= HUGEPD_SIZE; |
| 315 | if (addr > end - 1) |
| 316 | return; |
| 317 | |
| 318 | start = addr; |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 319 | pgd = pgd_offset(tlb->mm, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 320 | do { |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 321 | BUG_ON(get_slice_psize(tlb->mm, addr) != mmu_huge_psize); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 322 | next = pgd_addr_end(addr, end); |
| 323 | if (pgd_none_or_clear_bad(pgd)) |
| 324 | continue; |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 325 | hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 326 | } while (pgd++, addr = next, addr != end); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 327 | } |
| 328 | |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 329 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 330 | pte_t *ptep, pte_t pte) |
| 331 | { |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 332 | if (pte_present(*ptep)) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 333 | /* We open-code pte_clear because we need to pass the right |
Benjamin Herrenschmidt | a741e67 | 2007-04-10 17:09:37 +1000 | [diff] [blame] | 334 | * argument to hpte_need_flush (huge / !huge). Might not be |
| 335 | * necessary anymore if we make hpte_need_flush() get the |
| 336 | * page size from the slices |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 337 | */ |
Benjamin Herrenschmidt | a741e67 | 2007-04-10 17:09:37 +1000 | [diff] [blame] | 338 | pte_update(mm, addr & HPAGE_MASK, ptep, ~0UL, 1); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 339 | } |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 340 | *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 344 | pte_t *ptep) |
| 345 | { |
Benjamin Herrenschmidt | a741e67 | 2007-04-10 17:09:37 +1000 | [diff] [blame] | 346 | unsigned long old = pte_update(mm, addr, ptep, ~0UL, 1); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 347 | return __pte(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | struct page * |
| 351 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 352 | { |
| 353 | pte_t *ptep; |
| 354 | struct page *page; |
| 355 | |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 356 | if (get_slice_psize(mm, address) != mmu_huge_psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return ERR_PTR(-EINVAL); |
| 358 | |
| 359 | ptep = huge_pte_offset(mm, address); |
| 360 | page = pte_page(*ptep); |
| 361 | if (page) |
| 362 | page += (address % HPAGE_SIZE) / PAGE_SIZE; |
| 363 | |
| 364 | return page; |
| 365 | } |
| 366 | |
| 367 | int pmd_huge(pmd_t pmd) |
| 368 | { |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | struct page * |
| 373 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 374 | pmd_t *pmd, int write) |
| 375 | { |
| 376 | BUG(); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 382 | unsigned long len, unsigned long pgoff, |
| 383 | unsigned long flags) |
| 384 | { |
Benjamin Herrenschmidt | d0f13e3 | 2007-05-08 16:27:27 +1000 | [diff] [blame] | 385 | return slice_get_unmapped_area(addr, len, flags, |
| 386 | mmu_huge_psize, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 389 | /* |
| 390 | * Called by asm hashtable.S for doing lazy icache flush |
| 391 | */ |
| 392 | static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags, |
| 393 | pte_t pte, int trap) |
| 394 | { |
| 395 | struct page *page; |
| 396 | int i; |
| 397 | |
| 398 | if (!pfn_valid(pte_pfn(pte))) |
| 399 | return rflags; |
| 400 | |
| 401 | page = pte_page(pte); |
| 402 | |
| 403 | /* page is dirty */ |
| 404 | if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) { |
| 405 | if (trap == 0x400) { |
| 406 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) |
| 407 | __flush_dcache_icache(page_address(page+i)); |
| 408 | set_bit(PG_arch_1, &page->flags); |
| 409 | } else { |
| 410 | rflags |= HPTE_R_N; |
| 411 | } |
| 412 | } |
| 413 | return rflags; |
| 414 | } |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | int hash_huge_page(struct mm_struct *mm, unsigned long access, |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 417 | unsigned long ea, unsigned long vsid, int local, |
| 418 | unsigned long trap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
| 420 | pte_t *ptep; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 421 | unsigned long old_pte, new_pte; |
| 422 | unsigned long va, rflags, pa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | long slot; |
| 424 | int err = 1; |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 425 | int ssize = user_segment_size(ea); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | ptep = huge_pte_offset(mm, ea); |
| 428 | |
| 429 | /* Search the Linux page table for a match with va */ |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 430 | va = hpt_va(ea, vsid, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * If no pte found or not present, send the problem up to |
| 434 | * do_page_fault |
| 435 | */ |
| 436 | if (unlikely(!ptep || pte_none(*ptep))) |
| 437 | goto out; |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* |
| 440 | * Check the user's access rights to the page. If access should be |
| 441 | * prevented then send the problem up to do_page_fault. |
| 442 | */ |
| 443 | if (unlikely(access & ~pte_val(*ptep))) |
| 444 | goto out; |
| 445 | /* |
| 446 | * At this point, we have a pte (old_pte) which can be used to build |
| 447 | * or update an HPTE. There are 2 cases: |
| 448 | * |
| 449 | * 1. There is a valid (present) pte with no associated HPTE (this is |
| 450 | * the most common case) |
| 451 | * 2. There is a valid (present) pte with an associated HPTE. The |
| 452 | * current values of the pp bits in the HPTE prevent access |
| 453 | * because we are doing software DIRTY bit management and the |
| 454 | * page is currently not DIRTY. |
| 455 | */ |
| 456 | |
| 457 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 458 | do { |
| 459 | old_pte = pte_val(*ptep); |
| 460 | if (old_pte & _PAGE_BUSY) |
| 461 | goto out; |
Benjamin Herrenschmidt | 41743a4 | 2008-06-11 15:37:10 +1000 | [diff] [blame] | 462 | new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 463 | } while(old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 464 | old_pte, new_pte)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 466 | rflags = 0x2 | (!(new_pte & _PAGE_RW)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 468 | rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N); |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 469 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) |
| 470 | /* No CPU has hugepages but lacks no execute, so we |
| 471 | * don't need to worry about that case */ |
| 472 | rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte), |
| 473 | trap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | /* Check if pte already has an hpte (case 2) */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 476 | if (unlikely(old_pte & _PAGE_HASHPTE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | /* There MIGHT be an HPTE for this pte */ |
| 478 | unsigned long hash, slot; |
| 479 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 480 | hash = hpt_hash(va, HPAGE_SHIFT, ssize); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 481 | if (old_pte & _PAGE_F_SECOND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | hash = ~hash; |
| 483 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 484 | slot += (old_pte & _PAGE_F_GIX) >> 12; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Benjamin Herrenschmidt | 325c82a | 2005-12-08 16:51:44 +1100 | [diff] [blame] | 486 | if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 487 | ssize, local) == -1) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 488 | old_pte &= ~_PAGE_HPTEFLAGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 491 | if (likely(!(old_pte & _PAGE_HASHPTE))) { |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 492 | unsigned long hash = hpt_hash(va, HPAGE_SHIFT, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | unsigned long hpte_group; |
| 494 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 495 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | repeat: |
| 498 | hpte_group = ((hash & htab_hash_mask) * |
| 499 | HPTES_PER_GROUP) & ~0x7UL; |
| 500 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 501 | /* clear HPTE slot informations in new PTE */ |
Benjamin Herrenschmidt | 41743a4 | 2008-06-11 15:37:10 +1000 | [diff] [blame] | 502 | #ifdef CONFIG_PPC_64K_PAGES |
| 503 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HPTE_SUB0; |
| 504 | #else |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 505 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE; |
Benjamin Herrenschmidt | 41743a4 | 2008-06-11 15:37:10 +1000 | [diff] [blame] | 506 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | /* Add in WIMG bits */ |
Dave Kleikamp | 87e9ab1 | 2008-06-19 08:32:56 +1000 | [diff] [blame] | 508 | rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE | |
| 509 | _PAGE_COHERENT | _PAGE_GUARDED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 511 | /* Insert into the hash table, primary slot */ |
| 512 | slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 513 | mmu_huge_psize, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | /* Primary is full, try the secondary */ |
| 516 | if (unlikely(slot == -1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | hpte_group = ((~hash & htab_hash_mask) * |
| 518 | HPTES_PER_GROUP) & ~0x7UL; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 519 | slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, |
Benjamin Herrenschmidt | 67b1081 | 2005-09-23 13:24:07 -0700 | [diff] [blame] | 520 | HPTE_V_SECONDARY, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 521 | mmu_huge_psize, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (slot == -1) { |
| 523 | if (mftb() & 0x1) |
Benjamin Herrenschmidt | 67b1081 | 2005-09-23 13:24:07 -0700 | [diff] [blame] | 524 | hpte_group = ((hash & htab_hash_mask) * |
| 525 | HPTES_PER_GROUP)&~0x7UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | ppc_md.hpte_remove(hpte_group); |
| 528 | goto repeat; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (unlikely(slot == -2)) |
| 533 | panic("hash_huge_page: pte_insert failed\n"); |
| 534 | |
Ishizaki Kou | d649bd7 | 2007-01-12 09:54:39 +0900 | [diff] [blame] | 535 | new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 538 | /* |
Hugh Dickins | 01edcd8 | 2005-11-23 13:37:39 -0800 | [diff] [blame] | 539 | * No need to use ldarx/stdcx here |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 540 | */ |
| 541 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | err = 0; |
| 544 | |
| 545 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return err; |
| 547 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 548 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 549 | void set_huge_psize(int psize) |
| 550 | { |
| 551 | /* Check that it is a page size supported by the hardware and |
| 552 | * that it fits within pagetable limits. */ |
| 553 | if (mmu_psize_defs[psize].shift && mmu_psize_defs[psize].shift < SID_SHIFT && |
| 554 | (mmu_psize_defs[psize].shift > MIN_HUGEPTE_SHIFT || |
| 555 | mmu_psize_defs[psize].shift == HPAGE_SHIFT_64K)) { |
| 556 | HPAGE_SHIFT = mmu_psize_defs[psize].shift; |
| 557 | mmu_huge_psize = psize; |
| 558 | #ifdef CONFIG_PPC_64K_PAGES |
| 559 | hugepte_shift = (PMD_SHIFT-HPAGE_SHIFT); |
| 560 | #else |
| 561 | if (HPAGE_SHIFT == HPAGE_SHIFT_64K) |
| 562 | hugepte_shift = (PMD_SHIFT-HPAGE_SHIFT); |
| 563 | else |
| 564 | hugepte_shift = (PUD_SHIFT-HPAGE_SHIFT); |
| 565 | #endif |
| 566 | |
| 567 | } else |
| 568 | HPAGE_SHIFT = 0; |
| 569 | } |
| 570 | |
| 571 | static int __init hugepage_setup_sz(char *str) |
| 572 | { |
| 573 | unsigned long long size; |
| 574 | int mmu_psize = -1; |
| 575 | int shift; |
| 576 | |
| 577 | size = memparse(str, &str); |
| 578 | |
| 579 | shift = __ffs(size); |
| 580 | switch (shift) { |
| 581 | #ifndef CONFIG_PPC_64K_PAGES |
| 582 | case HPAGE_SHIFT_64K: |
| 583 | mmu_psize = MMU_PAGE_64K; |
| 584 | break; |
| 585 | #endif |
| 586 | case HPAGE_SHIFT_16M: |
| 587 | mmu_psize = MMU_PAGE_16M; |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | if (mmu_psize >=0 && mmu_psize_defs[mmu_psize].shift) |
| 592 | set_huge_psize(mmu_psize); |
| 593 | else |
| 594 | printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size); |
| 595 | |
| 596 | return 1; |
| 597 | } |
| 598 | __setup("hugepagesz=", hugepage_setup_sz); |
| 599 | |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 600 | static void zero_ctor(struct kmem_cache *cache, void *addr) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 601 | { |
| 602 | memset(addr, 0, kmem_cache_size(cache)); |
| 603 | } |
| 604 | |
| 605 | static int __init hugetlbpage_init(void) |
| 606 | { |
| 607 | if (!cpu_has_feature(CPU_FTR_16M_PAGE)) |
| 608 | return -ENODEV; |
| 609 | |
| 610 | huge_pgtable_cache = kmem_cache_create("hugepte_cache", |
| 611 | HUGEPTE_TABLE_SIZE, |
| 612 | HUGEPTE_TABLE_SIZE, |
Christoph Lameter | f0f3980 | 2007-05-06 14:49:58 -0700 | [diff] [blame] | 613 | 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 614 | zero_ctor); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 615 | if (! huge_pgtable_cache) |
| 616 | panic("hugetlbpage_init(): could not create hugepte cache\n"); |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | module_init(hugetlbpage_init); |