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 */
92 static int ipibuffers
= 0;
93 static int nostlb
= 0;
94 static int asidmask
= 0;
95 unsigned long smtc_asid_mask
= 0xff;
97 static int __init
vpe0tcs(char *str
)
99 get_option(&str
, &vpe0limit
);
104 static int __init
ipibufs(char *str
)
106 get_option(&str
, &ipibuffers
);
110 static int __init
stlb_disable(char *s
)
116 static int __init
asidmask_set(char *str
)
118 get_option(&str
, &asidmask
);
128 smtc_asid_mask
= (unsigned long)asidmask
;
131 printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask
);
136 __setup("vpe0tcs=", vpe0tcs
);
137 __setup("ipibufs=", ipibufs
);
138 __setup("nostlb", stlb_disable
);
139 __setup("asidmask=", asidmask_set
);
141 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
143 static int hang_trig
= 0;
145 static int __init
hangtrig_enable(char *s
)
152 __setup("hangtrig", hangtrig_enable
);
154 #define DEFAULT_BLOCKED_IPI_LIMIT 32
156 static int timerq_limit
= DEFAULT_BLOCKED_IPI_LIMIT
;
158 static int __init
tintq(char *str
)
160 get_option(&str
, &timerq_limit
);
164 __setup("tintq=", tintq
);
166 static int imstuckcount
[2][8];
167 /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
168 static int vpemask
[2][8] = {
169 {0, 0, 1, 0, 0, 0, 0, 1},
170 {0, 0, 0, 0, 0, 0, 0, 1}
172 int tcnoprog
[NR_CPUS
];
173 static atomic_t idle_hook_initialized
= {0};
174 static int clock_hang_reported
[NR_CPUS
];
176 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
178 /* Initialize shared TLB - the should probably migrate to smtc_setup_cpus() */
180 void __init
sanitize_tlb_entries(void)
182 printk("Deprecated sanitize_tlb_entries() invoked\n");
187 * Configure shared TLB - VPC configuration bit must be set by caller
190 static void smtc_configure_tlb(void)
193 unsigned long mvpconf0
;
194 unsigned long config1val
;
196 /* Set up ASID preservation table */
197 for (vpes
=0; vpes
<MAX_SMTC_TLBS
; vpes
++) {
198 for(i
= 0; i
< MAX_SMTC_ASIDS
; i
++) {
199 smtc_live_asid
[vpes
][i
] = 0;
202 mvpconf0
= read_c0_mvpconf0();
204 if ((vpes
= ((mvpconf0
& MVPCONF0_PVPE
)
205 >> MVPCONF0_PVPE_SHIFT
) + 1) > 1) {
206 /* If we have multiple VPEs, try to share the TLB */
207 if ((mvpconf0
& MVPCONF0_TLBS
) && !nostlb
) {
209 * If TLB sizing is programmable, shared TLB
210 * size is the total available complement.
211 * Otherwise, we have to take the sum of all
212 * static VPE TLB entries.
214 if ((tlbsiz
= ((mvpconf0
& MVPCONF0_PTLBE
)
215 >> MVPCONF0_PTLBE_SHIFT
)) == 0) {
217 * If there's more than one VPE, there had better
218 * be more than one TC, because we need one to bind
219 * to each VPE in turn to be able to read
220 * its configuration state!
223 /* Stop the TC from doing anything foolish */
224 write_tc_c0_tchalt(TCHALT_H
);
226 /* No need to un-Halt - that happens later anyway */
227 for (i
=0; i
< vpes
; i
++) {
228 write_tc_c0_tcbind(i
);
230 * To be 100% sure we're really getting the right
231 * information, we exit the configuration state
232 * and do an IHB after each rebinding.
235 read_c0_mvpcontrol() & ~ MVPCONTROL_VPC
);
238 * Only count if the MMU Type indicated is TLB
240 if (((read_vpe_c0_config() & MIPS_CONF_MT
) >> 7) == 1) {
241 config1val
= read_vpe_c0_config1();
242 tlbsiz
+= ((config1val
>> 25) & 0x3f) + 1;
245 /* Put core back in configuration state */
247 read_c0_mvpcontrol() | MVPCONTROL_VPC
);
251 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB
);
255 * Setup kernel data structures to use software total,
256 * rather than read the per-VPE Config1 value. The values
257 * for "CPU 0" gets copied to all the other CPUs as part
258 * of their initialization in smtc_cpu_setup().
261 /* MIPS32 limits TLB indices to 64 */
264 cpu_data
[0].tlbsize
= current_cpu_data
.tlbsize
= tlbsiz
;
265 smtc_status
|= SMTC_TLB_SHARED
;
266 local_flush_tlb_all();
268 printk("TLB of %d entry pairs shared by %d VPEs\n",
271 printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
278 * Incrementally build the CPU map out of constituent MIPS MT cores,
279 * using the specified available VPEs and TCs. Plaform code needs
280 * to ensure that each MIPS MT core invokes this routine on reset,
283 * This version of the build_cpu_map and prepare_cpus routines assumes
284 * that *all* TCs of a MIPS MT core will be used for Linux, and that
285 * they will be spread across *all* available VPEs (to minimise the
286 * loss of efficiency due to exception service serialization).
287 * An improved version would pick up configuration information and
288 * possibly leave some TCs/VPEs as "slave" processors.
290 * Use c0_MVPConf0 to find out how many TCs are available, setting up
291 * phys_cpu_present_map and the logical/physical mappings.
294 int __init
mipsmt_build_cpu_map(int start_cpu_slot
)
299 * The CPU map isn't actually used for anything at this point,
300 * so it's not clear what else we should do apart from set
301 * everything up so that "logical" = "physical".
303 ntcs
= ((read_c0_mvpconf0() & MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
304 for (i
=start_cpu_slot
; i
<NR_CPUS
&& i
<ntcs
; i
++) {
305 cpu_set(i
, phys_cpu_present_map
);
306 __cpu_number_map
[i
] = i
;
307 __cpu_logical_map
[i
] = i
;
309 #ifdef CONFIG_MIPS_MT_FPAFF
310 /* Initialize map of CPUs with FPUs */
311 cpus_clear(mt_fpu_cpumask
);
314 /* One of those TC's is the one booting, and not a secondary... */
315 printk("%i available secondary CPU TC(s)\n", i
- 1);
321 * Common setup before any secondaries are started
322 * Make sure all CPU's are in a sensible state before we boot any of the
325 * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
326 * as possible across the available VPEs.
329 static void smtc_tc_setup(int vpe
, int tc
, int cpu
)
332 write_tc_c0_tchalt(TCHALT_H
);
334 write_tc_c0_tcstatus((read_tc_c0_tcstatus()
335 & ~(TCSTATUS_TKSU
| TCSTATUS_DA
| TCSTATUS_IXMT
))
337 write_tc_c0_tccontext(0);
339 write_tc_c0_tcbind(vpe
);
340 /* In general, all TCs should have the same cpu_data indications */
341 memcpy(&cpu_data
[cpu
], &cpu_data
[0], sizeof(struct cpuinfo_mips
));
342 /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
343 if (cpu_data
[0].cputype
== CPU_34K
)
344 cpu_data
[cpu
].options
&= ~MIPS_CPU_FPU
;
345 cpu_data
[cpu
].vpe_id
= vpe
;
346 cpu_data
[cpu
].tc_id
= tc
;
350 void mipsmt_prepare_cpus(void)
352 int i
, vpe
, tc
, ntc
, nvpe
, tcpervpe
[NR_CPUS
], slop
, cpu
;
356 struct smtc_ipi
*pipi
;
358 /* disable interrupts so we can disable MT */
359 local_irq_save(flags
);
360 /* disable MT so we can configure */
364 spin_lock_init(&freeIPIq
.lock
);
367 * We probably don't have as many VPEs as we do SMP "CPUs",
368 * but it's possible - and in any case we'll never use more!
370 for (i
=0; i
<NR_CPUS
; i
++) {
371 IPIQ
[i
].head
= IPIQ
[i
].tail
= NULL
;
372 spin_lock_init(&IPIQ
[i
].lock
);
374 atomic_set(&ipi_timer_latch
[i
], 0);
377 /* cpu_data index starts at zero */
379 cpu_data
[cpu
].vpe_id
= 0;
380 cpu_data
[cpu
].tc_id
= 0;
383 /* Report on boot-time options */
384 mips_mt_set_cpuoptions();
386 printk("Limit of %d VPEs set\n", vpelimit
);
388 printk("Limit of %d TCs set\n", tclimit
);
390 printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
393 printk("ASID mask value override to 0x%x\n", asidmask
);
396 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
398 printk("Logic Analyser Trigger on suspected TC hang\n");
399 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
401 /* Put MVPE's into 'configuration state' */
402 write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC
);
404 val
= read_c0_mvpconf0();
405 nvpe
= ((val
& MVPCONF0_PVPE
) >> MVPCONF0_PVPE_SHIFT
) + 1;
406 if (vpelimit
> 0 && nvpe
> vpelimit
)
408 ntc
= ((val
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
411 if (tclimit
> 0 && ntc
> tclimit
)
414 for (i
= 0; i
< nvpe
; i
++) {
415 tcpervpe
[i
] = ntc
/ nvpe
;
417 if((slop
- i
) > 0) tcpervpe
[i
]++;
420 /* Handle command line override for VPE0 */
421 if (vpe0limit
> ntc
) vpe0limit
= ntc
;
424 if (vpe0limit
< tcpervpe
[0]) {
425 /* Reducing TC count - distribute to others */
426 slop
= tcpervpe
[0] - vpe0limit
;
427 slopslop
= slop
% (nvpe
- 1);
428 tcpervpe
[0] = vpe0limit
;
429 for (i
= 1; i
< nvpe
; i
++) {
430 tcpervpe
[i
] += slop
/ (nvpe
- 1);
431 if(slopslop
&& ((slopslop
- (i
- 1) > 0)))
434 } else if (vpe0limit
> tcpervpe
[0]) {
435 /* Increasing TC count - steal from others */
436 slop
= vpe0limit
- tcpervpe
[0];
437 slopslop
= slop
% (nvpe
- 1);
438 tcpervpe
[0] = vpe0limit
;
439 for (i
= 1; i
< nvpe
; i
++) {
440 tcpervpe
[i
] -= slop
/ (nvpe
- 1);
441 if(slopslop
&& ((slopslop
- (i
- 1) > 0)))
447 /* Set up shared TLB */
448 smtc_configure_tlb();
450 for (tc
= 0, vpe
= 0 ; (vpe
< nvpe
) && (tc
< ntc
) ; vpe
++) {
455 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP
);
458 printk("VPE %d: TC", vpe
);
459 for (i
= 0; i
< tcpervpe
[vpe
]; i
++) {
461 * TC 0 is bound to VPE 0 at reset,
462 * and is presumably executing this
463 * code. Leave it alone!
466 smtc_tc_setup(vpe
, tc
, cpu
);
474 * Clear any stale software interrupts from VPE's Cause
476 write_vpe_c0_cause(0);
479 * Clear ERL/EXL of VPEs other than 0
480 * and set restricted interrupt enable/mask.
482 write_vpe_c0_status((read_vpe_c0_status()
483 & ~(ST0_BEV
| ST0_ERL
| ST0_EXL
| ST0_IM
))
484 | (STATUSF_IP0
| STATUSF_IP1
| STATUSF_IP7
487 * set config to be the same as vpe0,
488 * particularly kseg0 coherency alg
490 write_vpe_c0_config(read_c0_config());
491 /* Clear any pending timer interrupt */
492 write_vpe_c0_compare(0);
493 /* Propagate Config7 */
494 write_vpe_c0_config7(read_c0_config7());
495 write_vpe_c0_count(read_c0_count());
497 /* enable multi-threading within VPE */
498 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE
);
500 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA
);
504 * Pull any physically present but unused TCs out of circulation.
506 while (tc
< (((val
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1)) {
507 cpu_clear(tc
, phys_cpu_present_map
);
508 cpu_clear(tc
, cpu_present_map
);
512 /* release config state */
513 write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC
);
517 /* Set up coprocessor affinity CPU mask(s) */
519 #ifdef CONFIG_MIPS_MT_FPAFF
520 for (tc
= 0; tc
< ntc
; tc
++) {
521 if (cpu_data
[tc
].options
& MIPS_CPU_FPU
)
522 cpu_set(tc
, mt_fpu_cpumask
);
526 /* set up ipi interrupts... */
528 /* If we have multiple VPEs running, set up the cross-VPE interrupt */
530 setup_cross_vpe_interrupts(nvpe
);
532 /* Set up queue of free IPI "messages". */
533 nipi
= NR_CPUS
* IPIBUF_PER_CPU
;
537 pipi
= kmalloc(nipi
*sizeof(struct smtc_ipi
), GFP_KERNEL
);
539 panic("kmalloc of IPI message buffers failed\n");
541 printk("IPI buffer pool of %d buffers\n", nipi
);
542 for (i
= 0; i
< nipi
; i
++) {
543 smtc_ipi_nq(&freeIPIq
, pipi
);
547 /* Arm multithreading and enable other VPEs - but all TCs are Halted */
550 local_irq_restore(flags
);
551 /* Initialize SMTC /proc statistics/diagnostics */
557 * Setup the PC, SP, and GP of a secondary processor and start it
559 * smp_bootstrap is the place to resume from
560 * __KSTK_TOS(idle) is apparently the stack pointer
561 * (unsigned long)idle->thread_info the gp
564 void __cpuinit
smtc_boot_secondary(int cpu
, struct task_struct
*idle
)
566 extern u32 kernelsp
[NR_CPUS
];
571 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
574 settc(cpu_data
[cpu
].tc_id
);
577 write_tc_c0_tcrestart((unsigned long)&smp_bootstrap
);
580 kernelsp
[cpu
] = __KSTK_TOS(idle
);
581 write_tc_gpr_sp(__KSTK_TOS(idle
));
584 write_tc_gpr_gp((unsigned long)task_thread_info(idle
));
586 smtc_status
|= SMTC_MTC_ACTIVE
;
587 write_tc_c0_tchalt(0);
588 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
594 void smtc_init_secondary(void)
597 * Start timer on secondary VPEs if necessary.
598 * plat_timer_setup has already have been invoked by init/main
599 * on "boot" TC. Like per_cpu_trap_init() hack, this assumes that
600 * SMTC init code assigns TCs consdecutively and in ascending order
601 * to across available VPEs.
603 if (((read_c0_tcbind() & TCBIND_CURTC
) != 0) &&
604 ((read_c0_tcbind() & TCBIND_CURVPE
)
605 != cpu_data
[smp_processor_id() - 1].vpe_id
)){
606 write_c0_compare(read_c0_count() + mips_hpt_frequency
/HZ
);
612 void smtc_smp_finish(void)
614 printk("TC %d going on-line as CPU %d\n",
615 cpu_data
[smp_processor_id()].tc_id
, smp_processor_id());
618 void smtc_cpus_done(void)
623 * Support for SMTC-optimized driver IRQ registration
627 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
628 * in do_IRQ. These are passed in setup_irq_smtc() and stored
632 int setup_irq_smtc(unsigned int irq
, struct irqaction
* new,
633 unsigned long hwmask
)
635 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
636 unsigned int vpe
= current_cpu_data
.vpe_id
;
638 vpemask
[vpe
][irq
- MIPS_CPU_IRQ_BASE
] = 1;
640 irq_hwmask
[irq
] = hwmask
;
642 return setup_irq(irq
, new);
645 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
647 * Support for IRQ affinity to TCs
650 void smtc_set_irq_affinity(unsigned int irq
, cpumask_t affinity
)
653 * If a "fast path" cache of quickly decodable affinity state
654 * is maintained, this is where it gets done, on a call up
655 * from the platform affinity code.
659 void smtc_forward_irq(unsigned int irq
)
664 * OK wise guy, now figure out how to get the IRQ
665 * to be serviced on an authorized "CPU".
667 * Ideally, to handle the situation where an IRQ has multiple
668 * eligible CPUS, we would maintain state per IRQ that would
669 * allow a fair distribution of service requests. Since the
670 * expected use model is any-or-only-one, for simplicity
671 * and efficiency, we just pick the easiest one to find.
674 target
= first_cpu(irq_desc
[irq
].affinity
);
677 * We depend on the platform code to have correctly processed
678 * IRQ affinity change requests to ensure that the IRQ affinity
679 * mask has been purged of bits corresponding to nonexistent and
680 * offline "CPUs", and to TCs bound to VPEs other than the VPE
681 * connected to the physical interrupt input for the interrupt
682 * in question. Otherwise we have a nasty problem with interrupt
683 * mask management. This is best handled in non-performance-critical
684 * platform IRQ affinity setting code, to minimize interrupt-time
688 /* If no one is eligible, service locally */
689 if (target
>= NR_CPUS
) {
690 do_IRQ_no_affinity(irq
);
694 smtc_send_ipi(target
, IRQ_AFFINITY_IPI
, irq
);
697 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
700 * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
701 * Within a VPE one TC can interrupt another by different approaches.
702 * The easiest to get right would probably be to make all TCs except
703 * the target IXMT and set a software interrupt, but an IXMT-based
704 * scheme requires that a handler must run before a new IPI could
705 * be sent, which would break the "broadcast" loops in MIPS MT.
706 * A more gonzo approach within a VPE is to halt the TC, extract
707 * its Restart, Status, and a couple of GPRs, and program the Restart
708 * address to emulate an interrupt.
710 * Within a VPE, one can be confident that the target TC isn't in
711 * a critical EXL state when halted, since the write to the Halt
712 * register could not have issued on the writing thread if the
713 * halting thread had EXL set. So k0 and k1 of the target TC
714 * can be used by the injection code. Across VPEs, one can't
715 * be certain that the target TC isn't in a critical exception
716 * state. So we try a two-step process of sending a software
717 * interrupt to the target VPE, which either handles the event
718 * itself (if it was the target) or injects the event within
722 static void smtc_ipi_qdump(void)
726 for (i
= 0; i
< NR_CPUS
;i
++) {
727 printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
728 i
, (unsigned)IPIQ
[i
].head
, (unsigned)IPIQ
[i
].tail
,
734 * The standard atomic.h primitives don't quite do what we want
735 * here: We need an atomic add-and-return-previous-value (which
736 * could be done with atomic_add_return and a decrement) and an
737 * atomic set/zero-and-return-previous-value (which can't really
738 * be done with the atomic.h primitives). And since this is
739 * MIPS MT, we can assume that we have LL/SC.
741 static inline int atomic_postincrement(atomic_t
*v
)
743 unsigned long result
;
747 __asm__
__volatile__(
753 : "=&r" (result
), "=&r" (temp
), "=m" (v
->counter
)
760 void smtc_send_ipi(int cpu
, int type
, unsigned int action
)
763 struct smtc_ipi
*pipi
;
767 if (cpu
== smp_processor_id()) {
768 printk("Cannot Send IPI to self!\n");
771 /* Set up a descriptor, to be delivered either promptly or queued */
772 pipi
= smtc_ipi_dq(&freeIPIq
);
775 mips_mt_regdump(dvpe());
776 panic("IPI Msg. Buffers Depleted\n");
779 pipi
->arg
= (void *)action
;
781 if (cpu_data
[cpu
].vpe_id
!= cpu_data
[smp_processor_id()].vpe_id
) {
782 if (type
== SMTC_CLOCK_TICK
)
783 atomic_inc(&ipi_timer_latch
[cpu
]);
784 /* If not on same VPE, enqueue and send cross-VPE interupt */
785 smtc_ipi_nq(&IPIQ
[cpu
], pipi
);
787 settc(cpu_data
[cpu
].tc_id
);
788 write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1
);
792 * Not sufficient to do a LOCK_MT_PRA (dmt) here,
793 * since ASID shootdown on the other VPE may
794 * collide with this operation.
797 settc(cpu_data
[cpu
].tc_id
);
798 /* Halt the targeted TC */
799 write_tc_c0_tchalt(TCHALT_H
);
803 * Inspect TCStatus - if IXMT is set, we have to queue
804 * a message. Otherwise, we set up the "interrupt"
807 tcstatus
= read_tc_c0_tcstatus();
809 if ((tcstatus
& TCSTATUS_IXMT
) != 0) {
811 * Spin-waiting here can deadlock,
812 * so we queue the message for the target TC.
814 write_tc_c0_tchalt(0);
816 /* Try to reduce redundant timer interrupt messages */
817 if (type
== SMTC_CLOCK_TICK
) {
818 if (atomic_postincrement(&ipi_timer_latch
[cpu
])!=0){
819 smtc_ipi_nq(&freeIPIq
, pipi
);
823 smtc_ipi_nq(&IPIQ
[cpu
], pipi
);
825 if (type
== SMTC_CLOCK_TICK
)
826 atomic_inc(&ipi_timer_latch
[cpu
]);
827 post_direct_ipi(cpu
, pipi
);
828 write_tc_c0_tchalt(0);
835 * Send IPI message to Halted TC, TargTC/TargVPE already having been set
837 static void post_direct_ipi(int cpu
, struct smtc_ipi
*pipi
)
839 struct pt_regs
*kstack
;
840 unsigned long tcstatus
;
841 unsigned long tcrestart
;
842 extern u32 kernelsp
[NR_CPUS
];
843 extern void __smtc_ipi_vector(void);
844 //printk("%s: on %d for %d\n", __func__, smp_processor_id(), cpu);
846 /* Extract Status, EPC from halted TC */
847 tcstatus
= read_tc_c0_tcstatus();
848 tcrestart
= read_tc_c0_tcrestart();
849 /* If TCRestart indicates a WAIT instruction, advance the PC */
850 if ((tcrestart
& 0x80000000)
851 && ((*(unsigned int *)tcrestart
& 0xfe00003f) == 0x42000020)) {
855 * Save on TC's future kernel stack
857 * CU bit of Status is indicator that TC was
858 * already running on a kernel stack...
860 if (tcstatus
& ST0_CU0
) {
861 /* Note that this "- 1" is pointer arithmetic */
862 kstack
= ((struct pt_regs
*)read_tc_gpr_sp()) - 1;
864 kstack
= ((struct pt_regs
*)kernelsp
[cpu
]) - 1;
867 kstack
->cp0_epc
= (long)tcrestart
;
869 kstack
->cp0_tcstatus
= tcstatus
;
870 /* Pass token of operation to be performed kernel stack pad area */
871 kstack
->pad0
[4] = (unsigned long)pipi
;
872 /* Pass address of function to be called likewise */
873 kstack
->pad0
[5] = (unsigned long)&ipi_decode
;
874 /* Set interrupt exempt and kernel mode */
875 tcstatus
|= TCSTATUS_IXMT
;
876 tcstatus
&= ~TCSTATUS_TKSU
;
877 write_tc_c0_tcstatus(tcstatus
);
879 /* Set TC Restart address to be SMTC IPI vector */
880 write_tc_c0_tcrestart(__smtc_ipi_vector
);
883 static void ipi_resched_interrupt(void)
885 /* Return from interrupt should be enough to cause scheduler check */
889 static void ipi_call_interrupt(void)
891 /* Invoke generic function invocation code in smp.c */
892 smp_call_function_interrupt();
895 DECLARE_PER_CPU(struct clock_event_device
, smtc_dummy_clockevent_device
);
897 void ipi_decode(struct smtc_ipi
*pipi
)
899 unsigned int cpu
= smp_processor_id();
900 struct clock_event_device
*cd
;
901 void *arg_copy
= pipi
->arg
;
902 int type_copy
= pipi
->type
;
905 smtc_ipi_nq(&freeIPIq
, pipi
);
907 case SMTC_CLOCK_TICK
:
909 kstat_this_cpu
.irqs
[MIPS_CPU_IRQ_BASE
+ 1]++;
910 cd
= &per_cpu(smtc_dummy_clockevent_device
, cpu
);
911 ticks
= atomic_read(&ipi_timer_latch
[cpu
]);
912 atomic_sub(ticks
, &ipi_timer_latch
[cpu
]);
914 cd
->event_handler(cd
);
921 switch ((int)arg_copy
) {
922 case SMP_RESCHEDULE_YOURSELF
:
923 ipi_resched_interrupt();
925 case SMP_CALL_FUNCTION
:
926 ipi_call_interrupt();
929 printk("Impossible SMTC IPI Argument 0x%x\n",
934 #ifdef CONFIG_MIPS_MT_SMTC_IRQAFF
935 case IRQ_AFFINITY_IPI
:
937 * Accept a "forwarded" interrupt that was initially
938 * taken by a TC who doesn't have affinity for the IRQ.
940 do_IRQ_no_affinity((int)arg_copy
);
942 #endif /* CONFIG_MIPS_MT_SMTC_IRQAFF */
944 printk("Impossible SMTC IPI Type 0x%x\n", type_copy
);
949 void deferred_smtc_ipi(void)
951 struct smtc_ipi
*pipi
;
954 int q
= smp_processor_id();
957 * Test is not atomic, but much faster than a dequeue,
958 * and the vast majority of invocations will have a null queue.
960 if (IPIQ
[q
].head
!= NULL
) {
961 while((pipi
= smtc_ipi_dq(&IPIQ
[q
])) != NULL
) {
962 /* ipi_decode() should be called with interrupts off */
963 local_irq_save(flags
);
965 local_irq_restore(flags
);
971 * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
972 * set via cross-VPE MTTR manipulation of the Cause register. It would be
973 * in some regards preferable to have external logic for "doorbell" hardware
977 static int cpu_ipi_irq
= MIPS_CPU_IRQ_BASE
+ MIPS_CPU_IPI_IRQ
;
979 static irqreturn_t
ipi_interrupt(int irq
, void *dev_idm
)
981 int my_vpe
= cpu_data
[smp_processor_id()].vpe_id
;
982 int my_tc
= cpu_data
[smp_processor_id()].tc_id
;
984 struct smtc_ipi
*pipi
;
985 unsigned long tcstatus
;
988 unsigned int mtflags
;
989 unsigned int vpflags
;
992 * So long as cross-VPE interrupts are done via
993 * MFTR/MTTR read-modify-writes of Cause, we need
994 * to stop other VPEs whenever the local VPE does
997 local_irq_save(flags
);
999 clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ
);
1000 set_c0_status(0x100 << MIPS_CPU_IPI_IRQ
);
1001 irq_enable_hazard();
1003 local_irq_restore(flags
);
1006 * Cross-VPE Interrupt handler: Try to directly deliver IPIs
1007 * queued for TCs on this VPE other than the current one.
1008 * Return-from-interrupt should cause us to drain the queue
1009 * for the current TC, so we ought not to have to do it explicitly here.
1012 for_each_online_cpu(cpu
) {
1013 if (cpu_data
[cpu
].vpe_id
!= my_vpe
)
1016 pipi
= smtc_ipi_dq(&IPIQ
[cpu
]);
1018 if (cpu_data
[cpu
].tc_id
!= my_tc
) {
1021 settc(cpu_data
[cpu
].tc_id
);
1022 write_tc_c0_tchalt(TCHALT_H
);
1024 tcstatus
= read_tc_c0_tcstatus();
1025 if ((tcstatus
& TCSTATUS_IXMT
) == 0) {
1026 post_direct_ipi(cpu
, pipi
);
1029 write_tc_c0_tchalt(0);
1032 smtc_ipi_req(&IPIQ
[cpu
], pipi
);
1036 * ipi_decode() should be called
1037 * with interrupts off
1039 local_irq_save(flags
);
1041 local_irq_restore(flags
);
1049 static void ipi_irq_dispatch(void)
1051 do_IRQ(cpu_ipi_irq
);
1054 static struct irqaction irq_ipi
= {
1055 .handler
= ipi_interrupt
,
1056 .flags
= IRQF_DISABLED
,
1058 .flags
= IRQF_PERCPU
1061 static void setup_cross_vpe_interrupts(unsigned int nvpe
)
1067 panic("SMTC Kernel requires Vectored Interupt support");
1069 set_vi_handler(MIPS_CPU_IPI_IRQ
, ipi_irq_dispatch
);
1071 setup_irq_smtc(cpu_ipi_irq
, &irq_ipi
, (0x100 << MIPS_CPU_IPI_IRQ
));
1073 set_irq_handler(cpu_ipi_irq
, handle_percpu_irq
);
1077 * SMTC-specific hacks invoked from elsewhere in the kernel.
1079 * smtc_ipi_replay is called from raw_local_irq_restore which is only ever
1080 * called with interrupts disabled. We do rely on interrupts being disabled
1081 * here because using spin_lock_irqsave()/spin_unlock_irqrestore() would
1082 * result in a recursive call to raw_local_irq_restore().
1085 static void __smtc_ipi_replay(void)
1087 unsigned int cpu
= smp_processor_id();
1090 * To the extent that we've ever turned interrupts off,
1091 * we may have accumulated deferred IPIs. This is subtle.
1092 * If we use the smtc_ipi_qdepth() macro, we'll get an
1093 * exact number - but we'll also disable interrupts
1094 * and create a window of failure where a new IPI gets
1095 * queued after we test the depth but before we re-enable
1096 * interrupts. So long as IXMT never gets set, however,
1097 * we should be OK: If we pick up something and dispatch
1098 * it here, that's great. If we see nothing, but concurrent
1099 * with this operation, another TC sends us an IPI, IXMT
1100 * is clear, and we'll handle it as a real pseudo-interrupt
1101 * and not a pseudo-pseudo interrupt.
1103 if (IPIQ
[cpu
].depth
> 0) {
1105 struct smtc_ipi_q
*q
= &IPIQ
[cpu
];
1106 struct smtc_ipi
*pipi
;
1107 extern void self_ipi(struct smtc_ipi
*);
1109 spin_lock(&q
->lock
);
1110 pipi
= __smtc_ipi_dq(q
);
1111 spin_unlock(&q
->lock
);
1116 smtc_cpu_stats
[cpu
].selfipis
++;
1121 void smtc_ipi_replay(void)
1123 raw_local_irq_disable();
1124 __smtc_ipi_replay();
1127 EXPORT_SYMBOL(smtc_ipi_replay
);
1129 void smtc_idle_loop_hook(void)
1131 #ifdef CONFIG_SMTC_IDLE_HOOK_DEBUG
1140 * printk within DMT-protected regions can deadlock,
1141 * so buffer diagnostic messages for later output.
1144 char id_ho_db_msg
[768]; /* worst-case use should be less than 700 */
1146 if (atomic_read(&idle_hook_initialized
) == 0) { /* fast test */
1147 if (atomic_add_return(1, &idle_hook_initialized
) == 1) {
1149 /* Tedious stuff to just do once */
1150 mvpconf0
= read_c0_mvpconf0();
1151 hook_ntcs
= ((mvpconf0
& MVPCONF0_PTC
) >> MVPCONF0_PTC_SHIFT
) + 1;
1152 if (hook_ntcs
> NR_CPUS
)
1153 hook_ntcs
= NR_CPUS
;
1154 for (tc
= 0; tc
< hook_ntcs
; tc
++) {
1156 clock_hang_reported
[tc
] = 0;
1158 for (vpe
= 0; vpe
< 2; vpe
++)
1159 for (im
= 0; im
< 8; im
++)
1160 imstuckcount
[vpe
][im
] = 0;
1161 printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs
);
1162 atomic_set(&idle_hook_initialized
, 1000);
1164 /* Someone else is initializing in parallel - let 'em finish */
1165 while (atomic_read(&idle_hook_initialized
) < 1000)
1170 /* Have we stupidly left IXMT set somewhere? */
1171 if (read_c0_tcstatus() & 0x400) {
1172 write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
1174 printk("Dangling IXMT in cpu_idle()\n");
1177 /* Have we stupidly left an IM bit turned off? */
1178 #define IM_LIMIT 2000
1179 local_irq_save(flags
);
1181 pdb_msg
= &id_ho_db_msg
[0];
1182 im
= read_c0_status();
1183 vpe
= current_cpu_data
.vpe_id
;
1184 for (bit
= 0; bit
< 8; bit
++) {
1186 * In current prototype, I/O interrupts
1187 * are masked for VPE > 0
1189 if (vpemask
[vpe
][bit
]) {
1190 if (!(im
& (0x100 << bit
)))
1191 imstuckcount
[vpe
][bit
]++;
1193 imstuckcount
[vpe
][bit
] = 0;
1194 if (imstuckcount
[vpe
][bit
] > IM_LIMIT
) {
1195 set_c0_status(0x100 << bit
);
1197 imstuckcount
[vpe
][bit
] = 0;
1198 pdb_msg
+= sprintf(pdb_msg
,
1199 "Dangling IM %d fixed for VPE %d\n", bit
,
1206 * Now that we limit outstanding timer IPIs, check for hung TC
1208 for (tc
= 0; tc
< NR_CPUS
; tc
++) {
1209 /* Don't check ourself - we'll dequeue IPIs just below */
1210 if ((tc
!= smp_processor_id()) &&
1211 atomic_read(&ipi_timer_latch
[tc
]) > timerq_limit
) {
1212 if (clock_hang_reported
[tc
] == 0) {
1213 pdb_msg
+= sprintf(pdb_msg
,
1214 "TC %d looks hung with timer latch at %d\n",
1215 tc
, atomic_read(&ipi_timer_latch
[tc
]));
1216 clock_hang_reported
[tc
]++;
1221 local_irq_restore(flags
);
1222 if (pdb_msg
!= &id_ho_db_msg
[0])
1223 printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg
);
1224 #endif /* CONFIG_SMTC_IDLE_HOOK_DEBUG */
1227 * Replay any accumulated deferred IPIs. If "Instant Replay"
1228 * is in use, there should never be any.
1230 #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
1232 unsigned long flags
;
1234 local_irq_save(flags
);
1235 __smtc_ipi_replay();
1236 local_irq_restore(flags
);
1238 #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
1241 void smtc_soft_dump(void)
1245 printk("Counter Interrupts taken per CPU (TC)\n");
1246 for (i
=0; i
< NR_CPUS
; i
++) {
1247 printk("%d: %ld\n", i
, smtc_cpu_stats
[i
].timerints
);
1249 printk("Self-IPI invocations:\n");
1250 for (i
=0; i
< NR_CPUS
; i
++) {
1251 printk("%d: %ld\n", i
, smtc_cpu_stats
[i
].selfipis
);
1254 printk("Timer IPI Backlogs:\n");
1255 for (i
=0; i
< NR_CPUS
; i
++) {
1256 printk("%d: %d\n", i
, atomic_read(&ipi_timer_latch
[i
]));
1258 printk("%d Recoveries of \"stolen\" FPU\n",
1259 atomic_read(&smtc_fpu_recoveries
));
1264 * TLB management routines special to SMTC
1267 void smtc_get_new_mmu_context(struct mm_struct
*mm
, unsigned long cpu
)
1269 unsigned long flags
, mtflags
, tcstat
, prevhalt
, asid
;
1273 * It would be nice to be able to use a spinlock here,
1274 * but this is invoked from within TLB flush routines
1275 * that protect themselves with DVPE, so if a lock is
1276 * held by another TC, it'll never be freed.
1278 * DVPE/DMT must not be done with interrupts enabled,
1279 * so even so most callers will already have disabled
1280 * them, let's be really careful...
1283 local_irq_save(flags
);
1284 if (smtc_status
& SMTC_TLB_SHARED
) {
1289 tlb
= cpu_data
[cpu
].vpe_id
;
1291 asid
= asid_cache(cpu
);
1294 if (!((asid
+= ASID_INC
) & ASID_MASK
) ) {
1295 if (cpu_has_vtag_icache
)
1297 /* Traverse all online CPUs (hack requires contigous range) */
1298 for_each_online_cpu(i
) {
1300 * We don't need to worry about our own CPU, nor those of
1301 * CPUs who don't share our TLB.
1303 if ((i
!= smp_processor_id()) &&
1304 ((smtc_status
& SMTC_TLB_SHARED
) ||
1305 (cpu_data
[i
].vpe_id
== cpu_data
[cpu
].vpe_id
))) {
1306 settc(cpu_data
[i
].tc_id
);
1307 prevhalt
= read_tc_c0_tchalt() & TCHALT_H
;
1309 write_tc_c0_tchalt(TCHALT_H
);
1312 tcstat
= read_tc_c0_tcstatus();
1313 smtc_live_asid
[tlb
][(tcstat
& ASID_MASK
)] |= (asiduse
)(0x1 << i
);
1315 write_tc_c0_tchalt(0);
1318 if (!asid
) /* fix version if needed */
1319 asid
= ASID_FIRST_VERSION
;
1320 local_flush_tlb_all(); /* start new asid cycle */
1322 } while (smtc_live_asid
[tlb
][(asid
& ASID_MASK
)]);
1325 * SMTC shares the TLB within VPEs and possibly across all VPEs.
1327 for_each_online_cpu(i
) {
1328 if ((smtc_status
& SMTC_TLB_SHARED
) ||
1329 (cpu_data
[i
].vpe_id
== cpu_data
[cpu
].vpe_id
))
1330 cpu_context(i
, mm
) = asid_cache(i
) = asid
;
1333 if (smtc_status
& SMTC_TLB_SHARED
)
1337 local_irq_restore(flags
);
1341 * Invoked from macros defined in mmu_context.h
1342 * which must already have disabled interrupts
1343 * and done a DVPE or DMT as appropriate.
1346 void smtc_flush_tlb_asid(unsigned long asid
)
1351 entry
= read_c0_wired();
1353 /* Traverse all non-wired entries */
1354 while (entry
< current_cpu_data
.tlbsize
) {
1355 write_c0_index(entry
);
1359 ehi
= read_c0_entryhi();
1360 if ((ehi
& ASID_MASK
) == asid
) {
1362 * Invalidate only entries with specified ASID,
1363 * makiing sure all entries differ.
1365 write_c0_entryhi(CKSEG0
+ (entry
<< (PAGE_SHIFT
+ 1)));
1366 write_c0_entrylo0(0);
1367 write_c0_entrylo1(0);
1369 tlb_write_indexed();
1373 write_c0_index(PARKED_INDEX
);
1378 * Support for single-threading cache flush operations.
1381 static int halt_state_save
[NR_CPUS
];
1384 * To really, really be sure that nothing is being done
1385 * by other TCs, halt them all. This code assumes that
1386 * a DVPE has already been done, so while their Halted
1387 * state is theoretically architecturally unstable, in
1388 * practice, it's not going to change while we're looking
1392 void smtc_cflush_lockdown(void)
1396 for_each_online_cpu(cpu
) {
1397 if (cpu
!= smp_processor_id()) {
1398 settc(cpu_data
[cpu
].tc_id
);
1399 halt_state_save
[cpu
] = read_tc_c0_tchalt();
1400 write_tc_c0_tchalt(TCHALT_H
);
1406 /* It would be cheating to change the cpu_online states during a flush! */
1408 void smtc_cflush_release(void)
1413 * Start with a hazard barrier to ensure
1414 * that all CACHE ops have played through.
1418 for_each_online_cpu(cpu
) {
1419 if (cpu
!= smp_processor_id()) {
1420 settc(cpu_data
[cpu
].tc_id
);
1421 write_tc_c0_tchalt(halt_state_save
[cpu
]);