xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision a6d3c4d79822658e7f2f9c4b73237fe2b057ed67)
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);
51735581a0SGerd Hoffmann 		if (!qdev->client_monitors_config)
52f64122c1SDave Airlie 			return;
53f64122c1SDave Airlie 	}
54f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
55f64122c1SDave Airlie }
56f64122c1SDave Airlie 
579e3b3178SChristophe Fergeau enum {
589e3b3178SChristophe Fergeau 	MONITORS_CONFIG_MODIFIED,
599e3b3178SChristophe Fergeau 	MONITORS_CONFIG_UNCHANGED,
609e3b3178SChristophe Fergeau 	MONITORS_CONFIG_BAD_CRC,
619e3b3178SChristophe Fergeau };
629e3b3178SChristophe Fergeau 
63f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
64f64122c1SDave Airlie {
65f64122c1SDave Airlie 	int i;
66f64122c1SDave Airlie 	int num_monitors;
67f64122c1SDave Airlie 	uint32_t crc;
689e3b3178SChristophe Fergeau 	int status = MONITORS_CONFIG_UNCHANGED;
69f64122c1SDave Airlie 
70f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
71f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
72f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
73735581a0SGerd Hoffmann 	if (crc != qdev->rom->client_monitors_config_crc)
749e3b3178SChristophe Fergeau 		return MONITORS_CONFIG_BAD_CRC;
75c50fad8fSGerd Hoffmann 	if (!num_monitors) {
76c50fad8fSGerd Hoffmann 		DRM_DEBUG_KMS("no client monitors configured\n");
77c50fad8fSGerd Hoffmann 		return status;
78c50fad8fSGerd Hoffmann 	}
79f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
805b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
81f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
82f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
83f64122c1SDave Airlie 	} else {
84f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
85f64122c1SDave Airlie 	}
869e3b3178SChristophe Fergeau 	if (qdev->client_monitors_config
879e3b3178SChristophe Fergeau 	      && (num_monitors != qdev->client_monitors_config->count)) {
889e3b3178SChristophe Fergeau 		status = MONITORS_CONFIG_MODIFIED;
899e3b3178SChristophe Fergeau 	}
90f64122c1SDave Airlie 	qxl_alloc_client_monitors_config(qdev, num_monitors);
91f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
92f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
93f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
94f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
95f64122c1SDave Airlie 		struct qxl_urect *c_rect =
96f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
97f64122c1SDave Airlie 		struct qxl_head *client_head =
98f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
999e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
10007f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1019e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1029e3b3178SChristophe Fergeau 		}
1039e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
10407f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1059e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1069e3b3178SChristophe Fergeau 		}
1079e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
10807f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1099e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1109e3b3178SChristophe Fergeau 		}
1119e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
11207f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1139e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1149e3b3178SChristophe Fergeau 		}
1159e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
11607f8d9bdSDave Airlie 			client_head->surface_id = 0;
1179e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1189e3b3178SChristophe Fergeau 		}
1199e3b3178SChristophe Fergeau 		if (client_head->id != i) {
12007f8d9bdSDave Airlie 			client_head->id = i;
1219e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1229e3b3178SChristophe Fergeau 		}
1239e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
12407f8d9bdSDave Airlie 			client_head->flags = 0;
1259e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1269e3b3178SChristophe Fergeau 		}
12707f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
12807f8d9bdSDave Airlie 			  client_head->x, client_head->y);
129f64122c1SDave Airlie 	}
1309e3b3178SChristophe Fergeau 
1319e3b3178SChristophe Fergeau 	return status;
132f64122c1SDave Airlie }
133f64122c1SDave Airlie 
1347dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1357dea0941SDave Airlie {
136cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1377dea0941SDave Airlie 	struct drm_connector *connector;
1387dea0941SDave Airlie 	struct qxl_output *output;
1397dea0941SDave Airlie 	struct qxl_head *head;
1407dea0941SDave Airlie 
1417dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1427dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1437dea0941SDave Airlie 
1447dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1457dea0941SDave Airlie 
1467dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1477dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1487dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1497dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1507dea0941SDave Airlie 	}
1517dea0941SDave Airlie }
1527dea0941SDave Airlie 
153f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
154f64122c1SDave Airlie {
155cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1569062155dSGerd Hoffmann 	int status, retries;
1579e3b3178SChristophe Fergeau 
1589062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1599e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1609062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1619062155dSGerd Hoffmann 			break;
1629062155dSGerd Hoffmann 		udelay(5);
1639062155dSGerd Hoffmann 	}
1649062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1659062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1669062155dSGerd Hoffmann 		return;
1679e3b3178SChristophe Fergeau 	}
1689e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1699062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1709e3b3178SChristophe Fergeau 		return;
171f64122c1SDave Airlie 	}
1724fdb0869SMarc-André Lureau 
1737dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1747dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1757dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
176cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1774fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1784fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
179cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1804fdb0869SMarc-André Lureau 	}
181f64122c1SDave Airlie }
182f64122c1SDave Airlie 
183b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector,
184b0807423SMarc-André Lureau                                          unsigned *pwidth,
185b0807423SMarc-André Lureau                                          unsigned *pheight)
186f64122c1SDave Airlie {
187f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
188f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
189f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
190f64122c1SDave Airlie 	int h = output->index;
191f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
192f64122c1SDave Airlie 	struct qxl_head *head;
193f64122c1SDave Airlie 
1942d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
1952d856f94SGerd Hoffmann 		return 0;
1962d856f94SGerd Hoffmann 	if (h >= qdev->monitors_config->max_allowed)
1972d856f94SGerd Hoffmann 		return 0;
19807f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
199f64122c1SDave Airlie 		return 0;
2002d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2012d856f94SGerd Hoffmann 		return 0;
2022d856f94SGerd Hoffmann 
20307f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2042d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
205f64122c1SDave Airlie 
206f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
207f64122c1SDave Airlie 			    false);
208f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
209ff996e72SChristophe Fergeau 	mode->hdisplay = head->width;
210ff996e72SChristophe Fergeau 	mode->vdisplay = head->height;
211ff996e72SChristophe Fergeau 	drm_mode_set_name(mode);
212b0807423SMarc-André Lureau 	*pwidth = head->width;
213b0807423SMarc-André Lureau 	*pheight = head->height;
214f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
215bd3e1c7cSJonathon Jongsma 	/* remember the last custom size for mode validation */
216bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_width = mode->hdisplay;
217bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_height = mode->vdisplay;
218f64122c1SDave Airlie 	return 1;
219f64122c1SDave Airlie }
220f64122c1SDave Airlie 
221bd3e1c7cSJonathon Jongsma static struct mode_size {
222f64122c1SDave Airlie 	int w;
223f64122c1SDave Airlie 	int h;
224f64122c1SDave Airlie } common_modes[] = {
225f64122c1SDave Airlie 	{ 640,  480},
226f64122c1SDave Airlie 	{ 720,  480},
227f64122c1SDave Airlie 	{ 800,  600},
228f64122c1SDave Airlie 	{ 848,  480},
229f64122c1SDave Airlie 	{1024,  768},
230f64122c1SDave Airlie 	{1152,  768},
231f64122c1SDave Airlie 	{1280,  720},
232f64122c1SDave Airlie 	{1280,  800},
233f64122c1SDave Airlie 	{1280,  854},
234f64122c1SDave Airlie 	{1280,  960},
235f64122c1SDave Airlie 	{1280, 1024},
236f64122c1SDave Airlie 	{1440,  900},
237f64122c1SDave Airlie 	{1400, 1050},
238f64122c1SDave Airlie 	{1680, 1050},
239f64122c1SDave Airlie 	{1600, 1200},
240f64122c1SDave Airlie 	{1920, 1080},
241f64122c1SDave Airlie 	{1920, 1200}
242f64122c1SDave Airlie };
243f64122c1SDave Airlie 
244bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector,
245bd3e1c7cSJonathon Jongsma                                 unsigned pwidth,
246bd3e1c7cSJonathon Jongsma                                 unsigned pheight)
247bd3e1c7cSJonathon Jongsma {
248bd3e1c7cSJonathon Jongsma 	struct drm_device *dev = connector->dev;
249bd3e1c7cSJonathon Jongsma 	struct drm_display_mode *mode = NULL;
250bd3e1c7cSJonathon Jongsma 	int i;
251f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
252f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
253f64122c1SDave Airlie 				    60, false, false, false);
254b0807423SMarc-André Lureau 		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
255f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
256f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
257f64122c1SDave Airlie 	}
258f64122c1SDave Airlie 	return i - 1;
259f64122c1SDave Airlie }
260f64122c1SDave Airlie 
261998010bfSGerd Hoffmann static void qxl_send_monitors_config(struct qxl_device *qdev)
262998010bfSGerd Hoffmann {
263998010bfSGerd Hoffmann 	int i;
264998010bfSGerd Hoffmann 
265998010bfSGerd Hoffmann 	BUG_ON(!qdev->ram_header->monitors_config);
266998010bfSGerd Hoffmann 
267998010bfSGerd Hoffmann 	if (qdev->monitors_config->count == 0)
268998010bfSGerd Hoffmann 		return;
269998010bfSGerd Hoffmann 
270998010bfSGerd Hoffmann 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
271998010bfSGerd Hoffmann 		struct qxl_head *head = &qdev->monitors_config->heads[i];
272998010bfSGerd Hoffmann 
273998010bfSGerd Hoffmann 		if (head->y > 8192 || head->x > 8192 ||
274998010bfSGerd Hoffmann 		    head->width > 8192 || head->height > 8192) {
275998010bfSGerd Hoffmann 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
276998010bfSGerd Hoffmann 				  i, head->width, head->height,
277998010bfSGerd Hoffmann 				  head->x, head->y);
278998010bfSGerd Hoffmann 			return;
279998010bfSGerd Hoffmann 		}
280998010bfSGerd Hoffmann 	}
281998010bfSGerd Hoffmann 	qxl_io_monitors_config(qdev);
282998010bfSGerd Hoffmann }
283998010bfSGerd Hoffmann 
284*a6d3c4d7SGerd Hoffmann static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
285*a6d3c4d7SGerd Hoffmann 					    const char *reason)
286*a6d3c4d7SGerd Hoffmann {
287*a6d3c4d7SGerd Hoffmann 	struct drm_device *dev = crtc->dev;
288*a6d3c4d7SGerd Hoffmann 	struct qxl_device *qdev = dev->dev_private;
289*a6d3c4d7SGerd Hoffmann 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
290*a6d3c4d7SGerd Hoffmann 	struct qxl_head head;
291*a6d3c4d7SGerd Hoffmann 	int oldcount, i = qcrtc->index;
292*a6d3c4d7SGerd Hoffmann 
293*a6d3c4d7SGerd Hoffmann 	if (!qdev->primary_created) {
294*a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
295*a6d3c4d7SGerd Hoffmann 		return;
296*a6d3c4d7SGerd Hoffmann 	}
297*a6d3c4d7SGerd Hoffmann 
298*a6d3c4d7SGerd Hoffmann 	if (!qdev->monitors_config ||
299*a6d3c4d7SGerd Hoffmann 	    qdev->monitors_config->max_allowed <= i)
300*a6d3c4d7SGerd Hoffmann 		return;
301*a6d3c4d7SGerd Hoffmann 
302*a6d3c4d7SGerd Hoffmann 	head.id = i;
303*a6d3c4d7SGerd Hoffmann 	head.flags = 0;
304*a6d3c4d7SGerd Hoffmann 	oldcount = qdev->monitors_config->count;
305*a6d3c4d7SGerd Hoffmann 	if (crtc->state->active) {
306*a6d3c4d7SGerd Hoffmann 		struct drm_display_mode *mode = &crtc->mode;
307*a6d3c4d7SGerd Hoffmann 		head.width = mode->hdisplay;
308*a6d3c4d7SGerd Hoffmann 		head.height = mode->vdisplay;
309*a6d3c4d7SGerd Hoffmann 		head.x = crtc->x;
310*a6d3c4d7SGerd Hoffmann 		head.y = crtc->y;
311*a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count < i + 1)
312*a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i + 1;
313*a6d3c4d7SGerd Hoffmann 	} else if (i > 0) {
314*a6d3c4d7SGerd Hoffmann 		head.width = 0;
315*a6d3c4d7SGerd Hoffmann 		head.height = 0;
316*a6d3c4d7SGerd Hoffmann 		head.x = 0;
317*a6d3c4d7SGerd Hoffmann 		head.y = 0;
318*a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count == i + 1)
319*a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i;
320*a6d3c4d7SGerd Hoffmann 	} else {
321*a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
322*a6d3c4d7SGerd Hoffmann 		return;
323*a6d3c4d7SGerd Hoffmann 	}
324*a6d3c4d7SGerd Hoffmann 
325*a6d3c4d7SGerd Hoffmann 	if (head.width  == qdev->monitors_config->heads[i].width  &&
326*a6d3c4d7SGerd Hoffmann 	    head.height == qdev->monitors_config->heads[i].height &&
327*a6d3c4d7SGerd Hoffmann 	    head.x      == qdev->monitors_config->heads[i].x      &&
328*a6d3c4d7SGerd Hoffmann 	    head.y      == qdev->monitors_config->heads[i].y      &&
329*a6d3c4d7SGerd Hoffmann 	    oldcount    == qdev->monitors_config->count)
330*a6d3c4d7SGerd Hoffmann 		return;
331*a6d3c4d7SGerd Hoffmann 
332*a6d3c4d7SGerd Hoffmann 	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
333*a6d3c4d7SGerd Hoffmann 		      i, head.width, head.height, head.x, head.y,
334*a6d3c4d7SGerd Hoffmann 		      crtc->state->active ? "on" : "off", reason);
335*a6d3c4d7SGerd Hoffmann 	if (oldcount != qdev->monitors_config->count)
336*a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
337*a6d3c4d7SGerd Hoffmann 			      oldcount, qdev->monitors_config->count,
338*a6d3c4d7SGerd Hoffmann 			      qdev->monitors_config->max_allowed);
339*a6d3c4d7SGerd Hoffmann 
340*a6d3c4d7SGerd Hoffmann 	qdev->monitors_config->heads[i] = head;
341*a6d3c4d7SGerd Hoffmann 	qxl_send_monitors_config(qdev);
342*a6d3c4d7SGerd Hoffmann }
343*a6d3c4d7SGerd Hoffmann 
344c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
345c2ff6632SGabriel Krisman Bertazi 				  struct drm_crtc_state *old_crtc_state)
346c2ff6632SGabriel Krisman Bertazi {
347c2ff6632SGabriel Krisman Bertazi 	struct drm_device *dev = crtc->dev;
348c2ff6632SGabriel Krisman Bertazi 	struct drm_pending_vblank_event *event;
349c2ff6632SGabriel Krisman Bertazi 	unsigned long flags;
350c2ff6632SGabriel Krisman Bertazi 
351c2ff6632SGabriel Krisman Bertazi 	if (crtc->state && crtc->state->event) {
352c2ff6632SGabriel Krisman Bertazi 		event = crtc->state->event;
353c2ff6632SGabriel Krisman Bertazi 		crtc->state->event = NULL;
354c2ff6632SGabriel Krisman Bertazi 
355c2ff6632SGabriel Krisman Bertazi 		spin_lock_irqsave(&dev->event_lock, flags);
356c2ff6632SGabriel Krisman Bertazi 		drm_crtc_send_vblank_event(crtc, event);
357c2ff6632SGabriel Krisman Bertazi 		spin_unlock_irqrestore(&dev->event_lock, flags);
358c2ff6632SGabriel Krisman Bertazi 	}
359*a6d3c4d7SGerd Hoffmann 
360*a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "flush");
361c2ff6632SGabriel Krisman Bertazi }
362c2ff6632SGabriel Krisman Bertazi 
363f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
364f64122c1SDave Airlie {
365f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
366f64122c1SDave Airlie 
3679428088cSRay Strode 	qxl_bo_unref(&qxl_crtc->cursor_bo);
368f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
369f64122c1SDave Airlie 	kfree(qxl_crtc);
370f64122c1SDave Airlie }
371f64122c1SDave Airlie 
372f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
373bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
374f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
3759973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
3769ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
3779ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
3789ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
379f64122c1SDave Airlie };
380f64122c1SDave Airlie 
3816819c3c2SNoralf Trønnes void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
382f64122c1SDave Airlie {
383f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
38462676d10SGerd Hoffmann 	struct qxl_bo *bo = gem_to_qxl_bo(qxl_fb->obj);
385f64122c1SDave Airlie 
38662676d10SGerd Hoffmann 	WARN_ON(bo->shadow);
3872793c1d7SSantha Meena Ramamoorthy 	drm_gem_object_put_unlocked(qxl_fb->obj);
388f64122c1SDave Airlie 	drm_framebuffer_cleanup(fb);
389f64122c1SDave Airlie 	kfree(qxl_fb);
390f64122c1SDave Airlie }
391f64122c1SDave Airlie 
3926d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
393f64122c1SDave Airlie 					 struct drm_file *file_priv,
394f64122c1SDave Airlie 					 unsigned flags, unsigned color,
395f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
396f64122c1SDave Airlie 					 unsigned num_clips)
397f64122c1SDave Airlie {
398f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
399f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
400f64122c1SDave Airlie 	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
401f64122c1SDave Airlie 	struct drm_clip_rect norect;
402f64122c1SDave Airlie 	struct qxl_bo *qobj;
403f64122c1SDave Airlie 	int inc = 1;
404f64122c1SDave Airlie 
40573e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
40673e9efd4SVille Syrjälä 
407f64122c1SDave Airlie 	qobj = gem_to_qxl_bo(qxl_fb->obj);
408b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
40973e9efd4SVille Syrjälä 	if (!qobj->is_primary) {
41073e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
411b2b4465dSDave Airlie 		return 0;
41273e9efd4SVille Syrjälä 	}
413b2b4465dSDave Airlie 
414f64122c1SDave Airlie 	if (!num_clips) {
415f64122c1SDave Airlie 		num_clips = 1;
416f64122c1SDave Airlie 		clips = &norect;
417f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
418f64122c1SDave Airlie 		norect.x2 = fb->width;
419f64122c1SDave Airlie 		norect.y2 = fb->height;
420f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
421f64122c1SDave Airlie 		num_clips /= 2;
422f64122c1SDave Airlie 		inc = 2; /* skip source rects */
423f64122c1SDave Airlie 	}
424f64122c1SDave Airlie 
425f64122c1SDave Airlie 	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
426f64122c1SDave Airlie 			  clips, num_clips, inc);
42773e9efd4SVille Syrjälä 
42873e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
42973e9efd4SVille Syrjälä 
430f64122c1SDave Airlie 	return 0;
431f64122c1SDave Airlie }
432f64122c1SDave Airlie 
433f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
434f64122c1SDave Airlie 	.destroy = qxl_user_framebuffer_destroy,
435f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
436f64122c1SDave Airlie /*	TODO?
437f64122c1SDave Airlie  *	.create_handle = qxl_user_framebuffer_create_handle, */
438f64122c1SDave Airlie };
439f64122c1SDave Airlie 
440f64122c1SDave Airlie int
441f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev,
442f64122c1SDave Airlie 		     struct qxl_framebuffer *qfb,
4431eb83451SVille Syrjälä 		     const struct drm_mode_fb_cmd2 *mode_cmd,
4446819c3c2SNoralf Trønnes 		     struct drm_gem_object *obj,
4456819c3c2SNoralf Trønnes 		     const struct drm_framebuffer_funcs *funcs)
446f64122c1SDave Airlie {
447f64122c1SDave Airlie 	int ret;
448f64122c1SDave Airlie 
449f64122c1SDave Airlie 	qfb->obj = obj;
4505360943fSVille Syrjälä 	drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
4516819c3c2SNoralf Trønnes 	ret = drm_framebuffer_init(dev, &qfb->base, funcs);
452f64122c1SDave Airlie 	if (ret) {
453f64122c1SDave Airlie 		qfb->obj = NULL;
454f64122c1SDave Airlie 		return ret;
455f64122c1SDave Airlie 	}
456f64122c1SDave Airlie 	return 0;
457f64122c1SDave Airlie }
458f64122c1SDave Airlie 
459f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
460f64122c1SDave Airlie 				  const struct drm_display_mode *mode,
461f64122c1SDave Airlie 				  struct drm_display_mode *adjusted_mode)
462f64122c1SDave Airlie {
463f64122c1SDave Airlie 	return true;
464f64122c1SDave Airlie }
465f64122c1SDave Airlie 
4660b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
4670b20a0f8SLaurent Pinchart 				   struct drm_crtc_state *old_state)
468f64122c1SDave Airlie {
469*a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "enable");
470f64122c1SDave Airlie }
471f64122c1SDave Airlie 
47264581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
47364581714SLaurent Pinchart 				    struct drm_crtc_state *old_state)
47407f8d9bdSDave Airlie {
475*a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "disable");
47607f8d9bdSDave Airlie }
47707f8d9bdSDave Airlie 
478f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
479f64122c1SDave Airlie 	.mode_fixup = qxl_crtc_mode_fixup,
480c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4810b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
48264581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
483f64122c1SDave Airlie };
484f64122c1SDave Airlie 
48545dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
486c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
487c2ff6632SGabriel Krisman Bertazi {
488c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
489c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb;
490c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
491c2ff6632SGabriel Krisman Bertazi 
492c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
493c2ff6632SGabriel Krisman Bertazi 		return 0;
494c2ff6632SGabriel Krisman Bertazi 
495c2ff6632SGabriel Krisman Bertazi 	qfb = to_qxl_framebuffer(state->fb);
496c2ff6632SGabriel Krisman Bertazi 	bo = gem_to_qxl_bo(qfb->obj);
497c2ff6632SGabriel Krisman Bertazi 
498c2ff6632SGabriel Krisman Bertazi 	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
499c2ff6632SGabriel Krisman Bertazi 		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
500c2ff6632SGabriel Krisman Bertazi 		return -EINVAL;
501c2ff6632SGabriel Krisman Bertazi 	}
502c2ff6632SGabriel Krisman Bertazi 
503c2ff6632SGabriel Krisman Bertazi 	return 0;
504c2ff6632SGabriel Krisman Bertazi }
505c2ff6632SGabriel Krisman Bertazi 
5069428088cSRay Strode static int qxl_primary_apply_cursor(struct drm_plane *plane)
5079428088cSRay Strode {
5089428088cSRay Strode 	struct drm_device *dev = plane->dev;
5099428088cSRay Strode 	struct qxl_device *qdev = dev->dev_private;
5109428088cSRay Strode 	struct drm_framebuffer *fb = plane->state->fb;
5119428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
5129428088cSRay Strode 	struct qxl_cursor_cmd *cmd;
5139428088cSRay Strode 	struct qxl_release *release;
5149428088cSRay Strode 	int ret = 0;
5159428088cSRay Strode 
5169428088cSRay Strode 	if (!qcrtc->cursor_bo)
5179428088cSRay Strode 		return 0;
5189428088cSRay Strode 
5199428088cSRay Strode 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5209428088cSRay Strode 					 QXL_RELEASE_CURSOR_CMD,
5219428088cSRay Strode 					 &release, NULL);
5229428088cSRay Strode 	if (ret)
5239428088cSRay Strode 		return ret;
5249428088cSRay Strode 
5259428088cSRay Strode 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
5269428088cSRay Strode 	if (ret)
5279428088cSRay Strode 		goto out_free_release;
5289428088cSRay Strode 
5299428088cSRay Strode 	ret = qxl_release_reserve_list(release, false);
5309428088cSRay Strode 	if (ret)
5319428088cSRay Strode 		goto out_free_release;
5329428088cSRay Strode 
5339428088cSRay Strode 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5349428088cSRay Strode 	cmd->type = QXL_CURSOR_SET;
5359428088cSRay Strode 	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
5369428088cSRay Strode 	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
5379428088cSRay Strode 
5389428088cSRay Strode 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5399428088cSRay Strode 
5409428088cSRay Strode 	cmd->u.set.visible = 1;
5419428088cSRay Strode 	qxl_release_unmap(qdev, release, &cmd->release_info);
5429428088cSRay Strode 
5439428088cSRay Strode 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5449428088cSRay Strode 	qxl_release_fence_buffer_objects(release);
5459428088cSRay Strode 
5469428088cSRay Strode 	return ret;
5479428088cSRay Strode 
5489428088cSRay Strode out_free_release:
5499428088cSRay Strode 	qxl_release_free(qdev, release);
5509428088cSRay Strode 	return ret;
5519428088cSRay Strode }
5529428088cSRay Strode 
553c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
554c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
555c2ff6632SGabriel Krisman Bertazi {
556c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
557c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb =
558c2ff6632SGabriel Krisman Bertazi 		to_qxl_framebuffer(plane->state->fb);
559c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb_old;
560c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
561c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo_old;
562c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
563c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
564c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
565c2ff6632SGabriel Krisman Bertazi 	    .x2 = qfb->base.width,
566c2ff6632SGabriel Krisman Bertazi 	    .y2 = qfb->base.height
567c2ff6632SGabriel Krisman Bertazi 	};
5689428088cSRay Strode 	int ret;
56962676d10SGerd Hoffmann 	bool same_shadow = false;
570c2ff6632SGabriel Krisman Bertazi 
571b0e07da3SGerd Hoffmann 	if (old_state->fb) {
572c2ff6632SGabriel Krisman Bertazi 		qfb_old = to_qxl_framebuffer(old_state->fb);
573c2ff6632SGabriel Krisman Bertazi 		bo_old = gem_to_qxl_bo(qfb_old->obj);
574b0e07da3SGerd Hoffmann 	} else {
575b0e07da3SGerd Hoffmann 		bo_old = NULL;
576b0e07da3SGerd Hoffmann 	}
577b0e07da3SGerd Hoffmann 
578b0e07da3SGerd Hoffmann 	if (bo == bo_old)
579b0e07da3SGerd Hoffmann 		return;
580b0e07da3SGerd Hoffmann 
58162676d10SGerd Hoffmann 	if (bo_old && bo_old->shadow && bo->shadow &&
58262676d10SGerd Hoffmann 	    bo_old->shadow == bo->shadow) {
58362676d10SGerd Hoffmann 		same_shadow = true;
58462676d10SGerd Hoffmann 	}
58562676d10SGerd Hoffmann 
586b0e07da3SGerd Hoffmann 	if (bo_old && bo_old->is_primary) {
58762676d10SGerd Hoffmann 		if (!same_shadow)
588b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
589c2ff6632SGabriel Krisman Bertazi 		bo_old->is_primary = false;
5909428088cSRay Strode 
5919428088cSRay Strode 		ret = qxl_primary_apply_cursor(plane);
5929428088cSRay Strode 		if (ret)
5939428088cSRay Strode 			DRM_ERROR(
5949428088cSRay Strode 			"could not set cursor after creating primary");
595c2ff6632SGabriel Krisman Bertazi 	}
596c2ff6632SGabriel Krisman Bertazi 
597b0e07da3SGerd Hoffmann 	if (!bo->is_primary) {
59862676d10SGerd Hoffmann 		if (!same_shadow)
599b0e07da3SGerd Hoffmann 			qxl_io_create_primary(qdev, 0, bo);
600c2ff6632SGabriel Krisman Bertazi 		bo->is_primary = true;
601b0e07da3SGerd Hoffmann 	}
60262676d10SGerd Hoffmann 
603c2ff6632SGabriel Krisman Bertazi 	qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
604c2ff6632SGabriel Krisman Bertazi }
605c2ff6632SGabriel Krisman Bertazi 
606c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
607c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
608c2ff6632SGabriel Krisman Bertazi {
609c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
610c2ff6632SGabriel Krisman Bertazi 
611b0e07da3SGerd Hoffmann 	if (old_state->fb) {
612b0e07da3SGerd Hoffmann 		struct qxl_framebuffer *qfb =
613c2ff6632SGabriel Krisman Bertazi 			to_qxl_framebuffer(old_state->fb);
614c2ff6632SGabriel Krisman Bertazi 		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
615c2ff6632SGabriel Krisman Bertazi 
616b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
617c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
618c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
619c2ff6632SGabriel Krisman Bertazi 		}
620c2ff6632SGabriel Krisman Bertazi 	}
621b0e07da3SGerd Hoffmann }
622c2ff6632SGabriel Krisman Bertazi 
62345dfe577SGerd Hoffmann static int qxl_plane_atomic_check(struct drm_plane *plane,
6241277eed5SGabriel Krisman Bertazi 				  struct drm_plane_state *state)
6251277eed5SGabriel Krisman Bertazi {
6261277eed5SGabriel Krisman Bertazi 	return 0;
6271277eed5SGabriel Krisman Bertazi }
6281277eed5SGabriel Krisman Bertazi 
6291277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
6301277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
6311277eed5SGabriel Krisman Bertazi {
6321277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
6331277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
6341277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
6359428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
6361277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6371277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6381277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
6391277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
64016c6db36SRay Strode 	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL;
6411277eed5SGabriel Krisman Bertazi 	int ret;
6421277eed5SGabriel Krisman Bertazi 	void *user_ptr;
6431277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
6441277eed5SGabriel Krisman Bertazi 
6451277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6461277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
6471277eed5SGabriel Krisman Bertazi 					 &release, NULL);
648ee5cb7c4SDan Carpenter 	if (ret)
649ee5cb7c4SDan Carpenter 		return;
6501277eed5SGabriel Krisman Bertazi 
6511277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
6521277eed5SGabriel Krisman Bertazi 		obj = to_qxl_framebuffer(fb)->obj;
6531277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
6541277eed5SGabriel Krisman Bertazi 
6551277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
6561277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
6571277eed5SGabriel Krisman Bertazi 		if (ret)
6581277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6591277eed5SGabriel Krisman Bertazi 
6601277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
6611277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6621277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6631277eed5SGabriel Krisman Bertazi 		if (ret)
6641277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6651277eed5SGabriel Krisman Bertazi 
6661277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6671277eed5SGabriel Krisman Bertazi 		if (ret)
6681277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6691277eed5SGabriel Krisman Bertazi 
6701277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6711277eed5SGabriel Krisman Bertazi 		if (ret)
6721277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6731277eed5SGabriel Krisman Bertazi 
6741277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6751277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6761277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6771277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6781277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6791277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6801277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6811277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6821277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6831277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6841277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6851277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6861277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6871277eed5SGabriel Krisman Bertazi 
688429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6891277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6901277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6911277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6921277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6939428088cSRay Strode 
6949428088cSRay Strode 		qxl_bo_unref(&qcrtc->cursor_bo);
6959428088cSRay Strode 		qcrtc->cursor_bo = cursor_bo;
6969428088cSRay Strode 		cursor_bo = NULL;
6971277eed5SGabriel Krisman Bertazi 	} else {
6981277eed5SGabriel Krisman Bertazi 
6991277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
7001277eed5SGabriel Krisman Bertazi 		if (ret)
7011277eed5SGabriel Krisman Bertazi 			goto out_free_release;
7021277eed5SGabriel Krisman Bertazi 
703429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
7041277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
7051277eed5SGabriel Krisman Bertazi 	}
7061277eed5SGabriel Krisman Bertazi 
7071277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
7081277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
7091277eed5SGabriel Krisman Bertazi 
7101277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7111277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7121277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7131277eed5SGabriel Krisman Bertazi 
71416c6db36SRay Strode 	qxl_bo_unref(&cursor_bo);
71516c6db36SRay Strode 
7161277eed5SGabriel Krisman Bertazi 	return;
7171277eed5SGabriel Krisman Bertazi 
7181277eed5SGabriel Krisman Bertazi out_backoff:
7191277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
7201277eed5SGabriel Krisman Bertazi out_free_bo:
7211277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
7221277eed5SGabriel Krisman Bertazi out_kunmap:
7231277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
7241277eed5SGabriel Krisman Bertazi out_free_release:
7251277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
7261277eed5SGabriel Krisman Bertazi 	return;
7271277eed5SGabriel Krisman Bertazi 
7281277eed5SGabriel Krisman Bertazi }
7291277eed5SGabriel Krisman Bertazi 
73045dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
7311277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
7321277eed5SGabriel Krisman Bertazi {
7331277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
7341277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
7351277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
7361277eed5SGabriel Krisman Bertazi 	int ret;
7371277eed5SGabriel Krisman Bertazi 
7381277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
7391277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
7401277eed5SGabriel Krisman Bertazi 					 &release, NULL);
7411277eed5SGabriel Krisman Bertazi 	if (ret)
7421277eed5SGabriel Krisman Bertazi 		return;
7431277eed5SGabriel Krisman Bertazi 
7441277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
7451277eed5SGabriel Krisman Bertazi 	if (ret) {
7461277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
7471277eed5SGabriel Krisman Bertazi 		return;
7481277eed5SGabriel Krisman Bertazi 	}
7491277eed5SGabriel Krisman Bertazi 
7501277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7511277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
7521277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7531277eed5SGabriel Krisman Bertazi 
7541277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7551277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7561277eed5SGabriel Krisman Bertazi }
7571277eed5SGabriel Krisman Bertazi 
75845dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
7591277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
7601277eed5SGabriel Krisman Bertazi {
76162676d10SGerd Hoffmann 	struct qxl_device *qdev = plane->dev->dev_private;
7621277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
76362676d10SGerd Hoffmann 	struct qxl_bo *user_bo, *old_bo = NULL;
7641277eed5SGabriel Krisman Bertazi 	int ret;
7651277eed5SGabriel Krisman Bertazi 
7661277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
7671277eed5SGabriel Krisman Bertazi 		return 0;
7681277eed5SGabriel Krisman Bertazi 
7691277eed5SGabriel Krisman Bertazi 	obj = to_qxl_framebuffer(new_state->fb)->obj;
7701277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7711277eed5SGabriel Krisman Bertazi 
77262676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
77362676d10SGerd Hoffmann 	    user_bo->is_dumb && !user_bo->shadow) {
77462676d10SGerd Hoffmann 		if (plane->state->fb) {
77562676d10SGerd Hoffmann 			obj = to_qxl_framebuffer(plane->state->fb)->obj;
77662676d10SGerd Hoffmann 			old_bo = gem_to_qxl_bo(obj);
77762676d10SGerd Hoffmann 		}
77862676d10SGerd Hoffmann 		if (old_bo && old_bo->shadow &&
77962676d10SGerd Hoffmann 		    user_bo->gem_base.size == old_bo->gem_base.size &&
78062676d10SGerd Hoffmann 		    plane->state->crtc     == new_state->crtc &&
78162676d10SGerd Hoffmann 		    plane->state->crtc_w   == new_state->crtc_w &&
78262676d10SGerd Hoffmann 		    plane->state->crtc_h   == new_state->crtc_h &&
78362676d10SGerd Hoffmann 		    plane->state->src_x    == new_state->src_x &&
78462676d10SGerd Hoffmann 		    plane->state->src_y    == new_state->src_y &&
78562676d10SGerd Hoffmann 		    plane->state->src_w    == new_state->src_w &&
78662676d10SGerd Hoffmann 		    plane->state->src_h    == new_state->src_h &&
78762676d10SGerd Hoffmann 		    plane->state->rotation == new_state->rotation &&
78862676d10SGerd Hoffmann 		    plane->state->zpos     == new_state->zpos) {
78962676d10SGerd Hoffmann 			drm_gem_object_get(&old_bo->shadow->gem_base);
79062676d10SGerd Hoffmann 			user_bo->shadow = old_bo->shadow;
79162676d10SGerd Hoffmann 		} else {
79262676d10SGerd Hoffmann 			qxl_bo_create(qdev, user_bo->gem_base.size,
79362676d10SGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_VRAM, NULL,
79462676d10SGerd Hoffmann 				      &user_bo->shadow);
79562676d10SGerd Hoffmann 		}
79662676d10SGerd Hoffmann 	}
79762676d10SGerd Hoffmann 
7981277eed5SGabriel Krisman Bertazi 	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
7991277eed5SGabriel Krisman Bertazi 	if (ret)
8001277eed5SGabriel Krisman Bertazi 		return ret;
8011277eed5SGabriel Krisman Bertazi 
8021277eed5SGabriel Krisman Bertazi 	return 0;
8031277eed5SGabriel Krisman Bertazi }
8041277eed5SGabriel Krisman Bertazi 
8051277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8061277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
8071277eed5SGabriel Krisman Bertazi {
8081277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
8091277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
8101277eed5SGabriel Krisman Bertazi 
8115f3d862aSGerd Hoffmann 	if (!old_state->fb) {
8125f3d862aSGerd Hoffmann 		/*
8135f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
8141277eed5SGabriel Krisman Bertazi 		 * unpin.
8151277eed5SGabriel Krisman Bertazi 		 */
8161277eed5SGabriel Krisman Bertazi 		return;
8171277eed5SGabriel Krisman Bertazi 	}
8181277eed5SGabriel Krisman Bertazi 
8195f3d862aSGerd Hoffmann 	obj = to_qxl_framebuffer(old_state->fb)->obj;
8201277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8211277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
82262676d10SGerd Hoffmann 
82362676d10SGerd Hoffmann 	if (user_bo->shadow && !user_bo->is_primary) {
82462676d10SGerd Hoffmann 		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
82562676d10SGerd Hoffmann 		user_bo->shadow = NULL;
82662676d10SGerd Hoffmann 	}
8271277eed5SGabriel Krisman Bertazi }
8281277eed5SGabriel Krisman Bertazi 
8291277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8301277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
8311277eed5SGabriel Krisman Bertazi };
8321277eed5SGabriel Krisman Bertazi 
8331277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8341277eed5SGabriel Krisman Bertazi 	.atomic_check = qxl_plane_atomic_check,
8351277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
8361277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
8371277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
8381277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
8391277eed5SGabriel Krisman Bertazi };
8401277eed5SGabriel Krisman Bertazi 
8411277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
842472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
843472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
8441277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8459ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8469ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8479ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
8481277eed5SGabriel Krisman Bertazi };
8491277eed5SGabriel Krisman Bertazi 
850d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
851d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
852d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
853d3e7e42dSGabriel Krisman Bertazi };
854d3e7e42dSGabriel Krisman Bertazi 
855c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
856c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
857c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
858c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
859c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
860c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
861c2ff6632SGabriel Krisman Bertazi };
862c2ff6632SGabriel Krisman Bertazi 
863d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
864472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
865472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
866d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8679ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8689ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8699ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
870d3e7e42dSGabriel Krisman Bertazi };
871d3e7e42dSGabriel Krisman Bertazi 
872d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
873d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
874d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
875d3e7e42dSGabriel Krisman Bertazi {
876d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
877d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
878d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
879d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
880d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
881d3e7e42dSGabriel Krisman Bertazi 	int err;
882d3e7e42dSGabriel Krisman Bertazi 
883d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
884d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
885d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
886d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
887c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
8881277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
8891277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
8901277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
8911277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
8921277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
893d3e7e42dSGabriel Krisman Bertazi 	} else {
894d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
895d3e7e42dSGabriel Krisman Bertazi 	}
896d3e7e42dSGabriel Krisman Bertazi 
897d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
898d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
899d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
900d3e7e42dSGabriel Krisman Bertazi 
901d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
902d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
903e6fc3b68SBen Widawsky 				       NULL, type, NULL);
904d3e7e42dSGabriel Krisman Bertazi 	if (err)
905d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
906d3e7e42dSGabriel Krisman Bertazi 
907d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
908d3e7e42dSGabriel Krisman Bertazi 
909d3e7e42dSGabriel Krisman Bertazi 	return plane;
910d3e7e42dSGabriel Krisman Bertazi 
911d3e7e42dSGabriel Krisman Bertazi free_plane:
912d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
913d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
914d3e7e42dSGabriel Krisman Bertazi }
915d3e7e42dSGabriel Krisman Bertazi 
91607f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
917f64122c1SDave Airlie {
918f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
9191277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
920d3e7e42dSGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
921d3e7e42dSGabriel Krisman Bertazi 	int r;
922f64122c1SDave Airlie 
923f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
924f64122c1SDave Airlie 	if (!qxl_crtc)
925f64122c1SDave Airlie 		return -ENOMEM;
926f64122c1SDave Airlie 
927d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
928d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
929d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
930d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
931d3e7e42dSGabriel Krisman Bertazi 	}
932d3e7e42dSGabriel Krisman Bertazi 
9331277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9341277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
9351277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
9361277eed5SGabriel Krisman Bertazi 		goto clean_primary;
9371277eed5SGabriel Krisman Bertazi 	}
9381277eed5SGabriel Krisman Bertazi 
9391277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
940d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
941d3e7e42dSGabriel Krisman Bertazi 	if (r)
9421277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
943d3e7e42dSGabriel Krisman Bertazi 
94407f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
945f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
946f64122c1SDave Airlie 	return 0;
947d3e7e42dSGabriel Krisman Bertazi 
9481277eed5SGabriel Krisman Bertazi clean_cursor:
9491277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
9501277eed5SGabriel Krisman Bertazi 	kfree(cursor);
951d3e7e42dSGabriel Krisman Bertazi clean_primary:
952d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
953d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
954d3e7e42dSGabriel Krisman Bertazi free_mem:
955d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
956d3e7e42dSGabriel Krisman Bertazi 	return r;
957f64122c1SDave Airlie }
958f64122c1SDave Airlie 
959f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
960f64122c1SDave Airlie {
961f64122c1SDave Airlie 	DRM_DEBUG("\n");
962f64122c1SDave Airlie }
963f64122c1SDave Airlie 
964f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder)
965f64122c1SDave Airlie {
966f64122c1SDave Airlie 	DRM_DEBUG("\n");
967f64122c1SDave Airlie }
968f64122c1SDave Airlie 
969f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder)
970f64122c1SDave Airlie {
971f64122c1SDave Airlie 	DRM_DEBUG("\n");
972f64122c1SDave Airlie }
973f64122c1SDave Airlie 
974f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder,
975f64122c1SDave Airlie 				struct drm_display_mode *mode,
976f64122c1SDave Airlie 				struct drm_display_mode *adjusted_mode)
977f64122c1SDave Airlie {
978f64122c1SDave Airlie 	DRM_DEBUG("\n");
979f64122c1SDave Airlie }
980f64122c1SDave Airlie 
981f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
982f64122c1SDave Airlie {
983b0807423SMarc-André Lureau 	unsigned pwidth = 1024;
984b0807423SMarc-André Lureau 	unsigned pheight = 768;
9852d856f94SGerd Hoffmann 	int ret = 0;
986f64122c1SDave Airlie 
987b0807423SMarc-André Lureau 	ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
988f64122c1SDave Airlie 	if (ret < 0)
989f64122c1SDave Airlie 		return ret;
990b0807423SMarc-André Lureau 	ret += qxl_add_common_modes(connector, pwidth, pheight);
991f64122c1SDave Airlie 	return ret;
992f64122c1SDave Airlie }
993f64122c1SDave Airlie 
994e0d92e16SLuc Van Oostenryck static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
995f64122c1SDave Airlie 			       struct drm_display_mode *mode)
996f64122c1SDave Airlie {
997bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
998bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
999bd3e1c7cSJonathon Jongsma 	int i;
1000bd3e1c7cSJonathon Jongsma 
1001f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
1002f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
1003bd3e1c7cSJonathon Jongsma 
1004bd3e1c7cSJonathon Jongsma 	if(qdev->monitors_config_width == mode->hdisplay &&
1005bd3e1c7cSJonathon Jongsma 	   qdev->monitors_config_height == mode->vdisplay)
1006f64122c1SDave Airlie 		return MODE_OK;
1007bd3e1c7cSJonathon Jongsma 
1008bd3e1c7cSJonathon Jongsma 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
1009bd3e1c7cSJonathon Jongsma 		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
1010bd3e1c7cSJonathon Jongsma 			return MODE_OK;
1011bd3e1c7cSJonathon Jongsma 	}
1012bd3e1c7cSJonathon Jongsma 	return MODE_BAD;
1013f64122c1SDave Airlie }
1014f64122c1SDave Airlie 
10156d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1016f64122c1SDave Airlie {
1017f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1018f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1019f64122c1SDave Airlie 
1020f64122c1SDave Airlie 	DRM_DEBUG("\n");
1021f64122c1SDave Airlie 	return &qxl_output->enc;
1022f64122c1SDave Airlie }
1023f64122c1SDave Airlie 
1024f64122c1SDave Airlie 
1025f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1026f64122c1SDave Airlie 	.dpms = qxl_enc_dpms,
1027f64122c1SDave Airlie 	.prepare = qxl_enc_prepare,
1028f64122c1SDave Airlie 	.mode_set = qxl_enc_mode_set,
1029f64122c1SDave Airlie 	.commit = qxl_enc_commit,
1030f64122c1SDave Airlie };
1031f64122c1SDave Airlie 
1032f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1033f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1034f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1035f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1036f64122c1SDave Airlie };
1037f64122c1SDave Airlie 
1038f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1039f64122c1SDave Airlie 			struct drm_connector *connector,
1040f64122c1SDave Airlie 			bool force)
1041f64122c1SDave Airlie {
1042f64122c1SDave Airlie 	struct qxl_output *output =
1043f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1044f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1045f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
104669e5d3f8SDave Airlie 	bool connected = false;
1047f64122c1SDave Airlie 
1048f64122c1SDave Airlie 	/* The first monitor is always connected */
104969e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
105069e5d3f8SDave Airlie 		if (output->index == 0)
105169e5d3f8SDave Airlie 			connected = true;
105269e5d3f8SDave Airlie 	} else
105369e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
105469e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1055f64122c1SDave Airlie 
10565cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10575cab51cbSMarc-André Lureau 
1058f64122c1SDave Airlie 	return connected ? connector_status_connected
1059f64122c1SDave Airlie 			 : connector_status_disconnected;
1060f64122c1SDave Airlie }
1061f64122c1SDave Airlie 
1062f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector,
1063f64122c1SDave Airlie 				   struct drm_property *property,
1064f64122c1SDave Airlie 				   uint64_t value)
1065f64122c1SDave Airlie {
1066f64122c1SDave Airlie 	DRM_DEBUG("\n");
1067f64122c1SDave Airlie 	return 0;
1068f64122c1SDave Airlie }
1069f64122c1SDave Airlie 
1070f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1071f64122c1SDave Airlie {
1072f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1073f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1074f64122c1SDave Airlie 
107534ea3d38SThomas Wood 	drm_connector_unregister(connector);
1076f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1077f64122c1SDave Airlie 	kfree(qxl_output);
1078f64122c1SDave Airlie }
1079f64122c1SDave Airlie 
1080f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1081f64122c1SDave Airlie 	.dpms = drm_helper_connector_dpms,
1082f64122c1SDave Airlie 	.detect = qxl_conn_detect,
10836af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1084f64122c1SDave Airlie 	.set_property = qxl_conn_set_property,
1085f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
10869ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
10879ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10889ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1089f64122c1SDave Airlie };
1090f64122c1SDave Airlie 
1091f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
1092f64122c1SDave Airlie {
1093f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
1094f64122c1SDave Airlie }
1095f64122c1SDave Airlie 
1096f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
1097f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
1098f64122c1SDave Airlie };
1099f64122c1SDave Airlie 
11004695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
11014695b039SDave Airlie {
11024695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
11034695b039SDave Airlie 		return 0;
11044695b039SDave Airlie 
11054695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1106cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
11074695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
11084695b039SDave Airlie 
11094695b039SDave Airlie 	return 0;
11104695b039SDave Airlie }
11114695b039SDave Airlie 
11126d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1113f64122c1SDave Airlie {
11144695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
1115f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1116f64122c1SDave Airlie 	struct drm_connector *connector;
1117f64122c1SDave Airlie 	struct drm_encoder *encoder;
1118f64122c1SDave Airlie 
1119f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1120f64122c1SDave Airlie 	if (!qxl_output)
1121f64122c1SDave Airlie 		return -ENOMEM;
1122f64122c1SDave Airlie 
1123f64122c1SDave Airlie 	qxl_output->index = num_output;
1124f64122c1SDave Airlie 
1125f64122c1SDave Airlie 	connector = &qxl_output->base;
1126f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1127f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1128f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1129f64122c1SDave Airlie 
1130f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
113113a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
1132f64122c1SDave Airlie 
11335ff91e44SDave Airlie 	/* we get HPD via client monitors config */
11345ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1135f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1136f64122c1SDave Airlie 	drm_mode_connector_attach_encoder(&qxl_output->base,
1137f64122c1SDave Airlie 					  &qxl_output->enc);
1138f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1139f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1140f64122c1SDave Airlie 
11414695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11424695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11437dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11447dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11457dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11467dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1147f64122c1SDave Airlie 	return 0;
1148f64122c1SDave Airlie }
1149f64122c1SDave Airlie 
1150f64122c1SDave Airlie static struct drm_framebuffer *
1151f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1152f64122c1SDave Airlie 			    struct drm_file *file_priv,
11531eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1154f64122c1SDave Airlie {
1155f64122c1SDave Airlie 	struct drm_gem_object *obj;
1156f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb;
1157f64122c1SDave Airlie 	int ret;
1158f64122c1SDave Airlie 
1159a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1160a8ad0bd8SChris Wilson 	if (!obj)
1161a8ad0bd8SChris Wilson 		return NULL;
1162f64122c1SDave Airlie 
1163f64122c1SDave Airlie 	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1164f64122c1SDave Airlie 	if (qxl_fb == NULL)
1165f64122c1SDave Airlie 		return NULL;
1166f64122c1SDave Airlie 
11676819c3c2SNoralf Trønnes 	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1168f64122c1SDave Airlie 	if (ret) {
1169f64122c1SDave Airlie 		kfree(qxl_fb);
11702793c1d7SSantha Meena Ramamoorthy 		drm_gem_object_put_unlocked(obj);
1171f64122c1SDave Airlie 		return NULL;
1172f64122c1SDave Airlie 	}
1173f64122c1SDave Airlie 
1174f64122c1SDave Airlie 	return &qxl_fb->base;
1175f64122c1SDave Airlie }
1176f64122c1SDave Airlie 
1177f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1178f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1179472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1180472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1181f64122c1SDave Airlie };
1182f64122c1SDave Airlie 
11832bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1184f64122c1SDave Airlie {
1185f64122c1SDave Airlie 	int ret;
1186f64122c1SDave Airlie 	struct drm_gem_object *gobj;
118707f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
1188f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1189f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
1190f64122c1SDave Airlie 
1191f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1192f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1193f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1194f64122c1SDave Airlie 	if (ret) {
1195f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1196f64122c1SDave Airlie 		return -ENOMEM;
1197f64122c1SDave Airlie 	}
1198f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
11992bd6ce84SDave Airlie 
1200715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
12012bd6ce84SDave Airlie 	if (ret)
12022bd6ce84SDave Airlie 		return ret;
12032bd6ce84SDave Airlie 
1204f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
12052bd6ce84SDave Airlie 
1206f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1207f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1208f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1209f64122c1SDave Airlie 
1210f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
1211f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
12122bd6ce84SDave Airlie 	return 0;
12132bd6ce84SDave Airlie }
12142bd6ce84SDave Airlie 
12152bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
12162bd6ce84SDave Airlie {
12172bd6ce84SDave Airlie 	int ret;
12182bd6ce84SDave Airlie 
12192bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
12202bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
12212bd6ce84SDave Airlie 
12222bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
1223715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_unpin(qdev->monitors_config_bo);
12242bd6ce84SDave Airlie 	if (ret)
12252bd6ce84SDave Airlie 		return ret;
12262bd6ce84SDave Airlie 
12272bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
12282bd6ce84SDave Airlie 	return 0;
12292bd6ce84SDave Airlie }
12302bd6ce84SDave Airlie 
12312bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
12322bd6ce84SDave Airlie {
12332bd6ce84SDave Airlie 	int i;
12342bd6ce84SDave Airlie 	int ret;
12352bd6ce84SDave Airlie 
1236cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_init(&qdev->ddev);
12372bd6ce84SDave Airlie 
12382bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
12392bd6ce84SDave Airlie 	if (ret)
12402bd6ce84SDave Airlie 		return ret;
1241f64122c1SDave Airlie 
1242cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1243f64122c1SDave Airlie 
1244f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
12451277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_width = 0;
12461277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_height = 0;
1247cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_width = 8192;
1248cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_height = 8192;
1249f64122c1SDave Airlie 
1250cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.fb_base = qdev->vram_base;
12514695b039SDave Airlie 
1252cbdded7fSGabriel Krisman Bertazi 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
12534695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
12544695b039SDave Airlie 
125507f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1256cbdded7fSGabriel Krisman Bertazi 		qdev_crtc_init(&qdev->ddev, i);
1257cbdded7fSGabriel Krisman Bertazi 		qdev_output_init(&qdev->ddev, i);
1258f64122c1SDave Airlie 	}
1259f64122c1SDave Airlie 
1260c50fad8fSGerd Hoffmann 	qxl_display_read_client_monitors_config(qdev);
1261f64122c1SDave Airlie 	qdev->mode_info.mode_config_initialized = true;
1262f64122c1SDave Airlie 
12639ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
12649ade8b98SGabriel Krisman Bertazi 
1265f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
1266f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
1267f64122c1SDave Airlie 	 * spice server. */
1268f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
1269f64122c1SDave Airlie 	return 0;
1270f64122c1SDave Airlie }
1271f64122c1SDave Airlie 
1272f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1273f64122c1SDave Airlie {
1274f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
12752bd6ce84SDave Airlie 
12762bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1277f64122c1SDave Airlie 	if (qdev->mode_info.mode_config_initialized) {
1278cbdded7fSGabriel Krisman Bertazi 		drm_mode_config_cleanup(&qdev->ddev);
1279f64122c1SDave Airlie 		qdev->mode_info.mode_config_initialized = false;
1280f64122c1SDave Airlie 	}
1281f64122c1SDave Airlie }
1282