blob: f7fa873c800df5fb4bea56ad3c27527ef4863ba0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* smp.c: Sparc64 SMP support.
2 *
3 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
4 */
5
6#include <linux/module.h>
7#include <linux/kernel.h>
8#include <linux/sched.h>
9#include <linux/mm.h>
10#include <linux/pagemap.h>
11#include <linux/threads.h>
12#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
15#include <linux/delay.h>
16#include <linux/init.h>
17#include <linux/spinlock.h>
18#include <linux/fs.h>
19#include <linux/seq_file.h>
20#include <linux/cache.h>
21#include <linux/jiffies.h>
22#include <linux/profile.h>
23#include <linux/bootmem.h>
24
25#include <asm/head.h>
26#include <asm/ptrace.h>
27#include <asm/atomic.h>
28#include <asm/tlbflush.h>
29#include <asm/mmu_context.h>
30#include <asm/cpudata.h>
31
32#include <asm/irq.h>
Al Viro6d24c8d2006-10-08 08:23:28 -040033#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/page.h>
35#include <asm/pgtable.h>
36#include <asm/oplib.h>
37#include <asm/uaccess.h>
38#include <asm/timer.h>
39#include <asm/starfire.h>
40#include <asm/tlb.h>
David S. Miller56fb4df2006-02-26 23:24:22 -080041#include <asm/sections.h>
David S. Miller07f8e5f2006-06-21 23:34:02 -070042#include <asm/prom.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070043#include <asm/mdesc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045extern void calibrate_delay(void);
46
47/* Please don't make this stuff initdata!!! --DaveM */
David S. Miller777a4472007-02-22 06:24:10 -080048unsigned char boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andrew Mortonc12a8282005-07-12 12:09:43 -070050cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;
51cpumask_t phys_cpu_present_map __read_mostly = CPU_MASK_NONE;
David S. Miller8935dce2006-03-08 16:09:19 -080052cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly =
53 { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static cpumask_t smp_commenced_mask;
55static cpumask_t cpu_callout_map;
56
57void smp_info(struct seq_file *m)
58{
59 int i;
60
61 seq_printf(m, "State:\n");
Andrew Morton394e3902006-03-23 03:01:05 -080062 for_each_online_cpu(i)
63 seq_printf(m, "CPU%d:\t\tonline\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66void smp_bogo(struct seq_file *m)
67{
68 int i;
69
Andrew Morton394e3902006-03-23 03:01:05 -080070 for_each_online_cpu(i)
71 seq_printf(m,
72 "Cpu%dBogo\t: %lu.%02lu\n"
73 "Cpu%dClkTck\t: %016lx\n",
74 i, cpu_data(i).udelay_val / (500000/HZ),
75 (cpu_data(i).udelay_val / (5000/HZ)) % 100,
76 i, cpu_data(i).clock_tick);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
David S. Miller112f4872007-03-05 15:28:37 -080079extern void setup_sparc64_timer(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static volatile unsigned long callin_flag = 0;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083void __init smp_callin(void)
84{
85 int cpuid = hard_smp_processor_id();
86
David S. Miller56fb4df2006-02-26 23:24:22 -080087 __local_per_cpu_offset = __per_cpu_offset(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David S. Miller4a07e642006-02-14 13:49:32 -080089 if (tlb_type == hypervisor)
David S. Miller490384e2006-02-11 14:41:18 -080090 sun4v_ktsb_register();
David S. Miller481295f2006-02-07 21:51:08 -080091
David S. Miller56fb4df2006-02-26 23:24:22 -080092 __flush_tlb_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David S. Miller112f4872007-03-05 15:28:37 -080094 setup_sparc64_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
David S. Miller816242d2005-05-23 15:52:08 -070096 if (cheetah_pcache_forced_on)
97 cheetah_enable_pcache();
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 local_irq_enable();
100
101 calibrate_delay();
David S. Miller5cbc3072007-05-25 15:49:59 -0700102 cpu_data(cpuid).udelay_val = loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 callin_flag = 1;
104 __asm__ __volatile__("membar #Sync\n\t"
105 "flush %%g6" : : : "memory");
106
107 /* Clear this or we will die instantly when we
108 * schedule back to this idler...
109 */
David S. Millerdb7d9a42005-07-24 19:36:26 -0700110 current_thread_info()->new_child = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* Attach to the address space of init_task. */
113 atomic_inc(&init_mm.mm_count);
114 current->active_mm = &init_mm;
115
116 while (!cpu_isset(cpuid, smp_commenced_mask))
David S. Miller4f071182005-08-29 12:46:22 -0700117 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 cpu_set(cpuid, cpu_online_map);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800120
121 /* idle thread is expected to have preempt disabled */
122 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125void cpu_panic(void)
126{
127 printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
128 panic("SMP bolixed\n");
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* This tick register synchronization scheme is taken entirely from
132 * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit.
133 *
134 * The only change I've made is to rework it so that the master
135 * initiates the synchonization instead of the slave. -DaveM
136 */
137
138#define MASTER 0
139#define SLAVE (SMP_CACHE_BYTES/sizeof(unsigned long))
140
141#define NUM_ROUNDS 64 /* magic value */
142#define NUM_ITERS 5 /* likewise */
143
144static DEFINE_SPINLOCK(itc_sync_lock);
145static unsigned long go[SLAVE + 1];
146
147#define DEBUG_TICK_SYNC 0
148
149static inline long get_delta (long *rt, long *master)
150{
151 unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
152 unsigned long tcenter, t0, t1, tm;
153 unsigned long i;
154
155 for (i = 0; i < NUM_ITERS; i++) {
156 t0 = tick_ops->get_tick();
157 go[MASTER] = 1;
David S. Miller4f071182005-08-29 12:46:22 -0700158 membar_storeload();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 while (!(tm = go[SLAVE]))
David S. Miller4f071182005-08-29 12:46:22 -0700160 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 go[SLAVE] = 0;
David S. Miller4f071182005-08-29 12:46:22 -0700162 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 t1 = tick_ops->get_tick();
164
165 if (t1 - t0 < best_t1 - best_t0)
166 best_t0 = t0, best_t1 = t1, best_tm = tm;
167 }
168
169 *rt = best_t1 - best_t0;
170 *master = best_tm - best_t0;
171
172 /* average best_t0 and best_t1 without overflow: */
173 tcenter = (best_t0/2 + best_t1/2);
174 if (best_t0 % 2 + best_t1 % 2 == 2)
175 tcenter++;
176 return tcenter - best_tm;
177}
178
179void smp_synchronize_tick_client(void)
180{
181 long i, delta, adj, adjust_latency = 0, done = 0;
182 unsigned long flags, rt, master_time_stamp, bound;
183#if DEBUG_TICK_SYNC
184 struct {
185 long rt; /* roundtrip time */
186 long master; /* master's timestamp */
187 long diff; /* difference between midpoint and master's timestamp */
188 long lat; /* estimate of itc adjustment latency */
189 } t[NUM_ROUNDS];
190#endif
191
192 go[MASTER] = 1;
193
194 while (go[MASTER])
David S. Miller4f071182005-08-29 12:46:22 -0700195 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 local_irq_save(flags);
198 {
199 for (i = 0; i < NUM_ROUNDS; i++) {
200 delta = get_delta(&rt, &master_time_stamp);
201 if (delta == 0) {
202 done = 1; /* let's lock on to this... */
203 bound = rt;
204 }
205
206 if (!done) {
207 if (i > 0) {
208 adjust_latency += -delta;
209 adj = -delta + adjust_latency/4;
210 } else
211 adj = -delta;
212
David S. Miller112f4872007-03-05 15:28:37 -0800213 tick_ops->add_tick(adj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215#if DEBUG_TICK_SYNC
216 t[i].rt = rt;
217 t[i].master = master_time_stamp;
218 t[i].diff = delta;
219 t[i].lat = adjust_latency/4;
220#endif
221 }
222 }
223 local_irq_restore(flags);
224
225#if DEBUG_TICK_SYNC
226 for (i = 0; i < NUM_ROUNDS; i++)
227 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
228 t[i].rt, t[i].master, t[i].diff, t[i].lat);
229#endif
230
231 printk(KERN_INFO "CPU %d: synchronized TICK with master CPU (last diff %ld cycles,"
232 "maxerr %lu cycles)\n", smp_processor_id(), delta, rt);
233}
234
235static void smp_start_sync_tick_client(int cpu);
236
237static void smp_synchronize_one_tick(int cpu)
238{
239 unsigned long flags, i;
240
241 go[MASTER] = 0;
242
243 smp_start_sync_tick_client(cpu);
244
245 /* wait for client to be ready */
246 while (!go[MASTER])
David S. Miller4f071182005-08-29 12:46:22 -0700247 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* now let the client proceed into his loop */
250 go[MASTER] = 0;
David S. Miller4f071182005-08-29 12:46:22 -0700251 membar_storeload();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 spin_lock_irqsave(&itc_sync_lock, flags);
254 {
255 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
256 while (!go[MASTER])
David S. Miller4f071182005-08-29 12:46:22 -0700257 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 go[MASTER] = 0;
David S. Miller4f071182005-08-29 12:46:22 -0700259 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 go[SLAVE] = tick_ops->get_tick();
David S. Miller4f071182005-08-29 12:46:22 -0700261 membar_storeload();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 }
264 spin_unlock_irqrestore(&itc_sync_lock, flags);
265}
266
David S. Miller72aff532006-02-17 01:29:17 -0800267extern void sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269extern unsigned long sparc64_cpu_startup;
270
271/* The OBP cpu startup callback truncates the 3rd arg cookie to
272 * 32-bits (I think) so to be safe we have it read the pointer
273 * contained here so we work on >4GB machines. -DaveM
274 */
275static struct thread_info *cpu_new_thread = NULL;
276
277static int __devinit smp_boot_one_cpu(unsigned int cpu)
278{
279 unsigned long entry =
280 (unsigned long)(&sparc64_cpu_startup);
281 unsigned long cookie =
282 (unsigned long)(&cpu_new_thread);
283 struct task_struct *p;
David S. Miller7890f792006-02-15 02:26:54 -0800284 int timeout, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 p = fork_idle(cpu);
287 callin_flag = 0;
Al Virof3169642006-01-12 01:05:42 -0800288 cpu_new_thread = task_thread_info(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 cpu_set(cpu, cpu_callout_map);
290
David S. Miller7890f792006-02-15 02:26:54 -0800291 if (tlb_type == hypervisor) {
David S. Miller72aff532006-02-17 01:29:17 -0800292 /* Alloc the mondo queues, cpu will load them. */
293 sun4v_init_mondo_queues(0, cpu, 1, 0);
294
David S. Miller7890f792006-02-15 02:26:54 -0800295 prom_startcpu_cpuid(cpu, entry, cookie);
296 } else {
David S. Miller5cbc3072007-05-25 15:49:59 -0700297 struct device_node *dp = of_find_node_by_cpuid(cpu);
David S. Miller7890f792006-02-15 02:26:54 -0800298
David S. Miller07f8e5f2006-06-21 23:34:02 -0700299 prom_startcpu(dp->node, entry, cookie);
David S. Miller7890f792006-02-15 02:26:54 -0800300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 for (timeout = 0; timeout < 5000000; timeout++) {
303 if (callin_flag)
304 break;
305 udelay(100);
306 }
David S. Miller72aff532006-02-17 01:29:17 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (callin_flag) {
309 ret = 0;
310 } else {
311 printk("Processor %d is stuck.\n", cpu);
312 cpu_clear(cpu, cpu_callout_map);
313 ret = -ENODEV;
314 }
315 cpu_new_thread = NULL;
316
317 return ret;
318}
319
320static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu)
321{
322 u64 result, target;
323 int stuck, tmp;
324
325 if (this_is_starfire) {
326 /* map to real upaid */
327 cpu = (((cpu & 0x3c) << 1) |
328 ((cpu & 0x40) >> 4) |
329 (cpu & 0x3));
330 }
331
332 target = (cpu << 14) | 0x70;
333again:
334 /* Ok, this is the real Spitfire Errata #54.
335 * One must read back from a UDB internal register
336 * after writes to the UDB interrupt dispatch, but
337 * before the membar Sync for that write.
338 * So we use the high UDB control register (ASI 0x7f,
339 * ADDR 0x20) for the dummy read. -DaveM
340 */
341 tmp = 0x40;
342 __asm__ __volatile__(
343 "wrpr %1, %2, %%pstate\n\t"
344 "stxa %4, [%0] %3\n\t"
345 "stxa %5, [%0+%8] %3\n\t"
346 "add %0, %8, %0\n\t"
347 "stxa %6, [%0+%8] %3\n\t"
348 "membar #Sync\n\t"
349 "stxa %%g0, [%7] %3\n\t"
350 "membar #Sync\n\t"
351 "mov 0x20, %%g1\n\t"
352 "ldxa [%%g1] 0x7f, %%g0\n\t"
353 "membar #Sync"
354 : "=r" (tmp)
355 : "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),
356 "r" (data0), "r" (data1), "r" (data2), "r" (target),
357 "r" (0x10), "0" (tmp)
358 : "g1");
359
360 /* NOTE: PSTATE_IE is still clear. */
361 stuck = 100000;
362 do {
363 __asm__ __volatile__("ldxa [%%g0] %1, %0"
364 : "=r" (result)
365 : "i" (ASI_INTR_DISPATCH_STAT));
366 if (result == 0) {
367 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
368 : : "r" (pstate));
369 return;
370 }
371 stuck -= 1;
372 if (stuck == 0)
373 break;
374 } while (result & 0x1);
375 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
376 : : "r" (pstate));
377 if (stuck == 0) {
378 printk("CPU[%d]: mondo stuckage result[%016lx]\n",
379 smp_processor_id(), result);
380 } else {
381 udelay(2);
382 goto again;
383 }
384}
385
386static __inline__ void spitfire_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
387{
388 u64 pstate;
389 int i;
390
391 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
392 for_each_cpu_mask(i, mask)
393 spitfire_xcall_helper(data0, data1, data2, pstate, i);
394}
395
396/* Cheetah now allows to send the whole 64-bytes of data in the interrupt
397 * packet, but we have no use for that. However we do take advantage of
398 * the new pipelining feature (ie. dispatch to multiple cpus simultaneously).
399 */
400static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
401{
402 u64 pstate, ver;
David S. Miller92704a12006-02-26 23:27:19 -0800403 int nack_busy_id, is_jbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (cpus_empty(mask))
406 return;
407
408 /* Unfortunately, someone at Sun had the brilliant idea to make the
409 * busy/nack fields hard-coded by ITID number for this Ultra-III
410 * derivative processor.
411 */
412 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
David S. Miller92704a12006-02-26 23:27:19 -0800413 is_jbus = ((ver >> 32) == __JALAPENO_ID ||
414 (ver >> 32) == __SERRANO_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
417
418retry:
419 __asm__ __volatile__("wrpr %0, %1, %%pstate\n\t"
420 : : "r" (pstate), "i" (PSTATE_IE));
421
422 /* Setup the dispatch data registers. */
423 __asm__ __volatile__("stxa %0, [%3] %6\n\t"
424 "stxa %1, [%4] %6\n\t"
425 "stxa %2, [%5] %6\n\t"
426 "membar #Sync\n\t"
427 : /* no outputs */
428 : "r" (data0), "r" (data1), "r" (data2),
429 "r" (0x40), "r" (0x50), "r" (0x60),
430 "i" (ASI_INTR_W));
431
432 nack_busy_id = 0;
433 {
434 int i;
435
436 for_each_cpu_mask(i, mask) {
437 u64 target = (i << 14) | 0x70;
438
David S. Miller92704a12006-02-26 23:27:19 -0800439 if (!is_jbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 target |= (nack_busy_id << 24);
441 __asm__ __volatile__(
442 "stxa %%g0, [%0] %1\n\t"
443 "membar #Sync\n\t"
444 : /* no outputs */
445 : "r" (target), "i" (ASI_INTR_W));
446 nack_busy_id++;
447 }
448 }
449
450 /* Now, poll for completion. */
451 {
452 u64 dispatch_stat;
453 long stuck;
454
455 stuck = 100000 * nack_busy_id;
456 do {
457 __asm__ __volatile__("ldxa [%%g0] %1, %0"
458 : "=r" (dispatch_stat)
459 : "i" (ASI_INTR_DISPATCH_STAT));
460 if (dispatch_stat == 0UL) {
461 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
462 : : "r" (pstate));
463 return;
464 }
465 if (!--stuck)
466 break;
467 } while (dispatch_stat & 0x5555555555555555UL);
468
469 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
470 : : "r" (pstate));
471
472 if ((dispatch_stat & ~(0x5555555555555555UL)) == 0) {
473 /* Busy bits will not clear, continue instead
474 * of freezing up on this cpu.
475 */
476 printk("CPU[%d]: mondo stuckage result[%016lx]\n",
477 smp_processor_id(), dispatch_stat);
478 } else {
479 int i, this_busy_nack = 0;
480
481 /* Delay some random time with interrupts enabled
482 * to prevent deadlock.
483 */
484 udelay(2 * nack_busy_id);
485
486 /* Clear out the mask bits for cpus which did not
487 * NACK us.
488 */
489 for_each_cpu_mask(i, mask) {
490 u64 check_mask;
491
David S. Miller92704a12006-02-26 23:27:19 -0800492 if (is_jbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 check_mask = (0x2UL << (2*i));
494 else
495 check_mask = (0x2UL <<
496 this_busy_nack);
497 if ((dispatch_stat & check_mask) == 0)
498 cpu_clear(i, mask);
499 this_busy_nack += 2;
500 }
501
502 goto retry;
503 }
504 }
505}
506
David S. Miller1d2f1f92006-02-08 16:41:20 -0800507/* Multi-cpu list version. */
David S. Millera43fe0e2006-02-04 03:10:53 -0800508static void hypervisor_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
509{
David S. Millerb830ab62006-02-28 15:10:26 -0800510 struct trap_per_cpu *tb;
511 u16 *cpu_list;
512 u64 *mondo;
513 cpumask_t error_mask;
514 unsigned long flags, status;
David S. Miller3cab0c32006-03-02 21:50:47 -0800515 int cnt, retries, this_cpu, prev_sent, i;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800516
David S. Miller17f34f02007-05-14 02:01:52 -0700517 if (cpus_empty(mask))
518 return;
519
David S. Millerb830ab62006-02-28 15:10:26 -0800520 /* We have to do this whole thing with interrupts fully disabled.
521 * Otherwise if we send an xcall from interrupt context it will
522 * corrupt both our mondo block and cpu list state.
523 *
524 * One consequence of this is that we cannot use timeout mechanisms
525 * that depend upon interrupts being delivered locally. So, for
526 * example, we cannot sample jiffies and expect it to advance.
527 *
528 * Fortunately, udelay() uses %stick/%tick so we can use that.
529 */
530 local_irq_save(flags);
531
532 this_cpu = smp_processor_id();
533 tb = &trap_block[this_cpu];
534
535 mondo = __va(tb->cpu_mondo_block_pa);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800536 mondo[0] = data0;
537 mondo[1] = data1;
538 mondo[2] = data2;
539 wmb();
540
David S. Millerb830ab62006-02-28 15:10:26 -0800541 cpu_list = __va(tb->cpu_list_pa);
542
543 /* Setup the initial cpu list. */
544 cnt = 0;
545 for_each_cpu_mask(i, mask)
546 cpu_list[cnt++] = i;
547
548 cpus_clear(error_mask);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800549 retries = 0;
David S. Miller3cab0c32006-03-02 21:50:47 -0800550 prev_sent = 0;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800551 do {
David S. Miller3cab0c32006-03-02 21:50:47 -0800552 int forward_progress, n_sent;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800553
David S. Millerb830ab62006-02-28 15:10:26 -0800554 status = sun4v_cpu_mondo_send(cnt,
555 tb->cpu_list_pa,
556 tb->cpu_mondo_block_pa);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800557
David S. Millerb830ab62006-02-28 15:10:26 -0800558 /* HV_EOK means all cpus received the xcall, we're done. */
559 if (likely(status == HV_EOK))
David S. Miller1d2f1f92006-02-08 16:41:20 -0800560 break;
561
David S. Miller3cab0c32006-03-02 21:50:47 -0800562 /* First, see if we made any forward progress.
563 *
564 * The hypervisor indicates successful sends by setting
565 * cpu list entries to the value 0xffff.
David S. Millerb830ab62006-02-28 15:10:26 -0800566 */
David S. Miller3cab0c32006-03-02 21:50:47 -0800567 n_sent = 0;
David S. Millerb830ab62006-02-28 15:10:26 -0800568 for (i = 0; i < cnt; i++) {
David S. Miller3cab0c32006-03-02 21:50:47 -0800569 if (likely(cpu_list[i] == 0xffff))
570 n_sent++;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800571 }
572
David S. Miller3cab0c32006-03-02 21:50:47 -0800573 forward_progress = 0;
574 if (n_sent > prev_sent)
575 forward_progress = 1;
576
577 prev_sent = n_sent;
578
David S. Millerb830ab62006-02-28 15:10:26 -0800579 /* If we get a HV_ECPUERROR, then one or more of the cpus
580 * in the list are in error state. Use the cpu_state()
581 * hypervisor call to find out which cpus are in error state.
582 */
583 if (unlikely(status == HV_ECPUERROR)) {
584 for (i = 0; i < cnt; i++) {
585 long err;
586 u16 cpu;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800587
David S. Millerb830ab62006-02-28 15:10:26 -0800588 cpu = cpu_list[i];
589 if (cpu == 0xffff)
590 continue;
591
592 err = sun4v_cpu_state(cpu);
593 if (err >= 0 &&
594 err == HV_CPU_STATE_ERROR) {
David S. Miller3cab0c32006-03-02 21:50:47 -0800595 cpu_list[i] = 0xffff;
David S. Millerb830ab62006-02-28 15:10:26 -0800596 cpu_set(cpu, error_mask);
597 }
598 }
599 } else if (unlikely(status != HV_EWOULDBLOCK))
600 goto fatal_mondo_error;
601
David S. Miller3cab0c32006-03-02 21:50:47 -0800602 /* Don't bother rewriting the CPU list, just leave the
603 * 0xffff and non-0xffff entries in there and the
604 * hypervisor will do the right thing.
605 *
606 * Only advance timeout state if we didn't make any
607 * forward progress.
608 */
David S. Millerb830ab62006-02-28 15:10:26 -0800609 if (unlikely(!forward_progress)) {
610 if (unlikely(++retries > 10000))
611 goto fatal_mondo_timeout;
612
613 /* Delay a little bit to let other cpus catch up
614 * on their cpu mondo queue work.
615 */
616 udelay(2 * cnt);
617 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800618 } while (1);
619
David S. Millerb830ab62006-02-28 15:10:26 -0800620 local_irq_restore(flags);
621
622 if (unlikely(!cpus_empty(error_mask)))
623 goto fatal_mondo_cpu_error;
624
625 return;
626
627fatal_mondo_cpu_error:
628 printk(KERN_CRIT "CPU[%d]: SUN4V mondo cpu error, some target cpus "
629 "were in error state\n",
630 this_cpu);
631 printk(KERN_CRIT "CPU[%d]: Error mask [ ", this_cpu);
632 for_each_cpu_mask(i, error_mask)
633 printk("%d ", i);
634 printk("]\n");
635 return;
636
637fatal_mondo_timeout:
638 local_irq_restore(flags);
639 printk(KERN_CRIT "CPU[%d]: SUN4V mondo timeout, no forward "
640 " progress after %d retries.\n",
641 this_cpu, retries);
642 goto dump_cpu_list_and_out;
643
644fatal_mondo_error:
645 local_irq_restore(flags);
646 printk(KERN_CRIT "CPU[%d]: Unexpected SUN4V mondo error %lu\n",
647 this_cpu, status);
648 printk(KERN_CRIT "CPU[%d]: Args were cnt(%d) cpulist_pa(%lx) "
649 "mondo_block_pa(%lx)\n",
650 this_cpu, cnt, tb->cpu_list_pa, tb->cpu_mondo_block_pa);
651
652dump_cpu_list_and_out:
653 printk(KERN_CRIT "CPU[%d]: CPU list [ ", this_cpu);
654 for (i = 0; i < cnt; i++)
655 printk("%u ", cpu_list[i]);
656 printk("]\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/* Send cross call to all processors mentioned in MASK
660 * except self.
661 */
662static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, cpumask_t mask)
663{
664 u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
665 int this_cpu = get_cpu();
666
667 cpus_and(mask, mask, cpu_online_map);
668 cpu_clear(this_cpu, mask);
669
670 if (tlb_type == spitfire)
671 spitfire_xcall_deliver(data0, data1, data2, mask);
David S. Millera43fe0e2006-02-04 03:10:53 -0800672 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 cheetah_xcall_deliver(data0, data1, data2, mask);
David S. Millera43fe0e2006-02-04 03:10:53 -0800674 else
675 hypervisor_xcall_deliver(data0, data1, data2, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* NOTE: Caller runs local copy on master. */
677
678 put_cpu();
679}
680
681extern unsigned long xcall_sync_tick;
682
683static void smp_start_sync_tick_client(int cpu)
684{
685 cpumask_t mask = cpumask_of_cpu(cpu);
686
687 smp_cross_call_masked(&xcall_sync_tick,
688 0, 0, 0, mask);
689}
690
691/* Send cross call to all processors except self. */
692#define smp_cross_call(func, ctx, data1, data2) \
693 smp_cross_call_masked(func, ctx, data1, data2, cpu_online_map)
694
695struct call_data_struct {
696 void (*func) (void *info);
697 void *info;
698 atomic_t finished;
699 int wait;
700};
701
David S. Milleraa1d1a02006-04-06 16:54:33 -0700702static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static struct call_data_struct *call_data;
704
705extern unsigned long xcall_call_function;
706
David S. Milleraa1d1a02006-04-06 16:54:33 -0700707/**
708 * smp_call_function(): Run a function on all other CPUs.
709 * @func: The function to run. This must be fast and non-blocking.
710 * @info: An arbitrary pointer to pass to the function.
711 * @nonatomic: currently unused.
712 * @wait: If true, wait (atomically) until function has completed on other CPUs.
713 *
714 * Returns 0 on success, else a negative status code. Does not return until
715 * remote CPUs are nearly ready to execute <<func>> or are or have executed.
716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * You must not call this function with disabled interrupts or from a
718 * hardware interrupt handler or from a bottom half handler.
719 */
David S. Millerbd407912006-01-31 18:31:38 -0800720static int smp_call_function_mask(void (*func)(void *info), void *info,
721 int nonatomic, int wait, cpumask_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct call_data_struct data;
David S. Milleree290742006-03-06 22:50:44 -0800724 int cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Can deadlock when called with interrupts disabled */
727 WARN_ON(irqs_disabled());
728
729 data.func = func;
730 data.info = info;
731 atomic_set(&data.finished, 0);
732 data.wait = wait;
733
734 spin_lock(&call_lock);
735
David S. Milleree290742006-03-06 22:50:44 -0800736 cpu_clear(smp_processor_id(), mask);
737 cpus = cpus_weight(mask);
738 if (!cpus)
739 goto out_unlock;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 call_data = &data;
David S. Milleraa1d1a02006-04-06 16:54:33 -0700742 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David S. Millerbd407912006-01-31 18:31:38 -0800744 smp_cross_call_masked(&xcall_call_function, 0, 0, 0, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
David S. Milleraa1d1a02006-04-06 16:54:33 -0700746 /* Wait for response */
747 while (atomic_read(&data.finished) != cpus)
748 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
David S. Milleree290742006-03-06 22:50:44 -0800750out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_unlock(&call_lock);
752
753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
David S. Millerbd407912006-01-31 18:31:38 -0800756int smp_call_function(void (*func)(void *info), void *info,
757 int nonatomic, int wait)
758{
759 return smp_call_function_mask(func, info, nonatomic, wait,
760 cpu_online_map);
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763void smp_call_function_client(int irq, struct pt_regs *regs)
764{
765 void (*func) (void *info) = call_data->func;
766 void *info = call_data->info;
767
768 clear_softint(1 << irq);
769 if (call_data->wait) {
770 /* let initiator proceed only after completion */
771 func(info);
772 atomic_inc(&call_data->finished);
773 } else {
774 /* let initiator proceed after getting data */
775 atomic_inc(&call_data->finished);
776 func(info);
777 }
778}
779
David S. Millerbd407912006-01-31 18:31:38 -0800780static void tsb_sync(void *info)
781{
David S. Miller6f25f392006-03-28 13:29:26 -0800782 struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
David S. Millerbd407912006-01-31 18:31:38 -0800783 struct mm_struct *mm = info;
784
David S. Miller6f25f392006-03-28 13:29:26 -0800785 /* It is not valid to test "currrent->active_mm == mm" here.
786 *
787 * The value of "current" is not changed atomically with
788 * switch_mm(). But that's OK, we just need to check the
789 * current cpu's trap block PGD physical address.
790 */
791 if (tp->pgd_paddr == __pa(mm->pgd))
David S. Millerbd407912006-01-31 18:31:38 -0800792 tsb_context_switch(mm);
793}
794
795void smp_tsb_sync(struct mm_struct *mm)
796{
797 smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800extern unsigned long xcall_flush_tlb_mm;
801extern unsigned long xcall_flush_tlb_pending;
802extern unsigned long xcall_flush_tlb_kernel_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803extern unsigned long xcall_report_regs;
804extern unsigned long xcall_receive_signal;
David S. Milleree290742006-03-06 22:50:44 -0800805extern unsigned long xcall_new_mmu_context_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807#ifdef DCACHE_ALIASING_POSSIBLE
808extern unsigned long xcall_flush_dcache_page_cheetah;
809#endif
810extern unsigned long xcall_flush_dcache_page_spitfire;
811
812#ifdef CONFIG_DEBUG_DCFLUSH
813extern atomic_t dcpage_flushes;
814extern atomic_t dcpage_flushes_xcall;
815#endif
816
817static __inline__ void __local_flush_dcache_page(struct page *page)
818{
819#ifdef DCACHE_ALIASING_POSSIBLE
820 __flush_dcache_page(page_address(page),
821 ((tlb_type == spitfire) &&
822 page_mapping(page) != NULL));
823#else
824 if (page_mapping(page) != NULL &&
825 tlb_type == spitfire)
826 __flush_icache_page(__pa(page_address(page)));
827#endif
828}
829
830void smp_flush_dcache_page_impl(struct page *page, int cpu)
831{
832 cpumask_t mask = cpumask_of_cpu(cpu);
David S. Millera43fe0e2006-02-04 03:10:53 -0800833 int this_cpu;
834
835 if (tlb_type == hypervisor)
836 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838#ifdef CONFIG_DEBUG_DCFLUSH
839 atomic_inc(&dcpage_flushes);
840#endif
David S. Millera43fe0e2006-02-04 03:10:53 -0800841
842 this_cpu = get_cpu();
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (cpu == this_cpu) {
845 __local_flush_dcache_page(page);
846 } else if (cpu_online(cpu)) {
847 void *pg_addr = page_address(page);
848 u64 data0;
849
850 if (tlb_type == spitfire) {
851 data0 =
852 ((u64)&xcall_flush_dcache_page_spitfire);
853 if (page_mapping(page) != NULL)
854 data0 |= ((u64)1 << 32);
855 spitfire_xcall_deliver(data0,
856 __pa(pg_addr),
857 (u64) pg_addr,
858 mask);
David S. Millera43fe0e2006-02-04 03:10:53 -0800859 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#ifdef DCACHE_ALIASING_POSSIBLE
861 data0 =
862 ((u64)&xcall_flush_dcache_page_cheetah);
863 cheetah_xcall_deliver(data0,
864 __pa(pg_addr),
865 0, mask);
866#endif
867 }
868#ifdef CONFIG_DEBUG_DCFLUSH
869 atomic_inc(&dcpage_flushes_xcall);
870#endif
871 }
872
873 put_cpu();
874}
875
876void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
877{
878 void *pg_addr = page_address(page);
879 cpumask_t mask = cpu_online_map;
880 u64 data0;
David S. Millera43fe0e2006-02-04 03:10:53 -0800881 int this_cpu;
882
883 if (tlb_type == hypervisor)
884 return;
885
886 this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 cpu_clear(this_cpu, mask);
889
890#ifdef CONFIG_DEBUG_DCFLUSH
891 atomic_inc(&dcpage_flushes);
892#endif
893 if (cpus_empty(mask))
894 goto flush_self;
895 if (tlb_type == spitfire) {
896 data0 = ((u64)&xcall_flush_dcache_page_spitfire);
897 if (page_mapping(page) != NULL)
898 data0 |= ((u64)1 << 32);
899 spitfire_xcall_deliver(data0,
900 __pa(pg_addr),
901 (u64) pg_addr,
902 mask);
David S. Millera43fe0e2006-02-04 03:10:53 -0800903 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#ifdef DCACHE_ALIASING_POSSIBLE
905 data0 = ((u64)&xcall_flush_dcache_page_cheetah);
906 cheetah_xcall_deliver(data0,
907 __pa(pg_addr),
908 0, mask);
909#endif
910 }
911#ifdef CONFIG_DEBUG_DCFLUSH
912 atomic_inc(&dcpage_flushes_xcall);
913#endif
914 flush_self:
915 __local_flush_dcache_page(page);
916
917 put_cpu();
918}
919
David S. Millera0663a72006-02-23 14:19:28 -0800920static void __smp_receive_signal_mask(cpumask_t mask)
921{
922 smp_cross_call_masked(&xcall_receive_signal, 0, 0, 0, mask);
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void smp_receive_signal(int cpu)
926{
927 cpumask_t mask = cpumask_of_cpu(cpu);
928
David S. Millera0663a72006-02-23 14:19:28 -0800929 if (cpu_online(cpu))
930 __smp_receive_signal_mask(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933void smp_receive_signal_client(int irq, struct pt_regs *regs)
934{
David S. Milleree290742006-03-06 22:50:44 -0800935 clear_softint(1 << irq);
936}
937
938void smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
939{
David S. Millera0663a72006-02-23 14:19:28 -0800940 struct mm_struct *mm;
David S. Milleree290742006-03-06 22:50:44 -0800941 unsigned long flags;
David S. Millera0663a72006-02-23 14:19:28 -0800942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 clear_softint(1 << irq);
David S. Millera0663a72006-02-23 14:19:28 -0800944
945 /* See if we need to allocate a new TLB context because
946 * the version of the one we are using is now out of date.
947 */
948 mm = current->active_mm;
David S. Milleree290742006-03-06 22:50:44 -0800949 if (unlikely(!mm || (mm == &init_mm)))
950 return;
David S. Millera0663a72006-02-23 14:19:28 -0800951
David S. Milleree290742006-03-06 22:50:44 -0800952 spin_lock_irqsave(&mm->context.lock, flags);
David S. Milleraac0aad2006-02-27 17:56:51 -0800953
David S. Milleree290742006-03-06 22:50:44 -0800954 if (unlikely(!CTX_VALID(mm->context)))
955 get_new_mmu_context(mm);
David S. Milleraac0aad2006-02-27 17:56:51 -0800956
David S. Milleree290742006-03-06 22:50:44 -0800957 spin_unlock_irqrestore(&mm->context.lock, flags);
David S. Milleraac0aad2006-02-27 17:56:51 -0800958
David S. Milleree290742006-03-06 22:50:44 -0800959 load_secondary_context(mm);
960 __flush_tlb_mm(CTX_HWBITS(mm->context),
961 SECONDARY_CONTEXT);
David S. Millera0663a72006-02-23 14:19:28 -0800962}
963
964void smp_new_mmu_context_version(void)
965{
David S. Milleree290742006-03-06 22:50:44 -0800966 smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969void smp_report_regs(void)
970{
971 smp_cross_call(&xcall_report_regs, 0, 0, 0);
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/* We know that the window frames of the user have been flushed
975 * to the stack before we get here because all callers of us
976 * are flush_tlb_*() routines, and these run after flush_cache_*()
977 * which performs the flushw.
978 *
979 * The SMP TLB coherency scheme we use works as follows:
980 *
981 * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
982 * space has (potentially) executed on, this is the heuristic
983 * we use to avoid doing cross calls.
984 *
985 * Also, for flushing from kswapd and also for clones, we
986 * use cpu_vm_mask as the list of cpus to make run the TLB.
987 *
988 * 2) TLB context numbers are shared globally across all processors
989 * in the system, this allows us to play several games to avoid
990 * cross calls.
991 *
992 * One invariant is that when a cpu switches to a process, and
993 * that processes tsk->active_mm->cpu_vm_mask does not have the
994 * current cpu's bit set, that tlb context is flushed locally.
995 *
996 * If the address space is non-shared (ie. mm->count == 1) we avoid
997 * cross calls when we want to flush the currently running process's
998 * tlb state. This is done by clearing all cpu bits except the current
999 * processor's in current->active_mm->cpu_vm_mask and performing the
1000 * flush locally only. This will force any subsequent cpus which run
1001 * this task to flush the context from the local tlb if the process
1002 * migrates to another cpu (again).
1003 *
1004 * 3) For shared address spaces (threads) and swapping we bite the
1005 * bullet for most cases and perform the cross call (but only to
1006 * the cpus listed in cpu_vm_mask).
1007 *
1008 * The performance gain from "optimizing" away the cross call for threads is
1009 * questionable (in theory the big win for threads is the massive sharing of
1010 * address space state across processors).
1011 */
David S. Miller62dbec72005-11-07 14:09:58 -08001012
1013/* This currently is only used by the hugetlb arch pre-fault
1014 * hook on UltraSPARC-III+ and later when changing the pagesize
1015 * bits of the context register for an address space.
1016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017void smp_flush_tlb_mm(struct mm_struct *mm)
1018{
David S. Miller62dbec72005-11-07 14:09:58 -08001019 u32 ctx = CTX_HWBITS(mm->context);
1020 int cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
David S. Miller62dbec72005-11-07 14:09:58 -08001022 if (atomic_read(&mm->mm_users) == 1) {
1023 mm->cpu_vm_mask = cpumask_of_cpu(cpu);
1024 goto local_flush_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
David S. Miller62dbec72005-11-07 14:09:58 -08001026
1027 smp_cross_call_masked(&xcall_flush_tlb_mm,
1028 ctx, 0, 0,
1029 mm->cpu_vm_mask);
1030
1031local_flush_and_out:
1032 __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
1033
1034 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
1038{
1039 u32 ctx = CTX_HWBITS(mm->context);
1040 int cpu = get_cpu();
1041
Hugh Dickinsdedeb002005-11-07 14:09:01 -08001042 if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 mm->cpu_vm_mask = cpumask_of_cpu(cpu);
Hugh Dickinsdedeb002005-11-07 14:09:01 -08001044 else
1045 smp_cross_call_masked(&xcall_flush_tlb_pending,
1046 ctx, nr, (unsigned long) vaddrs,
1047 mm->cpu_vm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 __flush_tlb_pending(ctx, nr, vaddrs);
1050
1051 put_cpu();
1052}
1053
1054void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
1055{
1056 start &= PAGE_MASK;
1057 end = PAGE_ALIGN(end);
1058 if (start != end) {
1059 smp_cross_call(&xcall_flush_tlb_kernel_range,
1060 0, start, end);
1061
1062 __flush_tlb_kernel_range(start, end);
1063 }
1064}
1065
1066/* CPU capture. */
1067/* #define CAPTURE_DEBUG */
1068extern unsigned long xcall_capture;
1069
1070static atomic_t smp_capture_depth = ATOMIC_INIT(0);
1071static atomic_t smp_capture_registry = ATOMIC_INIT(0);
1072static unsigned long penguins_are_doing_time;
1073
1074void smp_capture(void)
1075{
1076 int result = atomic_add_ret(1, &smp_capture_depth);
1077
1078 if (result == 1) {
1079 int ncpus = num_online_cpus();
1080
1081#ifdef CAPTURE_DEBUG
1082 printk("CPU[%d]: Sending penguins to jail...",
1083 smp_processor_id());
1084#endif
1085 penguins_are_doing_time = 1;
David S. Miller4f071182005-08-29 12:46:22 -07001086 membar_storestore_loadstore();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 atomic_inc(&smp_capture_registry);
1088 smp_cross_call(&xcall_capture, 0, 0, 0);
1089 while (atomic_read(&smp_capture_registry) != ncpus)
David S. Miller4f071182005-08-29 12:46:22 -07001090 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#ifdef CAPTURE_DEBUG
1092 printk("done\n");
1093#endif
1094 }
1095}
1096
1097void smp_release(void)
1098{
1099 if (atomic_dec_and_test(&smp_capture_depth)) {
1100#ifdef CAPTURE_DEBUG
1101 printk("CPU[%d]: Giving pardon to "
1102 "imprisoned penguins\n",
1103 smp_processor_id());
1104#endif
1105 penguins_are_doing_time = 0;
David S. Miller4f071182005-08-29 12:46:22 -07001106 membar_storeload_storestore();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 atomic_dec(&smp_capture_registry);
1108 }
1109}
1110
1111/* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
1112 * can service tlb flush xcalls...
1113 */
1114extern void prom_world(int);
David S. Miller96c6e0d2006-01-31 18:32:29 -08001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116void smp_penguin_jailcell(int irq, struct pt_regs *regs)
1117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 clear_softint(1 << irq);
1119
1120 preempt_disable();
1121
1122 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 prom_world(1);
1124 atomic_inc(&smp_capture_registry);
David S. Miller4f071182005-08-29 12:46:22 -07001125 membar_storeload_storestore();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 while (penguins_are_doing_time)
David S. Miller4f071182005-08-29 12:46:22 -07001127 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 atomic_dec(&smp_capture_registry);
1129 prom_world(0);
1130
1131 preempt_enable();
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134void __init smp_tick_init(void)
1135{
1136 boot_cpu_id = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139/* /proc/profile writes can call this, don't __init it please. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140int setup_profiling_timer(unsigned int multiplier)
1141{
David S. Miller777a4472007-02-22 06:24:10 -08001142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
David S. Miller9145bcf2006-06-10 22:02:17 -07001145static void __init smp_tune_scheduling(void)
1146{
David S. Miller5cbc3072007-05-25 15:49:59 -07001147 unsigned int smallest = ~0U;
1148 int i;
David S. Miller9145bcf2006-06-10 22:02:17 -07001149
David S. Miller5cbc3072007-05-25 15:49:59 -07001150 for (i = 0; i < NR_CPUS; i++) {
1151 unsigned int val = cpu_data(i).ecache_size;
David S. Miller9145bcf2006-06-10 22:02:17 -07001152
David S. Miller5cbc3072007-05-25 15:49:59 -07001153 if (val && val < smallest)
David S. Miller9145bcf2006-06-10 22:02:17 -07001154 smallest = val;
David S. Miller9145bcf2006-06-10 22:02:17 -07001155 }
1156
1157 /* Any value less than 256K is nonsense. */
1158 if (smallest < (256U * 1024U))
1159 smallest = 256 * 1024;
1160
1161 max_cache_size = smallest;
1162
1163 if (smallest < 1U * 1024U * 1024U)
1164 printk(KERN_INFO "Using max_cache_size of %uKB\n",
1165 smallest / 1024U);
1166 else
1167 printk(KERN_INFO "Using max_cache_size of %uMB\n",
1168 smallest / 1024U / 1024U);
1169}
1170
David S. Miller7abea922006-02-25 13:39:56 -08001171/* Constrain the number of cpus to max_cpus. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172void __init smp_prepare_cpus(unsigned int max_cpus)
1173{
David S. Miller8935dce2006-03-08 16:09:19 -08001174 int i;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (num_possible_cpus() > max_cpus) {
David S. Miller5cbc3072007-05-25 15:49:59 -07001177 for_each_possible_cpu(i) {
1178 if (i != boot_cpu_id) {
1179 cpu_clear(i, phys_cpu_present_map);
1180 cpu_clear(i, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (num_possible_cpus() <= max_cpus)
1182 break;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 }
1186
David S. Miller5cbc3072007-05-25 15:49:59 -07001187 cpu_data(boot_cpu_id).udelay_val = loops_per_jiffy;
David S. Miller9145bcf2006-06-10 22:02:17 -07001188 smp_tune_scheduling();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191void __devinit smp_prepare_boot_cpu(void)
1192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
David S. Miller5cbc3072007-05-25 15:49:59 -07001195void __devinit smp_fill_in_sib_core_maps(void)
1196{
1197 unsigned int i;
1198
1199 for_each_possible_cpu(i) {
1200 unsigned int j;
1201
1202 if (cpu_data(i).core_id == 0) {
1203 cpu_set(i, cpu_sibling_map[i]);
1204 continue;
1205 }
1206
1207 for_each_possible_cpu(j) {
1208 if (cpu_data(i).core_id ==
1209 cpu_data(j).core_id)
1210 cpu_set(j, cpu_sibling_map[i]);
1211 }
1212 }
1213}
1214
Gautham R Shenoyb282b6f2007-01-10 23:15:34 -08001215int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 int ret = smp_boot_one_cpu(cpu);
1218
1219 if (!ret) {
1220 cpu_set(cpu, smp_commenced_mask);
1221 while (!cpu_isset(cpu, cpu_online_map))
1222 mb();
1223 if (!cpu_isset(cpu, cpu_online_map)) {
1224 ret = -ENODEV;
1225 } else {
David S. Miller02fead72006-02-11 23:22:47 -08001226 /* On SUN4V, writes to %tick and %stick are
1227 * not allowed.
1228 */
1229 if (tlb_type != hypervisor)
1230 smp_synchronize_one_tick(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 }
1233 return ret;
1234}
1235
1236void __init smp_cpus_done(unsigned int max_cpus)
1237{
1238 unsigned long bogosum = 0;
1239 int i;
1240
Andrew Morton394e3902006-03-23 03:01:05 -08001241 for_each_online_cpu(i)
1242 bogosum += cpu_data(i).udelay_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 printk("Total of %ld processors activated "
1244 "(%lu.%02lu BogoMIPS).\n",
1245 (long) num_online_cpus(),
1246 bogosum/(500000/HZ),
1247 (bogosum/(5000/HZ))%100);
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250void smp_send_reschedule(int cpu)
1251{
Nick Piggin64c7c8f2005-11-08 21:39:04 -08001252 smp_receive_signal(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/* This is a nop because we capture all other cpus
1256 * anyways when making the PROM active.
1257 */
1258void smp_send_stop(void)
1259{
1260}
1261
David S. Millerd369ddd2005-07-10 15:45:11 -07001262unsigned long __per_cpu_base __read_mostly;
1263unsigned long __per_cpu_shift __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265EXPORT_SYMBOL(__per_cpu_base);
1266EXPORT_SYMBOL(__per_cpu_shift);
1267
David S. Miller5cbc3072007-05-25 15:49:59 -07001268void __init real_setup_per_cpu_areas(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 unsigned long goal, size, i;
1271 char *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* Copy section for each CPU (we discard the original) */
David S. Miller5a089002006-12-14 23:40:57 -08001274 goal = PERCPU_ENOUGH_ROOM;
1275
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +02001276 __per_cpu_shift = PAGE_SHIFT;
1277 for (size = PAGE_SIZE; size < goal; size <<= 1UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 __per_cpu_shift++;
1279
Jeremy Fitzhardingeb6e35902007-05-02 19:27:12 +02001280 ptr = alloc_bootmem_pages(size * NR_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 __per_cpu_base = ptr - __per_cpu_start;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 for (i = 0; i < NR_CPUS; i++, ptr += size)
1285 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
David S. Miller951bc822006-05-31 01:24:02 -07001286
1287 /* Setup %g5 for the boot cpu. */
1288 __local_per_cpu_offset = __per_cpu_offset(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}