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