1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 * Xen para-virtual DRM device
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
11 #include <linux/errno.h>
12 #include <linux/irq.h>
14 #include <drm/drm_print.h>
16 #include <xen/xenbus.h>
17 #include <xen/events.h>
18 #include <xen/grant_table.h>
20 #include "xen_drm_front.h"
21 #include "xen_drm_front_evtchnl.h"
23 static irqreturn_t evtchnl_interrupt_ctrl(int irq, void *dev_id)
25 struct xen_drm_front_evtchnl *evtchnl = dev_id;
26 struct xen_drm_front_info *front_info = evtchnl->front_info;
27 struct xendispl_resp *resp;
31 if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
34 spin_lock_irqsave(&front_info->io_lock, flags);
37 rp = evtchnl->u.req.ring.sring->rsp_prod;
38 /* ensure we see queued responses up to rp */
41 for (i = evtchnl->u.req.ring.rsp_cons; i != rp; i++) {
42 resp = RING_GET_RESPONSE(&evtchnl->u.req.ring, i);
43 if (unlikely(resp->id != evtchnl->evt_id))
46 switch (resp->operation) {
47 case XENDISPL_OP_PG_FLIP:
48 case XENDISPL_OP_FB_ATTACH:
49 case XENDISPL_OP_FB_DETACH:
50 case XENDISPL_OP_DBUF_CREATE:
51 case XENDISPL_OP_DBUF_DESTROY:
52 case XENDISPL_OP_SET_CONFIG:
53 evtchnl->u.req.resp_status = resp->status;
54 complete(&evtchnl->u.req.completion);
58 DRM_ERROR("Operation %d is not supported\n",
64 evtchnl->u.req.ring.rsp_cons = i;
66 if (i != evtchnl->u.req.ring.req_prod_pvt) {
69 RING_FINAL_CHECK_FOR_RESPONSES(&evtchnl->u.req.ring,
74 evtchnl->u.req.ring.sring->rsp_event = i + 1;
77 spin_unlock_irqrestore(&front_info->io_lock, flags);
81 static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
83 struct xen_drm_front_evtchnl *evtchnl = dev_id;
84 struct xen_drm_front_info *front_info = evtchnl->front_info;
85 struct xendispl_event_page *page = evtchnl->u.evt.page;
89 if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
92 spin_lock_irqsave(&front_info->io_lock, flags);
95 /* ensure we see ring contents up to prod */
97 if (prod == page->in_cons)
100 for (cons = page->in_cons; cons != prod; cons++) {
101 struct xendispl_evt *event;
103 event = &XENDISPL_IN_RING_REF(page, cons);
104 if (unlikely(event->id != evtchnl->evt_id++))
107 switch (event->type) {
108 case XENDISPL_EVT_PG_FLIP:
109 xen_drm_front_on_frame_done(front_info, evtchnl->index,
110 event->op.pg_flip.fb_cookie);
114 page->in_cons = cons;
115 /* ensure ring contents */
119 spin_unlock_irqrestore(&front_info->io_lock, flags);
123 static void evtchnl_free(struct xen_drm_front_info *front_info,
124 struct xen_drm_front_evtchnl *evtchnl)
128 if (evtchnl->type == EVTCHNL_TYPE_REQ)
129 page = evtchnl->u.req.ring.sring;
130 else if (evtchnl->type == EVTCHNL_TYPE_EVT)
131 page = evtchnl->u.evt.page;
135 evtchnl->state = EVTCHNL_STATE_DISCONNECTED;
137 if (evtchnl->type == EVTCHNL_TYPE_REQ) {
138 /* release all who still waits for response if any */
139 evtchnl->u.req.resp_status = -EIO;
140 complete_all(&evtchnl->u.req.completion);
144 unbind_from_irqhandler(evtchnl->irq, evtchnl);
147 xenbus_free_evtchn(front_info->xb_dev, evtchnl->port);
149 /* end access and free the page */
150 xenbus_teardown_ring(&page, 1, &evtchnl->gref);
152 memset(evtchnl, 0, sizeof(*evtchnl));
155 static int evtchnl_alloc(struct xen_drm_front_info *front_info, int index,
156 struct xen_drm_front_evtchnl *evtchnl,
157 enum xen_drm_front_evtchnl_type type)
159 struct xenbus_device *xb_dev = front_info->xb_dev;
161 irq_handler_t handler;
164 memset(evtchnl, 0, sizeof(*evtchnl));
165 evtchnl->type = type;
166 evtchnl->index = index;
167 evtchnl->front_info = front_info;
168 evtchnl->state = EVTCHNL_STATE_DISCONNECTED;
170 ret = xenbus_setup_ring(xb_dev, GFP_NOIO | __GFP_HIGH, &page,
175 if (type == EVTCHNL_TYPE_REQ) {
176 struct xen_displif_sring *sring;
178 init_completion(&evtchnl->u.req.completion);
179 mutex_init(&evtchnl->u.req.req_io_lock);
181 XEN_FRONT_RING_INIT(&evtchnl->u.req.ring, sring, XEN_PAGE_SIZE);
183 handler = evtchnl_interrupt_ctrl;
185 evtchnl->u.evt.page = page;
186 handler = evtchnl_interrupt_evt;
189 ret = xenbus_alloc_evtchn(xb_dev, &evtchnl->port);
193 ret = bind_evtchn_to_irqhandler(evtchnl->port,
194 handler, 0, xb_dev->devicetype,
203 DRM_ERROR("Failed to allocate ring: %d\n", ret);
207 int xen_drm_front_evtchnl_create_all(struct xen_drm_front_info *front_info)
209 struct xen_drm_front_cfg *cfg;
212 cfg = &front_info->cfg;
214 front_info->evt_pairs =
215 kcalloc(cfg->num_connectors,
216 sizeof(struct xen_drm_front_evtchnl_pair),
218 if (!front_info->evt_pairs) {
223 for (conn = 0; conn < cfg->num_connectors; conn++) {
224 ret = evtchnl_alloc(front_info, conn,
225 &front_info->evt_pairs[conn].req,
228 DRM_ERROR("Error allocating control channel\n");
232 ret = evtchnl_alloc(front_info, conn,
233 &front_info->evt_pairs[conn].evt,
236 DRM_ERROR("Error allocating in-event channel\n");
240 front_info->num_evt_pairs = cfg->num_connectors;
244 xen_drm_front_evtchnl_free_all(front_info);
248 static int evtchnl_publish(struct xenbus_transaction xbt,
249 struct xen_drm_front_evtchnl *evtchnl,
250 const char *path, const char *node_ring,
251 const char *node_chnl)
253 struct xenbus_device *xb_dev = evtchnl->front_info->xb_dev;
256 /* write control channel ring reference */
257 ret = xenbus_printf(xbt, path, node_ring, "%u", evtchnl->gref);
259 xenbus_dev_error(xb_dev, ret, "writing ring-ref");
263 /* write event channel ring reference */
264 ret = xenbus_printf(xbt, path, node_chnl, "%u", evtchnl->port);
266 xenbus_dev_error(xb_dev, ret, "writing event channel");
273 int xen_drm_front_evtchnl_publish_all(struct xen_drm_front_info *front_info)
275 struct xenbus_transaction xbt;
276 struct xen_drm_front_cfg *plat_data;
279 plat_data = &front_info->cfg;
282 ret = xenbus_transaction_start(&xbt);
284 xenbus_dev_fatal(front_info->xb_dev, ret,
285 "starting transaction");
289 for (conn = 0; conn < plat_data->num_connectors; conn++) {
290 ret = evtchnl_publish(xbt, &front_info->evt_pairs[conn].req,
291 plat_data->connectors[conn].xenstore_path,
292 XENDISPL_FIELD_REQ_RING_REF,
293 XENDISPL_FIELD_REQ_CHANNEL);
297 ret = evtchnl_publish(xbt, &front_info->evt_pairs[conn].evt,
298 plat_data->connectors[conn].xenstore_path,
299 XENDISPL_FIELD_EVT_RING_REF,
300 XENDISPL_FIELD_EVT_CHANNEL);
305 ret = xenbus_transaction_end(xbt, 0);
310 xenbus_dev_fatal(front_info->xb_dev, ret,
311 "completing transaction");
318 xenbus_transaction_end(xbt, 1);
321 xenbus_dev_fatal(front_info->xb_dev, ret, "writing Xen store");
325 void xen_drm_front_evtchnl_flush(struct xen_drm_front_evtchnl *evtchnl)
329 evtchnl->u.req.ring.req_prod_pvt++;
330 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&evtchnl->u.req.ring, notify);
332 notify_remote_via_irq(evtchnl->irq);
335 void xen_drm_front_evtchnl_set_state(struct xen_drm_front_info *front_info,
336 enum xen_drm_front_evtchnl_state state)
341 if (!front_info->evt_pairs)
344 spin_lock_irqsave(&front_info->io_lock, flags);
345 for (i = 0; i < front_info->num_evt_pairs; i++) {
346 front_info->evt_pairs[i].req.state = state;
347 front_info->evt_pairs[i].evt.state = state;
349 spin_unlock_irqrestore(&front_info->io_lock, flags);
352 void xen_drm_front_evtchnl_free_all(struct xen_drm_front_info *front_info)
356 if (!front_info->evt_pairs)
359 for (i = 0; i < front_info->num_evt_pairs; i++) {
360 evtchnl_free(front_info, &front_info->evt_pairs[i].req);
361 evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
364 kfree(front_info->evt_pairs);
365 front_info->evt_pairs = NULL;