1 /*
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.
6  *
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.
11  *
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.
15  *
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
20  */
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>
32 
33 #include <asm/atomic.h>
34 #include <asm/cpu.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>
40 #include <asm/smp.h>
41 
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);
46 
47 atomic_t cpus_booted = ATOMIC_INIT(0);
48 
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;
54 
55 EXPORT_SYMBOL(__cpu_number_map);
56 EXPORT_SYMBOL(__cpu_logical_map);
57 
smp_callin(void)58 void __init smp_callin(void)
59 {
60 #if 0
61 	calibrate_delay();
62 	smp_store_cpu_info(cpuid);
63 #endif
64 }
65 
smp_commence(void)66 void __init smp_commence(void)
67 {
68 	wmb();
69 	atomic_set(&smp_commenced, 1);
70 }
71 
72 /*
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 ...
76  */
smp_send_reschedule(int cpu)77 void smp_send_reschedule(int cpu)
78 {
79 	core_send_ipi(cpu, SMP_RESCHEDULE_YOURSELF);
80 }
81 
82 spinlock_t smp_call_lock = SPIN_LOCK_UNLOCKED;
83 
84 struct call_data_struct *call_data;
85 
86 /*
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.
93  *
94  * Does not return until remote CPUs are nearly ready to execute <func>
95  * or are or have executed.
96  */
smp_call_function(void (* func)(void * info),void * info,int retry,int wait)97 int smp_call_function (void (*func) (void *info), void *info, int retry,
98 								int wait)
99 {
100 	struct call_data_struct data;
101 	int i, cpus = smp_num_cpus - 1;
102 	int cpu = smp_processor_id();
103 
104 	if (!cpus)
105 		return 0;
106 
107 	data.func = func;
108 	data.info = info;
109 	atomic_set(&data.started, 0);
110 	data.wait = wait;
111 	if (wait)
112 		atomic_set(&data.finished, 0);
113 
114 	spin_lock(&smp_call_lock);
115 	call_data = &data;
116 	wmb();
117 
118 	/* Send a message to all other CPUs and wait for them to respond */
119 	for (i = 0; i < smp_num_cpus; i++)
120 		if (i != cpu)
121 			core_send_ipi(i, SMP_CALL_FUNCTION);
122 
123 	/* Wait for response */
124 	/* FIXME: lock-up detection, backtrace on lock-up */
125 	while (atomic_read(&data.started) != cpus)
126 		barrier();
127 
128 	if (wait)
129 		while (atomic_read(&data.finished) != cpus)
130 			barrier();
131 	spin_unlock(&smp_call_lock);
132 
133 	return 0;
134 }
135 
smp_call_function_interrupt(void)136 void smp_call_function_interrupt(void)
137 {
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();
142 
143 	irq_enter(cpu, 0);	/* XXX choose an irq number? */
144 	/*
145 	 * Notify initiating CPU that I've grabbed the data and am
146 	 * about to execute the function.
147 	 */
148 	mb();
149 	atomic_inc(&call_data->started);
150 
151 	/*
152 	 * At this point the info structure may be out of scope unless wait==1.
153 	 */
154 	(*func)(info);
155 	if (wait) {
156 		mb();
157 		atomic_inc(&call_data->finished);
158 	}
159 	irq_exit(cpu, 0);	/* XXX choose an irq number? */
160 }
161 
stop_this_cpu(void * dummy)162 static void stop_this_cpu(void *dummy)
163 {
164 	/*
165 	 * Remove this CPU:
166 	 */
167 	clear_bit(smp_processor_id(), &cpu_online_map);
168 	/* May need to service _machine_restart IPI */
169 	local_irq_enable();
170 	/* XXXKW wait if available? */
171 	for (;;);
172 }
173 
smp_send_stop(void)174 void smp_send_stop(void)
175 {
176 	smp_call_function(stop_this_cpu, NULL, 1, 0);
177 	/*
178 	 * Fix me: this prevents future IPIs, for example that would
179 	 * cause a restart to happen on CPU0.
180 	 */
181 	smp_num_cpus = 1;
182 }
183 
184 /* Not really SMP stuff ... */
setup_profiling_timer(unsigned int multiplier)185 int setup_profiling_timer(unsigned int multiplier)
186 {
187 	return 0;
188 }
189 
flush_tlb_all_ipi(void * info)190 static void flush_tlb_all_ipi(void *info)
191 {
192 	local_flush_tlb_all();
193 }
194 
flush_tlb_all(void)195 void flush_tlb_all(void)
196 {
197 	smp_call_function(flush_tlb_all_ipi, 0, 1, 1);
198 	local_flush_tlb_all();
199 }
200 
flush_tlb_mm_ipi(void * mm)201 static void flush_tlb_mm_ipi(void *mm)
202 {
203 	local_flush_tlb_mm((struct mm_struct *)mm);
204 }
205 
206 /*
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).
216  * Kanoj 07/00.
217  */
218 
flush_tlb_mm(struct mm_struct * mm)219 void flush_tlb_mm(struct mm_struct *mm)
220 {
221 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
222 		smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1, 1);
223 	} else {
224 		int i;
225 		for (i = 0; i < smp_num_cpus; i++)
226 			if (smp_processor_id() != i)
227 				cpu_context(i, mm) = 0;
228 	}
229 	local_flush_tlb_mm(mm);
230 }
231 
232 struct flush_tlb_data {
233 	struct mm_struct *mm;
234 	struct vm_area_struct *vma;
235 	unsigned long addr1;
236 	unsigned long addr2;
237 };
238 
flush_tlb_range_ipi(void * info)239 static void flush_tlb_range_ipi(void *info)
240 {
241 	struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
242 
243 	local_flush_tlb_range(fd->mm, fd->addr1, fd->addr2);
244 }
245 
flush_tlb_range(struct mm_struct * mm,unsigned long start,unsigned long end)246 void flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long end)
247 {
248 	if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
249 		struct flush_tlb_data fd;
250 
251 		fd.mm = mm;
252 		fd.addr1 = start;
253 		fd.addr2 = end;
254 		smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1, 1);
255 	} else {
256 		int i;
257 		for (i = 0; i < smp_num_cpus; i++)
258 			if (smp_processor_id() != i)
259 				cpu_context(i, mm) = 0;
260 	}
261 	local_flush_tlb_range(mm, start, end);
262 }
263 
flush_tlb_page_ipi(void * info)264 static void flush_tlb_page_ipi(void *info)
265 {
266 	struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
267 
268 	local_flush_tlb_page(fd->vma, fd->addr1);
269 }
270 
flush_tlb_page(struct vm_area_struct * vma,unsigned long page)271 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
272 {
273 	if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
274 		struct flush_tlb_data fd;
275 
276 		fd.vma = vma;
277 		fd.addr1 = page;
278 		smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1, 1);
279 	} else {
280 		int i;
281 		for (i = 0; i < smp_num_cpus; i++)
282 			if (smp_processor_id() != i)
283 				cpu_context(i, vma->vm_mm) = 0;
284 	}
285 	local_flush_tlb_page(vma, page);
286 }
287 
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);
296