Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[pandora-kernel.git] / arch / sparc / kernel / smp.c
1 /* smp.c: Sparc SMP support.
2  *
3  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
4  * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  * Copyright (C) 2004 Keith M Wesolowski (wesolows@foobazco.org)
6  */
7
8 #include <asm/head.h>
9
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/threads.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel_stat.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/mm.h>
20 #include <linux/fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/cache.h>
23 #include <linux/delay.h>
24
25 #include <asm/ptrace.h>
26 #include <asm/atomic.h>
27
28 #include <asm/irq.h>
29 #include <asm/page.h>
30 #include <asm/pgalloc.h>
31 #include <asm/pgtable.h>
32 #include <asm/oplib.h>
33 #include <asm/cacheflush.h>
34 #include <asm/tlbflush.h>
35 #include <asm/cpudata.h>
36
37 volatile int smp_processors_ready = 0;
38 int smp_num_cpus = 1;
39 volatile unsigned long cpu_callin_map[NR_CPUS] __initdata = {0,};
40 unsigned char boot_cpu_id = 0;
41 unsigned char boot_cpu_id4 = 0; /* boot_cpu_id << 2 */
42 int smp_activated = 0;
43 volatile int __cpu_number_map[NR_CPUS];
44 volatile int __cpu_logical_map[NR_CPUS];
45
46 cpumask_t cpu_online_map = CPU_MASK_NONE;
47 cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
48 cpumask_t smp_commenced_mask = CPU_MASK_NONE;
49
50 /* The only guaranteed locking primitive available on all Sparc
51  * processors is 'ldstub [%reg + immediate], %dest_reg' which atomically
52  * places the current byte at the effective address into dest_reg and
53  * places 0xff there afterwards.  Pretty lame locking primitive
54  * compared to the Alpha and the Intel no?  Most Sparcs have 'swap'
55  * instruction which is much better...
56  */
57
58 /* Used to make bitops atomic */
59 unsigned char bitops_spinlock = 0;
60
61 void __cpuinit smp_store_cpu_info(int id)
62 {
63         int cpu_node;
64
65         cpu_data(id).udelay_val = loops_per_jiffy;
66
67         cpu_find_by_mid(id, &cpu_node);
68         cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
69                                                      "clock-frequency", 0);
70         cpu_data(id).prom_node = cpu_node;
71         cpu_data(id).mid = cpu_get_hwmid(cpu_node);
72
73         /* this is required to tune the scheduler correctly */
74         /* is it possible to have CPUs with different cache sizes? */
75         if (id == boot_cpu_id) {
76                 int cache_line,cache_nlines;
77                 cache_line = 0x20;
78                 cache_line = prom_getintdefault(cpu_node, "ecache-line-size", cache_line);
79                 cache_nlines = 0x8000;
80                 cache_nlines = prom_getintdefault(cpu_node, "ecache-nlines", cache_nlines);
81                 max_cache_size = cache_line * cache_nlines;
82         }
83         if (cpu_data(id).mid < 0)
84                 panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
85 }
86
87 void __init smp_cpus_done(unsigned int max_cpus)
88 {
89         extern void smp4m_smp_done(void);
90         unsigned long bogosum = 0;
91         int cpu, num;
92
93         for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
94                 if (cpu_online(cpu)) {
95                         num++;
96                         bogosum += cpu_data(cpu).udelay_val;
97                 }
98
99         printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
100                 num, bogosum/(500000/HZ),
101                 (bogosum/(5000/HZ))%100);
102
103         BUG_ON(sparc_cpu_model != sun4m);
104         smp4m_smp_done();
105 }
106
107 void cpu_panic(void)
108 {
109         printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
110         panic("SMP bolixed\n");
111 }
112
113 struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
114
115 void smp_send_reschedule(int cpu)
116 {
117         /* See sparc64 */
118 }
119
120 void smp_send_stop(void)
121 {
122 }
123
124 void smp_flush_cache_all(void)
125 {
126         xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
127         local_flush_cache_all();
128 }
129
130 void smp_flush_tlb_all(void)
131 {
132         xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
133         local_flush_tlb_all();
134 }
135
136 void smp_flush_cache_mm(struct mm_struct *mm)
137 {
138         if(mm->context != NO_CONTEXT) {
139                 cpumask_t cpu_mask = mm->cpu_vm_mask;
140                 cpu_clear(smp_processor_id(), cpu_mask);
141                 if (!cpus_empty(cpu_mask))
142                         xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
143                 local_flush_cache_mm(mm);
144         }
145 }
146
147 void smp_flush_tlb_mm(struct mm_struct *mm)
148 {
149         if(mm->context != NO_CONTEXT) {
150                 cpumask_t cpu_mask = mm->cpu_vm_mask;
151                 cpu_clear(smp_processor_id(), cpu_mask);
152                 if (!cpus_empty(cpu_mask)) {
153                         xc1((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_mm), (unsigned long) mm);
154                         if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
155                                 mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
156                 }
157                 local_flush_tlb_mm(mm);
158         }
159 }
160
161 void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
162                            unsigned long end)
163 {
164         struct mm_struct *mm = vma->vm_mm;
165
166         if (mm->context != NO_CONTEXT) {
167                 cpumask_t cpu_mask = mm->cpu_vm_mask;
168                 cpu_clear(smp_processor_id(), cpu_mask);
169                 if (!cpus_empty(cpu_mask))
170                         xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
171                 local_flush_cache_range(vma, start, end);
172         }
173 }
174
175 void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
176                          unsigned long end)
177 {
178         struct mm_struct *mm = vma->vm_mm;
179
180         if (mm->context != NO_CONTEXT) {
181                 cpumask_t cpu_mask = mm->cpu_vm_mask;
182                 cpu_clear(smp_processor_id(), cpu_mask);
183                 if (!cpus_empty(cpu_mask))
184                         xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
185                 local_flush_tlb_range(vma, start, end);
186         }
187 }
188
189 void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
190 {
191         struct mm_struct *mm = vma->vm_mm;
192
193         if(mm->context != NO_CONTEXT) {
194                 cpumask_t cpu_mask = mm->cpu_vm_mask;
195                 cpu_clear(smp_processor_id(), cpu_mask);
196                 if (!cpus_empty(cpu_mask))
197                         xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
198                 local_flush_cache_page(vma, page);
199         }
200 }
201
202 void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
203 {
204         struct mm_struct *mm = vma->vm_mm;
205
206         if(mm->context != NO_CONTEXT) {
207                 cpumask_t cpu_mask = mm->cpu_vm_mask;
208                 cpu_clear(smp_processor_id(), cpu_mask);
209                 if (!cpus_empty(cpu_mask))
210                         xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
211                 local_flush_tlb_page(vma, page);
212         }
213 }
214
215 void smp_reschedule_irq(void)
216 {
217         set_need_resched();
218 }
219
220 void smp_flush_page_to_ram(unsigned long page)
221 {
222         /* Current theory is that those who call this are the one's
223          * who have just dirtied their cache with the pages contents
224          * in kernel space, therefore we only run this on local cpu.
225          *
226          * XXX This experiment failed, research further... -DaveM
227          */
228 #if 1
229         xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
230 #endif
231         local_flush_page_to_ram(page);
232 }
233
234 void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
235 {
236         cpumask_t cpu_mask = mm->cpu_vm_mask;
237         cpu_clear(smp_processor_id(), cpu_mask);
238         if (!cpus_empty(cpu_mask))
239                 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
240         local_flush_sig_insns(mm, insn_addr);
241 }
242
243 extern unsigned int lvl14_resolution;
244
245 /* /proc/profile writes can call this, don't __init it please. */
246 static DEFINE_SPINLOCK(prof_setup_lock);
247
248 int setup_profiling_timer(unsigned int multiplier)
249 {
250         int i;
251         unsigned long flags;
252
253         /* Prevent level14 ticker IRQ flooding. */
254         if((!multiplier) || (lvl14_resolution / multiplier) < 500)
255                 return -EINVAL;
256
257         spin_lock_irqsave(&prof_setup_lock, flags);
258         for_each_possible_cpu(i) {
259                 load_profile_irq(i, lvl14_resolution / multiplier);
260                 prof_multiplier(i) = multiplier;
261         }
262         spin_unlock_irqrestore(&prof_setup_lock, flags);
263
264         return 0;
265 }
266
267 void __init smp_prepare_cpus(unsigned int max_cpus)
268 {
269         extern void smp4m_boot_cpus(void);
270         int i, cpuid, extra;
271
272         BUG_ON(sparc_cpu_model != sun4m);
273         printk("Entering SMP Mode...\n");
274
275         extra = 0;
276         for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
277                 if (cpuid >= NR_CPUS)
278                         extra++;
279         }
280         /* i = number of cpus */
281         if (extra && max_cpus > i - extra)
282                 printk("Warning: NR_CPUS is too low to start all cpus\n");
283
284         smp_store_cpu_info(boot_cpu_id);
285
286         smp4m_boot_cpus();
287 }
288
289 /* Set this up early so that things like the scheduler can init
290  * properly.  We use the same cpu mask for both the present and
291  * possible cpu map.
292  */
293 void __init smp_setup_cpu_possible_map(void)
294 {
295         int instance, mid;
296
297         instance = 0;
298         while (!cpu_find_by_instance(instance, NULL, &mid)) {
299                 if (mid < NR_CPUS) {
300                         cpu_set(mid, phys_cpu_present_map);
301                         cpu_set(mid, cpu_present_map);
302                 }
303                 instance++;
304         }
305 }
306
307 void __init smp_prepare_boot_cpu(void)
308 {
309         int cpuid = hard_smp_processor_id();
310
311         if (cpuid >= NR_CPUS) {
312                 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
313                 prom_halt();
314         }
315         if (cpuid != 0)
316                 printk("boot cpu id != 0, this could work but is untested\n");
317
318         current_thread_info()->cpu = cpuid;
319         cpu_set(cpuid, cpu_online_map);
320         cpu_set(cpuid, phys_cpu_present_map);
321 }
322
323 int __cpuinit __cpu_up(unsigned int cpu)
324 {
325         extern int smp4m_boot_one_cpu(int);
326         int ret;
327
328         ret = smp4m_boot_one_cpu(cpu);
329
330         if (!ret) {
331                 cpu_set(cpu, smp_commenced_mask);
332                 while (!cpu_online(cpu))
333                         mb();
334         }
335         return ret;
336 }
337
338 void smp_bogo(struct seq_file *m)
339 {
340         int i;
341         
342         for_each_online_cpu(i) {
343                 seq_printf(m,
344                            "Cpu%dBogo\t: %lu.%02lu\n",
345                            i,
346                            cpu_data(i).udelay_val/(500000/HZ),
347                            (cpu_data(i).udelay_val/(5000/HZ))%100);
348         }
349 }
350
351 void smp_info(struct seq_file *m)
352 {
353         int i;
354
355         seq_printf(m, "State:\n");
356         for_each_online_cpu(i)
357                 seq_printf(m, "CPU%d\t\t: online\n", i);
358 }