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>
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>
18 #include <asm/mmu_context.h>
20 #include <asm/mipsregs.h>
21 #include <asm/cacheflush.h>
23 #include <asm/addrspace.h>
25 #include <asm/smtc_ipi.h>
26 #include <asm/smtc_proc.h>
29 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
30 * in do_IRQ. These are passed in setup_irq_smtc() and stored
33 unsigned long irq_hwmask
[NR_IRQS
];
35 #define LOCK_MT_PRA() \
36 local_irq_save(flags); \
39 #define UNLOCK_MT_PRA() \
41 local_irq_restore(flags)
43 #define LOCK_CORE_PRA() \
44 local_irq_save(flags); \
47 #define UNLOCK_CORE_PRA() \
49 local_irq_restore(flags)
52 * Data structures purely associated with SMTC parallelism
57 * Table for tracking ASIDs whose lifetime is prolonged.
60 asiduse smtc_live_asid
[MAX_SMTC_TLBS
][MAX_SMTC_ASIDS
];
63 * Clock interrupt "latch" buffers, per "CPU"
66 static atomic_t ipi_timer_latch
[NR_CPUS
];
69 * Number of InterProcessor Interupt (IPI) message buffers to allocate
72 #define IPIBUF_PER_CPU 4
74 static struct smtc_ipi_q IPIQ
[NR_CPUS
];
75 static struct smtc_ipi_q freeIPIq
;
78 /* Forward declarations */
80 void ipi_decode(struct smtc_ipi
*);
81 static void post_direct_ipi(int cpu
, struct smtc_ipi
*pipi
);
82 static void setup_cross_vpe_interrupts(unsigned int nvpe
);
83 void init_smtc_stats(void);
85 /* Global SMTC Status */
87 unsigned int smtc_status
= 0;
89 /* Boot command line configuration overrides */
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
ipibufs(char *str
)
98 get_option(&str
, &ipibuffers
);
102 static int __init
stlb_disable(char *s
)
108 static int __init
asidmask_set(char *str
)
110 get_option(&str
, &asidmask
);
120 smtc_asid_mask
= (unsigned long)asidmask
;
123 printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask
);
128 __setup("ipibufs=", ipibufs
);
129 __setup("nostlb", stlb_disable
);
130 __setup("asidmask=", asidmask_set
);
132 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
134 static int hang_trig
= 0;
136 static int __init
hangtrig_enable(char *s
)
143 __setup("hangtrig", hangtrig_enable
);
145 #define DEFAULT_BLOCKED_IPI_LIMIT 32
147 static int timerq_limit
= DEFAULT_BLOCKED_IPI_LIMIT
;
149 static int __init
tintq(char *str
)
151 get_option(&str
, &timerq_limit
);
155 __setup("tintq=", tintq
);
157 static int imstuckcount
[2][8];
158 /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
159 static int vpemask
[2][8] = {
160 {0, 0, 1, 0, 0, 0, 0, 1},
161 {0, 0, 0, 0, 0, 0, 0, 1}
163 int tcnoprog
[NR_CPUS
];
164 static atomic_t idle_hook_initialized
= {0};
165 static int clock_hang_reported
[NR_CPUS
];
167 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
169 /* Initialize shared TLB - the should probably migrate to smtc_setup_cpus() */
171 void __init
sanitize_tlb_entries(void)
173 printk("Deprecated sanitize_tlb_entries() invoked\n");
178 * Configure shared TLB - VPC configuration bit must be set by caller
181 static void smtc_configure_tlb(void)
184 unsigned long mvpconf0
;
185 unsigned long config1val
;
187 /* Set up ASID preservation table */
188 for (vpes
=0; vpes
<MAX_SMTC_TLBS
; vpes
++) {
189 for(i
= 0; i
< MAX_SMTC_ASIDS
; i
++) {
190 smtc_live_asid
[vpes
][i
] = 0;
193 mvpconf0
= read_c0_mvpconf0();
195 if ((vpes
= ((mvpconf0
& MVPCONF0_PVPE
)
196 >> MVPCONF0_PVPE_SHIFT
) + 1) > 1) {
197 /* If we have multiple VPEs, try to share the TLB */
198 if ((mvpconf0
& MVPCONF0_TLBS
) && !nostlb
) {
200 * If TLB sizing is programmable, shared TLB
201 * size is the total available complement.
202 * Otherwise, we have to take the sum of all
203 * static VPE TLB entries.
205 if ((tlbsiz
= ((mvpconf0
& MVPCONF0_PTLBE
)
206 >> MVPCONF0_PTLBE_SHIFT
)) == 0) {
208 * If there's more than one VPE, there had better
209 * be more than one TC, because we need one to bind
210 * to each VPE in turn to be able to read
211 * its configuration state!
214 /* Stop the TC from doing anything foolish */
215 write_tc_c0_tchalt(TCHALT_H
);
217 /* No need to un-Halt - that happens later anyway */
218 for (i
=0; i
< vpes
; i
++) {
219 write_tc_c0_tcbind(i
);
221 * To be 100% sure we're really getting the right
222 * information, we exit the configuration state
223 * and do an IHB after each rebinding.
226 read_c0_mvpcontrol() & ~ MVPCONTROL_VPC
);
229 * Only count if the MMU Type indicated is TLB
231 if (((read_vpe_c0_config() & MIPS_CONF_MT
) >> 7) == 1) {
232 config1val
= read_vpe_c0_config1();
233 tlbsiz
+= ((config1val
>> 25) & 0x3f) + 1;
236 /* Put core back in configuration state */
238 read_c0_mvpcontrol() | MVPCONTROL_VPC
);
242 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB
);
246 * Setup kernel data structures to use software total,
247 * rather than read the per-VPE Config1 value. The values
248 * for "CPU 0" gets copied to all the other CPUs as part
249 * of their initialization in smtc_cpu_setup().
252 /* MIPS32 limits TLB indices to 64 */
255 cpu_data
[0].tlbsize
= current_cpu_data
.tlbsize
= tlbsiz
;
256 smtc_status
|= SMTC_TLB_SHARED
;
257 local_flush_tlb_all();
259 printk("TLB of %d entry pairs shared by %d VPEs\n",
262 printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
269 * Incrementally build the CPU map out of constituent MIPS MT cores,
270 * using the specified available VPEs and TCs. Plaform code needs
271 * to ensure that each MIPS MT core invokes this routine on reset,
274 * This version of the build_cpu_map and prepare_cpus routines assumes
275 * that *all* TCs of a MIPS MT core will be used for Linux, and that
276 * they will be spread across *all* available VPEs (to minimise the
277 * loss of efficiency due to exception service serialization).
278 * An improved version would pick up configuration information and
279 * possibly leave some TCs/VPEs as "slave" processors.
281 * Use c0_MVPConf0 to find out how many TCs are available, setting up
282 * phys_cpu_present_map and the logical/physical mappings.
285 int __init
mipsmt_build_cpu_map(int start_cpu_slot
)
290 * The CPU map isn't actually used for anything at this point,
291 * so it's not clear what else we should do apart from set
292 * everything up so that "logical" = "physical".
294 ntcs
= ((read_c0_mvpconf0() & MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
295 for (i
=start_cpu_slot
; i
<NR_CPUS
&& i
<ntcs
; i
++) {
296 cpu_set(i
, phys_cpu_present_map
);
297 __cpu_number_map
[i
] = i
;
298 __cpu_logical_map
[i
] = i
;
300 #ifdef CONFIG_MIPS_MT_FPAFF
301 /* Initialize map of CPUs with FPUs */
302 cpus_clear(mt_fpu_cpumask
);
305 /* One of those TC's is the one booting, and not a secondary... */
306 printk("%i available secondary CPU TC(s)\n", i
- 1);
312 * Common setup before any secondaries are started
313 * Make sure all CPU's are in a sensible state before we boot any of the
316 * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
317 * as possible across the available VPEs.
320 static void smtc_tc_setup(int vpe
, int tc
, int cpu
)
323 write_tc_c0_tchalt(TCHALT_H
);
325 write_tc_c0_tcstatus((read_tc_c0_tcstatus()
326 & ~(TCSTATUS_TKSU
| TCSTATUS_DA
| TCSTATUS_IXMT
))
328 write_tc_c0_tccontext(0);
330 write_tc_c0_tcbind(vpe
);
331 /* In general, all TCs should have the same cpu_data indications */
332 memcpy(&cpu_data
[cpu
], &cpu_data
[0], sizeof(struct cpuinfo_mips
));
333 /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
334 if (cpu_data
[0].cputype
== CPU_34K
)
335 cpu_data
[cpu
].options
&= ~MIPS_CPU_FPU
;
336 cpu_data
[cpu
].vpe_id
= vpe
;
337 cpu_data
[cpu
].tc_id
= tc
;
341 void mipsmt_prepare_cpus(void)
343 int i
, vpe
, tc
, ntc
, nvpe
, tcpervpe
, slop
, cpu
;
347 struct smtc_ipi
*pipi
;
349 /* disable interrupts so we can disable MT */
350 local_irq_save(flags
);
351 /* disable MT so we can configure */
355 spin_lock_init(&freeIPIq
.lock
);
358 * We probably don't have as many VPEs as we do SMP "CPUs",
359 * but it's possible - and in any case we'll never use more!
361 for (i
=0; i
<NR_CPUS
; i
++) {
362 IPIQ
[i
].head
= IPIQ
[i
].tail
= NULL
;
363 spin_lock_init(&IPIQ
[i
].lock
);
365 atomic_set(&ipi_timer_latch
[i
], 0);
368 /* cpu_data index starts at zero */
370 cpu_data
[cpu
].vpe_id
= 0;
371 cpu_data
[cpu
].tc_id
= 0;
374 /* Report on boot-time options */
375 mips_mt_set_cpuoptions();
377 printk("Limit of %d VPEs set\n", vpelimit
);
379 printk("Limit of %d TCs set\n", tclimit
);
381 printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
384 printk("ASID mask value override to 0x%x\n", asidmask
);
387 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
389 printk("Logic Analyser Trigger on suspected TC hang\n");
390 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
392 /* Put MVPE's into 'configuration state' */
393 write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC
);
395 val
= read_c0_mvpconf0();
396 nvpe
= ((val
& MVPCONF0_PVPE
) >> MVPCONF0_PVPE_SHIFT
) + 1;
397 if (vpelimit
> 0 && nvpe
> vpelimit
)
399 ntc
= ((val
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
402 if (tclimit
> 0 && ntc
> tclimit
)
404 tcpervpe
= ntc
/ nvpe
;
405 slop
= ntc
% nvpe
; /* Residual TCs, < NVPE */
407 /* Set up shared TLB */
408 smtc_configure_tlb();
410 for (tc
= 0, vpe
= 0 ; (vpe
< nvpe
) && (tc
< ntc
) ; vpe
++) {
415 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP
);
418 printk("VPE %d: TC", vpe
);
419 for (i
= 0; i
< tcpervpe
; i
++) {
421 * TC 0 is bound to VPE 0 at reset,
422 * and is presumably executing this
423 * code. Leave it alone!
426 smtc_tc_setup(vpe
, tc
, cpu
);
434 smtc_tc_setup(vpe
, tc
, cpu
);
443 * Clear any stale software interrupts from VPE's Cause
445 write_vpe_c0_cause(0);
448 * Clear ERL/EXL of VPEs other than 0
449 * and set restricted interrupt enable/mask.
451 write_vpe_c0_status((read_vpe_c0_status()
452 & ~(ST0_BEV
| ST0_ERL
| ST0_EXL
| ST0_IM
))
453 | (STATUSF_IP0
| STATUSF_IP1
| STATUSF_IP7
456 * set config to be the same as vpe0,
457 * particularly kseg0 coherency alg
459 write_vpe_c0_config(read_c0_config());
460 /* Clear any pending timer interrupt */
461 write_vpe_c0_compare(0);
462 /* Propagate Config7 */
463 write_vpe_c0_config7(read_c0_config7());
464 write_vpe_c0_count(read_c0_count());
466 /* enable multi-threading within VPE */
467 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE
);
469 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA
);
473 * Pull any physically present but unused TCs out of circulation.
475 while (tc
< (((val
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1)) {
476 cpu_clear(tc
, phys_cpu_present_map
);
477 cpu_clear(tc
, cpu_present_map
);
481 /* release config state */
482 write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC
);
486 /* Set up coprocessor affinity CPU mask(s) */
488 #ifdef CONFIG_MIPS_MT_FPAFF
489 for (tc
= 0; tc
< ntc
; tc
++) {
490 if (cpu_data
[tc
].options
& MIPS_CPU_FPU
)
491 cpu_set(tc
, mt_fpu_cpumask
);
495 /* set up ipi interrupts... */
497 /* If we have multiple VPEs running, set up the cross-VPE interrupt */
499 setup_cross_vpe_interrupts(nvpe
);
501 /* Set up queue of free IPI "messages". */
502 nipi
= NR_CPUS
* IPIBUF_PER_CPU
;
506 pipi
= kmalloc(nipi
*sizeof(struct smtc_ipi
), GFP_KERNEL
);
508 panic("kmalloc of IPI message buffers failed\n");
510 printk("IPI buffer pool of %d buffers\n", nipi
);
511 for (i
= 0; i
< nipi
; i
++) {
512 smtc_ipi_nq(&freeIPIq
, pipi
);
516 /* Arm multithreading and enable other VPEs - but all TCs are Halted */
519 local_irq_restore(flags
);
520 /* Initialize SMTC /proc statistics/diagnostics */
526 * Setup the PC, SP, and GP of a secondary processor and start it
528 * smp_bootstrap is the place to resume from
529 * __KSTK_TOS(idle) is apparently the stack pointer
530 * (unsigned long)idle->thread_info the gp
533 void __cpuinit
smtc_boot_secondary(int cpu
, struct task_struct
*idle
)
535 extern u32 kernelsp
[NR_CPUS
];
540 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
543 settc(cpu_data
[cpu
].tc_id
);
546 write_tc_c0_tcrestart((unsigned long)&smp_bootstrap
);
549 kernelsp
[cpu
] = __KSTK_TOS(idle
);
550 write_tc_gpr_sp(__KSTK_TOS(idle
));
553 write_tc_gpr_gp((unsigned long)task_thread_info(idle
));
555 smtc_status
|= SMTC_MTC_ACTIVE
;
556 write_tc_c0_tchalt(0);
557 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
563 void smtc_init_secondary(void)
566 * Start timer on secondary VPEs if necessary.
567 * plat_timer_setup has already have been invoked by init/main
568 * on "boot" TC. Like per_cpu_trap_init() hack, this assumes that
569 * SMTC init code assigns TCs consdecutively and in ascending order
570 * to across available VPEs.
572 if (((read_c0_tcbind() & TCBIND_CURTC
) != 0) &&
573 ((read_c0_tcbind() & TCBIND_CURVPE
)
574 != cpu_data
[smp_processor_id() - 1].vpe_id
)){
575 write_c0_compare(read_c0_count() + mips_hpt_frequency
/HZ
);
581 void smtc_smp_finish(void)
583 printk("TC %d going on-line as CPU %d\n",
584 cpu_data
[smp_processor_id()].tc_id
, smp_processor_id());
587 void smtc_cpus_done(void)
592 * Support for SMTC-optimized driver IRQ registration
596 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
597 * in do_IRQ. These are passed in setup_irq_smtc() and stored
601 int setup_irq_smtc(unsigned int irq
, struct irqaction
* new,
602 unsigned long hwmask
)
604 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
605 unsigned int vpe
= current_cpu_data
.vpe_id
;
607 vpemask
[vpe
][irq
- MIPS_CPU_IRQ_BASE
] = 1;
609 irq_hwmask
[irq
] = hwmask
;
611 return setup_irq(irq
, new);
614 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
616 * Support for IRQ affinity to TCs
619 void smtc_set_irq_affinity(unsigned int irq
, cpumask_t affinity
)
622 * If a "fast path" cache of quickly decodable affinity state
623 * is maintained, this is where it gets done, on a call up
624 * from the platform affinity code.
628 void smtc_forward_irq(unsigned int irq
)
633 * OK wise guy, now figure out how to get the IRQ
634 * to be serviced on an authorized "CPU".
636 * Ideally, to handle the situation where an IRQ has multiple
637 * eligible CPUS, we would maintain state per IRQ that would
638 * allow a fair distribution of service requests. Since the
639 * expected use model is any-or-only-one, for simplicity
640 * and efficiency, we just pick the easiest one to find.
643 target
= first_cpu(irq_desc
[irq
].affinity
);
646 * We depend on the platform code to have correctly processed
647 * IRQ affinity change requests to ensure that the IRQ affinity
648 * mask has been purged of bits corresponding to nonexistent and
649 * offline "CPUs", and to TCs bound to VPEs other than the VPE
650 * connected to the physical interrupt input for the interrupt
651 * in question. Otherwise we have a nasty problem with interrupt
652 * mask management. This is best handled in non-performance-critical
653 * platform IRQ affinity setting code, to minimize interrupt-time
657 /* If no one is eligible, service locally */
658 if (target
>= NR_CPUS
) {
659 do_IRQ_no_affinity(irq
);
663 smtc_send_ipi(target
, IRQ_AFFINITY_IPI
, irq
);
666 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
669 * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
670 * Within a VPE one TC can interrupt another by different approaches.
671 * The easiest to get right would probably be to make all TCs except
672 * the target IXMT and set a software interrupt, but an IXMT-based
673 * scheme requires that a handler must run before a new IPI could
674 * be sent, which would break the "broadcast" loops in MIPS MT.
675 * A more gonzo approach within a VPE is to halt the TC, extract
676 * its Restart, Status, and a couple of GPRs, and program the Restart
677 * address to emulate an interrupt.
679 * Within a VPE, one can be confident that the target TC isn't in
680 * a critical EXL state when halted, since the write to the Halt
681 * register could not have issued on the writing thread if the
682 * halting thread had EXL set. So k0 and k1 of the target TC
683 * can be used by the injection code. Across VPEs, one can't
684 * be certain that the target TC isn't in a critical exception
685 * state. So we try a two-step process of sending a software
686 * interrupt to the target VPE, which either handles the event
687 * itself (if it was the target) or injects the event within
691 static void smtc_ipi_qdump(void)
695 for (i
= 0; i
< NR_CPUS
;i
++) {
696 printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
697 i
, (unsigned)IPIQ
[i
].head
, (unsigned)IPIQ
[i
].tail
,
703 * The standard atomic.h primitives don't quite do what we want
704 * here: We need an atomic add-and-return-previous-value (which
705 * could be done with atomic_add_return and a decrement) and an
706 * atomic set/zero-and-return-previous-value (which can't really
707 * be done with the atomic.h primitives). And since this is
708 * MIPS MT, we can assume that we have LL/SC.
710 static inline int atomic_postincrement(atomic_t
*v
)
712 unsigned long result
;
716 __asm__
__volatile__(
722 : "=&r" (result
), "=&r" (temp
), "=m" (v
->counter
)
729 void smtc_send_ipi(int cpu
, int type
, unsigned int action
)
732 struct smtc_ipi
*pipi
;
736 if (cpu
== smp_processor_id()) {
737 printk("Cannot Send IPI to self!\n");
740 /* Set up a descriptor, to be delivered either promptly or queued */
741 pipi
= smtc_ipi_dq(&freeIPIq
);
744 mips_mt_regdump(dvpe());
745 panic("IPI Msg. Buffers Depleted\n");
748 pipi
->arg
= (void *)action
;
750 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
751 if (type
== SMTC_CLOCK_TICK
)
752 atomic_inc(&ipi_timer_latch
[cpu
]);
753 /* If not on same VPE, enqueue and send cross-VPE interupt */
754 smtc_ipi_nq(&IPIQ
[cpu
], pipi
);
756 settc(cpu_data
[cpu
].tc_id
);
757 write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1
);
761 * Not sufficient to do a LOCK_MT_PRA (dmt) here,
762 * since ASID shootdown on the other VPE may
763 * collide with this operation.
766 settc(cpu_data
[cpu
].tc_id
);
767 /* Halt the targeted TC */
768 write_tc_c0_tchalt(TCHALT_H
);
772 * Inspect TCStatus - if IXMT is set, we have to queue
773 * a message. Otherwise, we set up the "interrupt"
776 tcstatus
= read_tc_c0_tcstatus();
778 if ((tcstatus
& TCSTATUS_IXMT
) != 0) {
780 * Spin-waiting here can deadlock,
781 * so we queue the message for the target TC.
783 write_tc_c0_tchalt(0);
785 /* Try to reduce redundant timer interrupt messages */
786 if (type
== SMTC_CLOCK_TICK
) {
787 if (atomic_postincrement(&ipi_timer_latch
[cpu
])!=0){
788 smtc_ipi_nq(&freeIPIq
, pipi
);
792 smtc_ipi_nq(&IPIQ
[cpu
], pipi
);
794 if (type
== SMTC_CLOCK_TICK
)
795 atomic_inc(&ipi_timer_latch
[cpu
]);
796 post_direct_ipi(cpu
, pipi
);
797 write_tc_c0_tchalt(0);
804 * Send IPI message to Halted TC, TargTC/TargVPE already having been set
806 static void post_direct_ipi(int cpu
, struct smtc_ipi
*pipi
)
808 struct pt_regs
*kstack
;
809 unsigned long tcstatus
;
810 unsigned long tcrestart
;
811 extern u32 kernelsp
[NR_CPUS
];
812 extern void __smtc_ipi_vector(void);
813 //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
815 /* Extract Status, EPC from halted TC */
816 tcstatus
= read_tc_c0_tcstatus();
817 tcrestart
= read_tc_c0_tcrestart();
818 /* If TCRestart indicates a WAIT instruction, advance the PC */
819 if ((tcrestart
& 0x80000000)
820 && ((*(unsigned int *)tcrestart
& 0xfe00003f) == 0x42000020)) {
824 * Save on TC's future kernel stack
826 * CU bit of Status is indicator that TC was
827 * already running on a kernel stack...
829 if (tcstatus
& ST0_CU0
) {
830 /* Note that this "- 1" is pointer arithmetic */
831 kstack
= ((struct pt_regs
*)read_tc_gpr_sp()) - 1;
833 kstack
= ((struct pt_regs
*)kernelsp
[cpu
]) - 1;
836 kstack
->cp0_epc
= (long)tcrestart
;
838 kstack
->cp0_tcstatus
= tcstatus
;
839 /* Pass token of operation to be performed kernel stack pad area */
840 kstack
->pad0
[4] = (unsigned long)pipi
;
841 /* Pass address of function to be called likewise */
842 kstack
->pad0
[5] = (unsigned long)&ipi_decode
;
843 /* Set interrupt exempt and kernel mode */
844 tcstatus
|= TCSTATUS_IXMT
;
845 tcstatus
&= ~TCSTATUS_TKSU
;
846 write_tc_c0_tcstatus(tcstatus
);
848 /* Set TC Restart address to be SMTC IPI vector */
849 write_tc_c0_tcrestart(__smtc_ipi_vector
);
852 static void ipi_resched_interrupt(void)
854 /* Return from interrupt should be enough to cause scheduler check */
858 static void ipi_call_interrupt(void)
860 /* Invoke generic function invocation code in smp.c */
861 smp_call_function_interrupt();
864 DECLARE_PER_CPU(struct clock_event_device
, smtc_dummy_clockevent_device
);
866 void ipi_decode(struct smtc_ipi
*pipi
)
868 unsigned int cpu
= smp_processor_id();
869 struct clock_event_device
*cd
;
870 void *arg_copy
= pipi
->arg
;
871 int type_copy
= pipi
->type
;
874 smtc_ipi_nq(&freeIPIq
, pipi
);
876 case SMTC_CLOCK_TICK
:
878 kstat_this_cpu
.irqs
[MIPS_CPU_IRQ_BASE
+ 1]++;
879 cd
= &per_cpu(smtc_dummy_clockevent_device
, cpu
);
880 ticks
= atomic_read(&ipi_timer_latch
[cpu
]);
881 atomic_sub(ticks
, &ipi_timer_latch
[cpu
]);
883 cd
->event_handler(cd
);
890 switch ((int)arg_copy
) {
891 case SMP_RESCHEDULE_YOURSELF
:
892 ipi_resched_interrupt();
894 case SMP_CALL_FUNCTION
:
895 ipi_call_interrupt();
898 printk("Impossible SMTC IPI Argument 0x%x\n",
903 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
904 case IRQ_AFFINITY_IPI
:
906 * Accept a "forwarded" interrupt that was initially
907 * taken by a TC who doesn't have affinity for the IRQ.
909 do_IRQ_no_affinity((int)arg_copy
);
911 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
913 printk("Impossible SMTC IPI Type 0x%x\n", type_copy
);
918 void deferred_smtc_ipi(void)
920 struct smtc_ipi
*pipi
;
923 int q
= smp_processor_id();
926 * Test is not atomic, but much faster than a dequeue,
927 * and the vast majority of invocations will have a null queue.
929 if (IPIQ
[q
].head
!= NULL
) {
930 while((pipi
= smtc_ipi_dq(&IPIQ
[q
])) != NULL
) {
931 /* ipi_decode() should be called with interrupts off */
932 local_irq_save(flags
);
934 local_irq_restore(flags
);
940 * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
941 * set via cross-VPE MTTR manipulation of the Cause register. It would be
942 * in some regards preferable to have external logic for "doorbell" hardware
946 static int cpu_ipi_irq
= MIPS_CPU_IRQ_BASE
+ MIPS_CPU_IPI_IRQ
;
948 static irqreturn_t
ipi_interrupt(int irq
, void *dev_idm
)
950 int my_vpe
= cpu_data
[smp_processor_id()].vpe_id
;
951 int my_tc
= cpu_data
[smp_processor_id()].tc_id
;
953 struct smtc_ipi
*pipi
;
954 unsigned long tcstatus
;
957 unsigned int mtflags
;
958 unsigned int vpflags
;
961 * So long as cross-VPE interrupts are done via
962 * MFTR/MTTR read-modify-writes of Cause, we need
963 * to stop other VPEs whenever the local VPE does
966 local_irq_save(flags
);
968 clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ
);
969 set_c0_status(0x100 << MIPS_CPU_IPI_IRQ
);
972 local_irq_restore(flags
);
975 * Cross-VPE Interrupt handler: Try to directly deliver IPIs
976 * queued for TCs on this VPE other than the current one.
977 * Return-from-interrupt should cause us to drain the queue
978 * for the current TC, so we ought not to have to do it explicitly here.
981 for_each_online_cpu(cpu
) {
982 if (cpu_data
[cpu
].vpe_id
!= my_vpe
)
985 pipi
= smtc_ipi_dq(&IPIQ
[cpu
]);
987 if (cpu_data
[cpu
].tc_id
!= my_tc
) {
990 settc(cpu_data
[cpu
].tc_id
);
991 write_tc_c0_tchalt(TCHALT_H
);
993 tcstatus
= read_tc_c0_tcstatus();
994 if ((tcstatus
& TCSTATUS_IXMT
) == 0) {
995 post_direct_ipi(cpu
, pipi
);
998 write_tc_c0_tchalt(0);
1001 smtc_ipi_req(&IPIQ
[cpu
], pipi
);
1005 * ipi_decode() should be called
1006 * with interrupts off
1008 local_irq_save(flags
);
1010 local_irq_restore(flags
);
1018 static void ipi_irq_dispatch(void)
1020 do_IRQ(cpu_ipi_irq
);
1023 static struct irqaction irq_ipi
= {
1024 .handler
= ipi_interrupt
,
1025 .flags
= IRQF_DISABLED
,
1027 .flags
= IRQF_PERCPU
1030 static void setup_cross_vpe_interrupts(unsigned int nvpe
)
1036 panic("SMTC Kernel requires Vectored Interupt support");
1038 set_vi_handler(MIPS_CPU_IPI_IRQ
, ipi_irq_dispatch
);
1040 setup_irq_smtc(cpu_ipi_irq
, &irq_ipi
, (0x100 << MIPS_CPU_IPI_IRQ
));
1042 set_irq_handler(cpu_ipi_irq
, handle_percpu_irq
);
1046 * SMTC-specific hacks invoked from elsewhere in the kernel.
1048 * smtc_ipi_replay is called from raw_local_irq_restore which is only ever
1049 * called with interrupts disabled. We do rely on interrupts being disabled
1050 * here because using spin_lock_irqsave()/spin_unlock_irqrestore() would
1051 * result in a recursive call to raw_local_irq_restore().
1054 static void __smtc_ipi_replay(void)
1056 unsigned int cpu
= smp_processor_id();
1059 * To the extent that we've ever turned interrupts off,
1060 * we may have accumulated deferred IPIs. This is subtle.
1061 * If we use the smtc_ipi_qdepth() macro, we'll get an
1062 * exact number - but we'll also disable interrupts
1063 * and create a window of failure where a new IPI gets
1064 * queued after we test the depth but before we re-enable
1065 * interrupts. So long as IXMT never gets set, however,
1066 * we should be OK: If we pick up something and dispatch
1067 * it here, that's great. If we see nothing, but concurrent
1068 * with this operation, another TC sends us an IPI, IXMT
1069 * is clear, and we'll handle it as a real pseudo-interrupt
1070 * and not a pseudo-pseudo interrupt.
1072 if (IPIQ
[cpu
].depth
> 0) {
1074 struct smtc_ipi_q
*q
= &IPIQ
[cpu
];
1075 struct smtc_ipi
*pipi
;
1076 extern void self_ipi(struct smtc_ipi
*);
1078 spin_lock(&q
->lock
);
1079 pipi
= __smtc_ipi_dq(q
);
1080 spin_unlock(&q
->lock
);
1085 smtc_cpu_stats
[cpu
].selfipis
++;
1090 void smtc_ipi_replay(void)
1092 raw_local_irq_disable();
1093 __smtc_ipi_replay();
1096 EXPORT_SYMBOL(smtc_ipi_replay
);
1098 void smtc_idle_loop_hook(void)
1100 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
1109 * printk within DMT-protected regions can deadlock,
1110 * so buffer diagnostic messages for later output.
1113 char id_ho_db_msg
[768]; /* worst-case use should be less than 700 */
1115 if (atomic_read(&idle_hook_initialized
) == 0) { /* fast test */
1116 if (atomic_add_return(1, &idle_hook_initialized
) == 1) {
1118 /* Tedious stuff to just do once */
1119 mvpconf0
= read_c0_mvpconf0();
1120 hook_ntcs
= ((mvpconf0
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
1121 if (hook_ntcs
> NR_CPUS
)
1122 hook_ntcs
= NR_CPUS
;
1123 for (tc
= 0; tc
< hook_ntcs
; tc
++) {
1125 clock_hang_reported
[tc
] = 0;
1127 for (vpe
= 0; vpe
< 2; vpe
++)
1128 for (im
= 0; im
< 8; im
++)
1129 imstuckcount
[vpe
][im
] = 0;
1130 printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs
);
1131 atomic_set(&idle_hook_initialized
, 1000);
1133 /* Someone else is initializing in parallel - let 'em finish */
1134 while (atomic_read(&idle_hook_initialized
) < 1000)
1139 /* Have we stupidly left IXMT set somewhere? */
1140 if (read_c0_tcstatus() & 0x400) {
1141 write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
1143 printk("Dangling IXMT in cpu_idle()\n");
1146 /* Have we stupidly left an IM bit turned off? */
1147 #define IM_LIMIT 2000
1148 local_irq_save(flags
);
1150 pdb_msg
= &id_ho_db_msg
[0];
1151 im
= read_c0_status();
1152 vpe
= current_cpu_data
.vpe_id
;
1153 for (bit
= 0; bit
< 8; bit
++) {
1155 * In current prototype, I/O interrupts
1156 * are masked for VPE > 0
1158 if (vpemask
[vpe
][bit
]) {
1159 if (!(im
& (0x100 << bit
)))
1160 imstuckcount
[vpe
][bit
]++;
1162 imstuckcount
[vpe
][bit
] = 0;
1163 if (imstuckcount
[vpe
][bit
] > IM_LIMIT
) {
1164 set_c0_status(0x100 << bit
);
1166 imstuckcount
[vpe
][bit
] = 0;
1167 pdb_msg
+= sprintf(pdb_msg
,
1168 "Dangling IM %d fixed for VPE %d\n", bit
,
1175 * Now that we limit outstanding timer IPIs, check for hung TC
1177 for (tc
= 0; tc
< NR_CPUS
; tc
++) {
1178 /* Don't check ourself - we'll dequeue IPIs just below */
1179 if ((tc
!= smp_processor_id()) &&
1180 atomic_read(&ipi_timer_latch
[tc
]) > timerq_limit
) {
1181 if (clock_hang_reported
[tc
] == 0) {
1182 pdb_msg
+= sprintf(pdb_msg
,
1183 "TC %d looks hung with timer latch at %d\n",
1184 tc
, atomic_read(&ipi_timer_latch
[tc
]));
1185 clock_hang_reported
[tc
]++;
1190 local_irq_restore(flags
);
1191 if (pdb_msg
!= &id_ho_db_msg
[0])
1192 printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg
);
1193 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
1196 * Replay any accumulated deferred IPIs. If "Instant Replay"
1197 * is in use, there should never be any.
1199 #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
1201 unsigned long flags
;
1203 local_irq_save(flags
);
1204 __smtc_ipi_replay();
1205 local_irq_restore(flags
);
1207 #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
1210 void smtc_soft_dump(void)
1214 printk("Counter Interrupts taken per CPU (TC)\n");
1215 for (i
=0; i
< NR_CPUS
; i
++) {
1216 printk("%d: %ld\n", i
, smtc_cpu_stats
[i
].timerints
);
1218 printk("Self-IPI invocations:\n");
1219 for (i
=0; i
< NR_CPUS
; i
++) {
1220 printk("%d: %ld\n", i
, smtc_cpu_stats
[i
].selfipis
);
1223 printk("Timer IPI Backlogs:\n");
1224 for (i
=0; i
< NR_CPUS
; i
++) {
1225 printk("%d: %d\n", i
, atomic_read(&ipi_timer_latch
[i
]));
1227 printk("%d Recoveries of \"stolen\" FPU\n",
1228 atomic_read(&smtc_fpu_recoveries
));
1233 * TLB management routines special to SMTC
1236 void smtc_get_new_mmu_context(struct mm_struct
*mm
, unsigned long cpu
)
1238 unsigned long flags
, mtflags
, tcstat
, prevhalt
, asid
;
1242 * It would be nice to be able to use a spinlock here,
1243 * but this is invoked from within TLB flush routines
1244 * that protect themselves with DVPE, so if a lock is
1245 * held by another TC, it'll never be freed.
1247 * DVPE/DMT must not be done with interrupts enabled,
1248 * so even so most callers will already have disabled
1249 * them, let's be really careful...
1252 local_irq_save(flags
);
1253 if (smtc_status
& SMTC_TLB_SHARED
) {
1258 tlb
= cpu_data
[cpu
].vpe_id
;
1260 asid
= asid_cache(cpu
);
1263 if (!((asid
+= ASID_INC
) & ASID_MASK
) ) {
1264 if (cpu_has_vtag_icache
)
1266 /* Traverse all online CPUs (hack requires contigous range) */
1267 for_each_online_cpu(i
) {
1269 * We don't need to worry about our own CPU, nor those of
1270 * CPUs who don't share our TLB.
1272 if ((i
!= smp_processor_id()) &&
1273 ((smtc_status
& SMTC_TLB_SHARED
) ||
1274 (cpu_data
[i
].vpe_id
== cpu_data
[cpu
].vpe_id
))) {
1275 settc(cpu_data
[i
].tc_id
);
1276 prevhalt
= read_tc_c0_tchalt() & TCHALT_H
;
1278 write_tc_c0_tchalt(TCHALT_H
);
1281 tcstat
= read_tc_c0_tcstatus();
1282 smtc_live_asid
[tlb
][(tcstat
& ASID_MASK
)] |= (asiduse
)(0x1 << i
);
1284 write_tc_c0_tchalt(0);
1287 if (!asid
) /* fix version if needed */
1288 asid
= ASID_FIRST_VERSION
;
1289 local_flush_tlb_all(); /* start new asid cycle */
1291 } while (smtc_live_asid
[tlb
][(asid
& ASID_MASK
)]);
1294 * SMTC shares the TLB within VPEs and possibly across all VPEs.
1296 for_each_online_cpu(i
) {
1297 if ((smtc_status
& SMTC_TLB_SHARED
) ||
1298 (cpu_data
[i
].vpe_id
== cpu_data
[cpu
].vpe_id
))
1299 cpu_context(i
, mm
) = asid_cache(i
) = asid
;
1302 if (smtc_status
& SMTC_TLB_SHARED
)
1306 local_irq_restore(flags
);
1310 * Invoked from macros defined in mmu_context.h
1311 * which must already have disabled interrupts
1312 * and done a DVPE or DMT as appropriate.
1315 void smtc_flush_tlb_asid(unsigned long asid
)
1320 entry
= read_c0_wired();
1322 /* Traverse all non-wired entries */
1323 while (entry
< current_cpu_data
.tlbsize
) {
1324 write_c0_index(entry
);
1328 ehi
= read_c0_entryhi();
1329 if ((ehi
& ASID_MASK
) == asid
) {
1331 * Invalidate only entries with specified ASID,
1332 * makiing sure all entries differ.
1334 write_c0_entryhi(CKSEG0
+ (entry
<< (PAGE_SHIFT
+ 1)));
1335 write_c0_entrylo0(0);
1336 write_c0_entrylo1(0);
1338 tlb_write_indexed();
1342 write_c0_index(PARKED_INDEX
);
1347 * Support for single-threading cache flush operations.
1350 static int halt_state_save
[NR_CPUS
];
1353 * To really, really be sure that nothing is being done
1354 * by other TCs, halt them all. This code assumes that
1355 * a DVPE has already been done, so while their Halted
1356 * state is theoretically architecturally unstable, in
1357 * practice, it's not going to change while we're looking
1361 void smtc_cflush_lockdown(void)
1365 for_each_online_cpu(cpu
) {
1366 if (cpu
!= smp_processor_id()) {
1367 settc(cpu_data
[cpu
].tc_id
);
1368 halt_state_save
[cpu
] = read_tc_c0_tchalt();
1369 write_tc_c0_tchalt(TCHALT_H
);
1375 /* It would be cheating to change the cpu_online states during a flush! */
1377 void smtc_cflush_release(void)
1382 * Start with a hazard barrier to ensure
1383 * that all CACHE ops have played through.
1387 for_each_online_cpu(cpu
) {
1388 if (cpu
!= smp_processor_id()) {
1389 settc(cpu_data
[cpu
].tc_id
);
1390 write_tc_c0_tchalt(halt_state_save
[cpu
]);