xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision 889ad63d41eea20184b0483e7e585e5b20fb6cfe)
1f64122c1SDave Airlie /*
2f64122c1SDave Airlie  * Copyright 2013 Red Hat Inc.
3f64122c1SDave Airlie  *
4f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
7f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10f64122c1SDave Airlie  *
11f64122c1SDave Airlie  * The above copyright notice and this permission notice shall be included in
12f64122c1SDave Airlie  * all copies or substantial portions of the Software.
13f64122c1SDave Airlie  *
14f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f64122c1SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
21f64122c1SDave Airlie  *
22f64122c1SDave Airlie  * Authors: Dave Airlie
23f64122c1SDave Airlie  *          Alon Levy
24f64122c1SDave Airlie  */
25f64122c1SDave Airlie 
26c5416d66SRandy Dunlap #include <linux/crc32.h>
27edaf492cSMasahiro Yamada #include <drm/drm_crtc_helper.h>
283cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
291277eed5SGabriel Krisman Bertazi #include <drm/drm_atomic_helper.h>
3010a0bd89SGabriel Krisman Bertazi #include <drm/drm_atomic.h>
31f64122c1SDave Airlie 
32edaf492cSMasahiro Yamada #include "qxl_drv.h"
33edaf492cSMasahiro Yamada #include "qxl_object.h"
34edaf492cSMasahiro Yamada 
3507f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
3607f8d9bdSDave Airlie {
3707f8d9bdSDave Airlie 	return head->width && head->height;
3807f8d9bdSDave Airlie }
3907f8d9bdSDave Airlie 
40e4a76442SChristophe Fergeau static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
41f64122c1SDave Airlie {
42f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
43f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
44f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
4562c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
46f64122c1SDave Airlie 	}
47f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
48f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
49f64122c1SDave Airlie 				sizeof(struct qxl_monitors_config) +
50f64122c1SDave Airlie 				sizeof(struct qxl_head) * count, GFP_KERNEL);
51f64122c1SDave Airlie 		if (!qdev->client_monitors_config) {
52f64122c1SDave Airlie 			qxl_io_log(qdev,
53f64122c1SDave Airlie 				   "%s: allocation failure for %u heads\n",
54f64122c1SDave Airlie 				   __func__, count);
55f64122c1SDave Airlie 			return;
56f64122c1SDave Airlie 		}
57f64122c1SDave Airlie 	}
58f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
59f64122c1SDave Airlie }
60f64122c1SDave Airlie 
619e3b3178SChristophe Fergeau enum {
629e3b3178SChristophe Fergeau 	MONITORS_CONFIG_MODIFIED,
639e3b3178SChristophe Fergeau 	MONITORS_CONFIG_UNCHANGED,
649e3b3178SChristophe Fergeau 	MONITORS_CONFIG_BAD_CRC,
659e3b3178SChristophe Fergeau };
669e3b3178SChristophe Fergeau 
67f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
68f64122c1SDave Airlie {
69f64122c1SDave Airlie 	int i;
70f64122c1SDave Airlie 	int num_monitors;
71f64122c1SDave Airlie 	uint32_t crc;
729e3b3178SChristophe Fergeau 	int status = MONITORS_CONFIG_UNCHANGED;
73f64122c1SDave Airlie 
74f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
75f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
76f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
77f64122c1SDave Airlie 	if (crc != qdev->rom->client_monitors_config_crc) {
7872ec5650SFrediano Ziglio 		qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
79f64122c1SDave Airlie 			   sizeof(qdev->rom->client_monitors_config),
80f64122c1SDave Airlie 			   qdev->rom->client_monitors_config_crc);
819e3b3178SChristophe Fergeau 		return MONITORS_CONFIG_BAD_CRC;
82f64122c1SDave Airlie 	}
83c50fad8fSGerd Hoffmann 	if (!num_monitors) {
84c50fad8fSGerd Hoffmann 		DRM_DEBUG_KMS("no client monitors configured\n");
85c50fad8fSGerd Hoffmann 		return status;
86c50fad8fSGerd Hoffmann 	}
87f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
885b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
89f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
90f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
91f64122c1SDave Airlie 	} else {
92f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
93f64122c1SDave Airlie 	}
949e3b3178SChristophe Fergeau 	if (qdev->client_monitors_config
959e3b3178SChristophe Fergeau 	      && (num_monitors != qdev->client_monitors_config->count)) {
969e3b3178SChristophe Fergeau 		status = MONITORS_CONFIG_MODIFIED;
979e3b3178SChristophe Fergeau 	}
98f64122c1SDave Airlie 	qxl_alloc_client_monitors_config(qdev, num_monitors);
99f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
100f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
101f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
102f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
103f64122c1SDave Airlie 		struct qxl_urect *c_rect =
104f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
105f64122c1SDave Airlie 		struct qxl_head *client_head =
106f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
1079e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
10807f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1099e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1109e3b3178SChristophe Fergeau 		}
1119e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
11207f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1139e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1149e3b3178SChristophe Fergeau 		}
1159e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
11607f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1179e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1189e3b3178SChristophe Fergeau 		}
1199e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
12007f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1219e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1229e3b3178SChristophe Fergeau 		}
1239e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
12407f8d9bdSDave Airlie 			client_head->surface_id = 0;
1259e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1269e3b3178SChristophe Fergeau 		}
1279e3b3178SChristophe Fergeau 		if (client_head->id != i) {
12807f8d9bdSDave Airlie 			client_head->id = i;
1299e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1309e3b3178SChristophe Fergeau 		}
1319e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
13207f8d9bdSDave Airlie 			client_head->flags = 0;
1339e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1349e3b3178SChristophe Fergeau 		}
13507f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
13607f8d9bdSDave Airlie 			  client_head->x, client_head->y);
137f64122c1SDave Airlie 	}
1389e3b3178SChristophe Fergeau 
1399e3b3178SChristophe Fergeau 	return status;
140f64122c1SDave Airlie }
141f64122c1SDave Airlie 
1427dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1437dea0941SDave Airlie {
144cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1457dea0941SDave Airlie 	struct drm_connector *connector;
1467dea0941SDave Airlie 	struct qxl_output *output;
1477dea0941SDave Airlie 	struct qxl_head *head;
1487dea0941SDave Airlie 
1497dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1507dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1517dea0941SDave Airlie 
1527dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1537dea0941SDave Airlie 
1547dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1557dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1567dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1577dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1587dea0941SDave Airlie 	}
1597dea0941SDave Airlie }
1607dea0941SDave Airlie 
161f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
162f64122c1SDave Airlie {
163cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1649062155dSGerd Hoffmann 	int status, retries;
1659e3b3178SChristophe Fergeau 
1669062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1679e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1689062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1699062155dSGerd Hoffmann 			break;
1709062155dSGerd Hoffmann 		udelay(5);
1719062155dSGerd Hoffmann 	}
1729062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1739062155dSGerd Hoffmann 		qxl_io_log(qdev, "config: bad crc\n");
1749062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1759062155dSGerd Hoffmann 		return;
1769e3b3178SChristophe Fergeau 	}
1779e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1789062155dSGerd Hoffmann 		qxl_io_log(qdev, "config: unchanged\n");
1799062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1809e3b3178SChristophe Fergeau 		return;
181f64122c1SDave Airlie 	}
1824fdb0869SMarc-André Lureau 
1837dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1847dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1857dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
186cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1874fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1884fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
189cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1904fdb0869SMarc-André Lureau 	}
191f64122c1SDave Airlie }
192f64122c1SDave Airlie 
193b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector,
194b0807423SMarc-André Lureau                                          unsigned *pwidth,
195b0807423SMarc-André Lureau                                          unsigned *pheight)
196f64122c1SDave Airlie {
197f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
198f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
199f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
200f64122c1SDave Airlie 	int h = output->index;
201f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
202f64122c1SDave Airlie 	struct qxl_head *head;
203f64122c1SDave Airlie 
2042d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
2052d856f94SGerd Hoffmann 		return 0;
2062d856f94SGerd Hoffmann 	if (h >= qdev->monitors_config->max_allowed)
2072d856f94SGerd Hoffmann 		return 0;
20807f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
209f64122c1SDave Airlie 		return 0;
2102d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2112d856f94SGerd Hoffmann 		return 0;
2122d856f94SGerd Hoffmann 
21307f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2142d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
215f64122c1SDave Airlie 
216f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
217f64122c1SDave Airlie 			    false);
218f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
219ff996e72SChristophe Fergeau 	mode->hdisplay = head->width;
220ff996e72SChristophe Fergeau 	mode->vdisplay = head->height;
221ff996e72SChristophe Fergeau 	drm_mode_set_name(mode);
222b0807423SMarc-André Lureau 	*pwidth = head->width;
223b0807423SMarc-André Lureau 	*pheight = head->height;
224f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
225bd3e1c7cSJonathon Jongsma 	/* remember the last custom size for mode validation */
226bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_width = mode->hdisplay;
227bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_height = mode->vdisplay;
228f64122c1SDave Airlie 	return 1;
229f64122c1SDave Airlie }
230f64122c1SDave Airlie 
231bd3e1c7cSJonathon Jongsma static struct mode_size {
232f64122c1SDave Airlie 	int w;
233f64122c1SDave Airlie 	int h;
234f64122c1SDave Airlie } common_modes[] = {
235f64122c1SDave Airlie 	{ 640,  480},
236f64122c1SDave Airlie 	{ 720,  480},
237f64122c1SDave Airlie 	{ 800,  600},
238f64122c1SDave Airlie 	{ 848,  480},
239f64122c1SDave Airlie 	{1024,  768},
240f64122c1SDave Airlie 	{1152,  768},
241f64122c1SDave Airlie 	{1280,  720},
242f64122c1SDave Airlie 	{1280,  800},
243f64122c1SDave Airlie 	{1280,  854},
244f64122c1SDave Airlie 	{1280,  960},
245f64122c1SDave Airlie 	{1280, 1024},
246f64122c1SDave Airlie 	{1440,  900},
247f64122c1SDave Airlie 	{1400, 1050},
248f64122c1SDave Airlie 	{1680, 1050},
249f64122c1SDave Airlie 	{1600, 1200},
250f64122c1SDave Airlie 	{1920, 1080},
251f64122c1SDave Airlie 	{1920, 1200}
252f64122c1SDave Airlie };
253f64122c1SDave Airlie 
254bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector,
255bd3e1c7cSJonathon Jongsma                                 unsigned pwidth,
256bd3e1c7cSJonathon Jongsma                                 unsigned pheight)
257bd3e1c7cSJonathon Jongsma {
258bd3e1c7cSJonathon Jongsma 	struct drm_device *dev = connector->dev;
259bd3e1c7cSJonathon Jongsma 	struct drm_display_mode *mode = NULL;
260bd3e1c7cSJonathon Jongsma 	int i;
261f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
262f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
263f64122c1SDave Airlie 				    60, false, false, false);
264b0807423SMarc-André Lureau 		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
265f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
266f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
267f64122c1SDave Airlie 	}
268f64122c1SDave Airlie 	return i - 1;
269f64122c1SDave Airlie }
270f64122c1SDave Airlie 
271c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
272c2ff6632SGabriel Krisman Bertazi 				  struct drm_crtc_state *old_crtc_state)
273c2ff6632SGabriel Krisman Bertazi {
274c2ff6632SGabriel Krisman Bertazi 	struct drm_device *dev = crtc->dev;
275c2ff6632SGabriel Krisman Bertazi 	struct drm_pending_vblank_event *event;
276c2ff6632SGabriel Krisman Bertazi 	unsigned long flags;
277c2ff6632SGabriel Krisman Bertazi 
278c2ff6632SGabriel Krisman Bertazi 	if (crtc->state && crtc->state->event) {
279c2ff6632SGabriel Krisman Bertazi 		event = crtc->state->event;
280c2ff6632SGabriel Krisman Bertazi 		crtc->state->event = NULL;
281c2ff6632SGabriel Krisman Bertazi 
282c2ff6632SGabriel Krisman Bertazi 		spin_lock_irqsave(&dev->event_lock, flags);
283c2ff6632SGabriel Krisman Bertazi 		drm_crtc_send_vblank_event(crtc, event);
284c2ff6632SGabriel Krisman Bertazi 		spin_unlock_irqrestore(&dev->event_lock, flags);
285c2ff6632SGabriel Krisman Bertazi 	}
286c2ff6632SGabriel Krisman Bertazi }
287c2ff6632SGabriel Krisman Bertazi 
288f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
289f64122c1SDave Airlie {
290f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
291f64122c1SDave Airlie 
2929428088cSRay Strode 	qxl_bo_unref(&qxl_crtc->cursor_bo);
293f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
294f64122c1SDave Airlie 	kfree(qxl_crtc);
295f64122c1SDave Airlie }
296f64122c1SDave Airlie 
297f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
298bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
299f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
3009973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
3019ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
3029ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
3039ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
304f64122c1SDave Airlie };
305f64122c1SDave Airlie 
3066819c3c2SNoralf Trønnes void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
307f64122c1SDave Airlie {
308f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
30962676d10SGerd Hoffmann 	struct qxl_bo *bo = gem_to_qxl_bo(qxl_fb->obj);
310f64122c1SDave Airlie 
31162676d10SGerd Hoffmann 	WARN_ON(bo->shadow);
3122793c1d7SSantha Meena Ramamoorthy 	drm_gem_object_put_unlocked(qxl_fb->obj);
313f64122c1SDave Airlie 	drm_framebuffer_cleanup(fb);
314f64122c1SDave Airlie 	kfree(qxl_fb);
315f64122c1SDave Airlie }
316f64122c1SDave Airlie 
3176d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
318f64122c1SDave Airlie 					 struct drm_file *file_priv,
319f64122c1SDave Airlie 					 unsigned flags, unsigned color,
320f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
321f64122c1SDave Airlie 					 unsigned num_clips)
322f64122c1SDave Airlie {
323f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
324f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
325f64122c1SDave Airlie 	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
326f64122c1SDave Airlie 	struct drm_clip_rect norect;
327f64122c1SDave Airlie 	struct qxl_bo *qobj;
328f64122c1SDave Airlie 	int inc = 1;
329f64122c1SDave Airlie 
33073e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
33173e9efd4SVille Syrjälä 
332f64122c1SDave Airlie 	qobj = gem_to_qxl_bo(qxl_fb->obj);
333b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
33473e9efd4SVille Syrjälä 	if (!qobj->is_primary) {
33573e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
336b2b4465dSDave Airlie 		return 0;
33773e9efd4SVille Syrjälä 	}
338b2b4465dSDave Airlie 
339f64122c1SDave Airlie 	if (!num_clips) {
340f64122c1SDave Airlie 		num_clips = 1;
341f64122c1SDave Airlie 		clips = &norect;
342f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
343f64122c1SDave Airlie 		norect.x2 = fb->width;
344f64122c1SDave Airlie 		norect.y2 = fb->height;
345f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
346f64122c1SDave Airlie 		num_clips /= 2;
347f64122c1SDave Airlie 		inc = 2; /* skip source rects */
348f64122c1SDave Airlie 	}
349f64122c1SDave Airlie 
350f64122c1SDave Airlie 	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
351f64122c1SDave Airlie 			  clips, num_clips, inc);
35273e9efd4SVille Syrjälä 
35373e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
35473e9efd4SVille Syrjälä 
355f64122c1SDave Airlie 	return 0;
356f64122c1SDave Airlie }
357f64122c1SDave Airlie 
358f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
359f64122c1SDave Airlie 	.destroy = qxl_user_framebuffer_destroy,
360f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
361f64122c1SDave Airlie /*	TODO?
362f64122c1SDave Airlie  *	.create_handle = qxl_user_framebuffer_create_handle, */
363f64122c1SDave Airlie };
364f64122c1SDave Airlie 
365f64122c1SDave Airlie int
366f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev,
367f64122c1SDave Airlie 		     struct qxl_framebuffer *qfb,
3681eb83451SVille Syrjälä 		     const struct drm_mode_fb_cmd2 *mode_cmd,
3696819c3c2SNoralf Trønnes 		     struct drm_gem_object *obj,
3706819c3c2SNoralf Trønnes 		     const struct drm_framebuffer_funcs *funcs)
371f64122c1SDave Airlie {
372f64122c1SDave Airlie 	int ret;
373f64122c1SDave Airlie 
374f64122c1SDave Airlie 	qfb->obj = obj;
3755360943fSVille Syrjälä 	drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
3766819c3c2SNoralf Trønnes 	ret = drm_framebuffer_init(dev, &qfb->base, funcs);
377f64122c1SDave Airlie 	if (ret) {
378f64122c1SDave Airlie 		qfb->obj = NULL;
379f64122c1SDave Airlie 		return ret;
380f64122c1SDave Airlie 	}
381f64122c1SDave Airlie 	return 0;
382f64122c1SDave Airlie }
383f64122c1SDave Airlie 
384f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
385f64122c1SDave Airlie 				  const struct drm_display_mode *mode,
386f64122c1SDave Airlie 				  struct drm_display_mode *adjusted_mode)
387f64122c1SDave Airlie {
388f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
389f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
390f64122c1SDave Airlie 
391f64122c1SDave Airlie 	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
392f64122c1SDave Airlie 		   __func__,
393f64122c1SDave Airlie 		   mode->hdisplay, mode->vdisplay,
394f64122c1SDave Airlie 		   adjusted_mode->hdisplay,
395f64122c1SDave Airlie 		   adjusted_mode->vdisplay);
396f64122c1SDave Airlie 	return true;
397f64122c1SDave Airlie }
398f64122c1SDave Airlie 
399e4a76442SChristophe Fergeau static void
400f64122c1SDave Airlie qxl_send_monitors_config(struct qxl_device *qdev)
401f64122c1SDave Airlie {
402f64122c1SDave Airlie 	int i;
403f64122c1SDave Airlie 
404f64122c1SDave Airlie 	BUG_ON(!qdev->ram_header->monitors_config);
405f64122c1SDave Airlie 
406f64122c1SDave Airlie 	if (qdev->monitors_config->count == 0) {
407f64122c1SDave Airlie 		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
408f64122c1SDave Airlie 		return;
409f64122c1SDave Airlie 	}
410f64122c1SDave Airlie 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
411f64122c1SDave Airlie 		struct qxl_head *head = &qdev->monitors_config->heads[i];
412f64122c1SDave Airlie 
41307f8d9bdSDave Airlie 		if (head->y > 8192 || head->x > 8192 ||
414f64122c1SDave Airlie 		    head->width > 8192 || head->height > 8192) {
415f64122c1SDave Airlie 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
416f64122c1SDave Airlie 				  i, head->width, head->height,
417f64122c1SDave Airlie 				  head->x, head->y);
418f64122c1SDave Airlie 			return;
419f64122c1SDave Airlie 		}
420f64122c1SDave Airlie 	}
421f64122c1SDave Airlie 	qxl_io_monitors_config(qdev);
422f64122c1SDave Airlie }
423f64122c1SDave Airlie 
42407f8d9bdSDave Airlie static void qxl_monitors_config_set(struct qxl_device *qdev,
42507f8d9bdSDave Airlie 				    int index,
426f64122c1SDave Airlie 				    unsigned x, unsigned y,
42707f8d9bdSDave Airlie 				    unsigned width, unsigned height,
42807f8d9bdSDave Airlie 				    unsigned surf_id)
429f64122c1SDave Airlie {
43007f8d9bdSDave Airlie 	DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
43107f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].x = x;
43207f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].y = y;
43307f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].width = width;
43407f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].height = height;
43507f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].surface_id = surf_id;
43607f8d9bdSDave Airlie 
437f64122c1SDave Airlie }
438f64122c1SDave Airlie 
43945dfe577SGerd Hoffmann static void qxl_mode_set_nofb(struct drm_crtc *crtc)
4403538e80aSGabriel Krisman Bertazi {
4413538e80aSGabriel Krisman Bertazi 	struct qxl_device *qdev = crtc->dev->dev_private;
4423538e80aSGabriel Krisman Bertazi 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
4433538e80aSGabriel Krisman Bertazi 	struct drm_display_mode *mode = &crtc->mode;
4443538e80aSGabriel Krisman Bertazi 
4453538e80aSGabriel Krisman Bertazi 	DRM_DEBUG("Mode set (%d,%d)\n",
4463538e80aSGabriel Krisman Bertazi 		  mode->hdisplay, mode->vdisplay);
4473538e80aSGabriel Krisman Bertazi 
4483538e80aSGabriel Krisman Bertazi 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
4493538e80aSGabriel Krisman Bertazi 				mode->hdisplay,	mode->vdisplay, 0);
4503538e80aSGabriel Krisman Bertazi 
4513538e80aSGabriel Krisman Bertazi }
4523538e80aSGabriel Krisman Bertazi 
4530b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
4540b20a0f8SLaurent Pinchart 				   struct drm_crtc_state *old_state)
455f64122c1SDave Airlie {
456f64122c1SDave Airlie 	DRM_DEBUG("\n");
457f64122c1SDave Airlie }
458f64122c1SDave Airlie 
45964581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
46064581714SLaurent Pinchart 				    struct drm_crtc_state *old_state)
46107f8d9bdSDave Airlie {
46207f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
46337235451SGabriel Krisman Bertazi 	struct qxl_device *qdev = crtc->dev->dev_private;
46407f8d9bdSDave Airlie 
46507f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
46607f8d9bdSDave Airlie 
46707f8d9bdSDave Airlie 	qxl_send_monitors_config(qdev);
46807f8d9bdSDave Airlie }
46907f8d9bdSDave Airlie 
470f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
471f64122c1SDave Airlie 	.mode_fixup = qxl_crtc_mode_fixup,
4723538e80aSGabriel Krisman Bertazi 	.mode_set_nofb = qxl_mode_set_nofb,
473c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4740b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
47564581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
476f64122c1SDave Airlie };
477f64122c1SDave Airlie 
47845dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
479c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
480c2ff6632SGabriel Krisman Bertazi {
481c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
482c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb;
483c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
484c2ff6632SGabriel Krisman Bertazi 
485c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
486c2ff6632SGabriel Krisman Bertazi 		return 0;
487c2ff6632SGabriel Krisman Bertazi 
488c2ff6632SGabriel Krisman Bertazi 	qfb = to_qxl_framebuffer(state->fb);
489c2ff6632SGabriel Krisman Bertazi 	bo = gem_to_qxl_bo(qfb->obj);
490c2ff6632SGabriel Krisman Bertazi 
491c2ff6632SGabriel Krisman Bertazi 	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
492c2ff6632SGabriel Krisman Bertazi 		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
493c2ff6632SGabriel Krisman Bertazi 		return -EINVAL;
494c2ff6632SGabriel Krisman Bertazi 	}
495c2ff6632SGabriel Krisman Bertazi 
496c2ff6632SGabriel Krisman Bertazi 	return 0;
497c2ff6632SGabriel Krisman Bertazi }
498c2ff6632SGabriel Krisman Bertazi 
4999428088cSRay Strode static int qxl_primary_apply_cursor(struct drm_plane *plane)
5009428088cSRay Strode {
5019428088cSRay Strode 	struct drm_device *dev = plane->dev;
5029428088cSRay Strode 	struct qxl_device *qdev = dev->dev_private;
5039428088cSRay Strode 	struct drm_framebuffer *fb = plane->state->fb;
5049428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
5059428088cSRay Strode 	struct qxl_cursor_cmd *cmd;
5069428088cSRay Strode 	struct qxl_release *release;
5079428088cSRay Strode 	int ret = 0;
5089428088cSRay Strode 
5099428088cSRay Strode 	if (!qcrtc->cursor_bo)
5109428088cSRay Strode 		return 0;
5119428088cSRay Strode 
5129428088cSRay Strode 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5139428088cSRay Strode 					 QXL_RELEASE_CURSOR_CMD,
5149428088cSRay Strode 					 &release, NULL);
5159428088cSRay Strode 	if (ret)
5169428088cSRay Strode 		return ret;
5179428088cSRay Strode 
5189428088cSRay Strode 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
5199428088cSRay Strode 	if (ret)
5209428088cSRay Strode 		goto out_free_release;
5219428088cSRay Strode 
5229428088cSRay Strode 	ret = qxl_release_reserve_list(release, false);
5239428088cSRay Strode 	if (ret)
5249428088cSRay Strode 		goto out_free_release;
5259428088cSRay Strode 
5269428088cSRay Strode 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5279428088cSRay Strode 	cmd->type = QXL_CURSOR_SET;
5289428088cSRay Strode 	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
5299428088cSRay Strode 	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
5309428088cSRay Strode 
5319428088cSRay Strode 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5329428088cSRay Strode 
5339428088cSRay Strode 	cmd->u.set.visible = 1;
5349428088cSRay Strode 	qxl_release_unmap(qdev, release, &cmd->release_info);
5359428088cSRay Strode 
5369428088cSRay Strode 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5379428088cSRay Strode 	qxl_release_fence_buffer_objects(release);
5389428088cSRay Strode 
5399428088cSRay Strode 	return ret;
5409428088cSRay Strode 
5419428088cSRay Strode out_free_release:
5429428088cSRay Strode 	qxl_release_free(qdev, release);
5439428088cSRay Strode 	return ret;
5449428088cSRay Strode }
5459428088cSRay Strode 
546c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
547c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
548c2ff6632SGabriel Krisman Bertazi {
549c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
550c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb =
551c2ff6632SGabriel Krisman Bertazi 		to_qxl_framebuffer(plane->state->fb);
552c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb_old;
553c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
554c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo_old;
555c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
556c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
557c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
558c2ff6632SGabriel Krisman Bertazi 	    .x2 = qfb->base.width,
559c2ff6632SGabriel Krisman Bertazi 	    .y2 = qfb->base.height
560c2ff6632SGabriel Krisman Bertazi 	};
5619428088cSRay Strode 	int ret;
56262676d10SGerd Hoffmann 	bool same_shadow = false;
563c2ff6632SGabriel Krisman Bertazi 
564b0e07da3SGerd Hoffmann 	if (old_state->fb) {
565c2ff6632SGabriel Krisman Bertazi 		qfb_old = to_qxl_framebuffer(old_state->fb);
566c2ff6632SGabriel Krisman Bertazi 		bo_old = gem_to_qxl_bo(qfb_old->obj);
567b0e07da3SGerd Hoffmann 	} else {
568b0e07da3SGerd Hoffmann 		bo_old = NULL;
569b0e07da3SGerd Hoffmann 	}
570b0e07da3SGerd Hoffmann 
571b0e07da3SGerd Hoffmann 	if (bo == bo_old)
572b0e07da3SGerd Hoffmann 		return;
573b0e07da3SGerd Hoffmann 
57462676d10SGerd Hoffmann 	if (bo_old && bo_old->shadow && bo->shadow &&
57562676d10SGerd Hoffmann 	    bo_old->shadow == bo->shadow) {
57662676d10SGerd Hoffmann 		same_shadow = true;
57762676d10SGerd Hoffmann 	}
57862676d10SGerd Hoffmann 
579b0e07da3SGerd Hoffmann 	if (bo_old && bo_old->is_primary) {
58062676d10SGerd Hoffmann 		if (!same_shadow)
581b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
582c2ff6632SGabriel Krisman Bertazi 		bo_old->is_primary = false;
5839428088cSRay Strode 
5849428088cSRay Strode 		ret = qxl_primary_apply_cursor(plane);
5859428088cSRay Strode 		if (ret)
5869428088cSRay Strode 			DRM_ERROR(
5879428088cSRay Strode 			"could not set cursor after creating primary");
588c2ff6632SGabriel Krisman Bertazi 	}
589c2ff6632SGabriel Krisman Bertazi 
590b0e07da3SGerd Hoffmann 	if (!bo->is_primary) {
59162676d10SGerd Hoffmann 		if (!same_shadow)
592b0e07da3SGerd Hoffmann 			qxl_io_create_primary(qdev, 0, bo);
593c2ff6632SGabriel Krisman Bertazi 		bo->is_primary = true;
594b0e07da3SGerd Hoffmann 	}
59562676d10SGerd Hoffmann 
596c2ff6632SGabriel Krisman Bertazi 	qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
597c2ff6632SGabriel Krisman Bertazi }
598c2ff6632SGabriel Krisman Bertazi 
599c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
600c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
601c2ff6632SGabriel Krisman Bertazi {
602c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
603c2ff6632SGabriel Krisman Bertazi 
604b0e07da3SGerd Hoffmann 	if (old_state->fb) {
605b0e07da3SGerd Hoffmann 		struct qxl_framebuffer *qfb =
606c2ff6632SGabriel Krisman Bertazi 			to_qxl_framebuffer(old_state->fb);
607c2ff6632SGabriel Krisman Bertazi 		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
608c2ff6632SGabriel Krisman Bertazi 
609b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
610c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
611c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
612c2ff6632SGabriel Krisman Bertazi 		}
613c2ff6632SGabriel Krisman Bertazi 	}
614b0e07da3SGerd Hoffmann }
615c2ff6632SGabriel Krisman Bertazi 
61645dfe577SGerd Hoffmann static int qxl_plane_atomic_check(struct drm_plane *plane,
6171277eed5SGabriel Krisman Bertazi 				  struct drm_plane_state *state)
6181277eed5SGabriel Krisman Bertazi {
6191277eed5SGabriel Krisman Bertazi 	return 0;
6201277eed5SGabriel Krisman Bertazi }
6211277eed5SGabriel Krisman Bertazi 
6221277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
6231277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
6241277eed5SGabriel Krisman Bertazi {
6251277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
6261277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
6271277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
6289428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
6291277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6301277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6311277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
6321277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
633*889ad63dSJeremy Cline 	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
6341277eed5SGabriel Krisman Bertazi 	int ret;
6351277eed5SGabriel Krisman Bertazi 	void *user_ptr;
6361277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
6371277eed5SGabriel Krisman Bertazi 
6381277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6391277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
6401277eed5SGabriel Krisman Bertazi 					 &release, NULL);
641ee5cb7c4SDan Carpenter 	if (ret)
642ee5cb7c4SDan Carpenter 		return;
6431277eed5SGabriel Krisman Bertazi 
6441277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
6451277eed5SGabriel Krisman Bertazi 		obj = to_qxl_framebuffer(fb)->obj;
6461277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
6471277eed5SGabriel Krisman Bertazi 
6481277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
6491277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
6501277eed5SGabriel Krisman Bertazi 		if (ret)
6511277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6521277eed5SGabriel Krisman Bertazi 
6531277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
6541277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6551277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6561277eed5SGabriel Krisman Bertazi 		if (ret)
6571277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6581277eed5SGabriel Krisman Bertazi 
6591277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6601277eed5SGabriel Krisman Bertazi 		if (ret)
6611277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6621277eed5SGabriel Krisman Bertazi 
6631277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6641277eed5SGabriel Krisman Bertazi 		if (ret)
6651277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6661277eed5SGabriel Krisman Bertazi 
6671277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6681277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6691277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6701277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6711277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6721277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6731277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6741277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6751277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6761277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6771277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6781277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6791277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6801277eed5SGabriel Krisman Bertazi 
681429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6821277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6831277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6841277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6851277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6869428088cSRay Strode 
687*889ad63dSJeremy Cline 		old_cursor_bo = qcrtc->cursor_bo;
6889428088cSRay Strode 		qcrtc->cursor_bo = cursor_bo;
6899428088cSRay Strode 		cursor_bo = NULL;
6901277eed5SGabriel Krisman Bertazi 	} else {
6911277eed5SGabriel Krisman Bertazi 
6921277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6931277eed5SGabriel Krisman Bertazi 		if (ret)
6941277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6951277eed5SGabriel Krisman Bertazi 
696429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6971277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
6981277eed5SGabriel Krisman Bertazi 	}
6991277eed5SGabriel Krisman Bertazi 
7001277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
7011277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
7021277eed5SGabriel Krisman Bertazi 
7031277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7041277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7051277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7061277eed5SGabriel Krisman Bertazi 
707*889ad63dSJeremy Cline 	if (old_cursor_bo)
708*889ad63dSJeremy Cline 		qxl_bo_unref(&old_cursor_bo);
709*889ad63dSJeremy Cline 
71016c6db36SRay Strode 	qxl_bo_unref(&cursor_bo);
71116c6db36SRay Strode 
7121277eed5SGabriel Krisman Bertazi 	return;
7131277eed5SGabriel Krisman Bertazi 
7141277eed5SGabriel Krisman Bertazi out_backoff:
7151277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
7161277eed5SGabriel Krisman Bertazi out_free_bo:
7171277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
7181277eed5SGabriel Krisman Bertazi out_kunmap:
7191277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
7201277eed5SGabriel Krisman Bertazi out_free_release:
7211277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
7221277eed5SGabriel Krisman Bertazi 	return;
7231277eed5SGabriel Krisman Bertazi 
7241277eed5SGabriel Krisman Bertazi }
7251277eed5SGabriel Krisman Bertazi 
72645dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
7271277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
7281277eed5SGabriel Krisman Bertazi {
7291277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
7301277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
7311277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
7321277eed5SGabriel Krisman Bertazi 	int ret;
7331277eed5SGabriel Krisman Bertazi 
7341277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
7351277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
7361277eed5SGabriel Krisman Bertazi 					 &release, NULL);
7371277eed5SGabriel Krisman Bertazi 	if (ret)
7381277eed5SGabriel Krisman Bertazi 		return;
7391277eed5SGabriel Krisman Bertazi 
7401277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
7411277eed5SGabriel Krisman Bertazi 	if (ret) {
7421277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
7431277eed5SGabriel Krisman Bertazi 		return;
7441277eed5SGabriel Krisman Bertazi 	}
7451277eed5SGabriel Krisman Bertazi 
7461277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7471277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
7481277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7491277eed5SGabriel Krisman Bertazi 
7501277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7511277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7521277eed5SGabriel Krisman Bertazi }
7531277eed5SGabriel Krisman Bertazi 
75445dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
7551277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
7561277eed5SGabriel Krisman Bertazi {
75762676d10SGerd Hoffmann 	struct qxl_device *qdev = plane->dev->dev_private;
7581277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
75962676d10SGerd Hoffmann 	struct qxl_bo *user_bo, *old_bo = NULL;
7601277eed5SGabriel Krisman Bertazi 	int ret;
7611277eed5SGabriel Krisman Bertazi 
7621277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
7631277eed5SGabriel Krisman Bertazi 		return 0;
7641277eed5SGabriel Krisman Bertazi 
7651277eed5SGabriel Krisman Bertazi 	obj = to_qxl_framebuffer(new_state->fb)->obj;
7661277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7671277eed5SGabriel Krisman Bertazi 
76862676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
76962676d10SGerd Hoffmann 	    user_bo->is_dumb && !user_bo->shadow) {
77062676d10SGerd Hoffmann 		if (plane->state->fb) {
77162676d10SGerd Hoffmann 			obj = to_qxl_framebuffer(plane->state->fb)->obj;
77262676d10SGerd Hoffmann 			old_bo = gem_to_qxl_bo(obj);
77362676d10SGerd Hoffmann 		}
77462676d10SGerd Hoffmann 		if (old_bo && old_bo->shadow &&
77562676d10SGerd Hoffmann 		    user_bo->gem_base.size == old_bo->gem_base.size &&
77662676d10SGerd Hoffmann 		    plane->state->crtc     == new_state->crtc &&
77762676d10SGerd Hoffmann 		    plane->state->crtc_w   == new_state->crtc_w &&
77862676d10SGerd Hoffmann 		    plane->state->crtc_h   == new_state->crtc_h &&
77962676d10SGerd Hoffmann 		    plane->state->src_x    == new_state->src_x &&
78062676d10SGerd Hoffmann 		    plane->state->src_y    == new_state->src_y &&
78162676d10SGerd Hoffmann 		    plane->state->src_w    == new_state->src_w &&
78262676d10SGerd Hoffmann 		    plane->state->src_h    == new_state->src_h &&
78362676d10SGerd Hoffmann 		    plane->state->rotation == new_state->rotation &&
78462676d10SGerd Hoffmann 		    plane->state->zpos     == new_state->zpos) {
78562676d10SGerd Hoffmann 			drm_gem_object_get(&old_bo->shadow->gem_base);
78662676d10SGerd Hoffmann 			user_bo->shadow = old_bo->shadow;
78762676d10SGerd Hoffmann 		} else {
78862676d10SGerd Hoffmann 			qxl_bo_create(qdev, user_bo->gem_base.size,
78962676d10SGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_VRAM, NULL,
79062676d10SGerd Hoffmann 				      &user_bo->shadow);
79162676d10SGerd Hoffmann 		}
79262676d10SGerd Hoffmann 	}
79362676d10SGerd Hoffmann 
7941277eed5SGabriel Krisman Bertazi 	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
7951277eed5SGabriel Krisman Bertazi 	if (ret)
7961277eed5SGabriel Krisman Bertazi 		return ret;
7971277eed5SGabriel Krisman Bertazi 
7981277eed5SGabriel Krisman Bertazi 	return 0;
7991277eed5SGabriel Krisman Bertazi }
8001277eed5SGabriel Krisman Bertazi 
8011277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8021277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
8031277eed5SGabriel Krisman Bertazi {
8041277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
8051277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
8061277eed5SGabriel Krisman Bertazi 
8075f3d862aSGerd Hoffmann 	if (!old_state->fb) {
8085f3d862aSGerd Hoffmann 		/*
8095f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
8101277eed5SGabriel Krisman Bertazi 		 * unpin.
8111277eed5SGabriel Krisman Bertazi 		 */
8121277eed5SGabriel Krisman Bertazi 		return;
8131277eed5SGabriel Krisman Bertazi 	}
8141277eed5SGabriel Krisman Bertazi 
8155f3d862aSGerd Hoffmann 	obj = to_qxl_framebuffer(old_state->fb)->obj;
8161277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8171277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
81862676d10SGerd Hoffmann 
81962676d10SGerd Hoffmann 	if (user_bo->shadow && !user_bo->is_primary) {
82062676d10SGerd Hoffmann 		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
82162676d10SGerd Hoffmann 		user_bo->shadow = NULL;
82262676d10SGerd Hoffmann 	}
8231277eed5SGabriel Krisman Bertazi }
8241277eed5SGabriel Krisman Bertazi 
8251277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8261277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
8271277eed5SGabriel Krisman Bertazi };
8281277eed5SGabriel Krisman Bertazi 
8291277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8301277eed5SGabriel Krisman Bertazi 	.atomic_check = qxl_plane_atomic_check,
8311277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
8321277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
8331277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
8341277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
8351277eed5SGabriel Krisman Bertazi };
8361277eed5SGabriel Krisman Bertazi 
8371277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
838472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
839472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
8401277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8419ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8429ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8439ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
8441277eed5SGabriel Krisman Bertazi };
8451277eed5SGabriel Krisman Bertazi 
846d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
847d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
848d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
849d3e7e42dSGabriel Krisman Bertazi };
850d3e7e42dSGabriel Krisman Bertazi 
851c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
852c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
853c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
854c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
855c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
856c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
857c2ff6632SGabriel Krisman Bertazi };
858c2ff6632SGabriel Krisman Bertazi 
859d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
860472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
861472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
862d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8639ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8649ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8659ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
866d3e7e42dSGabriel Krisman Bertazi };
867d3e7e42dSGabriel Krisman Bertazi 
868d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
869d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
870d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
871d3e7e42dSGabriel Krisman Bertazi {
872d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
873d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
874d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
875d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
876d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
877d3e7e42dSGabriel Krisman Bertazi 	int err;
878d3e7e42dSGabriel Krisman Bertazi 
879d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
880d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
881d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
882d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
883c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
8841277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
8851277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
8861277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
8871277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
8881277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
889d3e7e42dSGabriel Krisman Bertazi 	} else {
890d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
891d3e7e42dSGabriel Krisman Bertazi 	}
892d3e7e42dSGabriel Krisman Bertazi 
893d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
894d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
895d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
896d3e7e42dSGabriel Krisman Bertazi 
897d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
898d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
899e6fc3b68SBen Widawsky 				       NULL, type, NULL);
900d3e7e42dSGabriel Krisman Bertazi 	if (err)
901d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
902d3e7e42dSGabriel Krisman Bertazi 
903d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
904d3e7e42dSGabriel Krisman Bertazi 
905d3e7e42dSGabriel Krisman Bertazi 	return plane;
906d3e7e42dSGabriel Krisman Bertazi 
907d3e7e42dSGabriel Krisman Bertazi free_plane:
908d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
909d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
910d3e7e42dSGabriel Krisman Bertazi }
911d3e7e42dSGabriel Krisman Bertazi 
91207f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
913f64122c1SDave Airlie {
914f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
9151277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
916d3e7e42dSGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
917d3e7e42dSGabriel Krisman Bertazi 	int r;
918f64122c1SDave Airlie 
919f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
920f64122c1SDave Airlie 	if (!qxl_crtc)
921f64122c1SDave Airlie 		return -ENOMEM;
922f64122c1SDave Airlie 
923d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
924d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
925d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
926d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
927d3e7e42dSGabriel Krisman Bertazi 	}
928d3e7e42dSGabriel Krisman Bertazi 
9291277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9301277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
9311277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
9321277eed5SGabriel Krisman Bertazi 		goto clean_primary;
9331277eed5SGabriel Krisman Bertazi 	}
9341277eed5SGabriel Krisman Bertazi 
9351277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
936d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
937d3e7e42dSGabriel Krisman Bertazi 	if (r)
9381277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
939d3e7e42dSGabriel Krisman Bertazi 
94007f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
941f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
942f64122c1SDave Airlie 	return 0;
943d3e7e42dSGabriel Krisman Bertazi 
9441277eed5SGabriel Krisman Bertazi clean_cursor:
9451277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
9461277eed5SGabriel Krisman Bertazi 	kfree(cursor);
947d3e7e42dSGabriel Krisman Bertazi clean_primary:
948d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
949d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
950d3e7e42dSGabriel Krisman Bertazi free_mem:
951d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
952d3e7e42dSGabriel Krisman Bertazi 	return r;
953f64122c1SDave Airlie }
954f64122c1SDave Airlie 
955f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
956f64122c1SDave Airlie {
957f64122c1SDave Airlie 	DRM_DEBUG("\n");
958f64122c1SDave Airlie }
959f64122c1SDave Airlie 
960f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder)
961f64122c1SDave Airlie {
962f64122c1SDave Airlie 	DRM_DEBUG("\n");
963f64122c1SDave Airlie }
964f64122c1SDave Airlie 
965f64122c1SDave Airlie static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
966f64122c1SDave Airlie 		struct drm_encoder *encoder)
967f64122c1SDave Airlie {
968f64122c1SDave Airlie 	int i;
96907f8d9bdSDave Airlie 	struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
970f64122c1SDave Airlie 	struct qxl_head *head;
971f64122c1SDave Airlie 	struct drm_display_mode *mode;
972f64122c1SDave Airlie 
973f64122c1SDave Airlie 	BUG_ON(!encoder);
974f64122c1SDave Airlie 	/* TODO: ugly, do better */
97507f8d9bdSDave Airlie 	i = output->index;
976f64122c1SDave Airlie 	if (!qdev->monitors_config ||
977f64122c1SDave Airlie 	    qdev->monitors_config->max_allowed <= i) {
978f64122c1SDave Airlie 		DRM_ERROR(
979f64122c1SDave Airlie 		"head number too large or missing monitors config: %p, %d",
980f64122c1SDave Airlie 		qdev->monitors_config,
981f64122c1SDave Airlie 		qdev->monitors_config ?
982f64122c1SDave Airlie 			qdev->monitors_config->max_allowed : -1);
983f64122c1SDave Airlie 		return;
984f64122c1SDave Airlie 	}
985f64122c1SDave Airlie 	if (!encoder->crtc) {
986f64122c1SDave Airlie 		DRM_ERROR("missing crtc on encoder %p\n", encoder);
987f64122c1SDave Airlie 		return;
988f64122c1SDave Airlie 	}
989f64122c1SDave Airlie 	if (i != 0)
990f64122c1SDave Airlie 		DRM_DEBUG("missing for multiple monitors: no head holes\n");
991f64122c1SDave Airlie 	head = &qdev->monitors_config->heads[i];
992f64122c1SDave Airlie 	head->id = i;
993f64122c1SDave Airlie 	if (encoder->crtc->enabled) {
994f64122c1SDave Airlie 		mode = &encoder->crtc->mode;
995f64122c1SDave Airlie 		head->width = mode->hdisplay;
996f64122c1SDave Airlie 		head->height = mode->vdisplay;
997f64122c1SDave Airlie 		head->x = encoder->crtc->x;
998f64122c1SDave Airlie 		head->y = encoder->crtc->y;
999f64122c1SDave Airlie 		if (qdev->monitors_config->count < i + 1)
1000f64122c1SDave Airlie 			qdev->monitors_config->count = i + 1;
1001f64122c1SDave Airlie 	} else {
1002f64122c1SDave Airlie 		head->width = 0;
1003f64122c1SDave Airlie 		head->height = 0;
1004f64122c1SDave Airlie 		head->x = 0;
1005f64122c1SDave Airlie 		head->y = 0;
1006f64122c1SDave Airlie 	}
100707f8d9bdSDave Airlie 	DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
100807f8d9bdSDave Airlie 		      i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
1009f64122c1SDave Airlie 	head->flags = 0;
1010f64122c1SDave Airlie 	/* TODO - somewhere else to call this for multiple monitors
1011f64122c1SDave Airlie 	 * (config_commit?) */
1012f64122c1SDave Airlie 	qxl_send_monitors_config(qdev);
1013f64122c1SDave Airlie }
1014f64122c1SDave Airlie 
1015f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder)
1016f64122c1SDave Airlie {
1017f64122c1SDave Airlie 	struct qxl_device *qdev = encoder->dev->dev_private;
1018f64122c1SDave Airlie 
1019f64122c1SDave Airlie 	qxl_write_monitors_config_for_encoder(qdev, encoder);
1020f64122c1SDave Airlie 	DRM_DEBUG("\n");
1021f64122c1SDave Airlie }
1022f64122c1SDave Airlie 
1023f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder,
1024f64122c1SDave Airlie 				struct drm_display_mode *mode,
1025f64122c1SDave Airlie 				struct drm_display_mode *adjusted_mode)
1026f64122c1SDave Airlie {
1027f64122c1SDave Airlie 	DRM_DEBUG("\n");
1028f64122c1SDave Airlie }
1029f64122c1SDave Airlie 
1030f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
1031f64122c1SDave Airlie {
1032b0807423SMarc-André Lureau 	unsigned pwidth = 1024;
1033b0807423SMarc-André Lureau 	unsigned pheight = 768;
10342d856f94SGerd Hoffmann 	int ret = 0;
1035f64122c1SDave Airlie 
1036b0807423SMarc-André Lureau 	ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
1037f64122c1SDave Airlie 	if (ret < 0)
1038f64122c1SDave Airlie 		return ret;
1039b0807423SMarc-André Lureau 	ret += qxl_add_common_modes(connector, pwidth, pheight);
1040f64122c1SDave Airlie 	return ret;
1041f64122c1SDave Airlie }
1042f64122c1SDave Airlie 
1043f64122c1SDave Airlie static int qxl_conn_mode_valid(struct drm_connector *connector,
1044f64122c1SDave Airlie 			       struct drm_display_mode *mode)
1045f64122c1SDave Airlie {
1046bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
1047bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
1048bd3e1c7cSJonathon Jongsma 	int i;
1049bd3e1c7cSJonathon Jongsma 
1050f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
1051f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
1052bd3e1c7cSJonathon Jongsma 
1053bd3e1c7cSJonathon Jongsma 	if(qdev->monitors_config_width == mode->hdisplay &&
1054bd3e1c7cSJonathon Jongsma 	   qdev->monitors_config_height == mode->vdisplay)
1055f64122c1SDave Airlie 		return MODE_OK;
1056bd3e1c7cSJonathon Jongsma 
1057bd3e1c7cSJonathon Jongsma 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
1058bd3e1c7cSJonathon Jongsma 		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
1059bd3e1c7cSJonathon Jongsma 			return MODE_OK;
1060bd3e1c7cSJonathon Jongsma 	}
1061bd3e1c7cSJonathon Jongsma 	return MODE_BAD;
1062f64122c1SDave Airlie }
1063f64122c1SDave Airlie 
10646d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1065f64122c1SDave Airlie {
1066f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1067f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1068f64122c1SDave Airlie 
1069f64122c1SDave Airlie 	DRM_DEBUG("\n");
1070f64122c1SDave Airlie 	return &qxl_output->enc;
1071f64122c1SDave Airlie }
1072f64122c1SDave Airlie 
1073f64122c1SDave Airlie 
1074f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1075f64122c1SDave Airlie 	.dpms = qxl_enc_dpms,
1076f64122c1SDave Airlie 	.prepare = qxl_enc_prepare,
1077f64122c1SDave Airlie 	.mode_set = qxl_enc_mode_set,
1078f64122c1SDave Airlie 	.commit = qxl_enc_commit,
1079f64122c1SDave Airlie };
1080f64122c1SDave Airlie 
1081f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1082f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1083f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1084f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1085f64122c1SDave Airlie };
1086f64122c1SDave Airlie 
1087f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1088f64122c1SDave Airlie 			struct drm_connector *connector,
1089f64122c1SDave Airlie 			bool force)
1090f64122c1SDave Airlie {
1091f64122c1SDave Airlie 	struct qxl_output *output =
1092f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1093f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1094f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
109569e5d3f8SDave Airlie 	bool connected = false;
1096f64122c1SDave Airlie 
1097f64122c1SDave Airlie 	/* The first monitor is always connected */
109869e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
109969e5d3f8SDave Airlie 		if (output->index == 0)
110069e5d3f8SDave Airlie 			connected = true;
110169e5d3f8SDave Airlie 	} else
110269e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
110369e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1104f64122c1SDave Airlie 
11055cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
11065cab51cbSMarc-André Lureau 	if (!connected)
11075cab51cbSMarc-André Lureau 		qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
11085cab51cbSMarc-André Lureau 
1109f64122c1SDave Airlie 	return connected ? connector_status_connected
1110f64122c1SDave Airlie 			 : connector_status_disconnected;
1111f64122c1SDave Airlie }
1112f64122c1SDave Airlie 
1113f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector,
1114f64122c1SDave Airlie 				   struct drm_property *property,
1115f64122c1SDave Airlie 				   uint64_t value)
1116f64122c1SDave Airlie {
1117f64122c1SDave Airlie 	DRM_DEBUG("\n");
1118f64122c1SDave Airlie 	return 0;
1119f64122c1SDave Airlie }
1120f64122c1SDave Airlie 
1121f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1122f64122c1SDave Airlie {
1123f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1124f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1125f64122c1SDave Airlie 
112634ea3d38SThomas Wood 	drm_connector_unregister(connector);
1127f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1128f64122c1SDave Airlie 	kfree(qxl_output);
1129f64122c1SDave Airlie }
1130f64122c1SDave Airlie 
1131f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1132f64122c1SDave Airlie 	.dpms = drm_helper_connector_dpms,
1133f64122c1SDave Airlie 	.detect = qxl_conn_detect,
11346af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1135f64122c1SDave Airlie 	.set_property = qxl_conn_set_property,
1136f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
11379ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
11389ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
11399ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1140f64122c1SDave Airlie };
1141f64122c1SDave Airlie 
1142f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
1143f64122c1SDave Airlie {
1144f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
1145f64122c1SDave Airlie }
1146f64122c1SDave Airlie 
1147f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
1148f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
1149f64122c1SDave Airlie };
1150f64122c1SDave Airlie 
11514695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
11524695b039SDave Airlie {
11534695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
11544695b039SDave Airlie 		return 0;
11554695b039SDave Airlie 
11564695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1157cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
11584695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
11594695b039SDave Airlie 
11604695b039SDave Airlie 	return 0;
11614695b039SDave Airlie }
11624695b039SDave Airlie 
11636d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1164f64122c1SDave Airlie {
11654695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
1166f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1167f64122c1SDave Airlie 	struct drm_connector *connector;
1168f64122c1SDave Airlie 	struct drm_encoder *encoder;
1169f64122c1SDave Airlie 
1170f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1171f64122c1SDave Airlie 	if (!qxl_output)
1172f64122c1SDave Airlie 		return -ENOMEM;
1173f64122c1SDave Airlie 
1174f64122c1SDave Airlie 	qxl_output->index = num_output;
1175f64122c1SDave Airlie 
1176f64122c1SDave Airlie 	connector = &qxl_output->base;
1177f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1178f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1179f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1180f64122c1SDave Airlie 
1181f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
118213a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
1183f64122c1SDave Airlie 
11845ff91e44SDave Airlie 	/* we get HPD via client monitors config */
11855ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1186f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1187f64122c1SDave Airlie 	drm_mode_connector_attach_encoder(&qxl_output->base,
1188f64122c1SDave Airlie 					  &qxl_output->enc);
1189f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1190f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1191f64122c1SDave Airlie 
11924695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11934695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11947dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11957dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11967dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11977dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1198f64122c1SDave Airlie 	return 0;
1199f64122c1SDave Airlie }
1200f64122c1SDave Airlie 
1201f64122c1SDave Airlie static struct drm_framebuffer *
1202f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1203f64122c1SDave Airlie 			    struct drm_file *file_priv,
12041eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1205f64122c1SDave Airlie {
1206f64122c1SDave Airlie 	struct drm_gem_object *obj;
1207f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb;
1208f64122c1SDave Airlie 	int ret;
1209f64122c1SDave Airlie 
1210a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1211a8ad0bd8SChris Wilson 	if (!obj)
1212a8ad0bd8SChris Wilson 		return NULL;
1213f64122c1SDave Airlie 
1214f64122c1SDave Airlie 	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1215f64122c1SDave Airlie 	if (qxl_fb == NULL)
1216f64122c1SDave Airlie 		return NULL;
1217f64122c1SDave Airlie 
12186819c3c2SNoralf Trønnes 	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1219f64122c1SDave Airlie 	if (ret) {
1220f64122c1SDave Airlie 		kfree(qxl_fb);
12212793c1d7SSantha Meena Ramamoorthy 		drm_gem_object_put_unlocked(obj);
1222f64122c1SDave Airlie 		return NULL;
1223f64122c1SDave Airlie 	}
1224f64122c1SDave Airlie 
1225f64122c1SDave Airlie 	return &qxl_fb->base;
1226f64122c1SDave Airlie }
1227f64122c1SDave Airlie 
1228f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1229f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1230472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1231472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1232f64122c1SDave Airlie };
1233f64122c1SDave Airlie 
12342bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1235f64122c1SDave Airlie {
1236f64122c1SDave Airlie 	int ret;
1237f64122c1SDave Airlie 	struct drm_gem_object *gobj;
123807f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
1239f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1240f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
1241f64122c1SDave Airlie 
1242f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1243f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1244f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1245f64122c1SDave Airlie 	if (ret) {
1246f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1247f64122c1SDave Airlie 		return -ENOMEM;
1248f64122c1SDave Airlie 	}
1249f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
12502bd6ce84SDave Airlie 
1251715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
12522bd6ce84SDave Airlie 	if (ret)
12532bd6ce84SDave Airlie 		return ret;
12542bd6ce84SDave Airlie 
1255f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
12562bd6ce84SDave Airlie 
1257f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1258f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1259f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1260f64122c1SDave Airlie 
1261f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
1262f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
12632bd6ce84SDave Airlie 	return 0;
12642bd6ce84SDave Airlie }
12652bd6ce84SDave Airlie 
12662bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
12672bd6ce84SDave Airlie {
12682bd6ce84SDave Airlie 	int ret;
12692bd6ce84SDave Airlie 
12702bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
12712bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
12722bd6ce84SDave Airlie 
12732bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
1274715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_unpin(qdev->monitors_config_bo);
12752bd6ce84SDave Airlie 	if (ret)
12762bd6ce84SDave Airlie 		return ret;
12772bd6ce84SDave Airlie 
12782bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
12792bd6ce84SDave Airlie 	return 0;
12802bd6ce84SDave Airlie }
12812bd6ce84SDave Airlie 
12822bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
12832bd6ce84SDave Airlie {
12842bd6ce84SDave Airlie 	int i;
12852bd6ce84SDave Airlie 	int ret;
12862bd6ce84SDave Airlie 
1287cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_init(&qdev->ddev);
12882bd6ce84SDave Airlie 
12892bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
12902bd6ce84SDave Airlie 	if (ret)
12912bd6ce84SDave Airlie 		return ret;
1292f64122c1SDave Airlie 
1293cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1294f64122c1SDave Airlie 
1295f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
12961277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_width = 0;
12971277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_height = 0;
1298cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_width = 8192;
1299cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_height = 8192;
1300f64122c1SDave Airlie 
1301cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.fb_base = qdev->vram_base;
13024695b039SDave Airlie 
1303cbdded7fSGabriel Krisman Bertazi 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
13044695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
13054695b039SDave Airlie 
130607f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1307cbdded7fSGabriel Krisman Bertazi 		qdev_crtc_init(&qdev->ddev, i);
1308cbdded7fSGabriel Krisman Bertazi 		qdev_output_init(&qdev->ddev, i);
1309f64122c1SDave Airlie 	}
1310f64122c1SDave Airlie 
1311c50fad8fSGerd Hoffmann 	qxl_display_read_client_monitors_config(qdev);
1312f64122c1SDave Airlie 	qdev->mode_info.mode_config_initialized = true;
1313f64122c1SDave Airlie 
13149ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
13159ade8b98SGabriel Krisman Bertazi 
1316f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
1317f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
1318f64122c1SDave Airlie 	 * spice server. */
1319f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
1320f64122c1SDave Airlie 	return 0;
1321f64122c1SDave Airlie }
1322f64122c1SDave Airlie 
1323f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1324f64122c1SDave Airlie {
1325f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
13262bd6ce84SDave Airlie 
13272bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1328f64122c1SDave Airlie 	if (qdev->mode_info.mode_config_initialized) {
1329cbdded7fSGabriel Krisman Bertazi 		drm_mode_config_cleanup(&qdev->ddev);
1330f64122c1SDave Airlie 		qdev->mode_info.mode_config_initialized = false;
1331f64122c1SDave Airlie 	}
1332f64122c1SDave Airlie }
1333