1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2
3 /*
4 * Xen para-virtual DRM device
5 *
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
7 *
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
9 */
10
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_ioctl.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_file.h>
20 #include <drm/drm_gem.h>
21 #include <drm/drm_print.h>
22
23 #include <xen/platform_pci.h>
24 #include <xen/xen.h>
25 #include <xen/xenbus.h>
26
27 #include <xen/xen-front-pgdir-shbuf.h>
28 #include <xen/interface/io/displif.h>
29
30 #include "xen_drm_front.h"
31 #include "xen_drm_front_cfg.h"
32 #include "xen_drm_front_evtchnl.h"
33 #include "xen_drm_front_gem.h"
34 #include "xen_drm_front_kms.h"
35
36 struct xen_drm_front_dbuf {
37 struct list_head list;
38 u64 dbuf_cookie;
39 u64 fb_cookie;
40
41 struct xen_front_pgdir_shbuf shbuf;
42 };
43
dbuf_add_to_list(struct xen_drm_front_info * front_info,struct xen_drm_front_dbuf * dbuf,u64 dbuf_cookie)44 static void dbuf_add_to_list(struct xen_drm_front_info *front_info,
45 struct xen_drm_front_dbuf *dbuf, u64 dbuf_cookie)
46 {
47 dbuf->dbuf_cookie = dbuf_cookie;
48 list_add(&dbuf->list, &front_info->dbuf_list);
49 }
50
dbuf_get(struct list_head * dbuf_list,u64 dbuf_cookie)51 static struct xen_drm_front_dbuf *dbuf_get(struct list_head *dbuf_list,
52 u64 dbuf_cookie)
53 {
54 struct xen_drm_front_dbuf *buf, *q;
55
56 list_for_each_entry_safe(buf, q, dbuf_list, list)
57 if (buf->dbuf_cookie == dbuf_cookie)
58 return buf;
59
60 return NULL;
61 }
62
dbuf_free(struct list_head * dbuf_list,u64 dbuf_cookie)63 static void dbuf_free(struct list_head *dbuf_list, u64 dbuf_cookie)
64 {
65 struct xen_drm_front_dbuf *buf, *q;
66
67 list_for_each_entry_safe(buf, q, dbuf_list, list)
68 if (buf->dbuf_cookie == dbuf_cookie) {
69 list_del(&buf->list);
70 xen_front_pgdir_shbuf_unmap(&buf->shbuf);
71 xen_front_pgdir_shbuf_free(&buf->shbuf);
72 kfree(buf);
73 break;
74 }
75 }
76
dbuf_free_all(struct list_head * dbuf_list)77 static void dbuf_free_all(struct list_head *dbuf_list)
78 {
79 struct xen_drm_front_dbuf *buf, *q;
80
81 list_for_each_entry_safe(buf, q, dbuf_list, list) {
82 list_del(&buf->list);
83 xen_front_pgdir_shbuf_unmap(&buf->shbuf);
84 xen_front_pgdir_shbuf_free(&buf->shbuf);
85 kfree(buf);
86 }
87 }
88
89 static struct xendispl_req *
be_prepare_req(struct xen_drm_front_evtchnl * evtchnl,u8 operation)90 be_prepare_req(struct xen_drm_front_evtchnl *evtchnl, u8 operation)
91 {
92 struct xendispl_req *req;
93
94 req = RING_GET_REQUEST(&evtchnl->u.req.ring,
95 evtchnl->u.req.ring.req_prod_pvt);
96 req->operation = operation;
97 req->id = evtchnl->evt_next_id++;
98 evtchnl->evt_id = req->id;
99 return req;
100 }
101
be_stream_do_io(struct xen_drm_front_evtchnl * evtchnl,struct xendispl_req * req)102 static int be_stream_do_io(struct xen_drm_front_evtchnl *evtchnl,
103 struct xendispl_req *req)
104 {
105 reinit_completion(&evtchnl->u.req.completion);
106 if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
107 return -EIO;
108
109 xen_drm_front_evtchnl_flush(evtchnl);
110 return 0;
111 }
112
be_stream_wait_io(struct xen_drm_front_evtchnl * evtchnl)113 static int be_stream_wait_io(struct xen_drm_front_evtchnl *evtchnl)
114 {
115 if (wait_for_completion_timeout(&evtchnl->u.req.completion,
116 msecs_to_jiffies(XEN_DRM_FRONT_WAIT_BACK_MS)) <= 0)
117 return -ETIMEDOUT;
118
119 return evtchnl->u.req.resp_status;
120 }
121
xen_drm_front_mode_set(struct xen_drm_front_drm_pipeline * pipeline,u32 x,u32 y,u32 width,u32 height,u32 bpp,u64 fb_cookie)122 int xen_drm_front_mode_set(struct xen_drm_front_drm_pipeline *pipeline,
123 u32 x, u32 y, u32 width, u32 height,
124 u32 bpp, u64 fb_cookie)
125 {
126 struct xen_drm_front_evtchnl *evtchnl;
127 struct xen_drm_front_info *front_info;
128 struct xendispl_req *req;
129 unsigned long flags;
130 int ret;
131
132 front_info = pipeline->drm_info->front_info;
133 evtchnl = &front_info->evt_pairs[pipeline->index].req;
134 if (unlikely(!evtchnl))
135 return -EIO;
136
137 mutex_lock(&evtchnl->u.req.req_io_lock);
138
139 spin_lock_irqsave(&front_info->io_lock, flags);
140 req = be_prepare_req(evtchnl, XENDISPL_OP_SET_CONFIG);
141 req->op.set_config.x = x;
142 req->op.set_config.y = y;
143 req->op.set_config.width = width;
144 req->op.set_config.height = height;
145 req->op.set_config.bpp = bpp;
146 req->op.set_config.fb_cookie = fb_cookie;
147
148 ret = be_stream_do_io(evtchnl, req);
149 spin_unlock_irqrestore(&front_info->io_lock, flags);
150
151 if (ret == 0)
152 ret = be_stream_wait_io(evtchnl);
153
154 mutex_unlock(&evtchnl->u.req.req_io_lock);
155 return ret;
156 }
157
xen_drm_front_dbuf_create(struct xen_drm_front_info * front_info,u64 dbuf_cookie,u32 width,u32 height,u32 bpp,u64 size,u32 offset,struct page ** pages)158 int xen_drm_front_dbuf_create(struct xen_drm_front_info *front_info,
159 u64 dbuf_cookie, u32 width, u32 height,
160 u32 bpp, u64 size, u32 offset,
161 struct page **pages)
162 {
163 struct xen_drm_front_evtchnl *evtchnl;
164 struct xen_drm_front_dbuf *dbuf;
165 struct xendispl_req *req;
166 struct xen_front_pgdir_shbuf_cfg buf_cfg;
167 unsigned long flags;
168 int ret;
169
170 evtchnl = &front_info->evt_pairs[GENERIC_OP_EVT_CHNL].req;
171 if (unlikely(!evtchnl))
172 return -EIO;
173
174 dbuf = kzalloc_obj(*dbuf);
175 if (!dbuf)
176 return -ENOMEM;
177
178 dbuf_add_to_list(front_info, dbuf, dbuf_cookie);
179
180 memset(&buf_cfg, 0, sizeof(buf_cfg));
181 buf_cfg.xb_dev = front_info->xb_dev;
182 buf_cfg.num_pages = DIV_ROUND_UP(size, PAGE_SIZE);
183 buf_cfg.pages = pages;
184 buf_cfg.pgdir = &dbuf->shbuf;
185 buf_cfg.be_alloc = front_info->cfg.be_alloc;
186
187 ret = xen_front_pgdir_shbuf_alloc(&buf_cfg);
188 if (ret < 0)
189 goto fail_shbuf_alloc;
190
191 mutex_lock(&evtchnl->u.req.req_io_lock);
192
193 spin_lock_irqsave(&front_info->io_lock, flags);
194 req = be_prepare_req(evtchnl, XENDISPL_OP_DBUF_CREATE);
195 req->op.dbuf_create.gref_directory =
196 xen_front_pgdir_shbuf_get_dir_start(&dbuf->shbuf);
197 req->op.dbuf_create.buffer_sz = size;
198 req->op.dbuf_create.data_ofs = offset;
199 req->op.dbuf_create.dbuf_cookie = dbuf_cookie;
200 req->op.dbuf_create.width = width;
201 req->op.dbuf_create.height = height;
202 req->op.dbuf_create.bpp = bpp;
203 if (buf_cfg.be_alloc)
204 req->op.dbuf_create.flags |= XENDISPL_DBUF_FLG_REQ_ALLOC;
205
206 ret = be_stream_do_io(evtchnl, req);
207 spin_unlock_irqrestore(&front_info->io_lock, flags);
208
209 if (ret < 0)
210 goto fail;
211
212 ret = be_stream_wait_io(evtchnl);
213 if (ret < 0)
214 goto fail;
215
216 ret = xen_front_pgdir_shbuf_map(&dbuf->shbuf);
217 if (ret < 0)
218 goto fail;
219
220 mutex_unlock(&evtchnl->u.req.req_io_lock);
221 return 0;
222
223 fail:
224 mutex_unlock(&evtchnl->u.req.req_io_lock);
225 fail_shbuf_alloc:
226 dbuf_free(&front_info->dbuf_list, dbuf_cookie);
227 return ret;
228 }
229
xen_drm_front_dbuf_destroy(struct xen_drm_front_info * front_info,u64 dbuf_cookie)230 static int xen_drm_front_dbuf_destroy(struct xen_drm_front_info *front_info,
231 u64 dbuf_cookie)
232 {
233 struct xen_drm_front_evtchnl *evtchnl;
234 struct xendispl_req *req;
235 unsigned long flags;
236 bool be_alloc;
237 int ret;
238
239 evtchnl = &front_info->evt_pairs[GENERIC_OP_EVT_CHNL].req;
240 if (unlikely(!evtchnl))
241 return -EIO;
242
243 be_alloc = front_info->cfg.be_alloc;
244
245 /*
246 * For the backend allocated buffer release references now, so backend
247 * can free the buffer.
248 */
249 if (be_alloc)
250 dbuf_free(&front_info->dbuf_list, dbuf_cookie);
251
252 mutex_lock(&evtchnl->u.req.req_io_lock);
253
254 spin_lock_irqsave(&front_info->io_lock, flags);
255 req = be_prepare_req(evtchnl, XENDISPL_OP_DBUF_DESTROY);
256 req->op.dbuf_destroy.dbuf_cookie = dbuf_cookie;
257
258 ret = be_stream_do_io(evtchnl, req);
259 spin_unlock_irqrestore(&front_info->io_lock, flags);
260
261 if (ret == 0)
262 ret = be_stream_wait_io(evtchnl);
263
264 /*
265 * Do this regardless of communication status with the backend:
266 * if we cannot remove remote resources remove what we can locally.
267 */
268 if (!be_alloc)
269 dbuf_free(&front_info->dbuf_list, dbuf_cookie);
270
271 mutex_unlock(&evtchnl->u.req.req_io_lock);
272 return ret;
273 }
274
xen_drm_front_fb_attach(struct xen_drm_front_info * front_info,u64 dbuf_cookie,u64 fb_cookie,u32 width,u32 height,u32 pixel_format)275 int xen_drm_front_fb_attach(struct xen_drm_front_info *front_info,
276 u64 dbuf_cookie, u64 fb_cookie, u32 width,
277 u32 height, u32 pixel_format)
278 {
279 struct xen_drm_front_evtchnl *evtchnl;
280 struct xen_drm_front_dbuf *buf;
281 struct xendispl_req *req;
282 unsigned long flags;
283 int ret;
284
285 evtchnl = &front_info->evt_pairs[GENERIC_OP_EVT_CHNL].req;
286 if (unlikely(!evtchnl))
287 return -EIO;
288
289 buf = dbuf_get(&front_info->dbuf_list, dbuf_cookie);
290 if (!buf)
291 return -EINVAL;
292
293 buf->fb_cookie = fb_cookie;
294
295 mutex_lock(&evtchnl->u.req.req_io_lock);
296
297 spin_lock_irqsave(&front_info->io_lock, flags);
298 req = be_prepare_req(evtchnl, XENDISPL_OP_FB_ATTACH);
299 req->op.fb_attach.dbuf_cookie = dbuf_cookie;
300 req->op.fb_attach.fb_cookie = fb_cookie;
301 req->op.fb_attach.width = width;
302 req->op.fb_attach.height = height;
303 req->op.fb_attach.pixel_format = pixel_format;
304
305 ret = be_stream_do_io(evtchnl, req);
306 spin_unlock_irqrestore(&front_info->io_lock, flags);
307
308 if (ret == 0)
309 ret = be_stream_wait_io(evtchnl);
310
311 mutex_unlock(&evtchnl->u.req.req_io_lock);
312 return ret;
313 }
314
xen_drm_front_fb_detach(struct xen_drm_front_info * front_info,u64 fb_cookie)315 int xen_drm_front_fb_detach(struct xen_drm_front_info *front_info,
316 u64 fb_cookie)
317 {
318 struct xen_drm_front_evtchnl *evtchnl;
319 struct xendispl_req *req;
320 unsigned long flags;
321 int ret;
322
323 evtchnl = &front_info->evt_pairs[GENERIC_OP_EVT_CHNL].req;
324 if (unlikely(!evtchnl))
325 return -EIO;
326
327 mutex_lock(&evtchnl->u.req.req_io_lock);
328
329 spin_lock_irqsave(&front_info->io_lock, flags);
330 req = be_prepare_req(evtchnl, XENDISPL_OP_FB_DETACH);
331 req->op.fb_detach.fb_cookie = fb_cookie;
332
333 ret = be_stream_do_io(evtchnl, req);
334 spin_unlock_irqrestore(&front_info->io_lock, flags);
335
336 if (ret == 0)
337 ret = be_stream_wait_io(evtchnl);
338
339 mutex_unlock(&evtchnl->u.req.req_io_lock);
340 return ret;
341 }
342
xen_drm_front_page_flip(struct xen_drm_front_info * front_info,int conn_idx,u64 fb_cookie)343 int xen_drm_front_page_flip(struct xen_drm_front_info *front_info,
344 int conn_idx, u64 fb_cookie)
345 {
346 struct xen_drm_front_evtchnl *evtchnl;
347 struct xendispl_req *req;
348 unsigned long flags;
349 int ret;
350
351 if (unlikely(conn_idx >= front_info->num_evt_pairs))
352 return -EINVAL;
353
354 evtchnl = &front_info->evt_pairs[conn_idx].req;
355
356 mutex_lock(&evtchnl->u.req.req_io_lock);
357
358 spin_lock_irqsave(&front_info->io_lock, flags);
359 req = be_prepare_req(evtchnl, XENDISPL_OP_PG_FLIP);
360 req->op.pg_flip.fb_cookie = fb_cookie;
361
362 ret = be_stream_do_io(evtchnl, req);
363 spin_unlock_irqrestore(&front_info->io_lock, flags);
364
365 if (ret == 0)
366 ret = be_stream_wait_io(evtchnl);
367
368 mutex_unlock(&evtchnl->u.req.req_io_lock);
369 return ret;
370 }
371
xen_drm_front_on_frame_done(struct xen_drm_front_info * front_info,int conn_idx,u64 fb_cookie)372 void xen_drm_front_on_frame_done(struct xen_drm_front_info *front_info,
373 int conn_idx, u64 fb_cookie)
374 {
375 struct xen_drm_front_drm_info *drm_info = front_info->drm_info;
376
377 if (unlikely(conn_idx >= front_info->cfg.num_connectors))
378 return;
379
380 xen_drm_front_kms_on_frame_done(&drm_info->pipeline[conn_idx],
381 fb_cookie);
382 }
383
xen_drm_front_gem_object_free(struct drm_gem_object * obj)384 void xen_drm_front_gem_object_free(struct drm_gem_object *obj)
385 {
386 struct xen_drm_front_drm_info *drm_info = obj->dev->dev_private;
387 int idx;
388
389 if (drm_dev_enter(obj->dev, &idx)) {
390 xen_drm_front_dbuf_destroy(drm_info->front_info,
391 xen_drm_front_dbuf_to_cookie(obj));
392 drm_dev_exit(idx);
393 } else {
394 dbuf_free(&drm_info->front_info->dbuf_list,
395 xen_drm_front_dbuf_to_cookie(obj));
396 }
397
398 xen_drm_front_gem_free_object_unlocked(obj);
399 }
400
xen_drm_drv_dumb_create(struct drm_file * filp,struct drm_device * dev,struct drm_mode_create_dumb * args)401 static int xen_drm_drv_dumb_create(struct drm_file *filp,
402 struct drm_device *dev,
403 struct drm_mode_create_dumb *args)
404 {
405 struct xen_drm_front_drm_info *drm_info = dev->dev_private;
406 struct drm_gem_object *obj;
407 int ret;
408
409 /*
410 * Dumb creation is a two stage process: first we create a fully
411 * constructed GEM object which is communicated to the backend, and
412 * only after that we can create GEM's handle. This is done so,
413 * because of the possible races: once you create a handle it becomes
414 * immediately visible to user-space, so the latter can try accessing
415 * object without pages etc.
416 * For details also see drm_gem_handle_create
417 */
418 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
419 args->size = args->pitch * args->height;
420
421 obj = xen_drm_front_gem_create(dev, args->size);
422 if (IS_ERR(obj)) {
423 ret = PTR_ERR(obj);
424 goto fail;
425 }
426
427 ret = xen_drm_front_dbuf_create(drm_info->front_info,
428 xen_drm_front_dbuf_to_cookie(obj),
429 args->width, args->height, args->bpp,
430 args->size, 0,
431 xen_drm_front_gem_get_pages(obj));
432 if (ret)
433 goto fail_backend;
434
435 /* This is the tail of GEM object creation */
436 ret = drm_gem_handle_create(filp, obj, &args->handle);
437 if (ret)
438 goto fail_handle;
439
440 /* Drop reference from allocate - handle holds it now */
441 drm_gem_object_put(obj);
442 return 0;
443
444 fail_handle:
445 xen_drm_front_dbuf_destroy(drm_info->front_info,
446 xen_drm_front_dbuf_to_cookie(obj));
447 fail_backend:
448 /* drop reference from allocate */
449 drm_gem_object_put(obj);
450 fail:
451 DRM_ERROR("Failed to create dumb buffer: %d\n", ret);
452 return ret;
453 }
454
xen_drm_drv_release(struct drm_device * dev)455 static void xen_drm_drv_release(struct drm_device *dev)
456 {
457 struct xen_drm_front_drm_info *drm_info = dev->dev_private;
458 struct xen_drm_front_info *front_info = drm_info->front_info;
459
460 xen_drm_front_kms_fini(drm_info);
461
462 drm_atomic_helper_shutdown(dev);
463 drm_mode_config_cleanup(dev);
464
465 if (front_info->cfg.be_alloc)
466 xenbus_switch_state(front_info->xb_dev,
467 XenbusStateInitialising);
468
469 kfree(drm_info);
470 }
471
472 DEFINE_DRM_GEM_FOPS(xen_drm_dev_fops);
473
474 static const struct drm_driver xen_drm_driver = {
475 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
476 .release = xen_drm_drv_release,
477 .gem_prime_import_sg_table = xen_drm_front_gem_import_sg_table,
478 .dumb_create = xen_drm_drv_dumb_create,
479 .fops = &xen_drm_dev_fops,
480 .name = "xendrm-du",
481 .desc = "Xen PV DRM Display Unit",
482 .major = 1,
483 .minor = 0,
484
485 };
486
xen_drm_drv_init(struct xen_drm_front_info * front_info)487 static int xen_drm_drv_init(struct xen_drm_front_info *front_info)
488 {
489 struct device *dev = &front_info->xb_dev->dev;
490 struct xen_drm_front_drm_info *drm_info;
491 struct drm_device *drm_dev;
492 int ret;
493
494 if (drm_firmware_drivers_only())
495 return -ENODEV;
496
497 DRM_INFO("Creating %s\n", xen_drm_driver.desc);
498
499 drm_info = kzalloc_obj(*drm_info);
500 if (!drm_info) {
501 ret = -ENOMEM;
502 goto fail;
503 }
504
505 drm_info->front_info = front_info;
506 front_info->drm_info = drm_info;
507
508 drm_dev = drm_dev_alloc(&xen_drm_driver, dev);
509 if (IS_ERR(drm_dev)) {
510 ret = PTR_ERR(drm_dev);
511 goto fail_dev;
512 }
513
514 drm_info->drm_dev = drm_dev;
515
516 drm_dev->dev_private = drm_info;
517
518 ret = xen_drm_front_kms_init(drm_info);
519 if (ret) {
520 DRM_ERROR("Failed to initialize DRM/KMS, ret %d\n", ret);
521 goto fail_modeset;
522 }
523
524 ret = drm_dev_register(drm_dev, 0);
525 if (ret)
526 goto fail_register;
527
528 return 0;
529
530 fail_register:
531 drm_dev_unregister(drm_dev);
532 fail_modeset:
533 drm_kms_helper_poll_fini(drm_dev);
534 drm_mode_config_cleanup(drm_dev);
535 drm_dev_put(drm_dev);
536 fail_dev:
537 kfree(drm_info);
538 front_info->drm_info = NULL;
539 fail:
540 return ret;
541 }
542
xen_drm_drv_fini(struct xen_drm_front_info * front_info)543 static void xen_drm_drv_fini(struct xen_drm_front_info *front_info)
544 {
545 struct xen_drm_front_drm_info *drm_info = front_info->drm_info;
546 struct drm_device *dev;
547
548 if (!drm_info)
549 return;
550
551 dev = drm_info->drm_dev;
552 if (!dev)
553 return;
554
555 /* Nothing to do if device is already unplugged */
556 if (drm_dev_is_unplugged(dev))
557 return;
558
559 drm_kms_helper_poll_fini(dev);
560 drm_dev_unplug(dev);
561 drm_dev_put(dev);
562
563 front_info->drm_info = NULL;
564
565 xen_drm_front_evtchnl_free_all(front_info);
566 dbuf_free_all(&front_info->dbuf_list);
567
568 /*
569 * If we are not using backend allocated buffers, then tell the
570 * backend we are ready to (re)initialize. Otherwise, wait for
571 * drm_driver.release.
572 */
573 if (!front_info->cfg.be_alloc)
574 xenbus_switch_state(front_info->xb_dev,
575 XenbusStateInitialising);
576 }
577
displback_initwait(struct xen_drm_front_info * front_info)578 static int displback_initwait(struct xen_drm_front_info *front_info)
579 {
580 struct xen_drm_front_cfg *cfg = &front_info->cfg;
581 int ret;
582
583 cfg->front_info = front_info;
584 ret = xen_drm_front_cfg_card(front_info, cfg);
585 if (ret < 0)
586 return ret;
587
588 DRM_INFO("Have %d connector(s)\n", cfg->num_connectors);
589 /* Create event channels for all connectors and publish */
590 ret = xen_drm_front_evtchnl_create_all(front_info);
591 if (ret < 0)
592 return ret;
593
594 return xen_drm_front_evtchnl_publish_all(front_info);
595 }
596
displback_connect(struct xen_drm_front_info * front_info)597 static int displback_connect(struct xen_drm_front_info *front_info)
598 {
599 xen_drm_front_evtchnl_set_state(front_info, EVTCHNL_STATE_CONNECTED);
600 return xen_drm_drv_init(front_info);
601 }
602
displback_disconnect(struct xen_drm_front_info * front_info)603 static void displback_disconnect(struct xen_drm_front_info *front_info)
604 {
605 if (!front_info->drm_info)
606 return;
607
608 /* Tell the backend to wait until we release the DRM driver. */
609 xenbus_switch_state(front_info->xb_dev, XenbusStateReconfiguring);
610
611 xen_drm_drv_fini(front_info);
612 }
613
displback_changed(struct xenbus_device * xb_dev,enum xenbus_state backend_state)614 static void displback_changed(struct xenbus_device *xb_dev,
615 enum xenbus_state backend_state)
616 {
617 struct xen_drm_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
618 int ret;
619
620 DRM_DEBUG("Backend state is %s, front is %s\n",
621 xenbus_strstate(backend_state),
622 xenbus_strstate(xb_dev->state));
623
624 switch (backend_state) {
625 case XenbusStateReconfiguring:
626 case XenbusStateReconfigured:
627 case XenbusStateInitialised:
628 break;
629
630 case XenbusStateInitialising:
631 if (xb_dev->state == XenbusStateReconfiguring)
632 break;
633
634 /* recovering after backend unexpected closure */
635 displback_disconnect(front_info);
636 break;
637
638 case XenbusStateInitWait:
639 if (xb_dev->state == XenbusStateReconfiguring)
640 break;
641
642 /* recovering after backend unexpected closure */
643 displback_disconnect(front_info);
644 if (xb_dev->state != XenbusStateInitialising)
645 break;
646
647 ret = displback_initwait(front_info);
648 if (ret < 0)
649 xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
650 else
651 xenbus_switch_state(xb_dev, XenbusStateInitialised);
652 break;
653
654 case XenbusStateConnected:
655 if (xb_dev->state != XenbusStateInitialised)
656 break;
657
658 ret = displback_connect(front_info);
659 if (ret < 0) {
660 displback_disconnect(front_info);
661 xenbus_dev_fatal(xb_dev, ret, "connecting backend");
662 } else {
663 xenbus_switch_state(xb_dev, XenbusStateConnected);
664 }
665 break;
666
667 case XenbusStateClosing:
668 /*
669 * in this state backend starts freeing resources,
670 * so let it go into closed state, so we can also
671 * remove ours
672 */
673 break;
674
675 case XenbusStateUnknown:
676 case XenbusStateClosed:
677 if (xb_dev->state == XenbusStateClosed)
678 break;
679
680 displback_disconnect(front_info);
681 break;
682 }
683 }
684
xen_drv_probe(struct xenbus_device * xb_dev,const struct xenbus_device_id * id)685 static int xen_drv_probe(struct xenbus_device *xb_dev,
686 const struct xenbus_device_id *id)
687 {
688 struct xen_drm_front_info *front_info;
689 struct device *dev = &xb_dev->dev;
690 int ret;
691
692 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64));
693 if (ret < 0) {
694 DRM_ERROR("Cannot setup DMA mask, ret %d", ret);
695 return ret;
696 }
697
698 front_info = devm_kzalloc(&xb_dev->dev,
699 sizeof(*front_info), GFP_KERNEL);
700 if (!front_info)
701 return -ENOMEM;
702
703 front_info->xb_dev = xb_dev;
704 spin_lock_init(&front_info->io_lock);
705 INIT_LIST_HEAD(&front_info->dbuf_list);
706 dev_set_drvdata(&xb_dev->dev, front_info);
707
708 return xenbus_switch_state(xb_dev, XenbusStateInitialising);
709 }
710
xen_drv_remove(struct xenbus_device * dev)711 static void xen_drv_remove(struct xenbus_device *dev)
712 {
713 struct xen_drm_front_info *front_info = dev_get_drvdata(&dev->dev);
714 int to = 100;
715
716 xenbus_switch_state(dev, XenbusStateClosing);
717
718 /*
719 * On driver removal it is disconnected from XenBus,
720 * so no backend state change events come via .otherend_changed
721 * callback. This prevents us from exiting gracefully, e.g.
722 * signaling the backend to free event channels, waiting for its
723 * state to change to XenbusStateClosed and cleaning at our end.
724 * Normally when front driver removed backend will finally go into
725 * XenbusStateInitWait state.
726 *
727 * Workaround: read backend's state manually and wait with time-out.
728 */
729 while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
730 XenbusStateUnknown) != XenbusStateInitWait) &&
731 --to)
732 msleep(10);
733
734 if (!to) {
735 unsigned int state;
736
737 state = xenbus_read_unsigned(front_info->xb_dev->otherend,
738 "state", XenbusStateUnknown);
739 DRM_ERROR("Backend state is %s while removing driver\n",
740 xenbus_strstate(state));
741 }
742
743 xen_drm_drv_fini(front_info);
744 xenbus_frontend_closed(dev);
745 }
746
747 static const struct xenbus_device_id xen_driver_ids[] = {
748 { XENDISPL_DRIVER_NAME },
749 { "" }
750 };
751
752 static struct xenbus_driver xen_driver = {
753 .ids = xen_driver_ids,
754 .probe = xen_drv_probe,
755 .remove = xen_drv_remove,
756 .otherend_changed = displback_changed,
757 .not_essential = true,
758 };
759
xen_drv_init(void)760 static int __init xen_drv_init(void)
761 {
762 /* At the moment we only support case with XEN_PAGE_SIZE == PAGE_SIZE */
763 if (XEN_PAGE_SIZE != PAGE_SIZE) {
764 DRM_ERROR(XENDISPL_DRIVER_NAME ": different kernel and Xen page sizes are not supported: XEN_PAGE_SIZE (%lu) != PAGE_SIZE (%lu)\n",
765 XEN_PAGE_SIZE, PAGE_SIZE);
766 return -ENODEV;
767 }
768
769 if (!xen_domain())
770 return -ENODEV;
771
772 if (!xen_has_pv_devices())
773 return -ENODEV;
774
775 DRM_INFO("Registering XEN PV " XENDISPL_DRIVER_NAME "\n");
776 return xenbus_register_frontend(&xen_driver);
777 }
778
xen_drv_fini(void)779 static void __exit xen_drv_fini(void)
780 {
781 DRM_INFO("Unregistering XEN PV " XENDISPL_DRIVER_NAME "\n");
782 xenbus_unregister_driver(&xen_driver);
783 }
784
785 module_init(xen_drv_init);
786 module_exit(xen_drv_fini);
787
788 MODULE_DESCRIPTION("Xen para-virtualized display device frontend");
789 MODULE_LICENSE("GPL");
790 MODULE_ALIAS("xen:" XENDISPL_DRIVER_NAME);
791