sh: provide percpu CPU states for hotplug notifiers.
[deliverable/linux.git] / arch / sh / kernel / smp.c
1 /*
2 * arch/sh/kernel/smp.c
3 *
4 * SMP support for the SuperH processors.
5 *
6 * Copyright (C) 2002 - 2010 Paul Mundt
7 * Copyright (C) 2006 - 2007 Akio Idehara
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
11 * for more details.
12 */
13 #include <linux/err.h>
14 #include <linux/cache.h>
15 #include <linux/cpumask.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/cpu.h>
22 #include <linux/interrupt.h>
23 #include <asm/atomic.h>
24 #include <asm/processor.h>
25 #include <asm/system.h>
26 #include <asm/mmu_context.h>
27 #include <asm/smp.h>
28 #include <asm/cacheflush.h>
29 #include <asm/sections.h>
30
31 int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
32 int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
33
34 struct plat_smp_ops *mp_ops = NULL;
35
36 /* State of each CPU */
37 DEFINE_PER_CPU(int, cpu_state) = { 0 };
38
39 void __cpuinit register_smp_ops(struct plat_smp_ops *ops)
40 {
41 if (mp_ops)
42 printk(KERN_WARNING "Overriding previously set SMP ops\n");
43
44 mp_ops = ops;
45 }
46
47 static inline void __init smp_store_cpu_info(unsigned int cpu)
48 {
49 struct sh_cpuinfo *c = cpu_data + cpu;
50
51 memcpy(c, &boot_cpu_data, sizeof(struct sh_cpuinfo));
52
53 c->loops_per_jiffy = loops_per_jiffy;
54 }
55
56 void __init smp_prepare_cpus(unsigned int max_cpus)
57 {
58 unsigned int cpu = smp_processor_id();
59
60 init_new_context(current, &init_mm);
61 current_thread_info()->cpu = cpu;
62 mp_ops->prepare_cpus(max_cpus);
63
64 #ifndef CONFIG_HOTPLUG_CPU
65 init_cpu_present(&cpu_possible_map);
66 #endif
67 }
68
69 void __devinit smp_prepare_boot_cpu(void)
70 {
71 unsigned int cpu = smp_processor_id();
72
73 __cpu_number_map[0] = cpu;
74 __cpu_logical_map[0] = cpu;
75
76 set_cpu_online(cpu, true);
77 set_cpu_possible(cpu, true);
78
79 per_cpu(cpu_state, cpu) = CPU_ONLINE;
80 }
81
82 asmlinkage void __cpuinit start_secondary(void)
83 {
84 unsigned int cpu = smp_processor_id();
85 struct mm_struct *mm = &init_mm;
86
87 enable_mmu();
88 atomic_inc(&mm->mm_count);
89 atomic_inc(&mm->mm_users);
90 current->active_mm = mm;
91 BUG_ON(current->mm);
92 enter_lazy_tlb(mm, current);
93
94 per_cpu_trap_init();
95
96 preempt_disable();
97
98 notify_cpu_starting(cpu);
99
100 local_irq_enable();
101
102 /* Enable local timers */
103 local_timer_setup(cpu);
104 calibrate_delay();
105
106 smp_store_cpu_info(cpu);
107
108 set_cpu_online(cpu, true);
109 per_cpu(cpu_state, cpu) = CPU_ONLINE;
110
111 cpu_idle();
112 }
113
114 extern struct {
115 unsigned long sp;
116 unsigned long bss_start;
117 unsigned long bss_end;
118 void *start_kernel_fn;
119 void *cpu_init_fn;
120 void *thread_info;
121 } stack_start;
122
123 int __cpuinit __cpu_up(unsigned int cpu)
124 {
125 struct task_struct *tsk;
126 unsigned long timeout;
127
128 tsk = fork_idle(cpu);
129 if (IS_ERR(tsk)) {
130 printk(KERN_ERR "Failed forking idle task for cpu %d\n", cpu);
131 return PTR_ERR(tsk);
132 }
133
134 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
135
136 /* Fill in data in head.S for secondary cpus */
137 stack_start.sp = tsk->thread.sp;
138 stack_start.thread_info = tsk->stack;
139 stack_start.bss_start = 0; /* don't clear bss for secondary cpus */
140 stack_start.start_kernel_fn = start_secondary;
141
142 flush_icache_range((unsigned long)&stack_start,
143 (unsigned long)&stack_start + sizeof(stack_start));
144 wmb();
145
146 mp_ops->start_cpu(cpu, (unsigned long)_stext);
147
148 timeout = jiffies + HZ;
149 while (time_before(jiffies, timeout)) {
150 if (cpu_online(cpu))
151 break;
152
153 udelay(10);
154 }
155
156 if (cpu_online(cpu))
157 return 0;
158
159 return -ENOENT;
160 }
161
162 void __init smp_cpus_done(unsigned int max_cpus)
163 {
164 unsigned long bogosum = 0;
165 int cpu;
166
167 for_each_online_cpu(cpu)
168 bogosum += cpu_data[cpu].loops_per_jiffy;
169
170 printk(KERN_INFO "SMP: Total of %d processors activated "
171 "(%lu.%02lu BogoMIPS).\n", num_online_cpus(),
172 bogosum / (500000/HZ),
173 (bogosum / (5000/HZ)) % 100);
174 }
175
176 void smp_send_reschedule(int cpu)
177 {
178 mp_ops->send_ipi(cpu, SMP_MSG_RESCHEDULE);
179 }
180
181 void smp_send_stop(void)
182 {
183 smp_call_function(stop_this_cpu, 0, 0);
184 }
185
186 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
187 {
188 int cpu;
189
190 for_each_cpu(cpu, mask)
191 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION);
192 }
193
194 void arch_send_call_function_single_ipi(int cpu)
195 {
196 mp_ops->send_ipi(cpu, SMP_MSG_FUNCTION_SINGLE);
197 }
198
199 void smp_timer_broadcast(const struct cpumask *mask)
200 {
201 int cpu;
202
203 for_each_cpu(cpu, mask)
204 mp_ops->send_ipi(cpu, SMP_MSG_TIMER);
205 }
206
207 static void ipi_timer(void)
208 {
209 irq_enter();
210 local_timer_interrupt();
211 irq_exit();
212 }
213
214 void smp_message_recv(unsigned int msg)
215 {
216 switch (msg) {
217 case SMP_MSG_FUNCTION:
218 generic_smp_call_function_interrupt();
219 break;
220 case SMP_MSG_RESCHEDULE:
221 break;
222 case SMP_MSG_FUNCTION_SINGLE:
223 generic_smp_call_function_single_interrupt();
224 break;
225 case SMP_MSG_TIMER:
226 ipi_timer();
227 break;
228 default:
229 printk(KERN_WARNING "SMP %d: %s(): unknown IPI %d\n",
230 smp_processor_id(), __func__, msg);
231 break;
232 }
233 }
234
235 /* Not really SMP stuff ... */
236 int setup_profiling_timer(unsigned int multiplier)
237 {
238 return 0;
239 }
240
241 static void flush_tlb_all_ipi(void *info)
242 {
243 local_flush_tlb_all();
244 }
245
246 void flush_tlb_all(void)
247 {
248 on_each_cpu(flush_tlb_all_ipi, 0, 1);
249 }
250
251 static void flush_tlb_mm_ipi(void *mm)
252 {
253 local_flush_tlb_mm((struct mm_struct *)mm);
254 }
255
256 /*
257 * The following tlb flush calls are invoked when old translations are
258 * being torn down, or pte attributes are changing. For single threaded
259 * address spaces, a new context is obtained on the current cpu, and tlb
260 * context on other cpus are invalidated to force a new context allocation
261 * at switch_mm time, should the mm ever be used on other cpus. For
262 * multithreaded address spaces, intercpu interrupts have to be sent.
263 * Another case where intercpu interrupts are required is when the target
264 * mm might be active on another cpu (eg debuggers doing the flushes on
265 * behalf of debugees, kswapd stealing pages from another process etc).
266 * Kanoj 07/00.
267 */
268
269 void flush_tlb_mm(struct mm_struct *mm)
270 {
271 preempt_disable();
272
273 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
274 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1);
275 } else {
276 int i;
277 for (i = 0; i < num_online_cpus(); i++)
278 if (smp_processor_id() != i)
279 cpu_context(i, mm) = 0;
280 }
281 local_flush_tlb_mm(mm);
282
283 preempt_enable();
284 }
285
286 struct flush_tlb_data {
287 struct vm_area_struct *vma;
288 unsigned long addr1;
289 unsigned long addr2;
290 };
291
292 static void flush_tlb_range_ipi(void *info)
293 {
294 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
295
296 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
297 }
298
299 void flush_tlb_range(struct vm_area_struct *vma,
300 unsigned long start, unsigned long end)
301 {
302 struct mm_struct *mm = vma->vm_mm;
303
304 preempt_disable();
305 if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
306 struct flush_tlb_data fd;
307
308 fd.vma = vma;
309 fd.addr1 = start;
310 fd.addr2 = end;
311 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1);
312 } else {
313 int i;
314 for (i = 0; i < num_online_cpus(); i++)
315 if (smp_processor_id() != i)
316 cpu_context(i, mm) = 0;
317 }
318 local_flush_tlb_range(vma, start, end);
319 preempt_enable();
320 }
321
322 static void flush_tlb_kernel_range_ipi(void *info)
323 {
324 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
325
326 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
327 }
328
329 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
330 {
331 struct flush_tlb_data fd;
332
333 fd.addr1 = start;
334 fd.addr2 = end;
335 on_each_cpu(flush_tlb_kernel_range_ipi, (void *)&fd, 1);
336 }
337
338 static void flush_tlb_page_ipi(void *info)
339 {
340 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
341
342 local_flush_tlb_page(fd->vma, fd->addr1);
343 }
344
345 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
346 {
347 preempt_disable();
348 if ((atomic_read(&vma->vm_mm->mm_users) != 1) ||
349 (current->mm != vma->vm_mm)) {
350 struct flush_tlb_data fd;
351
352 fd.vma = vma;
353 fd.addr1 = page;
354 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1);
355 } else {
356 int i;
357 for (i = 0; i < num_online_cpus(); i++)
358 if (smp_processor_id() != i)
359 cpu_context(i, vma->vm_mm) = 0;
360 }
361 local_flush_tlb_page(vma, page);
362 preempt_enable();
363 }
364
365 static void flush_tlb_one_ipi(void *info)
366 {
367 struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
368 local_flush_tlb_one(fd->addr1, fd->addr2);
369 }
370
371 void flush_tlb_one(unsigned long asid, unsigned long vaddr)
372 {
373 struct flush_tlb_data fd;
374
375 fd.addr1 = asid;
376 fd.addr2 = vaddr;
377
378 smp_call_function(flush_tlb_one_ipi, (void *)&fd, 1);
379 local_flush_tlb_one(asid, vaddr);
380 }
This page took 0.055925 seconds and 5 git commands to generate.