xref: /titanic_44/usr/src/uts/i86pc/os/intr.c (revision 100b72f4fc209355ff607a0d787aad892d8a44ed)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*100b72f4Sandrei  * Common Development and Distribution License (the "License").
6*100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*100b72f4Sandrei  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
297c478bd9Sstevel@tonic-gate #include <sys/regset.h>
307c478bd9Sstevel@tonic-gate #include <sys/psw.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/segments.h>
357c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
367c478bd9Sstevel@tonic-gate #include <sys/trap.h>
377c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
387c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
397c478bd9Sstevel@tonic-gate #include <sys/clock.h>
407c478bd9Sstevel@tonic-gate #include <sys/panic.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
437c478bd9Sstevel@tonic-gate #include <sys/stack.h>
447c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
477c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
497c478bd9Sstevel@tonic-gate #include <sys/zone.h>
507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #if defined(__amd64)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #if defined(__lint)
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * atomic_btr32() is a gcc __inline__ function, defined in <asm/bitmap.h>
577c478bd9Sstevel@tonic-gate  * For lint purposes, define it here.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate uint_t
607c478bd9Sstevel@tonic-gate atomic_btr32(uint32_t *pending, uint_t pil)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	return (*pending &= ~(1 << pil));
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate extern uint_t atomic_btr32(uint32_t *pending, uint_t pil);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * This code is amd64-only for now, but as time permits, we should
727c478bd9Sstevel@tonic-gate  * use this on i386 too.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Some questions to ponder:
777c478bd9Sstevel@tonic-gate  * -	in several of these routines, we make multiple calls to tsc_read()
787c478bd9Sstevel@tonic-gate  *	without invoking functions .. couldn't we just reuse the same
797c478bd9Sstevel@tonic-gate  *	timestamp sometimes?
807c478bd9Sstevel@tonic-gate  * -	if we have the inline, we can probably make set_base_spl be a
817c478bd9Sstevel@tonic-gate  *	C routine too.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static uint_t
857c478bd9Sstevel@tonic-gate bsrw_insn(uint16_t mask)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	uint_t index = sizeof (mask) * NBBY - 1;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ASSERT(mask != 0);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	while ((mask & (1 << index)) == 0)
927c478bd9Sstevel@tonic-gate 		index--;
937c478bd9Sstevel@tonic-gate 	return (index);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Do all the work necessary to set up the cpu and thread structures
987c478bd9Sstevel@tonic-gate  * to dispatch a high-level interrupt.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * Returns 0 if we're -not- already on the high-level interrupt stack,
1017c478bd9Sstevel@tonic-gate  * (and *must* switch to it), non-zero if we are already on that stack.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * Called with interrupts masked.
1047c478bd9Sstevel@tonic-gate  * The 'pil' is already set to the appropriate level for rp->r_trapno.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate int
1077c478bd9Sstevel@tonic-gate hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
1107c478bd9Sstevel@tonic-gate 	uint_t mask;
111eda89462Sesolom 	hrtime_t intrtime;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	ASSERT(pil > LOCK_LEVEL);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (pil == CBE_HIGH_PIL) {
1167c478bd9Sstevel@tonic-gate 		cpu->cpu_profile_pil = oldpil;
1177c478bd9Sstevel@tonic-gate 		if (USERMODE(rp->r_cs)) {
1187c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = 0;
1197c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = rp->r_pc;
1207c478bd9Sstevel@tonic-gate 		} else {
1217c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_pc = rp->r_pc;
1227c478bd9Sstevel@tonic-gate 			cpu->cpu_profile_upc = 0;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
1277c478bd9Sstevel@tonic-gate 	if (mask != 0) {
1287c478bd9Sstevel@tonic-gate 		int nestpil;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		/*
1317c478bd9Sstevel@tonic-gate 		 * We have interrupted another high-level interrupt.
1327c478bd9Sstevel@tonic-gate 		 * Load starting timestamp, compute interval, update
1337c478bd9Sstevel@tonic-gate 		 * cumulative counter.
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
1367c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
137eda89462Sesolom 		intrtime = tsc_read() -
1387c478bd9Sstevel@tonic-gate 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
1397a364d25Sschwartz 		mcpu->intrstat[nestpil][0] += intrtime;
140eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1417c478bd9Sstevel@tonic-gate 		/*
1427c478bd9Sstevel@tonic-gate 		 * Another high-level interrupt is active below this one, so
1437c478bd9Sstevel@tonic-gate 		 * there is no need to check for an interrupt thread.  That
1447c478bd9Sstevel@tonic-gate 		 * will be done by the lowest priority high-level interrupt
1457c478bd9Sstevel@tonic-gate 		 * active.
1467c478bd9Sstevel@tonic-gate 		 */
1477c478bd9Sstevel@tonic-gate 	} else {
1487c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		/*
1517c478bd9Sstevel@tonic-gate 		 * See if we are interrupting a low-level interrupt thread.
1527c478bd9Sstevel@tonic-gate 		 * If so, account for its time slice only if its time stamp
1537c478bd9Sstevel@tonic-gate 		 * is non-zero.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
156eda89462Sesolom 			intrtime = tsc_read() - t->t_intr_start;
1577a364d25Sschwartz 			mcpu->intrstat[t->t_pil][0] += intrtime;
158eda89462Sesolom 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
1597c478bd9Sstevel@tonic-gate 			t->t_intr_start = 0;
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * Store starting timestamp in CPU structure for this PIL.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = tsc_read();
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (pil == 15) {
1717c478bd9Sstevel@tonic-gate 		/*
1727c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
1737c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
1747c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
1757c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
1767c478bd9Sstevel@tonic-gate 		 */
1777c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
1787c478bd9Sstevel@tonic-gate 		(*refcntp)++;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Does most of the work of returning from a high level interrupt.
1907c478bd9Sstevel@tonic-gate  *
1917c478bd9Sstevel@tonic-gate  * Returns 0 if there are no more high level interrupts (in which
1927c478bd9Sstevel@tonic-gate  * case we must switch back to the interrupted thread stack) or
1937c478bd9Sstevel@tonic-gate  * non-zero if there are more (in which case we should stay on it).
1947c478bd9Sstevel@tonic-gate  *
1957c478bd9Sstevel@tonic-gate  * Called with interrupts masked
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate int
1987c478bd9Sstevel@tonic-gate hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2017c478bd9Sstevel@tonic-gate 	uint_t mask;
202eda89462Sesolom 	hrtime_t intrtime;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->mcpu_pri == pil);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (pil == 15) {
2117c478bd9Sstevel@tonic-gate 		/*
2127c478bd9Sstevel@tonic-gate 		 * To support reentrant level 15 interrupts, we maintain a
2137c478bd9Sstevel@tonic-gate 		 * recursion count in the top half of cpu_intr_actv.  Only
2147c478bd9Sstevel@tonic-gate 		 * when this count hits zero do we clear the PIL 15 bit from
2157c478bd9Sstevel@tonic-gate 		 * the lower half of cpu_intr_actv.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		ASSERT(*refcntp > 0);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if (--(*refcntp) == 0)
2227c478bd9Sstevel@tonic-gate 			cpu->cpu_intr_actv &= ~(1 << pil);
2237c478bd9Sstevel@tonic-gate 	} else {
2247c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_actv &= ~(1 << pil);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
2287c478bd9Sstevel@tonic-gate 
229eda89462Sesolom 	intrtime = tsc_read() - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
2307a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
231eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * Check for lower-pil nested high-level interrupt beneath
2357c478bd9Sstevel@tonic-gate 	 * current one.  If so, place a starting timestamp in its
2367c478bd9Sstevel@tonic-gate 	 * pil_high_start entry.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
2397c478bd9Sstevel@tonic-gate 	if (mask != 0) {
2407c478bd9Sstevel@tonic-gate 		int nestpil;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * find PIL of nested interrupt
2447c478bd9Sstevel@tonic-gate 		 */
2457c478bd9Sstevel@tonic-gate 		nestpil = bsrw_insn((uint16_t)mask);
2467c478bd9Sstevel@tonic-gate 		ASSERT(nestpil < pil);
2477c478bd9Sstevel@tonic-gate 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = tsc_read();
2487c478bd9Sstevel@tonic-gate 		/*
2497c478bd9Sstevel@tonic-gate 		 * (Another high-level interrupt is active below this one,
2507c478bd9Sstevel@tonic-gate 		 * so there is no need to check for an interrupt
2517c478bd9Sstevel@tonic-gate 		 * thread.  That will be done by the lowest priority
2527c478bd9Sstevel@tonic-gate 		 * high-level interrupt active.)
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * Check to see if there is a low-level interrupt active.
2577c478bd9Sstevel@tonic-gate 		 * If so, place a starting timestamp in the thread
2587c478bd9Sstevel@tonic-gate 		 * structure.
2597c478bd9Sstevel@tonic-gate 		 */
2607c478bd9Sstevel@tonic-gate 		kthread_t *t = cpu->cpu_thread;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_INTR_THREAD)
2637c478bd9Sstevel@tonic-gate 			t->t_intr_start = tsc_read();
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = oldpil;
2677c478bd9Sstevel@tonic-gate 	(void) (*setlvlx)(oldpil, vecnum);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Set up the cpu, thread and interrupt thread structures for
2747c478bd9Sstevel@tonic-gate  * executing an interrupt thread.  The new stack pointer of the
2757c478bd9Sstevel@tonic-gate  * interrupt thread (which *must* be switched to) is returned.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate caddr_t
2787c478bd9Sstevel@tonic-gate intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
2817c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	ASSERT(pil > 0);
2847c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
2857c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Get set to run an interrupt thread.
2897c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread, since we
2907c478bd9Sstevel@tonic-gate 	 * allocate one for each level on each CPU.
2917c478bd9Sstevel@tonic-gate 	 *
292fd71cd2fSesolom 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	t = cpu->cpu_thread;
295fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
296eda89462Sesolom 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
2977a364d25Sschwartz 		mcpu->intrstat[t->t_pil][0] += intrtime;
298eda89462Sesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
2997c478bd9Sstevel@tonic-gate 		t->t_intr_start = 0;
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * unlink the interrupt thread off the cpu
308fd71cd2fSesolom 	 *
309fd71cd2fSesolom 	 * Note that the code in kcpc_overflow_intr -relies- on the
310fd71cd2fSesolom 	 * ordering of events here - in particular that t->t_lwp of
311fd71cd2fSesolom 	 * the interrupt thread is set to the pinned thread *before*
312fd71cd2fSesolom 	 * curthread is changed.
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
3157c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
3167c478bd9Sstevel@tonic-gate 	it->t_intr = t;
3177c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * (threads on the interrupt thread free list could have state
3217c478bd9Sstevel@tonic-gate 	 * preset to TS_ONPROC, but it helps in debugging if
3227c478bd9Sstevel@tonic-gate 	 * they're TS_FREE.)
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;		/* new curthread on this cpu */
3277c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
3287c478bd9Sstevel@tonic-gate 	it->t_pri = intr_pri + (pri_t)pil;
3297c478bd9Sstevel@tonic-gate 	it->t_intr_start = tsc_read();
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	return (it->t_stk);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate #ifdef DEBUG
3367c478bd9Sstevel@tonic-gate int intr_thread_cnt;
3377c478bd9Sstevel@tonic-gate #endif
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * Called with interrupts disabled
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate void
3437c478bd9Sstevel@tonic-gate intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
3467c478bd9Sstevel@tonic-gate 	kthread_t *t;
3477c478bd9Sstevel@tonic-gate 	kthread_t *it = cpu->cpu_thread;	/* curthread */
3487c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
349eda89462Sesolom 	hrtime_t intrtime;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
3527c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	ASSERT(it->t_intr_start != 0);
355eda89462Sesolom 	intrtime = tsc_read() - it->t_intr_start;
3567a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
357eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
3607c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
3647c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
3657c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
3687c478bd9Sstevel@tonic-gate 		/*
3697c478bd9Sstevel@tonic-gate 		 * The interrupted thread is no longer pinned underneath
3707c478bd9Sstevel@tonic-gate 		 * the interrupt thread.  This means the interrupt must
3717c478bd9Sstevel@tonic-gate 		 * have blocked, and the interrupted thread has been
3727c478bd9Sstevel@tonic-gate 		 * unpinned, and has probably been running around the
3737c478bd9Sstevel@tonic-gate 		 * system for a while.
3747c478bd9Sstevel@tonic-gate 		 *
3757c478bd9Sstevel@tonic-gate 		 * Since there is no longer a thread under this one, put
3767c478bd9Sstevel@tonic-gate 		 * this interrupt thread back on the CPU's free list and
3777c478bd9Sstevel@tonic-gate 		 * resume the idle thread which will dispatch the next
3787c478bd9Sstevel@tonic-gate 		 * thread to run.
3797c478bd9Sstevel@tonic-gate 		 */
3807c478bd9Sstevel@tonic-gate #ifdef DEBUG
3817c478bd9Sstevel@tonic-gate 		intr_thread_cnt++;
3827c478bd9Sstevel@tonic-gate #endif
3837c478bd9Sstevel@tonic-gate 		cpu->cpu_stats.sys.intrblk++;
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * Set CPU's base SPL based on active interrupts bitmask
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		set_base_spl();
3887c478bd9Sstevel@tonic-gate 		basespl = cpu->cpu_base_spl;
3897c478bd9Sstevel@tonic-gate 		mcpu->mcpu_pri = basespl;
3907c478bd9Sstevel@tonic-gate 		(*setlvlx)(basespl, vec);
3917c478bd9Sstevel@tonic-gate 		(void) splhigh();
3927c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
3937c478bd9Sstevel@tonic-gate 		/*
3947c478bd9Sstevel@tonic-gate 		 * Return interrupt thread to pool
3957c478bd9Sstevel@tonic-gate 		 */
3967c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
3977c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
3987c478bd9Sstevel@tonic-gate 		swtch();
3997c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Return interrupt thread to the pool
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
4067c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
4077c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
4107c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
4117c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
4127c478bd9Sstevel@tonic-gate 	(*setlvlx)(pil, vec);
4137c478bd9Sstevel@tonic-gate 	t->t_intr_start = tsc_read();
4147c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177a364d25Sschwartz /*
4187a364d25Sschwartz  * Called with interrupts disabled by an interrupt thread to determine
4197a364d25Sschwartz  * how much time has elapsed. See interrupt.s:intr_get_time() for detailed
4207a364d25Sschwartz  * theory of operation.
4217a364d25Sschwartz  */
4227a364d25Sschwartz uint64_t
4237a364d25Sschwartz intr_thread_get_time(struct cpu *cpu)
4247a364d25Sschwartz {
4257a364d25Sschwartz 	struct machcpu *mcpu = &cpu->cpu_m;
4267a364d25Sschwartz 	kthread_t *t = cpu->cpu_thread;
4277a364d25Sschwartz 	uint64_t time, delta, ret;
4287a364d25Sschwartz 	uint_t pil = t->t_pil;
4297a364d25Sschwartz 
4307a364d25Sschwartz 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
4317a364d25Sschwartz 	ASSERT(t->t_flag & T_INTR_THREAD);
4327a364d25Sschwartz 	ASSERT(pil != 0);
4337a364d25Sschwartz 	ASSERT(t->t_intr_start != 0);
4347a364d25Sschwartz 
4357a364d25Sschwartz 	time = tsc_read();
4367a364d25Sschwartz 	delta = time - t->t_intr_start;
4377a364d25Sschwartz 	t->t_intr_start = time;
4387a364d25Sschwartz 
4397a364d25Sschwartz 	time = mcpu->intrstat[pil][0] + delta;
4407a364d25Sschwartz 	ret = time - mcpu->intrstat[pil][1];
4417a364d25Sschwartz 	mcpu->intrstat[pil][0] = time;
4427a364d25Sschwartz 	mcpu->intrstat[pil][1] = time;
4437a364d25Sschwartz 
4447a364d25Sschwartz 	return (ret);
4457a364d25Sschwartz }
4467a364d25Sschwartz 
4477c478bd9Sstevel@tonic-gate caddr_t
4487c478bd9Sstevel@tonic-gate dosoftint_prolog(
4497c478bd9Sstevel@tonic-gate 	struct cpu *cpu,
4507c478bd9Sstevel@tonic-gate 	caddr_t stackptr,
4517c478bd9Sstevel@tonic-gate 	uint32_t st_pending,
4527c478bd9Sstevel@tonic-gate 	uint_t oldpil)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	kthread_t *t, *volatile it;
4557c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
4567c478bd9Sstevel@tonic-gate 	uint_t pil;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate top:
4597c478bd9Sstevel@tonic-gate 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	pil = bsrw_insn((uint16_t)st_pending);
4627c478bd9Sstevel@tonic-gate 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
4637c478bd9Sstevel@tonic-gate 		return (0);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * XX64	Sigh.
4677c478bd9Sstevel@tonic-gate 	 *
4687c478bd9Sstevel@tonic-gate 	 * This is a transliteration of the i386 assembler code for
4697c478bd9Sstevel@tonic-gate 	 * soft interrupts.  One question is "why does this need
4707c478bd9Sstevel@tonic-gate 	 * to be atomic?"  One possible race is -other- processors
4717c478bd9Sstevel@tonic-gate 	 * posting soft interrupts to us in set_pending() i.e. the
4727c478bd9Sstevel@tonic-gate 	 * CPU might get preempted just after the address computation,
4737c478bd9Sstevel@tonic-gate 	 * but just before the atomic transaction, so another CPU would
4747c478bd9Sstevel@tonic-gate 	 * actually set the original CPU's st_pending bit.  However,
4757c478bd9Sstevel@tonic-gate 	 * it looks like it would be simpler to disable preemption there.
4767c478bd9Sstevel@tonic-gate 	 * Are there other races for which preemption control doesn't work?
4777c478bd9Sstevel@tonic-gate 	 *
4787c478bd9Sstevel@tonic-gate 	 * The i386 assembler version -also- checks to see if the bit
4797c478bd9Sstevel@tonic-gate 	 * being cleared was actually set; if it wasn't, it rechecks
4807c478bd9Sstevel@tonic-gate 	 * for more.  This seems a bit strange, as the only code that
4817c478bd9Sstevel@tonic-gate 	 * ever clears the bit is -this- code running with interrupts
4827c478bd9Sstevel@tonic-gate 	 * disabled on -this- CPU.  This code would probably be cheaper:
4837c478bd9Sstevel@tonic-gate 	 *
4847c478bd9Sstevel@tonic-gate 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
4857c478bd9Sstevel@tonic-gate 	 *   ~(1 << pil));
4867c478bd9Sstevel@tonic-gate 	 *
4877c478bd9Sstevel@tonic-gate 	 * and t->t_preempt--/++ around set_pending() even cheaper,
4887c478bd9Sstevel@tonic-gate 	 * but at this point, correctness is critical, so we slavishly
4897c478bd9Sstevel@tonic-gate 	 * emulate the i386 port.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil)
4927c478bd9Sstevel@tonic-gate 	    == 0) {
4937c478bd9Sstevel@tonic-gate 		st_pending = mcpu->mcpu_softinfo.st_pending;
4947c478bd9Sstevel@tonic-gate 		goto top;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
4987c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * Get set to run interrupt thread.
5027c478bd9Sstevel@tonic-gate 	 * There should always be an interrupt thread since we
5037c478bd9Sstevel@tonic-gate 	 * allocate one for each level on the CPU.
5047c478bd9Sstevel@tonic-gate 	 */
5057c478bd9Sstevel@tonic-gate 	it = cpu->cpu_intr_thread;
5067c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it->t_link;
5077c478bd9Sstevel@tonic-gate 
508fd71cd2fSesolom 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
509fd71cd2fSesolom 	t = cpu->cpu_thread;
510fd71cd2fSesolom 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
511fd71cd2fSesolom 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
512fd71cd2fSesolom 		mcpu->intrstat[pil][0] += intrtime;
513fd71cd2fSesolom 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
514fd71cd2fSesolom 		t->t_intr_start = 0;
515fd71cd2fSesolom 	}
516fd71cd2fSesolom 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * Note that the code in kcpc_overflow_intr -relies- on the
5197c478bd9Sstevel@tonic-gate 	 * ordering of events here - in particular that t->t_lwp of
5207c478bd9Sstevel@tonic-gate 	 * the interrupt thread is set to the pinned thread *before*
521fd71cd2fSesolom 	 * curthread is changed.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	it->t_lwp = t->t_lwp;
5247c478bd9Sstevel@tonic-gate 	it->t_state = TS_ONPROC;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * Push interrupted thread onto list from new thread.
5287c478bd9Sstevel@tonic-gate 	 * Set the new thread as the current one.
5297c478bd9Sstevel@tonic-gate 	 * Set interrupted thread's T_SP because if it is the idle thread,
5307c478bd9Sstevel@tonic-gate 	 * resume() may use that stack between threads.
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
5347c478bd9Sstevel@tonic-gate 	t->t_sp = (uintptr_t)stackptr;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	it->t_intr = t;
5377c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = it;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * Set bit for this pil in CPU's interrupt active bitmask.
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
5437c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv |= (1 << pil);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Initialize thread priority level from intr_pri
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	it->t_pil = (uchar_t)pil;
5497c478bd9Sstevel@tonic-gate 	it->t_pri = (pri_t)pil + intr_pri;
5507c478bd9Sstevel@tonic-gate 	it->t_intr_start = tsc_read();
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	return (it->t_stk);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate void
5567c478bd9Sstevel@tonic-gate dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	struct machcpu *mcpu = &cpu->cpu_m;
5597c478bd9Sstevel@tonic-gate 	kthread_t *t, *it;
5607c478bd9Sstevel@tonic-gate 	uint_t pil, basespl;
561eda89462Sesolom 	hrtime_t intrtime;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	it = cpu->cpu_thread;
5647c478bd9Sstevel@tonic-gate 	pil = it->t_pil;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	cpu->cpu_stats.sys.intr[pil - 1]++;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
5697c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_actv &= ~(1 << pil);
570eda89462Sesolom 	intrtime = tsc_read() - it->t_intr_start;
5717a364d25Sschwartz 	mcpu->intrstat[pil][0] += intrtime;
572eda89462Sesolom 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * If there is still an interrupted thread underneath this one
5767c478bd9Sstevel@tonic-gate 	 * then the interrupt was never blocked and the return is
5777c478bd9Sstevel@tonic-gate 	 * fairly simple.  Otherwise it isn't.
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	if ((t = it->t_intr) == NULL) {
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * Put thread back on the interrupt thread list.
5827c478bd9Sstevel@tonic-gate 		 * This was an interrupt thread, so set CPU's base SPL.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		set_base_spl();
5857c478bd9Sstevel@tonic-gate 		it->t_state = TS_FREE;
5867c478bd9Sstevel@tonic-gate 		it->t_link = cpu->cpu_intr_thread;
5877c478bd9Sstevel@tonic-gate 		cpu->cpu_intr_thread = it;
5887c478bd9Sstevel@tonic-gate 		(void) splhigh();
5897c478bd9Sstevel@tonic-gate 		swtch();
5907c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	it->t_link = cpu->cpu_intr_thread;
5937c478bd9Sstevel@tonic-gate 	cpu->cpu_intr_thread = it;
5947c478bd9Sstevel@tonic-gate 	it->t_state = TS_FREE;
5957c478bd9Sstevel@tonic-gate 	cpu->cpu_thread = t;
5967c478bd9Sstevel@tonic-gate 	if (t->t_flag & T_INTR_THREAD)
5977c478bd9Sstevel@tonic-gate 		t->t_intr_start = tsc_read();
5987c478bd9Sstevel@tonic-gate 	basespl = cpu->cpu_base_spl;
5997c478bd9Sstevel@tonic-gate 	pil = MAX(oldpil, basespl);
6007c478bd9Sstevel@tonic-gate 	mcpu->mcpu_pri = pil;
6017c478bd9Sstevel@tonic-gate 	(*setspl)(pil);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * Make the interrupted thread 'to' be runnable.
6067c478bd9Sstevel@tonic-gate  *
6077c478bd9Sstevel@tonic-gate  * Since t->t_sp has already been saved, t->t_pc is all
6087c478bd9Sstevel@tonic-gate  * that needs to be set in this function.
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  * Returns the interrupt level of the interrupt thread.
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate int
6137c478bd9Sstevel@tonic-gate intr_passivate(
6147c478bd9Sstevel@tonic-gate 	kthread_t *it,		/* interrupt thread */
6157c478bd9Sstevel@tonic-gate 	kthread_t *t)		/* interrupted thread */
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	extern void _sys_rtt();
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	ASSERT(it->t_flag & T_INTR_THREAD);
6207c478bd9Sstevel@tonic-gate 	ASSERT(SA(t->t_sp) == t->t_sp);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	t->t_pc = (uintptr_t)_sys_rtt;
6237c478bd9Sstevel@tonic-gate 	return (it->t_pil);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate void
6327c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	int		i;
6357c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
6367c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
6377c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
6387c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
6437c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
6447c478bd9Sstevel@tonic-gate 	else
6457c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
6487c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
6527c478bd9Sstevel@tonic-gate 	 */
6537c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
6547c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
6557c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
6567c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
6577c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
6587c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
6597c478bd9Sstevel@tonic-gate 			    i + 1);
6607c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
6617c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
6627c478bd9Sstevel@tonic-gate 			    i + 1);
6637c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
6647c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate void
6747c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
6817c478bd9Sstevel@tonic-gate  * update kstat.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate int
6847c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
6877c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
6887c478bd9Sstevel@tonic-gate 	int		i;
6897c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
6927c478bd9Sstevel@tonic-gate 		return (EACCES);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
6957a364d25Sschwartz 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
6967c478bd9Sstevel@tonic-gate 		tsc_scalehrtime(&hrt);
6977c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = (uint64_t)hrt;
6987c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	return (0);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
7067c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate void
7097c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	uint64_t	interval;
7127c478bd9Sstevel@tonic-gate 	uint64_t	start;
713eda89462Sesolom 	cpu_t		*cpu;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7167c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
7207c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
7217c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
7227c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
7237c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
7247c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
725eda89462Sesolom 	 *
726eda89462Sesolom 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
727eda89462Sesolom 	 * we don't use in the functions above, because we're not called
728eda89462Sesolom 	 * with interrupts blocked, but the epilog/prolog functions are.
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
7317c478bd9Sstevel@tonic-gate 		do {
7327c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
7337c478bd9Sstevel@tonic-gate 			interval = tsc_read() - start;
7347c478bd9Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
735eda89462Sesolom 		cpu = CPU;
7367a364d25Sschwartz 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
737eda89462Sesolom 
738eda89462Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
739eda89462Sesolom 		    interval);
7407c478bd9Sstevel@tonic-gate 	} else
7417c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
7467c478bd9Sstevel@tonic-gate  * in its thread structure.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	uint64_t ts;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
7547c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	do {
7577c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
7587c478bd9Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
7597c478bd9Sstevel@tonic-gate }
760