xref: /freebsd/sys/xen/evtchn/evtchnvar.h (revision 4358928e235c1e188ad6b4650d78bcceb225b909)
1 /******************************************************************************
2  * evtchn.h
3  *
4  * Data structures and definitions private to the FreeBSD implementation
5  * of the Xen event channel API.
6  *
7  * Copyright (c) 2004, K A Fraser
8  * Copyright (c) 2012, Spectra Logic Corporation
9  * Copyright © 2022, Elliott Mitchell
10  *
11  * This file may be distributed separately from the Linux kernel, or
12  * incorporated into other software packages, subject to the following license:
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this source file (the "Software"), to deal in the Software without
16  * restriction, including without limitation the rights to use, copy, modify,
17  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in
22  * all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * IN THE SOFTWARE.
31  *
32  * $FreeBSD$
33  */
34 
35 #ifndef __XEN_EVTCHN_EVTCHNVAR_H__
36 #define __XEN_EVTCHN_EVTCHNVAR_H__
37 
38 #include <xen/hypervisor.h>
39 #include <contrib/xen/event_channel.h>
40 
41 enum evtchn_type {
42 	EVTCHN_TYPE_UNBOUND,
43 	EVTCHN_TYPE_VIRQ,
44 	EVTCHN_TYPE_IPI,
45 	EVTCHN_TYPE_PORT,
46 	EVTCHN_TYPE_COUNT
47 };
48 
49 /** Submit a port notification for delivery to a userland evtchn consumer */
50 void evtchn_device_upcall(evtchn_port_t port);
51 
52 /* Macros for accessing event channel values */
53 #define	EVTCHN_PTR(type, port) \
54 	(HYPERVISOR_shared_info->evtchn_##type + ((port) / __LONG_BIT))
55 #define	EVTCHN_BIT(port)	((port) & (__LONG_BIT - 1))
56 #define	EVTCHN_MASK(port)	(1UL << EVTCHN_BIT(port))
57 
58 /**
59  * Disable signal delivery for an event channel port, returning its
60  * previous mask state.
61  *
62  * \param port  The event channel port to query and mask.
63  *
64  * \returns  1 if event delivery was previously disabled.  Otherwise 0.
65  */
66 static inline int
67 evtchn_test_and_set_mask(evtchn_port_t port)
68 {
69 
70 	return (atomic_testandset_long(EVTCHN_PTR(mask, port),
71 	    EVTCHN_BIT(port)));
72 }
73 
74 /**
75  * Clear any pending event for the given event channel port.
76  *
77  * \param port  The event channel port to clear.
78  */
79 static inline void
80 evtchn_clear_port(evtchn_port_t port)
81 {
82 
83 	atomic_clear_long(EVTCHN_PTR(pending, port), EVTCHN_MASK(port));
84 }
85 
86 /**
87  * Disable signal delivery for an event channel port.
88  *
89  * \param port  The event channel port to mask.
90  */
91 static inline void
92 evtchn_mask_port(evtchn_port_t port)
93 {
94 
95 	atomic_set_long(EVTCHN_PTR(mask, port), EVTCHN_MASK(port));
96 }
97 
98 /**
99  * Enable signal delivery for an event channel port.
100  *
101  * \param port  The event channel port to enable.
102  */
103 static inline void
104 evtchn_unmask_port(evtchn_port_t port)
105 {
106 	evtchn_unmask_t op = { .port = port };
107 
108 	HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
109 }
110 
111 #endif /* __XEN_EVTCHN_EVTCHNVAR_H__ */
112