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