blob: 1a8948fd002905c361b17d135292bb1c630a8909 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Keiltydcc17d12005-10-31 16:44:47 -050011 * Rohit Seth <rohit.seth@intel.com>
12 * Ken Chen <kenneth.w.chen@intel.com>
de Dinechin, Christophe (Integrity VM)aec103b2007-12-13 15:03:07 +000013 * Christophe de Dinechin <ddd@hp.com>: Avoid ptc.e on memory allocation
Fenghua Yu2046b942008-04-04 11:05:59 -070014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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 Keiltydcc17d12005-10-31 16:44:47 -050024#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
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 Keiltydcc17d12005-10-31 16:44:47 -050031#include <asm/dma.h>
Fenghua Yu2046b942008-04-04 11:05:59 -070032#include <asm/sal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static struct {
35 unsigned long mask; /* mask of supported purge page-sizes */
Chen, Kenneth W58cd9082005-10-29 18:47:04 -070036 unsigned long max_bits; /* log2 of largest supported purge page-size */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037} purge;
38
39struct ia64_ctx ia64_ctx = {
Milind Arun Choudhary8737d592007-04-15 22:51:23 +053040 .lock = __SPIN_LOCK_UNLOCKED(ia64_ctx.lock),
41 .next = 1,
42 .max_ctx = ~0U
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45DEFINE_PER_CPU(u8, ia64_need_tlb_flush);
46
47/*
Peter Keiltydcc17d12005-10-31 16:44:47 -050048 * 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 */
52void __init
53mmu_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 Torvalds1da177e2005-04-16 15:20:36 -070060 * Acquire the ia64_ctx.lock before calling this function!
61 */
62void
63wrap_mmu_context (struct mm_struct *mm)
64{
Chen, Kenneth W58cd9082005-10-29 18:47:04 -070065 int i, cpu;
Peter Keiltydcc17d12005-10-31 16:44:47 -050066 unsigned long flush_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Peter Keiltydcc17d12005-10-31 16:44:47 -050068 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 Torvalds1da177e2005-04-16 15:20:36 -070071 }
Peter Keiltydcc17d12005-10-31 16:44:47 -050072
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 W58cd9082005-10-29 18:47:04 -070079 /*
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 Torvalds1da177e2005-04-16 15:20:36 -070088 local_flush_tlb_all();
89}
90
Fenghua Yu2046b942008-04-04 11:05:59 -070091/*
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 */
96struct spinaphore {
97 atomic_t cur;
98};
99
100static inline void spinaphore_init(struct spinaphore *ss, int val)
101{
102 atomic_set(&ss->cur, val);
103}
104
105static 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
112static inline void up_spin(struct spinaphore *ss)
113{
114 atomic_add(1, &ss->cur);
115}
116
117static struct spinaphore ptcg_sem;
118static u16 nptcg = 1;
119static int need_ptcg_sem = 1;
120static int toolatetochangeptcgsem = 0;
121
122/*
Fenghua Yua6c75b862008-03-14 13:57:08 -0700123 * 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 */
130static int __init
131set_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 Yu2046b942008-04-04 11:05:59 -0700144 * 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 Yua6c75b862008-03-14 13:57:08 -0700150 * 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 Yu2046b942008-04-04 11:05:59 -0700154 * Complicating the logic here is the fact that num_possible_cpus()
155 * isn't fully setup until we start bringing cpus online.
156 */
157void
Fenghua Yua6c75b862008-03-14 13:57:08 -0700158setup_ptcg_sem(int max_purges, int nptcg_from)
Fenghua Yu2046b942008-04-04 11:05:59 -0700159{
Fenghua Yua6c75b862008-03-14 13:57:08 -0700160 static int kp_override;
161 static int palo_override;
Fenghua Yu2046b942008-04-04 11:05:59 -0700162 static int firstcpu = 1;
163
164 if (toolatetochangeptcgsem) {
165 BUG_ON(max_purges < nptcg);
166 return;
167 }
168
Fenghua Yua6c75b862008-03-14 13:57:08 -0700169 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 Yu2046b942008-04-04 11:05:59 -0700181
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 Yua6c75b862008-03-14 13:57:08 -0700192 if (palo_override) {
Fenghua Yu2046b942008-04-04 11:05:59 -0700193 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
213resetsema:
214 spinaphore_init(&ptcg_sem, max_purges);
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217void
Chen, Kenneth W58cd9082005-10-29 18:47:04 -0700218ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
219 unsigned long end, unsigned long nbits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
de Dinechin, Christophe (Integrity VM)aec103b2007-12-13 15:03:07 +0000221 struct mm_struct *active_mm = current->active_mm;
222
Fenghua Yu2046b942008-04-04 11:05:59 -0700223 toolatetochangeptcgsem = 1;
224
de Dinechin, Christophe (Integrity VM)aec103b2007-12-13 15:03:07 +0000225 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 Roec1902aa2005-10-27 15:41:04 -0500233 }
234
Fenghua Yu2046b942008-04-04 11:05:59 -0700235 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)aec103b2007-12-13 15:03:07 +0000249
250 if (mm != active_mm) {
251 activate_context(active_mm);
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255void
256local_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
278void
Chen, Kenneth W58cd9082005-10-29 18:47:04 -0700279flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
280 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct mm_struct *mm = vma->vm_mm;
283 unsigned long size = end - start;
284 unsigned long nbits;
285
Dean Roec1902aa2005-10-27 15:41:04 -0500286#ifndef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (mm != current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 mm->context = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return;
290 }
Dean Roec1902aa2005-10-27 15:41:04 -0500291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 nbits = ia64_fls(size + 0xfff);
Chen, Kenneth W58cd9082005-10-29 18:47:04 -0700294 while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
295 (nbits < purge.max_bits))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 ++nbits;
297 if (nbits > purge.max_bits)
298 nbits = purge.max_bits;
299 start &= ~((1UL << nbits) - 1);
300
Hugh Dickins663b97f2005-10-29 18:16:28 -0700301 preempt_disable();
Chen, Kenneth Wce9eed52006-03-06 14:12:54 -0800302#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 Torvalds1da177e2005-04-16 15:20:36 -0700309 do {
310 ia64_ptcl(start, (nbits<<2));
311 start += (1UL << nbits);
312 } while (start < end);
Hugh Dickins663b97f2005-10-29 18:16:28 -0700313 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ia64_srlz_i(); /* srlz.i implies srlz.d */
315}
316EXPORT_SYMBOL(flush_tlb_range);
317
318void __devinit
319ia64_tlb_init (void)
320{
Jes Sorensen256a7e02007-07-11 17:26:30 +0200321 ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 unsigned long tr_pgbits;
323 long status;
324
325 if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
Joe Perchesc2eeb322007-11-19 17:47:53 -0800326 printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 "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 W58cd9082005-10-29 18:47:04 -0700339 local_flush_tlb_all(); /* nuke left overs from bootstrapping... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}