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