xref: /openbsd/sys/arch/i386/i386/ipifuncs.c (revision 28d09237)
1 /*	$OpenBSD: ipifuncs.c,v 1.36 2024/05/22 05:51:49 jsg Exp $	*/
2 /* $NetBSD: ipifuncs.c,v 1.1.2.3 2000/06/26 02:04:06 sommerfeld Exp $ */
3 
4 /*-
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by RedBack Networks Inc.
10  *
11  * Author: Bill Sommerfeld
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Interprocessor interrupt handlers.
37  */
38 
39 #include "npx.h"
40 
41 #include <sys/param.h>
42 #include <sys/memrange.h>
43 #include <sys/systm.h>
44 
45 #include <machine/cpufunc.h>
46 #include <machine/cpuvar.h>
47 #include <machine/intr.h>
48 #include <machine/atomic.h>
49 #include <machine/db_machdep.h>
50 
51 void i386_ipi_nop(struct cpu_info *);
52 void i386_ipi_halt(struct cpu_info *);
53 void i386_ipi_wbinvd(struct cpu_info *);
54 
55 #if NNPX > 0
56 void i386_ipi_synch_fpu(struct cpu_info *);
57 void i386_ipi_flush_fpu(struct cpu_info *);
58 #else
59 #define i386_ipi_synch_fpu NULL
60 #define i386_ipi_flush_fpu NULL
61 #endif
62 
63 #ifdef MTRR
64 void i386_ipi_reload_mtrr(struct cpu_info *);
65 #else
66 #define i386_ipi_reload_mtrr 0
67 #endif
68 
69 void (*ipifunc[I386_NIPI])(struct cpu_info *) =
70 {
71 	i386_ipi_halt,
72 	i386_ipi_nop,
73 	i386_ipi_flush_fpu,
74 	i386_ipi_synch_fpu,
75 	i386_ipi_reload_mtrr,
76 	NULL,
77 #ifdef DDB
78 	i386_ipi_db,
79 #else
80 	NULL,
81 #endif
82 	i386_setperf_ipi,
83 	i386_ipi_wbinvd,
84 };
85 
86 void
i386_ipi_nop(struct cpu_info * ci)87 i386_ipi_nop(struct cpu_info *ci)
88 {
89 }
90 
91 void
i386_ipi_halt(struct cpu_info * ci)92 i386_ipi_halt(struct cpu_info *ci)
93 {
94 	SCHED_ASSERT_UNLOCKED();
95 	KERNEL_ASSERT_UNLOCKED();
96 
97 	npxsave_cpu(ci, 1);
98 	intr_disable();
99 	lapic_disable();
100 	wbinvd();
101 	ci->ci_flags &= ~CPUF_RUNNING;
102 	wbinvd();
103 
104 	for(;;) {
105 		asm volatile("hlt");
106 	}
107 }
108 
109 #if NNPX > 0
110 void
i386_ipi_flush_fpu(struct cpu_info * ci)111 i386_ipi_flush_fpu(struct cpu_info *ci)
112 {
113 	if (ci->ci_fpsaveproc == ci->ci_fpcurproc)
114 		npxsave_cpu(ci, 0);
115 }
116 
117 void
i386_ipi_synch_fpu(struct cpu_info * ci)118 i386_ipi_synch_fpu(struct cpu_info *ci)
119 {
120 	if (ci->ci_fpsaveproc == ci->ci_fpcurproc)
121 		npxsave_cpu(ci, 1);
122 }
123 #endif
124 
125 #ifdef MTRR
126 void
i386_ipi_reload_mtrr(struct cpu_info * ci)127 i386_ipi_reload_mtrr(struct cpu_info *ci)
128 {
129 	if (mem_range_softc.mr_op != NULL)
130 		mem_range_softc.mr_op->reload(&mem_range_softc);
131 }
132 #endif
133 
134 void
i386_ipi_wbinvd(struct cpu_info * ci)135 i386_ipi_wbinvd(struct cpu_info *ci)
136 {
137 	wbinvd();
138 }
139 
140 void
i386_spurious(void)141 i386_spurious(void)
142 {
143 	printf("spurious intr\n");
144 }
145 
146 void
i386_send_ipi(struct cpu_info * ci,int ipimask)147 i386_send_ipi(struct cpu_info *ci, int ipimask)
148 {
149 	i386_atomic_setbits_l(&ci->ci_ipis, ipimask);
150 
151 	/* Don't send IPI to cpu which isn't (yet) running. */
152 	if (!(ci->ci_flags & CPUF_RUNNING))
153 		return;
154 
155 	i386_ipi(LAPIC_IPI_VECTOR, ci->ci_apicid, LAPIC_DLMODE_FIXED);
156 
157 	return;
158 }
159 
160 int
i386_fast_ipi(struct cpu_info * ci,int ipi)161 i386_fast_ipi(struct cpu_info *ci, int ipi)
162 {
163 	if (!(ci->ci_flags & CPUF_RUNNING))
164 		return (ENOENT);
165 
166 	i386_ipi(ipi, ci->ci_apicid, LAPIC_DLMODE_FIXED);
167 
168 	return 0;
169 }
170 
171 void
i386_broadcast_ipi(int ipimask)172 i386_broadcast_ipi(int ipimask)
173 {
174 	struct cpu_info *ci, *self = curcpu();
175 	CPU_INFO_ITERATOR cii;
176 	int count = 0;
177 
178 	CPU_INFO_FOREACH(cii, ci) {
179 		if (ci == self)
180 			continue;
181 		if ((ci->ci_flags & CPUF_RUNNING) == 0)
182 			continue;
183 		i386_atomic_setbits_l(&ci->ci_ipis, ipimask);
184 		count++;
185 	}
186 	if (!count)
187 		return;
188 
189 	i386_ipi(LAPIC_IPI_VECTOR, LAPIC_DEST_ALLEXCL, LAPIC_DLMODE_FIXED);
190 }
191 
192 void
i386_ipi_handler(void)193 i386_ipi_handler(void)
194 {
195 	extern struct evcount ipi_count;
196 	struct cpu_info *ci = curcpu();
197 	u_int32_t pending;
198 	int bit;
199 
200 	pending = i386_atomic_testset_ul(&ci->ci_ipis, 0);
201 
202 	for (bit = 0; bit < I386_NIPI && pending; bit++) {
203 		if (pending & (1<<bit)) {
204 			pending &= ~(1<<bit);
205 			(*ipifunc[bit])(ci);
206 			ipi_count.ec_count++;
207 		}
208 	}
209 }
210 
211