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