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, 2003 Broadcom Corporation
20 */
21 #include <linux/cache.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/smp.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 #include <linux/cpumask.h>
33 #include <linux/cpu.h>
34 #include <linux/err.h>
35 #include <linux/ftrace.h>
36
37 #include <linux/atomic.h>
38 #include <asm/cpu.h>
39 #include <asm/processor.h>
40 #include <asm/idle.h>
41 #include <asm/r4k-timer.h>
42 #include <asm/mmu_context.h>
43 #include <asm/time.h>
44 #include <asm/setup.h>
45
46 #ifdef CONFIG_MIPS_MT_SMTC
47 #include <asm/mipsmtregs.h>
48 #endif /* CONFIG_MIPS_MT_SMTC */
49
50 volatile cpumask_t cpu_callin_map; /* Bitmask of started secondaries */
51
52 int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
53 EXPORT_SYMBOL(__cpu_number_map);
54
55 int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
56 EXPORT_SYMBOL(__cpu_logical_map);
57
58 /* Number of TCs (or siblings in Intel speak) per CPU core */
59 int smp_num_siblings = 1;
60 EXPORT_SYMBOL(smp_num_siblings);
61
62 /* representing the TCs (or siblings in Intel speak) of each logical CPU */
63 DEFINE_PER_CPU_SHARED_ALIGNED(struct cpumask, cpu_sibling_map);
64 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
65
66 /* representing cpus for which sibling maps can be computed */
67 static cpumask_t cpu_sibling_setup_map;
68
69 extern void maar_setup(void);
70
71 /* CPU siblings in MIPS:
72 *
73 * SMVP kernel - VPEs on common core are siblings
74 * SMTC kernel - TC's on common core are siblings
75 */
set_cpu_sibling_map(int cpu)76 static inline void set_cpu_sibling_map(int cpu)
77 {
78 int i;
79
80 cpu_set(cpu, cpu_sibling_setup_map);
81
82 if (smp_num_siblings > 1) {
83 for_each_cpu_mask(i, cpu_sibling_setup_map) {
84 if (cpu_data[cpu].core == cpu_data[i].core) {
85 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
86 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
87 }
88 }
89 } else
90 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
91 }
92
93 struct plat_smp_ops *mp_ops;
94 EXPORT_SYMBOL(mp_ops);
95
register_smp_ops(struct plat_smp_ops * ops)96 __cpuinit void register_smp_ops(struct plat_smp_ops *ops)
97 {
98 if (mp_ops)
99 printk(KERN_WARNING "Overriding previously set SMP ops\n");
100
101 mp_ops = ops;
102 }
103
104 /*
105 * First C code run on the secondary CPUs after being started up by
106 * the master.
107 */
start_secondary(void)108 asmlinkage __cpuinit void start_secondary(void)
109 {
110 unsigned int cpu;
111
112 #ifdef CONFIG_MIPS_MT_SMTC
113 /* Only do cpu_probe for first TC of CPU */
114 if ((read_c0_tcbind() & TCBIND_CURTC) != 0)
115 __cpu_name[smp_processor_id()] = __cpu_name[0];
116 else
117 #endif /* CONFIG_MIPS_MT_SMTC */
118 cpu_probe();
119 cpu_report();
120 maar_setup();
121 per_cpu_trap_init(false);
122 mips_clockevent_init();
123 mp_ops->init_secondary();
124
125 /*
126 * XXX parity protection should be folded in here when it's converted
127 * to an option instead of something based on .cputype
128 */
129
130 calibrate_delay();
131 preempt_disable();
132 cpu = smp_processor_id();
133 cpu_data[cpu].udelay_val = loops_per_jiffy;
134
135 notify_cpu_starting(cpu);
136
137 set_cpu_online(cpu, true);
138
139 set_cpu_sibling_map(cpu);
140
141 cpu_set(cpu, cpu_callin_map);
142
143 synchronise_count_slave(cpu);
144
145 /*
146 * irq will be enabled in ->smp_finish(), enabling it too early
147 * is dangerous.
148 */
149 WARN_ON_ONCE(!irqs_disabled());
150 mp_ops->smp_finish();
151
152 cpu_startup_entry(CPUHP_ONLINE);
153 }
154
155 /*
156 * Call into both interrupt handlers, as we share the IPI for them
157 */
smp_call_function_interrupt(void)158 void __irq_entry smp_call_function_interrupt(void)
159 {
160 irq_enter();
161 generic_smp_call_function_single_interrupt();
162 generic_smp_call_function_interrupt();
163 irq_exit();
164 }
165
stop_this_cpu(void * dummy)166 static void stop_this_cpu(void *dummy)
167 {
168 /*
169 * Remove this CPU:
170 */
171 set_cpu_online(smp_processor_id(), false);
172 for (;;) {
173 if (cpu_wait)
174 (*cpu_wait)(); /* Wait if available. */
175 }
176 }
177
smp_send_stop(void)178 void smp_send_stop(void)
179 {
180 smp_call_function(stop_this_cpu, NULL, 0);
181 }
182
smp_cpus_done(unsigned int max_cpus)183 void __init smp_cpus_done(unsigned int max_cpus)
184 {
185 mp_ops->cpus_done();
186 }
187
188 /* called from main before smp_init() */
smp_prepare_cpus(unsigned int max_cpus)189 void __init smp_prepare_cpus(unsigned int max_cpus)
190 {
191 init_new_context(current, &init_mm);
192 current_thread_info()->cpu = 0;
193 mp_ops->prepare_cpus(max_cpus);
194 set_cpu_sibling_map(0);
195 #ifndef CONFIG_HOTPLUG_CPU
196 init_cpu_present(cpu_possible_mask);
197 #endif
198 }
199
200 /* preload SMP state for boot cpu */
smp_prepare_boot_cpu(void)201 void smp_prepare_boot_cpu(void)
202 {
203 set_cpu_possible(0, true);
204 set_cpu_online(0, true);
205 cpu_set(0, cpu_callin_map);
206 }
207
__cpu_up(unsigned int cpu,struct task_struct * tidle)208 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
209 {
210 mp_ops->boot_secondary(cpu, tidle);
211
212 /*
213 * Trust is futile. We should really have timeouts ...
214 */
215 while (!cpu_isset(cpu, cpu_callin_map))
216 udelay(100);
217
218 synchronise_count_master(cpu);
219 return 0;
220 }
221
222 /* Not really SMP stuff ... */
setup_profiling_timer(unsigned int multiplier)223 int setup_profiling_timer(unsigned int multiplier)
224 {
225 return 0;
226 }
227
flush_tlb_all_ipi(void * info)228 static void flush_tlb_all_ipi(void *info)
229 {
230 local_flush_tlb_all();
231 }
232
flush_tlb_all(void)233 void flush_tlb_all(void)
234 {
235 on_each_cpu(flush_tlb_all_ipi, NULL, 1);
236 }
237
flush_tlb_mm_ipi(void * mm)238 static void flush_tlb_mm_ipi(void *mm)
239 {
240 local_flush_tlb_mm((struct mm_struct *)mm);
241 }
242
243 /*
244 * Special Variant of smp_call_function for use by TLB functions:
245 *
246 * o No return value
247 * o collapses to normal function call on UP kernels
248 * o collapses to normal function call on systems with a single shared
249 * primary cache.
250 * o CONFIG_MIPS_MT_SMTC currently implies there is only one physical core.
251 */
smp_on_other_tlbs(void (* func)(void * info),void * info)252 static inline void smp_on_other_tlbs(void (*func) (void *info), void *info)
253 {
254 #ifndef CONFIG_MIPS_MT_SMTC
255 smp_call_function(func, info, 1);
256 #endif
257 }
258
smp_on_each_tlb(void (* func)(void * info),void * info)259 static inline void smp_on_each_tlb(void (*func) (void *info), void *info)
260 {
261 preempt_disable();
262
263 smp_on_other_tlbs(func, info);
264 func(info);
265
266 preempt_enable();
267 }
268
269 /*
270 * The following tlb flush calls are invoked when old translations are
271 * being torn down, or pte attributes are changing. For single threaded
272 * address spaces, a new context is obtained on the current cpu, and tlb
273 * context on other cpus are invalidated to force a new context allocation
274 * at switch_mm time, should the mm ever be used on other cpus. For
275 * multithreaded address spaces, intercpu interrupts have to be sent.
276 * Another case where intercpu interrupts are required is when the target
277 * mm might be active on another cpu (eg debuggers doing the flushes on
278 * behalf of debugees, kswapd stealing pages from another process etc).
279 * Kanoj 07/00.
280 */
281
flush_tlb_mm(struct mm_struct * mm)282 void flush_tlb_mm(struct mm_struct *mm)
283 {
284 preempt_disable();
285
286 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
287 smp_on_other_tlbs(flush_tlb_mm_ipi, mm);
288 } else {
289 unsigned int cpu;
290
291 for_each_online_cpu(cpu) {
292 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
293 cpu_context(cpu, mm) = 0;
294 }
295 }
296 local_flush_tlb_mm(mm);
297
298 preempt_enable();
299 }
300
301 struct flush_tlb_data {
302 struct vm_area_struct *vma;
303 unsigned long addr1;
304 unsigned long addr2;
305 };
306
flush_tlb_range_ipi(void * info)307 static void flush_tlb_range_ipi(void *info)
308 {
309 struct flush_tlb_data *fd = info;
310
311 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
312 }
313
flush_tlb_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)314 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
315 {
316 struct mm_struct *mm = vma->vm_mm;
317
318 preempt_disable();
319 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
320 struct flush_tlb_data fd = {
321 .vma = vma,
322 .addr1 = start,
323 .addr2 = end,
324 };
325
326 smp_on_other_tlbs(flush_tlb_range_ipi, &fd);
327 } else {
328 unsigned int cpu;
329
330 for_each_online_cpu(cpu) {
331 if (cpu != smp_processor_id() && cpu_context(cpu, mm))
332 cpu_context(cpu, mm) = 0;
333 }
334 }
335 local_flush_tlb_range(vma, start, end);
336 preempt_enable();
337 }
338
flush_tlb_kernel_range_ipi(void * info)339 static void flush_tlb_kernel_range_ipi(void *info)
340 {
341 struct flush_tlb_data *fd = info;
342
343 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
344 }
345
flush_tlb_kernel_range(unsigned long start,unsigned long end)346 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
347 {
348 struct flush_tlb_data fd = {
349 .addr1 = start,
350 .addr2 = end,
351 };
352
353 on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
354 }
355
flush_tlb_page_ipi(void * info)356 static void flush_tlb_page_ipi(void *info)
357 {
358 struct flush_tlb_data *fd = info;
359
360 local_flush_tlb_page(fd->vma, fd->addr1);
361 }
362
flush_tlb_page(struct vm_area_struct * vma,unsigned long page)363 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
364 {
365 preempt_disable();
366 if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
367 struct flush_tlb_data fd = {
368 .vma = vma,
369 .addr1 = page,
370 };
371
372 smp_on_other_tlbs(flush_tlb_page_ipi, &fd);
373 } else {
374 unsigned int cpu;
375
376 for_each_online_cpu(cpu) {
377 if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
378 cpu_context(cpu, vma->vm_mm) = 0;
379 }
380 }
381 local_flush_tlb_page(vma, page);
382 preempt_enable();
383 }
384
flush_tlb_one_ipi(void * info)385 static void flush_tlb_one_ipi(void *info)
386 {
387 unsigned long vaddr = (unsigned long) info;
388
389 local_flush_tlb_one(vaddr);
390 }
391
flush_tlb_one(unsigned long vaddr)392 void flush_tlb_one(unsigned long vaddr)
393 {
394 smp_on_each_tlb(flush_tlb_one_ipi, (void *) vaddr);
395 }
396
397 EXPORT_SYMBOL(flush_tlb_page);
398 EXPORT_SYMBOL(flush_tlb_one);
399
400 #if defined(CONFIG_KEXEC)
401 void (*dump_ipi_function_ptr)(void *) = NULL;
dump_send_ipi(void (* dump_ipi_callback)(void *))402 void dump_send_ipi(void (*dump_ipi_callback)(void *))
403 {
404 int i;
405 int cpu = smp_processor_id();
406
407 dump_ipi_function_ptr = dump_ipi_callback;
408 smp_mb();
409 for_each_online_cpu(i)
410 if (i != cpu)
411 mp_ops->send_ipi_single(i, SMP_DUMP);
412
413 }
414 EXPORT_SYMBOL(dump_send_ipi);
415 #endif
416