2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2000, 2001 Kanoj Sarcar
17 * Copyright (C) 2000, 2001 Ralf Baechle
18 * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
19 * Copyright (C) 2000, 2001 Broadcom Corporation
21 #include <linux/config.h>
22 #include <linux/cache.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/threads.h>
28 #include <linux/module.h>
29 #include <linux/time.h>
30 #include <linux/timex.h>
31 #include <linux/sched.h>
33 #include <asm/atomic.h>
35 #include <asm/processor.h>
36 #include <asm/system.h>
37 #include <asm/hardirq.h>
38 #include <asm/softirq.h>
39 #include <asm/mmu_context.h>
42 /* The 'big kernel lock' */
43 spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
44 int smp_threads_ready; /* Not used */
45 atomic_t smp_commenced = ATOMIC_INIT(0);
47 atomic_t cpus_booted = ATOMIC_INIT(0);
49 int smp_num_cpus = 1; /* Number that came online. */
50 cpumask_t cpu_online_map; /* Bitmask of currently online CPUs */
51 int __cpu_number_map[NR_CPUS];
52 int __cpu_logical_map[NR_CPUS];
53 cycles_t cacheflush_time;
55 EXPORT_SYMBOL(__cpu_number_map);
56 EXPORT_SYMBOL(__cpu_logical_map);
58 void __init smp_callin(void)
62 smp_store_cpu_info(cpuid);
66 void __init smp_commence(void)
69 atomic_set(&smp_commenced, 1);
73 * this function sends a 'reschedule' IPI to another CPU.
74 * it goes straight through and wastes no time serializing
75 * anything. Worst case is that we lose a reschedule ...
77 void smp_send_reschedule(int cpu)
79 core_send_ipi(cpu, SMP_RESCHEDULE_YOURSELF);
82 spinlock_t smp_call_lock = SPIN_LOCK_UNLOCKED;
84 struct call_data_struct *call_data;
87 * Run a function on all other CPUs.
88 * <func> The function to run. This must be fast and non-blocking.
89 * <info> An arbitrary pointer to pass to the function.
90 * <retry> If true, keep retrying until ready.
91 * <wait> If true, wait until function has completed on other CPUs.
92 * [RETURNS] 0 on success, else a negative status code.
94 * Does not return until remote CPUs are nearly ready to execute <func>
95 * or are or have executed.
97 int smp_call_function (void (*func) (void *info), void *info, int retry,
100 struct call_data_struct data;
101 int i, cpus = smp_num_cpus - 1;
102 int cpu = smp_processor_id();
109 atomic_set(&data.started, 0);
112 atomic_set(&data.finished, 0);
114 spin_lock(&smp_call_lock);
118 /* Send a message to all other CPUs and wait for them to respond */
119 for (i = 0; i < smp_num_cpus; i++)
121 core_send_ipi(i, SMP_CALL_FUNCTION);
123 /* Wait for response */
124 /* FIXME: lock-up detection, backtrace on lock-up */
125 while (atomic_read(&data.started) != cpus)
129 while (atomic_read(&data.finished) != cpus)
131 spin_unlock(&smp_call_lock);
136 void smp_call_function_interrupt(void)
138 void (*func) (void *info) = call_data->func;
139 void *info = call_data->info;
140 int wait = call_data->wait;
141 int cpu = smp_processor_id();
143 irq_enter(cpu, 0); /* XXX choose an irq number? */
145 * Notify initiating CPU that I've grabbed the data and am
146 * about to execute the function.
149 atomic_inc(&call_data->started);
152 * At this point the info structure may be out of scope unless wait==1.
157 atomic_inc(&call_data->finished);
159 irq_exit(cpu, 0); /* XXX choose an irq number? */
162 static void stop_this_cpu(void *dummy)
167 clear_bit(smp_processor_id(), &cpu_online_map);
168 /* May need to service _machine_restart IPI */
170 /* XXXKW wait if available? */
174 void smp_send_stop(void)
176 smp_call_function(stop_this_cpu, NULL, 1, 0);
178 * Fix me: this prevents future IPIs, for example that would
179 * cause a restart to happen on CPU0.
184 /* Not really SMP stuff ... */
185 int setup_profiling_timer(unsigned int multiplier)
190 static void flush_tlb_all_ipi(void *info)
192 local_flush_tlb_all();
195 void flush_tlb_all(void)
197 smp_call_function(flush_tlb_all_ipi, 0, 1, 1);
198 local_flush_tlb_all();
201 static void flush_tlb_mm_ipi(void *mm)
203 local_flush_tlb_mm((struct mm_struct *)mm);
207 * The following tlb flush calls are invoked when old translations are
208 * being torn down, or pte attributes are changing. For single threaded
209 * address spaces, a new context is obtained on the current cpu, and tlb
210 * context on other cpus are invalidated to force a new context allocation
211 * at switch_mm time, should the mm ever be used on other cpus. For
212 * multithreaded address spaces, intercpu interrupts have to be sent.
213 * Another case where intercpu interrupts are required is when the target
214 * mm might be active on another cpu (eg debuggers doing the flushes on
215 * behalf of debugees, kswapd stealing pages from another process etc).
219 void flush_tlb_mm(struct mm_struct *mm)
221 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
222 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1, 1);
225 for (i = 0; i < smp_num_cpus; i++)
226 if (smp_processor_id() != i)
227 cpu_context(i, mm) = 0;
229 local_flush_tlb_mm(mm);
232 struct flush_tlb_data {
233 struct mm_struct *mm;
234 struct vm_area_struct *vma;
239 static void flush_tlb_range_ipi(void *info)
241 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
243 local_flush_tlb_range(fd->mm, fd->addr1, fd->addr2);
246 void flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long end)
248 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
249 struct flush_tlb_data fd;
254 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1, 1);
257 for (i = 0; i < smp_num_cpus; i++)
258 if (smp_processor_id() != i)
259 cpu_context(i, mm) = 0;
261 local_flush_tlb_range(mm, start, end);
264 static void flush_tlb_page_ipi(void *info)
266 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
268 local_flush_tlb_page(fd->vma, fd->addr1);
271 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
273 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
274 struct flush_tlb_data fd;
278 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1, 1);
281 for (i = 0; i < smp_num_cpus; i++)
282 if (smp_processor_id() != i)
283 cpu_context(i, vma->vm_mm) = 0;
285 local_flush_tlb_page(vma, page);
288 EXPORT_SYMBOL(smp_num_cpus);
289 EXPORT_SYMBOL(flush_tlb_page);
290 EXPORT_SYMBOL(synchronize_irq);
291 EXPORT_SYMBOL(kernel_flag);
292 EXPORT_SYMBOL(__global_sti);
293 EXPORT_SYMBOL(__global_cli);
294 EXPORT_SYMBOL(__global_save_flags);
295 EXPORT_SYMBOL(__global_restore_flags);