Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[sfrench/cifs-2.6.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 __init 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         if (cpu_data(id).mid < 0)
73                 panic("No MID found for CPU%d at node 0x%08d", id, cpu_node);
74 }
75
76 void __init smp_cpus_done(unsigned int max_cpus)
77 {
78         extern void smp4m_smp_done(void);
79         unsigned long bogosum = 0;
80         int cpu, num;
81
82         for (cpu = 0, num = 0; cpu < NR_CPUS; cpu++)
83                 if (cpu_online(cpu)) {
84                         num++;
85                         bogosum += cpu_data(cpu).udelay_val;
86                 }
87
88         printk("Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
89                 num, bogosum/(500000/HZ),
90                 (bogosum/(5000/HZ))%100);
91
92         BUG_ON(sparc_cpu_model != sun4m);
93         smp4m_smp_done();
94 }
95
96 void cpu_panic(void)
97 {
98         printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
99         panic("SMP bolixed\n");
100 }
101
102 struct linux_prom_registers smp_penguin_ctable __initdata = { 0 };
103
104 void smp_send_reschedule(int cpu)
105 {
106         /* See sparc64 */
107 }
108
109 void smp_send_stop(void)
110 {
111 }
112
113 void smp_flush_cache_all(void)
114 {
115         xc0((smpfunc_t) BTFIXUP_CALL(local_flush_cache_all));
116         local_flush_cache_all();
117 }
118
119 void smp_flush_tlb_all(void)
120 {
121         xc0((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_all));
122         local_flush_tlb_all();
123 }
124
125 void smp_flush_cache_mm(struct mm_struct *mm)
126 {
127         if(mm->context != NO_CONTEXT) {
128                 cpumask_t cpu_mask = mm->cpu_vm_mask;
129                 cpu_clear(smp_processor_id(), cpu_mask);
130                 if (!cpus_empty(cpu_mask))
131                         xc1((smpfunc_t) BTFIXUP_CALL(local_flush_cache_mm), (unsigned long) mm);
132                 local_flush_cache_mm(mm);
133         }
134 }
135
136 void smp_flush_tlb_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_tlb_mm), (unsigned long) mm);
143                         if(atomic_read(&mm->mm_users) == 1 && current->active_mm == mm)
144                                 mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
145                 }
146                 local_flush_tlb_mm(mm);
147         }
148 }
149
150 void smp_flush_cache_range(struct vm_area_struct *vma, unsigned long start,
151                            unsigned long end)
152 {
153         struct mm_struct *mm = vma->vm_mm;
154
155         if (mm->context != NO_CONTEXT) {
156                 cpumask_t cpu_mask = mm->cpu_vm_mask;
157                 cpu_clear(smp_processor_id(), cpu_mask);
158                 if (!cpus_empty(cpu_mask))
159                         xc3((smpfunc_t) BTFIXUP_CALL(local_flush_cache_range), (unsigned long) vma, start, end);
160                 local_flush_cache_range(vma, start, end);
161         }
162 }
163
164 void smp_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
165                          unsigned long end)
166 {
167         struct mm_struct *mm = vma->vm_mm;
168
169         if (mm->context != NO_CONTEXT) {
170                 cpumask_t cpu_mask = mm->cpu_vm_mask;
171                 cpu_clear(smp_processor_id(), cpu_mask);
172                 if (!cpus_empty(cpu_mask))
173                         xc3((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_range), (unsigned long) vma, start, end);
174                 local_flush_tlb_range(vma, start, end);
175         }
176 }
177
178 void smp_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
179 {
180         struct mm_struct *mm = vma->vm_mm;
181
182         if(mm->context != NO_CONTEXT) {
183                 cpumask_t cpu_mask = mm->cpu_vm_mask;
184                 cpu_clear(smp_processor_id(), cpu_mask);
185                 if (!cpus_empty(cpu_mask))
186                         xc2((smpfunc_t) BTFIXUP_CALL(local_flush_cache_page), (unsigned long) vma, page);
187                 local_flush_cache_page(vma, page);
188         }
189 }
190
191 void smp_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
192 {
193         struct mm_struct *mm = vma->vm_mm;
194
195         if(mm->context != NO_CONTEXT) {
196                 cpumask_t cpu_mask = mm->cpu_vm_mask;
197                 cpu_clear(smp_processor_id(), cpu_mask);
198                 if (!cpus_empty(cpu_mask))
199                         xc2((smpfunc_t) BTFIXUP_CALL(local_flush_tlb_page), (unsigned long) vma, page);
200                 local_flush_tlb_page(vma, page);
201         }
202 }
203
204 void smp_reschedule_irq(void)
205 {
206         set_need_resched();
207 }
208
209 void smp_flush_page_to_ram(unsigned long page)
210 {
211         /* Current theory is that those who call this are the one's
212          * who have just dirtied their cache with the pages contents
213          * in kernel space, therefore we only run this on local cpu.
214          *
215          * XXX This experiment failed, research further... -DaveM
216          */
217 #if 1
218         xc1((smpfunc_t) BTFIXUP_CALL(local_flush_page_to_ram), page);
219 #endif
220         local_flush_page_to_ram(page);
221 }
222
223 void smp_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
224 {
225         cpumask_t cpu_mask = mm->cpu_vm_mask;
226         cpu_clear(smp_processor_id(), cpu_mask);
227         if (!cpus_empty(cpu_mask))
228                 xc2((smpfunc_t) BTFIXUP_CALL(local_flush_sig_insns), (unsigned long) mm, insn_addr);
229         local_flush_sig_insns(mm, insn_addr);
230 }
231
232 extern unsigned int lvl14_resolution;
233
234 /* /proc/profile writes can call this, don't __init it please. */
235 static DEFINE_SPINLOCK(prof_setup_lock);
236
237 int setup_profiling_timer(unsigned int multiplier)
238 {
239         int i;
240         unsigned long flags;
241
242         /* Prevent level14 ticker IRQ flooding. */
243         if((!multiplier) || (lvl14_resolution / multiplier) < 500)
244                 return -EINVAL;
245
246         spin_lock_irqsave(&prof_setup_lock, flags);
247         for_each_cpu(i) {
248                 load_profile_irq(i, lvl14_resolution / multiplier);
249                 prof_multiplier(i) = multiplier;
250         }
251         spin_unlock_irqrestore(&prof_setup_lock, flags);
252
253         return 0;
254 }
255
256 void __init smp_prepare_cpus(unsigned int max_cpus)
257 {
258         extern void smp4m_boot_cpus(void);
259         int i, cpuid, ncpus, extra;
260
261         BUG_ON(sparc_cpu_model != sun4m);
262         printk("Entering SMP Mode...\n");
263
264         ncpus = 1;
265         extra = 0;
266         for (i = 0; !cpu_find_by_instance(i, NULL, &cpuid); i++) {
267                 if (cpuid == boot_cpu_id)
268                         continue;
269                 if (cpuid < NR_CPUS && ncpus++ < max_cpus)
270                         cpu_set(cpuid, phys_cpu_present_map);
271                 else
272                         extra++;
273         }
274         if (max_cpus >= NR_CPUS && extra)
275                 printk("Warning: NR_CPUS is too low to start all cpus\n");
276
277         smp_store_cpu_info(boot_cpu_id);
278
279         smp4m_boot_cpus();
280 }
281
282 void __devinit smp_prepare_boot_cpu(void)
283 {
284         int cpuid = hard_smp_processor_id();
285
286         if (cpuid >= NR_CPUS) {
287                 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
288                 prom_halt();
289         }
290         if (cpuid != 0)
291                 printk("boot cpu id != 0, this could work but is untested\n");
292
293         current_thread_info()->cpu = cpuid;
294         cpu_set(cpuid, cpu_online_map);
295         cpu_set(cpuid, phys_cpu_present_map);
296 }
297
298 int __devinit __cpu_up(unsigned int cpu)
299 {
300         extern int smp4m_boot_one_cpu(int);
301         int ret;
302
303         ret = smp4m_boot_one_cpu(cpu);
304
305         if (!ret) {
306                 cpu_set(cpu, smp_commenced_mask);
307                 while (!cpu_online(cpu))
308                         mb();
309         }
310         return ret;
311 }
312
313 void smp_bogo(struct seq_file *m)
314 {
315         int i;
316         
317         for_each_online_cpu(i) {
318                 seq_printf(m,
319                            "Cpu%dBogo\t: %lu.%02lu\n",
320                            i,
321                            cpu_data(i).udelay_val/(500000/HZ),
322                            (cpu_data(i).udelay_val/(5000/HZ))%100);
323         }
324 }
325
326 void smp_info(struct seq_file *m)
327 {
328         int i;
329
330         seq_printf(m, "State:\n");
331         for_each_online_cpu(i)
332                 seq_printf(m, "CPU%d\t\t: online\n", i);
333 }