xref: /openbsd/sys/arch/amd64/amd64/ipi.c (revision 57b33517)
1 /*	$OpenBSD: ipi.c,v 1.18 2022/11/10 08:26:54 jmatthew Exp $	*/
2 /*	$NetBSD: ipi.c,v 1.2 2003/03/01 13:05:37 fvdl 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 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
38 
39 #include <machine/intr.h>
40 #include <machine/atomic.h>
41 #include <machine/cpuvar.h>
42 #include <machine/i82489reg.h>
43 #include <machine/i82489var.h>
44 
45 void
x86_send_ipi(struct cpu_info * ci,int ipimask)46 x86_send_ipi(struct cpu_info *ci, int ipimask)
47 {
48 	x86_atomic_setbits_u32(&ci->ci_ipis, ipimask);
49 
50 	/* Don't send IPI to cpu which isn't (yet) running. */
51 	if (!(ci->ci_flags & CPUF_RUNNING))
52 		return;
53 
54 	x86_ipi(LAPIC_IPI_VECTOR, ci->ci_apicid, LAPIC_DLMODE_FIXED);
55 }
56 
57 int
x86_fast_ipi(struct cpu_info * ci,int ipi)58 x86_fast_ipi(struct cpu_info *ci, int ipi)
59 {
60 	if (!(ci->ci_flags & CPUF_RUNNING))
61 		return (ENOENT);
62 
63 	x86_ipi(ipi, ci->ci_apicid, LAPIC_DLMODE_FIXED);
64 
65 	return 0;
66 }
67 
68 void
x86_broadcast_ipi(int ipimask)69 x86_broadcast_ipi(int ipimask)
70 {
71 	struct cpu_info *ci, *self = curcpu();
72 	int count = 0;
73 	CPU_INFO_ITERATOR cii;
74 
75 	CPU_INFO_FOREACH(cii, ci) {
76 		if (ci == self)
77 			continue;
78 		if ((ci->ci_flags & CPUF_RUNNING) == 0)
79 			continue;
80 		x86_atomic_setbits_u32(&ci->ci_ipis, ipimask);
81 		count++;
82 	}
83 	if (!count)
84 		return;
85 
86 	x86_ipi(LAPIC_IPI_VECTOR, LAPIC_DEST_ALLEXCL, LAPIC_DLMODE_FIXED);
87 }
88 
89 void
x86_ipi_handler(void)90 x86_ipi_handler(void)
91 {
92 	extern struct evcount ipi_count;
93 	struct cpu_info *ci = curcpu();
94 	u_int32_t pending;
95 	int bit;
96 	int floor;
97 
98 	floor = ci->ci_handled_intr_level;
99 	ci->ci_handled_intr_level = ci->ci_ilevel;
100 
101 	pending = atomic_swap_uint(&ci->ci_ipis, 0);
102 	for (bit = 0; bit < X86_NIPI && pending; bit++) {
103 		if (pending & (1 << bit)) {
104 			pending &= ~(1 << bit);
105 			(*ipifunc[bit])(ci);
106 			evcount_inc(&ipi_count);
107 		}
108 	}
109 
110 	ci->ci_handled_intr_level = floor;
111 }
112