1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
* Xen para-virtual DRM device
*
* Copyright (C) 2016-2018 EPAM Systems Inc.
*
* Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
*/
#include <linux/errno.h>
#include <linux/irq.h>
#include <drm/drm_print.h>
#include <xen/xenbus.h>
#include <xen/events.h>
#include <xen/grant_table.h>
#include "xen_drm_front.h"
#include "xen_drm_front_evtchnl.h"
static irqreturn_t evtchnl_interrupt_ctrl(int irq, void *dev_id)
{
struct xen_drm_front_evtchnl *evtchnl = dev_id;
struct xen_drm_front_info *front_info = evtchnl->front_info;
struct xendispl_resp *resp;
RING_IDX i, rp;
unsigned long flags;
if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
return IRQ_HANDLED;
spin_lock_irqsave(&front_info->io_lock, flags);
again:
rp = evtchnl->u.req.ring.sring->rsp_prod;
/* ensure we see queued responses up to rp */
virt_rmb();
for (i = evtchnl->u.req.ring.rsp_cons; i != rp; i++) {
resp = RING_GET_RESPONSE(&evtchnl->u.req.ring, i);
if (unlikely(resp->id != evtchnl->evt_id))
continue;
switch (resp->operation) {
case XENDISPL_OP_PG_FLIP:
case XENDISPL_OP_FB_ATTACH:
case XENDISPL_OP_FB_DETACH:
case XENDISPL_OP_DBUF_CREATE:
case XENDISPL_OP_DBUF_DESTROY:
case XENDISPL_OP_SET_CONFIG:
evtchnl->u.req.resp_status = resp->status;
complete(&evtchnl->u.req.completion);
break;
default:
DRM_ERROR("Operation %d is not supported\n",
resp->operation);
break;
}
}
evtchnl->u.req.ring.rsp_cons = i;
if (i != evtchnl->u.req.ring.req_prod_pvt) {
int more_to_do;
RING_FINAL_CHECK_FOR_RESPONSES(&evtchnl->u.req.ring,
more_to_do);
if (more_to_do)
goto again;
} else {
evtchnl->u.req.ring.sring->rsp_event = i + 1;
}
spin_unlock_irqrestore(&front_info->io_lock, flags);
return IRQ_HANDLED;
}
static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
{
struct xen_drm_front_evtchnl *evtchnl = dev_id;
struct xen_drm_front_info *front_info = evtchnl->front_info;
struct xendispl_event_page *page = evtchnl->u.evt.page;
u32 cons, prod;
unsigned long flags;
if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
return IRQ_HANDLED;
spin_lock_irqsave(&front_info->io_lock, flags);
prod = page->in_prod;
/* ensure we see ring contents up to prod */
virt_rmb();
if (prod == page->in_cons)
goto out;
for (cons = page->in_cons; cons != prod; cons++) {
struct xendispl_evt *event;
event = &XENDISPL_IN_RING_REF(page, cons);
if (unlikely(event->id != evtchnl->evt_id++))
continue;
switch (event->type) {
case XENDISPL_EVT_PG_FLIP:
xen_drm_front_on_frame_done(front_info, evtchnl->index,
event->op.pg_flip.fb_cookie);
break;
}
}
page->in_cons = cons;
/* ensure ring contents */
virt_wmb();
out:
spin_unlock_irqrestore(&front_info->io_lock, flags);
return IRQ_HANDLED;
}
static void evtchnl_free(struct xen_drm_front_info *front_info,
struct xen_drm_front_evtchnl *evtchnl)
{
void *page = NULL;
if (evtchnl->type == EVTCHNL_TYPE_REQ)
page = evtchnl->u.req.ring.sring;
else if (evtchnl->type == EVTCHNL_TYPE_EVT)
page = evtchnl->u.evt.page;
if (!page)
return;
evtchnl->state = EVTCHNL_STATE_DISCONNECTED;
if (evtchnl->type == EVTCHNL_TYPE_REQ) {
/* release all who still waits for response if any */
evtchnl->u.req.resp_status = -EIO;
complete_all(&evtchnl->u.req.completion);
}
if (evtchnl->irq)
unbind_from_irqhandler(evtchnl->irq, evtchnl);
if (evtchnl->port)
xenbus_free_evtchn(front_info->xb_dev, evtchnl->port);
/* end access and free the page */
xenbus_teardown_ring(&page, 1, &evtchnl->gref);
memset(evtchnl, 0, sizeof(*evtchnl));
}
static int evtchnl_alloc(struct xen_drm_front_info *front_info, int index,
struct xen_drm_front_evtchnl *evtchnl,
enum xen_drm_front_evtchnl_type type)
{
struct xenbus_device *xb_dev = front_info->xb_dev;
void *page;
irq_handler_t handler;
int ret;
memset(evtchnl, 0, sizeof(*evtchnl));
evtchnl->type = type;
evtchnl->index = index;
evtchnl->front_info = front_info;
evtchnl->state = EVTCHNL_STATE_DISCONNECTED;
ret = xenbus_setup_ring(xb_dev, GFP_NOIO | __GFP_HIGH, &page,
1, &evtchnl->gref);
if (ret)
goto fail;
if (type == EVTCHNL_TYPE_REQ) {
struct xen_displif_sring *sring;
init_completion(&evtchnl->u.req.completion);
mutex_init(&evtchnl->u.req.req_io_lock);
sring = page;
XEN_FRONT_RING_INIT(&evtchnl->u.req.ring, sring, XEN_PAGE_SIZE);
handler = evtchnl_interrupt_ctrl;
} else {
evtchnl->u.evt.page = page;
handler = evtchnl_interrupt_evt;
}
ret = xenbus_alloc_evtchn(xb_dev, &evtchnl->port);
if (ret < 0)
goto fail;
ret = bind_evtchn_to_irqhandler(evtchnl->port,
handler, 0, xb_dev->devicetype,
evtchnl);
if (ret < 0)
goto fail;
evtchnl->irq = ret;
return 0;
fail:
DRM_ERROR("Failed to allocate ring: %d\n", ret);
return ret;
}
int xen_drm_front_evtchnl_create_all(struct xen_drm_front_info *front_info)
{
struct xen_drm_front_cfg *cfg;
int ret, conn;
cfg = &front_info->cfg;
front_info->evt_pairs =
kcalloc(cfg->num_connectors,
sizeof(struct xen_drm_front_evtchnl_pair),
GFP_KERNEL);
if (!front_info->evt_pairs) {
ret = -ENOMEM;
goto fail;
}
for (conn = 0; conn < cfg->num_connectors; conn++) {
ret = evtchnl_alloc(front_info, conn,
&front_info->evt_pairs[conn].req,
EVTCHNL_TYPE_REQ);
if (ret < 0) {
DRM_ERROR("Error allocating control channel\n");
goto fail;
}
ret = evtchnl_alloc(front_info, conn,
&front_info->evt_pairs[conn].evt,
EVTCHNL_TYPE_EVT);
if (ret < 0) {
DRM_ERROR("Error allocating in-event channel\n");
goto fail;
}
}
front_info->num_evt_pairs = cfg->num_connectors;
return 0;
fail:
xen_drm_front_evtchnl_free_all(front_info);
return ret;
}
static int evtchnl_publish(struct xenbus_transaction xbt,
struct xen_drm_front_evtchnl *evtchnl,
const char *path, const char *node_ring,
const char *node_chnl)
{
struct xenbus_device *xb_dev = evtchnl->front_info->xb_dev;
int ret;
/* write control channel ring reference */
ret = xenbus_printf(xbt, path, node_ring, "%u", evtchnl->gref);
if (ret < 0) {
xenbus_dev_error(xb_dev, ret, "writing ring-ref");
return ret;
}
/* write event channel ring reference */
ret = xenbus_printf(xbt, path, node_chnl, "%u", evtchnl->port);
if (ret < 0) {
xenbus_dev_error(xb_dev, ret, "writing event channel");
return ret;
}
return 0;
}
int xen_drm_front_evtchnl_publish_all(struct xen_drm_front_info *front_info)
{
struct xenbus_transaction xbt;
struct xen_drm_front_cfg *plat_data;
int ret, conn;
plat_data = &front_info->cfg;
again:
ret = xenbus_transaction_start(&xbt);
if (ret < 0) {
xenbus_dev_fatal(front_info->xb_dev, ret,
"starting transaction");
return ret;
}
for (conn = 0; conn < plat_data->num_connectors; conn++) {
ret = evtchnl_publish(xbt, &front_info->evt_pairs[conn].req,
plat_data->connectors[conn].xenstore_path,
XENDISPL_FIELD_REQ_RING_REF,
XENDISPL_FIELD_REQ_CHANNEL);
if (ret < 0)
goto fail;
ret = evtchnl_publish(xbt, &front_info->evt_pairs[conn].evt,
plat_data->connectors[conn].xenstore_path,
XENDISPL_FIELD_EVT_RING_REF,
XENDISPL_FIELD_EVT_CHANNEL);
if (ret < 0)
goto fail;
}
ret = xenbus_transaction_end(xbt, 0);
if (ret < 0) {
if (ret == -EAGAIN)
goto again;
xenbus_dev_fatal(front_info->xb_dev, ret,
"completing transaction");
goto fail_to_end;
}
return 0;
fail:
xenbus_transaction_end(xbt, 1);
fail_to_end:
xenbus_dev_fatal(front_info->xb_dev, ret, "writing Xen store");
return ret;
}
void xen_drm_front_evtchnl_flush(struct xen_drm_front_evtchnl *evtchnl)
{
int notify;
evtchnl->u.req.ring.req_prod_pvt++;
RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&evtchnl->u.req.ring, notify);
if (notify)
notify_remote_via_irq(evtchnl->irq);
}
void xen_drm_front_evtchnl_set_state(struct xen_drm_front_info *front_info,
enum xen_drm_front_evtchnl_state state)
{
unsigned long flags;
int i;
if (!front_info->evt_pairs)
return;
spin_lock_irqsave(&front_info->io_lock, flags);
for (i = 0; i < front_info->num_evt_pairs; i++) {
front_info->evt_pairs[i].req.state = state;
front_info->evt_pairs[i].evt.state = state;
}
spin_unlock_irqrestore(&front_info->io_lock, flags);
}
void xen_drm_front_evtchnl_free_all(struct xen_drm_front_info *front_info)
{
int i;
if (!front_info->evt_pairs)
return;
for (i = 0; i < front_info->num_evt_pairs; i++) {
evtchnl_free(front_info, &front_info->evt_pairs[i].req);
evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
}
kfree(front_info->evt_pairs);
front_info->evt_pairs = NULL;
}
|