1 /* Copyright (C) 2004 Mips Technologies, Inc */
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/cpumask.h>
6 #include <linux/interrupt.h>
7 #include <linux/module.h>
10 #include <asm/processor.h>
11 #include <asm/atomic.h>
12 #include <asm/system.h>
13 #include <asm/hardirq.h>
14 #include <asm/hazards.h>
15 #include <asm/mmu_context.h>
17 #include <asm/mipsregs.h>
18 #include <asm/cacheflush.h>
20 #include <asm/addrspace.h>
22 #include <asm/smtc_ipi.h>
23 #include <asm/smtc_proc.h>
26 * This file should be built into the kernel only if CONFIG_MIPS_MT_SMTC is set.
30 * MIPSCPU_INT_BASE is identically defined in both
31 * asm-mips/mips-boards/maltaint.h and asm-mips/mips-boards/simint.h,
32 * but as yet there's no properly organized include structure that
33 * will ensure that the right *int.h file will be included for a
34 * given platform build.
37 #define MIPSCPU_INT_BASE 16
39 #define MIPS_CPU_IPI_IRQ 1
41 #define LOCK_MT_PRA() \
42 local_irq_save(flags); \
45 #define UNLOCK_MT_PRA() \
47 local_irq_restore(flags)
49 #define LOCK_CORE_PRA() \
50 local_irq_save(flags); \
53 #define UNLOCK_CORE_PRA() \
55 local_irq_restore(flags)
58 * Data structures purely associated with SMTC parallelism
63 * Table for tracking ASIDs whose lifetime is prolonged.
66 asiduse smtc_live_asid[MAX_SMTC_TLBS][MAX_SMTC_ASIDS];
69 * Clock interrupt "latch" buffers, per "CPU"
72 unsigned int ipi_timer_latch[NR_CPUS];
75 * Number of InterProcessor Interupt (IPI) message buffers to allocate
78 #define IPIBUF_PER_CPU 4
80 struct smtc_ipi_q IPIQ[NR_CPUS];
81 struct smtc_ipi_q freeIPIq;
84 /* Forward declarations */
86 void ipi_decode(struct smtc_ipi *);
87 void post_direct_ipi(int cpu, struct smtc_ipi *pipi);
88 void setup_cross_vpe_interrupts(void);
89 void init_smtc_stats(void);
91 /* Global SMTC Status */
93 unsigned int smtc_status = 0;
95 /* Boot command line configuration overrides */
97 static int vpelimit = 0;
98 static int tclimit = 0;
99 static int ipibuffers = 0;
100 static int nostlb = 0;
101 static int asidmask = 0;
102 unsigned long smtc_asid_mask = 0xff;
104 static int __init maxvpes(char *str)
106 get_option(&str, &vpelimit);
110 static int __init maxtcs(char *str)
112 get_option(&str, &tclimit);
116 static int __init ipibufs(char *str)
118 get_option(&str, &ipibuffers);
122 static int __init stlb_disable(char *s)
128 static int __init asidmask_set(char *str)
130 get_option(&str, &asidmask);
140 smtc_asid_mask = (unsigned long)asidmask;
143 printk("ILLEGAL ASID mask 0x%x from command line\n", asidmask);
148 __setup("maxvpes=", maxvpes);
149 __setup("maxtcs=", maxtcs);
150 __setup("ipibufs=", ipibufs);
151 __setup("nostlb", stlb_disable);
152 __setup("asidmask=", asidmask_set);
154 /* Enable additional debug checks before going into CPU idle loop */
155 #define SMTC_IDLE_HOOK_DEBUG
157 #ifdef SMTC_IDLE_HOOK_DEBUG
159 static int hang_trig = 0;
161 static int __init hangtrig_enable(char *s)
168 __setup("hangtrig", hangtrig_enable);
170 #define DEFAULT_BLOCKED_IPI_LIMIT 32
172 static int timerq_limit = DEFAULT_BLOCKED_IPI_LIMIT;
174 static int __init tintq(char *str)
176 get_option(&str, &timerq_limit);
180 __setup("tintq=", tintq);
182 int imstuckcount[2][8];
183 /* vpemask represents IM/IE bits of per-VPE Status registers, low-to-high */
184 int vpemask[2][8] = {{0,1,1,0,0,0,0,1},{0,1,0,0,0,0,0,1}};
185 int tcnoprog[NR_CPUS];
186 static atomic_t idle_hook_initialized = {0};
187 static int clock_hang_reported[NR_CPUS];
189 #endif /* SMTC_IDLE_HOOK_DEBUG */
191 /* Initialize shared TLB - the should probably migrate to smtc_setup_cpus() */
193 void __init sanitize_tlb_entries(void)
195 printk("Deprecated sanitize_tlb_entries() invoked\n");
200 * Configure shared TLB - VPC configuration bit must be set by caller
203 void smtc_configure_tlb(void)
206 unsigned long mvpconf0;
207 unsigned long config1val;
209 /* Set up ASID preservation table */
210 for (vpes=0; vpes<MAX_SMTC_TLBS; vpes++) {
211 for(i = 0; i < MAX_SMTC_ASIDS; i++) {
212 smtc_live_asid[vpes][i] = 0;
215 mvpconf0 = read_c0_mvpconf0();
217 if ((vpes = ((mvpconf0 & MVPCONF0_PVPE)
218 >> MVPCONF0_PVPE_SHIFT) + 1) > 1) {
219 /* If we have multiple VPEs, try to share the TLB */
220 if ((mvpconf0 & MVPCONF0_TLBS) && !nostlb) {
222 * If TLB sizing is programmable, shared TLB
223 * size is the total available complement.
224 * Otherwise, we have to take the sum of all
225 * static VPE TLB entries.
227 if ((tlbsiz = ((mvpconf0 & MVPCONF0_PTLBE)
228 >> MVPCONF0_PTLBE_SHIFT)) == 0) {
230 * If there's more than one VPE, there had better
231 * be more than one TC, because we need one to bind
232 * to each VPE in turn to be able to read
233 * its configuration state!
236 /* Stop the TC from doing anything foolish */
237 write_tc_c0_tchalt(TCHALT_H);
239 /* No need to un-Halt - that happens later anyway */
240 for (i=0; i < vpes; i++) {
241 write_tc_c0_tcbind(i);
243 * To be 100% sure we're really getting the right
244 * information, we exit the configuration state
245 * and do an IHB after each rebinding.
248 read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
251 * Only count if the MMU Type indicated is TLB
253 if (((read_vpe_c0_config() & MIPS_CONF_MT) >> 7) == 1) {
254 config1val = read_vpe_c0_config1();
255 tlbsiz += ((config1val >> 25) & 0x3f) + 1;
258 /* Put core back in configuration state */
260 read_c0_mvpcontrol() | MVPCONTROL_VPC );
264 write_c0_mvpcontrol(read_c0_mvpcontrol() | MVPCONTROL_STLB);
268 * Setup kernel data structures to use software total,
269 * rather than read the per-VPE Config1 value. The values
270 * for "CPU 0" gets copied to all the other CPUs as part
271 * of their initialization in smtc_cpu_setup().
274 /* MIPS32 limits TLB indices to 64 */
277 cpu_data[0].tlbsize = current_cpu_data.tlbsize = tlbsiz;
278 smtc_status |= SMTC_TLB_SHARED;
279 local_flush_tlb_all();
281 printk("TLB of %d entry pairs shared by %d VPEs\n",
284 printk("WARNING: TLB Not Sharable on SMTC Boot!\n");
291 * Incrementally build the CPU map out of constituent MIPS MT cores,
292 * using the specified available VPEs and TCs. Plaform code needs
293 * to ensure that each MIPS MT core invokes this routine on reset,
296 * This version of the build_cpu_map and prepare_cpus routines assumes
297 * that *all* TCs of a MIPS MT core will be used for Linux, and that
298 * they will be spread across *all* available VPEs (to minimise the
299 * loss of efficiency due to exception service serialization).
300 * An improved version would pick up configuration information and
301 * possibly leave some TCs/VPEs as "slave" processors.
303 * Use c0_MVPConf0 to find out how many TCs are available, setting up
304 * phys_cpu_present_map and the logical/physical mappings.
307 int __init mipsmt_build_cpu_map(int start_cpu_slot)
312 * The CPU map isn't actually used for anything at this point,
313 * so it's not clear what else we should do apart from set
314 * everything up so that "logical" = "physical".
316 ntcs = ((read_c0_mvpconf0() & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
317 for (i=start_cpu_slot; i<NR_CPUS && i<ntcs; i++) {
318 cpu_set(i, phys_cpu_present_map);
319 __cpu_number_map[i] = i;
320 __cpu_logical_map[i] = i;
322 /* Initialize map of CPUs with FPUs */
323 cpus_clear(mt_fpu_cpumask);
325 /* One of those TC's is the one booting, and not a secondary... */
326 printk("%i available secondary CPU TC(s)\n", i - 1);
332 * Common setup before any secondaries are started
333 * Make sure all CPU's are in a sensible state before we boot any of the
336 * For MIPS MT "SMTC" operation, we set up all TCs, spread as evenly
337 * as possible across the available VPEs.
340 static void smtc_tc_setup(int vpe, int tc, int cpu)
343 write_tc_c0_tchalt(TCHALT_H);
345 write_tc_c0_tcstatus((read_tc_c0_tcstatus()
346 & ~(TCSTATUS_TKSU | TCSTATUS_DA | TCSTATUS_IXMT))
348 write_tc_c0_tccontext(0);
350 write_tc_c0_tcbind(vpe);
351 /* In general, all TCs should have the same cpu_data indications */
352 memcpy(&cpu_data[cpu], &cpu_data[0], sizeof(struct cpuinfo_mips));
353 /* For 34Kf, start with TC/CPU 0 as sole owner of single FPU context */
354 if (cpu_data[0].cputype == CPU_34K)
355 cpu_data[cpu].options &= ~MIPS_CPU_FPU;
356 cpu_data[cpu].vpe_id = vpe;
357 cpu_data[cpu].tc_id = tc;
361 void mipsmt_prepare_cpus(void)
363 int i, vpe, tc, ntc, nvpe, tcpervpe, slop, cpu;
367 struct smtc_ipi *pipi;
369 /* disable interrupts so we can disable MT */
370 local_irq_save(flags);
371 /* disable MT so we can configure */
375 spin_lock_init(&freeIPIq.lock);
378 * We probably don't have as many VPEs as we do SMP "CPUs",
379 * but it's possible - and in any case we'll never use more!
381 for (i=0; i<NR_CPUS; i++) {
382 IPIQ[i].head = IPIQ[i].tail = NULL;
383 spin_lock_init(&IPIQ[i].lock);
385 ipi_timer_latch[i] = 0;
388 /* cpu_data index starts at zero */
390 cpu_data[cpu].vpe_id = 0;
391 cpu_data[cpu].tc_id = 0;
394 /* Report on boot-time options */
395 mips_mt_set_cpuoptions ();
397 printk("Limit of %d VPEs set\n", vpelimit);
399 printk("Limit of %d TCs set\n", tclimit);
401 printk("Shared TLB Use Inhibited - UNSAFE for Multi-VPE Operation\n");
404 printk("ASID mask value override to 0x%x\n", asidmask);
407 #ifdef SMTC_IDLE_HOOK_DEBUG
409 printk("Logic Analyser Trigger on suspected TC hang\n");
410 #endif /* SMTC_IDLE_HOOK_DEBUG */
412 /* Put MVPE's into 'configuration state' */
413 write_c0_mvpcontrol( read_c0_mvpcontrol() | MVPCONTROL_VPC );
415 val = read_c0_mvpconf0();
416 nvpe = ((val & MVPCONF0_PVPE) >> MVPCONF0_PVPE_SHIFT) + 1;
417 if (vpelimit > 0 && nvpe > vpelimit)
419 ntc = ((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
422 if (tclimit > 0 && ntc > tclimit)
424 tcpervpe = ntc / nvpe;
425 slop = ntc % nvpe; /* Residual TCs, < NVPE */
427 /* Set up shared TLB */
428 smtc_configure_tlb();
430 for (tc = 0, vpe = 0 ; (vpe < nvpe) && (tc < ntc) ; vpe++) {
435 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_MVP);
438 printk("VPE %d: TC", vpe);
439 for (i = 0; i < tcpervpe; i++) {
441 * TC 0 is bound to VPE 0 at reset,
442 * and is presumably executing this
443 * code. Leave it alone!
446 smtc_tc_setup(vpe,tc, cpu);
454 smtc_tc_setup(vpe,tc, cpu);
463 * Clear any stale software interrupts from VPE's Cause
465 write_vpe_c0_cause(0);
468 * Clear ERL/EXL of VPEs other than 0
469 * and set restricted interrupt enable/mask.
471 write_vpe_c0_status((read_vpe_c0_status()
472 & ~(ST0_BEV | ST0_ERL | ST0_EXL | ST0_IM))
473 | (STATUSF_IP0 | STATUSF_IP1 | STATUSF_IP7
476 * set config to be the same as vpe0,
477 * particularly kseg0 coherency alg
479 write_vpe_c0_config(read_c0_config());
480 /* Clear any pending timer interrupt */
481 write_vpe_c0_compare(0);
482 /* Propagate Config7 */
483 write_vpe_c0_config7(read_c0_config7());
484 write_vpe_c0_count(read_c0_count());
486 /* enable multi-threading within VPE */
487 write_vpe_c0_vpecontrol(read_vpe_c0_vpecontrol() | VPECONTROL_TE);
489 write_vpe_c0_vpeconf0(read_vpe_c0_vpeconf0() | VPECONF0_VPA);
493 * Pull any physically present but unused TCs out of circulation.
495 while (tc < (((val & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1)) {
496 cpu_clear(tc, phys_cpu_present_map);
497 cpu_clear(tc, cpu_present_map);
501 /* release config state */
502 write_c0_mvpcontrol( read_c0_mvpcontrol() & ~ MVPCONTROL_VPC );
506 /* Set up coprocessor affinity CPU mask(s) */
508 for (tc = 0; tc < ntc; tc++) {
509 if (cpu_data[tc].options & MIPS_CPU_FPU)
510 cpu_set(tc, mt_fpu_cpumask);
513 /* set up ipi interrupts... */
515 /* If we have multiple VPEs running, set up the cross-VPE interrupt */
518 setup_cross_vpe_interrupts();
520 /* Set up queue of free IPI "messages". */
521 nipi = NR_CPUS * IPIBUF_PER_CPU;
525 pipi = kmalloc(nipi *sizeof(struct smtc_ipi), GFP_KERNEL);
527 panic("kmalloc of IPI message buffers failed\n");
529 printk("IPI buffer pool of %d buffers\n", nipi);
530 for (i = 0; i < nipi; i++) {
531 smtc_ipi_nq(&freeIPIq, pipi);
535 /* Arm multithreading and enable other VPEs - but all TCs are Halted */
538 local_irq_restore(flags);
539 /* Initialize SMTC /proc statistics/diagnostics */
545 * Setup the PC, SP, and GP of a secondary processor and start it
547 * smp_bootstrap is the place to resume from
548 * __KSTK_TOS(idle) is apparently the stack pointer
549 * (unsigned long)idle->thread_info the gp
552 void smtc_boot_secondary(int cpu, struct task_struct *idle)
554 extern u32 kernelsp[NR_CPUS];
559 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
562 settc(cpu_data[cpu].tc_id);
565 write_tc_c0_tcrestart((unsigned long)&smp_bootstrap);
568 kernelsp[cpu] = __KSTK_TOS(idle);
569 write_tc_gpr_sp(__KSTK_TOS(idle));
572 write_tc_gpr_gp((unsigned long)idle->thread_info);
574 smtc_status |= SMTC_MTC_ACTIVE;
575 write_tc_c0_tchalt(0);
576 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
582 void smtc_init_secondary(void)
585 * Start timer on secondary VPEs if necessary.
586 * plat_timer_setup has already have been invoked by init/main
587 * on "boot" TC. Like per_cpu_trap_init() hack, this assumes that
588 * SMTC init code assigns TCs consdecutively and in ascending order
589 * to across available VPEs.
591 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
592 ((read_c0_tcbind() & TCBIND_CURVPE)
593 != cpu_data[smp_processor_id() - 1].vpe_id)){
594 write_c0_compare (read_c0_count() + mips_hpt_frequency/HZ);
600 void smtc_smp_finish(void)
602 printk("TC %d going on-line as CPU %d\n",
603 cpu_data[smp_processor_id()].tc_id, smp_processor_id());
606 void smtc_cpus_done(void)
611 * Support for SMTC-optimized driver IRQ registration
615 * SMTC Kernel needs to manipulate low-level CPU interrupt mask
616 * in do_IRQ. These are passed in setup_irq_smtc() and stored
620 int setup_irq_smtc(unsigned int irq, struct irqaction * new,
621 unsigned long hwmask)
623 irq_hwmask[irq] = hwmask;
625 return setup_irq(irq, new);
629 * IPI model for SMTC is tricky, because interrupts aren't TC-specific.
630 * Within a VPE one TC can interrupt another by different approaches.
631 * The easiest to get right would probably be to make all TCs except
632 * the target IXMT and set a software interrupt, but an IXMT-based
633 * scheme requires that a handler must run before a new IPI could
634 * be sent, which would break the "broadcast" loops in MIPS MT.
635 * A more gonzo approach within a VPE is to halt the TC, extract
636 * its Restart, Status, and a couple of GPRs, and program the Restart
637 * address to emulate an interrupt.
639 * Within a VPE, one can be confident that the target TC isn't in
640 * a critical EXL state when halted, since the write to the Halt
641 * register could not have issued on the writing thread if the
642 * halting thread had EXL set. So k0 and k1 of the target TC
643 * can be used by the injection code. Across VPEs, one can't
644 * be certain that the target TC isn't in a critical exception
645 * state. So we try a two-step process of sending a software
646 * interrupt to the target VPE, which either handles the event
647 * itself (if it was the target) or injects the event within
651 void smtc_ipi_qdump(void)
655 for (i = 0; i < NR_CPUS ;i++) {
656 printk("IPIQ[%d]: head = 0x%x, tail = 0x%x, depth = %d\n",
657 i, (unsigned)IPIQ[i].head, (unsigned)IPIQ[i].tail,
663 * The standard atomic.h primitives don't quite do what we want
664 * here: We need an atomic add-and-return-previous-value (which
665 * could be done with atomic_add_return and a decrement) and an
666 * atomic set/zero-and-return-previous-value (which can't really
667 * be done with the atomic.h primitives). And since this is
668 * MIPS MT, we can assume that we have LL/SC.
670 static __inline__ int atomic_postincrement(unsigned int *pv)
672 unsigned long result;
676 __asm__ __volatile__(
682 : "=&r" (result), "=&r" (temp), "=m" (*pv)
689 /* No longer used in IPI dispatch, but retained for future recycling */
691 static __inline__ int atomic_postclear(unsigned int *pv)
693 unsigned long result;
697 __asm__ __volatile__(
703 : "=&r" (result), "=&r" (temp), "=m" (*pv)
711 void smtc_send_ipi(int cpu, int type, unsigned int action)
714 struct smtc_ipi *pipi;
718 if (cpu == smp_processor_id()) {
719 printk("Cannot Send IPI to self!\n");
722 /* Set up a descriptor, to be delivered either promptly or queued */
723 pipi = smtc_ipi_dq(&freeIPIq);
726 mips_mt_regdump(dvpe());
727 panic("IPI Msg. Buffers Depleted\n");
730 pipi->arg = (void *)action;
732 if (cpu_data[cpu].vpe_id != cpu_data[smp_processor_id()].vpe_id) {
733 /* If not on same VPE, enqueue and send cross-VPE interupt */
734 smtc_ipi_nq(&IPIQ[cpu], pipi);
736 settc(cpu_data[cpu].tc_id);
737 write_vpe_c0_cause(read_vpe_c0_cause() | C_SW1);
741 * Not sufficient to do a LOCK_MT_PRA (dmt) here,
742 * since ASID shootdown on the other VPE may
743 * collide with this operation.
746 settc(cpu_data[cpu].tc_id);
747 /* Halt the targeted TC */
748 write_tc_c0_tchalt(TCHALT_H);
752 * Inspect TCStatus - if IXMT is set, we have to queue
753 * a message. Otherwise, we set up the "interrupt"
756 tcstatus = read_tc_c0_tcstatus();
758 if ((tcstatus & TCSTATUS_IXMT) != 0) {
760 * Spin-waiting here can deadlock,
761 * so we queue the message for the target TC.
763 write_tc_c0_tchalt(0);
765 /* Try to reduce redundant timer interrupt messages */
766 if (type == SMTC_CLOCK_TICK) {
767 if (atomic_postincrement(&ipi_timer_latch[cpu])!=0){
768 smtc_ipi_nq(&freeIPIq, pipi);
772 smtc_ipi_nq(&IPIQ[cpu], pipi);
774 post_direct_ipi(cpu, pipi);
775 write_tc_c0_tchalt(0);
782 * Send IPI message to Halted TC, TargTC/TargVPE already having been set
784 void post_direct_ipi(int cpu, struct smtc_ipi *pipi)
786 struct pt_regs *kstack;
787 unsigned long tcstatus;
788 unsigned long tcrestart;
789 extern u32 kernelsp[NR_CPUS];
790 extern void __smtc_ipi_vector(void);
792 /* Extract Status, EPC from halted TC */
793 tcstatus = read_tc_c0_tcstatus();
794 tcrestart = read_tc_c0_tcrestart();
795 /* If TCRestart indicates a WAIT instruction, advance the PC */
796 if ((tcrestart & 0x80000000)
797 && ((*(unsigned int *)tcrestart & 0xfe00003f) == 0x42000020)) {
801 * Save on TC's future kernel stack
803 * CU bit of Status is indicator that TC was
804 * already running on a kernel stack...
806 if (tcstatus & ST0_CU0) {
807 /* Note that this "- 1" is pointer arithmetic */
808 kstack = ((struct pt_regs *)read_tc_gpr_sp()) - 1;
810 kstack = ((struct pt_regs *)kernelsp[cpu]) - 1;
813 kstack->cp0_epc = (long)tcrestart;
815 kstack->cp0_tcstatus = tcstatus;
816 /* Pass token of operation to be performed kernel stack pad area */
817 kstack->pad0[4] = (unsigned long)pipi;
818 /* Pass address of function to be called likewise */
819 kstack->pad0[5] = (unsigned long)&ipi_decode;
820 /* Set interrupt exempt and kernel mode */
821 tcstatus |= TCSTATUS_IXMT;
822 tcstatus &= ~TCSTATUS_TKSU;
823 write_tc_c0_tcstatus(tcstatus);
825 /* Set TC Restart address to be SMTC IPI vector */
826 write_tc_c0_tcrestart(__smtc_ipi_vector);
829 static void ipi_resched_interrupt(void)
831 /* Return from interrupt should be enough to cause scheduler check */
835 static void ipi_call_interrupt(void)
837 /* Invoke generic function invocation code in smp.c */
838 smp_call_function_interrupt();
841 void ipi_decode(struct smtc_ipi *pipi)
843 void *arg_copy = pipi->arg;
844 int type_copy = pipi->type;
845 int dest_copy = pipi->dest;
847 smtc_ipi_nq(&freeIPIq, pipi);
849 case SMTC_CLOCK_TICK:
850 /* Invoke Clock "Interrupt" */
851 ipi_timer_latch[dest_copy] = 0;
852 #ifdef SMTC_IDLE_HOOK_DEBUG
853 clock_hang_reported[dest_copy] = 0;
854 #endif /* SMTC_IDLE_HOOK_DEBUG */
855 local_timer_interrupt(0, NULL);
858 switch ((int)arg_copy) {
859 case SMP_RESCHEDULE_YOURSELF:
860 ipi_resched_interrupt();
862 case SMP_CALL_FUNCTION:
863 ipi_call_interrupt();
866 printk("Impossible SMTC IPI Argument 0x%x\n",
872 printk("Impossible SMTC IPI Type 0x%x\n", type_copy);
877 void deferred_smtc_ipi(void)
879 struct smtc_ipi *pipi;
882 int q = smp_processor_id();
885 * Test is not atomic, but much faster than a dequeue,
886 * and the vast majority of invocations will have a null queue.
888 if (IPIQ[q].head != NULL) {
889 while((pipi = smtc_ipi_dq(&IPIQ[q])) != NULL) {
890 /* ipi_decode() should be called with interrupts off */
891 local_irq_save(flags);
893 local_irq_restore(flags);
899 * Send clock tick to all TCs except the one executing the funtion
902 void smtc_timer_broadcast(int vpe)
905 int myTC = cpu_data[smp_processor_id()].tc_id;
906 int myVPE = cpu_data[smp_processor_id()].vpe_id;
908 smtc_cpu_stats[smp_processor_id()].timerints++;
910 for_each_online_cpu(cpu) {
911 if (cpu_data[cpu].vpe_id == myVPE &&
912 cpu_data[cpu].tc_id != myTC)
913 smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);
918 * Cross-VPE interrupts in the SMTC prototype use "software interrupts"
919 * set via cross-VPE MTTR manipulation of the Cause register. It would be
920 * in some regards preferable to have external logic for "doorbell" hardware
924 static int cpu_ipi_irq = MIPSCPU_INT_BASE + MIPS_CPU_IPI_IRQ;
926 static irqreturn_t ipi_interrupt(int irq, void *dev_idm)
928 int my_vpe = cpu_data[smp_processor_id()].vpe_id;
929 int my_tc = cpu_data[smp_processor_id()].tc_id;
931 struct smtc_ipi *pipi;
932 unsigned long tcstatus;
935 unsigned int mtflags;
936 unsigned int vpflags;
939 * So long as cross-VPE interrupts are done via
940 * MFTR/MTTR read-modify-writes of Cause, we need
941 * to stop other VPEs whenever the local VPE does
944 local_irq_save(flags);
946 clear_c0_cause(0x100 << MIPS_CPU_IPI_IRQ);
947 set_c0_status(0x100 << MIPS_CPU_IPI_IRQ);
950 local_irq_restore(flags);
953 * Cross-VPE Interrupt handler: Try to directly deliver IPIs
954 * queued for TCs on this VPE other than the current one.
955 * Return-from-interrupt should cause us to drain the queue
956 * for the current TC, so we ought not to have to do it explicitly here.
959 for_each_online_cpu(cpu) {
960 if (cpu_data[cpu].vpe_id != my_vpe)
963 pipi = smtc_ipi_dq(&IPIQ[cpu]);
965 if (cpu_data[cpu].tc_id != my_tc) {
968 settc(cpu_data[cpu].tc_id);
969 write_tc_c0_tchalt(TCHALT_H);
971 tcstatus = read_tc_c0_tcstatus();
972 if ((tcstatus & TCSTATUS_IXMT) == 0) {
973 post_direct_ipi(cpu, pipi);
976 write_tc_c0_tchalt(0);
979 smtc_ipi_req(&IPIQ[cpu], pipi);
983 * ipi_decode() should be called
984 * with interrupts off
986 local_irq_save(flags);
988 local_irq_restore(flags);
996 static void ipi_irq_dispatch(void)
1001 static struct irqaction irq_ipi;
1003 void setup_cross_vpe_interrupts(void)
1006 panic("SMTC Kernel requires Vectored Interupt support");
1008 set_vi_handler(MIPS_CPU_IPI_IRQ, ipi_irq_dispatch);
1010 irq_ipi.handler = ipi_interrupt;
1011 irq_ipi.flags = IRQF_DISABLED;
1012 irq_ipi.name = "SMTC_IPI";
1014 setup_irq_smtc(cpu_ipi_irq, &irq_ipi, (0x100 << MIPS_CPU_IPI_IRQ));
1016 irq_desc[cpu_ipi_irq].status |= IRQ_PER_CPU;
1017 set_irq_handler(cpu_ipi_irq, handle_percpu_irq);
1021 * SMTC-specific hacks invoked from elsewhere in the kernel.
1024 void smtc_ipi_replay(void)
1027 * To the extent that we've ever turned interrupts off,
1028 * we may have accumulated deferred IPIs. This is subtle.
1029 * If we use the smtc_ipi_qdepth() macro, we'll get an
1030 * exact number - but we'll also disable interrupts
1031 * and create a window of failure where a new IPI gets
1032 * queued after we test the depth but before we re-enable
1033 * interrupts. So long as IXMT never gets set, however,
1034 * we should be OK: If we pick up something and dispatch
1035 * it here, that's great. If we see nothing, but concurrent
1036 * with this operation, another TC sends us an IPI, IXMT
1037 * is clear, and we'll handle it as a real pseudo-interrupt
1038 * and not a pseudo-pseudo interrupt.
1040 if (IPIQ[smp_processor_id()].depth > 0) {
1041 struct smtc_ipi *pipi;
1042 extern void self_ipi(struct smtc_ipi *);
1044 while ((pipi = smtc_ipi_dq(&IPIQ[smp_processor_id()]))) {
1046 smtc_cpu_stats[smp_processor_id()].selfipis++;
1051 EXPORT_SYMBOL(smtc_ipi_replay);
1053 void smtc_idle_loop_hook(void)
1055 #ifdef SMTC_IDLE_HOOK_DEBUG
1064 * printk within DMT-protected regions can deadlock,
1065 * so buffer diagnostic messages for later output.
1068 char id_ho_db_msg[768]; /* worst-case use should be less than 700 */
1070 if (atomic_read(&idle_hook_initialized) == 0) { /* fast test */
1071 if (atomic_add_return(1, &idle_hook_initialized) == 1) {
1073 /* Tedious stuff to just do once */
1074 mvpconf0 = read_c0_mvpconf0();
1075 hook_ntcs = ((mvpconf0 & MVPCONF0_PTC) >> MVPCONF0_PTC_SHIFT) + 1;
1076 if (hook_ntcs > NR_CPUS)
1077 hook_ntcs = NR_CPUS;
1078 for (tc = 0; tc < hook_ntcs; tc++) {
1080 clock_hang_reported[tc] = 0;
1082 for (vpe = 0; vpe < 2; vpe++)
1083 for (im = 0; im < 8; im++)
1084 imstuckcount[vpe][im] = 0;
1085 printk("Idle loop test hook initialized for %d TCs\n", hook_ntcs);
1086 atomic_set(&idle_hook_initialized, 1000);
1088 /* Someone else is initializing in parallel - let 'em finish */
1089 while (atomic_read(&idle_hook_initialized) < 1000)
1094 /* Have we stupidly left IXMT set somewhere? */
1095 if (read_c0_tcstatus() & 0x400) {
1096 write_c0_tcstatus(read_c0_tcstatus() & ~0x400);
1098 printk("Dangling IXMT in cpu_idle()\n");
1101 /* Have we stupidly left an IM bit turned off? */
1102 #define IM_LIMIT 2000
1103 local_irq_save(flags);
1105 pdb_msg = &id_ho_db_msg[0];
1106 im = read_c0_status();
1107 vpe = cpu_data[smp_processor_id()].vpe_id;
1108 for (bit = 0; bit < 8; bit++) {
1110 * In current prototype, I/O interrupts
1111 * are masked for VPE > 0
1113 if (vpemask[vpe][bit]) {
1114 if (!(im & (0x100 << bit)))
1115 imstuckcount[vpe][bit]++;
1117 imstuckcount[vpe][bit] = 0;
1118 if (imstuckcount[vpe][bit] > IM_LIMIT) {
1119 set_c0_status(0x100 << bit);
1121 imstuckcount[vpe][bit] = 0;
1122 pdb_msg += sprintf(pdb_msg,
1123 "Dangling IM %d fixed for VPE %d\n", bit,
1130 * Now that we limit outstanding timer IPIs, check for hung TC
1132 for (tc = 0; tc < NR_CPUS; tc++) {
1133 /* Don't check ourself - we'll dequeue IPIs just below */
1134 if ((tc != smp_processor_id()) &&
1135 ipi_timer_latch[tc] > timerq_limit) {
1136 if (clock_hang_reported[tc] == 0) {
1137 pdb_msg += sprintf(pdb_msg,
1138 "TC %d looks hung with timer latch at %d\n",
1139 tc, ipi_timer_latch[tc]);
1140 clock_hang_reported[tc]++;
1145 local_irq_restore(flags);
1146 if (pdb_msg != &id_ho_db_msg[0])
1147 printk("CPU%d: %s", smp_processor_id(), id_ho_db_msg);
1148 #endif /* SMTC_IDLE_HOOK_DEBUG */
1151 * Replay any accumulated deferred IPIs. If "Instant Replay"
1152 * is in use, there should never be any.
1154 #ifndef CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY
1156 #endif /* CONFIG_MIPS_MT_SMTC_INSTANT_REPLAY */
1159 void smtc_soft_dump(void)
1163 printk("Counter Interrupts taken per CPU (TC)\n");
1164 for (i=0; i < NR_CPUS; i++) {
1165 printk("%d: %ld\n", i, smtc_cpu_stats[i].timerints);
1167 printk("Self-IPI invocations:\n");
1168 for (i=0; i < NR_CPUS; i++) {
1169 printk("%d: %ld\n", i, smtc_cpu_stats[i].selfipis);
1172 printk("Timer IPI Backlogs:\n");
1173 for (i=0; i < NR_CPUS; i++) {
1174 printk("%d: %d\n", i, ipi_timer_latch[i]);
1176 printk("%d Recoveries of \"stolen\" FPU\n",
1177 atomic_read(&smtc_fpu_recoveries));
1182 * TLB management routines special to SMTC
1185 void smtc_get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
1187 unsigned long flags, mtflags, tcstat, prevhalt, asid;
1191 * It would be nice to be able to use a spinlock here,
1192 * but this is invoked from within TLB flush routines
1193 * that protect themselves with DVPE, so if a lock is
1194 * held by another TC, it'll never be freed.
1196 * DVPE/DMT must not be done with interrupts enabled,
1197 * so even so most callers will already have disabled
1198 * them, let's be really careful...
1201 local_irq_save(flags);
1202 if (smtc_status & SMTC_TLB_SHARED) {
1207 tlb = cpu_data[cpu].vpe_id;
1209 asid = asid_cache(cpu);
1212 if (!((asid += ASID_INC) & ASID_MASK) ) {
1213 if (cpu_has_vtag_icache)
1215 /* Traverse all online CPUs (hack requires contigous range) */
1216 for (i = 0; i < num_online_cpus(); i++) {
1218 * We don't need to worry about our own CPU, nor those of
1219 * CPUs who don't share our TLB.
1221 if ((i != smp_processor_id()) &&
1222 ((smtc_status & SMTC_TLB_SHARED) ||
1223 (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))) {
1224 settc(cpu_data[i].tc_id);
1225 prevhalt = read_tc_c0_tchalt() & TCHALT_H;
1227 write_tc_c0_tchalt(TCHALT_H);
1230 tcstat = read_tc_c0_tcstatus();
1231 smtc_live_asid[tlb][(tcstat & ASID_MASK)] |= (asiduse)(0x1 << i);
1233 write_tc_c0_tchalt(0);
1236 if (!asid) /* fix version if needed */
1237 asid = ASID_FIRST_VERSION;
1238 local_flush_tlb_all(); /* start new asid cycle */
1240 } while (smtc_live_asid[tlb][(asid & ASID_MASK)]);
1243 * SMTC shares the TLB within VPEs and possibly across all VPEs.
1245 for (i = 0; i < num_online_cpus(); i++) {
1246 if ((smtc_status & SMTC_TLB_SHARED) ||
1247 (cpu_data[i].vpe_id == cpu_data[cpu].vpe_id))
1248 cpu_context(i, mm) = asid_cache(i) = asid;
1251 if (smtc_status & SMTC_TLB_SHARED)
1255 local_irq_restore(flags);
1259 * Invoked from macros defined in mmu_context.h
1260 * which must already have disabled interrupts
1261 * and done a DVPE or DMT as appropriate.
1264 void smtc_flush_tlb_asid(unsigned long asid)
1269 entry = read_c0_wired();
1271 /* Traverse all non-wired entries */
1272 while (entry < current_cpu_data.tlbsize) {
1273 write_c0_index(entry);
1277 ehi = read_c0_entryhi();
1278 if ((ehi & ASID_MASK) == asid) {
1280 * Invalidate only entries with specified ASID,
1281 * makiing sure all entries differ.
1283 write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
1284 write_c0_entrylo0(0);
1285 write_c0_entrylo1(0);
1287 tlb_write_indexed();
1291 write_c0_index(PARKED_INDEX);
1296 * Support for single-threading cache flush operations.
1299 int halt_state_save[NR_CPUS];
1302 * To really, really be sure that nothing is being done
1303 * by other TCs, halt them all. This code assumes that
1304 * a DVPE has already been done, so while their Halted
1305 * state is theoretically architecturally unstable, in
1306 * practice, it's not going to change while we're looking
1310 void smtc_cflush_lockdown(void)
1314 for_each_online_cpu(cpu) {
1315 if (cpu != smp_processor_id()) {
1316 settc(cpu_data[cpu].tc_id);
1317 halt_state_save[cpu] = read_tc_c0_tchalt();
1318 write_tc_c0_tchalt(TCHALT_H);
1324 /* It would be cheating to change the cpu_online states during a flush! */
1326 void smtc_cflush_release(void)
1331 * Start with a hazard barrier to ensure
1332 * that all CACHE ops have played through.
1336 for_each_online_cpu(cpu) {
1337 if (cpu != smp_processor_id()) {
1338 settc(cpu_data[cpu].tc_id);
1339 write_tc_c0_tchalt(halt_state_save[cpu]);