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	KASSERT(port < nitems(HYPERVISOR_shared_info->evtchn_##type) *	\
42	    sizeof(xen_ulong_t) * 8, ("Invalid event channel port"));	\
43	(HYPERVISOR_shared_info->evtchn_##type + ((port) / __LONG_BIT));\
44})
45#define	EVTCHN_BIT(port)	((port) & (__LONG_BIT - 1))
46#define	EVTCHN_MASK(port)	(1UL << EVTCHN_BIT(port))
47
48/**
49 * Disable signal delivery for an event channel port, returning its
50 * previous mask state.
51 *
52 * \param port  The event channel port to query and mask.
53 *
54 * \returns  1 if event delivery was previously disabled.  Otherwise 0.
55 */
56static inline int
57evtchn_test_and_set_mask(evtchn_port_t port)
58{
59
60	return (atomic_testandset_xen_ulong(EVTCHN_PTR(mask, port),
61	    EVTCHN_BIT(port)));
62}
63
64/**
65 * Clear any pending event for the given event channel port.
66 *
67 * \param port  The event channel port to clear.
68 */
69static inline void
70evtchn_clear_port(evtchn_port_t port)
71{
72
73	atomic_clear_xen_ulong(EVTCHN_PTR(pending, port), EVTCHN_MASK(port));
74}
75
76/**
77 * Disable signal delivery for an event channel port.
78 *
79 * \param port  The event channel port to mask.
80 */
81static inline void
82evtchn_mask_port(evtchn_port_t port)
83{
84
85	atomic_set_xen_ulong(EVTCHN_PTR(mask, port), EVTCHN_MASK(port));
86}
87
88/**
89 * Enable signal delivery for an event channel port.
90 *
91 * \param port  The event channel port to enable.
92 */
93static inline void
94evtchn_unmask_port(evtchn_port_t port)
95{
96	evtchn_unmask_t op = { .port = port };
97
98	HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
99}
100
101#endif /* __XEN_EVTCHN_EVTCHNVAR_H__ */
102