xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision 90adda2ce898c7a76d1a55d4650ade8cc957e54f)
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>
2710a0bd89SGabriel Krisman Bertazi #include <drm/drm_atomic.h>
28fcd70cd3SDaniel Vetter #include <drm/drm_atomic_helper.h>
29bf8744e4SPeter Wu #include <drm/drm_gem_framebuffer_helper.h>
30fcd70cd3SDaniel Vetter #include <drm/drm_plane_helper.h>
31fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
32f64122c1SDave Airlie 
33edaf492cSMasahiro Yamada #include "qxl_drv.h"
34edaf492cSMasahiro Yamada #include "qxl_object.h"
35edaf492cSMasahiro Yamada 
3607f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
3707f8d9bdSDave Airlie {
3807f8d9bdSDave Airlie 	return head->width && head->height;
3907f8d9bdSDave Airlie }
4007f8d9bdSDave Airlie 
4166e0c8a5SAnton Vasilyev static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
4266e0c8a5SAnton Vasilyev 		unsigned int count)
43f64122c1SDave Airlie {
44f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
45f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
46f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
4762c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
48f64122c1SDave Airlie 	}
49f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
50f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
51d4b9dd50SGustavo A. R. Silva 				struct_size(qdev->client_monitors_config,
52d4b9dd50SGustavo A. R. Silva 				heads, count), GFP_KERNEL);
53735581a0SGerd Hoffmann 		if (!qdev->client_monitors_config)
5466e0c8a5SAnton Vasilyev 			return -ENOMEM;
55f64122c1SDave Airlie 	}
56f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
5766e0c8a5SAnton Vasilyev 	return 0;
58f64122c1SDave Airlie }
59f64122c1SDave Airlie 
609e3b3178SChristophe Fergeau enum {
619e3b3178SChristophe Fergeau 	MONITORS_CONFIG_MODIFIED,
629e3b3178SChristophe Fergeau 	MONITORS_CONFIG_UNCHANGED,
639e3b3178SChristophe Fergeau 	MONITORS_CONFIG_BAD_CRC,
6466e0c8a5SAnton Vasilyev 	MONITORS_CONFIG_ERROR,
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));
77735581a0SGerd Hoffmann 	if (crc != qdev->rom->client_monitors_config_crc)
789e3b3178SChristophe Fergeau 		return MONITORS_CONFIG_BAD_CRC;
79c50fad8fSGerd Hoffmann 	if (!num_monitors) {
80c50fad8fSGerd Hoffmann 		DRM_DEBUG_KMS("no client monitors configured\n");
81c50fad8fSGerd Hoffmann 		return status;
82c50fad8fSGerd Hoffmann 	}
83f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
845b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
85f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
86f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
87f64122c1SDave Airlie 	} else {
88f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
89f64122c1SDave Airlie 	}
909e3b3178SChristophe Fergeau 	if (qdev->client_monitors_config
919e3b3178SChristophe Fergeau 	      && (num_monitors != qdev->client_monitors_config->count)) {
929e3b3178SChristophe Fergeau 		status = MONITORS_CONFIG_MODIFIED;
939e3b3178SChristophe Fergeau 	}
9466e0c8a5SAnton Vasilyev 	if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
9566e0c8a5SAnton Vasilyev 		status = MONITORS_CONFIG_ERROR;
9666e0c8a5SAnton Vasilyev 		return status;
9766e0c8a5SAnton Vasilyev 	}
98f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
99f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
100f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
101f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
102f64122c1SDave Airlie 		struct qxl_urect *c_rect =
103f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
104f64122c1SDave Airlie 		struct qxl_head *client_head =
105f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
1069e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
10707f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1089e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1099e3b3178SChristophe Fergeau 		}
1109e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
11107f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1129e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1139e3b3178SChristophe Fergeau 		}
1149e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
11507f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1169e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1179e3b3178SChristophe Fergeau 		}
1189e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
11907f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1209e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1219e3b3178SChristophe Fergeau 		}
1229e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
12307f8d9bdSDave Airlie 			client_head->surface_id = 0;
1249e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1259e3b3178SChristophe Fergeau 		}
1269e3b3178SChristophe Fergeau 		if (client_head->id != i) {
12707f8d9bdSDave Airlie 			client_head->id = i;
1289e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1299e3b3178SChristophe Fergeau 		}
1309e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
13107f8d9bdSDave Airlie 			client_head->flags = 0;
1329e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1339e3b3178SChristophe Fergeau 		}
13407f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
13507f8d9bdSDave Airlie 			  client_head->x, client_head->y);
136f64122c1SDave Airlie 	}
1379e3b3178SChristophe Fergeau 
1389e3b3178SChristophe Fergeau 	return status;
139f64122c1SDave Airlie }
140f64122c1SDave Airlie 
1417dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1427dea0941SDave Airlie {
143cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1447dea0941SDave Airlie 	struct drm_connector *connector;
1457dea0941SDave Airlie 	struct qxl_output *output;
1467dea0941SDave Airlie 	struct qxl_head *head;
1477dea0941SDave Airlie 
1487dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1497dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1507dea0941SDave Airlie 
1517dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1527dea0941SDave Airlie 
1537dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1547dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1557dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1567dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1577dea0941SDave Airlie 	}
1587dea0941SDave Airlie }
1597dea0941SDave Airlie 
160f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
161f64122c1SDave Airlie {
162cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1639062155dSGerd Hoffmann 	int status, retries;
1649e3b3178SChristophe Fergeau 
1659062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1669e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1679062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1689062155dSGerd Hoffmann 			break;
1699062155dSGerd Hoffmann 		udelay(5);
1709062155dSGerd Hoffmann 	}
17166e0c8a5SAnton Vasilyev 	if (status == MONITORS_CONFIG_ERROR) {
17266e0c8a5SAnton Vasilyev 		DRM_DEBUG_KMS("ignoring client monitors config: error");
17366e0c8a5SAnton Vasilyev 		return;
17466e0c8a5SAnton Vasilyev 	}
1759062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1769062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1779062155dSGerd Hoffmann 		return;
1789e3b3178SChristophe Fergeau 	}
1799e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1809062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1819e3b3178SChristophe Fergeau 		return;
182f64122c1SDave Airlie 	}
1834fdb0869SMarc-André Lureau 
1847dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1857dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1867dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
187cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1884fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1894fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
190cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1914fdb0869SMarc-André Lureau 	}
192f64122c1SDave Airlie }
193f64122c1SDave Airlie 
194b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector,
195b0807423SMarc-André Lureau                                          unsigned *pwidth,
196b0807423SMarc-André Lureau                                          unsigned *pheight)
197f64122c1SDave Airlie {
198f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
199f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
200f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
201f64122c1SDave Airlie 	int h = output->index;
202f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
203f64122c1SDave Airlie 	struct qxl_head *head;
204f64122c1SDave Airlie 
2052d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
2062d856f94SGerd Hoffmann 		return 0;
2072d856f94SGerd Hoffmann 	if (h >= qdev->monitors_config->max_allowed)
2082d856f94SGerd Hoffmann 		return 0;
20907f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
210f64122c1SDave Airlie 		return 0;
2112d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2122d856f94SGerd Hoffmann 		return 0;
2132d856f94SGerd Hoffmann 
21407f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2152d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
216f64122c1SDave Airlie 
217f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
218f64122c1SDave Airlie 			    false);
219f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
220ff996e72SChristophe Fergeau 	mode->hdisplay = head->width;
221ff996e72SChristophe Fergeau 	mode->vdisplay = head->height;
222ff996e72SChristophe Fergeau 	drm_mode_set_name(mode);
223b0807423SMarc-André Lureau 	*pwidth = head->width;
224b0807423SMarc-André Lureau 	*pheight = head->height;
225f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
226bd3e1c7cSJonathon Jongsma 	/* remember the last custom size for mode validation */
227bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_width = mode->hdisplay;
228bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_height = mode->vdisplay;
229f64122c1SDave Airlie 	return 1;
230f64122c1SDave Airlie }
231f64122c1SDave Airlie 
232bd3e1c7cSJonathon Jongsma static struct mode_size {
233f64122c1SDave Airlie 	int w;
234f64122c1SDave Airlie 	int h;
235f64122c1SDave Airlie } common_modes[] = {
236f64122c1SDave Airlie 	{ 640,  480},
237f64122c1SDave Airlie 	{ 720,  480},
238f64122c1SDave Airlie 	{ 800,  600},
239f64122c1SDave Airlie 	{ 848,  480},
240f64122c1SDave Airlie 	{1024,  768},
241f64122c1SDave Airlie 	{1152,  768},
242f64122c1SDave Airlie 	{1280,  720},
243f64122c1SDave Airlie 	{1280,  800},
244f64122c1SDave Airlie 	{1280,  854},
245f64122c1SDave Airlie 	{1280,  960},
246f64122c1SDave Airlie 	{1280, 1024},
247f64122c1SDave Airlie 	{1440,  900},
248f64122c1SDave Airlie 	{1400, 1050},
249f64122c1SDave Airlie 	{1680, 1050},
250f64122c1SDave Airlie 	{1600, 1200},
251f64122c1SDave Airlie 	{1920, 1080},
252f64122c1SDave Airlie 	{1920, 1200}
253f64122c1SDave Airlie };
254f64122c1SDave Airlie 
255bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector,
2561b000494SShayenne da Luz Moura                                 unsigned int pwidth,
2571b000494SShayenne da Luz Moura                                 unsigned int pheight)
258bd3e1c7cSJonathon Jongsma {
259bd3e1c7cSJonathon Jongsma 	struct drm_device *dev = connector->dev;
260bd3e1c7cSJonathon Jongsma 	struct drm_display_mode *mode = NULL;
261bd3e1c7cSJonathon Jongsma 	int i;
262408799ebSShayenne da Luz Moura 
263f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
264f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
265f64122c1SDave Airlie 				    60, false, false, false);
266b0807423SMarc-André Lureau 		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
267f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
268f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
269f64122c1SDave Airlie 	}
270f64122c1SDave Airlie 	return i - 1;
271f64122c1SDave Airlie }
272f64122c1SDave Airlie 
273998010bfSGerd Hoffmann static void qxl_send_monitors_config(struct qxl_device *qdev)
274998010bfSGerd Hoffmann {
275998010bfSGerd Hoffmann 	int i;
276998010bfSGerd Hoffmann 
277998010bfSGerd Hoffmann 	BUG_ON(!qdev->ram_header->monitors_config);
278998010bfSGerd Hoffmann 
279998010bfSGerd Hoffmann 	if (qdev->monitors_config->count == 0)
280998010bfSGerd Hoffmann 		return;
281998010bfSGerd Hoffmann 
282998010bfSGerd Hoffmann 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
283998010bfSGerd Hoffmann 		struct qxl_head *head = &qdev->monitors_config->heads[i];
284998010bfSGerd Hoffmann 
285998010bfSGerd Hoffmann 		if (head->y > 8192 || head->x > 8192 ||
286998010bfSGerd Hoffmann 		    head->width > 8192 || head->height > 8192) {
287998010bfSGerd Hoffmann 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
288998010bfSGerd Hoffmann 				  i, head->width, head->height,
289998010bfSGerd Hoffmann 				  head->x, head->y);
290998010bfSGerd Hoffmann 			return;
291998010bfSGerd Hoffmann 		}
292998010bfSGerd Hoffmann 	}
293998010bfSGerd Hoffmann 	qxl_io_monitors_config(qdev);
294998010bfSGerd Hoffmann }
295998010bfSGerd Hoffmann 
296a6d3c4d7SGerd Hoffmann static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
297a6d3c4d7SGerd Hoffmann 					    const char *reason)
298a6d3c4d7SGerd Hoffmann {
299a6d3c4d7SGerd Hoffmann 	struct drm_device *dev = crtc->dev;
300a6d3c4d7SGerd Hoffmann 	struct qxl_device *qdev = dev->dev_private;
301a6d3c4d7SGerd Hoffmann 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
302a6d3c4d7SGerd Hoffmann 	struct qxl_head head;
303a6d3c4d7SGerd Hoffmann 	int oldcount, i = qcrtc->index;
304a6d3c4d7SGerd Hoffmann 
30516620544SGerd Hoffmann 	if (!qdev->primary_bo) {
306a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
307a6d3c4d7SGerd Hoffmann 		return;
308a6d3c4d7SGerd Hoffmann 	}
309a6d3c4d7SGerd Hoffmann 
310a6d3c4d7SGerd Hoffmann 	if (!qdev->monitors_config ||
311a6d3c4d7SGerd Hoffmann 	    qdev->monitors_config->max_allowed <= i)
312a6d3c4d7SGerd Hoffmann 		return;
313a6d3c4d7SGerd Hoffmann 
314a6d3c4d7SGerd Hoffmann 	head.id = i;
315a6d3c4d7SGerd Hoffmann 	head.flags = 0;
316a6d3c4d7SGerd Hoffmann 	oldcount = qdev->monitors_config->count;
317a6d3c4d7SGerd Hoffmann 	if (crtc->state->active) {
318a6d3c4d7SGerd Hoffmann 		struct drm_display_mode *mode = &crtc->mode;
319408799ebSShayenne da Luz Moura 
320a6d3c4d7SGerd Hoffmann 		head.width = mode->hdisplay;
321a6d3c4d7SGerd Hoffmann 		head.height = mode->vdisplay;
322a6d3c4d7SGerd Hoffmann 		head.x = crtc->x;
323a6d3c4d7SGerd Hoffmann 		head.y = crtc->y;
324a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count < i + 1)
325a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i + 1;
326*90adda2cSGerd Hoffmann 		if (qdev->primary_bo == qdev->dumb_shadow_bo)
327*90adda2cSGerd Hoffmann 			head.x += qdev->dumb_heads[i].x;
328a6d3c4d7SGerd Hoffmann 	} else if (i > 0) {
329a6d3c4d7SGerd Hoffmann 		head.width = 0;
330a6d3c4d7SGerd Hoffmann 		head.height = 0;
331a6d3c4d7SGerd Hoffmann 		head.x = 0;
332a6d3c4d7SGerd Hoffmann 		head.y = 0;
333a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count == i + 1)
334a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i;
335a6d3c4d7SGerd Hoffmann 	} else {
336a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
337a6d3c4d7SGerd Hoffmann 		return;
338a6d3c4d7SGerd Hoffmann 	}
339a6d3c4d7SGerd Hoffmann 
340a6d3c4d7SGerd Hoffmann 	if (head.width  == qdev->monitors_config->heads[i].width  &&
341a6d3c4d7SGerd Hoffmann 	    head.height == qdev->monitors_config->heads[i].height &&
342a6d3c4d7SGerd Hoffmann 	    head.x      == qdev->monitors_config->heads[i].x      &&
343a6d3c4d7SGerd Hoffmann 	    head.y      == qdev->monitors_config->heads[i].y      &&
344a6d3c4d7SGerd Hoffmann 	    oldcount    == qdev->monitors_config->count)
345a6d3c4d7SGerd Hoffmann 		return;
346a6d3c4d7SGerd Hoffmann 
347a6d3c4d7SGerd Hoffmann 	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
348a6d3c4d7SGerd Hoffmann 		      i, head.width, head.height, head.x, head.y,
349a6d3c4d7SGerd Hoffmann 		      crtc->state->active ? "on" : "off", reason);
350a6d3c4d7SGerd Hoffmann 	if (oldcount != qdev->monitors_config->count)
351a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
352a6d3c4d7SGerd Hoffmann 			      oldcount, qdev->monitors_config->count,
353a6d3c4d7SGerd Hoffmann 			      qdev->monitors_config->max_allowed);
354a6d3c4d7SGerd Hoffmann 
355a6d3c4d7SGerd Hoffmann 	qdev->monitors_config->heads[i] = head;
356a6d3c4d7SGerd Hoffmann 	qxl_send_monitors_config(qdev);
357a6d3c4d7SGerd Hoffmann }
358a6d3c4d7SGerd Hoffmann 
359c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
360c2ff6632SGabriel Krisman Bertazi 				  struct drm_crtc_state *old_crtc_state)
361c2ff6632SGabriel Krisman Bertazi {
362c2ff6632SGabriel Krisman Bertazi 	struct drm_device *dev = crtc->dev;
363c2ff6632SGabriel Krisman Bertazi 	struct drm_pending_vblank_event *event;
364c2ff6632SGabriel Krisman Bertazi 	unsigned long flags;
365c2ff6632SGabriel Krisman Bertazi 
366c2ff6632SGabriel Krisman Bertazi 	if (crtc->state && crtc->state->event) {
367c2ff6632SGabriel Krisman Bertazi 		event = crtc->state->event;
368c2ff6632SGabriel Krisman Bertazi 		crtc->state->event = NULL;
369c2ff6632SGabriel Krisman Bertazi 
370c2ff6632SGabriel Krisman Bertazi 		spin_lock_irqsave(&dev->event_lock, flags);
371c2ff6632SGabriel Krisman Bertazi 		drm_crtc_send_vblank_event(crtc, event);
372c2ff6632SGabriel Krisman Bertazi 		spin_unlock_irqrestore(&dev->event_lock, flags);
373c2ff6632SGabriel Krisman Bertazi 	}
374a6d3c4d7SGerd Hoffmann 
375a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "flush");
376c2ff6632SGabriel Krisman Bertazi }
377c2ff6632SGabriel Krisman Bertazi 
378f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
379f64122c1SDave Airlie {
380f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
381f64122c1SDave Airlie 
3829428088cSRay Strode 	qxl_bo_unref(&qxl_crtc->cursor_bo);
383f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
384f64122c1SDave Airlie 	kfree(qxl_crtc);
385f64122c1SDave Airlie }
386f64122c1SDave Airlie 
387f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
388bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
389f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
3909973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
3919ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
3929ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
3939ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
394f64122c1SDave Airlie };
395f64122c1SDave Airlie 
3966d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
397f64122c1SDave Airlie 					 struct drm_file *file_priv,
3981b000494SShayenne da Luz Moura 					 unsigned int flags, unsigned int color,
399f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
4001b000494SShayenne da Luz Moura 					 unsigned int num_clips)
401f64122c1SDave Airlie {
402f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
403bf8744e4SPeter Wu 	struct qxl_device *qdev = fb->dev->dev_private;
404f64122c1SDave Airlie 	struct drm_clip_rect norect;
405f64122c1SDave Airlie 	struct qxl_bo *qobj;
4064979904cSGerd Hoffmann 	bool is_primary;
407f64122c1SDave Airlie 	int inc = 1;
408f64122c1SDave Airlie 
40973e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
41073e9efd4SVille Syrjälä 
411bf8744e4SPeter Wu 	qobj = gem_to_qxl_bo(fb->obj[0]);
412b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
4134979904cSGerd Hoffmann 	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
4144979904cSGerd Hoffmann 	if (!is_primary) {
41573e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
416b2b4465dSDave Airlie 		return 0;
41773e9efd4SVille Syrjälä 	}
418b2b4465dSDave Airlie 
419f64122c1SDave Airlie 	if (!num_clips) {
420f64122c1SDave Airlie 		num_clips = 1;
421f64122c1SDave Airlie 		clips = &norect;
422f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
423f64122c1SDave Airlie 		norect.x2 = fb->width;
424f64122c1SDave Airlie 		norect.y2 = fb->height;
425f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
426f64122c1SDave Airlie 		num_clips /= 2;
427f64122c1SDave Airlie 		inc = 2; /* skip source rects */
428f64122c1SDave Airlie 	}
429f64122c1SDave Airlie 
430bf8744e4SPeter Wu 	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
431*90adda2cSGerd Hoffmann 			  clips, num_clips, inc, 0);
43273e9efd4SVille Syrjälä 
43373e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
43473e9efd4SVille Syrjälä 
435f64122c1SDave Airlie 	return 0;
436f64122c1SDave Airlie }
437f64122c1SDave Airlie 
438f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
439bf8744e4SPeter Wu 	.destroy = drm_gem_fb_destroy,
440f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
441bf8744e4SPeter Wu 	.create_handle = drm_gem_fb_create_handle,
442f64122c1SDave Airlie };
443f64122c1SDave Airlie 
4440b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
4450b20a0f8SLaurent Pinchart 				   struct drm_crtc_state *old_state)
446f64122c1SDave Airlie {
447a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "enable");
448f64122c1SDave Airlie }
449f64122c1SDave Airlie 
45064581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
45164581714SLaurent Pinchart 				    struct drm_crtc_state *old_state)
45207f8d9bdSDave Airlie {
453a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "disable");
45407f8d9bdSDave Airlie }
45507f8d9bdSDave Airlie 
456f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
457c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4580b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
45964581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
460f64122c1SDave Airlie };
461f64122c1SDave Airlie 
46245dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
463c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
464c2ff6632SGabriel Krisman Bertazi {
465c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
466c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
467c2ff6632SGabriel Krisman Bertazi 
468c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
469c2ff6632SGabriel Krisman Bertazi 		return 0;
470c2ff6632SGabriel Krisman Bertazi 
471bf8744e4SPeter Wu 	bo = gem_to_qxl_bo(state->fb->obj[0]);
472c2ff6632SGabriel Krisman Bertazi 
473c2ff6632SGabriel Krisman Bertazi 	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
474c2ff6632SGabriel Krisman Bertazi 		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
475c2ff6632SGabriel Krisman Bertazi 		return -EINVAL;
476c2ff6632SGabriel Krisman Bertazi 	}
477c2ff6632SGabriel Krisman Bertazi 
478c2ff6632SGabriel Krisman Bertazi 	return 0;
479c2ff6632SGabriel Krisman Bertazi }
480c2ff6632SGabriel Krisman Bertazi 
4819428088cSRay Strode static int qxl_primary_apply_cursor(struct drm_plane *plane)
4829428088cSRay Strode {
4839428088cSRay Strode 	struct drm_device *dev = plane->dev;
4849428088cSRay Strode 	struct qxl_device *qdev = dev->dev_private;
4859428088cSRay Strode 	struct drm_framebuffer *fb = plane->state->fb;
4869428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
4879428088cSRay Strode 	struct qxl_cursor_cmd *cmd;
4889428088cSRay Strode 	struct qxl_release *release;
4899428088cSRay Strode 	int ret = 0;
4909428088cSRay Strode 
4919428088cSRay Strode 	if (!qcrtc->cursor_bo)
4929428088cSRay Strode 		return 0;
4939428088cSRay Strode 
4949428088cSRay Strode 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
4959428088cSRay Strode 					 QXL_RELEASE_CURSOR_CMD,
4969428088cSRay Strode 					 &release, NULL);
4979428088cSRay Strode 	if (ret)
4989428088cSRay Strode 		return ret;
4999428088cSRay Strode 
5009428088cSRay Strode 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
5019428088cSRay Strode 	if (ret)
5029428088cSRay Strode 		goto out_free_release;
5039428088cSRay Strode 
5049428088cSRay Strode 	ret = qxl_release_reserve_list(release, false);
5059428088cSRay Strode 	if (ret)
5069428088cSRay Strode 		goto out_free_release;
5079428088cSRay Strode 
5089428088cSRay Strode 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5099428088cSRay Strode 	cmd->type = QXL_CURSOR_SET;
5109428088cSRay Strode 	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
5119428088cSRay Strode 	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
5129428088cSRay Strode 
5139428088cSRay Strode 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5149428088cSRay Strode 
5159428088cSRay Strode 	cmd->u.set.visible = 1;
5169428088cSRay Strode 	qxl_release_unmap(qdev, release, &cmd->release_info);
5179428088cSRay Strode 
5189428088cSRay Strode 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5199428088cSRay Strode 	qxl_release_fence_buffer_objects(release);
5209428088cSRay Strode 
5219428088cSRay Strode 	return ret;
5229428088cSRay Strode 
5239428088cSRay Strode out_free_release:
5249428088cSRay Strode 	qxl_release_free(qdev, release);
5259428088cSRay Strode 	return ret;
5269428088cSRay Strode }
5279428088cSRay Strode 
528c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
529c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
530c2ff6632SGabriel Krisman Bertazi {
531c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
532bf8744e4SPeter Wu 	struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
5334979904cSGerd Hoffmann 	struct qxl_bo *bo_old, *primary;
534c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
535c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
536c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
537bf8744e4SPeter Wu 	    .x2 = plane->state->fb->width,
538bf8744e4SPeter Wu 	    .y2 = plane->state->fb->height
539c2ff6632SGabriel Krisman Bertazi 	};
540*90adda2cSGerd Hoffmann 	uint32_t dumb_shadow_offset = 0;
541c2ff6632SGabriel Krisman Bertazi 
542b0e07da3SGerd Hoffmann 	if (old_state->fb) {
543bf8744e4SPeter Wu 		bo_old = gem_to_qxl_bo(old_state->fb->obj[0]);
544b0e07da3SGerd Hoffmann 	} else {
545b0e07da3SGerd Hoffmann 		bo_old = NULL;
546b0e07da3SGerd Hoffmann 	}
547b0e07da3SGerd Hoffmann 
5484979904cSGerd Hoffmann 	primary = bo->shadow ? bo->shadow : bo;
549b0e07da3SGerd Hoffmann 
5504979904cSGerd Hoffmann 	if (!primary->is_primary) {
5514979904cSGerd Hoffmann 		if (qdev->primary_bo)
552b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
5534979904cSGerd Hoffmann 		qxl_io_create_primary(qdev, primary);
5541f85535cSGerd Hoffmann 		qxl_primary_apply_cursor(plane);
5551f85535cSGerd Hoffmann 	}
55662676d10SGerd Hoffmann 
557*90adda2cSGerd Hoffmann 	if (bo->is_dumb)
558*90adda2cSGerd Hoffmann 		dumb_shadow_offset =
559*90adda2cSGerd Hoffmann 			qdev->dumb_heads[plane->state->crtc->index].x;
560*90adda2cSGerd Hoffmann 
561*90adda2cSGerd Hoffmann 	qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
562*90adda2cSGerd Hoffmann 			  dumb_shadow_offset);
563c2ff6632SGabriel Krisman Bertazi }
564c2ff6632SGabriel Krisman Bertazi 
565c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
566c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
567c2ff6632SGabriel Krisman Bertazi {
568c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
569c2ff6632SGabriel Krisman Bertazi 
570b0e07da3SGerd Hoffmann 	if (old_state->fb) {
571bf8744e4SPeter Wu 		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
572c2ff6632SGabriel Krisman Bertazi 
573b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
574c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
575c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
576c2ff6632SGabriel Krisman Bertazi 		}
577c2ff6632SGabriel Krisman Bertazi 	}
578b0e07da3SGerd Hoffmann }
579c2ff6632SGabriel Krisman Bertazi 
5801277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
5811277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
5821277eed5SGabriel Krisman Bertazi {
5831277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
5841277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
5851277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
5869428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
5871277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
5881277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
5891277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
5901277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
591889ad63dSJeremy Cline 	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
5921277eed5SGabriel Krisman Bertazi 	int ret;
5931277eed5SGabriel Krisman Bertazi 	void *user_ptr;
5941277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
5951277eed5SGabriel Krisman Bertazi 
5961277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5971277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
5981277eed5SGabriel Krisman Bertazi 					 &release, NULL);
599ee5cb7c4SDan Carpenter 	if (ret)
600ee5cb7c4SDan Carpenter 		return;
6011277eed5SGabriel Krisman Bertazi 
6021277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
603bf8744e4SPeter Wu 		obj = fb->obj[0];
6041277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
6051277eed5SGabriel Krisman Bertazi 
6061277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
6071277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
6081277eed5SGabriel Krisman Bertazi 		if (ret)
6091277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6101277eed5SGabriel Krisman Bertazi 
6111277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
6121277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6131277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6141277eed5SGabriel Krisman Bertazi 		if (ret)
6151277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6161277eed5SGabriel Krisman Bertazi 
6170081cdfeSChristophe Fergeau 		ret = qxl_bo_pin(cursor_bo);
6181277eed5SGabriel Krisman Bertazi 		if (ret)
6191277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6201277eed5SGabriel Krisman Bertazi 
6210081cdfeSChristophe Fergeau 		ret = qxl_release_reserve_list(release, true);
6220081cdfeSChristophe Fergeau 		if (ret)
6230081cdfeSChristophe Fergeau 			goto out_unpin;
6240081cdfeSChristophe Fergeau 
6251277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6261277eed5SGabriel Krisman Bertazi 		if (ret)
6271277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6281277eed5SGabriel Krisman Bertazi 
6291277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6301277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6311277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6321277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6331277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6341277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6351277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6361277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6371277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6381277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6391277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6401277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6411277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6421277eed5SGabriel Krisman Bertazi 
643429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6441277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6451277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6461277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6471277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6489428088cSRay Strode 
649889ad63dSJeremy Cline 		old_cursor_bo = qcrtc->cursor_bo;
6509428088cSRay Strode 		qcrtc->cursor_bo = cursor_bo;
6519428088cSRay Strode 		cursor_bo = NULL;
6521277eed5SGabriel Krisman Bertazi 	} else {
6531277eed5SGabriel Krisman Bertazi 
6541277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6551277eed5SGabriel Krisman Bertazi 		if (ret)
6561277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6571277eed5SGabriel Krisman Bertazi 
658429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6591277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
6601277eed5SGabriel Krisman Bertazi 	}
6611277eed5SGabriel Krisman Bertazi 
6621277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
6631277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
6641277eed5SGabriel Krisman Bertazi 
6651277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
6661277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
6671277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
6681277eed5SGabriel Krisman Bertazi 
6690081cdfeSChristophe Fergeau 	if (old_cursor_bo != NULL)
6700081cdfeSChristophe Fergeau 		qxl_bo_unpin(old_cursor_bo);
671889ad63dSJeremy Cline 	qxl_bo_unref(&old_cursor_bo);
67216c6db36SRay Strode 	qxl_bo_unref(&cursor_bo);
67316c6db36SRay Strode 
6741277eed5SGabriel Krisman Bertazi 	return;
6751277eed5SGabriel Krisman Bertazi 
6761277eed5SGabriel Krisman Bertazi out_backoff:
6771277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
6780081cdfeSChristophe Fergeau out_unpin:
6790081cdfeSChristophe Fergeau 	qxl_bo_unpin(cursor_bo);
6801277eed5SGabriel Krisman Bertazi out_free_bo:
6811277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
6821277eed5SGabriel Krisman Bertazi out_kunmap:
6831277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
6841277eed5SGabriel Krisman Bertazi out_free_release:
6851277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
6861277eed5SGabriel Krisman Bertazi 	return;
6871277eed5SGabriel Krisman Bertazi 
6881277eed5SGabriel Krisman Bertazi }
6891277eed5SGabriel Krisman Bertazi 
69045dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
6911277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
6921277eed5SGabriel Krisman Bertazi {
6931277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
6941277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6951277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6961277eed5SGabriel Krisman Bertazi 	int ret;
6971277eed5SGabriel Krisman Bertazi 
6981277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6991277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
7001277eed5SGabriel Krisman Bertazi 					 &release, NULL);
7011277eed5SGabriel Krisman Bertazi 	if (ret)
7021277eed5SGabriel Krisman Bertazi 		return;
7031277eed5SGabriel Krisman Bertazi 
7041277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
7051277eed5SGabriel Krisman Bertazi 	if (ret) {
7061277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
7071277eed5SGabriel Krisman Bertazi 		return;
7081277eed5SGabriel Krisman Bertazi 	}
7091277eed5SGabriel Krisman Bertazi 
7101277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7111277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
7121277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7131277eed5SGabriel Krisman Bertazi 
7141277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7151277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7161277eed5SGabriel Krisman Bertazi }
7171277eed5SGabriel Krisman Bertazi 
718*90adda2cSGerd Hoffmann static void qxl_update_dumb_head(struct qxl_device *qdev,
719*90adda2cSGerd Hoffmann 				 int index, struct qxl_bo *bo)
720*90adda2cSGerd Hoffmann {
721*90adda2cSGerd Hoffmann 	uint32_t width, height;
722*90adda2cSGerd Hoffmann 
723*90adda2cSGerd Hoffmann 	if (index >= qdev->monitors_config->max_allowed)
724*90adda2cSGerd Hoffmann 		return;
725*90adda2cSGerd Hoffmann 
726*90adda2cSGerd Hoffmann 	if (bo && bo->is_dumb) {
727*90adda2cSGerd Hoffmann 		width = bo->surf.width;
728*90adda2cSGerd Hoffmann 		height = bo->surf.height;
729*90adda2cSGerd Hoffmann 	} else {
730*90adda2cSGerd Hoffmann 		width = 0;
731*90adda2cSGerd Hoffmann 		height = 0;
732*90adda2cSGerd Hoffmann 	}
733*90adda2cSGerd Hoffmann 
734*90adda2cSGerd Hoffmann 	if (qdev->dumb_heads[index].width == width &&
735*90adda2cSGerd Hoffmann 	    qdev->dumb_heads[index].height == height)
736*90adda2cSGerd Hoffmann 		return;
737*90adda2cSGerd Hoffmann 
738*90adda2cSGerd Hoffmann 	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
739*90adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].width,
740*90adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].height,
741*90adda2cSGerd Hoffmann 		  width, height);
742*90adda2cSGerd Hoffmann 	qdev->dumb_heads[index].width = width;
743*90adda2cSGerd Hoffmann 	qdev->dumb_heads[index].height = height;
744*90adda2cSGerd Hoffmann }
745*90adda2cSGerd Hoffmann 
746*90adda2cSGerd Hoffmann static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
747*90adda2cSGerd Hoffmann 				 struct qxl_surface *surf)
748*90adda2cSGerd Hoffmann {
749*90adda2cSGerd Hoffmann 	struct qxl_head *head;
750*90adda2cSGerd Hoffmann 	int i;
751*90adda2cSGerd Hoffmann 
752*90adda2cSGerd Hoffmann 	memset(surf, 0, sizeof(*surf));
753*90adda2cSGerd Hoffmann 	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
754*90adda2cSGerd Hoffmann 		head = qdev->dumb_heads + i;
755*90adda2cSGerd Hoffmann 		head->x = surf->width;
756*90adda2cSGerd Hoffmann 		surf->width += head->width;
757*90adda2cSGerd Hoffmann 		if (surf->height < head->height)
758*90adda2cSGerd Hoffmann 			surf->height = head->height;
759*90adda2cSGerd Hoffmann 	}
760*90adda2cSGerd Hoffmann 	if (surf->width < 64)
761*90adda2cSGerd Hoffmann 		surf->width = 64;
762*90adda2cSGerd Hoffmann 	if (surf->height < 64)
763*90adda2cSGerd Hoffmann 		surf->height = 64;
764*90adda2cSGerd Hoffmann 	surf->format = SPICE_SURFACE_FMT_32_xRGB;
765*90adda2cSGerd Hoffmann 	surf->stride = surf->width * 4;
766*90adda2cSGerd Hoffmann 
767*90adda2cSGerd Hoffmann 	if (!qdev->dumb_shadow_bo ||
768*90adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.width != surf->width ||
769*90adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.height != surf->height)
770*90adda2cSGerd Hoffmann 		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
771*90adda2cSGerd Hoffmann }
772*90adda2cSGerd Hoffmann 
77345dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
7741277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
7751277eed5SGabriel Krisman Bertazi {
77662676d10SGerd Hoffmann 	struct qxl_device *qdev = plane->dev->dev_private;
7771277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
778*90adda2cSGerd Hoffmann 	struct qxl_bo *user_bo;
779*90adda2cSGerd Hoffmann 	struct qxl_surface surf;
7801277eed5SGabriel Krisman Bertazi 	int ret;
7811277eed5SGabriel Krisman Bertazi 
7821277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
7831277eed5SGabriel Krisman Bertazi 		return 0;
7841277eed5SGabriel Krisman Bertazi 
785bf8744e4SPeter Wu 	obj = new_state->fb->obj[0];
7861277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7871277eed5SGabriel Krisman Bertazi 
78862676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
789*90adda2cSGerd Hoffmann 	    user_bo->is_dumb) {
790*90adda2cSGerd Hoffmann 		qxl_update_dumb_head(qdev, new_state->crtc->index,
791*90adda2cSGerd Hoffmann 				     user_bo);
792*90adda2cSGerd Hoffmann 		qxl_calc_dumb_shadow(qdev, &surf);
793*90adda2cSGerd Hoffmann 		if (!qdev->dumb_shadow_bo ||
794*90adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.width  != surf.width ||
795*90adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.height != surf.height) {
796*90adda2cSGerd Hoffmann 			if (qdev->dumb_shadow_bo) {
797*90adda2cSGerd Hoffmann 				drm_gem_object_put_unlocked
798*90adda2cSGerd Hoffmann 					(&qdev->dumb_shadow_bo->gem_base);
799*90adda2cSGerd Hoffmann 				qdev->dumb_shadow_bo = NULL;
80062676d10SGerd Hoffmann 			}
801*90adda2cSGerd Hoffmann 			qxl_bo_create(qdev, surf.height * surf.stride,
802*90adda2cSGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
803*90adda2cSGerd Hoffmann 				      &qdev->dumb_shadow_bo);
804*90adda2cSGerd Hoffmann 		}
805*90adda2cSGerd Hoffmann 		if (user_bo->shadow != qdev->dumb_shadow_bo) {
806*90adda2cSGerd Hoffmann 			if (user_bo->shadow) {
807*90adda2cSGerd Hoffmann 				drm_gem_object_put_unlocked
808*90adda2cSGerd Hoffmann 					(&user_bo->shadow->gem_base);
809*90adda2cSGerd Hoffmann 				user_bo->shadow = NULL;
810*90adda2cSGerd Hoffmann 			}
811*90adda2cSGerd Hoffmann 			drm_gem_object_get(&qdev->dumb_shadow_bo->gem_base);
812*90adda2cSGerd Hoffmann 			user_bo->shadow = qdev->dumb_shadow_bo;
81362676d10SGerd Hoffmann 		}
81462676d10SGerd Hoffmann 	}
81562676d10SGerd Hoffmann 
816545592fbSChristophe Fergeau 	ret = qxl_bo_pin(user_bo);
8171277eed5SGabriel Krisman Bertazi 	if (ret)
8181277eed5SGabriel Krisman Bertazi 		return ret;
8191277eed5SGabriel Krisman Bertazi 
8201277eed5SGabriel Krisman Bertazi 	return 0;
8211277eed5SGabriel Krisman Bertazi }
8221277eed5SGabriel Krisman Bertazi 
8231277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8241277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
8251277eed5SGabriel Krisman Bertazi {
8261277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
8271277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
8281277eed5SGabriel Krisman Bertazi 
8295f3d862aSGerd Hoffmann 	if (!old_state->fb) {
8305f3d862aSGerd Hoffmann 		/*
8315f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
8321277eed5SGabriel Krisman Bertazi 		 * unpin.
8331277eed5SGabriel Krisman Bertazi 		 */
8341277eed5SGabriel Krisman Bertazi 		return;
8351277eed5SGabriel Krisman Bertazi 	}
8361277eed5SGabriel Krisman Bertazi 
837bf8744e4SPeter Wu 	obj = old_state->fb->obj[0];
8381277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8391277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
84062676d10SGerd Hoffmann 
841*90adda2cSGerd Hoffmann 	if (old_state->fb != plane->state->fb && user_bo->shadow) {
84262676d10SGerd Hoffmann 		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
84362676d10SGerd Hoffmann 		user_bo->shadow = NULL;
84462676d10SGerd Hoffmann 	}
8451277eed5SGabriel Krisman Bertazi }
8461277eed5SGabriel Krisman Bertazi 
8471277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8481277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
8491277eed5SGabriel Krisman Bertazi };
8501277eed5SGabriel Krisman Bertazi 
8511277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8521277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
8531277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
8541277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
8551277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
8561277eed5SGabriel Krisman Bertazi };
8571277eed5SGabriel Krisman Bertazi 
8581277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
859472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
860472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
8611277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8629ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8639ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8649ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
8651277eed5SGabriel Krisman Bertazi };
8661277eed5SGabriel Krisman Bertazi 
867d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
868d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
869d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
870d3e7e42dSGabriel Krisman Bertazi };
871d3e7e42dSGabriel Krisman Bertazi 
872c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
873c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
874c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
875c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
876c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
877c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
878c2ff6632SGabriel Krisman Bertazi };
879c2ff6632SGabriel Krisman Bertazi 
880d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
881472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
882472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
883d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8849ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8859ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8869ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
887d3e7e42dSGabriel Krisman Bertazi };
888d3e7e42dSGabriel Krisman Bertazi 
889d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
890d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
891d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
892d3e7e42dSGabriel Krisman Bertazi {
893d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
894d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
895d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
896d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
897d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
898d3e7e42dSGabriel Krisman Bertazi 	int err;
899d3e7e42dSGabriel Krisman Bertazi 
900d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
901d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
902d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
903d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
904c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
9051277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
9061277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
9071277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
9081277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
9091277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
910d3e7e42dSGabriel Krisman Bertazi 	} else {
911d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
912d3e7e42dSGabriel Krisman Bertazi 	}
913d3e7e42dSGabriel Krisman Bertazi 
914d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
915d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
916d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
917d3e7e42dSGabriel Krisman Bertazi 
918d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
919d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
920e6fc3b68SBen Widawsky 				       NULL, type, NULL);
921d3e7e42dSGabriel Krisman Bertazi 	if (err)
922d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
923d3e7e42dSGabriel Krisman Bertazi 
924d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
925d3e7e42dSGabriel Krisman Bertazi 
926d3e7e42dSGabriel Krisman Bertazi 	return plane;
927d3e7e42dSGabriel Krisman Bertazi 
928d3e7e42dSGabriel Krisman Bertazi free_plane:
929d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
930d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
931d3e7e42dSGabriel Krisman Bertazi }
932d3e7e42dSGabriel Krisman Bertazi 
93307f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
934f64122c1SDave Airlie {
935f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
9361277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
937d3e7e42dSGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
938d3e7e42dSGabriel Krisman Bertazi 	int r;
939f64122c1SDave Airlie 
940f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
941f64122c1SDave Airlie 	if (!qxl_crtc)
942f64122c1SDave Airlie 		return -ENOMEM;
943f64122c1SDave Airlie 
944d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
945d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
946d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
947d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
948d3e7e42dSGabriel Krisman Bertazi 	}
949d3e7e42dSGabriel Krisman Bertazi 
9501277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9511277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
9521277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
9531277eed5SGabriel Krisman Bertazi 		goto clean_primary;
9541277eed5SGabriel Krisman Bertazi 	}
9551277eed5SGabriel Krisman Bertazi 
9561277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
957d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
958d3e7e42dSGabriel Krisman Bertazi 	if (r)
9591277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
960d3e7e42dSGabriel Krisman Bertazi 
96107f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
962f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
963f64122c1SDave Airlie 	return 0;
964d3e7e42dSGabriel Krisman Bertazi 
9651277eed5SGabriel Krisman Bertazi clean_cursor:
9661277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
9671277eed5SGabriel Krisman Bertazi 	kfree(cursor);
968d3e7e42dSGabriel Krisman Bertazi clean_primary:
969d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
970d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
971d3e7e42dSGabriel Krisman Bertazi free_mem:
972d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
973d3e7e42dSGabriel Krisman Bertazi 	return r;
974f64122c1SDave Airlie }
975f64122c1SDave Airlie 
976f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
977f64122c1SDave Airlie {
9781b000494SShayenne da Luz Moura 	unsigned int pwidth = 1024;
9791b000494SShayenne da Luz Moura 	unsigned int pheight = 768;
9802d856f94SGerd Hoffmann 	int ret = 0;
981f64122c1SDave Airlie 
982b0807423SMarc-André Lureau 	ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
983f64122c1SDave Airlie 	if (ret < 0)
984f64122c1SDave Airlie 		return ret;
985b0807423SMarc-André Lureau 	ret += qxl_add_common_modes(connector, pwidth, pheight);
986f64122c1SDave Airlie 	return ret;
987f64122c1SDave Airlie }
988f64122c1SDave Airlie 
989e0d92e16SLuc Van Oostenryck static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
990f64122c1SDave Airlie 			       struct drm_display_mode *mode)
991f64122c1SDave Airlie {
992bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
993bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
994bd3e1c7cSJonathon Jongsma 	int i;
995bd3e1c7cSJonathon Jongsma 
996f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
997f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
998bd3e1c7cSJonathon Jongsma 
999bd3e1c7cSJonathon Jongsma 	if (qdev->monitors_config_width == mode->hdisplay &&
1000bd3e1c7cSJonathon Jongsma 	    qdev->monitors_config_height == mode->vdisplay)
1001f64122c1SDave Airlie 		return MODE_OK;
1002bd3e1c7cSJonathon Jongsma 
1003bd3e1c7cSJonathon Jongsma 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
1004bd3e1c7cSJonathon Jongsma 		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
1005bd3e1c7cSJonathon Jongsma 			return MODE_OK;
1006bd3e1c7cSJonathon Jongsma 	}
1007bd3e1c7cSJonathon Jongsma 	return MODE_BAD;
1008f64122c1SDave Airlie }
1009f64122c1SDave Airlie 
10106d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1011f64122c1SDave Airlie {
1012f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1013f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1014f64122c1SDave Airlie 
1015f64122c1SDave Airlie 	DRM_DEBUG("\n");
1016f64122c1SDave Airlie 	return &qxl_output->enc;
1017f64122c1SDave Airlie }
1018f64122c1SDave Airlie 
1019f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1020f64122c1SDave Airlie };
1021f64122c1SDave Airlie 
1022f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1023f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1024f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1025f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1026f64122c1SDave Airlie };
1027f64122c1SDave Airlie 
1028f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1029f64122c1SDave Airlie 			struct drm_connector *connector,
1030f64122c1SDave Airlie 			bool force)
1031f64122c1SDave Airlie {
1032f64122c1SDave Airlie 	struct qxl_output *output =
1033f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1034f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1035f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
103669e5d3f8SDave Airlie 	bool connected = false;
1037f64122c1SDave Airlie 
1038f64122c1SDave Airlie 	/* The first monitor is always connected */
103969e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
104069e5d3f8SDave Airlie 		if (output->index == 0)
104169e5d3f8SDave Airlie 			connected = true;
104269e5d3f8SDave Airlie 	} else
104369e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
104469e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1045f64122c1SDave Airlie 
10465cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10475cab51cbSMarc-André Lureau 
1048f64122c1SDave Airlie 	return connected ? connector_status_connected
1049f64122c1SDave Airlie 			 : connector_status_disconnected;
1050f64122c1SDave Airlie }
1051f64122c1SDave Airlie 
1052f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1053f64122c1SDave Airlie {
1054f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1055f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1056f64122c1SDave Airlie 
105734ea3d38SThomas Wood 	drm_connector_unregister(connector);
1058f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1059f64122c1SDave Airlie 	kfree(qxl_output);
1060f64122c1SDave Airlie }
1061f64122c1SDave Airlie 
1062f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1063f64122c1SDave Airlie 	.detect = qxl_conn_detect,
10646af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1065f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
10669ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
10679ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10689ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1069f64122c1SDave Airlie };
1070f64122c1SDave Airlie 
1071f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
1072f64122c1SDave Airlie {
1073f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
1074f64122c1SDave Airlie }
1075f64122c1SDave Airlie 
1076f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
1077f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
1078f64122c1SDave Airlie };
1079f64122c1SDave Airlie 
10804695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
10814695b039SDave Airlie {
10824695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
10834695b039SDave Airlie 		return 0;
10844695b039SDave Airlie 
10854695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1086cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
10874695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
10884695b039SDave Airlie 
10894695b039SDave Airlie 	return 0;
10904695b039SDave Airlie }
10914695b039SDave Airlie 
10926d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1093f64122c1SDave Airlie {
10944695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
1095f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1096f64122c1SDave Airlie 	struct drm_connector *connector;
1097f64122c1SDave Airlie 	struct drm_encoder *encoder;
1098f64122c1SDave Airlie 
1099f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1100f64122c1SDave Airlie 	if (!qxl_output)
1101f64122c1SDave Airlie 		return -ENOMEM;
1102f64122c1SDave Airlie 
1103f64122c1SDave Airlie 	qxl_output->index = num_output;
1104f64122c1SDave Airlie 
1105f64122c1SDave Airlie 	connector = &qxl_output->base;
1106f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1107f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1108f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1109f64122c1SDave Airlie 
1110f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
111113a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
1112f64122c1SDave Airlie 
11135ff91e44SDave Airlie 	/* we get HPD via client monitors config */
11145ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1115f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1116cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&qxl_output->base,
1117f64122c1SDave Airlie 					  &qxl_output->enc);
1118f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1119f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1120f64122c1SDave Airlie 
11214695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11224695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11237dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11247dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11257dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11267dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1127f64122c1SDave Airlie 	return 0;
1128f64122c1SDave Airlie }
1129f64122c1SDave Airlie 
1130f64122c1SDave Airlie static struct drm_framebuffer *
1131f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1132f64122c1SDave Airlie 			    struct drm_file *file_priv,
11331eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1134f64122c1SDave Airlie {
1135bf8744e4SPeter Wu 	return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1136bf8744e4SPeter Wu 					    &qxl_fb_funcs);
1137f64122c1SDave Airlie }
1138f64122c1SDave Airlie 
1139f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1140f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1141472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1142472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1143f64122c1SDave Airlie };
1144f64122c1SDave Airlie 
11452bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1146f64122c1SDave Airlie {
1147f64122c1SDave Airlie 	int ret;
1148f64122c1SDave Airlie 	struct drm_gem_object *gobj;
114907f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
1150f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1151f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
1152f64122c1SDave Airlie 
1153f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1154f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1155f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1156f64122c1SDave Airlie 	if (ret) {
1157f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1158f64122c1SDave Airlie 		return -ENOMEM;
1159f64122c1SDave Airlie 	}
1160f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
11612bd6ce84SDave Airlie 
1162545592fbSChristophe Fergeau 	ret = qxl_bo_pin(qdev->monitors_config_bo);
11632bd6ce84SDave Airlie 	if (ret)
11642bd6ce84SDave Airlie 		return ret;
11652bd6ce84SDave Airlie 
1166f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
11672bd6ce84SDave Airlie 
1168f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1169f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1170f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1171f64122c1SDave Airlie 
1172f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
1173f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
1174*90adda2cSGerd Hoffmann 
1175*90adda2cSGerd Hoffmann 	qdev->dumb_heads = kcalloc(max_allowed, sizeof(qdev->dumb_heads[0]), GFP_KERNEL);
1176*90adda2cSGerd Hoffmann 	if (!qdev->dumb_heads) {
1177*90adda2cSGerd Hoffmann 		qxl_destroy_monitors_object(qdev);
1178*90adda2cSGerd Hoffmann 		return -ENOMEM;
1179*90adda2cSGerd Hoffmann 	}
11802bd6ce84SDave Airlie 	return 0;
11812bd6ce84SDave Airlie }
11822bd6ce84SDave Airlie 
11832bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
11842bd6ce84SDave Airlie {
11852bd6ce84SDave Airlie 	int ret;
11862bd6ce84SDave Airlie 
11872bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
11882bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
11892bd6ce84SDave Airlie 
11902bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
1191715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_unpin(qdev->monitors_config_bo);
11922bd6ce84SDave Airlie 	if (ret)
11932bd6ce84SDave Airlie 		return ret;
11942bd6ce84SDave Airlie 
11952bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
11962bd6ce84SDave Airlie 	return 0;
11972bd6ce84SDave Airlie }
11982bd6ce84SDave Airlie 
11992bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
12002bd6ce84SDave Airlie {
12012bd6ce84SDave Airlie 	int i;
12022bd6ce84SDave Airlie 	int ret;
12032bd6ce84SDave Airlie 
1204cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_init(&qdev->ddev);
12052bd6ce84SDave Airlie 
12062bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
12072bd6ce84SDave Airlie 	if (ret)
12082bd6ce84SDave Airlie 		return ret;
1209f64122c1SDave Airlie 
1210cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1211f64122c1SDave Airlie 
1212f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
12131277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_width = 0;
12141277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_height = 0;
1215cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_width = 8192;
1216cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_height = 8192;
1217f64122c1SDave Airlie 
1218cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.fb_base = qdev->vram_base;
12194695b039SDave Airlie 
1220cbdded7fSGabriel Krisman Bertazi 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
12214695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
12224695b039SDave Airlie 
122307f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1224cbdded7fSGabriel Krisman Bertazi 		qdev_crtc_init(&qdev->ddev, i);
1225cbdded7fSGabriel Krisman Bertazi 		qdev_output_init(&qdev->ddev, i);
1226f64122c1SDave Airlie 	}
1227f64122c1SDave Airlie 
1228c50fad8fSGerd Hoffmann 	qxl_display_read_client_monitors_config(qdev);
1229f64122c1SDave Airlie 
12309ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
12319ade8b98SGabriel Krisman Bertazi 
1232f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
1233f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
1234f64122c1SDave Airlie 	 * spice server. */
1235f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
1236f64122c1SDave Airlie 	return 0;
1237f64122c1SDave Airlie }
1238f64122c1SDave Airlie 
1239f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1240f64122c1SDave Airlie {
1241f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
12422bd6ce84SDave Airlie 
12432bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1244cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_cleanup(&qdev->ddev);
1245f64122c1SDave Airlie }
1246