also update spec file ...
[linux-flexiantxendom0-3.2.10.git] / arch / mips64 / kernel / smp.c
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/mmu_context.h>
39 #include <asm/smp.h>
40
41 int smp_threads_ready;  /* Not used */
42
43 // static atomic_t cpus_booted = ATOMIC_INIT(0);
44 atomic_t cpus_booted = ATOMIC_INIT(0);
45
46 cpumask_t phys_cpu_present_map;         /* Bitmask of physically CPUs */
47 cpumask_t cpu_online_map;               /* Bitmask of currently online CPUs */
48 int __cpu_number_map[NR_CPUS];
49 int __cpu_logical_map[NR_CPUS];
50
51 /* These are defined by the board-specific code. */
52
53 /*
54  * Cause the function described by call_data to be executed on the passed
55  * cpu.  When the function has finished, increment the finished field of
56  * call_data.
57  */
58 void core_send_ipi(int cpu, unsigned int action);
59
60 /*
61  * Clear all undefined state in the cpu, set up sp and gp to the passed
62  * values, and kick the cpu into smp_bootstrap();
63  */
64 void prom_boot_secondary(int cpu, unsigned long sp, unsigned long gp);
65
66 /*
67  *  After we've done initial boot, this function is called to allow the
68  *  board code to clean up state, if needed
69  */
70 void prom_init_secondary(void);
71
72 void prom_smp_finish(void);
73
74 cycles_t cacheflush_time;
75 unsigned long cache_decay_ticks;
76
77 void smp_tune_scheduling (void)
78 {
79         struct cache_desc *cd = &current_cpu_data.scache;
80         unsigned long cachesize;       /* kB   */
81         unsigned long bandwidth = 350; /* MB/s */
82         unsigned long cpu_khz;
83
84         /*
85          * Crude estimate until we actually meassure ...
86          */
87         cpu_khz = loops_per_jiffy * 2 * HZ / 1000;
88
89         /*
90          * Rough estimation for SMP scheduling, this is the number of
91          * cycles it takes for a fully memory-limited process to flush
92          * the SMP-local cache.
93          *
94          * (For a P5 this pretty much means we will choose another idle
95          *  CPU almost always at wakeup time (this is due to the small
96          *  L1 cache), on PIIs it's around 50-100 usecs, depending on
97          *  the cache size)
98          */
99         if (!cpu_khz) {
100                 /*
101                  * This basically disables processor-affinity scheduling on SMP
102                  * without a cycle counter.  Currently all SMP capable MIPS
103                  * processors have a cycle counter.
104                  */
105                 cacheflush_time = 0;
106                 return;
107         }
108
109         cachesize = cd->linesz * cd->sets * cd->ways;
110         cacheflush_time = (cpu_khz>>10) * (cachesize<<10) / bandwidth;
111         cache_decay_ticks = (long)cacheflush_time/cpu_khz * HZ / 1000;
112
113         printk("per-CPU timeslice cutoff: %ld.%02ld usecs.\n",
114                 (long)cacheflush_time/(cpu_khz/1000),
115                 ((long)cacheflush_time*100/(cpu_khz/1000)) % 100);
116         printk("task migration cache decay timeout: %ld msecs.\n",
117                 (cache_decay_ticks + 1) * 1000 / HZ);
118 }
119
120 void __init smp_callin(void)
121 {
122 #if 0
123         calibrate_delay();
124         smp_store_cpu_info(cpuid);
125 #endif
126 }
127
128 #ifndef CONFIG_SGI_IP27
129 /*
130  * Hook for doing final board-specific setup after the generic smp setup
131  * is done
132  */
133 asmlinkage void start_secondary(void)
134 {
135         unsigned int cpu = smp_processor_id();
136
137         cpu_probe();
138         prom_init_secondary();
139         per_cpu_trap_init();
140
141         /*
142          * XXX parity protection should be folded in here when it's converted
143          * to an option instead of something based on .cputype
144          */
145         pgd_current[cpu] = init_mm.pgd;
146         cpu_data[cpu].udelay_val = loops_per_jiffy;
147         prom_smp_finish();
148         printk("Slave cpu booted successfully\n");
149         CPUMASK_SETB(cpu_online_map, cpu);
150         atomic_inc(&cpus_booted);
151         cpu_idle();
152 }
153 #endif /* CONFIG_SGI_IP27 */
154
155 /*
156  * this function sends a 'reschedule' IPI to another CPU.
157  * it goes straight through and wastes no time serializing
158  * anything. Worst case is that we lose a reschedule ...
159  */
160 void smp_send_reschedule(int cpu)
161 {
162         core_send_ipi(cpu, SMP_RESCHEDULE_YOURSELF);
163 }
164
165 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
166
167 struct call_data_struct *call_data;
168
169 /*
170  * Run a function on all other CPUs.
171  *  <func>      The function to run. This must be fast and non-blocking.
172  *  <info>      An arbitrary pointer to pass to the function.
173  *  <retry>     If true, keep retrying until ready.
174  *  <wait>      If true, wait until function has completed on other CPUs.
175  *  [RETURNS]   0 on success, else a negative status code.
176  *
177  * Does not return until remote CPUs are nearly ready to execute <func>
178  * or are or have executed.
179  *
180  * You must not call this function with disabled interrupts or from a
181  * hardware interrupt handler or from a bottom half handler.
182  */
183 int smp_call_function (void (*func) (void *info), void *info, int retry,
184                                                                 int wait)
185 {
186         struct call_data_struct data;
187         int i, cpus = num_online_cpus() - 1;
188         int cpu = smp_processor_id();
189
190         if (!cpus)
191                 return 0;
192
193         data.func = func;
194         data.info = info;
195         atomic_set(&data.started, 0);
196         data.wait = wait;
197         if (wait)
198                 atomic_set(&data.finished, 0);
199
200         spin_lock(&call_lock);
201         call_data = &data;
202
203         /* Send a message to all other CPUs and wait for them to respond */
204         for (i = 0; i < NR_CPUS; i++)
205                 if (cpu_online(cpu) && cpu != smp_processor_id())
206                         core_send_ipi(i, SMP_CALL_FUNCTION);
207
208         /* Wait for response */
209         /* FIXME: lock-up detection, backtrace on lock-up */
210         while (atomic_read(&data.started) != cpus)
211                 barrier();
212
213         if (wait)
214                 while (atomic_read(&data.finished) != cpus)
215                         barrier();
216         spin_unlock(&call_lock);
217
218         return 0;
219 }
220
221 void smp_call_function_interrupt(void)
222 {
223         void (*func) (void *info) = call_data->func;
224         void *info = call_data->info;
225         int wait = call_data->wait;
226
227         irq_enter();
228         /*
229          * Notify initiating CPU that I've grabbed the data and am
230          * about to execute the function.
231          */
232         mb();
233         atomic_inc(&call_data->started);
234
235         /*
236          * At this point the info structure may be out of scope unless wait==1.
237          */
238         irq_enter();
239         (*func)(info);
240         irq_exit();
241
242         if (wait) {
243                 mb();
244                 atomic_inc(&call_data->finished);
245         }
246 }
247
248 static void stop_this_cpu(void *dummy)
249 {
250         /*
251          * Remove this CPU:
252          */
253         clear_bit(smp_processor_id(), &cpu_online_map);
254         local_irq_enable();     /* May need to service _machine_restart IPI */
255         for (;;);               /* Wait if available. */
256 }
257
258 void smp_send_stop(void)
259 {
260         smp_call_function(stop_this_cpu, NULL, 1, 0);
261 }
262
263 /* Not really SMP stuff ... */
264 int setup_profiling_timer(unsigned int multiplier)
265 {
266         return 0;
267 }
268
269 static void flush_tlb_all_ipi(void *info)
270 {
271         local_flush_tlb_all();
272 }
273
274 void flush_tlb_all(void)
275 {
276         on_each_cpu(flush_tlb_all_ipi, 0, 1, 1);
277 }
278
279 static void flush_tlb_mm_ipi(void *mm)
280 {
281         local_flush_tlb_mm((struct mm_struct *)mm);
282 }
283
284 /*
285  * The following tlb flush calls are invoked when old translations are
286  * being torn down, or pte attributes are changing. For single threaded
287  * address spaces, a new context is obtained on the current cpu, and tlb
288  * context on other cpus are invalidated to force a new context allocation
289  * at switch_mm time, should the mm ever be used on other cpus. For
290  * multithreaded address spaces, intercpu interrupts have to be sent.
291  * Another case where intercpu interrupts are required is when the target
292  * mm might be active on another cpu (eg debuggers doing the flushes on
293  * behalf of debugees, kswapd stealing pages from another process etc).
294  * Kanoj 07/00.
295  */
296
297 void flush_tlb_mm(struct mm_struct *mm)
298 {
299         preempt_disable();
300
301         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
302                 smp_call_function(flush_tlb_mm_ipi, (void *)mm, 1, 1);
303         } else {
304                 int i;
305                 for (i = 0; i < num_online_cpus(); i++)
306                         if (smp_processor_id() != i)
307                                 cpu_context(i, mm) = 0;
308         }
309         local_flush_tlb_mm(mm);
310
311         preempt_enable();
312 }
313
314 struct flush_tlb_data {
315         struct vm_area_struct *vma;
316         unsigned long addr1;
317         unsigned long addr2;
318 };
319
320 static void flush_tlb_range_ipi(void *info)
321 {
322         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
323
324         local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
325 }
326
327 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
328 {
329         struct mm_struct *mm = vma->vm_mm;
330
331         preempt_disable();
332         if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
333                 struct flush_tlb_data fd;
334
335                 fd.vma = vma;
336                 fd.addr1 = start;
337                 fd.addr2 = end;
338                 smp_call_function(flush_tlb_range_ipi, (void *)&fd, 1, 1);
339         } else {
340                 int i;
341                 for (i = 0; i < num_online_cpus(); i++)
342                         if (smp_processor_id() != i)
343                                 cpu_context(i, mm) = 0;
344         }
345         local_flush_tlb_range(vma, start, end);
346         preempt_enable();
347 }
348
349 static void flush_tlb_kernel_range_ipi(void *info)
350 {
351         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
352
353         local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
354 }
355
356 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
357 {
358         struct flush_tlb_data fd;
359
360         fd.addr1 = start;
361         fd.addr2 = end;
362         smp_call_function(flush_tlb_kernel_range_ipi, (void *)&fd, 1, 1);
363         local_flush_tlb_kernel_range(start, end);
364 }
365
366 static void flush_tlb_page_ipi(void *info)
367 {
368         struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
369
370         local_flush_tlb_page(fd->vma, fd->addr1);
371 }
372
373 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
374 {
375         preempt_disable();
376         if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
377                 struct flush_tlb_data fd;
378
379                 fd.vma = vma;
380                 fd.addr1 = page;
381                 smp_call_function(flush_tlb_page_ipi, (void *)&fd, 1, 1);
382         } else {
383                 int i;
384                 for (i = 0; i < num_online_cpus(); i++)
385                         if (smp_processor_id() != i)
386                                 cpu_context(i, vma->vm_mm) = 0;
387         }
388         local_flush_tlb_page(vma, page);
389         preempt_enable();
390 }
391
392 static void flush_tlb_one_ipi(void *info)
393 {
394         unsigned long vaddr = (unsigned long) info;
395
396         local_flush_tlb_one(vaddr);
397 }
398
399 void flush_tlb_one(unsigned long vaddr)
400 {
401         smp_call_function(flush_tlb_one_ipi, (void *) vaddr, 1, 1);
402         local_flush_tlb_one(vaddr);
403 }
404
405 EXPORT_SYMBOL(flush_tlb_page);
406 EXPORT_SYMBOL(flush_tlb_one);
407 EXPORT_SYMBOL(cpu_data);
408 EXPORT_SYMBOL(synchronize_irq);