Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TLB support routines. |
| 3 | * |
| 4 | * Copyright (C) 1998-2001, 2003 Hewlett-Packard Co |
| 5 | * David Mosberger-Tang <davidm@hpl.hp.com> |
| 6 | * |
| 7 | * 08/02/00 A. Mallick <asit.k.mallick@intel.com> |
| 8 | * Modified RID allocation for SMP |
| 9 | * Goutham Rao <goutham.rao@intel.com> |
| 10 | * IPI based ptc implementation and A-step IPI implementation. |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 11 | * Rohit Seth <rohit.seth@intel.com> |
| 12 | * Ken Chen <kenneth.w.chen@intel.com> |
de Dinechin, Christophe (Integrity VM) | aec103b | 2007-12-13 15:03:07 +0000 | [diff] [blame] | 13 | * Christophe de Dinechin <ddd@hp.com>: Avoid ptc.e on memory allocation |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 14 | * Copyright (C) 2007 Intel Corp |
| 15 | * Fenghua Yu <fenghua.yu@intel.com> |
| 16 | * Add multiple ptc.g/ptc.ga instruction support in global tlb purge. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/smp.h> |
| 23 | #include <linux/mm.h> |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 24 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/delay.h> |
| 27 | #include <asm/mmu_context.h> |
| 28 | #include <asm/pgalloc.h> |
| 29 | #include <asm/pal.h> |
| 30 | #include <asm/tlbflush.h> |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 31 | #include <asm/dma.h> |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 32 | #include <asm/sal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static struct { |
| 35 | unsigned long mask; /* mask of supported purge page-sizes */ |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 36 | unsigned long max_bits; /* log2 of largest supported purge page-size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } purge; |
| 38 | |
| 39 | struct ia64_ctx ia64_ctx = { |
Milind Arun Choudhary | 8737d59 | 2007-04-15 22:51:23 +0530 | [diff] [blame] | 40 | .lock = __SPIN_LOCK_UNLOCKED(ia64_ctx.lock), |
| 41 | .next = 1, |
| 42 | .max_ctx = ~0U |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | DEFINE_PER_CPU(u8, ia64_need_tlb_flush); |
| 46 | |
| 47 | /* |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 48 | * Initializes the ia64_ctx.bitmap array based on max_ctx+1. |
| 49 | * Called after cpu_init() has setup ia64_ctx.max_ctx based on |
| 50 | * maximum RID that is supported by boot CPU. |
| 51 | */ |
| 52 | void __init |
| 53 | mmu_context_init (void) |
| 54 | { |
| 55 | ia64_ctx.bitmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3); |
| 56 | ia64_ctx.flushmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3); |
| 57 | } |
| 58 | |
| 59 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * Acquire the ia64_ctx.lock before calling this function! |
| 61 | */ |
| 62 | void |
| 63 | wrap_mmu_context (struct mm_struct *mm) |
| 64 | { |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 65 | int i, cpu; |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 66 | unsigned long flush_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 68 | for (i=0; i <= ia64_ctx.max_ctx / BITS_PER_LONG; i++) { |
| 69 | flush_bit = xchg(&ia64_ctx.flushmap[i], 0); |
| 70 | ia64_ctx.bitmap[i] ^= flush_bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Peter Keilty | dcc17d1 | 2005-10-31 16:44:47 -0500 | [diff] [blame] | 72 | |
| 73 | /* use offset at 300 to skip daemons */ |
| 74 | ia64_ctx.next = find_next_zero_bit(ia64_ctx.bitmap, |
| 75 | ia64_ctx.max_ctx, 300); |
| 76 | ia64_ctx.limit = find_next_bit(ia64_ctx.bitmap, |
| 77 | ia64_ctx.max_ctx, ia64_ctx.next); |
| 78 | |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 79 | /* |
| 80 | * can't call flush_tlb_all() here because of race condition |
| 81 | * with O(1) scheduler [EF] |
| 82 | */ |
| 83 | cpu = get_cpu(); /* prevent preemption/migration */ |
| 84 | for_each_online_cpu(i) |
| 85 | if (i != cpu) |
| 86 | per_cpu(ia64_need_tlb_flush, i) = 1; |
| 87 | put_cpu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | local_flush_tlb_all(); |
| 89 | } |
| 90 | |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Implement "spinaphores" ... like counting semaphores, but they |
| 93 | * spin instead of sleeping. If there are ever any other users for |
| 94 | * this primitive it can be moved up to a spinaphore.h header. |
| 95 | */ |
| 96 | struct spinaphore { |
| 97 | atomic_t cur; |
| 98 | }; |
| 99 | |
| 100 | static inline void spinaphore_init(struct spinaphore *ss, int val) |
| 101 | { |
| 102 | atomic_set(&ss->cur, val); |
| 103 | } |
| 104 | |
| 105 | static inline void down_spin(struct spinaphore *ss) |
| 106 | { |
| 107 | while (unlikely(!atomic_add_unless(&ss->cur, -1, 0))) |
| 108 | while (atomic_read(&ss->cur) == 0) |
| 109 | cpu_relax(); |
| 110 | } |
| 111 | |
| 112 | static inline void up_spin(struct spinaphore *ss) |
| 113 | { |
| 114 | atomic_add(1, &ss->cur); |
| 115 | } |
| 116 | |
| 117 | static struct spinaphore ptcg_sem; |
| 118 | static u16 nptcg = 1; |
| 119 | static int need_ptcg_sem = 1; |
| 120 | static int toolatetochangeptcgsem = 0; |
| 121 | |
| 122 | /* |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 123 | * Kernel parameter "nptcg=" overrides max number of concurrent global TLB |
| 124 | * purges which is reported from either PAL or SAL PALO. |
| 125 | * |
| 126 | * We don't have sanity checking for nptcg value. It's the user's responsibility |
| 127 | * for valid nptcg value on the platform. Otherwise, kernel may hang in some |
| 128 | * cases. |
| 129 | */ |
| 130 | static int __init |
| 131 | set_nptcg(char *str) |
| 132 | { |
| 133 | int value = 0; |
| 134 | |
| 135 | get_option(&str, &value); |
| 136 | setup_ptcg_sem(value, NPTCG_FROM_KERNEL_PARAMETER); |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | __setup("nptcg=", set_nptcg); |
| 142 | |
| 143 | /* |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 144 | * Maximum number of simultaneous ptc.g purges in the system can |
| 145 | * be defined by PAL_VM_SUMMARY (in which case we should take |
| 146 | * the smallest value for any cpu in the system) or by the PAL |
| 147 | * override table (in which case we should ignore the value from |
| 148 | * PAL_VM_SUMMARY). |
| 149 | * |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 150 | * Kernel parameter "nptcg=" overrides maximum number of simultanesous ptc.g |
| 151 | * purges defined in either PAL_VM_SUMMARY or PAL override table. In this case, |
| 152 | * we should ignore the value from either PAL_VM_SUMMARY or PAL override table. |
| 153 | * |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 154 | * Complicating the logic here is the fact that num_possible_cpus() |
| 155 | * isn't fully setup until we start bringing cpus online. |
| 156 | */ |
| 157 | void |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 158 | setup_ptcg_sem(int max_purges, int nptcg_from) |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 159 | { |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 160 | static int kp_override; |
| 161 | static int palo_override; |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 162 | static int firstcpu = 1; |
| 163 | |
| 164 | if (toolatetochangeptcgsem) { |
| 165 | BUG_ON(max_purges < nptcg); |
| 166 | return; |
| 167 | } |
| 168 | |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 169 | if (nptcg_from == NPTCG_FROM_KERNEL_PARAMETER) { |
| 170 | kp_override = 1; |
| 171 | nptcg = max_purges; |
| 172 | goto resetsema; |
| 173 | } |
| 174 | if (kp_override) { |
| 175 | need_ptcg_sem = num_possible_cpus() > nptcg; |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | if (nptcg_from == NPTCG_FROM_PALO) { |
| 180 | palo_override = 1; |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 181 | |
| 182 | /* In PALO max_purges == 0 really means it! */ |
| 183 | if (max_purges == 0) |
| 184 | panic("Whoa! Platform does not support global TLB purges.\n"); |
| 185 | nptcg = max_purges; |
| 186 | if (nptcg == PALO_MAX_TLB_PURGES) { |
| 187 | need_ptcg_sem = 0; |
| 188 | return; |
| 189 | } |
| 190 | goto resetsema; |
| 191 | } |
Fenghua Yu | a6c75b86 | 2008-03-14 13:57:08 -0700 | [diff] [blame^] | 192 | if (palo_override) { |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 193 | if (nptcg != PALO_MAX_TLB_PURGES) |
| 194 | need_ptcg_sem = (num_possible_cpus() > nptcg); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */ |
| 199 | if (max_purges == 0) max_purges = 1; |
| 200 | |
| 201 | if (firstcpu) { |
| 202 | nptcg = max_purges; |
| 203 | firstcpu = 0; |
| 204 | } |
| 205 | if (max_purges < nptcg) |
| 206 | nptcg = max_purges; |
| 207 | if (nptcg == PAL_MAX_PURGES) { |
| 208 | need_ptcg_sem = 0; |
| 209 | return; |
| 210 | } else |
| 211 | need_ptcg_sem = (num_possible_cpus() > nptcg); |
| 212 | |
| 213 | resetsema: |
| 214 | spinaphore_init(&ptcg_sem, max_purges); |
| 215 | } |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | void |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 218 | ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start, |
| 219 | unsigned long end, unsigned long nbits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
de Dinechin, Christophe (Integrity VM) | aec103b | 2007-12-13 15:03:07 +0000 | [diff] [blame] | 221 | struct mm_struct *active_mm = current->active_mm; |
| 222 | |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 223 | toolatetochangeptcgsem = 1; |
| 224 | |
de Dinechin, Christophe (Integrity VM) | aec103b | 2007-12-13 15:03:07 +0000 | [diff] [blame] | 225 | if (mm != active_mm) { |
| 226 | /* Restore region IDs for mm */ |
| 227 | if (mm && active_mm) { |
| 228 | activate_context(mm); |
| 229 | } else { |
| 230 | flush_tlb_all(); |
| 231 | return; |
| 232 | } |
Dean Roe | c1902aa | 2005-10-27 15:41:04 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Fenghua Yu | 2046b94 | 2008-04-04 11:05:59 -0700 | [diff] [blame] | 235 | if (need_ptcg_sem) |
| 236 | down_spin(&ptcg_sem); |
| 237 | |
| 238 | do { |
| 239 | /* |
| 240 | * Flush ALAT entries also. |
| 241 | */ |
| 242 | ia64_ptcga(start, (nbits << 2)); |
| 243 | ia64_srlz_i(); |
| 244 | start += (1UL << nbits); |
| 245 | } while (start < end); |
| 246 | |
| 247 | if (need_ptcg_sem) |
| 248 | up_spin(&ptcg_sem); |
de Dinechin, Christophe (Integrity VM) | aec103b | 2007-12-13 15:03:07 +0000 | [diff] [blame] | 249 | |
| 250 | if (mm != active_mm) { |
| 251 | activate_context(active_mm); |
| 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
| 256 | local_flush_tlb_all (void) |
| 257 | { |
| 258 | unsigned long i, j, flags, count0, count1, stride0, stride1, addr; |
| 259 | |
| 260 | addr = local_cpu_data->ptce_base; |
| 261 | count0 = local_cpu_data->ptce_count[0]; |
| 262 | count1 = local_cpu_data->ptce_count[1]; |
| 263 | stride0 = local_cpu_data->ptce_stride[0]; |
| 264 | stride1 = local_cpu_data->ptce_stride[1]; |
| 265 | |
| 266 | local_irq_save(flags); |
| 267 | for (i = 0; i < count0; ++i) { |
| 268 | for (j = 0; j < count1; ++j) { |
| 269 | ia64_ptce(addr); |
| 270 | addr += stride1; |
| 271 | } |
| 272 | addr += stride0; |
| 273 | } |
| 274 | local_irq_restore(flags); |
| 275 | ia64_srlz_i(); /* srlz.i implies srlz.d */ |
| 276 | } |
| 277 | |
| 278 | void |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 279 | flush_tlb_range (struct vm_area_struct *vma, unsigned long start, |
| 280 | unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct mm_struct *mm = vma->vm_mm; |
| 283 | unsigned long size = end - start; |
| 284 | unsigned long nbits; |
| 285 | |
Dean Roe | c1902aa | 2005-10-27 15:41:04 -0500 | [diff] [blame] | 286 | #ifndef CONFIG_SMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (mm != current->active_mm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | mm->context = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return; |
| 290 | } |
Dean Roe | c1902aa | 2005-10-27 15:41:04 -0500 | [diff] [blame] | 291 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | nbits = ia64_fls(size + 0xfff); |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 294 | while (unlikely (((1UL << nbits) & purge.mask) == 0) && |
| 295 | (nbits < purge.max_bits)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | ++nbits; |
| 297 | if (nbits > purge.max_bits) |
| 298 | nbits = purge.max_bits; |
| 299 | start &= ~((1UL << nbits) - 1); |
| 300 | |
Hugh Dickins | 663b97f | 2005-10-29 18:16:28 -0700 | [diff] [blame] | 301 | preempt_disable(); |
Chen, Kenneth W | ce9eed5 | 2006-03-06 14:12:54 -0800 | [diff] [blame] | 302 | #ifdef CONFIG_SMP |
| 303 | if (mm != current->active_mm || cpus_weight(mm->cpu_vm_mask) != 1) { |
| 304 | platform_global_tlb_purge(mm, start, end, nbits); |
| 305 | preempt_enable(); |
| 306 | return; |
| 307 | } |
| 308 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | do { |
| 310 | ia64_ptcl(start, (nbits<<2)); |
| 311 | start += (1UL << nbits); |
| 312 | } while (start < end); |
Hugh Dickins | 663b97f | 2005-10-29 18:16:28 -0700 | [diff] [blame] | 313 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | ia64_srlz_i(); /* srlz.i implies srlz.d */ |
| 315 | } |
| 316 | EXPORT_SYMBOL(flush_tlb_range); |
| 317 | |
| 318 | void __devinit |
| 319 | ia64_tlb_init (void) |
| 320 | { |
Jes Sorensen | 256a7e0 | 2007-07-11 17:26:30 +0200 | [diff] [blame] | 321 | ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | unsigned long tr_pgbits; |
| 323 | long status; |
| 324 | |
| 325 | if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) { |
Joe Perches | c2eeb32 | 2007-11-19 17:47:53 -0800 | [diff] [blame] | 326 | printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | "defaulting to architected purge page-sizes.\n", status); |
| 328 | purge.mask = 0x115557000UL; |
| 329 | } |
| 330 | purge.max_bits = ia64_fls(purge.mask); |
| 331 | |
| 332 | ia64_get_ptce(&ptce_info); |
| 333 | local_cpu_data->ptce_base = ptce_info.base; |
| 334 | local_cpu_data->ptce_count[0] = ptce_info.count[0]; |
| 335 | local_cpu_data->ptce_count[1] = ptce_info.count[1]; |
| 336 | local_cpu_data->ptce_stride[0] = ptce_info.stride[0]; |
| 337 | local_cpu_data->ptce_stride[1] = ptce_info.stride[1]; |
| 338 | |
Chen, Kenneth W | 58cd908 | 2005-10-29 18:47:04 -0700 | [diff] [blame] | 339 | local_flush_tlb_all(); /* nuke left overs from bootstrapping... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |