]> git.karo-electronics.de Git - linux-beck.git/blob - arch/sparc64/kernel/smp.c
f7fa873c800df5fb4bea56ad3c27527ef4863ba0
[linux-beck.git] / arch / sparc64 / kernel / smp.c
1 /* 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>
13 #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>
33 #include <asm/irq_regs.h>
34 #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>
41 #include <asm/sections.h>
42 #include <asm/prom.h>
43 #include <asm/mdesc.h>
44
45 extern void calibrate_delay(void);
46
47 /* Please don't make this stuff initdata!!!  --DaveM */
48 unsigned char boot_cpu_id;
49
50 cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;
51 cpumask_t phys_cpu_present_map __read_mostly = CPU_MASK_NONE;
52 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly =
53         { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
54 static cpumask_t smp_commenced_mask;
55 static cpumask_t cpu_callout_map;
56
57 void smp_info(struct seq_file *m)
58 {
59         int i;
60         
61         seq_printf(m, "State:\n");
62         for_each_online_cpu(i)
63                 seq_printf(m, "CPU%d:\t\tonline\n", i);
64 }
65
66 void smp_bogo(struct seq_file *m)
67 {
68         int i;
69         
70         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);
77 }
78
79 extern void setup_sparc64_timer(void);
80
81 static volatile unsigned long callin_flag = 0;
82
83 void __init smp_callin(void)
84 {
85         int cpuid = hard_smp_processor_id();
86
87         __local_per_cpu_offset = __per_cpu_offset(cpuid);
88
89         if (tlb_type == hypervisor)
90                 sun4v_ktsb_register();
91
92         __flush_tlb_all();
93
94         setup_sparc64_timer();
95
96         if (cheetah_pcache_forced_on)
97                 cheetah_enable_pcache();
98
99         local_irq_enable();
100
101         calibrate_delay();
102         cpu_data(cpuid).udelay_val = loops_per_jiffy;
103         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          */
110         current_thread_info()->new_child = 0;
111
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))
117                 rmb();
118
119         cpu_set(cpuid, cpu_online_map);
120
121         /* idle thread is expected to have preempt disabled */
122         preempt_disable();
123 }
124
125 void cpu_panic(void)
126 {
127         printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
128         panic("SMP bolixed\n");
129 }
130
131 /* 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
144 static DEFINE_SPINLOCK(itc_sync_lock);
145 static unsigned long go[SLAVE + 1];
146
147 #define DEBUG_TICK_SYNC 0
148
149 static 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;
158                 membar_storeload();
159                 while (!(tm = go[SLAVE]))
160                         rmb();
161                 go[SLAVE] = 0;
162                 wmb();
163                 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
179 void 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])
195                 rmb();
196
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
213                                 tick_ops->add_tick(adj);
214                         }
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
235 static void smp_start_sync_tick_client(int cpu);
236
237 static 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])
247                 rmb();
248
249         /* now let the client proceed into his loop */
250         go[MASTER] = 0;
251         membar_storeload();
252
253         spin_lock_irqsave(&itc_sync_lock, flags);
254         {
255                 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
256                         while (!go[MASTER])
257                                 rmb();
258                         go[MASTER] = 0;
259                         wmb();
260                         go[SLAVE] = tick_ops->get_tick();
261                         membar_storeload();
262                 }
263         }
264         spin_unlock_irqrestore(&itc_sync_lock, flags);
265 }
266
267 extern void sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load);
268
269 extern 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  */
275 static struct thread_info *cpu_new_thread = NULL;
276
277 static 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;
284         int timeout, ret;
285
286         p = fork_idle(cpu);
287         callin_flag = 0;
288         cpu_new_thread = task_thread_info(p);
289         cpu_set(cpu, cpu_callout_map);
290
291         if (tlb_type == hypervisor) {
292                 /* Alloc the mondo queues, cpu will load them.  */
293                 sun4v_init_mondo_queues(0, cpu, 1, 0);
294
295                 prom_startcpu_cpuid(cpu, entry, cookie);
296         } else {
297                 struct device_node *dp = of_find_node_by_cpuid(cpu);
298
299                 prom_startcpu(dp->node, entry, cookie);
300         }
301
302         for (timeout = 0; timeout < 5000000; timeout++) {
303                 if (callin_flag)
304                         break;
305                 udelay(100);
306         }
307
308         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
320 static 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;
333 again:
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
386 static __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  */
400 static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
401 {
402         u64 pstate, ver;
403         int nack_busy_id, is_jbus;
404
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));
413         is_jbus = ((ver >> 32) == __JALAPENO_ID ||
414                    (ver >> 32) == __SERRANO_ID);
415
416         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
417
418 retry:
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
439                         if (!is_jbus)
440                                 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
492                                 if (is_jbus)
493                                         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
507 /* Multi-cpu list version.  */
508 static void hypervisor_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
509 {
510         struct trap_per_cpu *tb;
511         u16 *cpu_list;
512         u64 *mondo;
513         cpumask_t error_mask;
514         unsigned long flags, status;
515         int cnt, retries, this_cpu, prev_sent, i;
516
517         if (cpus_empty(mask))
518                 return;
519
520         /* 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);
536         mondo[0] = data0;
537         mondo[1] = data1;
538         mondo[2] = data2;
539         wmb();
540
541         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);
549         retries = 0;
550         prev_sent = 0;
551         do {
552                 int forward_progress, n_sent;
553
554                 status = sun4v_cpu_mondo_send(cnt,
555                                               tb->cpu_list_pa,
556                                               tb->cpu_mondo_block_pa);
557
558                 /* HV_EOK means all cpus received the xcall, we're done.  */
559                 if (likely(status == HV_EOK))
560                         break;
561
562                 /* 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.
566                  */
567                 n_sent = 0;
568                 for (i = 0; i < cnt; i++) {
569                         if (likely(cpu_list[i] == 0xffff))
570                                 n_sent++;
571                 }
572
573                 forward_progress = 0;
574                 if (n_sent > prev_sent)
575                         forward_progress = 1;
576
577                 prev_sent = n_sent;
578
579                 /* 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;
587
588                                 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) {
595                                         cpu_list[i] = 0xffff;
596                                         cpu_set(cpu, error_mask);
597                                 }
598                         }
599                 } else if (unlikely(status != HV_EWOULDBLOCK))
600                         goto fatal_mondo_error;
601
602                 /* 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                  */
609                 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                 }
618         } while (1);
619
620         local_irq_restore(flags);
621
622         if (unlikely(!cpus_empty(error_mask)))
623                 goto fatal_mondo_cpu_error;
624
625         return;
626
627 fatal_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
637 fatal_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
644 fatal_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
652 dump_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");
657 }
658
659 /* Send cross call to all processors mentioned in MASK
660  * except self.
661  */
662 static 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);
672         else if (tlb_type == cheetah || tlb_type == cheetah_plus)
673                 cheetah_xcall_deliver(data0, data1, data2, mask);
674         else
675                 hypervisor_xcall_deliver(data0, data1, data2, mask);
676         /* NOTE: Caller runs local copy on master. */
677
678         put_cpu();
679 }
680
681 extern unsigned long xcall_sync_tick;
682
683 static 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
695 struct call_data_struct {
696         void (*func) (void *info);
697         void *info;
698         atomic_t finished;
699         int wait;
700 };
701
702 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
703 static struct call_data_struct *call_data;
704
705 extern unsigned long xcall_call_function;
706
707 /**
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  *
717  * You must not call this function with disabled interrupts or from a
718  * hardware interrupt handler or from a bottom half handler.
719  */
720 static int smp_call_function_mask(void (*func)(void *info), void *info,
721                                   int nonatomic, int wait, cpumask_t mask)
722 {
723         struct call_data_struct data;
724         int cpus;
725
726         /* 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
736         cpu_clear(smp_processor_id(), mask);
737         cpus = cpus_weight(mask);
738         if (!cpus)
739                 goto out_unlock;
740
741         call_data = &data;
742         mb();
743
744         smp_cross_call_masked(&xcall_call_function, 0, 0, 0, mask);
745
746         /* Wait for response */
747         while (atomic_read(&data.finished) != cpus)
748                 cpu_relax();
749
750 out_unlock:
751         spin_unlock(&call_lock);
752
753         return 0;
754 }
755
756 int 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
763 void 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
780 static void tsb_sync(void *info)
781 {
782         struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
783         struct mm_struct *mm = info;
784
785         /* 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))
792                 tsb_context_switch(mm);
793 }
794
795 void smp_tsb_sync(struct mm_struct *mm)
796 {
797         smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
798 }
799
800 extern unsigned long xcall_flush_tlb_mm;
801 extern unsigned long xcall_flush_tlb_pending;
802 extern unsigned long xcall_flush_tlb_kernel_range;
803 extern unsigned long xcall_report_regs;
804 extern unsigned long xcall_receive_signal;
805 extern unsigned long xcall_new_mmu_context_version;
806
807 #ifdef DCACHE_ALIASING_POSSIBLE
808 extern unsigned long xcall_flush_dcache_page_cheetah;
809 #endif
810 extern unsigned long xcall_flush_dcache_page_spitfire;
811
812 #ifdef CONFIG_DEBUG_DCFLUSH
813 extern atomic_t dcpage_flushes;
814 extern atomic_t dcpage_flushes_xcall;
815 #endif
816
817 static __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
830 void smp_flush_dcache_page_impl(struct page *page, int cpu)
831 {
832         cpumask_t mask = cpumask_of_cpu(cpu);
833         int this_cpu;
834
835         if (tlb_type == hypervisor)
836                 return;
837
838 #ifdef CONFIG_DEBUG_DCFLUSH
839         atomic_inc(&dcpage_flushes);
840 #endif
841
842         this_cpu = get_cpu();
843
844         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);
859                 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
860 #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
876 void 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;
881         int this_cpu;
882
883         if (tlb_type == hypervisor)
884                 return;
885
886         this_cpu = get_cpu();
887
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);
903         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
904 #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
920 static void __smp_receive_signal_mask(cpumask_t mask)
921 {
922         smp_cross_call_masked(&xcall_receive_signal, 0, 0, 0, mask);
923 }
924
925 void smp_receive_signal(int cpu)
926 {
927         cpumask_t mask = cpumask_of_cpu(cpu);
928
929         if (cpu_online(cpu))
930                 __smp_receive_signal_mask(mask);
931 }
932
933 void smp_receive_signal_client(int irq, struct pt_regs *regs)
934 {
935         clear_softint(1 << irq);
936 }
937
938 void smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
939 {
940         struct mm_struct *mm;
941         unsigned long flags;
942
943         clear_softint(1 << irq);
944
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;
949         if (unlikely(!mm || (mm == &init_mm)))
950                 return;
951
952         spin_lock_irqsave(&mm->context.lock, flags);
953
954         if (unlikely(!CTX_VALID(mm->context)))
955                 get_new_mmu_context(mm);
956
957         spin_unlock_irqrestore(&mm->context.lock, flags);
958
959         load_secondary_context(mm);
960         __flush_tlb_mm(CTX_HWBITS(mm->context),
961                        SECONDARY_CONTEXT);
962 }
963
964 void smp_new_mmu_context_version(void)
965 {
966         smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
967 }
968
969 void smp_report_regs(void)
970 {
971         smp_cross_call(&xcall_report_regs, 0, 0, 0);
972 }
973
974 /* 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  */
1012
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  */
1017 void smp_flush_tlb_mm(struct mm_struct *mm)
1018 {
1019         u32 ctx = CTX_HWBITS(mm->context);
1020         int cpu = get_cpu();
1021
1022         if (atomic_read(&mm->mm_users) == 1) {
1023                 mm->cpu_vm_mask = cpumask_of_cpu(cpu);
1024                 goto local_flush_and_out;
1025         }
1026
1027         smp_cross_call_masked(&xcall_flush_tlb_mm,
1028                               ctx, 0, 0,
1029                               mm->cpu_vm_mask);
1030
1031 local_flush_and_out:
1032         __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
1033
1034         put_cpu();
1035 }
1036
1037 void 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
1042         if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
1043                 mm->cpu_vm_mask = cpumask_of_cpu(cpu);
1044         else
1045                 smp_cross_call_masked(&xcall_flush_tlb_pending,
1046                                       ctx, nr, (unsigned long) vaddrs,
1047                                       mm->cpu_vm_mask);
1048
1049         __flush_tlb_pending(ctx, nr, vaddrs);
1050
1051         put_cpu();
1052 }
1053
1054 void 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 */
1068 extern unsigned long xcall_capture;
1069
1070 static atomic_t smp_capture_depth = ATOMIC_INIT(0);
1071 static atomic_t smp_capture_registry = ATOMIC_INIT(0);
1072 static unsigned long penguins_are_doing_time;
1073
1074 void 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;
1086                 membar_storestore_loadstore();
1087                 atomic_inc(&smp_capture_registry);
1088                 smp_cross_call(&xcall_capture, 0, 0, 0);
1089                 while (atomic_read(&smp_capture_registry) != ncpus)
1090                         rmb();
1091 #ifdef CAPTURE_DEBUG
1092                 printk("done\n");
1093 #endif
1094         }
1095 }
1096
1097 void 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;
1106                 membar_storeload_storestore();
1107                 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  */
1114 extern void prom_world(int);
1115
1116 void smp_penguin_jailcell(int irq, struct pt_regs *regs)
1117 {
1118         clear_softint(1 << irq);
1119
1120         preempt_disable();
1121
1122         __asm__ __volatile__("flushw");
1123         prom_world(1);
1124         atomic_inc(&smp_capture_registry);
1125         membar_storeload_storestore();
1126         while (penguins_are_doing_time)
1127                 rmb();
1128         atomic_dec(&smp_capture_registry);
1129         prom_world(0);
1130
1131         preempt_enable();
1132 }
1133
1134 void __init smp_tick_init(void)
1135 {
1136         boot_cpu_id = hard_smp_processor_id();
1137 }
1138
1139 /* /proc/profile writes can call this, don't __init it please. */
1140 int setup_profiling_timer(unsigned int multiplier)
1141 {
1142         return -EINVAL;
1143 }
1144
1145 static void __init smp_tune_scheduling(void)
1146 {
1147         unsigned int smallest = ~0U;
1148         int i;
1149
1150         for (i = 0; i < NR_CPUS; i++) {
1151                 unsigned int val = cpu_data(i).ecache_size;
1152
1153                 if (val && val < smallest)
1154                         smallest = val;
1155         }
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
1171 /* Constrain the number of cpus to max_cpus.  */
1172 void __init smp_prepare_cpus(unsigned int max_cpus)
1173 {
1174         int i;
1175
1176         if (num_possible_cpus() > max_cpus) {
1177                 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);
1181                                 if (num_possible_cpus() <= max_cpus)
1182                                         break;
1183                         }
1184                 }
1185         }
1186
1187         cpu_data(boot_cpu_id).udelay_val = loops_per_jiffy;
1188         smp_tune_scheduling();
1189 }
1190
1191 void __devinit smp_prepare_boot_cpu(void)
1192 {
1193 }
1194
1195 void __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
1215 int __cpuinit __cpu_up(unsigned int cpu)
1216 {
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 {
1226                         /* On SUN4V, writes to %tick and %stick are
1227                          * not allowed.
1228                          */
1229                         if (tlb_type != hypervisor)
1230                                 smp_synchronize_one_tick(cpu);
1231                 }
1232         }
1233         return ret;
1234 }
1235
1236 void __init smp_cpus_done(unsigned int max_cpus)
1237 {
1238         unsigned long bogosum = 0;
1239         int i;
1240
1241         for_each_online_cpu(i)
1242                 bogosum += cpu_data(i).udelay_val;
1243         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
1250 void smp_send_reschedule(int cpu)
1251 {
1252         smp_receive_signal(cpu);
1253 }
1254
1255 /* This is a nop because we capture all other cpus
1256  * anyways when making the PROM active.
1257  */
1258 void smp_send_stop(void)
1259 {
1260 }
1261
1262 unsigned long __per_cpu_base __read_mostly;
1263 unsigned long __per_cpu_shift __read_mostly;
1264
1265 EXPORT_SYMBOL(__per_cpu_base);
1266 EXPORT_SYMBOL(__per_cpu_shift);
1267
1268 void __init real_setup_per_cpu_areas(void)
1269 {
1270         unsigned long goal, size, i;
1271         char *ptr;
1272
1273         /* Copy section for each CPU (we discard the original) */
1274         goal = PERCPU_ENOUGH_ROOM;
1275
1276         __per_cpu_shift = PAGE_SHIFT;
1277         for (size = PAGE_SIZE; size < goal; size <<= 1UL)
1278                 __per_cpu_shift++;
1279
1280         ptr = alloc_bootmem_pages(size * NR_CPUS);
1281
1282         __per_cpu_base = ptr - __per_cpu_start;
1283
1284         for (i = 0; i < NR_CPUS; i++, ptr += size)
1285                 memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
1286
1287         /* Setup %g5 for the boot cpu.  */
1288         __local_per_cpu_offset = __per_cpu_offset(smp_processor_id());
1289 }