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