1 // SPDX-License-Identifier: GPL-2.0
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
5 * (C) 2020 - EPAM Systems Inc.
8 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
9 * Changes: Grzegorz Milos (gm281@cam.ac.uk)
11 * Date: Jul 2003, changes Jun 2005
13 * Description: Deals with events received on event channels
15 * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
21 #include <asm/xen/system.h>
23 #include <xen/events.h>
26 #if CONFIG_IS_ENABLED(XEN_SERIAL)
27 extern u32 console_evtchn;
28 #endif /* CONFIG_IS_ENABLED(XEN_SERIAL) */
33 * struct _ev_action - represents a event handler.
35 * Chaining or sharing is not allowed
38 void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
43 static struct _ev_action ev_actions[NR_EVS];
44 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
46 static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];
48 void unbind_all_ports(void)
52 struct shared_info *s = HYPERVISOR_shared_info;
53 struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
55 for (i = 0; i < NR_EVS; i++) {
56 #if CONFIG_IS_ENABLED(XEN_SERIAL)
57 if (i == console_evtchn)
59 #endif /* CONFIG_IS_ENABLED(XEN_SERIAL) */
61 if (test_and_clear_bit(i, bound_ports)) {
62 printf("port %d still bound!\n", i);
66 vcpu_info->evtchn_upcall_pending = 0;
67 vcpu_info->evtchn_pending_sel = 0;
70 int do_event(evtchn_port_t port, struct pt_regs *regs)
72 struct _ev_action *action;
77 printk("WARN: do_event(): Port number too large: %d\n", port);
81 action = &ev_actions[port];
84 /* call the handler */
85 action->handler(port, regs, action->data);
90 evtchn_port_t bind_evtchn(evtchn_port_t port,
91 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
94 if (ev_actions[port].handler != default_handler)
95 printf("WARN: Handler for port %d already registered, replacing\n",
98 ev_actions[port].data = data;
100 ev_actions[port].handler = handler;
101 synch_set_bit(port, bound_ports);
107 * unbind_evtchn() - Unbind event channel for selected port
109 void unbind_evtchn(evtchn_port_t port)
111 struct evtchn_close close;
114 if (ev_actions[port].handler == default_handler)
115 debug("Default handler for port %d when unbinding\n", port);
119 ev_actions[port].handler = default_handler;
121 ev_actions[port].data = NULL;
122 synch_clear_bit(port, bound_ports);
125 rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
127 printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
130 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
132 debug("[Port %d] - event received\n", port);
136 * evtchn_alloc_unbound() - Create a port available to the pal for
137 * exchanging notifications.
139 * Unfortunate confusion of terminology: the port is unbound as far
140 * as Xen is concerned, but we automatically bind a handler to it.
142 * Return: The result of the hypervisor call.
144 int evtchn_alloc_unbound(domid_t pal,
145 void (*handler)(evtchn_port_t, struct pt_regs *, void *),
146 void *data, evtchn_port_t *port)
150 struct evtchn_alloc_unbound op;
154 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
156 printf("ERROR: alloc_unbound failed with rc=%d", rc);
160 handler = default_handler;
161 *port = bind_evtchn(op.port, handler, data);
166 * eventchn_poll() - Event channel polling function
168 * Check and process any pending events
170 void eventchn_poll(void)
172 do_hypervisor_callback(NULL);
176 * init_events() - Initialize event handler
178 * Initially all events are without a handler and disabled.
180 void init_events(void)
184 debug("%s\n", __func__);
186 for (i = 0; i < NR_EVS; i++) {
187 ev_actions[i].handler = default_handler;
193 * fini_events() - Close all ports
195 * Mask and clear event channels. Close port using EVTCHNOP_close
198 void fini_events(void)
200 debug("%s\n", __func__);
201 /* Dealloc all events */