make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / alpha / kernel / smp.c
1 /*
2  *      linux/arch/alpha/kernel/smp.c
3  *
4  *      2001-07-09 Phil Ezolt (Phillip.Ezolt@compaq.com)
5  *            Renamed modified smp_call_function to smp_call_function_on_cpu()
6  *            Created an function that conforms to the old calling convention
7  *            of smp_call_function().
8  *
9  *            This is helpful for DCPI.
10  *
11  */
12
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/threads.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/irq.h>
26 #include <linux/cache.h>
27
28 #include <asm/hwrpb.h>
29 #include <asm/ptrace.h>
30 #include <asm/atomic.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/bitops.h>
35 #include <asm/pgtable.h>
36 #include <asm/pgalloc.h>
37 #include <asm/hardirq.h>
38 #include <asm/softirq.h>
39 #include <asm/mmu_context.h>
40
41 #define __KERNEL_SYSCALLS__
42 #include <asm/unistd.h>
43
44 #include "proto.h"
45 #include "irq_impl.h"
46
47
48 #define DEBUG_SMP 0
49 #if DEBUG_SMP
50 #define DBGS(args)      printk args
51 #else
52 #define DBGS(args)
53 #endif
54
55 /* A collection of per-processor data.  */
56 struct cpuinfo_alpha cpu_data[NR_CPUS];
57
58 /* A collection of single bit ipi messages.  */
59 static struct {
60         unsigned long bits ____cacheline_aligned;
61 } ipi_data[NR_CPUS] __cacheline_aligned;
62
63 enum ipi_message_type {
64         IPI_RESCHEDULE,
65         IPI_CALL_FUNC,
66         IPI_CPU_STOP,
67 };
68
69 spinlock_t kernel_flag __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
70
71 /* Set to a secondary's cpuid when it comes online.  */
72 static unsigned long smp_secondary_alive;
73
74 /* Which cpus ids came online.  */
75 unsigned long cpu_present_mask;
76
77 /* cpus reported in the hwrpb */
78 static unsigned long hwrpb_cpu_present_mask __initdata = 0;
79
80 static int max_cpus = -1;       /* Command-line limitation.  */
81 int smp_num_probed;             /* Internal processor count */
82 int smp_num_cpus = 1;           /* Number that came online.  */
83 int smp_threads_ready;          /* True once the per process idle is forked. */
84
85 int __cpu_number_map[NR_CPUS];
86 int __cpu_logical_map[NR_CPUS];
87
88 extern void calibrate_delay(void);
89 extern asmlinkage void entInt(void);
90
91 \f
92 static int __init nosmp(char *str)
93 {
94         max_cpus = 0;
95         return 1;
96 }
97
98 __setup("nosmp", nosmp);
99
100 static int __init maxcpus(char *str)
101 {
102         get_option(&str, &max_cpus);
103         return 1;
104 }
105
106 __setup("maxcpus", maxcpus);
107
108
109 /*
110  * Called by both boot and secondaries to move global data into
111  *  per-processor storage.
112  */
113 static inline void __init
114 smp_store_cpu_info(int cpuid)
115 {
116         cpu_data[cpuid].loops_per_jiffy = loops_per_jiffy;
117         cpu_data[cpuid].last_asn = ASN_FIRST_VERSION;
118         cpu_data[cpuid].need_new_asn = 0;
119         cpu_data[cpuid].asn_lock = 0;
120         local_irq_count(cpuid) = 0;
121         local_bh_count(cpuid) = 0;
122 }
123
124 /*
125  * Ideally sets up per-cpu profiling hooks.  Doesn't do much now...
126  */
127 static inline void __init
128 smp_setup_percpu_timer(int cpuid)
129 {
130         cpu_data[cpuid].prof_counter = 1;
131         cpu_data[cpuid].prof_multiplier = 1;
132 }
133
134 static void __init
135 wait_boot_cpu_to_stop(int cpuid)
136 {
137         long stop = jiffies + 10*HZ;
138
139         while (time_before(jiffies, stop)) {
140                 if (!smp_secondary_alive)
141                         return;
142                 barrier();
143         }
144
145         printk("wait_boot_cpu_to_stop: FAILED on CPU %d, hanging now\n", cpuid);
146         for (;;)
147                 barrier();
148 }
149
150 /*
151  * Where secondaries begin a life of C.
152  */
153 void __init
154 smp_callin(void)
155 {
156         int cpuid = hard_smp_processor_id();
157
158         if (current != init_tasks[cpu_number_map(cpuid)]) {
159                 printk("BUG: smp_calling: cpu %d current %p init_tasks[cpu_number_map(cpuid)] %p\n",
160                        cpuid, current, init_tasks[cpu_number_map(cpuid)]);
161         }
162
163         DBGS(("CALLIN %d state 0x%lx\n", cpuid, current->state));
164
165         /* Turn on machine checks.  */
166         wrmces(7);
167
168         /* Set trap vectors.  */
169         trap_init();
170
171         /* Set interrupt vector.  */
172         wrent(entInt, 0);
173
174         /* Get our local ticker going. */
175         smp_setup_percpu_timer(cpuid);
176
177         /* Call platform-specific callin, if specified */
178         if (alpha_mv.smp_callin) alpha_mv.smp_callin();
179
180         /* Must have completely accurate bogos.  */
181         __sti();
182
183         /*
184          * Wait boot CPU to stop with irq enabled before
185          * running calibrate_delay().
186          */
187         wait_boot_cpu_to_stop(cpuid);
188         mb();
189
190         calibrate_delay();
191
192         smp_store_cpu_info(cpuid);
193
194         {
195 #define LPJ(c) ((long)cpu_data[c].loops_per_jiffy)
196           long diff = LPJ(boot_cpuid) - LPJ(cpuid);
197           if (diff < 0) diff = -diff;
198                                 
199           if (diff > LPJ(boot_cpuid)/10) {
200                 printk("Bogus BogoMIPS for cpu %d - trusting boot CPU\n",
201                        cpuid);
202                 loops_per_jiffy = LPJ(cpuid) = LPJ(boot_cpuid);
203           }
204         }
205
206         /*
207          * Allow master to continue only after we written
208          * the loops_per_jiffy.
209          */
210         wmb();
211         smp_secondary_alive = 1;
212
213         /* Wait for the go code.  */
214         while (!smp_threads_ready)
215                 barrier();
216
217         DBGS(("smp_callin: commencing CPU %d current %p\n",
218               cpuid, current));
219
220         /* Setup the scheduler for this processor.  */
221         init_idle();
222
223         /* ??? This should be in init_idle.  */
224         atomic_inc(&init_mm.mm_count);
225         current->active_mm = &init_mm;
226         /* Do nothing.  */
227         cpu_idle();
228 }
229
230 /*
231  * Send a message to a secondary's console.  "START" is one such
232  * interesting message.  ;-)
233  */
234 static void
235 send_secondary_console_msg(char *str, int cpuid)
236 {
237         struct percpu_struct *cpu;
238         register char *cp1, *cp2;
239         unsigned long cpumask;
240         size_t len;
241         long timeout;
242
243         cpu = (struct percpu_struct *)
244                 ((char*)hwrpb
245                  + hwrpb->processor_offset
246                  + cpuid * hwrpb->processor_size);
247
248         cpumask = (1UL << cpuid);
249         if (hwrpb->txrdy & cpumask)
250                 goto delay1;
251         ready1:
252
253         cp2 = str;
254         len = strlen(cp2);
255         *(unsigned int *)&cpu->ipc_buffer[0] = len;
256         cp1 = (char *) &cpu->ipc_buffer[1];
257         memcpy(cp1, cp2, len);
258
259         /* atomic test and set */
260         wmb();
261         set_bit(cpuid, &hwrpb->rxrdy);
262
263         if (hwrpb->txrdy & cpumask)
264                 goto delay2;
265         ready2:
266         return;
267
268 delay1:
269         /* Wait 10 seconds.  Note that jiffies aren't ticking yet.  */
270         for (timeout = 1000000; timeout > 0; --timeout) {
271                 if (!(hwrpb->txrdy & cpumask))
272                         goto ready1;
273                 udelay(10);
274                 barrier();
275         }
276         goto timeout;
277
278 delay2:
279         /* Wait 10 seconds.  */
280         for (timeout = 1000000; timeout > 0; --timeout) {
281                 if (!(hwrpb->txrdy & cpumask))
282                         goto ready2;
283                 udelay(10);
284                 barrier();
285         }
286         goto timeout;
287
288 timeout:
289         printk("Processor %x not ready\n", cpuid);
290         return;
291 }
292
293 /*
294  * A secondary console wants to send a message.  Receive it.
295  */
296 static void
297 recv_secondary_console_msg(void)
298 {
299         int mycpu, i, cnt;
300         unsigned long txrdy = hwrpb->txrdy;
301         char *cp1, *cp2, buf[80];
302         struct percpu_struct *cpu;
303
304         DBGS(("recv_secondary_console_msg: TXRDY 0x%lx.\n", txrdy));
305
306         mycpu = hard_smp_processor_id();
307
308         for (i = 0; i < NR_CPUS; i++) {
309                 if (!(txrdy & (1UL << i)))
310                         continue;
311
312                 DBGS(("recv_secondary_console_msg: "
313                       "TXRDY contains CPU %d.\n", i));
314
315                 cpu = (struct percpu_struct *)
316                   ((char*)hwrpb
317                    + hwrpb->processor_offset
318                    + i * hwrpb->processor_size);
319
320                 DBGS(("recv_secondary_console_msg: on %d from %d"
321                       " HALT_REASON 0x%lx FLAGS 0x%lx\n",
322                       mycpu, i, cpu->halt_reason, cpu->flags));
323
324                 cnt = cpu->ipc_buffer[0] >> 32;
325                 if (cnt <= 0 || cnt >= 80)
326                         strcpy(buf, "<<< BOGUS MSG >>>");
327                 else {
328                         cp1 = (char *) &cpu->ipc_buffer[11];
329                         cp2 = buf;
330                         strcpy(cp2, cp1);
331                         
332                         while ((cp2 = strchr(cp2, '\r')) != 0) {
333                                 *cp2 = ' ';
334                                 if (cp2[1] == '\n')
335                                         cp2[1] = ' ';
336                         }
337                 }
338
339                 DBGS((KERN_INFO "recv_secondary_console_msg: on %d "
340                       "message is '%s'\n", mycpu, buf));
341         }
342
343         hwrpb->txrdy = 0;
344 }
345
346 /*
347  * Convince the console to have a secondary cpu begin execution.
348  */
349 static int __init
350 secondary_cpu_start(int cpuid, struct task_struct *idle)
351 {
352         struct percpu_struct *cpu;
353         struct pcb_struct *hwpcb;
354         long timeout;
355           
356         cpu = (struct percpu_struct *)
357                 ((char*)hwrpb
358                  + hwrpb->processor_offset
359                  + cpuid * hwrpb->processor_size);
360         hwpcb = (struct pcb_struct *) cpu->hwpcb;
361
362         /* Initialize the CPU's HWPCB to something just good enough for
363            us to get started.  Immediately after starting, we'll swpctx
364            to the target idle task's ptb.  Reuse the stack in the mean
365            time.  Precalculate the target PCBB.  */
366         hwpcb->ksp = (unsigned long) idle + sizeof(union task_union) - 16;
367         hwpcb->usp = 0;
368         hwpcb->ptbr = idle->thread.ptbr;
369         hwpcb->pcc = 0;
370         hwpcb->asn = 0;
371         hwpcb->unique = virt_to_phys(&idle->thread);
372         hwpcb->flags = idle->thread.pal_flags;
373         hwpcb->res1 = hwpcb->res2 = 0;
374
375 #if 0
376         DBGS(("KSP 0x%lx PTBR 0x%lx VPTBR 0x%lx UNIQUE 0x%lx\n",
377               hwpcb->ksp, hwpcb->ptbr, hwrpb->vptb, hwpcb->unique));
378 #endif
379         DBGS(("Starting secondary cpu %d: state 0x%lx pal_flags 0x%lx\n",
380               cpuid, idle->state, idle->thread.pal_flags));
381
382         /* Setup HWRPB fields that SRM uses to activate secondary CPU */
383         hwrpb->CPU_restart = __smp_callin;
384         hwrpb->CPU_restart_data = (unsigned long) __smp_callin;
385
386         /* Recalculate and update the HWRPB checksum */
387         hwrpb_update_checksum(hwrpb);
388
389         /*
390          * Send a "start" command to the specified processor.
391          */
392
393         /* SRM III 3.4.1.3 */
394         cpu->flags |= 0x22;     /* turn on Context Valid and Restart Capable */
395         cpu->flags &= ~1;       /* turn off Bootstrap In Progress */
396         wmb();
397
398         send_secondary_console_msg("START\r\n", cpuid);
399
400         /* Wait 10 seconds for an ACK from the console.  Note that jiffies 
401            aren't ticking yet.  */
402         for (timeout = 1000000; timeout > 0; timeout--) {
403                 if (cpu->flags & 1)
404                         goto started;
405                 udelay(10);
406                 barrier();
407         }
408         printk(KERN_ERR "SMP: Processor %d failed to start.\n", cpuid);
409         return -1;
410
411 started:
412         DBGS(("secondary_cpu_start: SUCCESS for CPU %d!!!\n", cpuid));
413         return 0;
414 }
415
416 static int __init fork_by_hand(void)
417 {
418         struct pt_regs regs;
419         /*
420          * don't care about the regs settings since
421          * we'll never reschedule the forked task.
422          */
423         return do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0);
424 }
425
426 /*
427  * Bring one cpu online.
428  */
429 static int __init
430 smp_boot_one_cpu(int cpuid, int cpunum)
431 {
432         struct task_struct *idle;
433         long timeout;
434
435         /* Cook up an idler for this guy.  Note that the address we give
436            to kernel_thread is irrelevant -- it's going to start where
437            HWRPB.CPU_restart says to start.  But this gets all the other
438            task-y sort of data structures set up like we wish.  */
439         /*
440          * We can't use kernel_thread since we must avoid to
441          * reschedule the child.
442          */
443         if (fork_by_hand() < 0)
444                 panic("failed fork for CPU %d", cpuid);
445
446         idle = init_task.prev_task;
447         if (!idle)
448                 panic("No idle process for CPU %d", cpuid);
449         if (idle == &init_task)
450                 panic("idle process is init_task for CPU %d", cpuid);
451
452         idle->processor = cpuid;
453         idle->cpus_runnable = 1 << cpuid; /* we schedule the first task manually */
454         __cpu_logical_map[cpunum] = cpuid;
455         __cpu_number_map[cpuid] = cpunum;
456  
457         del_from_runqueue(idle);
458         unhash_process(idle);
459         init_tasks[cpunum] = idle;
460
461         DBGS(("smp_boot_one_cpu: CPU %d state 0x%lx flags 0x%lx\n",
462               cpuid, idle->state, idle->flags));
463
464         /* The secondary will change this once it is happy.  Note that
465            secondary_cpu_start contains the necessary memory barrier.  */
466         smp_secondary_alive = -1;
467
468         /* Whirrr, whirrr, whirrrrrrrrr... */
469         if (secondary_cpu_start(cpuid, idle))
470                 return -1;
471
472         mb();
473         /* Notify the secondary CPU it can run calibrate_delay() */
474         smp_secondary_alive = 0;
475
476         /* We've been acked by the console; wait one second for the task
477            to start up for real.  Note that jiffies aren't ticking yet.  */
478         for (timeout = 0; timeout < 1000000; timeout++) {
479                 if (smp_secondary_alive == 1)
480                         goto alive;
481                 udelay(10);
482                 barrier();
483         }
484
485         /* we must invalidate our stuff as we failed to boot the CPU */
486         __cpu_logical_map[cpunum] = -1;
487         __cpu_number_map[cpuid] = -1;
488
489         /* the idle task is local to us so free it as we don't use it */
490         free_task_struct(idle);
491
492         printk(KERN_ERR "SMP: Processor %d is stuck.\n", cpuid);
493         return -1;
494
495 alive:
496         /* Another "Red Snapper". */
497         return 0;
498 }
499
500 /*
501  * Called from setup_arch.  Detect an SMP system and which processors
502  * are present.
503  */
504 void __init
505 setup_smp(void)
506 {
507         struct percpu_struct *cpubase, *cpu;
508         int i;
509
510         if (boot_cpuid != 0) {
511                 printk(KERN_WARNING "SMP: Booting off cpu %d instead of 0?\n",
512                        boot_cpuid);
513         }
514
515         if (hwrpb->nr_processors > 1) {
516                 int boot_cpu_palrev;
517
518                 DBGS(("setup_smp: nr_processors %ld\n",
519                       hwrpb->nr_processors));
520
521                 cpubase = (struct percpu_struct *)
522                         ((char*)hwrpb + hwrpb->processor_offset);
523                 boot_cpu_palrev = cpubase->pal_revision;
524
525                 for (i = 0; i < hwrpb->nr_processors; i++ ) {
526                         cpu = (struct percpu_struct *)
527                                 ((char *)cpubase + i*hwrpb->processor_size);
528                         if ((cpu->flags & 0x1cc) == 0x1cc) {
529                                 smp_num_probed++;
530                                 /* Assume here that "whami" == index */
531                                 hwrpb_cpu_present_mask |= (1UL << i);
532                                 cpu->pal_revision = boot_cpu_palrev;
533                         }
534
535                         DBGS(("setup_smp: CPU %d: flags 0x%lx type 0x%lx\n",
536                               i, cpu->flags, cpu->type));
537                         DBGS(("setup_smp: CPU %d: PAL rev 0x%lx\n",
538                               i, cpu->pal_revision));
539                 }
540         } else {
541                 smp_num_probed = 1;
542                 hwrpb_cpu_present_mask = (1UL << boot_cpuid);
543         }
544         cpu_present_mask = 1UL << boot_cpuid;
545
546         printk(KERN_INFO "SMP: %d CPUs probed -- cpu_present_mask = %lx\n",
547                smp_num_probed, hwrpb_cpu_present_mask);
548 }
549
550 /*
551  * Called by smp_init bring all the secondaries online and hold them.
552  */
553 void __init
554 smp_boot_cpus(void)
555 {
556         int cpu_count, i;
557         unsigned long bogosum;
558
559         /* Take care of some initial bookkeeping.  */
560         memset(__cpu_number_map, -1, sizeof(__cpu_number_map));
561         memset(__cpu_logical_map, -1, sizeof(__cpu_logical_map));
562         memset(ipi_data, 0, sizeof(ipi_data));
563
564         __cpu_number_map[boot_cpuid] = 0;
565         __cpu_logical_map[0] = boot_cpuid;
566         current->processor = boot_cpuid;
567
568         smp_store_cpu_info(boot_cpuid);
569         smp_setup_percpu_timer(boot_cpuid);
570
571         init_idle();
572
573         /* ??? This should be in init_idle.  */
574         atomic_inc(&init_mm.mm_count);
575         current->active_mm = &init_mm;
576
577         /* Nothing to do on a UP box, or when told not to.  */
578         if (smp_num_probed == 1 || max_cpus == 0) {
579                 printk(KERN_INFO "SMP mode deactivated.\n");
580                 return;
581         }
582
583         printk(KERN_INFO "SMP starting up secondaries.\n");
584
585         cpu_count = 1;
586         for (i = 0; i < NR_CPUS; i++) {
587                 if (i == boot_cpuid)
588                         continue;
589
590                 if (((hwrpb_cpu_present_mask >> i) & 1) == 0)
591                         continue;
592
593                 if (smp_boot_one_cpu(i, cpu_count))
594                         continue;
595
596                 cpu_present_mask |= 1UL << i;
597                 cpu_count++;
598         }
599
600         if (cpu_count == 1) {
601                 printk(KERN_ERR "SMP: Only one lonely processor alive.\n");
602                 return;
603         }
604
605         bogosum = 0;
606         for (i = 0; i < NR_CPUS; i++) {
607                 if (cpu_present_mask & (1UL << i))
608                         bogosum += cpu_data[i].loops_per_jiffy;
609         }
610         printk(KERN_INFO "SMP: Total of %d processors activated "
611                "(%lu.%02lu BogoMIPS).\n",
612                cpu_count, bogosum / (500000/HZ),
613                (bogosum / (5000/HZ)) % 100);
614
615         smp_num_cpus = cpu_count;
616 }
617
618 /*
619  * Called by smp_init to release the blocking online cpus once they 
620  * are all started.
621  */
622 void __init
623 smp_commence(void)
624 {
625         /* smp_init sets smp_threads_ready -- that's enough.  */
626         mb();
627 }
628
629 \f
630 void
631 smp_percpu_timer_interrupt(struct pt_regs *regs)
632 {
633         int cpu = smp_processor_id();
634         unsigned long user = user_mode(regs);
635         struct cpuinfo_alpha *data = &cpu_data[cpu];
636
637         /* Record kernel PC.  */
638         if (!user)
639                 alpha_do_profile(regs->pc);
640
641         if (!--data->prof_counter) {
642                 /* We need to make like a normal interrupt -- otherwise
643                    timer interrupts ignore the global interrupt lock,
644                    which would be a Bad Thing.  */
645                 irq_enter(cpu, RTC_IRQ);
646
647                 update_process_times(user);
648
649                 data->prof_counter = data->prof_multiplier;
650                 irq_exit(cpu, RTC_IRQ);
651
652                 if (softirq_pending(cpu))
653                         do_softirq();
654         }
655 }
656
657 int __init
658 setup_profiling_timer(unsigned int multiplier)
659 {
660         return -EINVAL;
661 }
662
663 \f
664 static void
665 send_ipi_message(unsigned long to_whom, enum ipi_message_type operation)
666 {
667         long i, j;
668
669         /* Reduce the number of memory barriers by doing two loops,
670            one to set the bits, one to invoke the interrupts.  */
671
672         mb();   /* Order out-of-band data and bit setting. */
673
674         for (i = 0, j = 1; i < NR_CPUS; ++i, j <<= 1) {
675                 if (to_whom & j)
676                         set_bit(operation, &ipi_data[i].bits);
677         }
678
679         mb();   /* Order bit setting and interrupt. */
680
681         for (i = 0, j = 1; i < NR_CPUS; ++i, j <<= 1) {
682                 if (to_whom & j)
683                         wripir(i);
684         }
685 }
686
687 /* Structure and data for smp_call_function.  This is designed to 
688    minimize static memory requirements.  Plus it looks cleaner.  */
689
690 struct smp_call_struct {
691         void (*func) (void *info);
692         void *info;
693         long wait;
694         atomic_t unstarted_count;
695         atomic_t unfinished_count;
696 };
697
698 static struct smp_call_struct *smp_call_function_data;
699
700 /* Atomicly drop data into a shared pointer.  The pointer is free if
701    it is initially locked.  If retry, spin until free.  */
702
703 static inline int
704 pointer_lock (void *lock, void *data, int retry)
705 {
706         void *old, *tmp;
707
708         mb();
709 again:
710         /* Compare and swap with zero.  */
711         asm volatile (
712         "1:     ldq_l   %0,%1\n"
713         "       mov     %3,%2\n"
714         "       bne     %0,2f\n"
715         "       stq_c   %2,%1\n"
716         "       beq     %2,1b\n"
717         "2:"
718         : "=&r"(old), "=m"(*(void **)lock), "=&r"(tmp)
719         : "r"(data)
720         : "memory");
721
722         if (old == 0)
723                 return 0;
724         if (! retry)
725                 return -EBUSY;
726
727         while (*(void **)lock)
728                 barrier();
729         goto again;
730 }
731
732 void
733 handle_ipi(struct pt_regs *regs)
734 {
735         int this_cpu = smp_processor_id();
736         unsigned long *pending_ipis = &ipi_data[this_cpu].bits;
737         unsigned long ops;
738
739 #if 0
740         DBGS(("handle_ipi: on CPU %d ops 0x%lx PC 0x%lx\n",
741               this_cpu, *pending_ipis, regs->pc));
742 #endif
743
744         mb();   /* Order interrupt and bit testing. */
745         while ((ops = xchg(pending_ipis, 0)) != 0) {
746           mb(); /* Order bit clearing and data access. */
747           do {
748                 unsigned long which;
749
750                 which = ops & -ops;
751                 ops &= ~which;
752                 which = ffz(~which);
753
754                 if (which == IPI_RESCHEDULE) {
755                         /* Reschedule callback.  Everything to be done
756                            is done by the interrupt return path.  */
757                 }
758                 else if (which == IPI_CALL_FUNC) {
759                         struct smp_call_struct *data;
760                         void (*func)(void *info);
761                         void *info;
762                         int wait;
763
764                         data = smp_call_function_data;
765                         func = data->func;
766                         info = data->info;
767                         wait = data->wait;
768
769                         /* Notify the sending CPU that the data has been
770                            received, and execution is about to begin.  */
771                         mb();
772                         atomic_dec (&data->unstarted_count);
773
774                         /* At this point the structure may be gone unless
775                            wait is true.  */
776                         (*func)(info);
777
778                         /* Notify the sending CPU that the task is done.  */
779                         mb();
780                         if (wait) atomic_dec (&data->unfinished_count);
781                 }
782                 else if (which == IPI_CPU_STOP) {
783                         halt();
784                 }
785                 else {
786                         printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
787                                this_cpu, which);
788                 }
789           } while (ops);
790
791           mb(); /* Order data access and bit testing. */
792         }
793
794         cpu_data[this_cpu].ipi_count++;
795
796         if (hwrpb->txrdy)
797                 recv_secondary_console_msg();
798 }
799
800 void
801 smp_send_reschedule(int cpu)
802 {
803 #if DEBUG_IPI_MSG
804         if (cpu == hard_smp_processor_id())
805                 printk(KERN_WARNING
806                        "smp_send_reschedule: Sending IPI to self.\n");
807 #endif
808         send_ipi_message(1UL << cpu, IPI_RESCHEDULE);
809 }
810
811 void
812 smp_send_stop(void)
813 {
814         unsigned long to_whom = cpu_present_mask ^ (1UL << smp_processor_id());
815 #if DEBUG_IPI_MSG
816         if (hard_smp_processor_id() != boot_cpu_id)
817                 printk(KERN_WARNING "smp_send_stop: Not on boot cpu.\n");
818 #endif
819         send_ipi_message(to_whom, IPI_CPU_STOP);
820 }
821
822 /*
823  * Run a function on all other CPUs.
824  *  <func>      The function to run. This must be fast and non-blocking.
825  *  <info>      An arbitrary pointer to pass to the function.
826  *  <retry>     If true, keep retrying until ready.
827  *  <wait>      If true, wait until function has completed on other CPUs.
828  *  [RETURNS]   0 on success, else a negative status code.
829  *
830  * Does not return until remote CPUs are nearly ready to execute <func>
831  * or are or have executed.
832  */
833
834 int
835 smp_call_function_on_cpu (void (*func) (void *info), void *info, int retry,
836                           int wait, unsigned long to_whom)
837 {
838         struct smp_call_struct data;
839         long timeout;
840         int num_cpus_to_call;
841         long i,j;
842         
843         data.func = func;
844         data.info = info;
845         data.wait = wait;
846
847         to_whom &= ~(1L << smp_processor_id());
848         for (i = 0, j = 1, num_cpus_to_call = 0; i < NR_CPUS; ++i, j <<= 1)
849                 if (to_whom & j)
850                         num_cpus_to_call++;
851
852         atomic_set(&data.unstarted_count, num_cpus_to_call);
853         atomic_set(&data.unfinished_count, num_cpus_to_call);
854
855         /* Acquire the smp_call_function_data mutex.  */
856         if (pointer_lock(&smp_call_function_data, &data, retry))
857                 return -EBUSY;
858
859         /* Send a message to the requested CPUs.  */
860         send_ipi_message(to_whom, IPI_CALL_FUNC);
861
862         /* Wait for a minimal response.  */
863         timeout = jiffies + HZ;
864         while (atomic_read (&data.unstarted_count) > 0
865                && time_before (jiffies, timeout))
866                 barrier();
867
868         /* If there's no response yet, log a message but allow a longer
869          * timeout period -- if we get a response this time, log
870          * a message saying when we got it.. 
871          */
872         if (atomic_read(&data.unstarted_count) > 0) {
873                 long start_time = jiffies;
874                 printk(KERN_ERR "%s: initial timeout -- trying long wait\n",
875                        __FUNCTION__);
876                 timeout = jiffies + 30 * HZ;
877                 while (atomic_read(&data.unstarted_count) > 0
878                        && time_before(jiffies, timeout))
879                         barrier();
880                 if (atomic_read(&data.unstarted_count) <= 0) {
881                         long delta = jiffies - start_time;
882                         printk(KERN_ERR 
883                                "%s: response %ld.%ld seconds into long wait\n",
884                                __FUNCTION__, delta / HZ,
885                                (100 * (delta - ((delta / HZ) * HZ))) / HZ);
886                 }
887         }
888
889         /* We either got one or timed out -- clear the lock. */
890         mb();
891         smp_call_function_data = 0;
892
893         /* 
894          * If after both the initial and long timeout periods we still don't
895          * have a response, something is very wrong...
896          */
897         BUG_ON(atomic_read (&data.unstarted_count) > 0);
898
899         /* Wait for a complete response, if needed.  */
900         if (wait) {
901                 while (atomic_read (&data.unfinished_count) > 0)
902                         barrier();
903         }
904
905         return 0;
906 }
907
908 int
909 smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
910 {
911         return smp_call_function_on_cpu (func, info, retry, wait,
912                                          cpu_present_mask);
913 }
914
915 static void
916 ipi_imb(void *ignored)
917 {
918         imb();
919 }
920
921 void
922 smp_imb(void)
923 {
924         /* Must wait other processors to flush their icache before continue. */
925         if (smp_call_function(ipi_imb, NULL, 1, 1))
926                 printk(KERN_CRIT "smp_imb: timed out\n");
927
928         imb();
929 }
930
931 static void
932 ipi_flush_tlb_all(void *ignored)
933 {
934         tbia();
935 }
936
937 void
938 flush_tlb_all(void)
939 {
940         /* Although we don't have any data to pass, we do want to
941            synchronize with the other processors.  */
942         if (smp_call_function(ipi_flush_tlb_all, NULL, 1, 1)) {
943                 printk(KERN_CRIT "flush_tlb_all: timed out\n");
944         }
945
946         tbia();
947 }
948
949 #define asn_locked() (cpu_data[smp_processor_id()].asn_lock)
950
951 static void
952 ipi_flush_tlb_mm(void *x)
953 {
954         struct mm_struct *mm = (struct mm_struct *) x;
955         if (mm == current->active_mm && !asn_locked())
956                 flush_tlb_current(mm);
957         else
958                 flush_tlb_other(mm);
959 }
960
961 void
962 flush_tlb_mm(struct mm_struct *mm)
963 {
964         if (mm == current->active_mm) {
965                 flush_tlb_current(mm);
966                 if (atomic_read(&mm->mm_users) <= 1) {
967                         int i, cpu, this_cpu = smp_processor_id();
968                         for (i = 0; i < smp_num_cpus; i++) {
969                                 cpu = cpu_logical_map(i);
970                                 if (cpu == this_cpu)
971                                         continue;
972                                 if (mm->context[cpu])
973                                         mm->context[cpu] = 0;
974                         }
975                         return;
976                 }
977         }
978
979         if (smp_call_function(ipi_flush_tlb_mm, mm, 1, 1)) {
980                 printk(KERN_CRIT "flush_tlb_mm: timed out\n");
981         }
982 }
983
984 struct flush_tlb_page_struct {
985         struct vm_area_struct *vma;
986         struct mm_struct *mm;
987         unsigned long addr;
988 };
989
990 static void
991 ipi_flush_tlb_page(void *x)
992 {
993         struct flush_tlb_page_struct *data = (struct flush_tlb_page_struct *)x;
994         struct mm_struct * mm = data->mm;
995
996         if (mm == current->active_mm && !asn_locked())
997                 flush_tlb_current_page(mm, data->vma, data->addr);
998         else
999                 flush_tlb_other(mm);
1000 }
1001
1002 void
1003 flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
1004 {
1005         struct flush_tlb_page_struct data;
1006         struct mm_struct *mm = vma->vm_mm;
1007
1008         if (mm == current->active_mm) {
1009                 flush_tlb_current_page(mm, vma, addr);
1010                 if (atomic_read(&mm->mm_users) <= 1) {
1011                         int i, cpu, this_cpu = smp_processor_id();
1012                         for (i = 0; i < smp_num_cpus; i++) {
1013                                 cpu = cpu_logical_map(i);
1014                                 if (cpu == this_cpu)
1015                                         continue;
1016                                 if (mm->context[cpu])
1017                                         mm->context[cpu] = 0;
1018                         }
1019                         return;
1020                 }
1021         }
1022
1023         data.vma = vma;
1024         data.mm = mm;
1025         data.addr = addr;
1026
1027         if (smp_call_function(ipi_flush_tlb_page, &data, 1, 1)) {
1028                 printk(KERN_CRIT "flush_tlb_page: timed out\n");
1029         }
1030 }
1031
1032 void
1033 flush_tlb_range(struct mm_struct *mm, unsigned long start, unsigned long end)
1034 {
1035         /* On the Alpha we always flush the whole user tlb.  */
1036         flush_tlb_mm(mm);
1037 }
1038
1039 static void
1040 ipi_flush_icache_page(void *x)
1041 {
1042         struct mm_struct *mm = (struct mm_struct *) x;
1043         if (mm == current->active_mm && !asn_locked())
1044                 __load_new_mm_context(mm);
1045         else
1046                 flush_tlb_other(mm);
1047 }
1048
1049 void
1050 flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
1051                         unsigned long addr, int len)
1052 {
1053         struct mm_struct *mm = vma->vm_mm;
1054
1055         if ((vma->vm_flags & VM_EXEC) == 0)
1056                 return;
1057
1058         if (mm == current->active_mm) {
1059                 __load_new_mm_context(mm);
1060                 if (atomic_read(&mm->mm_users) <= 1) {
1061                         int i, cpu, this_cpu = smp_processor_id();
1062                         for (i = 0; i < smp_num_cpus; i++) {
1063                                 cpu = cpu_logical_map(i);
1064                                 if (cpu == this_cpu)
1065                                         continue;
1066                                 if (mm->context[cpu])
1067                                         mm->context[cpu] = 0;
1068                         }
1069                         return;
1070                 }
1071         }
1072
1073         if (smp_call_function(ipi_flush_icache_page, mm, 1, 1)) {
1074                 printk(KERN_CRIT "flush_icache_page: timed out\n");
1075         }
1076 }
1077 \f
1078 #ifdef CONFIG_DEBUG_SPINLOCK
1079 void
1080 spin_unlock(spinlock_t * lock)
1081 {
1082         mb();
1083         lock->lock = 0;
1084
1085         lock->on_cpu = -1;
1086         lock->previous = NULL;
1087         lock->task = NULL;
1088         lock->base_file = "none";
1089         lock->line_no = 0;
1090 }
1091
1092 void
1093 debug_spin_lock(spinlock_t * lock, const char *base_file, int line_no)
1094 {
1095         long tmp;
1096         long stuck;
1097         void *inline_pc = __builtin_return_address(0);
1098         unsigned long started = jiffies;
1099         int printed = 0;
1100         int cpu = smp_processor_id();
1101
1102         stuck = 1L << 30;
1103  try_again:
1104
1105         /* Use sub-sections to put the actual loop at the end
1106            of this object file's text section so as to perfect
1107            branch prediction.  */
1108         __asm__ __volatile__(
1109         "1:     ldl_l   %0,%1\n"
1110         "       subq    %2,1,%2\n"
1111         "       blbs    %0,2f\n"
1112         "       or      %0,1,%0\n"
1113         "       stl_c   %0,%1\n"
1114         "       beq     %0,3f\n"
1115         "4:     mb\n"
1116         ".subsection 2\n"
1117         "2:     ldl     %0,%1\n"
1118         "       subq    %2,1,%2\n"
1119         "3:     blt     %2,4b\n"
1120         "       blbs    %0,2b\n"
1121         "       br      1b\n"
1122         ".previous"
1123         : "=r" (tmp), "=m" (lock->lock), "=r" (stuck)
1124         : "1" (lock->lock), "2" (stuck) : "memory");
1125
1126         if (stuck < 0) {
1127                 printk(KERN_WARNING
1128                        "%s:%d spinlock stuck in %s at %p(%d)"
1129                        " owner %s at %p(%d) %s:%d\n",
1130                        base_file, line_no,
1131                        current->comm, inline_pc, cpu,
1132                        lock->task->comm, lock->previous,
1133                        lock->on_cpu, lock->base_file, lock->line_no);
1134                 stuck = 1L << 36;
1135                 printed = 1;
1136                 goto try_again;
1137         }
1138
1139         /* Exiting.  Got the lock.  */
1140         lock->on_cpu = cpu;
1141         lock->previous = inline_pc;
1142         lock->task = current;
1143         lock->base_file = base_file;
1144         lock->line_no = line_no;
1145
1146         if (printed) {
1147                 printk(KERN_WARNING
1148                        "%s:%d spinlock grabbed in %s at %p(%d) %ld ticks\n",
1149                        base_file, line_no, current->comm, inline_pc,
1150                        cpu, jiffies - started);
1151         }
1152 }
1153
1154 int
1155 debug_spin_trylock(spinlock_t * lock, const char *base_file, int line_no)
1156 {
1157         int ret;
1158         if ((ret = !test_and_set_bit(0, lock))) {
1159                 lock->on_cpu = smp_processor_id();
1160                 lock->previous = __builtin_return_address(0);
1161                 lock->task = current;
1162         } else {
1163                 lock->base_file = base_file;
1164                 lock->line_no = line_no;
1165         }
1166         return ret;
1167 }
1168 #endif /* CONFIG_DEBUG_SPINLOCK */
1169 \f
1170 #ifdef CONFIG_DEBUG_RWLOCK
1171 void write_lock(rwlock_t * lock)
1172 {
1173         long regx, regy;
1174         int stuck_lock, stuck_reader;
1175         void *inline_pc = __builtin_return_address(0);
1176
1177  try_again:
1178
1179         stuck_lock = 1<<30;
1180         stuck_reader = 1<<30;
1181
1182         __asm__ __volatile__(
1183         "1:     ldl_l   %1,%0\n"
1184         "       blbs    %1,6f\n"
1185         "       blt     %1,8f\n"
1186         "       mov     1,%1\n"
1187         "       stl_c   %1,%0\n"
1188         "       beq     %1,6f\n"
1189         "4:     mb\n"
1190         ".subsection 2\n"
1191         "6:     blt     %3,4b   # debug\n"
1192         "       subl    %3,1,%3 # debug\n"
1193         "       ldl     %1,%0\n"
1194         "       blbs    %1,6b\n"
1195         "8:     blt     %4,4b   # debug\n"
1196         "       subl    %4,1,%4 # debug\n"
1197         "       ldl     %1,%0\n"
1198         "       blt     %1,8b\n"
1199         "       br      1b\n"
1200         ".previous"
1201         : "=m" (*(volatile int *)lock), "=&r" (regx), "=&r" (regy),
1202           "=&r" (stuck_lock), "=&r" (stuck_reader)
1203         : "0" (*(volatile int *)lock), "3" (stuck_lock), "4" (stuck_reader) : "memory");
1204
1205         if (stuck_lock < 0) {
1206                 printk(KERN_WARNING "write_lock stuck at %p\n", inline_pc);
1207                 goto try_again;
1208         }
1209         if (stuck_reader < 0) {
1210                 printk(KERN_WARNING "write_lock stuck on readers at %p\n",
1211                        inline_pc);
1212                 goto try_again;
1213         }
1214 }
1215
1216 void read_lock(rwlock_t * lock)
1217 {
1218         long regx;
1219         int stuck_lock;
1220         void *inline_pc = __builtin_return_address(0);
1221
1222  try_again:
1223
1224         stuck_lock = 1<<30;
1225
1226         __asm__ __volatile__(
1227         "1:     ldl_l   %1,%0;"
1228         "       blbs    %1,6f;"
1229         "       subl    %1,2,%1;"
1230         "       stl_c   %1,%0;"
1231         "       beq     %1,6f;"
1232         "4:     mb\n"
1233         ".subsection 2\n"
1234         "6:     ldl     %1,%0;"
1235         "       blt     %2,4b   # debug\n"
1236         "       subl    %2,1,%2 # debug\n"
1237         "       blbs    %1,6b;"
1238         "       br      1b\n"
1239         ".previous"
1240         : "=m" (*(volatile int *)lock), "=&r" (regx), "=&r" (stuck_lock)
1241         : "0" (*(volatile int *)lock), "2" (stuck_lock) : "memory");
1242
1243         if (stuck_lock < 0) {
1244                 printk(KERN_WARNING "read_lock stuck at %p\n", inline_pc);
1245                 goto try_again;
1246         }
1247 }
1248 #endif /* CONFIG_DEBUG_RWLOCK */