2 * TLB support routines.
4 * Copyright (C) 1998-2001, 2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
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.
11 * Rohit Seth <rohit.seth@intel.com>
12 * Ken Chen <kenneth.w.chen@intel.com>
13 * Christophe de Dinechin <ddd@hp.com>: Avoid ptc.e on memory allocation
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.
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>
24 #include <linux/bootmem.h>
26 #include <asm/delay.h>
27 #include <asm/mmu_context.h>
28 #include <asm/pgalloc.h>
30 #include <asm/tlbflush.h>
35 unsigned long mask; /* mask of supported purge page-sizes */
36 unsigned long max_bits; /* log2 of largest supported purge page-size */
39 struct ia64_ctx ia64_ctx = {
40 .lock = __SPIN_LOCK_UNLOCKED(ia64_ctx.lock),
45 DEFINE_PER_CPU(u8, ia64_need_tlb_flush);
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.
53 mmu_context_init (void)
55 ia64_ctx.bitmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
56 ia64_ctx.flushmap = alloc_bootmem((ia64_ctx.max_ctx+1)>>3);
60 * Acquire the ia64_ctx.lock before calling this function!
63 wrap_mmu_context (struct mm_struct *mm)
66 unsigned long flush_bit;
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;
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);
80 * can't call flush_tlb_all() here because of race condition
81 * with O(1) scheduler [EF]
83 cpu = get_cpu(); /* prevent preemption/migration */
84 for_each_online_cpu(i)
86 per_cpu(ia64_need_tlb_flush, i) = 1;
88 local_flush_tlb_all();
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.
100 static inline void spinaphore_init(struct spinaphore *ss, int val)
102 atomic_set(&ss->cur, val);
105 static inline void down_spin(struct spinaphore *ss)
107 while (unlikely(!atomic_add_unless(&ss->cur, -1, 0)))
108 while (atomic_read(&ss->cur) == 0)
112 static inline void up_spin(struct spinaphore *ss)
114 atomic_add(1, &ss->cur);
117 static struct spinaphore ptcg_sem;
118 static u16 nptcg = 1;
119 static int need_ptcg_sem = 1;
120 static int toolatetochangeptcgsem = 0;
123 * Maximum number of simultaneous ptc.g purges in the system can
124 * be defined by PAL_VM_SUMMARY (in which case we should take
125 * the smallest value for any cpu in the system) or by the PAL
126 * override table (in which case we should ignore the value from
129 * Complicating the logic here is the fact that num_possible_cpus()
130 * isn't fully setup until we start bringing cpus online.
133 setup_ptcg_sem(int max_purges, int from_palo)
135 static int have_palo;
136 static int firstcpu = 1;
138 if (toolatetochangeptcgsem) {
139 BUG_ON(max_purges < nptcg);
146 /* In PALO max_purges == 0 really means it! */
148 panic("Whoa! Platform does not support global TLB purges.\n");
150 if (nptcg == PALO_MAX_TLB_PURGES) {
157 if (nptcg != PALO_MAX_TLB_PURGES)
158 need_ptcg_sem = (num_possible_cpus() > nptcg);
162 /* In PAL_VM_SUMMARY max_purges == 0 actually means 1 */
163 if (max_purges == 0) max_purges = 1;
169 if (max_purges < nptcg)
171 if (nptcg == PAL_MAX_PURGES) {
175 need_ptcg_sem = (num_possible_cpus() > nptcg);
178 spinaphore_init(&ptcg_sem, max_purges);
182 ia64_global_tlb_purge (struct mm_struct *mm, unsigned long start,
183 unsigned long end, unsigned long nbits)
185 struct mm_struct *active_mm = current->active_mm;
187 toolatetochangeptcgsem = 1;
189 if (mm != active_mm) {
190 /* Restore region IDs for mm */
191 if (mm && active_mm) {
192 activate_context(mm);
200 down_spin(&ptcg_sem);
204 * Flush ALAT entries also.
206 ia64_ptcga(start, (nbits << 2));
208 start += (1UL << nbits);
209 } while (start < end);
214 if (mm != active_mm) {
215 activate_context(active_mm);
220 local_flush_tlb_all (void)
222 unsigned long i, j, flags, count0, count1, stride0, stride1, addr;
224 addr = local_cpu_data->ptce_base;
225 count0 = local_cpu_data->ptce_count[0];
226 count1 = local_cpu_data->ptce_count[1];
227 stride0 = local_cpu_data->ptce_stride[0];
228 stride1 = local_cpu_data->ptce_stride[1];
230 local_irq_save(flags);
231 for (i = 0; i < count0; ++i) {
232 for (j = 0; j < count1; ++j) {
238 local_irq_restore(flags);
239 ia64_srlz_i(); /* srlz.i implies srlz.d */
243 flush_tlb_range (struct vm_area_struct *vma, unsigned long start,
246 struct mm_struct *mm = vma->vm_mm;
247 unsigned long size = end - start;
251 if (mm != current->active_mm) {
257 nbits = ia64_fls(size + 0xfff);
258 while (unlikely (((1UL << nbits) & purge.mask) == 0) &&
259 (nbits < purge.max_bits))
261 if (nbits > purge.max_bits)
262 nbits = purge.max_bits;
263 start &= ~((1UL << nbits) - 1);
267 if (mm != current->active_mm || cpus_weight(mm->cpu_vm_mask) != 1) {
268 platform_global_tlb_purge(mm, start, end, nbits);
274 ia64_ptcl(start, (nbits<<2));
275 start += (1UL << nbits);
276 } while (start < end);
278 ia64_srlz_i(); /* srlz.i implies srlz.d */
280 EXPORT_SYMBOL(flush_tlb_range);
285 ia64_ptce_info_t uninitialized_var(ptce_info); /* GCC be quiet */
286 unsigned long tr_pgbits;
289 if ((status = ia64_pal_vm_page_size(&tr_pgbits, &purge.mask)) != 0) {
290 printk(KERN_ERR "PAL_VM_PAGE_SIZE failed with status=%ld; "
291 "defaulting to architected purge page-sizes.\n", status);
292 purge.mask = 0x115557000UL;
294 purge.max_bits = ia64_fls(purge.mask);
296 ia64_get_ptce(&ptce_info);
297 local_cpu_data->ptce_base = ptce_info.base;
298 local_cpu_data->ptce_count[0] = ptce_info.count[0];
299 local_cpu_data->ptce_count[1] = ptce_info.count[1];
300 local_cpu_data->ptce_stride[0] = ptce_info.stride[0];
301 local_cpu_data->ptce_stride[1] = ptce_info.stride[1];
303 local_flush_tlb_all(); /* nuke left overs from bootstrapping... */