Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PowerPC64 port by Mike Corrigan and Dave Engebretsen |
| 3 | * {mikejc|engebret}@us.ibm.com |
| 4 | * |
| 5 | * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com> |
| 6 | * |
| 7 | * SMP scalability work: |
| 8 | * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM |
| 9 | * |
| 10 | * Module name: htab.c |
| 11 | * |
| 12 | * Description: |
| 13 | * PowerPC Hashed Page Table functions |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | */ |
| 20 | |
| 21 | #undef DEBUG |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 22 | #undef DEBUG_LOW |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/proc_fs.h> |
| 28 | #include <linux/stat.h> |
| 29 | #include <linux/sysctl.h> |
| 30 | #include <linux/ctype.h> |
| 31 | #include <linux/cache.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/signal.h> |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/processor.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/mmu.h> |
| 38 | #include <asm/mmu_context.h> |
| 39 | #include <asm/page.h> |
| 40 | #include <asm/types.h> |
| 41 | #include <asm/system.h> |
| 42 | #include <asm/uaccess.h> |
| 43 | #include <asm/machdep.h> |
| 44 | #include <asm/lmb.h> |
| 45 | #include <asm/abs_addr.h> |
| 46 | #include <asm/tlbflush.h> |
| 47 | #include <asm/io.h> |
| 48 | #include <asm/eeh.h> |
| 49 | #include <asm/tlb.h> |
| 50 | #include <asm/cacheflush.h> |
| 51 | #include <asm/cputable.h> |
| 52 | #include <asm/abs_addr.h> |
| 53 | #include <asm/sections.h> |
| 54 | |
| 55 | #ifdef DEBUG |
| 56 | #define DBG(fmt...) udbg_printf(fmt) |
| 57 | #else |
| 58 | #define DBG(fmt...) |
| 59 | #endif |
| 60 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 61 | #ifdef DEBUG_LOW |
| 62 | #define DBG_LOW(fmt...) udbg_printf(fmt) |
| 63 | #else |
| 64 | #define DBG_LOW(fmt...) |
| 65 | #endif |
| 66 | |
| 67 | #define KB (1024) |
| 68 | #define MB (1024*KB) |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Note: pte --> Linux PTE |
| 72 | * HPTE --> PowerPC Hashed Page Table Entry |
| 73 | * |
| 74 | * Execution context: |
| 75 | * htab_initialize is called with the MMU off (of course), but |
| 76 | * the kernel has been copied down to zero so it can directly |
| 77 | * reference global data. At this point it is very difficult |
| 78 | * to print debug info. |
| 79 | * |
| 80 | */ |
| 81 | |
| 82 | #ifdef CONFIG_U3_DART |
| 83 | extern unsigned long dart_tablebase; |
| 84 | #endif /* CONFIG_U3_DART */ |
| 85 | |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 86 | static unsigned long _SDR1; |
| 87 | struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT]; |
| 88 | |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 89 | hpte_t *htab_address; |
Michael Ellerman | 337a712 | 2006-02-21 17:22:55 +1100 | [diff] [blame] | 90 | unsigned long htab_size_bytes; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 91 | unsigned long htab_hash_mask; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 92 | int mmu_linear_psize = MMU_PAGE_4K; |
| 93 | int mmu_virtual_psize = MMU_PAGE_4K; |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 94 | int mmu_vmalloc_psize = MMU_PAGE_4K; |
| 95 | int mmu_io_psize = MMU_PAGE_4K; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 96 | #ifdef CONFIG_HUGETLB_PAGE |
| 97 | int mmu_huge_psize = MMU_PAGE_16M; |
| 98 | unsigned int HPAGE_SHIFT; |
| 99 | #endif |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 100 | #ifdef CONFIG_PPC_64K_PAGES |
| 101 | int mmu_ci_restrictions; |
| 102 | #endif |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 103 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 104 | static u8 *linear_map_hash_slots; |
| 105 | static unsigned long linear_map_hash_count; |
Michael Ellerman | ed16669 | 2007-04-18 11:50:09 +1000 | [diff] [blame] | 106 | static DEFINE_SPINLOCK(linear_map_hash_lock); |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 107 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 109 | /* There are definitions of page sizes arrays to be used when none |
| 110 | * is provided by the firmware. |
| 111 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 113 | /* Pre-POWER4 CPUs (4k pages only) |
| 114 | */ |
| 115 | struct mmu_psize_def mmu_psize_defaults_old[] = { |
| 116 | [MMU_PAGE_4K] = { |
| 117 | .shift = 12, |
| 118 | .sllp = 0, |
| 119 | .penc = 0, |
| 120 | .avpnm = 0, |
| 121 | .tlbiel = 0, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | /* POWER4, GPUL, POWER5 |
| 126 | * |
| 127 | * Support for 16Mb large pages |
| 128 | */ |
| 129 | struct mmu_psize_def mmu_psize_defaults_gp[] = { |
| 130 | [MMU_PAGE_4K] = { |
| 131 | .shift = 12, |
| 132 | .sllp = 0, |
| 133 | .penc = 0, |
| 134 | .avpnm = 0, |
| 135 | .tlbiel = 1, |
| 136 | }, |
| 137 | [MMU_PAGE_16M] = { |
| 138 | .shift = 24, |
| 139 | .sllp = SLB_VSID_L, |
| 140 | .penc = 0, |
| 141 | .avpnm = 0x1UL, |
| 142 | .tlbiel = 0, |
| 143 | }, |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | int htab_bolt_mapping(unsigned long vstart, unsigned long vend, |
| 148 | unsigned long pstart, unsigned long mode, int psize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 150 | unsigned long vaddr, paddr; |
| 151 | unsigned int step, shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | unsigned long tmp_mode; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 153 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 155 | shift = mmu_psize_defs[psize].shift; |
| 156 | step = 1 << shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 158 | for (vaddr = vstart, paddr = pstart; vaddr < vend; |
| 159 | vaddr += step, paddr += step) { |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 160 | unsigned long hash, hpteg; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 161 | unsigned long vsid = get_kernel_vsid(vaddr); |
| 162 | unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | tmp_mode = mode; |
| 165 | |
| 166 | /* Make non-kernel text non-executable */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 167 | if (!in_kernel_text(vaddr)) |
| 168 | tmp_mode = mode | HPTE_R_N; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 170 | hash = hpt_hash(va, shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 172 | |
Michael Ellerman | c30a4df | 2006-06-23 18:16:39 +1000 | [diff] [blame] | 173 | DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert); |
| 174 | |
| 175 | BUG_ON(!ppc_md.hpte_insert); |
| 176 | ret = ppc_md.hpte_insert(hpteg, va, paddr, |
| 177 | tmp_mode, HPTE_V_BOLTED, psize); |
| 178 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 179 | if (ret < 0) |
| 180 | break; |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 181 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 182 | if ((paddr >> PAGE_SHIFT) < linear_map_hash_count) |
| 183 | linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80; |
| 184 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 186 | return ret < 0 ? ret : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 189 | static int __init htab_dt_scan_page_sizes(unsigned long node, |
| 190 | const char *uname, int depth, |
| 191 | void *data) |
| 192 | { |
| 193 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 194 | u32 *prop; |
| 195 | unsigned long size = 0; |
| 196 | |
| 197 | /* We are scanning "cpu" nodes only */ |
| 198 | if (type == NULL || strcmp(type, "cpu") != 0) |
| 199 | return 0; |
| 200 | |
| 201 | prop = (u32 *)of_get_flat_dt_prop(node, |
| 202 | "ibm,segment-page-sizes", &size); |
| 203 | if (prop != NULL) { |
| 204 | DBG("Page sizes from device-tree:\n"); |
| 205 | size /= 4; |
| 206 | cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE); |
| 207 | while(size > 0) { |
| 208 | unsigned int shift = prop[0]; |
| 209 | unsigned int slbenc = prop[1]; |
| 210 | unsigned int lpnum = prop[2]; |
| 211 | unsigned int lpenc = 0; |
| 212 | struct mmu_psize_def *def; |
| 213 | int idx = -1; |
| 214 | |
| 215 | size -= 3; prop += 3; |
| 216 | while(size > 0 && lpnum) { |
| 217 | if (prop[0] == shift) |
| 218 | lpenc = prop[1]; |
| 219 | prop += 2; size -= 2; |
| 220 | lpnum--; |
| 221 | } |
| 222 | switch(shift) { |
| 223 | case 0xc: |
| 224 | idx = MMU_PAGE_4K; |
| 225 | break; |
| 226 | case 0x10: |
| 227 | idx = MMU_PAGE_64K; |
| 228 | break; |
| 229 | case 0x14: |
| 230 | idx = MMU_PAGE_1M; |
| 231 | break; |
| 232 | case 0x18: |
| 233 | idx = MMU_PAGE_16M; |
| 234 | cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE; |
| 235 | break; |
| 236 | case 0x22: |
| 237 | idx = MMU_PAGE_16G; |
| 238 | break; |
| 239 | } |
| 240 | if (idx < 0) |
| 241 | continue; |
| 242 | def = &mmu_psize_defs[idx]; |
| 243 | def->shift = shift; |
| 244 | if (shift <= 23) |
| 245 | def->avpnm = 0; |
| 246 | else |
| 247 | def->avpnm = (1 << (shift - 23)) - 1; |
| 248 | def->sllp = slbenc; |
| 249 | def->penc = lpenc; |
| 250 | /* We don't know for sure what's up with tlbiel, so |
| 251 | * for now we only set it for 4K and 64K pages |
| 252 | */ |
| 253 | if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K) |
| 254 | def->tlbiel = 1; |
| 255 | else |
| 256 | def->tlbiel = 0; |
| 257 | |
| 258 | DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, " |
| 259 | "tlbiel=%d, penc=%d\n", |
| 260 | idx, shift, def->sllp, def->avpnm, def->tlbiel, |
| 261 | def->penc); |
| 262 | } |
| 263 | return 1; |
| 264 | } |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static void __init htab_init_page_sizes(void) |
| 270 | { |
| 271 | int rc; |
| 272 | |
| 273 | /* Default to 4K pages only */ |
| 274 | memcpy(mmu_psize_defs, mmu_psize_defaults_old, |
| 275 | sizeof(mmu_psize_defaults_old)); |
| 276 | |
| 277 | /* |
| 278 | * Try to find the available page sizes in the device-tree |
| 279 | */ |
| 280 | rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL); |
| 281 | if (rc != 0) /* Found */ |
| 282 | goto found; |
| 283 | |
| 284 | /* |
| 285 | * Not in the device-tree, let's fallback on known size |
| 286 | * list for 16M capable GP & GR |
| 287 | */ |
Stephen Rothwell | 0470466 | 2006-11-30 11:46:22 +1100 | [diff] [blame] | 288 | if (cpu_has_feature(CPU_FTR_16M_PAGE)) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 289 | memcpy(mmu_psize_defs, mmu_psize_defaults_gp, |
| 290 | sizeof(mmu_psize_defaults_gp)); |
| 291 | found: |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 292 | #ifndef CONFIG_DEBUG_PAGEALLOC |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 293 | /* |
| 294 | * Pick a size for the linear mapping. Currently, we only support |
| 295 | * 16M, 1M and 4K which is the default |
| 296 | */ |
| 297 | if (mmu_psize_defs[MMU_PAGE_16M].shift) |
| 298 | mmu_linear_psize = MMU_PAGE_16M; |
| 299 | else if (mmu_psize_defs[MMU_PAGE_1M].shift) |
| 300 | mmu_linear_psize = MMU_PAGE_1M; |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 301 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 302 | |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 303 | #ifdef CONFIG_PPC_64K_PAGES |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 304 | /* |
| 305 | * Pick a size for the ordinary pages. Default is 4K, we support |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 306 | * 64K for user mappings and vmalloc if supported by the processor. |
| 307 | * We only use 64k for ioremap if the processor |
| 308 | * (and firmware) support cache-inhibited large pages. |
| 309 | * If not, we use 4k and set mmu_ci_restrictions so that |
| 310 | * hash_page knows to switch processes that use cache-inhibited |
| 311 | * mappings to 4k pages. |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 312 | */ |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 313 | if (mmu_psize_defs[MMU_PAGE_64K].shift) { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 314 | mmu_virtual_psize = MMU_PAGE_64K; |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 315 | mmu_vmalloc_psize = MMU_PAGE_64K; |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 316 | if (mmu_linear_psize == MMU_PAGE_4K) |
| 317 | mmu_linear_psize = MMU_PAGE_64K; |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 318 | if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) |
| 319 | mmu_io_psize = MMU_PAGE_64K; |
| 320 | else |
| 321 | mmu_ci_restrictions = 1; |
| 322 | } |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 323 | #endif /* CONFIG_PPC_64K_PAGES */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 324 | |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 325 | printk(KERN_DEBUG "Page orders: linear mapping = %d, " |
| 326 | "virtual = %d, io = %d\n", |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 327 | mmu_psize_defs[mmu_linear_psize].shift, |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 328 | mmu_psize_defs[mmu_virtual_psize].shift, |
| 329 | mmu_psize_defs[mmu_io_psize].shift); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 330 | |
| 331 | #ifdef CONFIG_HUGETLB_PAGE |
| 332 | /* Init large page size. Currently, we pick 16M or 1M depending |
| 333 | * on what is available |
| 334 | */ |
| 335 | if (mmu_psize_defs[MMU_PAGE_16M].shift) |
| 336 | mmu_huge_psize = MMU_PAGE_16M; |
David Gibson | 7d24f0b | 2005-11-07 00:57:52 -0800 | [diff] [blame] | 337 | /* With 4k/4level pagetables, we can't (for now) cope with a |
| 338 | * huge page size < PMD_SIZE */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 339 | else if (mmu_psize_defs[MMU_PAGE_1M].shift) |
| 340 | mmu_huge_psize = MMU_PAGE_1M; |
| 341 | |
| 342 | /* Calculate HPAGE_SHIFT and sanity check it */ |
David Gibson | 7d24f0b | 2005-11-07 00:57:52 -0800 | [diff] [blame] | 343 | if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT && |
| 344 | mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 345 | HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift; |
| 346 | else |
| 347 | HPAGE_SHIFT = 0; /* No huge pages dude ! */ |
| 348 | #endif /* CONFIG_HUGETLB_PAGE */ |
| 349 | } |
| 350 | |
| 351 | static int __init htab_dt_scan_pftsize(unsigned long node, |
| 352 | const char *uname, int depth, |
| 353 | void *data) |
| 354 | { |
| 355 | char *type = of_get_flat_dt_prop(node, "device_type", NULL); |
| 356 | u32 *prop; |
| 357 | |
| 358 | /* We are scanning "cpu" nodes only */ |
| 359 | if (type == NULL || strcmp(type, "cpu") != 0) |
| 360 | return 0; |
| 361 | |
| 362 | prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL); |
| 363 | if (prop != NULL) { |
| 364 | /* pft_size[0] is the NUMA CEC cookie */ |
| 365 | ppc64_pft_size = prop[1]; |
| 366 | return 1; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static unsigned long __init htab_get_table_size(void) |
Paul Mackerras | 3eac8c6 | 2005-10-12 16:58:53 +1000 | [diff] [blame] | 372 | { |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 373 | unsigned long mem_size, rnd_mem_size, pteg_count; |
Paul Mackerras | 3eac8c6 | 2005-10-12 16:58:53 +1000 | [diff] [blame] | 374 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 375 | /* If hash size isn't already provided by the platform, we try to |
Adrian Bunk | 943ffb5 | 2006-01-10 00:10:13 +0100 | [diff] [blame] | 376 | * retrieve it from the device-tree. If it's not there neither, we |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 377 | * calculate it now based on the total RAM size |
Paul Mackerras | 3eac8c6 | 2005-10-12 16:58:53 +1000 | [diff] [blame] | 378 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 379 | if (ppc64_pft_size == 0) |
| 380 | of_scan_flat_dt(htab_dt_scan_pftsize, NULL); |
Paul Mackerras | 3eac8c6 | 2005-10-12 16:58:53 +1000 | [diff] [blame] | 381 | if (ppc64_pft_size) |
| 382 | return 1UL << ppc64_pft_size; |
| 383 | |
| 384 | /* round mem_size up to next power of 2 */ |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 385 | mem_size = lmb_phys_mem_size(); |
| 386 | rnd_mem_size = 1UL << __ilog2(mem_size); |
| 387 | if (rnd_mem_size < mem_size) |
Paul Mackerras | 3eac8c6 | 2005-10-12 16:58:53 +1000 | [diff] [blame] | 388 | rnd_mem_size <<= 1; |
| 389 | |
| 390 | /* # pages / 2 */ |
| 391 | pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11); |
| 392 | |
| 393 | return pteg_count << 7; |
| 394 | } |
| 395 | |
Mike Kravetz | 54b7924 | 2005-11-07 16:25:48 -0800 | [diff] [blame] | 396 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 397 | void create_section_mapping(unsigned long start, unsigned long end) |
| 398 | { |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 399 | BUG_ON(htab_bolt_mapping(start, end, __pa(start), |
Mike Kravetz | 54b7924 | 2005-11-07 16:25:48 -0800 | [diff] [blame] | 400 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX, |
| 401 | mmu_linear_psize)); |
| 402 | } |
| 403 | #endif /* CONFIG_MEMORY_HOTPLUG */ |
| 404 | |
Michael Ellerman | 7d0daae | 2006-06-23 18:16:38 +1000 | [diff] [blame] | 405 | static inline void make_bl(unsigned int *insn_addr, void *func) |
| 406 | { |
| 407 | unsigned long funcp = *((unsigned long *)func); |
| 408 | int offset = funcp - (unsigned long)insn_addr; |
| 409 | |
| 410 | *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc)); |
| 411 | flush_icache_range((unsigned long)insn_addr, 4+ |
| 412 | (unsigned long)insn_addr); |
| 413 | } |
| 414 | |
| 415 | static void __init htab_finish_init(void) |
| 416 | { |
| 417 | extern unsigned int *htab_call_hpte_insert1; |
| 418 | extern unsigned int *htab_call_hpte_insert2; |
| 419 | extern unsigned int *htab_call_hpte_remove; |
| 420 | extern unsigned int *htab_call_hpte_updatepp; |
| 421 | |
| 422 | #ifdef CONFIG_PPC_64K_PAGES |
| 423 | extern unsigned int *ht64_call_hpte_insert1; |
| 424 | extern unsigned int *ht64_call_hpte_insert2; |
| 425 | extern unsigned int *ht64_call_hpte_remove; |
| 426 | extern unsigned int *ht64_call_hpte_updatepp; |
| 427 | |
| 428 | make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert); |
| 429 | make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert); |
| 430 | make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove); |
| 431 | make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp); |
| 432 | #endif /* CONFIG_PPC_64K_PAGES */ |
| 433 | |
| 434 | make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert); |
| 435 | make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert); |
| 436 | make_bl(htab_call_hpte_remove, ppc_md.hpte_remove); |
| 437 | make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp); |
| 438 | } |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | void __init htab_initialize(void) |
| 441 | { |
Michael Ellerman | 337a712 | 2006-02-21 17:22:55 +1100 | [diff] [blame] | 442 | unsigned long table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | unsigned long pteg_count; |
| 444 | unsigned long mode_rw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | unsigned long base = 0, size = 0; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 446 | int i; |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | extern unsigned long tce_alloc_start, tce_alloc_end; |
| 449 | |
| 450 | DBG(" -> htab_initialize()\n"); |
| 451 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 452 | /* Initialize page sizes */ |
| 453 | htab_init_page_sizes(); |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Calculate the required size of the htab. We want the number of |
| 457 | * PTEGs to equal one half the number of real pages. |
| 458 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 459 | htab_size_bytes = htab_get_table_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | pteg_count = htab_size_bytes >> 7; |
| 461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | htab_hash_mask = pteg_count - 1; |
| 463 | |
Michael Ellerman | 57cfb81 | 2006-03-21 20:45:59 +1100 | [diff] [blame] | 464 | if (firmware_has_feature(FW_FEATURE_LPAR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* Using a hypervisor which owns the htab */ |
| 466 | htab_address = NULL; |
| 467 | _SDR1 = 0; |
| 468 | } else { |
| 469 | /* Find storage for the HPT. Must be contiguous in |
| 470 | * the absolute address space. |
| 471 | */ |
| 472 | table = lmb_alloc(htab_size_bytes, htab_size_bytes); |
| 473 | |
| 474 | DBG("Hash table allocated at %lx, size: %lx\n", table, |
| 475 | htab_size_bytes); |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | htab_address = abs_to_virt(table); |
| 478 | |
| 479 | /* htab absolute addr + encoded htabsize */ |
| 480 | _SDR1 = table + __ilog2(pteg_count) - 11; |
| 481 | |
| 482 | /* Initialize the HPT with no entries */ |
| 483 | memset((void *)table, 0, htab_size_bytes); |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 484 | |
| 485 | /* Set SDR1 */ |
| 486 | mtspr(SPRN_SDR1, _SDR1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Anton Blanchard | 515bae9 | 2005-06-21 17:15:55 -0700 | [diff] [blame] | 489 | mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 491 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 492 | linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT; |
| 493 | linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count, |
| 494 | 1, lmb.rmo_size)); |
| 495 | memset(linear_map_hash_slots, 0, linear_map_hash_count); |
| 496 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* On U3 based machines, we need to reserve the DART area and |
| 499 | * _NOT_ map it to avoid cache paradoxes as it's remapped non |
| 500 | * cacheable later on |
| 501 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
| 503 | /* create bolted the linear mapping in the hash table */ |
| 504 | for (i=0; i < lmb.memory.cnt; i++) { |
Michael Ellerman | b5666f7 | 2005-12-05 10:24:33 -0600 | [diff] [blame] | 505 | base = (unsigned long)__va(lmb.memory.region[i].base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | size = lmb.memory.region[i].size; |
| 507 | |
| 508 | DBG("creating mapping for region: %lx : %lx\n", base, size); |
| 509 | |
| 510 | #ifdef CONFIG_U3_DART |
| 511 | /* Do not map the DART space. Fortunately, it will be aligned |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 512 | * in such a way that it will not cross two lmb regions and |
| 513 | * will fit within a single 16Mb page. |
| 514 | * The DART space is assumed to be a full 16Mb region even if |
| 515 | * we only use 2Mb of that space. We will use more of it later |
| 516 | * for AGP GART. We have to use a full 16Mb large page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | */ |
| 518 | DBG("DART base: %lx\n", dart_tablebase); |
| 519 | |
| 520 | if (dart_tablebase != 0 && dart_tablebase >= base |
| 521 | && dart_tablebase < (base + size)) { |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 522 | unsigned long dart_table_end = dart_tablebase + 16 * MB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (base != dart_tablebase) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 524 | BUG_ON(htab_bolt_mapping(base, dart_tablebase, |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 525 | __pa(base), mode_rw, |
| 526 | mmu_linear_psize)); |
| 527 | if ((base + size) > dart_table_end) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 528 | BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB, |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 529 | base + size, |
| 530 | __pa(dart_table_end), |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 531 | mode_rw, |
| 532 | mmu_linear_psize)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | continue; |
| 534 | } |
| 535 | #endif /* CONFIG_U3_DART */ |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 536 | BUG_ON(htab_bolt_mapping(base, base + size, __pa(base), |
| 537 | mode_rw, mmu_linear_psize)); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * If we have a memory_limit and we've allocated TCEs then we need to |
| 542 | * explicitly map the TCE area at the top of RAM. We also cope with the |
| 543 | * case that the TCEs start below memory_limit. |
| 544 | * tce_alloc_start/end are 16MB aligned so the mapping should work |
| 545 | * for either 4K or 16MB pages. |
| 546 | */ |
| 547 | if (tce_alloc_start) { |
Michael Ellerman | b5666f7 | 2005-12-05 10:24:33 -0600 | [diff] [blame] | 548 | tce_alloc_start = (unsigned long)__va(tce_alloc_start); |
| 549 | tce_alloc_end = (unsigned long)__va(tce_alloc_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | if (base + size >= tce_alloc_start) |
| 552 | tce_alloc_start = base + size + 1; |
| 553 | |
Michael Ellerman | caf80e5 | 2006-03-21 20:45:51 +1100 | [diff] [blame] | 554 | BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end, |
| 555 | __pa(tce_alloc_start), mode_rw, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 556 | mmu_linear_psize)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Michael Ellerman | 7d0daae | 2006-06-23 18:16:38 +1000 | [diff] [blame] | 559 | htab_finish_init(); |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | DBG(" <- htab_initialize()\n"); |
| 562 | } |
| 563 | #undef KB |
| 564 | #undef MB |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Anton Blanchard | e597cb32 | 2005-12-29 10:46:29 +1100 | [diff] [blame] | 566 | void htab_initialize_secondary(void) |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 567 | { |
Michael Ellerman | 57cfb81 | 2006-03-21 20:45:59 +1100 | [diff] [blame] | 568 | if (!firmware_has_feature(FW_FEATURE_LPAR)) |
Paul Mackerras | 799d604 | 2005-11-10 13:37:51 +1100 | [diff] [blame] | 569 | mtspr(SPRN_SDR1, _SDR1); |
| 570 | } |
| 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | /* |
| 573 | * Called by asm hashtable.S for doing lazy icache flush |
| 574 | */ |
| 575 | unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap) |
| 576 | { |
| 577 | struct page *page; |
| 578 | |
Benjamin Herrenschmidt | 76c8e25 | 2005-11-08 11:21:05 +1100 | [diff] [blame] | 579 | if (!pfn_valid(pte_pfn(pte))) |
| 580 | return pp; |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | page = pte_page(pte); |
| 583 | |
| 584 | /* page is dirty */ |
| 585 | if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) { |
| 586 | if (trap == 0x400) { |
| 587 | __flush_dcache_icache(page_address(page)); |
| 588 | set_bit(PG_arch_1, &page->flags); |
| 589 | } else |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 590 | pp |= HPTE_R_N; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | return pp; |
| 593 | } |
| 594 | |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 595 | /* |
| 596 | * Demote a segment to using 4k pages. |
| 597 | * For now this makes the whole process use 4k pages. |
| 598 | */ |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 599 | #ifdef CONFIG_PPC_64K_PAGES |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 600 | static void demote_segment_4k(struct mm_struct *mm, unsigned long addr) |
| 601 | { |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 602 | if (mm->context.user_psize == MMU_PAGE_4K) |
| 603 | return; |
| 604 | mm->context.user_psize = MMU_PAGE_4K; |
| 605 | mm->context.sllp = SLB_VSID_USER | mmu_psize_defs[MMU_PAGE_4K].sllp; |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 606 | #ifdef CONFIG_SPE_BASE |
| 607 | spu_flush_all_slbs(mm); |
| 608 | #endif |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 609 | } |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 610 | #endif /* CONFIG_PPC_64K_PAGES */ |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /* Result code is: |
| 613 | * 0 - handled |
| 614 | * 1 - normal page fault |
| 615 | * -1 - critical hash insertion error |
| 616 | */ |
| 617 | int hash_page(unsigned long ea, unsigned long access, unsigned long trap) |
| 618 | { |
| 619 | void *pgdir; |
| 620 | unsigned long vsid; |
| 621 | struct mm_struct *mm; |
| 622 | pte_t *ptep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | cpumask_t tmp; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 624 | int rc, user_region = 0, local = 0; |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 625 | int psize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 627 | DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n", |
| 628 | ea, access, trap); |
David Gibson | 1f8d419 | 2005-05-05 16:15:13 -0700 | [diff] [blame] | 629 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 630 | if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) { |
| 631 | DBG_LOW(" out of pgtable range !\n"); |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | /* Get region & vsid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | switch (REGION_ID(ea)) { |
| 637 | case USER_REGION_ID: |
| 638 | user_region = 1; |
| 639 | mm = current->mm; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 640 | if (! mm) { |
| 641 | DBG_LOW(" user region with no mm !\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return 1; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | vsid = get_vsid(mm->context.id, ea); |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 645 | psize = mm->context.user_psize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | case VMALLOC_REGION_ID: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | mm = &init_mm; |
| 649 | vsid = get_kernel_vsid(ea); |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 650 | if (ea < VMALLOC_END) |
| 651 | psize = mmu_vmalloc_psize; |
| 652 | else |
| 653 | psize = mmu_io_psize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | default: |
| 656 | /* Not a valid range |
| 657 | * Send the problem up to do_page_fault |
| 658 | */ |
| 659 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 661 | DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 663 | /* Get pgdir */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | pgdir = mm->pgd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | if (pgdir == NULL) |
| 666 | return 1; |
| 667 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 668 | /* Check CPU locality */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | tmp = cpumask_of_cpu(smp_processor_id()); |
| 670 | if (user_region && cpus_equal(mm->cpu_vm_mask, tmp)) |
| 671 | local = 1; |
| 672 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 673 | /* Handle hugepage regions */ |
| 674 | if (unlikely(in_hugepage_area(mm->context, ea))) { |
| 675 | DBG_LOW(" -> huge page !\n"); |
David Gibson | cbf52af | 2005-12-09 14:20:52 +1100 | [diff] [blame] | 676 | return hash_huge_page(mm, access, ea, vsid, local, trap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 679 | /* Get PTE and page size from page tables */ |
| 680 | ptep = find_linux_pte(pgdir, ea); |
| 681 | if (ptep == NULL || !pte_present(*ptep)) { |
| 682 | DBG_LOW(" no PTE !\n"); |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | #ifndef CONFIG_PPC_64K_PAGES |
| 687 | DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep)); |
| 688 | #else |
| 689 | DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep), |
| 690 | pte_val(*(ptep + PTRS_PER_PTE))); |
| 691 | #endif |
| 692 | /* Pre-check access permissions (will be re-checked atomically |
| 693 | * in __hash_page_XX but this pre-check is a fast path |
| 694 | */ |
| 695 | if (access & ~pte_val(*ptep)) { |
| 696 | DBG_LOW(" no access !\n"); |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | /* Do actual hashing */ |
| 701 | #ifndef CONFIG_PPC_64K_PAGES |
| 702 | rc = __hash_page_4K(ea, access, vsid, ptep, trap, local); |
| 703 | #else |
Paul Mackerras | 721151d | 2007-04-03 21:24:02 +1000 | [diff] [blame] | 704 | /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */ |
| 705 | if (pte_val(*ptep) & _PAGE_4K_PFN) { |
| 706 | demote_segment_4k(mm, ea); |
| 707 | psize = MMU_PAGE_4K; |
| 708 | } |
| 709 | |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 710 | /* If this PTE is non-cacheable and we have restrictions on |
| 711 | * using non cacheable large pages, then we switch to 4k |
| 712 | */ |
| 713 | if (mmu_ci_restrictions && psize == MMU_PAGE_64K && |
| 714 | (pte_val(*ptep) & _PAGE_NO_CACHE)) { |
| 715 | if (user_region) { |
| 716 | demote_segment_4k(mm, ea); |
| 717 | psize = MMU_PAGE_4K; |
| 718 | } else if (ea < VMALLOC_END) { |
| 719 | /* |
| 720 | * some driver did a non-cacheable mapping |
| 721 | * in vmalloc space, so switch vmalloc |
| 722 | * to 4k pages |
| 723 | */ |
| 724 | printk(KERN_ALERT "Reducing vmalloc segment " |
| 725 | "to 4kB pages because of " |
| 726 | "non-cacheable mapping\n"); |
| 727 | psize = mmu_vmalloc_psize = MMU_PAGE_4K; |
Benjamin Herrenschmidt | 94b2a43 | 2007-03-10 00:05:37 +0100 | [diff] [blame] | 728 | #ifdef CONFIG_SPE_BASE |
| 729 | spu_flush_all_slbs(mm); |
| 730 | #endif |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 731 | } |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 732 | } |
| 733 | if (user_region) { |
| 734 | if (psize != get_paca()->context.user_psize) { |
| 735 | get_paca()->context.user_psize = |
| 736 | mm->context.user_psize; |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 737 | slb_flush_and_rebolt(); |
| 738 | } |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 739 | } else if (get_paca()->vmalloc_sllp != |
| 740 | mmu_psize_defs[mmu_vmalloc_psize].sllp) { |
| 741 | get_paca()->vmalloc_sllp = |
| 742 | mmu_psize_defs[mmu_vmalloc_psize].sllp; |
| 743 | slb_flush_and_rebolt(); |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 744 | } |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 745 | |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 746 | if (psize == MMU_PAGE_64K) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 747 | rc = __hash_page_64K(ea, access, vsid, ptep, trap, local); |
| 748 | else |
| 749 | rc = __hash_page_4K(ea, access, vsid, ptep, trap, local); |
| 750 | #endif /* CONFIG_PPC_64K_PAGES */ |
| 751 | |
| 752 | #ifndef CONFIG_PPC_64K_PAGES |
| 753 | DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep)); |
| 754 | #else |
| 755 | DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep), |
| 756 | pte_val(*(ptep + PTRS_PER_PTE))); |
| 757 | #endif |
| 758 | DBG_LOW(" -> rc=%d\n", rc); |
| 759 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame] | 761 | EXPORT_SYMBOL_GPL(hash_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 763 | void hash_preload(struct mm_struct *mm, unsigned long ea, |
| 764 | unsigned long access, unsigned long trap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 766 | unsigned long vsid; |
| 767 | void *pgdir; |
| 768 | pte_t *ptep; |
| 769 | cpumask_t mask; |
| 770 | unsigned long flags; |
| 771 | int local = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 773 | /* We don't want huge pages prefaulted for now |
| 774 | */ |
| 775 | if (unlikely(in_hugepage_area(mm->context, ea))) |
| 776 | return; |
| 777 | |
| 778 | DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx," |
| 779 | " trap=%lx\n", mm, mm->pgd, ea, access, trap); |
| 780 | |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 781 | /* Get Linux PTE if available */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 782 | pgdir = mm->pgd; |
| 783 | if (pgdir == NULL) |
| 784 | return; |
| 785 | ptep = find_linux_pte(pgdir, ea); |
| 786 | if (!ptep) |
| 787 | return; |
Benjamin Herrenschmidt | 16f1c74 | 2007-05-08 16:27:27 +1000 | [diff] [blame^] | 788 | |
| 789 | #ifdef CONFIG_PPC_64K_PAGES |
| 790 | /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on |
| 791 | * a 64K kernel), then we don't preload, hash_page() will take |
| 792 | * care of it once we actually try to access the page. |
| 793 | * That way we don't have to duplicate all of the logic for segment |
| 794 | * page size demotion here |
| 795 | */ |
| 796 | if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE)) |
| 797 | return; |
| 798 | #endif /* CONFIG_PPC_64K_PAGES */ |
| 799 | |
| 800 | /* Get VSID */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 801 | vsid = get_vsid(mm->context.id, ea); |
| 802 | |
| 803 | /* Hash it in */ |
| 804 | local_irq_save(flags); |
| 805 | mask = cpumask_of_cpu(smp_processor_id()); |
| 806 | if (cpus_equal(mm->cpu_vm_mask, mask)) |
| 807 | local = 1; |
| 808 | #ifndef CONFIG_PPC_64K_PAGES |
| 809 | __hash_page_4K(ea, access, vsid, ptep, trap, local); |
| 810 | #else |
Paul Mackerras | bf72aeb | 2006-06-15 10:45:18 +1000 | [diff] [blame] | 811 | if (mm->context.user_psize == MMU_PAGE_64K) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 812 | __hash_page_64K(ea, access, vsid, ptep, trap, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | else |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 814 | __hash_page_4K(ea, access, vsid, ptep, trap, local); |
| 815 | #endif /* CONFIG_PPC_64K_PAGES */ |
| 816 | local_irq_restore(flags); |
| 817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 819 | void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int local) |
| 820 | { |
| 821 | unsigned long hash, index, shift, hidx, slot; |
| 822 | |
| 823 | DBG_LOW("flush_hash_page(va=%016x)\n", va); |
| 824 | pte_iterate_hashed_subpages(pte, psize, va, index, shift) { |
| 825 | hash = hpt_hash(va, shift); |
| 826 | hidx = __rpte_to_hidx(pte, index); |
| 827 | if (hidx & _PTEIDX_SECONDARY) |
| 828 | hash = ~hash; |
| 829 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 830 | slot += hidx & _PTEIDX_GROUP_IX; |
| 831 | DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx); |
| 832 | ppc_md.hpte_invalidate(slot, va, psize, local); |
| 833 | } pte_iterate_hashed_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Benjamin Herrenschmidt | 61b1a94 | 2005-09-20 13:52:50 +1000 | [diff] [blame] | 836 | void flush_hash_range(unsigned long number, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 838 | if (ppc_md.flush_hash_range) |
Benjamin Herrenschmidt | 61b1a94 | 2005-09-20 13:52:50 +1000 | [diff] [blame] | 839 | ppc_md.flush_hash_range(number, local); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 840 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | int i; |
Benjamin Herrenschmidt | 61b1a94 | 2005-09-20 13:52:50 +1000 | [diff] [blame] | 842 | struct ppc64_tlb_batch *batch = |
| 843 | &__get_cpu_var(ppc64_tlb_batch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
| 845 | for (i = 0; i < number; i++) |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 846 | flush_hash_page(batch->vaddr[i], batch->pte[i], |
| 847 | batch->psize, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /* |
| 852 | * low_hash_fault is called when we the low level hash code failed |
| 853 | * to instert a PTE due to an hypervisor error |
| 854 | */ |
| 855 | void low_hash_fault(struct pt_regs *regs, unsigned long address) |
| 856 | { |
| 857 | if (user_mode(regs)) { |
| 858 | siginfo_t info; |
| 859 | |
| 860 | info.si_signo = SIGBUS; |
| 861 | info.si_errno = 0; |
| 862 | info.si_code = BUS_ADRERR; |
| 863 | info.si_addr = (void __user *)address; |
| 864 | force_sig_info(SIGBUS, &info, current); |
| 865 | return; |
| 866 | } |
| 867 | bad_page_fault(regs, address, SIGBUS); |
| 868 | } |
Benjamin Herrenschmidt | 370a908 | 2007-04-12 15:30:23 +1000 | [diff] [blame] | 869 | |
| 870 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 871 | static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi) |
| 872 | { |
| 873 | unsigned long hash, hpteg, vsid = get_kernel_vsid(vaddr); |
| 874 | unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff); |
| 875 | unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY | |
| 876 | _PAGE_COHERENT | PP_RWXX | HPTE_R_N; |
| 877 | int ret; |
| 878 | |
| 879 | hash = hpt_hash(va, PAGE_SHIFT); |
| 880 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 881 | |
| 882 | ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr), |
| 883 | mode, HPTE_V_BOLTED, mmu_linear_psize); |
| 884 | BUG_ON (ret < 0); |
| 885 | spin_lock(&linear_map_hash_lock); |
| 886 | BUG_ON(linear_map_hash_slots[lmi] & 0x80); |
| 887 | linear_map_hash_slots[lmi] = ret | 0x80; |
| 888 | spin_unlock(&linear_map_hash_lock); |
| 889 | } |
| 890 | |
| 891 | static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi) |
| 892 | { |
| 893 | unsigned long hash, hidx, slot, vsid = get_kernel_vsid(vaddr); |
| 894 | unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff); |
| 895 | |
| 896 | hash = hpt_hash(va, PAGE_SHIFT); |
| 897 | spin_lock(&linear_map_hash_lock); |
| 898 | BUG_ON(!(linear_map_hash_slots[lmi] & 0x80)); |
| 899 | hidx = linear_map_hash_slots[lmi] & 0x7f; |
| 900 | linear_map_hash_slots[lmi] = 0; |
| 901 | spin_unlock(&linear_map_hash_lock); |
| 902 | if (hidx & _PTEIDX_SECONDARY) |
| 903 | hash = ~hash; |
| 904 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 905 | slot += hidx & _PTEIDX_GROUP_IX; |
| 906 | ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, 0); |
| 907 | } |
| 908 | |
| 909 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 910 | { |
| 911 | unsigned long flags, vaddr, lmi; |
| 912 | int i; |
| 913 | |
| 914 | local_irq_save(flags); |
| 915 | for (i = 0; i < numpages; i++, page++) { |
| 916 | vaddr = (unsigned long)page_address(page); |
| 917 | lmi = __pa(vaddr) >> PAGE_SHIFT; |
| 918 | if (lmi >= linear_map_hash_count) |
| 919 | continue; |
| 920 | if (enable) |
| 921 | kernel_map_linear_page(vaddr, lmi); |
| 922 | else |
| 923 | kernel_unmap_linear_page(vaddr, lmi); |
| 924 | } |
| 925 | local_irq_restore(flags); |
| 926 | } |
| 927 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |