xen_ipi.c revision 1.1.2.1 1 1.1.2.1 cherry /* $NetBSD: xen_ipi.c,v 1.1.2.1 2011/06/03 13:27:41 cherry Exp $ */
2 1.1.2.1 cherry
3 1.1.2.1 cherry /*-
4 1.1.2.1 cherry * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1.2.1 cherry * All rights reserved.
6 1.1.2.1 cherry *
7 1.1.2.1 cherry * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 cherry * by Cherry G. Mathew <cherry (at) zyx.in>
9 1.1.2.1 cherry *
10 1.1.2.1 cherry * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 cherry * modification, are permitted provided that the following conditions
12 1.1.2.1 cherry * are met:
13 1.1.2.1 cherry * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 cherry * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 cherry * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 cherry * documentation and/or other materials provided with the distribution.
18 1.1.2.1 cherry *
19 1.1.2.1 cherry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 cherry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 cherry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 cherry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 cherry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 cherry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 cherry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 cherry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 cherry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 cherry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 cherry * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 cherry */
31 1.1.2.1 cherry
32 1.1.2.1 cherry #include <sys/cdefs.h> /* RCS ID macro */
33 1.1.2.1 cherry
34 1.1.2.1 cherry /*
35 1.1.2.1 cherry * Based on: x86/ipi.c
36 1.1.2.1 cherry * __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.1.2.1 2011/06/03 13:27:41 cherry Exp $");
37 1.1.2.1 cherry */
38 1.1.2.1 cherry
39 1.1.2.1 cherry __KERNEL_RCSID(0, "$NetBSD: xen_ipi.c,v 1.1.2.1 2011/06/03 13:27:41 cherry Exp $");
40 1.1.2.1 cherry
41 1.1.2.1 cherry #include <sys/types.h>
42 1.1.2.1 cherry
43 1.1.2.1 cherry #include <sys/atomic.h>
44 1.1.2.1 cherry #include <sys/mutex.h>
45 1.1.2.1 cherry #include <sys/cpu.h>
46 1.1.2.1 cherry #include <sys/device.h>
47 1.1.2.1 cherry #include <sys/xcall.h>
48 1.1.2.1 cherry #include <sys/errno.h>
49 1.1.2.1 cherry #include <sys/systm.h>
50 1.1.2.1 cherry
51 1.1.2.1 cherry #include <machine/cpu.h>
52 1.1.2.1 cherry #include <machine/fpu.h>
53 1.1.2.1 cherry #include <machine/frame.h>
54 1.1.2.1 cherry
55 1.1.2.1 cherry #include <xen/intr.h>
56 1.1.2.1 cherry #include <xen/intrdefs.h>
57 1.1.2.1 cherry #include <xen/hypervisor.h>
58 1.1.2.1 cherry #include <xen/xen3-public/vcpu.h>
59 1.1.2.1 cherry
60 1.1.2.1 cherry extern void ddb_ipi(struct trapframe);
61 1.1.2.1 cherry
62 1.1.2.1 cherry static void xen_ipi_halt(struct cpu_info *, struct intrframe *);
63 1.1.2.1 cherry static void xen_ipi_synch_fpu(struct cpu_info *, struct intrframe *);
64 1.1.2.1 cherry static void xen_ipi_ddb(struct cpu_info *, struct intrframe *);
65 1.1.2.1 cherry static void xen_ipi_xcall(struct cpu_info *, struct intrframe *);
66 1.1.2.1 cherry
67 1.1.2.1 cherry static void (*ipifunc[XEN_NIPIS])(struct cpu_info *, struct intrframe *) =
68 1.1.2.1 cherry { /* In order of priority (see: xen/include/intrdefs.h */
69 1.1.2.1 cherry xen_ipi_halt,
70 1.1.2.1 cherry xen_ipi_synch_fpu,
71 1.1.2.1 cherry xen_ipi_ddb,
72 1.1.2.1 cherry xen_ipi_xcall
73 1.1.2.1 cherry };
74 1.1.2.1 cherry
75 1.1.2.1 cherry static void
76 1.1.2.1 cherry xen_ipi_handler(struct cpu_info *ci, struct intrframe *regs)
77 1.1.2.1 cherry {
78 1.1.2.1 cherry uint32_t pending;
79 1.1.2.1 cherry int bit;
80 1.1.2.1 cherry
81 1.1.2.1 cherry pending = atomic_swap_32(&ci->ci_ipis, 0);
82 1.1.2.1 cherry
83 1.1.2.1 cherry KDASSERT((pending >> XEN_NIPIS) == 0);
84 1.1.2.1 cherry while ((bit = ffs(pending)) != 0) {
85 1.1.2.1 cherry bit--;
86 1.1.2.1 cherry pending &= ~(1 << bit);
87 1.1.2.1 cherry ci->ci_ipi_events[bit].ev_count++;
88 1.1.2.1 cherry if (ipifunc[bit] != NULL) {
89 1.1.2.1 cherry (*ipifunc[bit])(ci, regs);
90 1.1.2.1 cherry }
91 1.1.2.1 cherry else {
92 1.1.2.1 cherry panic("ipifunc[%d] unsupported!\n", bit);
93 1.1.2.1 cherry /* NOTREACHED */
94 1.1.2.1 cherry }
95 1.1.2.1 cherry }
96 1.1.2.1 cherry
97 1.1.2.1 cherry return;
98 1.1.2.1 cherry }
99 1.1.2.1 cherry
100 1.1.2.1 cherry /* Must be called once for every cpu that expects to send/recv ipis */
101 1.1.2.1 cherry void
102 1.1.2.1 cherry xen_ipi_init(void)
103 1.1.2.1 cherry {
104 1.1.2.1 cherry cpuid_t vcpu;
105 1.1.2.1 cherry evtchn_port_t evtchn;
106 1.1.2.1 cherry struct cpu_info *ci;
107 1.1.2.1 cherry
108 1.1.2.1 cherry ci = curcpu();
109 1.1.2.1 cherry
110 1.1.2.1 cherry vcpu = ci->ci_cpuid;
111 1.1.2.1 cherry KASSERT(vcpu < MAX_VIRT_CPUS);
112 1.1.2.1 cherry
113 1.1.2.1 cherry evtchn = ci->ci_ipi_evtchn = bind_vcpu_to_evtch(vcpu);
114 1.1.2.1 cherry
115 1.1.2.1 cherry KASSERT(evtchn != -1 && evtchn < NR_EVENT_CHANNELS);
116 1.1.2.1 cherry
117 1.1.2.1 cherry if (0 != event_set_handler(evtchn, (int (*)(void *))xen_ipi_handler,
118 1.1.2.1 cherry ci, IPL_HIGH, "ipi")) {
119 1.1.2.1 cherry panic("event_set_handler(...) KPI violation\n");
120 1.1.2.1 cherry /* NOTREACHED */
121 1.1.2.1 cherry }
122 1.1.2.1 cherry
123 1.1.2.1 cherry hypervisor_enable_event(evtchn);
124 1.1.2.1 cherry return;
125 1.1.2.1 cherry }
126 1.1.2.1 cherry
127 1.1.2.1 cherry /* prefer this to global variable */
128 1.1.2.1 cherry static inline u_int max_cpus(void)
129 1.1.2.1 cherry {
130 1.1.2.1 cherry return maxcpus;
131 1.1.2.1 cherry }
132 1.1.2.1 cherry
133 1.1.2.1 cherry static inline bool /* helper */
134 1.1.2.1 cherry valid_ipimask(uint32_t ipimask)
135 1.1.2.1 cherry {
136 1.1.2.1 cherry uint32_t masks = XEN_IPI_XCALL | XEN_IPI_DDB |
137 1.1.2.1 cherry XEN_IPI_SYNCH_FPU | XEN_IPI_HALT |
138 1.1.2.1 cherry XEN_IPI_KICK;
139 1.1.2.1 cherry
140 1.1.2.1 cherry if (ipimask & ~masks) {
141 1.1.2.1 cherry return false;
142 1.1.2.1 cherry }
143 1.1.2.1 cherry else {
144 1.1.2.1 cherry return true;
145 1.1.2.1 cherry }
146 1.1.2.1 cherry
147 1.1.2.1 cherry }
148 1.1.2.1 cherry
149 1.1.2.1 cherry int
150 1.1.2.1 cherry xen_send_ipi(struct cpu_info *ci, uint32_t ipimask)
151 1.1.2.1 cherry {
152 1.1.2.1 cherry evtchn_port_t evtchn;
153 1.1.2.1 cherry
154 1.1.2.1 cherry KASSERT(ci != NULL || ci != curcpu());
155 1.1.2.1 cherry
156 1.1.2.1 cherry if (!(ci->ci_flags & CPUF_RUNNING)) {
157 1.1.2.1 cherry return ENOENT;
158 1.1.2.1 cherry }
159 1.1.2.1 cherry
160 1.1.2.1 cherry evtchn = ci->ci_ipi_evtchn;
161 1.1.2.1 cherry if (false == valid_ipimask(ipimask)) {
162 1.1.2.1 cherry panic("xen_send_ipi() called with invalid ipimask\n");
163 1.1.2.1 cherry /* NOTREACHED */
164 1.1.2.1 cherry }
165 1.1.2.1 cherry
166 1.1.2.1 cherry atomic_or_32(&ci->ci_ipis, ipimask);
167 1.1.2.1 cherry hypervisor_notify_via_evtchn(evtchn);
168 1.1.2.1 cherry
169 1.1.2.1 cherry return 0;
170 1.1.2.1 cherry }
171 1.1.2.1 cherry
172 1.1.2.1 cherry void
173 1.1.2.1 cherry xen_broadcast_ipi(uint32_t ipimask)
174 1.1.2.1 cherry {
175 1.1.2.1 cherry struct cpu_info *ci, *self = curcpu();
176 1.1.2.1 cherry CPU_INFO_ITERATOR cii;
177 1.1.2.1 cherry
178 1.1.2.1 cherry if (false == valid_ipimask(ipimask)) {
179 1.1.2.1 cherry panic("xen_broadcast_ipi() called with invalid ipimask\n");
180 1.1.2.1 cherry /* NOTREACHED */
181 1.1.2.1 cherry }
182 1.1.2.1 cherry
183 1.1.2.1 cherry /*
184 1.1.2.1 cherry * XXX-cherry: there's an implicit broadcast sending order
185 1.1.2.1 cherry * which I dislike. Randomise this ? :-)
186 1.1.2.1 cherry */
187 1.1.2.1 cherry
188 1.1.2.1 cherry for (CPU_INFO_FOREACH(cii, ci)) {
189 1.1.2.1 cherry if (ci == NULL)
190 1.1.2.1 cherry continue;
191 1.1.2.1 cherry if (ci == self)
192 1.1.2.1 cherry continue;
193 1.1.2.1 cherry if (ci->ci_data.cpu_idlelwp == NULL)
194 1.1.2.1 cherry continue;
195 1.1.2.1 cherry if ((ci->ci_flags & CPUF_PRESENT) == 0)
196 1.1.2.1 cherry continue;
197 1.1.2.1 cherry if (ci->ci_flags & (CPUF_RUNNING)) {
198 1.1.2.1 cherry if (0 != xen_send_ipi(ci, ipimask)) {
199 1.1.2.1 cherry panic("xen_ipi of %x from %s to %s failed\n",
200 1.1.2.1 cherry ipimask, cpu_name(curcpu()),
201 1.1.2.1 cherry cpu_name(ci));
202 1.1.2.1 cherry }
203 1.1.2.1 cherry }
204 1.1.2.1 cherry }
205 1.1.2.1 cherry
206 1.1.2.1 cherry return;
207 1.1.2.1 cherry /* NOTREACHED */
208 1.1.2.1 cherry }
209 1.1.2.1 cherry
210 1.1.2.1 cherry /* MD wrapper for the xcall(9) callback. */
211 1.1.2.1 cherry #define PRIuCPUID "lu" /* XXX: move this somewhere more appropriate */
212 1.1.2.1 cherry
213 1.1.2.1 cherry static void
214 1.1.2.1 cherry xen_ipi_halt(struct cpu_info *ci, struct intrframe *intrf)
215 1.1.2.1 cherry {
216 1.1.2.1 cherry KASSERT(ci == curcpu());
217 1.1.2.1 cherry KASSERT(ci != NULL);
218 1.1.2.1 cherry if (HYPERVISOR_vcpu_op(VCPUOP_down, ci->ci_cpuid, NULL)) {
219 1.1.2.1 cherry panic("vcpu%" PRIuCPUID "shutdown failed.\n", ci->ci_cpuid);
220 1.1.2.1 cherry }
221 1.1.2.1 cherry
222 1.1.2.1 cherry return;
223 1.1.2.1 cherry }
224 1.1.2.1 cherry
225 1.1.2.1 cherry static void
226 1.1.2.1 cherry xen_ipi_synch_fpu(struct cpu_info *ci, struct intrframe *intrf)
227 1.1.2.1 cherry {
228 1.1.2.1 cherry KASSERT(ci != NULL);
229 1.1.2.1 cherry KASSERT(intrf != NULL);
230 1.1.2.1 cherry
231 1.1.2.1 cherry fpusave_cpu(true);
232 1.1.2.1 cherry return;
233 1.1.2.1 cherry }
234 1.1.2.1 cherry
235 1.1.2.1 cherry static void
236 1.1.2.1 cherry xen_ipi_ddb(struct cpu_info *ci, struct intrframe *intrf)
237 1.1.2.1 cherry {
238 1.1.2.1 cherry KASSERT(ci != NULL);
239 1.1.2.1 cherry KASSERT(intrf != NULL);
240 1.1.2.1 cherry
241 1.1.2.1 cherry ddb_ipi(intrf->if_tf);
242 1.1.2.1 cherry }
243 1.1.2.1 cherry
244 1.1.2.1 cherry static void
245 1.1.2.1 cherry xen_ipi_xcall(struct cpu_info *ci, struct intrframe *intrf)
246 1.1.2.1 cherry {
247 1.1.2.1 cherry KASSERT(ci != NULL);
248 1.1.2.1 cherry KASSERT(intrf != NULL);
249 1.1.2.1 cherry
250 1.1.2.1 cherry xc_ipi_handler();
251 1.1.2.1 cherry return;
252 1.1.2.1 cherry }
253 1.1.2.1 cherry
254 1.1.2.1 cherry void
255 1.1.2.1 cherry xc_send_ipi(struct cpu_info *ci)
256 1.1.2.1 cherry {
257 1.1.2.1 cherry
258 1.1.2.1 cherry KASSERT(kpreempt_disabled());
259 1.1.2.1 cherry KASSERT(curcpu() != ci);
260 1.1.2.1 cherry printf("xc_send_ipi called \n");
261 1.1.2.1 cherry if (ci) {
262 1.1.2.1 cherry if (0 != xen_send_ipi(ci, XEN_IPI_XCALL)) {
263 1.1.2.1 cherry panic("xen_send_ipi(XEN_IPI_XCALL) failed\n");
264 1.1.2.1 cherry };
265 1.1.2.1 cherry } else {
266 1.1.2.1 cherry xen_broadcast_ipi(XEN_IPI_XCALL);
267 1.1.2.1 cherry }
268 1.1.2.1 cherry }
269