176acc41fSJustin T. Gibbs /******************************************************************************
276acc41fSJustin T. Gibbs * evtchn.h
376acc41fSJustin T. Gibbs *
476acc41fSJustin T. Gibbs * Data structures and definitions private to the FreeBSD implementation
576acc41fSJustin T. Gibbs * of the Xen event channel API.
676acc41fSJustin T. Gibbs *
776acc41fSJustin T. Gibbs * Copyright (c) 2004, K A Fraser
876acc41fSJustin T. Gibbs * Copyright (c) 2012, Spectra Logic Corporation
99f3be3a6SElliott Mitchell * Copyright © 2022, Elliott Mitchell
1076acc41fSJustin T. Gibbs *
1176acc41fSJustin T. Gibbs * This file may be distributed separately from the Linux kernel, or
1276acc41fSJustin T. Gibbs * incorporated into other software packages, subject to the following license:
1376acc41fSJustin T. Gibbs *
1476acc41fSJustin T. Gibbs * Permission is hereby granted, free of charge, to any person obtaining a copy
1576acc41fSJustin T. Gibbs * of this source file (the "Software"), to deal in the Software without
1676acc41fSJustin T. Gibbs * restriction, including without limitation the rights to use, copy, modify,
1776acc41fSJustin T. Gibbs * merge, publish, distribute, sublicense, and/or sell copies of the Software,
1876acc41fSJustin T. Gibbs * and to permit persons to whom the Software is furnished to do so, subject to
1976acc41fSJustin T. Gibbs * the following conditions:
2076acc41fSJustin T. Gibbs *
2176acc41fSJustin T. Gibbs * The above copyright notice and this permission notice shall be included in
2276acc41fSJustin T. Gibbs * all copies or substantial portions of the Software.
2376acc41fSJustin T. Gibbs *
2476acc41fSJustin T. Gibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2576acc41fSJustin T. Gibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2676acc41fSJustin T. Gibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2776acc41fSJustin T. Gibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2876acc41fSJustin T. Gibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2976acc41fSJustin T. Gibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3076acc41fSJustin T. Gibbs * IN THE SOFTWARE.
3176acc41fSJustin T. Gibbs */
3276acc41fSJustin T. Gibbs
3376acc41fSJustin T. Gibbs #ifndef __XEN_EVTCHN_EVTCHNVAR_H__
3476acc41fSJustin T. Gibbs #define __XEN_EVTCHN_EVTCHNVAR_H__
3576acc41fSJustin T. Gibbs
3676acc41fSJustin T. Gibbs #include <xen/hypervisor.h>
37ad7dd514SElliott Mitchell #include <contrib/xen/event_channel.h>
3876acc41fSJustin T. Gibbs
399f3be3a6SElliott Mitchell /* Macros for accessing event channel values */
40*4ece7996SRoger Pau Monné #define EVTCHN_PTR(type, port) ({ \
41*4ece7996SRoger Pau Monné KASSERT(port < nitems(HYPERVISOR_shared_info->evtchn_##type) * \
42*4ece7996SRoger Pau Monné sizeof(xen_ulong_t) * 8, ("Invalid event channel port")); \
43*4ece7996SRoger Pau Monné (HYPERVISOR_shared_info->evtchn_##type + ((port) / __LONG_BIT));\
44*4ece7996SRoger Pau Monné })
459f3be3a6SElliott Mitchell #define EVTCHN_BIT(port) ((port) & (__LONG_BIT - 1))
469f3be3a6SElliott Mitchell #define EVTCHN_MASK(port) (1UL << EVTCHN_BIT(port))
479f3be3a6SElliott Mitchell
4876acc41fSJustin T. Gibbs /**
4976acc41fSJustin T. Gibbs * Disable signal delivery for an event channel port, returning its
5076acc41fSJustin T. Gibbs * previous mask state.
5176acc41fSJustin T. Gibbs *
5276acc41fSJustin T. Gibbs * \param port The event channel port to query and mask.
5376acc41fSJustin T. Gibbs *
5476acc41fSJustin T. Gibbs * \returns 1 if event delivery was previously disabled. Otherwise 0.
5576acc41fSJustin T. Gibbs */
5676acc41fSJustin T. Gibbs static inline int
evtchn_test_and_set_mask(evtchn_port_t port)5776acc41fSJustin T. Gibbs evtchn_test_and_set_mask(evtchn_port_t port)
5876acc41fSJustin T. Gibbs {
599f3be3a6SElliott Mitchell
604c9e6ad3SElliott Mitchell return (atomic_testandset_xen_ulong(EVTCHN_PTR(mask, port),
619f3be3a6SElliott Mitchell EVTCHN_BIT(port)));
6276acc41fSJustin T. Gibbs }
6376acc41fSJustin T. Gibbs
6476acc41fSJustin T. Gibbs /**
6576acc41fSJustin T. Gibbs * Clear any pending event for the given event channel port.
6676acc41fSJustin T. Gibbs *
6776acc41fSJustin T. Gibbs * \param port The event channel port to clear.
6876acc41fSJustin T. Gibbs */
6976acc41fSJustin T. Gibbs static inline void
evtchn_clear_port(evtchn_port_t port)7076acc41fSJustin T. Gibbs evtchn_clear_port(evtchn_port_t port)
7176acc41fSJustin T. Gibbs {
729f3be3a6SElliott Mitchell
734c9e6ad3SElliott Mitchell atomic_clear_xen_ulong(EVTCHN_PTR(pending, port), EVTCHN_MASK(port));
7476acc41fSJustin T. Gibbs }
7576acc41fSJustin T. Gibbs
7676acc41fSJustin T. Gibbs /**
7776acc41fSJustin T. Gibbs * Disable signal delivery for an event channel port.
7876acc41fSJustin T. Gibbs *
7976acc41fSJustin T. Gibbs * \param port The event channel port to mask.
8076acc41fSJustin T. Gibbs */
8176acc41fSJustin T. Gibbs static inline void
evtchn_mask_port(evtchn_port_t port)8276acc41fSJustin T. Gibbs evtchn_mask_port(evtchn_port_t port)
8376acc41fSJustin T. Gibbs {
8476acc41fSJustin T. Gibbs
854c9e6ad3SElliott Mitchell atomic_set_xen_ulong(EVTCHN_PTR(mask, port), EVTCHN_MASK(port));
8676acc41fSJustin T. Gibbs }
8776acc41fSJustin T. Gibbs
8876acc41fSJustin T. Gibbs /**
8976acc41fSJustin T. Gibbs * Enable signal delivery for an event channel port.
9076acc41fSJustin T. Gibbs *
9176acc41fSJustin T. Gibbs * \param port The event channel port to enable.
9276acc41fSJustin T. Gibbs */
9376acc41fSJustin T. Gibbs static inline void
evtchn_unmask_port(evtchn_port_t port)9476acc41fSJustin T. Gibbs evtchn_unmask_port(evtchn_port_t port)
9576acc41fSJustin T. Gibbs {
9676acc41fSJustin T. Gibbs evtchn_unmask_t op = { .port = port };
9776acc41fSJustin T. Gibbs
9876acc41fSJustin T. Gibbs HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &op);
9976acc41fSJustin T. Gibbs }
10076acc41fSJustin T. Gibbs
10176acc41fSJustin T. Gibbs #endif /* __XEN_EVTCHN_EVTCHNVAR_H__ */
102