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