Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 2 | * PPC Huge TLB Page Support for Kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 David Gibson, IBM Corporation. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on the IA-32 version: |
| 8 | * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com> |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/mm.h> |
David Gibson | 883a3e5 | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 12 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/hugetlb.h> |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 15 | #include <linux/export.h> |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 16 | #include <linux/of_fdt.h> |
| 17 | #include <linux/memblock.h> |
| 18 | #include <linux/bootmem.h> |
Kumar Gala | 13020be | 2011-11-24 09:40:07 +0000 | [diff] [blame] | 19 | #include <linux/moduleparam.h> |
David Gibson | 883a3e5 | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 20 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/pgalloc.h> |
| 22 | #include <asm/tlb.h> |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 23 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jon Tollefson | 9122434 | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 25 | #define PAGE_SHIFT_64K 16 |
| 26 | #define PAGE_SHIFT_16M 24 |
| 27 | #define PAGE_SHIFT_16G 34 |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 28 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 29 | unsigned int HPAGE_SHIFT; |
| 30 | |
| 31 | /* |
| 32 | * Tracks gpages after the device tree is scanned and before the |
Becky Bruce | a614688 | 2011-10-10 10:50:43 +0000 | [diff] [blame] | 33 | * huge_boot_pages list is ready. On non-Freescale implementations, this is |
| 34 | * just used to track 16G pages and so is a single array. FSL-based |
| 35 | * implementations may have more than one gpage size, so we need multiple |
| 36 | * arrays |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 37 | */ |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 39 | #define MAX_NUMBER_GPAGES 128 |
| 40 | struct psize_gpages { |
| 41 | u64 gpage_list[MAX_NUMBER_GPAGES]; |
| 42 | unsigned int nr_gpages; |
| 43 | }; |
| 44 | static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT]; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 45 | #else |
| 46 | #define MAX_NUMBER_GPAGES 1024 |
| 47 | static u64 gpage_freearray[MAX_NUMBER_GPAGES]; |
| 48 | static unsigned nr_gpages; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 49 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 50 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 51 | #define hugepd_none(hpd) ((hpd).pd == 0) |
| 52 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 53 | pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea, unsigned *shift) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 54 | { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 55 | pgd_t *pg; |
| 56 | pud_t *pu; |
| 57 | pmd_t *pm; |
| 58 | hugepd_t *hpdp = NULL; |
| 59 | unsigned pdshift = PGDIR_SHIFT; |
| 60 | |
| 61 | if (shift) |
| 62 | *shift = 0; |
| 63 | |
| 64 | pg = pgdir + pgd_index(ea); |
| 65 | if (is_hugepd(pg)) { |
| 66 | hpdp = (hugepd_t *)pg; |
| 67 | } else if (!pgd_none(*pg)) { |
| 68 | pdshift = PUD_SHIFT; |
| 69 | pu = pud_offset(pg, ea); |
| 70 | if (is_hugepd(pu)) |
| 71 | hpdp = (hugepd_t *)pu; |
| 72 | else if (!pud_none(*pu)) { |
| 73 | pdshift = PMD_SHIFT; |
| 74 | pm = pmd_offset(pu, ea); |
| 75 | if (is_hugepd(pm)) |
| 76 | hpdp = (hugepd_t *)pm; |
| 77 | else if (!pmd_none(*pm)) { |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 78 | return pte_offset_kernel(pm, ea); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (!hpdp) |
| 84 | return NULL; |
| 85 | |
| 86 | if (shift) |
| 87 | *shift = hugepd_shift(*hpdp); |
| 88 | return hugepte_offset(hpdp, ea, pdshift); |
| 89 | } |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 90 | EXPORT_SYMBOL_GPL(find_linux_pte_or_hugepte); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 91 | |
| 92 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
| 93 | { |
| 94 | return find_linux_pte_or_hugepte(mm->pgd, addr, NULL); |
| 95 | } |
| 96 | |
| 97 | static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp, |
| 98 | unsigned long address, unsigned pdshift, unsigned pshift) |
| 99 | { |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 100 | struct kmem_cache *cachep; |
| 101 | pte_t *new; |
| 102 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 104 | int i; |
| 105 | int num_hugepd = 1 << (pshift - pdshift); |
| 106 | cachep = hugepte_cache; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 107 | #else |
| 108 | cachep = PGT_CACHE(pdshift - pshift); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
| 111 | new = kmem_cache_zalloc(cachep, GFP_KERNEL|__GFP_REPEAT); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 112 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 113 | BUG_ON(pshift > HUGEPD_SHIFT_MASK); |
| 114 | BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK); |
| 115 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 116 | if (! new) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | spin_lock(&mm->page_table_lock); |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 121 | /* |
| 122 | * We have multiple higher-level entries that point to the same |
| 123 | * actual pte location. Fill in each as we go and backtrack on error. |
| 124 | * We need all of these so the DTLB pgtable walk code can find the |
| 125 | * right higher-level entry without knowing if it's a hugepage or not. |
| 126 | */ |
| 127 | for (i = 0; i < num_hugepd; i++, hpdp++) { |
| 128 | if (unlikely(!hugepd_none(*hpdp))) |
| 129 | break; |
| 130 | else |
Aneesh Kumar K.V | cf9427b | 2013-04-28 09:37:29 +0000 | [diff] [blame^] | 131 | /* We use the old format for PPC_FSL_BOOK3E */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 132 | hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift; |
| 133 | } |
| 134 | /* If we bailed from the for loop early, an error occurred, clean up */ |
| 135 | if (i < num_hugepd) { |
| 136 | for (i = i - 1 ; i >= 0; i--, hpdp--) |
| 137 | hpdp->pd = 0; |
| 138 | kmem_cache_free(cachep, new); |
| 139 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 140 | #else |
| 141 | if (!hugepd_none(*hpdp)) |
| 142 | kmem_cache_free(cachep, new); |
Aneesh Kumar K.V | cf9427b | 2013-04-28 09:37:29 +0000 | [diff] [blame^] | 143 | else { |
| 144 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 145 | hpdp->pd = (unsigned long)new | |
| 146 | (shift_to_mmu_psize(pshift) << 2); |
| 147 | #else |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 148 | hpdp->pd = ((unsigned long)new & ~PD_HUGE) | pshift; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 149 | #endif |
Aneesh Kumar K.V | cf9427b | 2013-04-28 09:37:29 +0000 | [diff] [blame^] | 150 | } |
| 151 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 152 | spin_unlock(&mm->page_table_lock); |
| 153 | return 0; |
| 154 | } |
| 155 | |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 156 | /* |
| 157 | * These macros define how to determine which level of the page table holds |
| 158 | * the hpdp. |
| 159 | */ |
| 160 | #ifdef CONFIG_PPC_FSL_BOOK3E |
| 161 | #define HUGEPD_PGD_SHIFT PGDIR_SHIFT |
| 162 | #define HUGEPD_PUD_SHIFT PUD_SHIFT |
| 163 | #else |
| 164 | #define HUGEPD_PGD_SHIFT PUD_SHIFT |
| 165 | #define HUGEPD_PUD_SHIFT PMD_SHIFT |
| 166 | #endif |
| 167 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 168 | pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz) |
| 169 | { |
| 170 | pgd_t *pg; |
| 171 | pud_t *pu; |
| 172 | pmd_t *pm; |
| 173 | hugepd_t *hpdp = NULL; |
| 174 | unsigned pshift = __ffs(sz); |
| 175 | unsigned pdshift = PGDIR_SHIFT; |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 176 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 177 | addr &= ~(sz-1); |
| 178 | |
| 179 | pg = pgd_offset(mm, addr); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 180 | |
| 181 | if (pshift >= HUGEPD_PGD_SHIFT) { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 182 | hpdp = (hugepd_t *)pg; |
| 183 | } else { |
| 184 | pdshift = PUD_SHIFT; |
| 185 | pu = pud_alloc(mm, pg, addr); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 186 | if (pshift >= HUGEPD_PUD_SHIFT) { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 187 | hpdp = (hugepd_t *)pu; |
| 188 | } else { |
| 189 | pdshift = PMD_SHIFT; |
| 190 | pm = pmd_alloc(mm, pu, addr); |
| 191 | hpdp = (hugepd_t *)pm; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (!hpdp) |
| 196 | return NULL; |
| 197 | |
| 198 | BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp)); |
| 199 | |
| 200 | if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift)) |
| 201 | return NULL; |
| 202 | |
| 203 | return hugepte_offset(hpdp, addr, pdshift); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 204 | } |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 205 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 206 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 207 | /* Build list of addresses of gigantic pages. This function is used in early |
| 208 | * boot before the buddy or bootmem allocator is setup. |
| 209 | */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 210 | void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages) |
| 211 | { |
| 212 | unsigned int idx = shift_to_mmu_psize(__ffs(page_size)); |
| 213 | int i; |
| 214 | |
| 215 | if (addr == 0) |
| 216 | return; |
| 217 | |
| 218 | gpage_freearray[idx].nr_gpages = number_of_pages; |
| 219 | |
| 220 | for (i = 0; i < number_of_pages; i++) { |
| 221 | gpage_freearray[idx].gpage_list[i] = addr; |
| 222 | addr += page_size; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Moves the gigantic page addresses from the temporary list to the |
| 228 | * huge_boot_pages list. |
| 229 | */ |
| 230 | int alloc_bootmem_huge_page(struct hstate *hstate) |
| 231 | { |
| 232 | struct huge_bootmem_page *m; |
| 233 | int idx = shift_to_mmu_psize(hstate->order + PAGE_SHIFT); |
| 234 | int nr_gpages = gpage_freearray[idx].nr_gpages; |
| 235 | |
| 236 | if (nr_gpages == 0) |
| 237 | return 0; |
| 238 | |
| 239 | #ifdef CONFIG_HIGHMEM |
| 240 | /* |
| 241 | * If gpages can be in highmem we can't use the trick of storing the |
| 242 | * data structure in the page; allocate space for this |
| 243 | */ |
| 244 | m = alloc_bootmem(sizeof(struct huge_bootmem_page)); |
| 245 | m->phys = gpage_freearray[idx].gpage_list[--nr_gpages]; |
| 246 | #else |
| 247 | m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]); |
| 248 | #endif |
| 249 | |
| 250 | list_add(&m->list, &huge_boot_pages); |
| 251 | gpage_freearray[idx].nr_gpages = nr_gpages; |
| 252 | gpage_freearray[idx].gpage_list[nr_gpages] = 0; |
| 253 | m->hstate = hstate; |
| 254 | |
| 255 | return 1; |
| 256 | } |
| 257 | /* |
| 258 | * Scan the command line hugepagesz= options for gigantic pages; store those in |
| 259 | * a list that we use to allocate the memory once all options are parsed. |
| 260 | */ |
| 261 | |
| 262 | unsigned long gpage_npages[MMU_PAGE_COUNT]; |
| 263 | |
Paul Gortmaker | 8952812 | 2012-05-07 10:32:22 -0400 | [diff] [blame] | 264 | static int __init do_gpage_early_setup(char *param, char *val, |
| 265 | const char *unused) |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 266 | { |
| 267 | static phys_addr_t size; |
| 268 | unsigned long npages; |
| 269 | |
| 270 | /* |
| 271 | * The hugepagesz and hugepages cmdline options are interleaved. We |
| 272 | * use the size variable to keep track of whether or not this was done |
| 273 | * properly and skip over instances where it is incorrect. Other |
| 274 | * command-line parsing code will issue warnings, so we don't need to. |
| 275 | * |
| 276 | */ |
| 277 | if ((strcmp(param, "default_hugepagesz") == 0) || |
| 278 | (strcmp(param, "hugepagesz") == 0)) { |
| 279 | size = memparse(val, NULL); |
| 280 | } else if (strcmp(param, "hugepages") == 0) { |
| 281 | if (size != 0) { |
| 282 | if (sscanf(val, "%lu", &npages) <= 0) |
| 283 | npages = 0; |
| 284 | gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages; |
| 285 | size = 0; |
| 286 | } |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /* |
| 293 | * This function allocates physical space for pages that are larger than the |
| 294 | * buddy allocator can handle. We want to allocate these in highmem because |
| 295 | * the amount of lowmem is limited. This means that this function MUST be |
| 296 | * called before lowmem_end_addr is set up in MMU_init() in order for the lmb |
| 297 | * allocate to grab highmem. |
| 298 | */ |
| 299 | void __init reserve_hugetlb_gpages(void) |
| 300 | { |
| 301 | static __initdata char cmdline[COMMAND_LINE_SIZE]; |
| 302 | phys_addr_t size, base; |
| 303 | int i; |
| 304 | |
| 305 | strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE); |
Pawel Moll | 026cee0 | 2012-03-26 12:50:51 +1030 | [diff] [blame] | 306 | parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0, |
| 307 | &do_gpage_early_setup); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Walk gpage list in reverse, allocating larger page sizes first. |
| 311 | * Skip over unsupported sizes, or sizes that have 0 gpages allocated. |
| 312 | * When we reach the point in the list where pages are no longer |
| 313 | * considered gpages, we're done. |
| 314 | */ |
| 315 | for (i = MMU_PAGE_COUNT-1; i >= 0; i--) { |
| 316 | if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0) |
| 317 | continue; |
| 318 | else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT)) |
| 319 | break; |
| 320 | |
| 321 | size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i)); |
| 322 | base = memblock_alloc_base(size * gpage_npages[i], size, |
| 323 | MEMBLOCK_ALLOC_ANYWHERE); |
| 324 | add_gpage(base, size, gpage_npages[i]); |
| 325 | } |
| 326 | } |
| 327 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 328 | #else /* !PPC_FSL_BOOK3E */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 329 | |
| 330 | /* Build list of addresses of gigantic pages. This function is used in early |
| 331 | * boot before the buddy or bootmem allocator is setup. |
| 332 | */ |
| 333 | void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages) |
Jon Tollefson | 658013e | 2008-07-23 21:27:54 -0700 | [diff] [blame] | 334 | { |
| 335 | if (!addr) |
| 336 | return; |
| 337 | while (number_of_pages > 0) { |
| 338 | gpage_freearray[nr_gpages] = addr; |
| 339 | nr_gpages++; |
| 340 | number_of_pages--; |
| 341 | addr += page_size; |
| 342 | } |
| 343 | } |
| 344 | |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 345 | /* Moves the gigantic page addresses from the temporary list to the |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 346 | * huge_boot_pages list. |
| 347 | */ |
| 348 | int alloc_bootmem_huge_page(struct hstate *hstate) |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 349 | { |
| 350 | struct huge_bootmem_page *m; |
| 351 | if (nr_gpages == 0) |
| 352 | return 0; |
| 353 | m = phys_to_virt(gpage_freearray[--nr_gpages]); |
| 354 | gpage_freearray[nr_gpages] = 0; |
| 355 | list_add(&m->list, &huge_boot_pages); |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 356 | m->hstate = hstate; |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 357 | return 1; |
| 358 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 359 | #endif |
Jon Tollefson | ec4b2c0 | 2008-07-23 21:27:53 -0700 | [diff] [blame] | 360 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 361 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) |
| 362 | { |
| 363 | return 0; |
| 364 | } |
| 365 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 366 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 367 | #define HUGEPD_FREELIST_SIZE \ |
| 368 | ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t)) |
| 369 | |
| 370 | struct hugepd_freelist { |
| 371 | struct rcu_head rcu; |
| 372 | unsigned int index; |
| 373 | void *ptes[0]; |
| 374 | }; |
| 375 | |
| 376 | static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur); |
| 377 | |
| 378 | static void hugepd_free_rcu_callback(struct rcu_head *head) |
| 379 | { |
| 380 | struct hugepd_freelist *batch = |
| 381 | container_of(head, struct hugepd_freelist, rcu); |
| 382 | unsigned int i; |
| 383 | |
| 384 | for (i = 0; i < batch->index; i++) |
| 385 | kmem_cache_free(hugepte_cache, batch->ptes[i]); |
| 386 | |
| 387 | free_page((unsigned long)batch); |
| 388 | } |
| 389 | |
| 390 | static void hugepd_free(struct mmu_gather *tlb, void *hugepte) |
| 391 | { |
| 392 | struct hugepd_freelist **batchp; |
| 393 | |
| 394 | batchp = &__get_cpu_var(hugepd_freelist_cur); |
| 395 | |
| 396 | if (atomic_read(&tlb->mm->mm_users) < 2 || |
| 397 | cpumask_equal(mm_cpumask(tlb->mm), |
| 398 | cpumask_of(smp_processor_id()))) { |
| 399 | kmem_cache_free(hugepte_cache, hugepte); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | if (*batchp == NULL) { |
| 404 | *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC); |
| 405 | (*batchp)->index = 0; |
| 406 | } |
| 407 | |
| 408 | (*batchp)->ptes[(*batchp)->index++] = hugepte; |
| 409 | if ((*batchp)->index == HUGEPD_FREELIST_SIZE) { |
| 410 | call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback); |
| 411 | *batchp = NULL; |
| 412 | } |
| 413 | } |
| 414 | #endif |
| 415 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 416 | static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift, |
| 417 | unsigned long start, unsigned long end, |
| 418 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 419 | { |
| 420 | pte_t *hugepte = hugepd_page(*hpdp); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 421 | int i; |
| 422 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 423 | unsigned long pdmask = ~((1UL << pdshift) - 1); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 424 | unsigned int num_hugepd = 1; |
| 425 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 426 | #ifdef CONFIG_PPC_FSL_BOOK3E |
| 427 | /* Note: On fsl the hpdp may be the first of several */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 428 | num_hugepd = (1 << (hugepd_shift(*hpdp) - pdshift)); |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 429 | #else |
| 430 | unsigned int shift = hugepd_shift(*hpdp); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 431 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 432 | |
| 433 | start &= pdmask; |
| 434 | if (start < floor) |
| 435 | return; |
| 436 | if (ceiling) { |
| 437 | ceiling &= pdmask; |
| 438 | if (! ceiling) |
| 439 | return; |
| 440 | } |
| 441 | if (end - 1 > ceiling - 1) |
| 442 | return; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 443 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 444 | for (i = 0; i < num_hugepd; i++, hpdp++) |
| 445 | hpdp->pd = 0; |
| 446 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 447 | tlb->need_flush = 1; |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 448 | |
| 449 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 450 | hugepd_free(tlb, hugepte); |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 451 | #else |
| 452 | pgtable_free_tlb(tlb, hugepte, pdshift - shift); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 453 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 454 | } |
| 455 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 456 | static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud, |
| 457 | unsigned long addr, unsigned long end, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 458 | unsigned long floor, unsigned long ceiling) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 459 | { |
| 460 | pmd_t *pmd; |
| 461 | unsigned long next; |
| 462 | unsigned long start; |
| 463 | |
| 464 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 465 | do { |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 466 | pmd = pmd_offset(pud, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 467 | next = pmd_addr_end(addr, end); |
| 468 | if (pmd_none(*pmd)) |
| 469 | continue; |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 470 | #ifdef CONFIG_PPC_FSL_BOOK3E |
| 471 | /* |
| 472 | * Increment next by the size of the huge mapping since |
| 473 | * there may be more than one entry at this level for a |
| 474 | * single hugepage, but all of them point to |
| 475 | * the same kmem cache that holds the hugepte. |
| 476 | */ |
| 477 | next = addr + (1 << hugepd_shift(*(hugepd_t *)pmd)); |
| 478 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 479 | free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT, |
| 480 | addr, next, floor, ceiling); |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 481 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 482 | |
| 483 | start &= PUD_MASK; |
| 484 | if (start < floor) |
| 485 | return; |
| 486 | if (ceiling) { |
| 487 | ceiling &= PUD_MASK; |
| 488 | if (!ceiling) |
| 489 | return; |
| 490 | } |
| 491 | if (end - 1 > ceiling - 1) |
| 492 | return; |
| 493 | |
| 494 | pmd = pmd_offset(pud, start); |
| 495 | pud_clear(pud); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 496 | pmd_free_tlb(tlb, pmd, start); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 497 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 498 | |
| 499 | static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd, |
| 500 | unsigned long addr, unsigned long end, |
| 501 | unsigned long floor, unsigned long ceiling) |
| 502 | { |
| 503 | pud_t *pud; |
| 504 | unsigned long next; |
| 505 | unsigned long start; |
| 506 | |
| 507 | start = addr; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 508 | do { |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 509 | pud = pud_offset(pgd, addr); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 510 | next = pud_addr_end(addr, end); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 511 | if (!is_hugepd(pud)) { |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 512 | if (pud_none_or_clear_bad(pud)) |
| 513 | continue; |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 514 | hugetlb_free_pmd_range(tlb, pud, addr, next, floor, |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 515 | ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 516 | } else { |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 517 | #ifdef CONFIG_PPC_FSL_BOOK3E |
| 518 | /* |
| 519 | * Increment next by the size of the huge mapping since |
| 520 | * there may be more than one entry at this level for a |
| 521 | * single hugepage, but all of them point to |
| 522 | * the same kmem cache that holds the hugepte. |
| 523 | */ |
| 524 | next = addr + (1 << hugepd_shift(*(hugepd_t *)pud)); |
| 525 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 526 | free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT, |
| 527 | addr, next, floor, ceiling); |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 528 | } |
Becky Bruce | a1cd541 | 2011-10-10 10:50:39 +0000 | [diff] [blame] | 529 | } while (addr = next, addr != end); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 530 | |
| 531 | start &= PGDIR_MASK; |
| 532 | if (start < floor) |
| 533 | return; |
| 534 | if (ceiling) { |
| 535 | ceiling &= PGDIR_MASK; |
| 536 | if (!ceiling) |
| 537 | return; |
| 538 | } |
| 539 | if (end - 1 > ceiling - 1) |
| 540 | return; |
| 541 | |
| 542 | pud = pud_offset(pgd, start); |
| 543 | pgd_clear(pgd); |
Benjamin Herrenschmidt | 9e1b32c | 2009-07-22 15:44:28 +1000 | [diff] [blame] | 544 | pud_free_tlb(tlb, pud, start); |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * This function frees user-level page tables of a process. |
| 549 | * |
| 550 | * Must be called with pagetable lock held. |
| 551 | */ |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 552 | void hugetlb_free_pgd_range(struct mmu_gather *tlb, |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 553 | unsigned long addr, unsigned long end, |
| 554 | unsigned long floor, unsigned long ceiling) |
| 555 | { |
| 556 | pgd_t *pgd; |
| 557 | unsigned long next; |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 558 | |
| 559 | /* |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 560 | * Because there are a number of different possible pagetable |
| 561 | * layouts for hugepage ranges, we limit knowledge of how |
| 562 | * things should be laid out to the allocation path |
| 563 | * (huge_pte_alloc(), above). Everything else works out the |
| 564 | * structure as it goes from information in the hugepd |
| 565 | * pointers. That means that we can't here use the |
| 566 | * optimization used in the normal page free_pgd_range(), of |
| 567 | * checking whether we're actually covering a large enough |
| 568 | * range to have to do anything at the top level of the walk |
| 569 | * instead of at the bottom. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 570 | * |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 571 | * To make sense of this, you should probably go read the big |
| 572 | * block comment at the top of the normal free_pgd_range(), |
| 573 | * too. |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 574 | */ |
| 575 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 576 | do { |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 577 | next = pgd_addr_end(addr, end); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 578 | pgd = pgd_offset(tlb->mm, addr); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 579 | if (!is_hugepd(pgd)) { |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 580 | if (pgd_none_or_clear_bad(pgd)) |
| 581 | continue; |
| 582 | hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling); |
| 583 | } else { |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 584 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 585 | /* |
| 586 | * Increment next by the size of the huge mapping since |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 587 | * there may be more than one entry at the pgd level |
| 588 | * for a single hugepage, but all of them point to the |
| 589 | * same kmem cache that holds the hugepte. |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 590 | */ |
| 591 | next = addr + (1 << hugepd_shift(*(hugepd_t *)pgd)); |
| 592 | #endif |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 593 | free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT, |
| 594 | addr, next, floor, ceiling); |
David Gibson | 0b26425 | 2008-09-05 11:49:54 +1000 | [diff] [blame] | 595 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 596 | } while (addr = next, addr != end); |
David Gibson | e28f7fa | 2005-08-05 19:39:06 +1000 | [diff] [blame] | 597 | } |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | struct page * |
| 600 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 601 | { |
| 602 | pte_t *ptep; |
| 603 | struct page *page; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 604 | unsigned shift; |
| 605 | unsigned long mask; |
| 606 | |
| 607 | ptep = find_linux_pte_or_hugepte(mm->pgd, address, &shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 609 | /* Verify it is a huge page else bail. */ |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 610 | if (!ptep || !shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return ERR_PTR(-EINVAL); |
| 612 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 613 | mask = (1UL << shift) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | page = pte_page(*ptep); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 615 | if (page) |
| 616 | page += (address & mask) / PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | return page; |
| 619 | } |
| 620 | |
| 621 | int pmd_huge(pmd_t pmd) |
| 622 | { |
| 623 | return 0; |
| 624 | } |
| 625 | |
Andi Kleen | ceb8687 | 2008-07-23 21:27:50 -0700 | [diff] [blame] | 626 | int pud_huge(pud_t pud) |
| 627 | { |
| 628 | return 0; |
| 629 | } |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | struct page * |
| 632 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 633 | pmd_t *pmd, int write) |
| 634 | { |
| 635 | BUG(); |
| 636 | return NULL; |
| 637 | } |
| 638 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 639 | static noinline int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr, |
| 640 | unsigned long end, int write, struct page **pages, int *nr) |
| 641 | { |
| 642 | unsigned long mask; |
| 643 | unsigned long pte_end; |
Andrea Arcangeli | 3526741 | 2011-11-02 13:37:15 -0700 | [diff] [blame] | 644 | struct page *head, *page, *tail; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 645 | pte_t pte; |
| 646 | int refs; |
| 647 | |
| 648 | pte_end = (addr + sz) & ~(sz-1); |
| 649 | if (pte_end < end) |
| 650 | end = pte_end; |
| 651 | |
| 652 | pte = *ptep; |
| 653 | mask = _PAGE_PRESENT | _PAGE_USER; |
| 654 | if (write) |
| 655 | mask |= _PAGE_RW; |
| 656 | |
| 657 | if ((pte_val(pte) & mask) != mask) |
| 658 | return 0; |
| 659 | |
| 660 | /* hugepages are never "special" */ |
| 661 | VM_BUG_ON(!pfn_valid(pte_pfn(pte))); |
| 662 | |
| 663 | refs = 0; |
| 664 | head = pte_page(pte); |
| 665 | |
| 666 | page = head + ((addr & (sz-1)) >> PAGE_SHIFT); |
Andrea Arcangeli | 3526741 | 2011-11-02 13:37:15 -0700 | [diff] [blame] | 667 | tail = page; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 668 | do { |
| 669 | VM_BUG_ON(compound_head(page) != head); |
| 670 | pages[*nr] = page; |
| 671 | (*nr)++; |
| 672 | page++; |
| 673 | refs++; |
| 674 | } while (addr += PAGE_SIZE, addr != end); |
| 675 | |
| 676 | if (!page_cache_add_speculative(head, refs)) { |
| 677 | *nr -= refs; |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | if (unlikely(pte_val(pte) != pte_val(*ptep))) { |
| 682 | /* Could be optimized better */ |
Andrea Arcangeli | 8596468 | 2011-11-02 13:37:11 -0700 | [diff] [blame] | 683 | *nr -= refs; |
| 684 | while (refs--) |
Andrea Arcangeli | 405e44f | 2011-11-02 13:37:08 -0700 | [diff] [blame] | 685 | put_page(head); |
Andrea Arcangeli | cf592bf | 2011-11-02 13:37:19 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Any tail page need their mapcount reference taken before we |
| 691 | * return. |
| 692 | */ |
| 693 | while (refs--) { |
| 694 | if (PageTail(tail)) |
| 695 | get_huge_page_tail(tail); |
| 696 | tail++; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | return 1; |
| 700 | } |
| 701 | |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 702 | static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end, |
| 703 | unsigned long sz) |
| 704 | { |
| 705 | unsigned long __boundary = (addr + sz) & ~(sz-1); |
| 706 | return (__boundary - 1 < end - 1) ? __boundary : end; |
| 707 | } |
| 708 | |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 709 | int gup_hugepd(hugepd_t *hugepd, unsigned pdshift, |
| 710 | unsigned long addr, unsigned long end, |
| 711 | int write, struct page **pages, int *nr) |
| 712 | { |
| 713 | pte_t *ptep; |
| 714 | unsigned long sz = 1UL << hugepd_shift(*hugepd); |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 715 | unsigned long next; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 716 | |
| 717 | ptep = hugepte_offset(hugepd, addr, pdshift); |
| 718 | do { |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 719 | next = hugepte_addr_end(addr, end, sz); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 720 | if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr)) |
| 721 | return 0; |
David Gibson | 39adfa5 | 2009-11-23 20:03:40 +0000 | [diff] [blame] | 722 | } while (ptep++, addr = next, addr != end); |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 723 | |
| 724 | return 1; |
| 725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Becky Bruce | 7651295 | 2011-10-10 10:50:36 +0000 | [diff] [blame] | 727 | #ifdef CONFIG_PPC_MM_SLICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 729 | unsigned long len, unsigned long pgoff, |
| 730 | unsigned long flags) |
| 731 | { |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 732 | struct hstate *hstate = hstate_file(file); |
| 733 | int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate)); |
Brian King | 48f797d | 2008-12-04 04:07:54 +0000 | [diff] [blame] | 734 | |
Michel Lespinasse | 34d0717 | 2013-04-29 11:53:52 -0700 | [diff] [blame] | 735 | return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
Becky Bruce | 7651295 | 2011-10-10 10:50:36 +0000 | [diff] [blame] | 737 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 739 | unsigned long vma_mmu_pagesize(struct vm_area_struct *vma) |
| 740 | { |
Paul Mackerras | 25c29f9 | 2011-09-20 19:58:10 +0000 | [diff] [blame] | 741 | #ifdef CONFIG_PPC_MM_SLICES |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 742 | unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start); |
| 743 | |
| 744 | return 1UL << mmu_psize_to_shift(psize); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 745 | #else |
| 746 | if (!is_vm_hugetlb_page(vma)) |
| 747 | return PAGE_SIZE; |
| 748 | |
| 749 | return huge_page_size(hstate_vma(vma)); |
| 750 | #endif |
| 751 | } |
| 752 | |
| 753 | static inline bool is_power_of_4(unsigned long x) |
| 754 | { |
| 755 | if (is_power_of_2(x)) |
| 756 | return (__ilog2(x) % 2) ? false : true; |
| 757 | return false; |
Mel Gorman | 3340289 | 2009-01-06 14:38:54 -0800 | [diff] [blame] | 758 | } |
| 759 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 760 | static int __init add_huge_page_size(unsigned long long size) |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 761 | { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 762 | int shift = __ffs(size); |
| 763 | int mmu_psize; |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 764 | |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 765 | /* Check that it is a page size supported by the hardware and |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 766 | * that it fits within pagetable and slice limits. */ |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 767 | #ifdef CONFIG_PPC_FSL_BOOK3E |
| 768 | if ((size < PAGE_SIZE) || !is_power_of_4(size)) |
| 769 | return -EINVAL; |
| 770 | #else |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 771 | if (!is_power_of_2(size) |
| 772 | || (shift > SLICE_HIGH_SHIFT) || (shift <= PAGE_SHIFT)) |
| 773 | return -EINVAL; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 774 | #endif |
Jon Tollefson | 9122434 | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 775 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 776 | if ((mmu_psize = shift_to_mmu_psize(shift)) < 0) |
| 777 | return -EINVAL; |
| 778 | |
| 779 | #ifdef CONFIG_SPU_FS_64K_LS |
| 780 | /* Disable support for 64K huge pages when 64K SPU local store |
| 781 | * support is enabled as the current implementation conflicts. |
| 782 | */ |
| 783 | if (shift == PAGE_SHIFT_64K) |
| 784 | return -EINVAL; |
| 785 | #endif /* CONFIG_SPU_FS_64K_LS */ |
| 786 | |
| 787 | BUG_ON(mmu_psize_defs[mmu_psize].shift != shift); |
| 788 | |
| 789 | /* Return if huge page size has already been setup */ |
| 790 | if (size_to_hstate(size)) |
| 791 | return 0; |
| 792 | |
| 793 | hugetlb_add_hstate(shift - PAGE_SHIFT); |
| 794 | |
| 795 | return 0; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static int __init hugepage_setup_sz(char *str) |
| 799 | { |
| 800 | unsigned long long size; |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 801 | |
| 802 | size = memparse(str, &str); |
| 803 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 804 | if (add_huge_page_size(size) != 0) |
Jon Tollefson | 4ec161c | 2008-01-04 09:59:50 +1100 | [diff] [blame] | 805 | printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size); |
| 806 | |
| 807 | return 1; |
| 808 | } |
| 809 | __setup("hugepagesz=", hugepage_setup_sz); |
| 810 | |
Becky Bruce | 881fde1 | 2011-10-10 10:50:40 +0000 | [diff] [blame] | 811 | #ifdef CONFIG_PPC_FSL_BOOK3E |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 812 | struct kmem_cache *hugepte_cache; |
| 813 | static int __init hugetlbpage_init(void) |
| 814 | { |
| 815 | int psize; |
| 816 | |
| 817 | for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) { |
| 818 | unsigned shift; |
| 819 | |
| 820 | if (!mmu_psize_defs[psize].shift) |
| 821 | continue; |
| 822 | |
| 823 | shift = mmu_psize_to_shift(psize); |
| 824 | |
| 825 | /* Don't treat normal page sizes as huge... */ |
| 826 | if (shift != PAGE_SHIFT) |
| 827 | if (add_huge_page_size(1ULL << shift) < 0) |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Create a kmem cache for hugeptes. The bottom bits in the pte have |
| 833 | * size information encoded in them, so align them to allow this |
| 834 | */ |
| 835 | hugepte_cache = kmem_cache_create("hugepte-cache", sizeof(pte_t), |
| 836 | HUGEPD_SHIFT_MASK + 1, 0, NULL); |
| 837 | if (hugepte_cache == NULL) |
| 838 | panic("%s: Unable to create kmem cache for hugeptes\n", |
| 839 | __func__); |
| 840 | |
| 841 | /* Default hpage size = 4M */ |
| 842 | if (mmu_psize_defs[MMU_PAGE_4M].shift) |
| 843 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift; |
| 844 | else |
| 845 | panic("%s: Unable to set default huge page size\n", __func__); |
| 846 | |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | #else |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 851 | static int __init hugetlbpage_init(void) |
| 852 | { |
David Gibson | a4fe3ce | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 853 | int psize; |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 854 | |
Matt Evans | 44ae3ab | 2011-04-06 19:48:50 +0000 | [diff] [blame] | 855 | if (!mmu_has_feature(MMU_FTR_16M_PAGE)) |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 856 | return -ENODEV; |
Benjamin Herrenschmidt | 00df438 | 2008-07-28 16:13:18 +1000 | [diff] [blame] | 857 | |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 858 | for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) { |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 859 | unsigned shift; |
| 860 | unsigned pdshift; |
| 861 | |
| 862 | if (!mmu_psize_defs[psize].shift) |
| 863 | continue; |
| 864 | |
| 865 | shift = mmu_psize_to_shift(psize); |
| 866 | |
| 867 | if (add_huge_page_size(1ULL << shift) < 0) |
| 868 | continue; |
| 869 | |
| 870 | if (shift < PMD_SHIFT) |
| 871 | pdshift = PMD_SHIFT; |
| 872 | else if (shift < PUD_SHIFT) |
| 873 | pdshift = PUD_SHIFT; |
| 874 | else |
| 875 | pdshift = PGDIR_SHIFT; |
| 876 | |
| 877 | pgtable_cache_add(pdshift - shift, NULL); |
| 878 | if (!PGT_CACHE(pdshift - shift)) |
| 879 | panic("hugetlbpage_init(): could not create " |
| 880 | "pgtable cache for %d bit pagesize\n", shift); |
Jon Tollefson | 0d9ea75 | 2008-07-23 21:27:56 -0700 | [diff] [blame] | 881 | } |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 882 | |
David Gibson | d1837cb | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 883 | /* Set default large page size. Currently, we pick 16M or 1M |
| 884 | * depending on what is available |
| 885 | */ |
| 886 | if (mmu_psize_defs[MMU_PAGE_16M].shift) |
| 887 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift; |
| 888 | else if (mmu_psize_defs[MMU_PAGE_1M].shift) |
| 889 | HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift; |
| 890 | |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 891 | return 0; |
| 892 | } |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 893 | #endif |
David Gibson | f10a04c | 2006-04-28 15:02:51 +1000 | [diff] [blame] | 894 | module_init(hugetlbpage_init); |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 895 | |
| 896 | void flush_dcache_icache_hugepage(struct page *page) |
| 897 | { |
| 898 | int i; |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 899 | void *start; |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 900 | |
| 901 | BUG_ON(!PageCompound(page)); |
| 902 | |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 903 | for (i = 0; i < (1UL << compound_order(page)); i++) { |
| 904 | if (!PageHighMem(page)) { |
| 905 | __flush_dcache_icache(page_address(page+i)); |
| 906 | } else { |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 907 | start = kmap_atomic(page+i); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 908 | __flush_dcache_icache(start); |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 909 | kunmap_atomic(start); |
Becky Bruce | 41151e7 | 2011-06-28 09:54:48 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
David Gibson | 0895ecd | 2009-10-26 19:24:31 +0000 | [diff] [blame] | 912 | } |