xen: Port Xen event channel driver from mini-os
[platform/kernel/u-boot.git] / drivers / xen / events.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4  * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
5  * (C) 2020 - EPAM Systems Inc.
6  *
7  * File: events.c [1]
8  * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
9  * Changes: Grzegorz Milos (gm281@cam.ac.uk)
10  *
11  * Date: Jul 2003, changes Jun 2005
12  *
13  * Description: Deals with events received on event channels
14  *
15  * [1] - http://xenbits.xen.org/gitweb/?p=mini-os.git;a=summary
16  */
17 #include <common.h>
18 #include <log.h>
19
20 #include <asm/io.h>
21 #include <asm/xen/system.h>
22
23 #include <xen/events.h>
24 #include <xen/hvm.h>
25
26 #define NR_EVS 1024
27
28 /**
29  * struct _ev_action - represents a event handler.
30  *
31  * Chaining or sharing is not allowed
32  */
33 struct _ev_action {
34         void (*handler)(evtchn_port_t port, struct pt_regs *regs, void *data);
35         void *data;
36         u32 count;
37 };
38
39 static struct _ev_action ev_actions[NR_EVS];
40 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *data);
41
42 static unsigned long bound_ports[NR_EVS / (8 * sizeof(unsigned long))];
43
44 void unbind_all_ports(void)
45 {
46         int i;
47         int cpu = 0;
48         struct shared_info *s = HYPERVISOR_shared_info;
49         struct vcpu_info *vcpu_info = &s->vcpu_info[cpu];
50
51         for (i = 0; i < NR_EVS; i++) {
52                 if (test_and_clear_bit(i, bound_ports)) {
53                         printf("port %d still bound!\n", i);
54                         unbind_evtchn(i);
55                 }
56         }
57         vcpu_info->evtchn_upcall_pending = 0;
58         vcpu_info->evtchn_pending_sel = 0;
59 }
60
61 int do_event(evtchn_port_t port, struct pt_regs *regs)
62 {
63         struct _ev_action *action;
64
65         clear_evtchn(port);
66
67         if (port >= NR_EVS) {
68                 printk("WARN: do_event(): Port number too large: %d\n", port);
69                 return 1;
70         }
71
72         action = &ev_actions[port];
73         action->count++;
74
75         /* call the handler */
76         action->handler(port, regs, action->data);
77
78         return 1;
79 }
80
81 evtchn_port_t bind_evtchn(evtchn_port_t port,
82                           void (*handler)(evtchn_port_t, struct pt_regs *, void *),
83                           void *data)
84 {
85         if (ev_actions[port].handler != default_handler)
86                 printf("WARN: Handler for port %d already registered, replacing\n",
87                        port);
88
89         ev_actions[port].data = data;
90         wmb();
91         ev_actions[port].handler = handler;
92         synch_set_bit(port, bound_ports);
93
94         return port;
95 }
96
97 /**
98  * unbind_evtchn() - Unbind event channel for selected port
99  */
100 void unbind_evtchn(evtchn_port_t port)
101 {
102         struct evtchn_close close;
103         int rc;
104
105         if (ev_actions[port].handler == default_handler)
106                 printf("WARN: No handler for port %d when unbinding\n", port);
107         mask_evtchn(port);
108         clear_evtchn(port);
109
110         ev_actions[port].handler = default_handler;
111         wmb();
112         ev_actions[port].data = NULL;
113         synch_clear_bit(port, bound_ports);
114
115         close.port = port;
116         rc = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
117         if (rc)
118                 printf("WARN: close_port %d failed rc=%d. ignored\n", port, rc);
119 }
120
121 void default_handler(evtchn_port_t port, struct pt_regs *regs, void *ignore)
122 {
123         debug("[Port %d] - event received\n", port);
124 }
125
126 /**
127  * evtchn_alloc_unbound() - Create a port available to the pal for
128  * exchanging notifications.
129  *
130  * Unfortunate confusion of terminology: the port is unbound as far
131  * as Xen is concerned, but we automatically bind a handler to it.
132  *
133  * Return: The result of the hypervisor call.
134  */
135 int evtchn_alloc_unbound(domid_t pal,
136                          void (*handler)(evtchn_port_t, struct pt_regs *, void *),
137                          void *data, evtchn_port_t *port)
138 {
139         int rc;
140
141         struct evtchn_alloc_unbound op;
142
143         op.dom = DOMID_SELF;
144         op.remote_dom = pal;
145         rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
146         if (rc) {
147                 printf("ERROR: alloc_unbound failed with rc=%d", rc);
148                 return rc;
149         }
150         if (!handler)
151                 handler = default_handler;
152         *port = bind_evtchn(op.port, handler, data);
153         return rc;
154 }
155
156 /**
157  * eventchn_poll() - Event channel polling function
158  *
159  * Check and process any pending events
160  */
161 void eventchn_poll(void)
162 {
163         do_hypervisor_callback(NULL);
164 }
165
166 /**
167  * init_events() - Initialize event handler
168  *
169  * Initially all events are without a handler and disabled.
170  */
171 void init_events(void)
172 {
173         int i;
174
175         debug("%s\n", __func__);
176
177         for (i = 0; i < NR_EVS; i++) {
178                 ev_actions[i].handler = default_handler;
179                 mask_evtchn(i);
180         }
181 }
182
183 /**
184  * fini_events() - Close all ports
185  *
186  * Mask and clear event channels. Close port using EVTCHNOP_close
187  * hypercall.
188  */
189 void fini_events(void)
190 {
191         debug("%s\n", __func__);
192         /* Dealloc all events */
193         unbind_all_ports();
194 }
195