1 /* SMP TLB support routines.
3 * Copyright (C) 2006-2008 Panasonic Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 #include <linux/interrupt.h>
16 #include <linux/spinlock.h>
17 #include <linux/init.h>
18 #include <linux/jiffies.h>
19 #include <linux/cpumask.h>
20 #include <linux/err.h>
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/sched/mm.h>
24 #include <linux/profile.h>
25 #include <linux/smp.h>
26 #include <asm/tlbflush.h>
27 #include <asm/bitops.h>
28 #include <asm/processor.h>
30 #include <asm/exceptions.h>
31 #include <asm/hardirq.h>
33 #include <asm/mmu_context.h>
34 #include <asm/thread_info.h>
35 #include <asm/cpu-regs.h>
36 #include <asm/intctl-regs.h>
41 #define FLUSH_ALL 0xffffffff
43 static cpumask_t flush_cpumask
;
44 static struct mm_struct
*flush_mm
;
45 static unsigned long flush_va
;
46 static DEFINE_SPINLOCK(tlbstate_lock
);
48 DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state
, cpu_tlbstate
) = {
52 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
54 static void do_flush_tlb_all(void *info
);
57 * smp_flush_tlb - Callback to invalidate the TLB.
58 * @unused: Callback context (ignored).
60 void smp_flush_tlb(void *unused
)
66 if (!cpumask_test_cpu(cpu_id
, &flush_cpumask
))
67 /* This was a BUG() but until someone can quote me the line
68 * from the intel manual that guarantees an IPI to multiple
69 * CPUs is retried _only_ on the erroring CPUs its staying as a
76 if (flush_va
== FLUSH_ALL
)
79 local_flush_tlb_page(flush_mm
, flush_va
);
81 smp_mb__before_atomic();
82 cpumask_clear_cpu(cpu_id
, &flush_cpumask
);
83 smp_mb__after_atomic();
89 * flush_tlb_others - Tell the specified CPUs to invalidate their TLBs
90 * @cpumask: The list of CPUs to target.
91 * @mm: The VM context to flush from (if va!=FLUSH_ALL).
92 * @va: Virtual address to flush or FLUSH_ALL to flush everything.
94 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
99 /* A couple of sanity checks (to be removed):
100 * - mask must not be empty
101 * - current CPU must not be in mask
102 * - we do not send IPIs to as-yet unbooted CPUs.
105 BUG_ON(cpumask_empty(&cpumask
));
106 BUG_ON(cpumask_test_cpu(smp_processor_id(), &cpumask
));
108 cpumask_and(&tmp
, &cpumask
, cpu_online_mask
);
109 BUG_ON(!cpumask_equal(&cpumask
, &tmp
));
111 /* I'm not happy about this global shared spinlock in the MM hot path,
112 * but we'll see how contended it is.
114 * Temporarily this turns IRQs off, so that lockups are detected by the
117 spin_lock(&tlbstate_lock
);
121 #if NR_CPUS <= BITS_PER_LONG
122 atomic_or(cpumask
.bits
[0], (atomic_t
*)&flush_cpumask
.bits
[0]);
124 #error Not supported.
127 /* FIXME: if NR_CPUS>=3, change send_IPI_mask */
128 smp_call_function(smp_flush_tlb
, NULL
, 1);
130 while (!cpumask_empty(&flush_cpumask
))
131 /* Lockup detection does not belong here */
136 spin_unlock(&tlbstate_lock
);
140 * flush_tlb_mm - Invalidate TLB of specified VM context
141 * @mm: The VM context to invalidate.
143 void flush_tlb_mm(struct mm_struct
*mm
)
148 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
149 cpumask_clear_cpu(smp_processor_id(), &cpu_mask
);
152 if (!cpumask_empty(&cpu_mask
))
153 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
159 * flush_tlb_current_task - Invalidate TLB of current task
161 void flush_tlb_current_task(void)
163 struct mm_struct
*mm
= current
->mm
;
167 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
168 cpumask_clear_cpu(smp_processor_id(), &cpu_mask
);
171 if (!cpumask_empty(&cpu_mask
))
172 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
178 * flush_tlb_page - Invalidate TLB of page
179 * @vma: The VM context to invalidate the page for.
180 * @va: The virtual address of the page to invalidate.
182 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long va
)
184 struct mm_struct
*mm
= vma
->vm_mm
;
188 cpumask_copy(&cpu_mask
, mm_cpumask(mm
));
189 cpumask_clear_cpu(smp_processor_id(), &cpu_mask
);
191 local_flush_tlb_page(mm
, va
);
192 if (!cpumask_empty(&cpu_mask
))
193 flush_tlb_others(cpu_mask
, mm
, va
);
199 * do_flush_tlb_all - Callback to completely invalidate a TLB
200 * @unused: Callback context (ignored).
202 static void do_flush_tlb_all(void *unused
)
204 local_flush_tlb_all();
208 * flush_tlb_all - Completely invalidate TLBs on all CPUs
210 void flush_tlb_all(void)
212 on_each_cpu(do_flush_tlb_all
, 0, 1);