xref: /freebsd/sys/xen/evtchn/evtchnvar.h (revision 02e9120893770924227138ba49df1edb3896112a)
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 
33 #ifndef __XEN_EVTCHN_EVTCHNVAR_H__
34 #define __XEN_EVTCHN_EVTCHNVAR_H__
35 
36 #include <xen/hypervisor.h>
37 #include <contrib/xen/event_channel.h>
38 
39 /* Macros for accessing event channel values */
40 #define	EVTCHN_PTR(type, port) \
41 	(HYPERVISOR_shared_info->evtchn_##type + ((port) / __LONG_BIT))
42 #define	EVTCHN_BIT(port)	((port) & (__LONG_BIT - 1))
43 #define	EVTCHN_MASK(port)	(1UL << EVTCHN_BIT(port))
44 
45 /**
46  * Disable signal delivery for an event channel port, returning its
47  * previous mask state.
48  *
49  * \param port  The event channel port to query and mask.
50  *
51  * \returns  1 if event delivery was previously disabled.  Otherwise 0.
52  */
53 static inline int
54 evtchn_test_and_set_mask(evtchn_port_t port)
55 {
56 
57 	return (atomic_testandset_xen_ulong(EVTCHN_PTR(mask, port),
58 	    EVTCHN_BIT(port)));
59 }
60 
61 /**
62  * Clear any pending event for the given event channel port.
63  *
64  * \param port  The event channel port to clear.
65  */
66 static inline void
67 evtchn_clear_port(evtchn_port_t port)
68 {
69 
70 	atomic_clear_xen_ulong(EVTCHN_PTR(pending, port), EVTCHN_MASK(port));
71 }
72 
73 /**
74  * Disable signal delivery for an event channel port.
75  *
76  * \param port  The event channel port to mask.
77  */
78 static inline void
79 evtchn_mask_port(evtchn_port_t port)
80 {
81 
82 	atomic_set_xen_ulong(EVTCHN_PTR(mask, port), EVTCHN_MASK(port));
83 }
84 
85 /**
86  * Enable signal delivery for an event channel port.
87  *
88  * \param port  The event channel port to enable.
89  */
90 static inline void
91 evtchn_unmask_port(evtchn_port_t port)
92 {
93 	evtchn_unmask_t op = { .port = port };
94 
95 	HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
96 }
97 
98 #endif /* __XEN_EVTCHN_EVTCHNVAR_H__ */
99