ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/kernel.h>
7 #include <linux/sched.h>
8 #include <linux/mm.h>
9 #include <linux/pagemap.h>
10 #include <linux/threads.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.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
23 #include <asm/head.h>
24 #include <asm/ptrace.h>
25 #include <asm/atomic.h>
26 #include <asm/tlbflush.h>
27 #include <asm/mmu_context.h>
28 #include <asm/cpudata.h>
29
30 #include <asm/irq.h>
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 #include <asm/oplib.h>
34 #include <asm/hardirq.h>
35 #include <asm/uaccess.h>
36 #include <asm/timer.h>
37 #include <asm/starfire.h>
38
39 extern int linux_num_cpus;
40 extern void calibrate_delay(void);
41
42 /* Please don't make this stuff initdata!!!  --DaveM */
43 static unsigned char boot_cpu_id;
44
45 cpumask_t cpu_online_map = CPU_MASK_NONE;
46 cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
47 static cpumask_t smp_commenced_mask;
48 static cpumask_t cpu_callout_map;
49
50 void smp_info(struct seq_file *m)
51 {
52         int i;
53         
54         seq_printf(m, "State:\n");
55         for (i = 0; i < NR_CPUS; i++) {
56                 if (cpu_online(i))
57                         seq_printf(m,
58                                    "CPU%d:\t\tonline\n", i);
59         }
60 }
61
62 void smp_bogo(struct seq_file *m)
63 {
64         int i;
65         
66         for (i = 0; i < NR_CPUS; i++)
67                 if (cpu_online(i))
68                         seq_printf(m,
69                                    "Cpu%dBogo\t: %lu.%02lu\n"
70                                    "Cpu%dClkTck\t: %016lx\n",
71                                    i, cpu_data(i).udelay_val / (500000/HZ),
72                                    (cpu_data(i).udelay_val / (5000/HZ)) % 100,
73                                    i, cpu_data(i).clock_tick);
74 }
75
76 void __init smp_store_cpu_info(int id)
77 {
78         int cpu_node;
79
80         /* multiplier and counter set by
81            smp_setup_percpu_timer()  */
82         cpu_data(id).udelay_val                 = loops_per_jiffy;
83
84         cpu_find_by_mid(id, &cpu_node);
85         cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
86                                                      "clock-frequency", 0);
87
88         cpu_data(id).pgcache_size               = 0;
89         cpu_data(id).pte_cache[0]               = NULL;
90         cpu_data(id).pte_cache[1]               = NULL;
91         cpu_data(id).pgdcache_size              = 0;
92         cpu_data(id).pgd_cache                  = NULL;
93         cpu_data(id).idle_volume                = 1;
94 }
95
96 static void smp_setup_percpu_timer(void);
97
98 static volatile unsigned long callin_flag = 0;
99
100 extern void inherit_locked_prom_mappings(int save_p);
101
102 void __init smp_callin(void)
103 {
104         int cpuid = hard_smp_processor_id();
105         extern int bigkernel;
106         extern unsigned long kern_locked_tte_data;
107
108         if (bigkernel) {
109                 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, 
110                         kern_locked_tte_data + 0x400000, KERNBASE + 0x400000);
111                 prom_itlb_load(sparc64_highest_locked_tlbent()-1, 
112                         kern_locked_tte_data + 0x400000, KERNBASE + 0x400000);
113         }
114
115         inherit_locked_prom_mappings(0);
116
117         __flush_tlb_all();
118
119         smp_setup_percpu_timer();
120
121         local_irq_enable();
122
123         calibrate_delay();
124         smp_store_cpu_info(cpuid);
125         callin_flag = 1;
126         __asm__ __volatile__("membar #Sync\n\t"
127                              "flush  %%g6" : : : "memory");
128
129         /* Clear this or we will die instantly when we
130          * schedule back to this idler...
131          */
132         clear_thread_flag(TIF_NEWCHILD);
133
134         /* Attach to the address space of init_task. */
135         atomic_inc(&init_mm.mm_count);
136         current->active_mm = &init_mm;
137
138         while (!cpu_isset(cpuid, smp_commenced_mask))
139                 membar("#LoadLoad");
140
141         cpu_set(cpuid, cpu_online_map);
142 }
143
144 void cpu_panic(void)
145 {
146         printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
147         panic("SMP bolixed\n");
148 }
149
150 static unsigned long current_tick_offset;
151
152 /* This tick register synchronization scheme is taken entirely from
153  * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit.
154  *
155  * The only change I've made is to rework it so that the master
156  * initiates the synchonization instead of the slave. -DaveM
157  */
158
159 #define MASTER  0
160 #define SLAVE   (SMP_CACHE_BYTES/sizeof(unsigned long))
161
162 #define NUM_ROUNDS      64      /* magic value */
163 #define NUM_ITERS       5       /* likewise */
164
165 static spinlock_t itc_sync_lock = SPIN_LOCK_UNLOCKED;
166 static unsigned long go[SLAVE + 1];
167
168 #define DEBUG_TICK_SYNC 0
169
170 static inline long get_delta (long *rt, long *master)
171 {
172         unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
173         unsigned long tcenter, t0, t1, tm;
174         unsigned long i;
175
176         for (i = 0; i < NUM_ITERS; i++) {
177                 t0 = tick_ops->get_tick();
178                 go[MASTER] = 1;
179                 membar("#StoreLoad");
180                 while (!(tm = go[SLAVE]))
181                         membar("#LoadLoad");
182                 go[SLAVE] = 0;
183                 membar("#StoreStore");
184                 t1 = tick_ops->get_tick();
185
186                 if (t1 - t0 < best_t1 - best_t0)
187                         best_t0 = t0, best_t1 = t1, best_tm = tm;
188         }
189
190         *rt = best_t1 - best_t0;
191         *master = best_tm - best_t0;
192
193         /* average best_t0 and best_t1 without overflow: */
194         tcenter = (best_t0/2 + best_t1/2);
195         if (best_t0 % 2 + best_t1 % 2 == 2)
196                 tcenter++;
197         return tcenter - best_tm;
198 }
199
200 void smp_synchronize_tick_client(void)
201 {
202         long i, delta, adj, adjust_latency = 0, done = 0;
203         unsigned long flags, rt, master_time_stamp, bound;
204 #if DEBUG_TICK_SYNC
205         struct {
206                 long rt;        /* roundtrip time */
207                 long master;    /* master's timestamp */
208                 long diff;      /* difference between midpoint and master's timestamp */
209                 long lat;       /* estimate of itc adjustment latency */
210         } t[NUM_ROUNDS];
211 #endif
212
213         go[MASTER] = 1;
214
215         while (go[MASTER])
216                 membar("#LoadLoad");
217
218         local_irq_save(flags);
219         {
220                 for (i = 0; i < NUM_ROUNDS; i++) {
221                         delta = get_delta(&rt, &master_time_stamp);
222                         if (delta == 0) {
223                                 done = 1;       /* let's lock on to this... */
224                                 bound = rt;
225                         }
226
227                         if (!done) {
228                                 if (i > 0) {
229                                         adjust_latency += -delta;
230                                         adj = -delta + adjust_latency/4;
231                                 } else
232                                         adj = -delta;
233
234                                 tick_ops->add_tick(adj, current_tick_offset);
235                         }
236 #if DEBUG_TICK_SYNC
237                         t[i].rt = rt;
238                         t[i].master = master_time_stamp;
239                         t[i].diff = delta;
240                         t[i].lat = adjust_latency/4;
241 #endif
242                 }
243         }
244         local_irq_restore(flags);
245
246 #if DEBUG_TICK_SYNC
247         for (i = 0; i < NUM_ROUNDS; i++)
248                 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
249                        t[i].rt, t[i].master, t[i].diff, t[i].lat);
250 #endif
251
252         printk(KERN_INFO "CPU %d: synchronized TICK with master CPU (last diff %ld cycles,"
253                "maxerr %lu cycles)\n", smp_processor_id(), delta, rt);
254 }
255
256 static void smp_start_sync_tick_client(int cpu);
257
258 static void smp_synchronize_one_tick(int cpu)
259 {
260         unsigned long flags, i;
261
262         go[MASTER] = 0;
263
264         smp_start_sync_tick_client(cpu);
265
266         /* wait for client to be ready */
267         while (!go[MASTER])
268                 membar("#LoadLoad");
269
270         /* now let the client proceed into his loop */
271         go[MASTER] = 0;
272         membar("#StoreLoad");
273
274         spin_lock_irqsave(&itc_sync_lock, flags);
275         {
276                 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
277                         while (!go[MASTER])
278                                 membar("#LoadLoad");
279                         go[MASTER] = 0;
280                         membar("#StoreStore");
281                         go[SLAVE] = tick_ops->get_tick();
282                         membar("#StoreLoad");
283                 }
284         }
285         spin_unlock_irqrestore(&itc_sync_lock, flags);
286 }
287
288 extern unsigned long sparc64_cpu_startup;
289
290 /* The OBP cpu startup callback truncates the 3rd arg cookie to
291  * 32-bits (I think) so to be safe we have it read the pointer
292  * contained here so we work on >4GB machines. -DaveM
293  */
294 static struct thread_info *cpu_new_thread = NULL;
295
296 static int __devinit smp_boot_one_cpu(unsigned int cpu)
297 {
298         unsigned long entry =
299                 (unsigned long)(&sparc64_cpu_startup);
300         unsigned long cookie =
301                 (unsigned long)(&cpu_new_thread);
302         struct task_struct *p;
303         int timeout, ret, cpu_node;
304
305         kernel_thread(NULL, NULL, CLONE_IDLETASK);
306
307         p = prev_task(&init_task);
308
309         init_idle(p, cpu);
310
311         unhash_process(p);
312
313         callin_flag = 0;
314         cpu_new_thread = p->thread_info;
315         cpu_set(cpu, cpu_callout_map);
316
317         cpu_find_by_mid(cpu, &cpu_node);
318         prom_startcpu(cpu_node, entry, cookie);
319
320         for (timeout = 0; timeout < 5000000; timeout++) {
321                 if (callin_flag)
322                         break;
323                 udelay(100);
324         }
325         if (callin_flag) {
326                 ret = 0;
327         } else {
328                 printk("Processor %d is stuck.\n", cpu);
329                 cpu_clear(cpu, cpu_callout_map);
330                 ret = -ENODEV;
331         }
332         cpu_new_thread = NULL;
333
334         return ret;
335 }
336
337 static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu)
338 {
339         u64 result, target;
340         int stuck, tmp;
341
342         if (this_is_starfire) {
343                 /* map to real upaid */
344                 cpu = (((cpu & 0x3c) << 1) |
345                         ((cpu & 0x40) >> 4) |
346                         (cpu & 0x3));
347         }
348
349         target = (cpu << 14) | 0x70;
350 again:
351         /* Ok, this is the real Spitfire Errata #54.
352          * One must read back from a UDB internal register
353          * after writes to the UDB interrupt dispatch, but
354          * before the membar Sync for that write.
355          * So we use the high UDB control register (ASI 0x7f,
356          * ADDR 0x20) for the dummy read. -DaveM
357          */
358         tmp = 0x40;
359         __asm__ __volatile__(
360         "wrpr   %1, %2, %%pstate\n\t"
361         "stxa   %4, [%0] %3\n\t"
362         "stxa   %5, [%0+%8] %3\n\t"
363         "add    %0, %8, %0\n\t"
364         "stxa   %6, [%0+%8] %3\n\t"
365         "membar #Sync\n\t"
366         "stxa   %%g0, [%7] %3\n\t"
367         "membar #Sync\n\t"
368         "mov    0x20, %%g1\n\t"
369         "ldxa   [%%g1] 0x7f, %%g0\n\t"
370         "membar #Sync"
371         : "=r" (tmp)
372         : "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),
373           "r" (data0), "r" (data1), "r" (data2), "r" (target),
374           "r" (0x10), "0" (tmp)
375         : "g1");
376
377         /* NOTE: PSTATE_IE is still clear. */
378         stuck = 100000;
379         do {
380                 __asm__ __volatile__("ldxa [%%g0] %1, %0"
381                         : "=r" (result)
382                         : "i" (ASI_INTR_DISPATCH_STAT));
383                 if (result == 0) {
384                         __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
385                                              : : "r" (pstate));
386                         return;
387                 }
388                 stuck -= 1;
389                 if (stuck == 0)
390                         break;
391         } while (result & 0x1);
392         __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
393                              : : "r" (pstate));
394         if (stuck == 0) {
395                 printk("CPU[%d]: mondo stuckage result[%016lx]\n",
396                        smp_processor_id(), result);
397         } else {
398                 udelay(2);
399                 goto again;
400         }
401 }
402
403 static __inline__ void spitfire_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
404 {
405         u64 pstate;
406         int i;
407
408         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
409         for (i = 0; i < NR_CPUS; i++) {
410                 if (cpu_isset(i, mask)) {
411                         spitfire_xcall_helper(data0, data1, data2, pstate, i);
412                         cpu_clear(i, mask);
413                         if (cpus_empty(mask))
414                                 break;
415                 }
416         }
417 }
418
419 /* Cheetah now allows to send the whole 64-bytes of data in the interrupt
420  * packet, but we have no use for that.  However we do take advantage of
421  * the new pipelining feature (ie. dispatch to multiple cpus simultaneously).
422  */
423 #if NR_CPUS > 32
424 #error Fixup cheetah_xcall_deliver Dave...
425 #endif
426 static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
427 {
428         u64 pstate, ver;
429         int nack_busy_id, is_jalapeno;
430
431         if (cpus_empty(mask))
432                 return;
433
434         /* Unfortunately, someone at Sun had the brilliant idea to make the
435          * busy/nack fields hard-coded by ITID number for this Ultra-III
436          * derivative processor.
437          */
438         __asm__ ("rdpr %%ver, %0" : "=r" (ver));
439         is_jalapeno = ((ver >> 32) == 0x003e0016);
440
441         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
442
443 retry:
444         __asm__ __volatile__("wrpr %0, %1, %%pstate\n\t"
445                              : : "r" (pstate), "i" (PSTATE_IE));
446
447         /* Setup the dispatch data registers. */
448         __asm__ __volatile__("stxa      %0, [%3] %6\n\t"
449                              "stxa      %1, [%4] %6\n\t"
450                              "stxa      %2, [%5] %6\n\t"
451                              "membar    #Sync\n\t"
452                              : /* no outputs */
453                              : "r" (data0), "r" (data1), "r" (data2),
454                                "r" (0x40), "r" (0x50), "r" (0x60),
455                                "i" (ASI_INTR_W));
456
457         nack_busy_id = 0;
458         {
459                 cpumask_t work_mask = mask;
460                 int i;
461
462                 for (i = 0; i < NR_CPUS; i++) {
463                         if (cpu_isset(i, work_mask)) {
464                                 u64 target = (i << 14) | 0x70;
465
466                                 if (!is_jalapeno)
467                                         target |= (nack_busy_id << 24);
468                                 __asm__ __volatile__(
469                                         "stxa   %%g0, [%0] %1\n\t"
470                                         "membar #Sync\n\t"
471                                         : /* no outputs */
472                                         : "r" (target), "i" (ASI_INTR_W));
473                                 nack_busy_id++;
474                                 cpu_clear(i, work_mask);
475                                 if (cpus_empty(work_mask))
476                                         break;
477                         }
478                 }
479         }
480
481         /* Now, poll for completion. */
482         {
483                 u64 dispatch_stat;
484                 long stuck;
485
486                 stuck = 100000 * nack_busy_id;
487                 do {
488                         __asm__ __volatile__("ldxa      [%%g0] %1, %0"
489                                              : "=r" (dispatch_stat)
490                                              : "i" (ASI_INTR_DISPATCH_STAT));
491                         if (dispatch_stat == 0UL) {
492                                 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
493                                                      : : "r" (pstate));
494                                 return;
495                         }
496                         if (!--stuck)
497                                 break;
498                 } while (dispatch_stat & 0x5555555555555555UL);
499
500                 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
501                                      : : "r" (pstate));
502
503                 if ((dispatch_stat & ~(0x5555555555555555UL)) == 0) {
504                         /* Busy bits will not clear, continue instead
505                          * of freezing up on this cpu.
506                          */
507                         printk("CPU[%d]: mondo stuckage result[%016lx]\n",
508                                smp_processor_id(), dispatch_stat);
509                 } else {
510                         cpumask_t work_mask = mask;
511                         int i, this_busy_nack = 0;
512
513                         /* Delay some random time with interrupts enabled
514                          * to prevent deadlock.
515                          */
516                         udelay(2 * nack_busy_id);
517
518                         /* Clear out the mask bits for cpus which did not
519                          * NACK us.
520                          */
521                         for (i = 0; i < NR_CPUS; i++) {
522                                 if (cpu_isset(i, work_mask)) {
523                                         u64 check_mask;
524
525                                         if (is_jalapeno)
526                                                 check_mask = (0x2UL << (2*i));
527                                         else
528                                                 check_mask = (0x2UL <<
529                                                               this_busy_nack);
530                                         if ((dispatch_stat & check_mask) == 0)
531                                                 cpu_clear(i, mask);
532                                         this_busy_nack += 2;
533                                         cpu_clear(i, work_mask);
534                                         if (cpus_empty(work_mask))
535                                                 break;
536                                 }
537                         }
538
539                         goto retry;
540                 }
541         }
542 }
543
544 /* Send cross call to all processors mentioned in MASK
545  * except self.
546  */
547 static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, cpumask_t mask)
548 {
549         u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
550
551         cpus_and(mask, mask, cpu_online_map);
552         cpu_clear(smp_processor_id(), mask);
553
554         if (tlb_type == spitfire)
555                 spitfire_xcall_deliver(data0, data1, data2, mask);
556         else
557                 cheetah_xcall_deliver(data0, data1, data2, mask);
558         /* NOTE: Caller runs local copy on master. */
559 }
560
561 extern unsigned long xcall_sync_tick;
562
563 static void smp_start_sync_tick_client(int cpu)
564 {
565         cpumask_t mask = cpumask_of_cpu(cpu);
566
567         smp_cross_call_masked(&xcall_sync_tick,
568                               0, 0, 0, mask);
569 }
570
571 /* Send cross call to all processors except self. */
572 #define smp_cross_call(func, ctx, data1, data2) \
573         smp_cross_call_masked(func, ctx, data1, data2, cpu_online_map)
574
575 struct call_data_struct {
576         void (*func) (void *info);
577         void *info;
578         atomic_t finished;
579         int wait;
580 };
581
582 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
583 static struct call_data_struct *call_data;
584
585 extern unsigned long xcall_call_function;
586
587 /*
588  * You must not call this function with disabled interrupts or from a
589  * hardware interrupt handler or from a bottom half handler.
590  */
591 int smp_call_function(void (*func)(void *info), void *info,
592                       int nonatomic, int wait)
593 {
594         struct call_data_struct data;
595         int cpus = num_online_cpus() - 1;
596         long timeout;
597
598         if (!cpus)
599                 return 0;
600
601         data.func = func;
602         data.info = info;
603         atomic_set(&data.finished, 0);
604         data.wait = wait;
605
606         spin_lock(&call_lock);
607
608         call_data = &data;
609
610         smp_cross_call(&xcall_call_function, 0, 0, 0);
611
612         /* 
613          * Wait for other cpus to complete function or at
614          * least snap the call data.
615          */
616         timeout = 1000000;
617         while (atomic_read(&data.finished) != cpus) {
618                 if (--timeout <= 0)
619                         goto out_timeout;
620                 barrier();
621                 udelay(1);
622         }
623
624         spin_unlock(&call_lock);
625
626         return 0;
627
628 out_timeout:
629         spin_unlock(&call_lock);
630         printk("XCALL: Remote cpus not responding, ncpus=%ld finished=%ld\n",
631                (long) num_online_cpus() - 1L,
632                (long) atomic_read(&data.finished));
633         return 0;
634 }
635
636 void smp_call_function_client(int irq, struct pt_regs *regs)
637 {
638         void (*func) (void *info) = call_data->func;
639         void *info = call_data->info;
640
641         clear_softint(1 << irq);
642         if (call_data->wait) {
643                 /* let initiator proceed only after completion */
644                 func(info);
645                 atomic_inc(&call_data->finished);
646         } else {
647                 /* let initiator proceed after getting data */
648                 atomic_inc(&call_data->finished);
649                 func(info);
650         }
651 }
652
653 extern unsigned long xcall_flush_tlb_page;
654 extern unsigned long xcall_flush_tlb_mm;
655 extern unsigned long xcall_flush_tlb_range;
656 extern unsigned long xcall_flush_tlb_kernel_range;
657 extern unsigned long xcall_flush_tlb_all_spitfire;
658 extern unsigned long xcall_flush_tlb_all_cheetah;
659 extern unsigned long xcall_report_regs;
660 extern unsigned long xcall_receive_signal;
661 extern unsigned long xcall_flush_dcache_page_cheetah;
662 extern unsigned long xcall_flush_dcache_page_spitfire;
663
664 #ifdef CONFIG_DEBUG_DCFLUSH
665 extern atomic_t dcpage_flushes;
666 extern atomic_t dcpage_flushes_xcall;
667 #endif
668
669 static __inline__ void __local_flush_dcache_page(struct page *page)
670 {
671 #if (L1DCACHE_SIZE > PAGE_SIZE)
672         __flush_dcache_page(page->virtual,
673                             ((tlb_type == spitfire) &&
674                              page_mapping(page) != NULL));
675 #else
676         if (page_mapping(page) != NULL &&
677             tlb_type == spitfire)
678                 __flush_icache_page(__pa(page->virtual));
679 #endif
680 }
681
682 void smp_flush_dcache_page_impl(struct page *page, int cpu)
683 {
684         cpumask_t mask = cpumask_of_cpu(cpu);
685
686 #ifdef CONFIG_DEBUG_DCFLUSH
687         atomic_inc(&dcpage_flushes);
688 #endif
689         if (cpu == smp_processor_id()) {
690                 __local_flush_dcache_page(page);
691         } else if (cpu_online(cpu)) {
692                 u64 data0;
693
694                 if (tlb_type == spitfire) {
695                         data0 =
696                                 ((u64)&xcall_flush_dcache_page_spitfire);
697                         if (page_mapping(page) != NULL)
698                                 data0 |= ((u64)1 << 32);
699                         spitfire_xcall_deliver(data0,
700                                                __pa(page->virtual),
701                                                (u64) page->virtual,
702                                                mask);
703                 } else {
704                         data0 =
705                                 ((u64)&xcall_flush_dcache_page_cheetah);
706                         cheetah_xcall_deliver(data0,
707                                               __pa(page->virtual),
708                                               0, mask);
709                 }
710 #ifdef CONFIG_DEBUG_DCFLUSH
711                 atomic_inc(&dcpage_flushes_xcall);
712 #endif
713         }
714 }
715
716 void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
717 {
718         cpumask_t mask = cpu_online_map;
719         u64 data0;
720
721         cpu_clear(smp_processor_id(), mask);
722
723 #ifdef CONFIG_DEBUG_DCFLUSH
724         atomic_inc(&dcpage_flushes);
725 #endif
726         if (cpus_empty(mask))
727                 goto flush_self;
728         if (tlb_type == spitfire) {
729                 data0 = ((u64)&xcall_flush_dcache_page_spitfire);
730                 if (page_mapping(page) != NULL)
731                         data0 |= ((u64)1 << 32);
732                 spitfire_xcall_deliver(data0,
733                                        __pa(page->virtual),
734                                        (u64) page->virtual,
735                                        mask);
736         } else {
737                 data0 = ((u64)&xcall_flush_dcache_page_cheetah);
738                 cheetah_xcall_deliver(data0,
739                                       __pa(page->virtual),
740                                       0, mask);
741         }
742 #ifdef CONFIG_DEBUG_DCFLUSH
743         atomic_inc(&dcpage_flushes_xcall);
744 #endif
745  flush_self:
746         __local_flush_dcache_page(page);
747 }
748
749 void smp_receive_signal(int cpu)
750 {
751         cpumask_t mask = cpumask_of_cpu(cpu);
752
753         if (cpu_online(cpu)) {
754                 u64 data0 = (((u64)&xcall_receive_signal) & 0xffffffff);
755
756                 if (tlb_type == spitfire)
757                         spitfire_xcall_deliver(data0, 0, 0, mask);
758                 else
759                         cheetah_xcall_deliver(data0, 0, 0, mask);
760         }
761 }
762
763 void smp_receive_signal_client(int irq, struct pt_regs *regs)
764 {
765         /* Just return, rtrap takes care of the rest. */
766         clear_softint(1 << irq);
767 }
768
769 void smp_report_regs(void)
770 {
771         smp_cross_call(&xcall_report_regs, 0, 0, 0);
772 }
773
774 void smp_flush_tlb_all(void)
775 {
776         if (tlb_type == spitfire)
777                 smp_cross_call(&xcall_flush_tlb_all_spitfire, 0, 0, 0);
778         else
779                 smp_cross_call(&xcall_flush_tlb_all_cheetah, 0, 0, 0);
780         __flush_tlb_all();
781 }
782
783 /* We know that the window frames of the user have been flushed
784  * to the stack before we get here because all callers of us
785  * are flush_tlb_*() routines, and these run after flush_cache_*()
786  * which performs the flushw.
787  *
788  * The SMP TLB coherency scheme we use works as follows:
789  *
790  * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
791  *    space has (potentially) executed on, this is the heuristic
792  *    we use to avoid doing cross calls.
793  *
794  *    Also, for flushing from kswapd and also for clones, we
795  *    use cpu_vm_mask as the list of cpus to make run the TLB.
796  *
797  * 2) TLB context numbers are shared globally across all processors
798  *    in the system, this allows us to play several games to avoid
799  *    cross calls.
800  *
801  *    One invariant is that when a cpu switches to a process, and
802  *    that processes tsk->active_mm->cpu_vm_mask does not have the
803  *    current cpu's bit set, that tlb context is flushed locally.
804  *
805  *    If the address space is non-shared (ie. mm->count == 1) we avoid
806  *    cross calls when we want to flush the currently running process's
807  *    tlb state.  This is done by clearing all cpu bits except the current
808  *    processor's in current->active_mm->cpu_vm_mask and performing the
809  *    flush locally only.  This will force any subsequent cpus which run
810  *    this task to flush the context from the local tlb if the process
811  *    migrates to another cpu (again).
812  *
813  * 3) For shared address spaces (threads) and swapping we bite the
814  *    bullet for most cases and perform the cross call (but only to
815  *    the cpus listed in cpu_vm_mask).
816  *
817  *    The performance gain from "optimizing" away the cross call for threads is
818  *    questionable (in theory the big win for threads is the massive sharing of
819  *    address space state across processors).
820  */
821 void smp_flush_tlb_mm(struct mm_struct *mm)
822 {
823         /*
824          * This code is called from two places, dup_mmap and exit_mmap. In the
825          * former case, we really need a flush. In the later case, the callers
826          * are single threaded exec_mmap (really need a flush), multithreaded
827          * exec_mmap case (do not need to flush, since the caller gets a new
828          * context via activate_mm), and all other callers of mmput() whence
829          * the flush can be optimized since the associated threads are dead and
830          * the mm is being torn down (__exit_mm and other mmput callers) or the
831          * owning thread is dissociating itself from the mm. The
832          * (atomic_read(&mm->mm_users) == 0) check ensures real work is done
833          * for single thread exec and dup_mmap cases. An alternate check might
834          * have been (current->mm != mm).
835          *                                              Kanoj Sarcar
836          */
837         if (atomic_read(&mm->mm_users) == 0)
838                 return;
839
840         {
841                 u32 ctx = CTX_HWBITS(mm->context);
842                 int cpu = smp_processor_id();
843
844                 if (atomic_read(&mm->mm_users) == 1) {
845                         /* See smp_flush_tlb_page for info about this. */
846                         mm->cpu_vm_mask = cpumask_of_cpu(cpu);
847                         goto local_flush_and_out;
848                 }
849
850                 smp_cross_call_masked(&xcall_flush_tlb_mm,
851                                       ctx, 0, 0,
852                                       mm->cpu_vm_mask);
853
854         local_flush_and_out:
855                 __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
856         }
857 }
858
859 void smp_flush_tlb_range(struct mm_struct *mm, unsigned long start,
860                          unsigned long end)
861 {
862         u32 ctx = CTX_HWBITS(mm->context);
863         int cpu = smp_processor_id();
864
865         start &= PAGE_MASK;
866         end    = PAGE_ALIGN(end);
867
868         if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1) {
869                 mm->cpu_vm_mask = cpumask_of_cpu(cpu);
870                 goto local_flush_and_out;
871         }
872
873         smp_cross_call_masked(&xcall_flush_tlb_range,
874                               ctx, start, end,
875                               mm->cpu_vm_mask);
876
877  local_flush_and_out:
878         __flush_tlb_range(ctx, start, SECONDARY_CONTEXT,
879                           end, PAGE_SIZE, (end-start));
880 }
881
882 void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
883 {
884         start &= PAGE_MASK;
885         end    = PAGE_ALIGN(end);
886         if (start != end) {
887                 smp_cross_call(&xcall_flush_tlb_kernel_range,
888                                0, start, end);
889
890                 __flush_tlb_kernel_range(start, end);
891         }
892 }
893
894 void smp_flush_tlb_page(struct mm_struct *mm, unsigned long page)
895 {
896         {
897                 u32 ctx = CTX_HWBITS(mm->context);
898                 int cpu = smp_processor_id();
899
900                 page &= PAGE_MASK;
901                 if (mm == current->active_mm &&
902                     atomic_read(&mm->mm_users) == 1) {
903                         /* By virtue of being the current address space, and
904                          * having the only reference to it, the following
905                          * operation is safe.
906                          *
907                          * It would not be a win to perform the xcall tlb
908                          * flush in this case, because even if we switch back
909                          * to one of the other processors in cpu_vm_mask it
910                          * is almost certain that all TLB entries for this
911                          * context will be replaced by the time that happens.
912                          */
913                         mm->cpu_vm_mask = cpumask_of_cpu(cpu);
914                         goto local_flush_and_out;
915                 } else {
916                         cpumask_t this_cpu_mask = cpumask_of_cpu(cpu);
917
918                         /* By virtue of running under the mm->page_table_lock,
919                          * and mmu_context.h:switch_mm doing the same, the
920                          * following operation is safe.
921                          */
922                         if (cpus_equal(mm->cpu_vm_mask, this_cpu_mask))
923                                 goto local_flush_and_out;
924                 }
925
926                 /* OK, we have to actually perform the cross call.  Most
927                  * likely this is a cloned mm or kswapd is kicking out pages
928                  * for a task which has run recently on another cpu.
929                  */
930                 smp_cross_call_masked(&xcall_flush_tlb_page,
931                                       ctx, page, 0,
932                                       mm->cpu_vm_mask);
933                 if (!cpu_isset(cpu, mm->cpu_vm_mask))
934                         return;
935
936         local_flush_and_out:
937                 __flush_tlb_page(ctx, page, SECONDARY_CONTEXT);
938         }
939 }
940
941 /* CPU capture. */
942 /* #define CAPTURE_DEBUG */
943 extern unsigned long xcall_capture;
944
945 static atomic_t smp_capture_depth = ATOMIC_INIT(0);
946 static atomic_t smp_capture_registry = ATOMIC_INIT(0);
947 static unsigned long penguins_are_doing_time;
948
949 void smp_capture(void)
950 {
951         int result = __atomic_add(1, &smp_capture_depth);
952
953         membar("#StoreStore | #LoadStore");
954         if (result == 1) {
955                 int ncpus = num_online_cpus();
956
957 #ifdef CAPTURE_DEBUG
958                 printk("CPU[%d]: Sending penguins to jail...",
959                        smp_processor_id());
960 #endif
961                 penguins_are_doing_time = 1;
962                 membar("#StoreStore | #LoadStore");
963                 atomic_inc(&smp_capture_registry);
964                 smp_cross_call(&xcall_capture, 0, 0, 0);
965                 while (atomic_read(&smp_capture_registry) != ncpus)
966                         membar("#LoadLoad");
967 #ifdef CAPTURE_DEBUG
968                 printk("done\n");
969 #endif
970         }
971 }
972
973 void smp_release(void)
974 {
975         if (atomic_dec_and_test(&smp_capture_depth)) {
976 #ifdef CAPTURE_DEBUG
977                 printk("CPU[%d]: Giving pardon to "
978                        "imprisoned penguins\n",
979                        smp_processor_id());
980 #endif
981                 penguins_are_doing_time = 0;
982                 membar("#StoreStore | #StoreLoad");
983                 atomic_dec(&smp_capture_registry);
984         }
985 }
986
987 /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
988  * can service tlb flush xcalls...
989  */
990 extern void prom_world(int);
991 extern void save_alternate_globals(unsigned long *);
992 extern void restore_alternate_globals(unsigned long *);
993 void smp_penguin_jailcell(int irq, struct pt_regs *regs)
994 {
995         unsigned long global_save[24];
996
997         clear_softint(1 << irq);
998
999         preempt_disable();
1000
1001         __asm__ __volatile__("flushw");
1002         save_alternate_globals(global_save);
1003         prom_world(1);
1004         atomic_inc(&smp_capture_registry);
1005         membar("#StoreLoad | #StoreStore");
1006         while (penguins_are_doing_time)
1007                 membar("#LoadLoad");
1008         restore_alternate_globals(global_save);
1009         atomic_dec(&smp_capture_registry);
1010         prom_world(0);
1011
1012         preempt_enable();
1013 }
1014
1015 extern unsigned long xcall_promstop;
1016
1017 void smp_promstop_others(void)
1018 {
1019         smp_cross_call(&xcall_promstop, 0, 0, 0);
1020 }
1021
1022 extern void sparc64_do_profile(struct pt_regs *regs);
1023
1024 #define prof_multiplier(__cpu)          cpu_data(__cpu).multiplier
1025 #define prof_counter(__cpu)             cpu_data(__cpu).counter
1026
1027 void smp_percpu_timer_interrupt(struct pt_regs *regs)
1028 {
1029         unsigned long compare, tick, pstate;
1030         int cpu = smp_processor_id();
1031         int user = user_mode(regs);
1032
1033         /*
1034          * Check for level 14 softint.
1035          */
1036         {
1037                 unsigned long tick_mask = tick_ops->softint_mask;
1038
1039                 if (!(get_softint() & tick_mask)) {
1040                         extern void handler_irq(int, struct pt_regs *);
1041
1042                         handler_irq(14, regs);
1043                         return;
1044                 }
1045                 clear_softint(tick_mask);
1046         }
1047
1048         do {
1049                 sparc64_do_profile(regs);
1050                 if (!--prof_counter(cpu)) {
1051                         irq_enter();
1052
1053                         if (cpu == boot_cpu_id) {
1054                                 kstat_this_cpu.irqs[0]++;
1055                                 timer_tick_interrupt(regs);
1056                         }
1057
1058                         update_process_times(user);
1059
1060                         irq_exit();
1061
1062                         prof_counter(cpu) = prof_multiplier(cpu);
1063                 }
1064
1065                 /* Guarantee that the following sequences execute
1066                  * uninterrupted.
1067                  */
1068                 __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
1069                                      "wrpr      %0, %1, %%pstate"
1070                                      : "=r" (pstate)
1071                                      : "i" (PSTATE_IE));
1072
1073                 compare = tick_ops->add_compare(current_tick_offset);
1074                 tick = tick_ops->get_tick();
1075
1076                 /* Restore PSTATE_IE. */
1077                 __asm__ __volatile__("wrpr      %0, 0x0, %%pstate"
1078                                      : /* no outputs */
1079                                      : "r" (pstate));
1080         } while (time_after_eq(tick, compare));
1081 }
1082
1083 static void __init smp_setup_percpu_timer(void)
1084 {
1085         int cpu = smp_processor_id();
1086         unsigned long pstate;
1087
1088         prof_counter(cpu) = prof_multiplier(cpu) = 1;
1089
1090         /* Guarantee that the following sequences execute
1091          * uninterrupted.
1092          */
1093         __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
1094                              "wrpr      %0, %1, %%pstate"
1095                              : "=r" (pstate)
1096                              : "i" (PSTATE_IE));
1097
1098         tick_ops->init_tick(current_tick_offset);
1099
1100         /* Restore PSTATE_IE. */
1101         __asm__ __volatile__("wrpr      %0, 0x0, %%pstate"
1102                              : /* no outputs */
1103                              : "r" (pstate));
1104 }
1105
1106 void __init smp_tick_init(void)
1107 {
1108         boot_cpu_id = hard_smp_processor_id();
1109         current_tick_offset = timer_tick_offset;
1110
1111         if (boot_cpu_id >= NR_CPUS) {
1112                 prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
1113                 prom_halt();
1114         }
1115
1116         cpu_set(boot_cpu_id, cpu_online_map);
1117         prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;
1118 }
1119
1120 cycles_t cacheflush_time;
1121 unsigned long cache_decay_ticks;
1122
1123 extern unsigned long cheetah_tune_scheduling(void);
1124
1125 static void __init smp_tune_scheduling(void)
1126 {
1127         unsigned long orig_flush_base, flush_base, flags, *p;
1128         unsigned int ecache_size, order;
1129         cycles_t tick1, tick2, raw;
1130         int cpu_node;
1131
1132         /* Approximate heuristic for SMP scheduling.  It is an
1133          * estimation of the time it takes to flush the L2 cache
1134          * on the local processor.
1135          *
1136          * The ia32 chooses to use the L1 cache flush time instead,
1137          * and I consider this complete nonsense.  The Ultra can service
1138          * a miss to the L1 with a hit to the L2 in 7 or 8 cycles, and
1139          * L2 misses are what create extra bus traffic (ie. the "cost"
1140          * of moving a process from one cpu to another).
1141          */
1142         printk("SMP: Calibrating ecache flush... ");
1143         if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1144                 cacheflush_time = cheetah_tune_scheduling();
1145                 goto report;
1146         }
1147
1148         cpu_find_by_instance(0, &cpu_node, NULL);
1149         ecache_size = prom_getintdefault(cpu_node,
1150                                          "ecache-size", (512 * 1024));
1151         if (ecache_size > (4 * 1024 * 1024))
1152                 ecache_size = (4 * 1024 * 1024);
1153         orig_flush_base = flush_base =
1154                 __get_free_pages(GFP_KERNEL, order = get_order(ecache_size));
1155
1156         if (flush_base != 0UL) {
1157                 local_irq_save(flags);
1158
1159                 /* Scan twice the size once just to get the TLB entries
1160                  * loaded and make sure the second scan measures pure misses.
1161                  */
1162                 for (p = (unsigned long *)flush_base;
1163                      ((unsigned long)p) < (flush_base + (ecache_size<<1));
1164                      p += (64 / sizeof(unsigned long)))
1165                         *((volatile unsigned long *)p);
1166
1167                 tick1 = tick_ops->get_tick();
1168
1169                 __asm__ __volatile__("1:\n\t"
1170                                      "ldx       [%0 + 0x000], %%g1\n\t"
1171                                      "ldx       [%0 + 0x040], %%g2\n\t"
1172                                      "ldx       [%0 + 0x080], %%g3\n\t"
1173                                      "ldx       [%0 + 0x0c0], %%g5\n\t"
1174                                      "add       %0, 0x100, %0\n\t"
1175                                      "cmp       %0, %2\n\t"
1176                                      "bne,pt    %%xcc, 1b\n\t"
1177                                      " nop"
1178                                      : "=&r" (flush_base)
1179                                      : "0" (flush_base),
1180                                        "r" (flush_base + ecache_size)
1181                                      : "g1", "g2", "g3", "g5");
1182
1183                 tick2 = tick_ops->get_tick();
1184
1185                 local_irq_restore(flags);
1186
1187                 raw = (tick2 - tick1);
1188
1189                 /* Dampen it a little, considering two processes
1190                  * sharing the cache and fitting.
1191                  */
1192                 cacheflush_time = (raw - (raw >> 2));
1193
1194                 free_pages(orig_flush_base, order);
1195         } else {
1196                 cacheflush_time = ((ecache_size << 2) +
1197                                    (ecache_size << 1));
1198         }
1199 report:
1200         /* Convert ticks/sticks to jiffies. */
1201         cache_decay_ticks = cacheflush_time / timer_tick_offset;
1202         if (cache_decay_ticks < 1)
1203                 cache_decay_ticks = 1;
1204
1205         printk("Using heuristic of %ld cycles, %ld ticks.\n",
1206                cacheflush_time, cache_decay_ticks);
1207 }
1208
1209 /* /proc/profile writes can call this, don't __init it please. */
1210 static spinlock_t prof_setup_lock = SPIN_LOCK_UNLOCKED;
1211
1212 int setup_profiling_timer(unsigned int multiplier)
1213 {
1214         unsigned long flags;
1215         int i;
1216
1217         if ((!multiplier) || (timer_tick_offset / multiplier) < 1000)
1218                 return -EINVAL;
1219
1220         spin_lock_irqsave(&prof_setup_lock, flags);
1221         for (i = 0; i < NR_CPUS; i++)
1222                 prof_multiplier(i) = multiplier;
1223         current_tick_offset = (timer_tick_offset / multiplier);
1224         spin_unlock_irqrestore(&prof_setup_lock, flags);
1225
1226         return 0;
1227 }
1228
1229 void __init smp_prepare_cpus(unsigned int max_cpus)
1230 {
1231         int instance, mid;
1232
1233         instance = 0;
1234         while (!cpu_find_by_instance(instance, NULL, &mid)) {
1235                 if (mid < max_cpus)
1236                         cpu_set(mid, phys_cpu_present_map);
1237                 instance++;
1238         }
1239
1240         if (num_possible_cpus() > max_cpus) {
1241                 instance = 0;
1242                 while (!cpu_find_by_instance(instance, NULL, &mid)) {
1243                         if (mid != boot_cpu_id) {
1244                                 cpu_clear(mid, phys_cpu_present_map);
1245                                 if (num_possible_cpus() <= max_cpus)
1246                                         break;
1247                         }
1248                         instance++;
1249                 }
1250         }
1251
1252         smp_store_cpu_info(boot_cpu_id);
1253 }
1254
1255 void __devinit smp_prepare_boot_cpu(void)
1256 {
1257         current_thread_info()->cpu = hard_smp_processor_id();
1258         cpu_set(smp_processor_id(), cpu_online_map);
1259         cpu_set(smp_processor_id(), phys_cpu_present_map);
1260 }
1261
1262 int __devinit __cpu_up(unsigned int cpu)
1263 {
1264         int ret = smp_boot_one_cpu(cpu);
1265
1266         if (!ret) {
1267                 cpu_set(cpu, smp_commenced_mask);
1268                 while (!cpu_isset(cpu, cpu_online_map))
1269                         mb();
1270                 if (!cpu_isset(cpu, cpu_online_map)) {
1271                         ret = -ENODEV;
1272                 } else {
1273                         smp_synchronize_one_tick(cpu);
1274                 }
1275         }
1276         return ret;
1277 }
1278
1279 void __init smp_cpus_done(unsigned int max_cpus)
1280 {
1281         unsigned long bogosum = 0;
1282         int i;
1283
1284         for (i = 0; i < NR_CPUS; i++) {
1285                 if (cpu_online(i))
1286                         bogosum += cpu_data(i).udelay_val;
1287         }
1288         printk("Total of %ld processors activated "
1289                "(%lu.%02lu BogoMIPS).\n",
1290                (long) num_online_cpus(),
1291                bogosum/(500000/HZ),
1292                (bogosum/(5000/HZ))%100);
1293
1294         /* We want to run this with all the other cpus spinning
1295          * in the kernel.
1296          */
1297         smp_tune_scheduling();
1298 }
1299
1300 /* This needn't do anything as we do not sleep the cpu
1301  * inside of the idler task, so an interrupt is not needed
1302  * to get a clean fast response.
1303  *
1304  * XXX Reverify this assumption... -DaveM
1305  *
1306  * Addendum: We do want it to do something for the signal
1307  *           delivery case, we detect that by just seeing
1308  *           if we are trying to send this to an idler or not.
1309  */
1310 void smp_send_reschedule(int cpu)
1311 {
1312         if (cpu_data(cpu).idle_volume == 0)
1313                 smp_receive_signal(cpu);
1314 }
1315
1316 /* This is a nop because we capture all other cpus
1317  * anyways when making the PROM active.
1318  */
1319 void smp_send_stop(void)
1320 {
1321 }
1322