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