44238ab2fc99ff803836e263f42e7a2860fd3996
[powerpc.git] / arch / mips / kernel / smtc.c
1 /* Copyright (C) 2004 Mips Technologies, Inc */
2
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/cpumask.h>
6 #include <linux/interrupt.h>
7
8 #include <asm/cpu.h>
9 #include <asm/processor.h>
10 #include <asm/atomic.h>
11 #include <asm/system.h>
12 #include <asm/hardirq.h>
13 #include <asm/hazards.h>
14 #include <asm/mmu_context.h>
15 #include <asm/smp.h>
16 #include <asm/mipsregs.h>
17 #include <asm/cacheflush.h>
18 #include <asm/time.h>
19 #include <asm/addrspace.h>
20 #include <asm/smtc.h>
21 #include <asm/smtc_ipi.h>
22 #include <asm/smtc_proc.h>
23
24 /*
25  * This file should be built into the kernel only if CONFIG_MIPS_MT_SMTC is set.
26  */
27
28 /*
29  * MIPSCPU_INT_BASE is identically defined in both
30  * asm-mips/mips-boards/maltaint.h and asm-mips/mips-boards/simint.h,
31  * but as yet there's no properly organized include structure that
32  * will ensure that the right *int.h file will be included for a
33  * given platform build.
34  */
35
36 #define MIPSCPU_INT_BASE        16
37
38 #define MIPS_CPU_IPI_IRQ        1
39
40 #define LOCK_MT_PRA() \
41         local_irq_save(flags); \
42         mtflags = dmt()
43
44 #define UNLOCK_MT_PRA() \
45         emt(mtflags); \
46         local_irq_restore(flags)
47
48 #define LOCK_CORE_PRA() \
49         local_irq_save(flags); \
50         mtflags = dvpe()
51
52 #define UNLOCK_CORE_PRA() \
53         evpe(mtflags); \
54         local_irq_restore(flags)
55
56 /*
57  * Data structures purely associated with SMTC parallelism
58  */
59
60
61 /*
62  * Table for tracking ASIDs whose lifetime is prolonged.
63  */
64
65 asiduse smtc_live_asid[MAX_SMTC_TLBS][MAX_SMTC_ASIDS];
66
67 /*
68  * Clock interrupt "latch" buffers, per "CPU"
69  */
70
71 unsigned int ipi_timer_latch[NR_CPUS];
72
73 /*
74  * Number of InterProcessor Interupt (IPI) message buffers to allocate
75  */
76
77 #define IPIBUF_PER_CPU 4
78
79 struct smtc_ipi_q IPIQ[NR_CPUS];
80 struct smtc_ipi_q freeIPIq;
81
82
83 /* Forward declarations */
84
85 void ipi_decode(struct smtc_ipi *);
86 void post_direct_ipi(int cpu, struct smtc_ipi *pipi);
87 void setup_cross_vpe_interrupts(void);
88 void init_smtc_stats(void);
89
90 /* Global SMTC Status */
91
92 unsigned int smtc_status = 0;
93
94 /* Boot command line configuration overrides */
95
96 static int vpelimit = 0;
97 static int tclimit = 0;
98 static int ipibuffers = 0;
99 static int nostlb = 0;
100 static int asidmask = 0;
101 unsigned long smtc_asid_mask = 0xff;
102
103 static int __init maxvpes(char *str)
104 {
105         get_option(&str, &vpelimit);
106         return 1;
107 }
108
109 static int __init maxtcs(char *str)
110 {
111         get_option(&str, &tclimit);
112         return 1;
113 }
114
115 static int __init ipibufs(char *str)
116 {
117         get_option(&str, &ipibuffers);
118         return 1;
119 }
120
121 static int __init stlb_disable(char *s)
122 {
123         nostlb = 1;
124         return 1;
125 }
126
127 static int __init asidmask_set(char *str)
128 {
129         get_option(&str, &asidmask);
130         switch (asidmask) {
131         case 0x1:
132         case 0x3:
133         case 0x7:
134         case 0xf:
135         case 0x1f:
136         case 0x3f:
137         case 0x7f:
138         case 0xff:
139                 smtc_asid_mask = (unsigned long)asidmask;
140                 break;
141         default:
142                 printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask);
143         }
144         return 1;
145 }
146
147 __setup("maxvpes=", maxvpes);
148 __setup("maxtcs=", maxtcs);
149 __setup("ipibufs=", ipibufs);
150 __setup("nostlb", stlb_disable);
151 __setup("asidmask=", asidmask_set);
152
153 /* Enable additional debug checks before going into CPU idle loop */
154 #define SMTC_IDLE_HOOK_DEBUG
155
156 #ifdef SMTC_IDLE_HOOK_DEBUG
157
158 static int hang_trig = 0;
159
160 static int __init hangtrig_enable(char *s)
161 {
162         hang_trig = 1;
163         return 1;
164 }
165
166
167 __setup("hangtrig", hangtrig_enable);
168
169 #define DEFAULT_BLOCKED_IPI_LIMIT 32
170
171 static int timerq_limit = DEFAULT_BLOCKED_IPI_LIMIT;
172
173 static int __init tintq(char *str)
174 {
175         get_option(&str, &timerq_limit);
176         return 1;
177 }
178
179 __setup("tintq=", tintq);
180
181 int imstuckcount[2][8];
182 /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
183 int vpemask[2][8] = {{0,1,1,0,0,0,0,1},{0,1,0,0,0,0,0,1}};
184 int tcnoprog[NR_CPUS];
185 static atomic_t idle_hook_initialized = {0};
186 static int clock_hang_reported[NR_CPUS];
187
188 #endif /* SMTC_IDLE_HOOK_DEBUG */
189
190 /* Initialize shared TLB - the should probably migrate to smtc_setup_cpus() */
191
192 void __init sanitize_tlb_entries(void)
193 {
194         printk("Deprecated sanitize_tlb_entries() invoked\n");
195 }
196
197
198 /*
199  * Configure shared TLB - VPC configuration bit must be set by caller
200  */
201
202 void smtc_configure_tlb(void)
203 {
204         int i,tlbsiz,vpes;
205         unsigned long mvpconf0;
206         unsigned long config1val;
207
208         /* Set up ASID preservation table */
209         for (vpes=0; vpes<MAX_SMTC_TLBS; vpes++) {
210             for(i = 0; i < MAX_SMTC_ASIDS; i++) {
211                 smtc_live_asid[vpes][i] = 0;
212             }
213         }
214         mvpconf0 = read_c0_mvpconf0();
215
216         if ((vpes = ((mvpconf0 & MVPCONF0_PVPE)
217                         >> MVPCONF0_PVPE_SHIFT) + 1) > 1) {
218             /* If we have multiple VPEs, try to share the TLB */
219             if ((mvpconf0 & MVPCONF0_TLBS) && !nostlb) {
220                 /*
221                  * If TLB sizing is programmable, shared TLB
222                  * size is the total available complement.
223                  * Otherwise, we have to take the sum of all
224                  * static VPE TLB entries.
225                  */
226                 if ((tlbsiz = ((mvpconf0 & MVPCONF0_PTLBE)
227                                 >> MVPCONF0_PTLBE_SHIFT)) == 0) {
228                     /*
229                      * If there's more than one VPE, there had better
230                      * be more than one TC, because we need one to bind
231                      * to each VPE in turn to be able to read
232                      * its configuration state!
233                      */
234                     settc(1);
235                     /* Stop the TC from doing anything foolish */
236                     write_tc_c0_tchalt(TCHALT_H);
237                     mips_ihb();
238                     /* No need to un-Halt - that happens later anyway */
239                     for (i=0; i < vpes; i++) {
240                         write_tc_c0_tcbind(i);
241                         /*
242                          * To be 100% sure we're really getting the right
243                          * information, we exit the configuration state
244                          * and do an IHB after each rebinding.
245                          */
246                         write_c0_mvpcontrol(
247                                 read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
248                         mips_ihb();
249                         /*
250                          * Only count if the MMU Type indicated is TLB
251                          */
252                         if (((read_vpe_c0_config() & MIPS_CONF_MT) >> 7) == 1) {
253                                 config1val = read_vpe_c0_config1();
254                                 tlbsiz += ((config1val >> 25) & 0x3f) + 1;
255                         }
256
257                         /* Put core back in configuration state */
258                         write_c0_mvpcontrol(
259                                 read_c0_mvpcontrol() | MVPCONTROL_VPC );
260                         mips_ihb();
261                     }
262                 }
263                 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB);
264                 ehb();
265
266                 /*
267                  * Setup kernel data structures to use software total,
268                  * rather than read the per-VPE Config1 value. The values
269                  * for "CPU 0" gets copied to all the other CPUs as part
270                  * of their initialization in smtc_cpu_setup().
271                  */
272
273                 tlbsiz = tlbsiz & 0x3f; /* MIPS32 limits TLB indices to 64 */
274                 cpu_data[0].tlbsize = tlbsiz;
275                 smtc_status |= SMTC_TLB_SHARED;
276
277                 printk("TLB of %d entry pairs shared by %d VPEs\n",
278                         tlbsiz, vpes);
279             } else {
280                 printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
281             }
282         }
283 }
284
285
286 /*
287  * Incrementally build the CPU map out of constituent MIPS MT cores,
288  * using the specified available VPEs and TCs.  Plaform code needs
289  * to ensure that each MIPS MT core invokes this routine on reset,
290  * one at a time(!).
291  *
292  * This version of the build_cpu_map and prepare_cpus routines assumes
293  * that *all* TCs of a MIPS MT core will be used for Linux, and that
294  * they will be spread across *all* available VPEs (to minimise the
295  * loss of efficiency due to exception service serialization).
296  * An improved version would pick up configuration information and
297  * possibly leave some TCs/VPEs as "slave" processors.
298  *
299  * Use c0_MVPConf0 to find out how many TCs are available, setting up
300  * phys_cpu_present_map and the logical/physical mappings.
301  */
302
303 int __init mipsmt_build_cpu_map(int start_cpu_slot)
304 {
305         int i, ntcs;
306
307         /*
308          * The CPU map isn't actually used for anything at this point,
309          * so it's not clear what else we should do apart from set
310          * everything up so that "logical" = "physical".
311          */
312         ntcs = ((read_c0_mvpconf0() & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
313         for (i=start_cpu_slot; i<NR_CPUS && i<ntcs; i++) {
314                 cpu_set(i, phys_cpu_present_map);
315                 __cpu_number_map[i] = i;
316                 __cpu_logical_map[i] = i;
317         }
318         /* Initialize map of CPUs with FPUs */
319         cpus_clear(mt_fpu_cpumask);
320
321         /* One of those TC's is the one booting, and not a secondary... */
322         printk("%i available secondary CPU TC(s)\n", i - 1);
323
324         return i;
325 }
326
327 /*
328  * Common setup before any secondaries are started
329  * Make sure all CPU's are in a sensible state before we boot any of the
330  * secondaries.
331  *
332  * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
333  * as possible across the available VPEs.
334  */
335
336 static void smtc_tc_setup(int vpe, int tc, int cpu)
337 {
338         settc(tc);
339         write_tc_c0_tchalt(TCHALT_H);
340         mips_ihb();
341         write_tc_c0_tcstatus((read_tc_c0_tcstatus()
342                         & ~(TCSTATUS_TKSU | TCSTATUS_DA | TCSTATUS_IXMT))
343                         | TCSTATUS_A);
344         write_tc_c0_tccontext(0);
345         /* Bind tc to vpe */
346         write_tc_c0_tcbind(vpe);
347         /* In general, all TCs should have the same cpu_data indications */
348         memcpy(&cpu_data[cpu], &cpu_data[0], sizeof(struct cpuinfo_mips));
349         /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
350         if (cpu_data[0].cputype == CPU_34K)
351                 cpu_data[cpu].options &= ~MIPS_CPU_FPU;
352         cpu_data[cpu].vpe_id = vpe;
353         cpu_data[cpu].tc_id = tc;
354 }
355
356
357 void mipsmt_prepare_cpus(void)
358 {
359         int i, vpe, tc, ntc, nvpe, tcpervpe, slop, cpu;
360         unsigned long flags;
361         unsigned long val;
362         int nipi;
363         struct smtc_ipi *pipi;
364
365         /* disable interrupts so we can disable MT */
366         local_irq_save(flags);
367         /* disable MT so we can configure */
368         dvpe();
369         dmt();
370
371         spin_lock_init(&freeIPIq.lock);
372
373         /*
374          * We probably don't have as many VPEs as we do SMP "CPUs",
375          * but it's possible - and in any case we'll never use more!
376          */
377         for (i=0; i<NR_CPUS; i++) {
378                 IPIQ[i].head = IPIQ[i].tail = NULL;
379                 spin_lock_init(&IPIQ[i].lock);
380                 IPIQ[i].depth = 0;
381                 ipi_timer_latch[i] = 0;
382         }
383
384         /* cpu_data index starts at zero */
385         cpu = 0;
386         cpu_data[cpu].vpe_id = 0;
387         cpu_data[cpu].tc_id = 0;
388         cpu++;
389
390         /* Report on boot-time options */
391         mips_mt_set_cpuoptions ();
392         if (vpelimit > 0)
393                 printk("Limit of %d VPEs set\n", vpelimit);
394         if (tclimit > 0)
395                 printk("Limit of %d TCs set\n", tclimit);
396         if (nostlb) {
397                 printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
398         }
399         if (asidmask)
400                 printk("ASID mask value override to 0x%x\n", asidmask);
401
402         /* Temporary */
403 #ifdef SMTC_IDLE_HOOK_DEBUG
404         if (hang_trig)
405                 printk("Logic Analyser Trigger on suspected TC hang\n");
406 #endif /* SMTC_IDLE_HOOK_DEBUG */
407
408         /* Put MVPE's into 'configuration state' */
409         write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC );
410
411         val = read_c0_mvpconf0();
412         nvpe = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
413         if (vpelimit > 0 && nvpe > vpelimit)
414                 nvpe = vpelimit;
415         ntc = ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
416         if (ntc > NR_CPUS)
417                 ntc = NR_CPUS;
418         if (tclimit > 0 && ntc > tclimit)
419                 ntc = tclimit;
420         tcpervpe = ntc / nvpe;
421         slop = ntc % nvpe;      /* Residual TCs, < NVPE */
422
423         /* Set up shared TLB */
424         smtc_configure_tlb();
425
426         for (tc = 0, vpe = 0 ; (vpe < nvpe) && (tc < ntc) ; vpe++) {
427                 /*
428                  * Set the MVP bits.
429                  */
430                 settc(tc);
431                 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP);
432                 if (vpe != 0)
433                         printk(", ");
434                 printk("VPE %d: TC", vpe);
435                 for (i = 0; i < tcpervpe; i++) {
436                         /*
437                          * TC 0 is bound to VPE 0 at reset,
438                          * and is presumably executing this
439                          * code.  Leave it alone!
440                          */
441                         if (tc != 0) {
442                                 smtc_tc_setup(vpe,tc, cpu);
443                                 cpu++;
444                         }
445                         printk(" %d", tc);
446                         tc++;
447                 }
448                 if (slop) {
449                         if (tc != 0) {
450                                 smtc_tc_setup(vpe,tc, cpu);
451                                 cpu++;
452                         }
453                         printk(" %d", tc);
454                         tc++;
455                         slop--;
456                 }
457                 if (vpe != 0) {
458                         /*
459                          * Clear any stale software interrupts from VPE's Cause
460                          */
461                         write_vpe_c0_cause(0);
462
463                         /*
464                          * Clear ERL/EXL of VPEs other than 0
465                          * and set restricted interrupt enable/mask.
466                          */
467                         write_vpe_c0_status((read_vpe_c0_status()
468                                 & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
469                                 | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
470                                 | ST0_IE));
471                         /*
472                          * set config to be the same as vpe0,
473                          *  particularly kseg0 coherency alg
474                          */
475                         write_vpe_c0_config(read_c0_config());
476                         /* Clear any pending timer interrupt */
477                         write_vpe_c0_compare(0);
478                         /* Propagate Config7 */
479                         write_vpe_c0_config7(read_c0_config7());
480                         write_vpe_c0_count(read_c0_count());
481                 }
482                 /* enable multi-threading within VPE */
483                 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
484                 /* enable the VPE */
485                 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
486         }
487
488         /*
489          * Pull any physically present but unused TCs out of circulation.
490          */
491         while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
492                 cpu_clear(tc, phys_cpu_present_map);
493                 cpu_clear(tc, cpu_present_map);
494                 tc++;
495         }
496
497         /* release config state */
498         write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
499
500         printk("\n");
501
502         /* Set up coprocessor affinity CPU mask(s) */
503
504         for (tc = 0; tc < ntc; tc++) {
505                 if (cpu_data[tc].options & MIPS_CPU_FPU)
506                         cpu_set(tc, mt_fpu_cpumask);
507         }
508
509         /* set up ipi interrupts... */
510
511         /* If we have multiple VPEs running, set up the cross-VPE interrupt */
512
513         if (nvpe > 1)
514                 setup_cross_vpe_interrupts();
515
516         /* Set up queue of free IPI "messages". */
517         nipi = NR_CPUS * IPIBUF_PER_CPU;
518         if (ipibuffers > 0)
519                 nipi = ipibuffers;
520
521         pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
522         if (pipi == NULL)
523                 panic("kmalloc of IPI message buffers failed\n");
524         else
525                 printk("IPI buffer pool of %d buffers\n", nipi);
526         for (i = 0; i < nipi; i++) {
527                 smtc_ipi_nq(&freeIPIq, pipi);
528                 pipi++;
529         }
530
531         /* Arm multithreading and enable other VPEs - but all TCs are Halted */
532         emt(EMT_ENABLE);
533         evpe(EVPE_ENABLE);
534         local_irq_restore(flags);
535         /* Initialize SMTC /proc statistics/diagnostics */
536         init_smtc_stats();
537 }
538
539
540 /*
541  * Setup the PC, SP, and GP of a secondary processor and start it
542  * running!
543  * smp_bootstrap is the place to resume from
544  * __KSTK_TOS(idle) is apparently the stack pointer
545  * (unsigned long)idle->thread_info the gp
546  *
547  */
548 void smtc_boot_secondary(int cpu, struct task_struct *idle)
549 {
550         extern u32 kernelsp[NR_CPUS];
551         long flags;
552         int mtflags;
553
554         LOCK_MT_PRA();
555         if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
556                 dvpe();
557         }
558         settc(cpu_data[cpu].tc_id);
559
560         /* pc */
561         write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
562
563         /* stack pointer */
564         kernelsp[cpu] = __KSTK_TOS(idle);
565         write_tc_gpr_sp(__KSTK_TOS(idle));
566
567         /* global pointer */
568         write_tc_gpr_gp((unsigned long)idle->thread_info);
569
570         smtc_status |= SMTC_MTC_ACTIVE;
571         write_tc_c0_tchalt(0);
572         if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
573                 evpe(EVPE_ENABLE);
574         }
575         UNLOCK_MT_PRA();
576 }
577
578 void smtc_init_secondary(void)
579 {
580         /*
581          * Start timer on secondary VPEs if necessary.
582          * plat_timer_setup has already have been invoked by init/main
583          * on "boot" TC.  Like per_cpu_trap_init() hack, this assumes that
584          * SMTC init code assigns TCs consdecutively and in ascending order
585          * to across available VPEs.
586          */
587         if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
588             ((read_c0_tcbind() & TCBIND_CURVPE)
589             != cpu_data[smp_processor_id() - 1].vpe_id)){
590                 write_c0_compare (read_c0_count() + mips_hpt_frequency/HZ);
591         }
592
593         local_irq_enable();
594 }
595
596 void smtc_smp_finish(void)
597 {
598         printk("TC %d going on-line as CPU %d\n",
599                 cpu_data[smp_processor_id()].tc_id, smp_processor_id());
600 }
601
602 void smtc_cpus_done(void)
603 {
604 }
605
606 /*
607  * Support for SMTC-optimized driver IRQ registration
608  */
609
610 /*
611  * SMTC Kernel needs to manipulate low-level CPU interrupt mask
612  * in do_IRQ. These are passed in setup_irq_smtc() and stored
613  * in this table.
614  */
615
616 int setup_irq_smtc(unsigned int irq, struct irqaction * new,
617                         unsigned long hwmask)
618 {
619         irq_hwmask[irq] = hwmask;
620
621         return setup_irq(irq, new);
622 }
623
624 /*
625  * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
626  * Within a VPE one TC can interrupt another by different approaches.
627  * The easiest to get right would probably be to make all TCs except
628  * the target IXMT and set a software interrupt, but an IXMT-based
629  * scheme requires that a handler must run before a new IPI could
630  * be sent, which would break the "broadcast" loops in MIPS MT.
631  * A more gonzo approach within a VPE is to halt the TC, extract
632  * its Restart, Status, and a couple of GPRs, and program the Restart
633  * address to emulate an interrupt.
634  *
635  * Within a VPE, one can be confident that the target TC isn't in
636  * a critical EXL state when halted, since the write to the Halt
637  * register could not have issued on the writing thread if the
638  * halting thread had EXL set. So k0 and k1 of the target TC
639  * can be used by the injection code.  Across VPEs, one can't
640  * be certain that the target TC isn't in a critical exception
641  * state. So we try a two-step process of sending a software
642  * interrupt to the target VPE, which either handles the event
643  * itself (if it was the target) or injects the event within
644  * the VPE.
645  */
646
647 void smtc_ipi_qdump(void)
648 {
649         int i;
650
651         for (i = 0; i < NR_CPUS ;i++) {
652                 printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
653                         i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
654                         IPIQ[i].depth);
655         }
656 }
657
658 /*
659  * The standard atomic.h primitives don't quite do what we want
660  * here: We need an atomic add-and-return-previous-value (which
661  * could be done with atomic_add_return and a decrement) and an
662  * atomic set/zero-and-return-previous-value (which can't really
663  * be done with the atomic.h primitives). And since this is
664  * MIPS MT, we can assume that we have LL/SC.
665  */
666 static __inline__ int atomic_postincrement(unsigned int *pv)
667 {
668         unsigned long result;
669
670         unsigned long temp;
671
672         __asm__ __volatile__(
673         "1:     ll      %0, %2                                  \n"
674         "       addu    %1, %0, 1                               \n"
675         "       sc      %1, %2                                  \n"
676         "       beqz    %1, 1b                                  \n"
677         "       sync                                            \n"
678         : "=&r" (result), "=&r" (temp), "=m" (*pv)
679         : "m" (*pv)
680         : "memory");
681
682         return result;
683 }
684
685 /* No longer used in IPI dispatch, but retained for future recycling */
686
687 static __inline__ int atomic_postclear(unsigned int *pv)
688 {
689         unsigned long result;
690
691         unsigned long temp;
692
693         __asm__ __volatile__(
694         "1:     ll      %0, %2                                  \n"
695         "       or      %1, $0, $0                              \n"
696         "       sc      %1, %2                                  \n"
697         "       beqz    %1, 1b                                  \n"
698         "       sync                                            \n"
699         : "=&r" (result), "=&r" (temp), "=m" (*pv)
700         : "m" (*pv)
701         : "memory");
702
703         return result;
704 }
705
706
707 void smtc_send_ipi(int cpu, int type, unsigned int action)
708 {
709         int tcstatus;
710         struct smtc_ipi *pipi;
711         long flags;
712         int mtflags;
713
714         if (cpu == smp_processor_id()) {
715                 printk("Cannot Send IPI to self!\n");
716                 return;
717         }
718         /* Set up a descriptor, to be delivered either promptly or queued */
719         pipi = smtc_ipi_dq(&freeIPIq);
720         if (pipi == NULL) {
721                 bust_spinlocks(1);
722                 mips_mt_regdump(dvpe());
723                 panic("IPI Msg. Buffers Depleted\n");
724         }
725         pipi->type = type;
726         pipi->arg = (void *)action;
727         pipi->dest = cpu;
728         if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
729                 /* If not on same VPE, enqueue and send cross-VPE interupt */
730                 smtc_ipi_nq(&IPIQ[cpu], pipi);
731                 LOCK_CORE_PRA();
732                 settc(cpu_data[cpu].tc_id);
733                 write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
734                 UNLOCK_CORE_PRA();
735         } else {
736                 /*
737                  * Not sufficient to do a LOCK_MT_PRA (dmt) here,
738                  * since ASID shootdown on the other VPE may
739                  * collide with this operation.
740                  */
741                 LOCK_CORE_PRA();
742                 settc(cpu_data[cpu].tc_id);
743                 /* Halt the targeted TC */
744                 write_tc_c0_tchalt(TCHALT_H);
745                 mips_ihb();
746
747                 /*
748                  * Inspect TCStatus - if IXMT is set, we have to queue
749                  * a message. Otherwise, we set up the "interrupt"
750                  * of the other TC
751                  */
752                 tcstatus = read_tc_c0_tcstatus();
753
754                 if ((tcstatus & TCSTATUS_IXMT) != 0) {
755                         /*
756                          * Spin-waiting here can deadlock,
757                          * so we queue the message for the target TC.
758                          */
759                         write_tc_c0_tchalt(0);
760                         UNLOCK_CORE_PRA();
761                         /* Try to reduce redundant timer interrupt messages */
762                         if (type == SMTC_CLOCK_TICK) {
763                             if (atomic_postincrement(&ipi_timer_latch[cpu])!=0){
764                                 smtc_ipi_nq(&freeIPIq, pipi);
765                                 return;
766                             }
767                         }
768                         smtc_ipi_nq(&IPIQ[cpu], pipi);
769                 } else {
770                         post_direct_ipi(cpu, pipi);
771                         write_tc_c0_tchalt(0);
772                         UNLOCK_CORE_PRA();
773                 }
774         }
775 }
776
777 /*
778  * Send IPI message to Halted TC, TargTC/TargVPE already having been set
779  */
780 void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
781 {
782         struct pt_regs *kstack;
783         unsigned long tcstatus;
784         unsigned long tcrestart;
785         extern u32 kernelsp[NR_CPUS];
786         extern void __smtc_ipi_vector(void);
787
788         /* Extract Status, EPC from halted TC */
789         tcstatus = read_tc_c0_tcstatus();
790         tcrestart = read_tc_c0_tcrestart();
791         /* If TCRestart indicates a WAIT instruction, advance the PC */
792         if ((tcrestart & 0x80000000)
793             && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
794                 tcrestart += 4;
795         }
796         /*
797          * Save on TC's future kernel stack
798          *
799          * CU bit of Status is indicator that TC was
800          * already running on a kernel stack...
801          */
802         if (tcstatus & ST0_CU0)  {
803                 /* Note that this "- 1" is pointer arithmetic */
804                 kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
805         } else {
806                 kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
807         }
808
809         kstack->cp0_epc = (long)tcrestart;
810         /* Save TCStatus */
811         kstack->cp0_tcstatus = tcstatus;
812         /* Pass token of operation to be performed kernel stack pad area */
813         kstack->pad0[4] = (unsigned long)pipi;
814         /* Pass address of function to be called likewise */
815         kstack->pad0[5] = (unsigned long)&ipi_decode;
816         /* Set interrupt exempt and kernel mode */
817         tcstatus |= TCSTATUS_IXMT;
818         tcstatus &= ~TCSTATUS_TKSU;
819         write_tc_c0_tcstatus(tcstatus);
820         ehb();
821         /* Set TC Restart address to be SMTC IPI vector */
822         write_tc_c0_tcrestart(__smtc_ipi_vector);
823 }
824
825 static void ipi_resched_interrupt(void)
826 {
827         /* Return from interrupt should be enough to cause scheduler check */
828 }
829
830
831 static void ipi_call_interrupt(void)
832 {
833         /* Invoke generic function invocation code in smp.c */
834         smp_call_function_interrupt();
835 }
836
837 void ipi_decode(struct smtc_ipi *pipi)
838 {
839         void *arg_copy = pipi->arg;
840         int type_copy = pipi->type;
841         int dest_copy = pipi->dest;
842
843         smtc_ipi_nq(&freeIPIq, pipi);
844         switch (type_copy) {
845         case SMTC_CLOCK_TICK:
846                 /* Invoke Clock "Interrupt" */
847                 ipi_timer_latch[dest_copy] = 0;
848 #ifdef SMTC_IDLE_HOOK_DEBUG
849                 clock_hang_reported[dest_copy] = 0;
850 #endif /* SMTC_IDLE_HOOK_DEBUG */
851                 local_timer_interrupt(0, NULL);
852                 break;
853         case LINUX_SMP_IPI:
854                 switch ((int)arg_copy) {
855                 case SMP_RESCHEDULE_YOURSELF:
856                         ipi_resched_interrupt();
857                         break;
858                 case SMP_CALL_FUNCTION:
859                         ipi_call_interrupt();
860                         break;
861                 default:
862                         printk("Impossible SMTC IPI Argument 0x%x\n",
863                                 (int)arg_copy);
864                         break;
865                 }
866                 break;
867         default:
868                 printk("Impossible SMTC IPI Type 0x%x\n", type_copy);
869                 break;
870         }
871 }
872
873 void deferred_smtc_ipi(void)
874 {
875         struct smtc_ipi *pipi;
876         unsigned long flags;
877 /* DEBUG */
878         int q = smp_processor_id();
879
880         /*
881          * Test is not atomic, but much faster than a dequeue,
882          * and the vast majority of invocations will have a null queue.
883          */
884         if (IPIQ[q].head != NULL) {
885                 while((pipi = smtc_ipi_dq(&IPIQ[q])) != NULL) {
886                         /* ipi_decode() should be called with interrupts off */
887                         local_irq_save(flags);
888                         ipi_decode(pipi);
889                         local_irq_restore(flags);
890                 }
891         }
892 }
893
894 /*
895  * Send clock tick to all TCs except the one executing the funtion
896  */
897
898 void smtc_timer_broadcast(int vpe)
899 {
900         int cpu;
901         int myTC = cpu_data[smp_processor_id()].tc_id;
902         int myVPE = cpu_data[smp_processor_id()].vpe_id;
903
904         smtc_cpu_stats[smp_processor_id()].timerints++;
905
906         for_each_online_cpu(cpu) {
907                 if (cpu_data[cpu].vpe_id == myVPE &&
908                     cpu_data[cpu].tc_id != myTC)
909                         smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);
910         }
911 }
912
913 /*
914  * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
915  * set via cross-VPE MTTR manipulation of the Cause register. It would be
916  * in some regards preferable to have external logic for "doorbell" hardware
917  * interrupts.
918  */
919
920 static int cpu_ipi_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_IRQ;
921
922 static irqreturn_t ipi_interrupt(int irq, void *dev_idm)
923 {
924         int my_vpe = cpu_data[smp_processor_id()].vpe_id;
925         int my_tc = cpu_data[smp_processor_id()].tc_id;
926         int cpu;
927         struct smtc_ipi *pipi;
928         unsigned long tcstatus;
929         int sent;
930         long flags;
931         unsigned int mtflags;
932         unsigned int vpflags;
933
934         /*
935          * So long as cross-VPE interrupts are done via
936          * MFTR/MTTR read-modify-writes of Cause, we need
937          * to stop other VPEs whenever the local VPE does
938          * anything similar.
939          */
940         local_irq_save(flags);
941         vpflags = dvpe();
942         clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ);
943         set_c0_status(0x100 << MIPS_CPU_IPI_IRQ);
944         irq_enable_hazard();
945         evpe(vpflags);
946         local_irq_restore(flags);
947
948         /*
949          * Cross-VPE Interrupt handler: Try to directly deliver IPIs
950          * queued for TCs on this VPE other than the current one.
951          * Return-from-interrupt should cause us to drain the queue
952          * for the current TC, so we ought not to have to do it explicitly here.
953          */
954
955         for_each_online_cpu(cpu) {
956                 if (cpu_data[cpu].vpe_id != my_vpe)
957                         continue;
958
959                 pipi = smtc_ipi_dq(&IPIQ[cpu]);
960                 if (pipi != NULL) {
961                         if (cpu_data[cpu].tc_id != my_tc) {
962                                 sent = 0;
963                                 LOCK_MT_PRA();
964                                 settc(cpu_data[cpu].tc_id);
965                                 write_tc_c0_tchalt(TCHALT_H);
966                                 mips_ihb();
967                                 tcstatus = read_tc_c0_tcstatus();
968                                 if ((tcstatus & TCSTATUS_IXMT) == 0) {
969                                         post_direct_ipi(cpu, pipi);
970                                         sent = 1;
971                                 }
972                                 write_tc_c0_tchalt(0);
973                                 UNLOCK_MT_PRA();
974                                 if (!sent) {
975                                         smtc_ipi_req(&IPIQ[cpu], pipi);
976                                 }
977                         } else {
978                                 /*
979                                  * ipi_decode() should be called
980                                  * with interrupts off
981                                  */
982                                 local_irq_save(flags);
983                                 ipi_decode(pipi);
984                                 local_irq_restore(flags);
985                         }
986                 }
987         }
988
989         return IRQ_HANDLED;
990 }
991
992 static void ipi_irq_dispatch(void)
993 {
994         do_IRQ(cpu_ipi_irq);
995 }
996
997 static struct irqaction irq_ipi;
998
999 void setup_cross_vpe_interrupts(void)
1000 {
1001         if (!cpu_has_vint)
1002                 panic("SMTC Kernel requires Vectored Interupt support");
1003
1004         set_vi_handler(MIPS_CPU_IPI_IRQ, ipi_irq_dispatch);
1005
1006         irq_ipi.handler = ipi_interrupt;
1007         irq_ipi.flags = IRQF_DISABLED;
1008         irq_ipi.name = "SMTC_IPI";
1009
1010         setup_irq_smtc(cpu_ipi_irq, &irq_ipi, (0x100 << MIPS_CPU_IPI_IRQ));
1011
1012         irq_desc[cpu_ipi_irq].status |= IRQ_PER_CPU;
1013         set_irq_handler(cpu_ipi_irq, handle_percpu_irq);
1014 }
1015
1016 /*
1017  * SMTC-specific hacks invoked from elsewhere in the kernel.
1018  */
1019
1020 void smtc_ipi_replay(void)
1021 {
1022         /*
1023          * To the extent that we've ever turned interrupts off,
1024          * we may have accumulated deferred IPIs.  This is subtle.
1025          * If we use the smtc_ipi_qdepth() macro, we'll get an
1026          * exact number - but we'll also disable interrupts
1027          * and create a window of failure where a new IPI gets
1028          * queued after we test the depth but before we re-enable
1029          * interrupts. So long as IXMT never gets set, however,
1030          * we should be OK:  If we pick up something and dispatch
1031          * it here, that's great. If we see nothing, but concurrent
1032          * with this operation, another TC sends us an IPI, IXMT
1033          * is clear, and we'll handle it as a real pseudo-interrupt
1034          * and not a pseudo-pseudo interrupt.
1035          */
1036         if (IPIQ[smp_processor_id()].depth > 0) {
1037                 struct smtc_ipi *pipi;
1038                 extern void self_ipi(struct smtc_ipi *);
1039
1040                 while ((pipi = smtc_ipi_dq(&IPIQ[smp_processor_id()]))) {
1041                         self_ipi(pipi);
1042                         smtc_cpu_stats[smp_processor_id()].selfipis++;
1043                 }
1044         }
1045 }
1046
1047 void smtc_idle_loop_hook(void)
1048 {
1049 #ifdef SMTC_IDLE_HOOK_DEBUG
1050         int im;
1051         int flags;
1052         int mtflags;
1053         int bit;
1054         int vpe;
1055         int tc;
1056         int hook_ntcs;
1057         /*
1058          * printk within DMT-protected regions can deadlock,
1059          * so buffer diagnostic messages for later output.
1060          */
1061         char *pdb_msg;
1062         char id_ho_db_msg[768]; /* worst-case use should be less than 700 */
1063
1064         if (atomic_read(&idle_hook_initialized) == 0) { /* fast test */
1065                 if (atomic_add_return(1, &idle_hook_initialized) == 1) {
1066                         int mvpconf0;
1067                         /* Tedious stuff to just do once */
1068                         mvpconf0 = read_c0_mvpconf0();
1069                         hook_ntcs = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
1070                         if (hook_ntcs > NR_CPUS)
1071                                 hook_ntcs = NR_CPUS;
1072                         for (tc = 0; tc < hook_ntcs; tc++) {
1073                                 tcnoprog[tc] = 0;
1074                                 clock_hang_reported[tc] = 0;
1075                         }
1076                         for (vpe = 0; vpe < 2; vpe++)
1077                                 for (im = 0; im < 8; im++)
1078                                         imstuckcount[vpe][im] = 0;
1079                         printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs);
1080                         atomic_set(&idle_hook_initialized, 1000);
1081                 } else {
1082                         /* Someone else is initializing in parallel - let 'em finish */
1083                         while (atomic_read(&idle_hook_initialized) < 1000)
1084                                 ;
1085                 }
1086         }
1087
1088         /* Have we stupidly left IXMT set somewhere? */
1089         if (read_c0_tcstatus() & 0x400) {
1090                 write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
1091                 ehb();
1092                 printk("Dangling IXMT in cpu_idle()\n");
1093         }
1094
1095         /* Have we stupidly left an IM bit turned off? */
1096 #define IM_LIMIT 2000
1097         local_irq_save(flags);
1098         mtflags = dmt();
1099         pdb_msg = &id_ho_db_msg[0];
1100         im = read_c0_status();
1101         vpe = cpu_data[smp_processor_id()].vpe_id;
1102         for (bit = 0; bit < 8; bit++) {
1103                 /*
1104                  * In current prototype, I/O interrupts
1105                  * are masked for VPE > 0
1106                  */
1107                 if (vpemask[vpe][bit]) {
1108                         if (!(im & (0x100 << bit)))
1109                                 imstuckcount[vpe][bit]++;
1110                         else
1111                                 imstuckcount[vpe][bit] = 0;
1112                         if (imstuckcount[vpe][bit] > IM_LIMIT) {
1113                                 set_c0_status(0x100 << bit);
1114                                 ehb();
1115                                 imstuckcount[vpe][bit] = 0;
1116                                 pdb_msg += sprintf(pdb_msg,
1117                                         "Dangling IM %d fixed for VPE %d\n", bit,
1118                                         vpe);
1119                         }
1120                 }
1121         }
1122
1123         /*
1124          * Now that we limit outstanding timer IPIs, check for hung TC
1125          */
1126         for (tc = 0; tc < NR_CPUS; tc++) {
1127                 /* Don't check ourself - we'll dequeue IPIs just below */
1128                 if ((tc != smp_processor_id()) &&
1129                     ipi_timer_latch[tc] > timerq_limit) {
1130                     if (clock_hang_reported[tc] == 0) {
1131                         pdb_msg += sprintf(pdb_msg,
1132                                 "TC %d looks hung with timer latch at %d\n",
1133                                 tc, ipi_timer_latch[tc]);
1134                         clock_hang_reported[tc]++;
1135                         }
1136                 }
1137         }
1138         emt(mtflags);
1139         local_irq_restore(flags);
1140         if (pdb_msg != &id_ho_db_msg[0])
1141                 printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg);
1142 #endif /* SMTC_IDLE_HOOK_DEBUG */
1143
1144         /*
1145          * Replay any accumulated deferred IPIs. If "Instant Replay"
1146          * is in use, there should never be any.
1147          */
1148 #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
1149         smtc_ipi_replay();
1150 #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
1151 }
1152
1153 void smtc_soft_dump(void)
1154 {
1155         int i;
1156
1157         printk("Counter Interrupts taken per CPU (TC)\n");
1158         for (i=0; i < NR_CPUS; i++) {
1159                 printk("%d: %ld\n", i, smtc_cpu_stats[i].timerints);
1160         }
1161         printk("Self-IPI invocations:\n");
1162         for (i=0; i < NR_CPUS; i++) {
1163                 printk("%d: %ld\n", i, smtc_cpu_stats[i].selfipis);
1164         }
1165         smtc_ipi_qdump();
1166         printk("Timer IPI Backlogs:\n");
1167         for (i=0; i < NR_CPUS; i++) {
1168                 printk("%d: %d\n", i, ipi_timer_latch[i]);
1169         }
1170         printk("%d Recoveries of \"stolen\" FPU\n",
1171                atomic_read(&smtc_fpu_recoveries));
1172 }
1173
1174
1175 /*
1176  * TLB management routines special to SMTC
1177  */
1178
1179 void smtc_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
1180 {
1181         unsigned long flags, mtflags, tcstat, prevhalt, asid;
1182         int tlb, i;
1183
1184         /*
1185          * It would be nice to be able to use a spinlock here,
1186          * but this is invoked from within TLB flush routines
1187          * that protect themselves with DVPE, so if a lock is
1188          * held by another TC, it'll never be freed.
1189          *
1190          * DVPE/DMT must not be done with interrupts enabled,
1191          * so even so most callers will already have disabled
1192          * them, let's be really careful...
1193          */
1194
1195         local_irq_save(flags);
1196         if (smtc_status & SMTC_TLB_SHARED) {
1197                 mtflags = dvpe();
1198                 tlb = 0;
1199         } else {
1200                 mtflags = dmt();
1201                 tlb = cpu_data[cpu].vpe_id;
1202         }
1203         asid = asid_cache(cpu);
1204
1205         do {
1206                 if (!((asid += ASID_INC) & ASID_MASK) ) {
1207                         if (cpu_has_vtag_icache)
1208                                 flush_icache_all();
1209                         /* Traverse all online CPUs (hack requires contigous range) */
1210                         for (i = 0; i < num_online_cpus(); i++) {
1211                                 /*
1212                                  * We don't need to worry about our own CPU, nor those of
1213                                  * CPUs who don't share our TLB.
1214                                  */
1215                                 if ((i != smp_processor_id()) &&
1216                                     ((smtc_status & SMTC_TLB_SHARED) ||
1217                                      (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))) {
1218                                         settc(cpu_data[i].tc_id);
1219                                         prevhalt = read_tc_c0_tchalt() & TCHALT_H;
1220                                         if (!prevhalt) {
1221                                                 write_tc_c0_tchalt(TCHALT_H);
1222                                                 mips_ihb();
1223                                         }
1224                                         tcstat = read_tc_c0_tcstatus();
1225                                         smtc_live_asid[tlb][(tcstat & ASID_MASK)] |= (asiduse)(0x1 << i);
1226                                         if (!prevhalt)
1227                                                 write_tc_c0_tchalt(0);
1228                                 }
1229                         }
1230                         if (!asid)              /* fix version if needed */
1231                                 asid = ASID_FIRST_VERSION;
1232                         local_flush_tlb_all();  /* start new asid cycle */
1233                 }
1234         } while (smtc_live_asid[tlb][(asid & ASID_MASK)]);
1235
1236         /*
1237          * SMTC shares the TLB within VPEs and possibly across all VPEs.
1238          */
1239         for (i = 0; i < num_online_cpus(); i++) {
1240                 if ((smtc_status & SMTC_TLB_SHARED) ||
1241                     (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
1242                         cpu_context(i, mm) = asid_cache(i) = asid;
1243         }
1244
1245         if (smtc_status & SMTC_TLB_SHARED)
1246                 evpe(mtflags);
1247         else
1248                 emt(mtflags);
1249         local_irq_restore(flags);
1250 }
1251
1252 /*
1253  * Invoked from macros defined in mmu_context.h
1254  * which must already have disabled interrupts
1255  * and done a DVPE or DMT as appropriate.
1256  */
1257
1258 void smtc_flush_tlb_asid(unsigned long asid)
1259 {
1260         int entry;
1261         unsigned long ehi;
1262
1263         entry = read_c0_wired();
1264
1265         /* Traverse all non-wired entries */
1266         while (entry < current_cpu_data.tlbsize) {
1267                 write_c0_index(entry);
1268                 ehb();
1269                 tlb_read();
1270                 ehb();
1271                 ehi = read_c0_entryhi();
1272                 if ((ehi & ASID_MASK) == asid) {
1273                     /*
1274                      * Invalidate only entries with specified ASID,
1275                      * makiing sure all entries differ.
1276                      */
1277                     write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
1278                     write_c0_entrylo0(0);
1279                     write_c0_entrylo1(0);
1280                     mtc0_tlbw_hazard();
1281                     tlb_write_indexed();
1282                 }
1283                 entry++;
1284         }
1285         write_c0_index(PARKED_INDEX);
1286         tlbw_use_hazard();
1287 }
1288
1289 /*
1290  * Support for single-threading cache flush operations.
1291  */
1292
1293 int halt_state_save[NR_CPUS];
1294
1295 /*
1296  * To really, really be sure that nothing is being done
1297  * by other TCs, halt them all.  This code assumes that
1298  * a DVPE has already been done, so while their Halted
1299  * state is theoretically architecturally unstable, in
1300  * practice, it's not going to change while we're looking
1301  * at it.
1302  */
1303
1304 void smtc_cflush_lockdown(void)
1305 {
1306         int cpu;
1307
1308         for_each_online_cpu(cpu) {
1309                 if (cpu != smp_processor_id()) {
1310                         settc(cpu_data[cpu].tc_id);
1311                         halt_state_save[cpu] = read_tc_c0_tchalt();
1312                         write_tc_c0_tchalt(TCHALT_H);
1313                 }
1314         }
1315         mips_ihb();
1316 }
1317
1318 /* It would be cheating to change the cpu_online states during a flush! */
1319
1320 void smtc_cflush_release(void)
1321 {
1322         int cpu;
1323
1324         /*
1325          * Start with a hazard barrier to ensure
1326          * that all CACHE ops have played through.
1327          */
1328         mips_ihb();
1329
1330         for_each_online_cpu(cpu) {
1331                 if (cpu != smp_processor_id()) {
1332                         settc(cpu_data[cpu].tc_id);
1333                         write_tc_c0_tchalt(halt_state_save[cpu]);
1334                 }
1335         }
1336         mips_ihb();
1337 }