au1550_spi: fix prototype of irq handler
[wrt350n-kernel.git] / arch / mips / kernel / smtc.c
blobb42e71c711199a4f169f717e8467240b8ebbae57
1 /* Copyright (C) 2004 Mips Technologies, Inc */
3 #include <linux/clockchips.h>
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/cpumask.h>
7 #include <linux/interrupt.h>
8 #include <linux/kernel_stat.h>
9 #include <linux/module.h>
11 #include <asm/cpu.h>
12 #include <asm/processor.h>
13 #include <asm/atomic.h>
14 #include <asm/system.h>
15 #include <asm/hardirq.h>
16 #include <asm/hazards.h>
17 #include <asm/irq.h>
18 #include <asm/mmu_context.h>
19 #include <asm/mipsregs.h>
20 #include <asm/cacheflush.h>
21 #include <asm/time.h>
22 #include <asm/addrspace.h>
23 #include <asm/smtc.h>
24 #include <asm/smtc_ipi.h>
25 #include <asm/smtc_proc.h>
28 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
29 * in do_IRQ. These are passed in setup_irq_smtc() and stored
30 * in this table.
32 unsigned long irq_hwmask[NR_IRQS];
34 #define LOCK_MT_PRA() \
35 local_irq_save(flags); \
36 mtflags = dmt()
38 #define UNLOCK_MT_PRA() \
39 emt(mtflags); \
40 local_irq_restore(flags)
42 #define LOCK_CORE_PRA() \
43 local_irq_save(flags); \
44 mtflags = dvpe()
46 #define UNLOCK_CORE_PRA() \
47 evpe(mtflags); \
48 local_irq_restore(flags)
51 * Data structures purely associated with SMTC parallelism
56 * Table for tracking ASIDs whose lifetime is prolonged.
59 asiduse smtc_live_asid[MAX_SMTC_TLBS][MAX_SMTC_ASIDS];
62 * Clock interrupt "latch" buffers, per "CPU"
65 static atomic_t ipi_timer_latch[NR_CPUS];
68 * Number of InterProcessor Interrupt (IPI) message buffers to allocate
71 #define IPIBUF_PER_CPU 4
73 static struct smtc_ipi_q IPIQ[NR_CPUS];
74 static struct smtc_ipi_q freeIPIq;
77 /* Forward declarations */
79 void ipi_decode(struct smtc_ipi *);
80 static void post_direct_ipi(int cpu, struct smtc_ipi *pipi);
81 static void setup_cross_vpe_interrupts(unsigned int nvpe);
82 void init_smtc_stats(void);
84 /* Global SMTC Status */
86 unsigned int smtc_status = 0;
88 /* Boot command line configuration overrides */
90 static int vpe0limit;
91 static int ipibuffers = 0;
92 static int nostlb = 0;
93 static int asidmask = 0;
94 unsigned long smtc_asid_mask = 0xff;
96 static int __init vpe0tcs(char *str)
98 get_option(&str, &vpe0limit);
100 return 1;
103 static int __init ipibufs(char *str)
105 get_option(&str, &ipibuffers);
106 return 1;
109 static int __init stlb_disable(char *s)
111 nostlb = 1;
112 return 1;
115 static int __init asidmask_set(char *str)
117 get_option(&str, &asidmask);
118 switch (asidmask) {
119 case 0x1:
120 case 0x3:
121 case 0x7:
122 case 0xf:
123 case 0x1f:
124 case 0x3f:
125 case 0x7f:
126 case 0xff:
127 smtc_asid_mask = (unsigned long)asidmask;
128 break;
129 default:
130 printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask);
132 return 1;
135 __setup("vpe0tcs=", vpe0tcs);
136 __setup("ipibufs=", ipibufs);
137 __setup("nostlb", stlb_disable);
138 __setup("asidmask=", asidmask_set);
140 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
142 static int hang_trig = 0;
144 static int __init hangtrig_enable(char *s)
146 hang_trig = 1;
147 return 1;
151 __setup("hangtrig", hangtrig_enable);
153 #define DEFAULT_BLOCKED_IPI_LIMIT 32
155 static int timerq_limit = DEFAULT_BLOCKED_IPI_LIMIT;
157 static int __init tintq(char *str)
159 get_option(&str, &timerq_limit);
160 return 1;
163 __setup("tintq=", tintq);
165 static int imstuckcount[2][8];
166 /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
167 static int vpemask[2][8] = {
168 {0, 0, 1, 0, 0, 0, 0, 1},
169 {0, 0, 0, 0, 0, 0, 0, 1}
171 int tcnoprog[NR_CPUS];
172 static atomic_t idle_hook_initialized = {0};
173 static int clock_hang_reported[NR_CPUS];
175 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
177 /* Initialize shared TLB - the should probably migrate to smtc_setup_cpus() */
179 void __init sanitize_tlb_entries(void)
181 printk("Deprecated sanitize_tlb_entries() invoked\n");
186 * Configure shared TLB - VPC configuration bit must be set by caller
189 static void smtc_configure_tlb(void)
191 int i, tlbsiz, vpes;
192 unsigned long mvpconf0;
193 unsigned long config1val;
195 /* Set up ASID preservation table */
196 for (vpes=0; vpes<MAX_SMTC_TLBS; vpes++) {
197 for(i = 0; i < MAX_SMTC_ASIDS; i++) {
198 smtc_live_asid[vpes][i] = 0;
201 mvpconf0 = read_c0_mvpconf0();
203 if ((vpes = ((mvpconf0 & MVPCONF0_PVPE)
204 >> MVPCONF0_PVPE_SHIFT) + 1) > 1) {
205 /* If we have multiple VPEs, try to share the TLB */
206 if ((mvpconf0 & MVPCONF0_TLBS) && !nostlb) {
208 * If TLB sizing is programmable, shared TLB
209 * size is the total available complement.
210 * Otherwise, we have to take the sum of all
211 * static VPE TLB entries.
213 if ((tlbsiz = ((mvpconf0 & MVPCONF0_PTLBE)
214 >> MVPCONF0_PTLBE_SHIFT)) == 0) {
216 * If there's more than one VPE, there had better
217 * be more than one TC, because we need one to bind
218 * to each VPE in turn to be able to read
219 * its configuration state!
221 settc(1);
222 /* Stop the TC from doing anything foolish */
223 write_tc_c0_tchalt(TCHALT_H);
224 mips_ihb();
225 /* No need to un-Halt - that happens later anyway */
226 for (i=0; i < vpes; i++) {
227 write_tc_c0_tcbind(i);
229 * To be 100% sure we're really getting the right
230 * information, we exit the configuration state
231 * and do an IHB after each rebinding.
233 write_c0_mvpcontrol(
234 read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
235 mips_ihb();
237 * Only count if the MMU Type indicated is TLB
239 if (((read_vpe_c0_config() & MIPS_CONF_MT) >> 7) == 1) {
240 config1val = read_vpe_c0_config1();
241 tlbsiz += ((config1val >> 25) & 0x3f) + 1;
244 /* Put core back in configuration state */
245 write_c0_mvpcontrol(
246 read_c0_mvpcontrol() | MVPCONTROL_VPC );
247 mips_ihb();
250 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB);
251 ehb();
254 * Setup kernel data structures to use software total,
255 * rather than read the per-VPE Config1 value. The values
256 * for "CPU 0" gets copied to all the other CPUs as part
257 * of their initialization in smtc_cpu_setup().
260 /* MIPS32 limits TLB indices to 64 */
261 if (tlbsiz > 64)
262 tlbsiz = 64;
263 cpu_data[0].tlbsize = current_cpu_data.tlbsize = tlbsiz;
264 smtc_status |= SMTC_TLB_SHARED;
265 local_flush_tlb_all();
267 printk("TLB of %d entry pairs shared by %d VPEs\n",
268 tlbsiz, vpes);
269 } else {
270 printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
277 * Incrementally build the CPU map out of constituent MIPS MT cores,
278 * using the specified available VPEs and TCs. Plaform code needs
279 * to ensure that each MIPS MT core invokes this routine on reset,
280 * one at a time(!).
282 * This version of the build_cpu_map and prepare_cpus routines assumes
283 * that *all* TCs of a MIPS MT core will be used for Linux, and that
284 * they will be spread across *all* available VPEs (to minimise the
285 * loss of efficiency due to exception service serialization).
286 * An improved version would pick up configuration information and
287 * possibly leave some TCs/VPEs as "slave" processors.
289 * Use c0_MVPConf0 to find out how many TCs are available, setting up
290 * phys_cpu_present_map and the logical/physical mappings.
293 int __init mipsmt_build_cpu_map(int start_cpu_slot)
295 int i, ntcs;
298 * The CPU map isn't actually used for anything at this point,
299 * so it's not clear what else we should do apart from set
300 * everything up so that "logical" = "physical".
302 ntcs = ((read_c0_mvpconf0() & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
303 for (i=start_cpu_slot; i<NR_CPUS && i<ntcs; i++) {
304 cpu_set(i, phys_cpu_present_map);
305 __cpu_number_map[i] = i;
306 __cpu_logical_map[i] = i;
308 #ifdef CONFIG_MIPS_MT_FPAFF
309 /* Initialize map of CPUs with FPUs */
310 cpus_clear(mt_fpu_cpumask);
311 #endif
313 /* One of those TC's is the one booting, and not a secondary... */
314 printk("%i available secondary CPU TC(s)\n", i - 1);
316 return i;
320 * Common setup before any secondaries are started
321 * Make sure all CPU's are in a sensible state before we boot any of the
322 * secondaries.
324 * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
325 * as possible across the available VPEs.
328 static void smtc_tc_setup(int vpe, int tc, int cpu)
330 settc(tc);
331 write_tc_c0_tchalt(TCHALT_H);
332 mips_ihb();
333 write_tc_c0_tcstatus((read_tc_c0_tcstatus()
334 & ~(TCSTATUS_TKSU | TCSTATUS_DA | TCSTATUS_IXMT))
335 | TCSTATUS_A);
336 write_tc_c0_tccontext(0);
337 /* Bind tc to vpe */
338 write_tc_c0_tcbind(vpe);
339 /* In general, all TCs should have the same cpu_data indications */
340 memcpy(&cpu_data[cpu], &cpu_data[0], sizeof(struct cpuinfo_mips));
341 /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
342 if (cpu_data[0].cputype == CPU_34K)
343 cpu_data[cpu].options &= ~MIPS_CPU_FPU;
344 cpu_data[cpu].vpe_id = vpe;
345 cpu_data[cpu].tc_id = tc;
349 void mipsmt_prepare_cpus(void)
351 int i, vpe, tc, ntc, nvpe, tcpervpe[NR_CPUS], slop, cpu;
352 unsigned long flags;
353 unsigned long val;
354 int nipi;
355 struct smtc_ipi *pipi;
357 /* disable interrupts so we can disable MT */
358 local_irq_save(flags);
359 /* disable MT so we can configure */
360 dvpe();
361 dmt();
363 spin_lock_init(&freeIPIq.lock);
366 * We probably don't have as many VPEs as we do SMP "CPUs",
367 * but it's possible - and in any case we'll never use more!
369 for (i=0; i<NR_CPUS; i++) {
370 IPIQ[i].head = IPIQ[i].tail = NULL;
371 spin_lock_init(&IPIQ[i].lock);
372 IPIQ[i].depth = 0;
373 atomic_set(&ipi_timer_latch[i], 0);
376 /* cpu_data index starts at zero */
377 cpu = 0;
378 cpu_data[cpu].vpe_id = 0;
379 cpu_data[cpu].tc_id = 0;
380 cpu++;
382 /* Report on boot-time options */
383 mips_mt_set_cpuoptions();
384 if (vpelimit > 0)
385 printk("Limit of %d VPEs set\n", vpelimit);
386 if (tclimit > 0)
387 printk("Limit of %d TCs set\n", tclimit);
388 if (nostlb) {
389 printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
391 if (asidmask)
392 printk("ASID mask value override to 0x%x\n", asidmask);
394 /* Temporary */
395 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
396 if (hang_trig)
397 printk("Logic Analyser Trigger on suspected TC hang\n");
398 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
400 /* Put MVPE's into 'configuration state' */
401 write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC );
403 val = read_c0_mvpconf0();
404 nvpe = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
405 if (vpelimit > 0 && nvpe > vpelimit)
406 nvpe = vpelimit;
407 ntc = ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
408 if (ntc > NR_CPUS)
409 ntc = NR_CPUS;
410 if (tclimit > 0 && ntc > tclimit)
411 ntc = tclimit;
412 slop = ntc % nvpe;
413 for (i = 0; i < nvpe; i++) {
414 tcpervpe[i] = ntc / nvpe;
415 if (slop) {
416 if((slop - i) > 0) tcpervpe[i]++;
419 /* Handle command line override for VPE0 */
420 if (vpe0limit > ntc) vpe0limit = ntc;
421 if (vpe0limit > 0) {
422 int slopslop;
423 if (vpe0limit < tcpervpe[0]) {
424 /* Reducing TC count - distribute to others */
425 slop = tcpervpe[0] - vpe0limit;
426 slopslop = slop % (nvpe - 1);
427 tcpervpe[0] = vpe0limit;
428 for (i = 1; i < nvpe; i++) {
429 tcpervpe[i] += slop / (nvpe - 1);
430 if(slopslop && ((slopslop - (i - 1) > 0)))
431 tcpervpe[i]++;
433 } else if (vpe0limit > tcpervpe[0]) {
434 /* Increasing TC count - steal from others */
435 slop = vpe0limit - tcpervpe[0];
436 slopslop = slop % (nvpe - 1);
437 tcpervpe[0] = vpe0limit;
438 for (i = 1; i < nvpe; i++) {
439 tcpervpe[i] -= slop / (nvpe - 1);
440 if(slopslop && ((slopslop - (i - 1) > 0)))
441 tcpervpe[i]--;
446 /* Set up shared TLB */
447 smtc_configure_tlb();
449 for (tc = 0, vpe = 0 ; (vpe < nvpe) && (tc < ntc) ; vpe++) {
451 * Set the MVP bits.
453 settc(tc);
454 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP);
455 if (vpe != 0)
456 printk(", ");
457 printk("VPE %d: TC", vpe);
458 for (i = 0; i < tcpervpe[vpe]; i++) {
460 * TC 0 is bound to VPE 0 at reset,
461 * and is presumably executing this
462 * code. Leave it alone!
464 if (tc != 0) {
465 smtc_tc_setup(vpe, tc, cpu);
466 cpu++;
468 printk(" %d", tc);
469 tc++;
471 if (vpe != 0) {
473 * Clear any stale software interrupts from VPE's Cause
475 write_vpe_c0_cause(0);
478 * Clear ERL/EXL of VPEs other than 0
479 * and set restricted interrupt enable/mask.
481 write_vpe_c0_status((read_vpe_c0_status()
482 & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
483 | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
484 | ST0_IE));
486 * set config to be the same as vpe0,
487 * particularly kseg0 coherency alg
489 write_vpe_c0_config(read_c0_config());
490 /* Clear any pending timer interrupt */
491 write_vpe_c0_compare(0);
492 /* Propagate Config7 */
493 write_vpe_c0_config7(read_c0_config7());
494 write_vpe_c0_count(read_c0_count());
496 /* enable multi-threading within VPE */
497 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
498 /* enable the VPE */
499 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
503 * Pull any physically present but unused TCs out of circulation.
505 while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
506 cpu_clear(tc, phys_cpu_present_map);
507 cpu_clear(tc, cpu_present_map);
508 tc++;
511 /* release config state */
512 write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
514 printk("\n");
516 /* Set up coprocessor affinity CPU mask(s) */
518 #ifdef CONFIG_MIPS_MT_FPAFF
519 for (tc = 0; tc < ntc; tc++) {
520 if (cpu_data[tc].options & MIPS_CPU_FPU)
521 cpu_set(tc, mt_fpu_cpumask);
523 #endif
525 /* set up ipi interrupts... */
527 /* If we have multiple VPEs running, set up the cross-VPE interrupt */
529 setup_cross_vpe_interrupts(nvpe);
531 /* Set up queue of free IPI "messages". */
532 nipi = NR_CPUS * IPIBUF_PER_CPU;
533 if (ipibuffers > 0)
534 nipi = ipibuffers;
536 pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
537 if (pipi == NULL)
538 panic("kmalloc of IPI message buffers failed\n");
539 else
540 printk("IPI buffer pool of %d buffers\n", nipi);
541 for (i = 0; i < nipi; i++) {
542 smtc_ipi_nq(&freeIPIq, pipi);
543 pipi++;
546 /* Arm multithreading and enable other VPEs - but all TCs are Halted */
547 emt(EMT_ENABLE);
548 evpe(EVPE_ENABLE);
549 local_irq_restore(flags);
550 /* Initialize SMTC /proc statistics/diagnostics */
551 init_smtc_stats();
556 * Setup the PC, SP, and GP of a secondary processor and start it
557 * running!
558 * smp_bootstrap is the place to resume from
559 * __KSTK_TOS(idle) is apparently the stack pointer
560 * (unsigned long)idle->thread_info the gp
563 void __cpuinit smtc_boot_secondary(int cpu, struct task_struct *idle)
565 extern u32 kernelsp[NR_CPUS];
566 long flags;
567 int mtflags;
569 LOCK_MT_PRA();
570 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
571 dvpe();
573 settc(cpu_data[cpu].tc_id);
575 /* pc */
576 write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
578 /* stack pointer */
579 kernelsp[cpu] = __KSTK_TOS(idle);
580 write_tc_gpr_sp(__KSTK_TOS(idle));
582 /* global pointer */
583 write_tc_gpr_gp((unsigned long)task_thread_info(idle));
585 smtc_status |= SMTC_MTC_ACTIVE;
586 write_tc_c0_tchalt(0);
587 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
588 evpe(EVPE_ENABLE);
590 UNLOCK_MT_PRA();
593 void smtc_init_secondary(void)
596 * Start timer on secondary VPEs if necessary.
597 * plat_timer_setup has already have been invoked by init/main
598 * on "boot" TC. Like per_cpu_trap_init() hack, this assumes that
599 * SMTC init code assigns TCs consdecutively and in ascending order
600 * to across available VPEs.
602 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
603 ((read_c0_tcbind() & TCBIND_CURVPE)
604 != cpu_data[smp_processor_id() - 1].vpe_id)){
605 write_c0_compare(read_c0_count() + mips_hpt_frequency/HZ);
608 local_irq_enable();
611 void smtc_smp_finish(void)
613 printk("TC %d going on-line as CPU %d\n",
614 cpu_data[smp_processor_id()].tc_id, smp_processor_id());
617 void smtc_cpus_done(void)
622 * Support for SMTC-optimized driver IRQ registration
626 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
627 * in do_IRQ. These are passed in setup_irq_smtc() and stored
628 * in this table.
631 int setup_irq_smtc(unsigned int irq, struct irqaction * new,
632 unsigned long hwmask)
634 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
635 unsigned int vpe = current_cpu_data.vpe_id;
637 vpemask[vpe][irq - MIPS_CPU_IRQ_BASE] = 1;
638 #endif
639 irq_hwmask[irq] = hwmask;
641 return setup_irq(irq, new);
644 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
646 * Support for IRQ affinity to TCs
649 void smtc_set_irq_affinity(unsigned int irq, cpumask_t affinity)
652 * If a "fast path" cache of quickly decodable affinity state
653 * is maintained, this is where it gets done, on a call up
654 * from the platform affinity code.
658 void smtc_forward_irq(unsigned int irq)
660 int target;
663 * OK wise guy, now figure out how to get the IRQ
664 * to be serviced on an authorized "CPU".
666 * Ideally, to handle the situation where an IRQ has multiple
667 * eligible CPUS, we would maintain state per IRQ that would
668 * allow a fair distribution of service requests. Since the
669 * expected use model is any-or-only-one, for simplicity
670 * and efficiency, we just pick the easiest one to find.
673 target = first_cpu(irq_desc[irq].affinity);
676 * We depend on the platform code to have correctly processed
677 * IRQ affinity change requests to ensure that the IRQ affinity
678 * mask has been purged of bits corresponding to nonexistent and
679 * offline "CPUs", and to TCs bound to VPEs other than the VPE
680 * connected to the physical interrupt input for the interrupt
681 * in question. Otherwise we have a nasty problem with interrupt
682 * mask management. This is best handled in non-performance-critical
683 * platform IRQ affinity setting code, to minimize interrupt-time
684 * checks.
687 /* If no one is eligible, service locally */
688 if (target >= NR_CPUS) {
689 do_IRQ_no_affinity(irq);
690 return;
693 smtc_send_ipi(target, IRQ_AFFINITY_IPI, irq);
696 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
699 * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
700 * Within a VPE one TC can interrupt another by different approaches.
701 * The easiest to get right would probably be to make all TCs except
702 * the target IXMT and set a software interrupt, but an IXMT-based
703 * scheme requires that a handler must run before a new IPI could
704 * be sent, which would break the "broadcast" loops in MIPS MT.
705 * A more gonzo approach within a VPE is to halt the TC, extract
706 * its Restart, Status, and a couple of GPRs, and program the Restart
707 * address to emulate an interrupt.
709 * Within a VPE, one can be confident that the target TC isn't in
710 * a critical EXL state when halted, since the write to the Halt
711 * register could not have issued on the writing thread if the
712 * halting thread had EXL set. So k0 and k1 of the target TC
713 * can be used by the injection code. Across VPEs, one can't
714 * be certain that the target TC isn't in a critical exception
715 * state. So we try a two-step process of sending a software
716 * interrupt to the target VPE, which either handles the event
717 * itself (if it was the target) or injects the event within
718 * the VPE.
721 static void smtc_ipi_qdump(void)
723 int i;
725 for (i = 0; i < NR_CPUS ;i++) {
726 printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
727 i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
728 IPIQ[i].depth);
733 * The standard atomic.h primitives don't quite do what we want
734 * here: We need an atomic add-and-return-previous-value (which
735 * could be done with atomic_add_return and a decrement) and an
736 * atomic set/zero-and-return-previous-value (which can't really
737 * be done with the atomic.h primitives). And since this is
738 * MIPS MT, we can assume that we have LL/SC.
740 static inline int atomic_postincrement(atomic_t *v)
742 unsigned long result;
744 unsigned long temp;
746 __asm__ __volatile__(
747 "1: ll %0, %2 \n"
748 " addu %1, %0, 1 \n"
749 " sc %1, %2 \n"
750 " beqz %1, 1b \n"
751 __WEAK_LLSC_MB
752 : "=&r" (result), "=&r" (temp), "=m" (v->counter)
753 : "m" (v->counter)
754 : "memory");
756 return result;
759 void smtc_send_ipi(int cpu, int type, unsigned int action)
761 int tcstatus;
762 struct smtc_ipi *pipi;
763 long flags;
764 int mtflags;
766 if (cpu == smp_processor_id()) {
767 printk("Cannot Send IPI to self!\n");
768 return;
770 /* Set up a descriptor, to be delivered either promptly or queued */
771 pipi = smtc_ipi_dq(&freeIPIq);
772 if (pipi == NULL) {
773 bust_spinlocks(1);
774 mips_mt_regdump(dvpe());
775 panic("IPI Msg. Buffers Depleted\n");
777 pipi->type = type;
778 pipi->arg = (void *)action;
779 pipi->dest = cpu;
780 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
781 if (type == SMTC_CLOCK_TICK)
782 atomic_inc(&ipi_timer_latch[cpu]);
783 /* If not on same VPE, enqueue and send cross-VPE interrupt */
784 smtc_ipi_nq(&IPIQ[cpu], pipi);
785 LOCK_CORE_PRA();
786 settc(cpu_data[cpu].tc_id);
787 write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
788 UNLOCK_CORE_PRA();
789 } else {
791 * Not sufficient to do a LOCK_MT_PRA (dmt) here,
792 * since ASID shootdown on the other VPE may
793 * collide with this operation.
795 LOCK_CORE_PRA();
796 settc(cpu_data[cpu].tc_id);
797 /* Halt the targeted TC */
798 write_tc_c0_tchalt(TCHALT_H);
799 mips_ihb();
802 * Inspect TCStatus - if IXMT is set, we have to queue
803 * a message. Otherwise, we set up the "interrupt"
804 * of the other TC
806 tcstatus = read_tc_c0_tcstatus();
808 if ((tcstatus & TCSTATUS_IXMT) != 0) {
810 * Spin-waiting here can deadlock,
811 * so we queue the message for the target TC.
813 write_tc_c0_tchalt(0);
814 UNLOCK_CORE_PRA();
815 /* Try to reduce redundant timer interrupt messages */
816 if (type == SMTC_CLOCK_TICK) {
817 if (atomic_postincrement(&ipi_timer_latch[cpu])!=0){
818 smtc_ipi_nq(&freeIPIq, pipi);
819 return;
822 smtc_ipi_nq(&IPIQ[cpu], pipi);
823 } else {
824 if (type == SMTC_CLOCK_TICK)
825 atomic_inc(&ipi_timer_latch[cpu]);
826 post_direct_ipi(cpu, pipi);
827 write_tc_c0_tchalt(0);
828 UNLOCK_CORE_PRA();
834 * Send IPI message to Halted TC, TargTC/TargVPE already having been set
836 static void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
838 struct pt_regs *kstack;
839 unsigned long tcstatus;
840 unsigned long tcrestart;
841 extern u32 kernelsp[NR_CPUS];
842 extern void __smtc_ipi_vector(void);
843 //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
845 /* Extract Status, EPC from halted TC */
846 tcstatus = read_tc_c0_tcstatus();
847 tcrestart = read_tc_c0_tcrestart();
848 /* If TCRestart indicates a WAIT instruction, advance the PC */
849 if ((tcrestart & 0x80000000)
850 && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
851 tcrestart += 4;
854 * Save on TC's future kernel stack
856 * CU bit of Status is indicator that TC was
857 * already running on a kernel stack...
859 if (tcstatus & ST0_CU0) {
860 /* Note that this "- 1" is pointer arithmetic */
861 kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
862 } else {
863 kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
866 kstack->cp0_epc = (long)tcrestart;
867 /* Save TCStatus */
868 kstack->cp0_tcstatus = tcstatus;
869 /* Pass token of operation to be performed kernel stack pad area */
870 kstack->pad0[4] = (unsigned long)pipi;
871 /* Pass address of function to be called likewise */
872 kstack->pad0[5] = (unsigned long)&ipi_decode;
873 /* Set interrupt exempt and kernel mode */
874 tcstatus |= TCSTATUS_IXMT;
875 tcstatus &= ~TCSTATUS_TKSU;
876 write_tc_c0_tcstatus(tcstatus);
877 ehb();
878 /* Set TC Restart address to be SMTC IPI vector */
879 write_tc_c0_tcrestart(__smtc_ipi_vector);
882 static void ipi_resched_interrupt(void)
884 /* Return from interrupt should be enough to cause scheduler check */
888 static void ipi_call_interrupt(void)
890 /* Invoke generic function invocation code in smp.c */
891 smp_call_function_interrupt();
894 DECLARE_PER_CPU(struct clock_event_device, smtc_dummy_clockevent_device);
896 void ipi_decode(struct smtc_ipi *pipi)
898 unsigned int cpu = smp_processor_id();
899 struct clock_event_device *cd;
900 void *arg_copy = pipi->arg;
901 int type_copy = pipi->type;
902 int ticks;
904 smtc_ipi_nq(&freeIPIq, pipi);
905 switch (type_copy) {
906 case SMTC_CLOCK_TICK:
907 irq_enter();
908 kstat_this_cpu.irqs[MIPS_CPU_IRQ_BASE + 1]++;
909 cd = &per_cpu(smtc_dummy_clockevent_device, cpu);
910 ticks = atomic_read(&ipi_timer_latch[cpu]);
911 atomic_sub(ticks, &ipi_timer_latch[cpu]);
912 while (ticks) {
913 cd->event_handler(cd);
914 ticks--;
916 irq_exit();
917 break;
919 case LINUX_SMP_IPI:
920 switch ((int)arg_copy) {
921 case SMP_RESCHEDULE_YOURSELF:
922 ipi_resched_interrupt();
923 break;
924 case SMP_CALL_FUNCTION:
925 ipi_call_interrupt();
926 break;
927 default:
928 printk("Impossible SMTC IPI Argument 0x%x\n",
929 (int)arg_copy);
930 break;
932 break;
933 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
934 case IRQ_AFFINITY_IPI:
936 * Accept a "forwarded" interrupt that was initially
937 * taken by a TC who doesn't have affinity for the IRQ.
939 do_IRQ_no_affinity((int)arg_copy);
940 break;
941 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
942 default:
943 printk("Impossible SMTC IPI Type 0x%x\n", type_copy);
944 break;
948 void deferred_smtc_ipi(void)
950 struct smtc_ipi *pipi;
951 unsigned long flags;
952 /* DEBUG */
953 int q = smp_processor_id();
956 * Test is not atomic, but much faster than a dequeue,
957 * and the vast majority of invocations will have a null queue.
959 if (IPIQ[q].head != NULL) {
960 while((pipi = smtc_ipi_dq(&IPIQ[q])) != NULL) {
961 /* ipi_decode() should be called with interrupts off */
962 local_irq_save(flags);
963 ipi_decode(pipi);
964 local_irq_restore(flags);
970 * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
971 * set via cross-VPE MTTR manipulation of the Cause register. It would be
972 * in some regards preferable to have external logic for "doorbell" hardware
973 * interrupts.
976 static int cpu_ipi_irq = MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_IRQ;
978 static irqreturn_t ipi_interrupt(int irq, void *dev_idm)
980 int my_vpe = cpu_data[smp_processor_id()].vpe_id;
981 int my_tc = cpu_data[smp_processor_id()].tc_id;
982 int cpu;
983 struct smtc_ipi *pipi;
984 unsigned long tcstatus;
985 int sent;
986 long flags;
987 unsigned int mtflags;
988 unsigned int vpflags;
991 * So long as cross-VPE interrupts are done via
992 * MFTR/MTTR read-modify-writes of Cause, we need
993 * to stop other VPEs whenever the local VPE does
994 * anything similar.
996 local_irq_save(flags);
997 vpflags = dvpe();
998 clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ);
999 set_c0_status(0x100 << MIPS_CPU_IPI_IRQ);
1000 irq_enable_hazard();
1001 evpe(vpflags);
1002 local_irq_restore(flags);
1005 * Cross-VPE Interrupt handler: Try to directly deliver IPIs
1006 * queued for TCs on this VPE other than the current one.
1007 * Return-from-interrupt should cause us to drain the queue
1008 * for the current TC, so we ought not to have to do it explicitly here.
1011 for_each_online_cpu(cpu) {
1012 if (cpu_data[cpu].vpe_id != my_vpe)
1013 continue;
1015 pipi = smtc_ipi_dq(&IPIQ[cpu]);
1016 if (pipi != NULL) {
1017 if (cpu_data[cpu].tc_id != my_tc) {
1018 sent = 0;
1019 LOCK_MT_PRA();
1020 settc(cpu_data[cpu].tc_id);
1021 write_tc_c0_tchalt(TCHALT_H);
1022 mips_ihb();
1023 tcstatus = read_tc_c0_tcstatus();
1024 if ((tcstatus & TCSTATUS_IXMT) == 0) {
1025 post_direct_ipi(cpu, pipi);
1026 sent = 1;
1028 write_tc_c0_tchalt(0);
1029 UNLOCK_MT_PRA();
1030 if (!sent) {
1031 smtc_ipi_req(&IPIQ[cpu], pipi);
1033 } else {
1035 * ipi_decode() should be called
1036 * with interrupts off
1038 local_irq_save(flags);
1039 ipi_decode(pipi);
1040 local_irq_restore(flags);
1045 return IRQ_HANDLED;
1048 static void ipi_irq_dispatch(void)
1050 do_IRQ(cpu_ipi_irq);
1053 static struct irqaction irq_ipi = {
1054 .handler = ipi_interrupt,
1055 .flags = IRQF_DISABLED,
1056 .name = "SMTC_IPI",
1057 .flags = IRQF_PERCPU
1060 static void setup_cross_vpe_interrupts(unsigned int nvpe)
1062 if (nvpe < 1)
1063 return;
1065 if (!cpu_has_vint)
1066 panic("SMTC Kernel requires Vectored Interrupt support");
1068 set_vi_handler(MIPS_CPU_IPI_IRQ, ipi_irq_dispatch);
1070 setup_irq_smtc(cpu_ipi_irq, &irq_ipi, (0x100 << MIPS_CPU_IPI_IRQ));
1072 set_irq_handler(cpu_ipi_irq, handle_percpu_irq);
1076 * SMTC-specific hacks invoked from elsewhere in the kernel.
1078 * smtc_ipi_replay is called from raw_local_irq_restore which is only ever
1079 * called with interrupts disabled. We do rely on interrupts being disabled
1080 * here because using spin_lock_irqsave()/spin_unlock_irqrestore() would
1081 * result in a recursive call to raw_local_irq_restore().
1084 static void __smtc_ipi_replay(void)
1086 unsigned int cpu = smp_processor_id();
1089 * To the extent that we've ever turned interrupts off,
1090 * we may have accumulated deferred IPIs. This is subtle.
1091 * If we use the smtc_ipi_qdepth() macro, we'll get an
1092 * exact number - but we'll also disable interrupts
1093 * and create a window of failure where a new IPI gets
1094 * queued after we test the depth but before we re-enable
1095 * interrupts. So long as IXMT never gets set, however,
1096 * we should be OK: If we pick up something and dispatch
1097 * it here, that's great. If we see nothing, but concurrent
1098 * with this operation, another TC sends us an IPI, IXMT
1099 * is clear, and we'll handle it as a real pseudo-interrupt
1100 * and not a pseudo-pseudo interrupt.
1102 if (IPIQ[cpu].depth > 0) {
1103 while (1) {
1104 struct smtc_ipi_q *q = &IPIQ[cpu];
1105 struct smtc_ipi *pipi;
1106 extern void self_ipi(struct smtc_ipi *);
1108 spin_lock(&q->lock);
1109 pipi = __smtc_ipi_dq(q);
1110 spin_unlock(&q->lock);
1111 if (!pipi)
1112 break;
1114 self_ipi(pipi);
1115 smtc_cpu_stats[cpu].selfipis++;
1120 void smtc_ipi_replay(void)
1122 raw_local_irq_disable();
1123 __smtc_ipi_replay();
1126 EXPORT_SYMBOL(smtc_ipi_replay);
1128 void smtc_idle_loop_hook(void)
1130 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
1131 int im;
1132 int flags;
1133 int mtflags;
1134 int bit;
1135 int vpe;
1136 int tc;
1137 int hook_ntcs;
1139 * printk within DMT-protected regions can deadlock,
1140 * so buffer diagnostic messages for later output.
1142 char *pdb_msg;
1143 char id_ho_db_msg[768]; /* worst-case use should be less than 700 */
1145 if (atomic_read(&idle_hook_initialized) == 0) { /* fast test */
1146 if (atomic_add_return(1, &idle_hook_initialized) == 1) {
1147 int mvpconf0;
1148 /* Tedious stuff to just do once */
1149 mvpconf0 = read_c0_mvpconf0();
1150 hook_ntcs = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
1151 if (hook_ntcs > NR_CPUS)
1152 hook_ntcs = NR_CPUS;
1153 for (tc = 0; tc < hook_ntcs; tc++) {
1154 tcnoprog[tc] = 0;
1155 clock_hang_reported[tc] = 0;
1157 for (vpe = 0; vpe < 2; vpe++)
1158 for (im = 0; im < 8; im++)
1159 imstuckcount[vpe][im] = 0;
1160 printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs);
1161 atomic_set(&idle_hook_initialized, 1000);
1162 } else {
1163 /* Someone else is initializing in parallel - let 'em finish */
1164 while (atomic_read(&idle_hook_initialized) < 1000)
1169 /* Have we stupidly left IXMT set somewhere? */
1170 if (read_c0_tcstatus() & 0x400) {
1171 write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
1172 ehb();
1173 printk("Dangling IXMT in cpu_idle()\n");
1176 /* Have we stupidly left an IM bit turned off? */
1177 #define IM_LIMIT 2000
1178 local_irq_save(flags);
1179 mtflags = dmt();
1180 pdb_msg = &id_ho_db_msg[0];
1181 im = read_c0_status();
1182 vpe = current_cpu_data.vpe_id;
1183 for (bit = 0; bit < 8; bit++) {
1185 * In current prototype, I/O interrupts
1186 * are masked for VPE > 0
1188 if (vpemask[vpe][bit]) {
1189 if (!(im & (0x100 << bit)))
1190 imstuckcount[vpe][bit]++;
1191 else
1192 imstuckcount[vpe][bit] = 0;
1193 if (imstuckcount[vpe][bit] > IM_LIMIT) {
1194 set_c0_status(0x100 << bit);
1195 ehb();
1196 imstuckcount[vpe][bit] = 0;
1197 pdb_msg += sprintf(pdb_msg,
1198 "Dangling IM %d fixed for VPE %d\n", bit,
1199 vpe);
1205 * Now that we limit outstanding timer IPIs, check for hung TC
1207 for (tc = 0; tc < NR_CPUS; tc++) {
1208 /* Don't check ourself - we'll dequeue IPIs just below */
1209 if ((tc != smp_processor_id()) &&
1210 atomic_read(&ipi_timer_latch[tc]) > timerq_limit) {
1211 if (clock_hang_reported[tc] == 0) {
1212 pdb_msg += sprintf(pdb_msg,
1213 "TC %d looks hung with timer latch at %d\n",
1214 tc, atomic_read(&ipi_timer_latch[tc]));
1215 clock_hang_reported[tc]++;
1219 emt(mtflags);
1220 local_irq_restore(flags);
1221 if (pdb_msg != &id_ho_db_msg[0])
1222 printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg);
1223 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
1226 * Replay any accumulated deferred IPIs. If "Instant Replay"
1227 * is in use, there should never be any.
1229 #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
1231 unsigned long flags;
1233 local_irq_save(flags);
1234 __smtc_ipi_replay();
1235 local_irq_restore(flags);
1237 #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
1240 void smtc_soft_dump(void)
1242 int i;
1244 printk("Counter Interrupts taken per CPU (TC)\n");
1245 for (i=0; i < NR_CPUS; i++) {
1246 printk("%d: %ld\n", i, smtc_cpu_stats[i].timerints);
1248 printk("Self-IPI invocations:\n");
1249 for (i=0; i < NR_CPUS; i++) {
1250 printk("%d: %ld\n", i, smtc_cpu_stats[i].selfipis);
1252 smtc_ipi_qdump();
1253 printk("Timer IPI Backlogs:\n");
1254 for (i=0; i < NR_CPUS; i++) {
1255 printk("%d: %d\n", i, atomic_read(&ipi_timer_latch[i]));
1257 printk("%d Recoveries of \"stolen\" FPU\n",
1258 atomic_read(&smtc_fpu_recoveries));
1263 * TLB management routines special to SMTC
1266 void smtc_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
1268 unsigned long flags, mtflags, tcstat, prevhalt, asid;
1269 int tlb, i;
1272 * It would be nice to be able to use a spinlock here,
1273 * but this is invoked from within TLB flush routines
1274 * that protect themselves with DVPE, so if a lock is
1275 * held by another TC, it'll never be freed.
1277 * DVPE/DMT must not be done with interrupts enabled,
1278 * so even so most callers will already have disabled
1279 * them, let's be really careful...
1282 local_irq_save(flags);
1283 if (smtc_status & SMTC_TLB_SHARED) {
1284 mtflags = dvpe();
1285 tlb = 0;
1286 } else {
1287 mtflags = dmt();
1288 tlb = cpu_data[cpu].vpe_id;
1290 asid = asid_cache(cpu);
1292 do {
1293 if (!((asid += ASID_INC) & ASID_MASK) ) {
1294 if (cpu_has_vtag_icache)
1295 flush_icache_all();
1296 /* Traverse all online CPUs (hack requires contigous range) */
1297 for_each_online_cpu(i) {
1299 * We don't need to worry about our own CPU, nor those of
1300 * CPUs who don't share our TLB.
1302 if ((i != smp_processor_id()) &&
1303 ((smtc_status & SMTC_TLB_SHARED) ||
1304 (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))) {
1305 settc(cpu_data[i].tc_id);
1306 prevhalt = read_tc_c0_tchalt() & TCHALT_H;
1307 if (!prevhalt) {
1308 write_tc_c0_tchalt(TCHALT_H);
1309 mips_ihb();
1311 tcstat = read_tc_c0_tcstatus();
1312 smtc_live_asid[tlb][(tcstat & ASID_MASK)] |= (asiduse)(0x1 << i);
1313 if (!prevhalt)
1314 write_tc_c0_tchalt(0);
1317 if (!asid) /* fix version if needed */
1318 asid = ASID_FIRST_VERSION;
1319 local_flush_tlb_all(); /* start new asid cycle */
1321 } while (smtc_live_asid[tlb][(asid & ASID_MASK)]);
1324 * SMTC shares the TLB within VPEs and possibly across all VPEs.
1326 for_each_online_cpu(i) {
1327 if ((smtc_status & SMTC_TLB_SHARED) ||
1328 (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
1329 cpu_context(i, mm) = asid_cache(i) = asid;
1332 if (smtc_status & SMTC_TLB_SHARED)
1333 evpe(mtflags);
1334 else
1335 emt(mtflags);
1336 local_irq_restore(flags);
1340 * Invoked from macros defined in mmu_context.h
1341 * which must already have disabled interrupts
1342 * and done a DVPE or DMT as appropriate.
1345 void smtc_flush_tlb_asid(unsigned long asid)
1347 int entry;
1348 unsigned long ehi;
1350 entry = read_c0_wired();
1352 /* Traverse all non-wired entries */
1353 while (entry < current_cpu_data.tlbsize) {
1354 write_c0_index(entry);
1355 ehb();
1356 tlb_read();
1357 ehb();
1358 ehi = read_c0_entryhi();
1359 if ((ehi & ASID_MASK) == asid) {
1361 * Invalidate only entries with specified ASID,
1362 * makiing sure all entries differ.
1364 write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
1365 write_c0_entrylo0(0);
1366 write_c0_entrylo1(0);
1367 mtc0_tlbw_hazard();
1368 tlb_write_indexed();
1370 entry++;
1372 write_c0_index(PARKED_INDEX);
1373 tlbw_use_hazard();
1377 * Support for single-threading cache flush operations.
1380 static int halt_state_save[NR_CPUS];
1383 * To really, really be sure that nothing is being done
1384 * by other TCs, halt them all. This code assumes that
1385 * a DVPE has already been done, so while their Halted
1386 * state is theoretically architecturally unstable, in
1387 * practice, it's not going to change while we're looking
1388 * at it.
1391 void smtc_cflush_lockdown(void)
1393 int cpu;
1395 for_each_online_cpu(cpu) {
1396 if (cpu != smp_processor_id()) {
1397 settc(cpu_data[cpu].tc_id);
1398 halt_state_save[cpu] = read_tc_c0_tchalt();
1399 write_tc_c0_tchalt(TCHALT_H);
1402 mips_ihb();
1405 /* It would be cheating to change the cpu_online states during a flush! */
1407 void smtc_cflush_release(void)
1409 int cpu;
1412 * Start with a hazard barrier to ensure
1413 * that all CACHE ops have played through.
1415 mips_ihb();
1417 for_each_online_cpu(cpu) {
1418 if (cpu != smp_processor_id()) {
1419 settc(cpu_data[cpu].tc_id);
1420 write_tc_c0_tchalt(halt_state_save[cpu]);
1423 mips_ihb();