xref: /illumos-gate/usr/src/uts/common/os/cpu_intr.c (revision 86d949f9497332fe19be6b5d711d265eb957439f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/var.h>
30 #include <sys/thread.h>
31 #include <sys/cpuvar.h>
32 #include <sys/kstat.h>
33 #include <sys/uadmin.h>
34 #include <sys/systm.h>
35 #include <sys/errno.h>
36 #include <sys/cmn_err.h>
37 #include <sys/procset.h>
38 #include <sys/processor.h>
39 #include <sys/debug.h>
40 #include <sys/cyclic.h>
41 #include <sys/pool_pset.h>
42 
43 /*
44  * cpu_intr_on - determine whether the CPU is participating
45  * in I/O interrupts.
46  */
47 int
48 cpu_intr_on(cpu_t *cp)
49 {
50 	ASSERT(MUTEX_HELD(&cpu_lock));
51 	return ((cp->cpu_flags & CPU_ENABLE) != 0);
52 }
53 
54 /*
55  * Return the next on-line CPU handling interrupts.
56  */
57 cpu_t *
58 cpu_intr_next(cpu_t *cp)
59 {
60 	cpu_t	*c;
61 
62 	ASSERT(MUTEX_HELD(&cpu_lock));
63 
64 	c = cp->cpu_next_onln;
65 	while (c != cp) {
66 		if (cpu_intr_on(c)) {
67 			return (c);
68 		}
69 		c = c->cpu_next_onln;
70 	}
71 	return (NULL);
72 }
73 
74 /*
75  * cpu_intr_count - count how many CPUs are handling I/O interrupts.
76  */
77 int
78 cpu_intr_count(cpu_t *cp)
79 {
80 	cpu_t	*c;
81 	int	count = 0;
82 
83 	ASSERT(MUTEX_HELD(&cpu_lock));
84 	c = cp;
85 	do {
86 		if (cpu_intr_on(c)) {
87 			++count;
88 		}
89 	} while ((c = c->cpu_next) != cp);
90 	return (count);
91 }
92 
93 /*
94  * Enable I/O interrupts on this CPU, if they are disabled.
95  */
96 void
97 cpu_intr_enable(cpu_t *cp)
98 {
99 	ASSERT(MUTEX_HELD(&cpu_lock));
100 	if (!cpu_intr_on(cp)) {
101 		cpu_enable_intr(cp);
102 		cpu_set_state(cp);
103 	}
104 }
105 
106 /*
107  * cpu_intr_disable - redirect I/O interrupts targetted at this CPU.
108  *
109  * semantics: We check the count of CPUs that are accepting
110  * interrupts, because it's stupid to take the last CPU out
111  * of I/O interrupt participation. This also permits the
112  * p_online syscall to fail gracefully in uniprocessor configurations
113  * without having to perform any special platform-specific operations.
114  */
115 int
116 cpu_intr_disable(cpu_t *cp)
117 {
118 	int	e = EBUSY;
119 
120 	ASSERT(MUTEX_HELD(&cpu_lock));
121 	if ((cpu_intr_count(cp) > 1) && (cpu_intr_next(cp) != NULL)) {
122 		if (cpu_intr_on(cp)) {
123 			/*
124 			 * Juggle away cyclics, but don't fail if we don't
125 			 * manage to juggle all of them away; we want to allow
126 			 * CPU-bound cyclics to continue to fire on the
127 			 * sheltered CPU.
128 			 */
129 			(void) cyclic_juggle(cp);
130 			e = cpu_disable_intr(cp);
131 		}
132 	}
133 	if (e == 0)
134 		cpu_set_state(cp);
135 	return (e);
136 }
137