win-pvdrivers

view xenpci/evtchn.c @ 433:b1da81cc9868

updates
author James Harper <james.harper@bendigoit.com.au>
date Tue Oct 14 10:45:56 2008 +1100 (2008-10-14)
parents 68565fbe4425
children 605747e0db9c
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenpci.h"
22 #if defined(__MINGW32__)
23 #define xchg(p1, p2) InterlockedExchange((xen_long_t * volatile)p1, p2)
24 /* rest implemented in mingw_extras.c */
25 #elif defined(_X86_)
26 #define xchg(p1, p2) InterlockedExchange(p1, p2)
27 #define synch_clear_bit(p1, p2) InterlockedBitTestAndReset(p2, p1)
28 #define synch_set_bit(p1, p2) InterlockedBitTestAndSet(p2, p1)
29 #define bit_scan_forward(p1, p2) _BitScanForward(p1, p2)
30 #else
31 #define xchg(p1, p2) InterlockedExchange64(p1, p2)
32 #define synch_clear_bit(p1, p2) _interlockedbittestandreset64(p2, p1)
33 #define synch_set_bit(p1, p2) _interlockedbittestandset64(p2, p1)
34 #define bit_scan_forward(p1, p2) _BitScanForward64(p1, p2)
35 #endif
37 #define BITS_PER_LONG (sizeof(xen_ulong_t) * 8)
38 #define BITS_PER_LONG_SHIFT (5 + (sizeof(xen_ulong_t) >> 3))
40 static DDKAPI VOID
41 EvtChn_DpcBounce(PRKDPC Dpc, PVOID Context, PVOID SystemArgument1, PVOID SystemArgument2)
42 {
43 ev_action_t *action = Context;
45 UNREFERENCED_PARAMETER(Dpc);
46 UNREFERENCED_PARAMETER(SystemArgument1);
47 UNREFERENCED_PARAMETER(SystemArgument2);
49 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
51 if (action->type == EVT_ACTION_TYPE_DPC)
52 {
53 action->ServiceRoutine(NULL, action->ServiceContext);
54 }
55 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
56 }
58 /* Called at DIRQL */
59 BOOLEAN
60 EvtChn_AckEvent(PVOID context, evtchn_port_t port)
61 {
62 PXENPCI_DEVICE_DATA xpdd = context;
63 ULONG evt_word;
64 ULONG evt_bit;
65 xen_ulong_t val;
67 evt_bit = port & (BITS_PER_LONG - 1);
68 evt_word = port >> BITS_PER_LONG_SHIFT;
70 val = synch_clear_bit(evt_bit, (volatile xen_long_t *)&xpdd->evtchn_pending_pvt[evt_word]);
72 return (BOOLEAN)!!val;
73 }
75 static DDKAPI BOOLEAN
76 EvtChn_Interrupt(PKINTERRUPT Interrupt, PVOID Context)
77 {
78 /*
79 For HVM domains, Xen always triggers the event on CPU0. Because the
80 interrupt is delivered via the virtual PCI device it might get delivered
81 to CPU != 0, but we should always use vcpu_info[0]
82 */
83 int cpu = 0;
84 vcpu_info_t *vcpu_info;
85 PXENPCI_DEVICE_DATA xpdd = (PXENPCI_DEVICE_DATA)Context;
86 shared_info_t *shared_info_area = xpdd->shared_info_area;
87 xen_ulong_t evt_words;
88 unsigned long evt_word;
89 unsigned long evt_bit;
90 unsigned int port;
91 ev_action_t *ev_action;
92 BOOLEAN handled = FALSE;
93 BOOLEAN deferred = FALSE;
94 int i;
96 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ " (cpu = %d)\n", KeGetCurrentProcessorNumber()));
98 if (xpdd->interrupts_masked)
99 {
100 KdPrint((__DRIVER_NAME " unhandled interrupt\n"));
101 return TRUE;
102 }
104 UNREFERENCED_PARAMETER(Interrupt);
106 for (i = 0; i < ARRAY_SIZE(xpdd->evtchn_pending_pvt); i++)
107 {
108 if (xpdd->evtchn_pending_pvt[i])
109 {
110 KdPrint((__DRIVER_NAME " Unacknowledged event word = %d, val = %p\n", i, xpdd->evtchn_pending_pvt[i]));
111 xpdd->evtchn_pending_pvt[i] = 0;
112 }
113 }
115 vcpu_info = &shared_info_area->vcpu_info[cpu];
117 vcpu_info->evtchn_upcall_pending = 0;
119 evt_words = (xen_ulong_t)xchg((volatile xen_long_t *)&vcpu_info->evtchn_pending_sel, 0);
121 while (bit_scan_forward(&evt_word, evt_words))
122 {
123 evt_words &= ~(1 << evt_word);
124 while (bit_scan_forward(&evt_bit, shared_info_area->evtchn_pending[evt_word] & ~shared_info_area->evtchn_mask[evt_word]))
125 {
126 synch_clear_bit(evt_bit, (volatile xen_long_t *)&shared_info_area->evtchn_pending[evt_word]);
127 handled = TRUE;
128 port = (evt_word << BITS_PER_LONG_SHIFT) + evt_bit;
129 ev_action = &xpdd->ev_actions[port];
130 ev_action->count++;
131 switch (ev_action->type)
132 {
133 case EVT_ACTION_TYPE_NORMAL:
134 ev_action->ServiceRoutine(NULL, ev_action->ServiceContext);
135 break;
136 case EVT_ACTION_TYPE_IRQ:
137 synch_set_bit(evt_bit, (volatile xen_long_t *)&xpdd->evtchn_pending_pvt[evt_word]);
138 deferred = TRUE;
139 break;
140 case EVT_ACTION_TYPE_DPC:
141 KeInsertQueueDpc(&ev_action->Dpc, NULL, NULL);
142 break;
143 case EVT_ACTION_TYPE_SUSPEND:
144 KdPrint((__DRIVER_NAME " EVT_ACTION_TYPE_SUSPEND\n"));
145 for (i = 0; i < ARRAY_SIZE(xpdd->evtchn_pending_pvt); i++)
146 {
147 if (xpdd->ev_actions[i].type == EVT_ACTION_TYPE_IRQ)
148 {
149 int suspend_bit = i & (BITS_PER_LONG - 1);
150 int suspend_word = i >> BITS_PER_LONG_SHIFT;
151 synch_set_bit(suspend_bit, (volatile xen_long_t *)&xpdd->evtchn_pending_pvt[suspend_word]);
152 }
153 }
154 deferred = TRUE;
155 break;
156 default:
157 KdPrint((__DRIVER_NAME " Unhandled Event!!!\n"));
158 break;
159 }
160 }
161 }
163 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
165 return handled && !deferred;
166 }
168 NTSTATUS
169 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
170 {
171 PXENPCI_DEVICE_DATA xpdd = Context;
173 FUNCTION_ENTER();
175 if (xpdd->ev_actions[Port].type != EVT_ACTION_TYPE_EMPTY)
176 {
177 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_EMPTY;
178 KeMemoryBarrier(); // make sure we don't call the old Service Routine with the new data...
179 KdPrint((__DRIVER_NAME " Handler for port %d already registered, replacing\n", Port));
180 }
182 xpdd->ev_actions[Port].ServiceRoutine = ServiceRoutine;
183 xpdd->ev_actions[Port].ServiceContext = ServiceContext;
184 xpdd->ev_actions[Port].xpdd = xpdd;
185 KeMemoryBarrier();
186 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_NORMAL;
188 EvtChn_Unmask(Context, Port);
190 FUNCTION_EXIT();
192 return STATUS_SUCCESS;
193 }
195 NTSTATUS
196 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
197 {
198 PXENPCI_DEVICE_DATA xpdd = Context;
200 FUNCTION_ENTER();
202 if (xpdd->ev_actions[Port].type != EVT_ACTION_TYPE_EMPTY)
203 {
204 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_EMPTY;
205 KeMemoryBarrier(); // make sure we don't call the old Service Routine with the new data...
206 KdPrint((__DRIVER_NAME " Handler for port %d already registered, replacing\n", Port));
207 }
209 xpdd->ev_actions[Port].ServiceRoutine = ServiceRoutine;
210 xpdd->ev_actions[Port].ServiceContext = ServiceContext;
211 xpdd->ev_actions[Port].xpdd = xpdd;
212 KeInitializeDpc(&xpdd->ev_actions[Port].Dpc, EvtChn_DpcBounce, &xpdd->ev_actions[Port]);
213 KeMemoryBarrier(); // make sure that the new service routine is only called once the context is set up
214 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_DPC;
216 EvtChn_Unmask(Context, Port);
218 FUNCTION_EXIT();
220 return STATUS_SUCCESS;
221 }
223 NTSTATUS
224 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector, PCHAR description)
225 {
226 PXENPCI_DEVICE_DATA xpdd = Context;
228 FUNCTION_ENTER();
230 if (xpdd->ev_actions[Port].type != EVT_ACTION_TYPE_EMPTY)
231 {
232 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_EMPTY;
233 KeMemoryBarrier(); // make sure we don't call the old Service Routine with the new data...
234 KdPrint((__DRIVER_NAME " Handler for port %d already registered, replacing\n", Port));
235 }
237 xpdd->ev_actions[Port].vector = vector;
238 xpdd->ev_actions[Port].xpdd = xpdd;
239 KeMemoryBarrier();
240 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_IRQ;
241 strncpy(xpdd->ev_actions[Port].description, description, 128);
243 EvtChn_Unmask(Context, Port);
245 FUNCTION_EXIT();
247 return STATUS_SUCCESS;
248 }
250 NTSTATUS
251 EvtChn_Unbind(PVOID Context, evtchn_port_t Port)
252 {
253 PXENPCI_DEVICE_DATA xpdd = Context;
254 int old_type;
256 EvtChn_Mask(Context, Port);
257 old_type = xpdd->ev_actions[Port].type;
258 xpdd->ev_actions[Port].type = EVT_ACTION_TYPE_EMPTY;
259 KeMemoryBarrier(); // make sure we don't call the old Service Routine with the new data...
260 xpdd->ev_actions[Port].ServiceRoutine = NULL;
261 xpdd->ev_actions[Port].ServiceContext = NULL;
263 if (old_type == EVT_ACTION_TYPE_DPC)
264 KeRemoveQueueDpc(&xpdd->ev_actions[Port].Dpc);
266 return STATUS_SUCCESS;
267 }
269 NTSTATUS
270 EvtChn_Mask(PVOID Context, evtchn_port_t port)
271 {
272 PXENPCI_DEVICE_DATA xpdd = Context;
274 synch_set_bit(port & (BITS_PER_LONG - 1),
275 (volatile xen_long_t *)&xpdd->shared_info_area->evtchn_mask[port >> BITS_PER_LONG_SHIFT]);
276 return STATUS_SUCCESS;
277 }
279 NTSTATUS
280 EvtChn_Unmask(PVOID context, evtchn_port_t port)
281 {
282 PXENPCI_DEVICE_DATA xpdd = context;
284 synch_clear_bit(port & (BITS_PER_LONG - 1),
285 (volatile xen_long_t *)&xpdd->shared_info_area->evtchn_mask[port >> BITS_PER_LONG_SHIFT]);
286 return STATUS_SUCCESS;
287 }
289 NTSTATUS
290 EvtChn_Notify(PVOID Context, evtchn_port_t Port)
291 {
292 PXENPCI_DEVICE_DATA xpdd = Context;
293 struct evtchn_send send;
295 send.port = Port;
296 (void)HYPERVISOR_event_channel_op(xpdd, EVTCHNOP_send, &send);
297 return STATUS_SUCCESS;
298 }
300 evtchn_port_t
301 EvtChn_BindIpi(PVOID context, ULONG vcpu)
302 {
303 PXENPCI_DEVICE_DATA xpdd = context;
304 evtchn_bind_ipi_t op;
306 FUNCTION_ENTER();
307 op.vcpu = vcpu;
308 op.port = 0;
309 HYPERVISOR_event_channel_op(xpdd, EVTCHNOP_bind_ipi, &op);
310 FUNCTION_EXIT();
311 return op.port;
312 }
314 evtchn_port_t
315 EvtChn_AllocUnbound(PVOID Context, domid_t Domain)
316 {
317 PXENPCI_DEVICE_DATA xpdd = Context;
318 evtchn_alloc_unbound_t op;
319 op.dom = DOMID_SELF;
320 op.remote_dom = Domain;
321 HYPERVISOR_event_channel_op(xpdd, EVTCHNOP_alloc_unbound, &op);
322 return op.port;
323 }
325 VOID
326 EvtChn_Close(PVOID Context, evtchn_port_t port )
327 {
328 PXENPCI_DEVICE_DATA xpdd = Context;
329 evtchn_close_t op;
330 op.port = port;
331 HYPERVISOR_event_channel_op(xpdd, EVTCHNOP_close, &op);
332 return;
333 }
335 NTSTATUS
336 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd)
337 {
338 int i;
340 FUNCTION_ENTER();
342 for (i = 0; i < NR_EVENTS; i++)
343 {
344 EvtChn_Mask(xpdd, i);
345 xpdd->ev_actions[i].type = EVT_ACTION_TYPE_EMPTY;
346 xpdd->ev_actions[i].count = 0;
347 }
349 for (i = 0; i < 8; i++)
350 {
351 xpdd->shared_info_area->evtchn_pending[i] = 0;
352 }
354 for (i = 0; i < MAX_VIRT_CPUS; i++)
355 {
356 xpdd->shared_info_area->vcpu_info[i].evtchn_upcall_pending = 0;
357 xpdd->shared_info_area->vcpu_info[i].evtchn_pending_sel = 0;
358 xpdd->shared_info_area->vcpu_info[i].evtchn_upcall_mask = 1; /* apparantly this doesn't do anything */
359 }
361 KeMemoryBarrier();
363 hvm_set_parameter(xpdd, HVM_PARAM_CALLBACK_IRQ, xpdd->irq_number);
365 for (i = 0; i < MAX_VIRT_CPUS; i++)
366 xpdd->shared_info_area->vcpu_info[i].evtchn_upcall_mask = 0;
367 xpdd->interrupts_masked = FALSE;
368 KeMemoryBarrier();
370 xpdd->suspend_evtchn = EvtChn_BindIpi(xpdd, 0);
371 xpdd->ev_actions[xpdd->suspend_evtchn].type = EVT_ACTION_TYPE_SUSPEND;
372 EvtChn_Unmask(xpdd, xpdd->suspend_evtchn);
374 KdPrint((__DRIVER_NAME " suspend_evtchn = %d\n", xpdd->suspend_evtchn));
376 FUNCTION_EXIT();
378 return STATUS_SUCCESS;
379 }
381 NTSTATUS
382 EvtChn_ConnectInterrupt(PXENPCI_DEVICE_DATA xpdd)
383 {
384 NTSTATUS status = STATUS_SUCCESS;
386 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
388 status = IoConnectInterrupt(
389 &xpdd->interrupt,
390 EvtChn_Interrupt,
391 xpdd,
392 NULL,
393 xpdd->irq_vector,
394 xpdd->irq_level,
395 xpdd->irq_level,
396 xpdd->irq_mode, //LevelSensitive,
397 TRUE,
398 xpdd->irq_affinity,
399 FALSE);
401 if (!NT_SUCCESS(status))
402 {
403 KdPrint((__DRIVER_NAME " IoConnectInterrupt failed 0x%08x\n", status));
404 return status;
405 }
407 return status;
408 }
410 NTSTATUS
411 EvtChn_Shutdown(PXENPCI_DEVICE_DATA xpdd)
412 {
413 int i;
414 // LARGE_INTEGER wait_time;
416 xpdd->interrupts_masked = TRUE;
417 for (i = 0; i < MAX_VIRT_CPUS; i++)
418 xpdd->shared_info_area->vcpu_info[i].evtchn_upcall_mask = 1;
419 KeMemoryBarrier();
420 hvm_set_parameter(xpdd, HVM_PARAM_CALLBACK_IRQ, 0);
422 for (i = 0; i < NR_EVENTS; i++)
423 {
424 if (xpdd->ev_actions[i].type == EVT_ACTION_TYPE_DPC)
425 {
426 KeRemoveQueueDpc(&xpdd->ev_actions[i].Dpc);
427 }
428 }
429 #if (NTDDI_VERSION >= NTDDI_WINXP)
430 KeFlushQueuedDpcs();
431 #endif
433 return STATUS_SUCCESS;
434 }