xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision f6ebe9f9c9233a6114eb922aba9a0c9ccc2d2e14)
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>
27c0f4b75cSSam Ravnborg #include <linux/delay.h>
28c0f4b75cSSam Ravnborg 
29ce5c207cSDave Airlie #include <drm/drm_drv.h>
3010a0bd89SGabriel Krisman Bertazi #include <drm/drm_atomic.h>
31fcd70cd3SDaniel Vetter #include <drm/drm_atomic_helper.h>
32bf8744e4SPeter Wu #include <drm/drm_gem_framebuffer_helper.h>
33fcd70cd3SDaniel Vetter #include <drm/drm_plane_helper.h>
34fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
356f2bb119SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
36f64122c1SDave Airlie 
37edaf492cSMasahiro Yamada #include "qxl_drv.h"
38edaf492cSMasahiro Yamada #include "qxl_object.h"
39edaf492cSMasahiro Yamada 
4007f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
4107f8d9bdSDave Airlie {
4207f8d9bdSDave Airlie 	return head->width && head->height;
4307f8d9bdSDave Airlie }
4407f8d9bdSDave Airlie 
4566e0c8a5SAnton Vasilyev static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
4666e0c8a5SAnton Vasilyev 		unsigned int count)
47f64122c1SDave Airlie {
48f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
49f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
50f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
5162c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
52f64122c1SDave Airlie 	}
53f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
54f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
55d4b9dd50SGustavo A. R. Silva 				struct_size(qdev->client_monitors_config,
56d4b9dd50SGustavo A. R. Silva 				heads, count), GFP_KERNEL);
57735581a0SGerd Hoffmann 		if (!qdev->client_monitors_config)
5866e0c8a5SAnton Vasilyev 			return -ENOMEM;
59f64122c1SDave Airlie 	}
60f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
6166e0c8a5SAnton Vasilyev 	return 0;
62f64122c1SDave Airlie }
63f64122c1SDave Airlie 
649e3b3178SChristophe Fergeau enum {
659e3b3178SChristophe Fergeau 	MONITORS_CONFIG_MODIFIED,
669e3b3178SChristophe Fergeau 	MONITORS_CONFIG_UNCHANGED,
679e3b3178SChristophe Fergeau 	MONITORS_CONFIG_BAD_CRC,
6866e0c8a5SAnton Vasilyev 	MONITORS_CONFIG_ERROR,
699e3b3178SChristophe Fergeau };
709e3b3178SChristophe Fergeau 
71f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
72f64122c1SDave Airlie {
73f64122c1SDave Airlie 	int i;
74f64122c1SDave Airlie 	int num_monitors;
75f64122c1SDave Airlie 	uint32_t crc;
769e3b3178SChristophe Fergeau 	int status = MONITORS_CONFIG_UNCHANGED;
77f64122c1SDave Airlie 
78f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
79f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
80f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
81735581a0SGerd Hoffmann 	if (crc != qdev->rom->client_monitors_config_crc)
829e3b3178SChristophe Fergeau 		return MONITORS_CONFIG_BAD_CRC;
83c50fad8fSGerd Hoffmann 	if (!num_monitors) {
84c50fad8fSGerd Hoffmann 		DRM_DEBUG_KMS("no client monitors configured\n");
85c50fad8fSGerd Hoffmann 		return status;
86c50fad8fSGerd Hoffmann 	}
8721c76bd1SGerd Hoffmann 	if (num_monitors > qxl_num_crtc) {
885b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
8921c76bd1SGerd Hoffmann 			      qxl_num_crtc, num_monitors);
9021c76bd1SGerd Hoffmann 		num_monitors = qxl_num_crtc;
91f64122c1SDave Airlie 	} else {
92f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
93f64122c1SDave Airlie 	}
949e3b3178SChristophe Fergeau 	if (qdev->client_monitors_config
959e3b3178SChristophe Fergeau 	      && (num_monitors != qdev->client_monitors_config->count)) {
969e3b3178SChristophe Fergeau 		status = MONITORS_CONFIG_MODIFIED;
979e3b3178SChristophe Fergeau 	}
9866e0c8a5SAnton Vasilyev 	if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
9966e0c8a5SAnton Vasilyev 		status = MONITORS_CONFIG_ERROR;
10066e0c8a5SAnton Vasilyev 		return status;
10166e0c8a5SAnton Vasilyev 	}
102f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
10321c76bd1SGerd Hoffmann 	qdev->client_monitors_config->max_allowed = qxl_num_crtc;
104f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
105f64122c1SDave Airlie 		struct qxl_urect *c_rect =
106f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
107f64122c1SDave Airlie 		struct qxl_head *client_head =
108f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
1099e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
11007f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1119e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1129e3b3178SChristophe Fergeau 		}
1139e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
11407f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1159e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1169e3b3178SChristophe Fergeau 		}
1179e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
11807f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1199e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1209e3b3178SChristophe Fergeau 		}
1219e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
12207f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1239e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1249e3b3178SChristophe Fergeau 		}
1259e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
12607f8d9bdSDave Airlie 			client_head->surface_id = 0;
1279e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1289e3b3178SChristophe Fergeau 		}
1299e3b3178SChristophe Fergeau 		if (client_head->id != i) {
13007f8d9bdSDave Airlie 			client_head->id = i;
1319e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1329e3b3178SChristophe Fergeau 		}
1339e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
13407f8d9bdSDave Airlie 			client_head->flags = 0;
1359e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1369e3b3178SChristophe Fergeau 		}
13707f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
13807f8d9bdSDave Airlie 			  client_head->x, client_head->y);
139f64122c1SDave Airlie 	}
1409e3b3178SChristophe Fergeau 
1419e3b3178SChristophe Fergeau 	return status;
142f64122c1SDave Airlie }
143f64122c1SDave Airlie 
1447dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1457dea0941SDave Airlie {
146cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1477dea0941SDave Airlie 	struct drm_connector *connector;
1487dea0941SDave Airlie 	struct qxl_output *output;
1497dea0941SDave Airlie 	struct qxl_head *head;
1507dea0941SDave Airlie 
1517dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1527dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1537dea0941SDave Airlie 
1547dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1557dea0941SDave Airlie 
1567dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1577dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1587dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1597dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1607dea0941SDave Airlie 	}
1617dea0941SDave Airlie }
1627dea0941SDave Airlie 
163f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
164f64122c1SDave Airlie {
165cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
166bbaac135SSidong Yang 	struct drm_modeset_acquire_ctx ctx;
167bbaac135SSidong Yang 	int status, retries, ret;
1689e3b3178SChristophe Fergeau 
1699062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1709e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1719062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1729062155dSGerd Hoffmann 			break;
1739062155dSGerd Hoffmann 		udelay(5);
1749062155dSGerd Hoffmann 	}
17566e0c8a5SAnton Vasilyev 	if (status == MONITORS_CONFIG_ERROR) {
17666e0c8a5SAnton Vasilyev 		DRM_DEBUG_KMS("ignoring client monitors config: error");
17766e0c8a5SAnton Vasilyev 		return;
17866e0c8a5SAnton Vasilyev 	}
1799062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1809062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1819062155dSGerd Hoffmann 		return;
1829e3b3178SChristophe Fergeau 	}
1839e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1849062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1859e3b3178SChristophe Fergeau 		return;
186f64122c1SDave Airlie 	}
1874fdb0869SMarc-André Lureau 
188bbaac135SSidong Yang 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
1897dea0941SDave Airlie 	qxl_update_offset_props(qdev);
190ce5c207cSDave Airlie 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
191cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1924fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1934fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
194cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1954fdb0869SMarc-André Lureau 	}
196f64122c1SDave Airlie }
197f64122c1SDave Airlie 
198feba24deSGerd Hoffmann static int qxl_check_mode(struct qxl_device *qdev,
199feba24deSGerd Hoffmann 			  unsigned int width,
200feba24deSGerd Hoffmann 			  unsigned int height)
201feba24deSGerd Hoffmann {
202feba24deSGerd Hoffmann 	unsigned int stride;
203feba24deSGerd Hoffmann 	unsigned int size;
204feba24deSGerd Hoffmann 
205feba24deSGerd Hoffmann 	if (check_mul_overflow(width, 4u, &stride))
206feba24deSGerd Hoffmann 		return -EINVAL;
207feba24deSGerd Hoffmann 	if (check_mul_overflow(stride, height, &size))
208feba24deSGerd Hoffmann 		return -EINVAL;
209feba24deSGerd Hoffmann 	if (size > qdev->vram_size)
210feba24deSGerd Hoffmann 		return -ENOMEM;
211feba24deSGerd Hoffmann 	return 0;
212feba24deSGerd Hoffmann }
213feba24deSGerd Hoffmann 
214feba24deSGerd Hoffmann static int qxl_check_framebuffer(struct qxl_device *qdev,
215feba24deSGerd Hoffmann 				 struct qxl_bo *bo)
216feba24deSGerd Hoffmann {
217feba24deSGerd Hoffmann 	return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
218feba24deSGerd Hoffmann }
219feba24deSGerd Hoffmann 
2201b043677SGerd Hoffmann static int qxl_add_mode(struct drm_connector *connector,
2211b043677SGerd Hoffmann 			unsigned int width,
2221b043677SGerd Hoffmann 			unsigned int height,
2231b043677SGerd Hoffmann 			bool preferred)
2241b043677SGerd Hoffmann {
2251b043677SGerd Hoffmann 	struct drm_device *dev = connector->dev;
226e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
2271b043677SGerd Hoffmann 	struct drm_display_mode *mode = NULL;
2281b043677SGerd Hoffmann 	int rc;
2291b043677SGerd Hoffmann 
2301b043677SGerd Hoffmann 	rc = qxl_check_mode(qdev, width, height);
2311b043677SGerd Hoffmann 	if (rc != 0)
2321b043677SGerd Hoffmann 		return 0;
2331b043677SGerd Hoffmann 
2341b043677SGerd Hoffmann 	mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
2351b043677SGerd Hoffmann 	if (preferred)
2361b043677SGerd Hoffmann 		mode->type |= DRM_MODE_TYPE_PREFERRED;
2371b043677SGerd Hoffmann 	mode->hdisplay = width;
2381b043677SGerd Hoffmann 	mode->vdisplay = height;
2391b043677SGerd Hoffmann 	drm_mode_set_name(mode);
2401b043677SGerd Hoffmann 	drm_mode_probed_add(connector, mode);
2411b043677SGerd Hoffmann 	return 1;
2421b043677SGerd Hoffmann }
2431b043677SGerd Hoffmann 
2441b043677SGerd Hoffmann static int qxl_add_monitors_config_modes(struct drm_connector *connector)
245f64122c1SDave Airlie {
246f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
247e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
248f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
249f64122c1SDave Airlie 	int h = output->index;
250f64122c1SDave Airlie 	struct qxl_head *head;
251f64122c1SDave Airlie 
2522d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
2532d856f94SGerd Hoffmann 		return 0;
25421c76bd1SGerd Hoffmann 	if (h >= qxl_num_crtc)
2552d856f94SGerd Hoffmann 		return 0;
25607f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
257f64122c1SDave Airlie 		return 0;
2582d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2592d856f94SGerd Hoffmann 		return 0;
2602d856f94SGerd Hoffmann 
26107f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2622d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
263f64122c1SDave Airlie 
2641b043677SGerd Hoffmann 	return qxl_add_mode(connector, head->width, head->height, true);
265f64122c1SDave Airlie }
266f64122c1SDave Airlie 
267bd3e1c7cSJonathon Jongsma static struct mode_size {
268f64122c1SDave Airlie 	int w;
269f64122c1SDave Airlie 	int h;
270b5f030b7SGerd Hoffmann } extra_modes[] = {
271f64122c1SDave Airlie 	{ 720,  480},
272f64122c1SDave Airlie 	{1152,  768},
273f64122c1SDave Airlie 	{1280,  854},
274f64122c1SDave Airlie };
275f64122c1SDave Airlie 
276b5f030b7SGerd Hoffmann static int qxl_add_extra_modes(struct drm_connector *connector)
277bd3e1c7cSJonathon Jongsma {
2781b043677SGerd Hoffmann 	int i, ret = 0;
279408799ebSShayenne da Luz Moura 
280b5f030b7SGerd Hoffmann 	for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
2811b043677SGerd Hoffmann 		ret += qxl_add_mode(connector,
282b5f030b7SGerd Hoffmann 				    extra_modes[i].w,
283b5f030b7SGerd Hoffmann 				    extra_modes[i].h,
2841b043677SGerd Hoffmann 				    false);
2851b043677SGerd Hoffmann 	return ret;
286f64122c1SDave Airlie }
287f64122c1SDave Airlie 
288998010bfSGerd Hoffmann static void qxl_send_monitors_config(struct qxl_device *qdev)
289998010bfSGerd Hoffmann {
290998010bfSGerd Hoffmann 	int i;
291998010bfSGerd Hoffmann 
292998010bfSGerd Hoffmann 	BUG_ON(!qdev->ram_header->monitors_config);
293998010bfSGerd Hoffmann 
294998010bfSGerd Hoffmann 	if (qdev->monitors_config->count == 0)
295998010bfSGerd Hoffmann 		return;
296998010bfSGerd Hoffmann 
297998010bfSGerd Hoffmann 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
298998010bfSGerd Hoffmann 		struct qxl_head *head = &qdev->monitors_config->heads[i];
299998010bfSGerd Hoffmann 
300998010bfSGerd Hoffmann 		if (head->y > 8192 || head->x > 8192 ||
301998010bfSGerd Hoffmann 		    head->width > 8192 || head->height > 8192) {
302998010bfSGerd Hoffmann 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
303998010bfSGerd Hoffmann 				  i, head->width, head->height,
304998010bfSGerd Hoffmann 				  head->x, head->y);
305998010bfSGerd Hoffmann 			return;
306998010bfSGerd Hoffmann 		}
307998010bfSGerd Hoffmann 	}
308998010bfSGerd Hoffmann 	qxl_io_monitors_config(qdev);
309998010bfSGerd Hoffmann }
310998010bfSGerd Hoffmann 
311a6d3c4d7SGerd Hoffmann static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
312a6d3c4d7SGerd Hoffmann 					    const char *reason)
313a6d3c4d7SGerd Hoffmann {
314a6d3c4d7SGerd Hoffmann 	struct drm_device *dev = crtc->dev;
315e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
316a6d3c4d7SGerd Hoffmann 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
317a6d3c4d7SGerd Hoffmann 	struct qxl_head head;
318a6d3c4d7SGerd Hoffmann 	int oldcount, i = qcrtc->index;
319a6d3c4d7SGerd Hoffmann 
32016620544SGerd Hoffmann 	if (!qdev->primary_bo) {
321a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
322a6d3c4d7SGerd Hoffmann 		return;
323a6d3c4d7SGerd Hoffmann 	}
324a6d3c4d7SGerd Hoffmann 
32521c76bd1SGerd Hoffmann 	if (!qdev->monitors_config || qxl_num_crtc <= i)
326a6d3c4d7SGerd Hoffmann 		return;
327a6d3c4d7SGerd Hoffmann 
328a6d3c4d7SGerd Hoffmann 	head.id = i;
329a6d3c4d7SGerd Hoffmann 	head.flags = 0;
330a6d3c4d7SGerd Hoffmann 	oldcount = qdev->monitors_config->count;
331a6d3c4d7SGerd Hoffmann 	if (crtc->state->active) {
332a6d3c4d7SGerd Hoffmann 		struct drm_display_mode *mode = &crtc->mode;
333408799ebSShayenne da Luz Moura 
334a6d3c4d7SGerd Hoffmann 		head.width = mode->hdisplay;
335a6d3c4d7SGerd Hoffmann 		head.height = mode->vdisplay;
336a6d3c4d7SGerd Hoffmann 		head.x = crtc->x;
337a6d3c4d7SGerd Hoffmann 		head.y = crtc->y;
338a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count < i + 1)
339a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i + 1;
34090adda2cSGerd Hoffmann 		if (qdev->primary_bo == qdev->dumb_shadow_bo)
34190adda2cSGerd Hoffmann 			head.x += qdev->dumb_heads[i].x;
342a6d3c4d7SGerd Hoffmann 	} else if (i > 0) {
343a6d3c4d7SGerd Hoffmann 		head.width = 0;
344a6d3c4d7SGerd Hoffmann 		head.height = 0;
345a6d3c4d7SGerd Hoffmann 		head.x = 0;
346a6d3c4d7SGerd Hoffmann 		head.y = 0;
347a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count == i + 1)
348a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i;
349a6d3c4d7SGerd Hoffmann 	} else {
350a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
351a6d3c4d7SGerd Hoffmann 		return;
352a6d3c4d7SGerd Hoffmann 	}
353a6d3c4d7SGerd Hoffmann 
354a6d3c4d7SGerd Hoffmann 	if (head.width  == qdev->monitors_config->heads[i].width  &&
355a6d3c4d7SGerd Hoffmann 	    head.height == qdev->monitors_config->heads[i].height &&
356a6d3c4d7SGerd Hoffmann 	    head.x      == qdev->monitors_config->heads[i].x      &&
357a6d3c4d7SGerd Hoffmann 	    head.y      == qdev->monitors_config->heads[i].y      &&
358a6d3c4d7SGerd Hoffmann 	    oldcount    == qdev->monitors_config->count)
359a6d3c4d7SGerd Hoffmann 		return;
360a6d3c4d7SGerd Hoffmann 
361a6d3c4d7SGerd Hoffmann 	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
362a6d3c4d7SGerd Hoffmann 		      i, head.width, head.height, head.x, head.y,
363a6d3c4d7SGerd Hoffmann 		      crtc->state->active ? "on" : "off", reason);
364a6d3c4d7SGerd Hoffmann 	if (oldcount != qdev->monitors_config->count)
365a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
366a6d3c4d7SGerd Hoffmann 			      oldcount, qdev->monitors_config->count,
36721c76bd1SGerd Hoffmann 			      qxl_num_crtc);
368a6d3c4d7SGerd Hoffmann 
369a6d3c4d7SGerd Hoffmann 	qdev->monitors_config->heads[i] = head;
37021c76bd1SGerd Hoffmann 	qdev->monitors_config->max_allowed = qxl_num_crtc;
371a6d3c4d7SGerd Hoffmann 	qxl_send_monitors_config(qdev);
372a6d3c4d7SGerd Hoffmann }
373a6d3c4d7SGerd Hoffmann 
374c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
375*f6ebe9f9SMaxime Ripard 				  struct drm_atomic_state *state)
376c2ff6632SGabriel Krisman Bertazi {
377a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "flush");
378c2ff6632SGabriel Krisman Bertazi }
379c2ff6632SGabriel Krisman Bertazi 
380f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
381f64122c1SDave Airlie {
382f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
383f64122c1SDave Airlie 
3849428088cSRay Strode 	qxl_bo_unref(&qxl_crtc->cursor_bo);
385f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
386f64122c1SDave Airlie 	kfree(qxl_crtc);
387f64122c1SDave Airlie }
388f64122c1SDave Airlie 
389f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
390bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
391f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
3929973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
3939ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
3949ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
3959ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
396f64122c1SDave Airlie };
397f64122c1SDave Airlie 
3986d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
399f64122c1SDave Airlie 					 struct drm_file *file_priv,
4001b000494SShayenne da Luz Moura 					 unsigned int flags, unsigned int color,
401f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
4021b000494SShayenne da Luz Moura 					 unsigned int num_clips)
403f64122c1SDave Airlie {
404f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
405e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(fb->dev);
406f64122c1SDave Airlie 	struct drm_clip_rect norect;
407f64122c1SDave Airlie 	struct qxl_bo *qobj;
408bbaac135SSidong Yang 	struct drm_modeset_acquire_ctx ctx;
4094979904cSGerd Hoffmann 	bool is_primary;
410bbaac135SSidong Yang 	int inc = 1, ret;
411f64122c1SDave Airlie 
412bbaac135SSidong Yang 	DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
41373e9efd4SVille Syrjälä 
414bf8744e4SPeter Wu 	qobj = gem_to_qxl_bo(fb->obj[0]);
415b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
4164979904cSGerd Hoffmann 	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
417bbaac135SSidong Yang 	if (!is_primary)
418bbaac135SSidong Yang 		goto out_lock_end;
419b2b4465dSDave Airlie 
420f64122c1SDave Airlie 	if (!num_clips) {
421f64122c1SDave Airlie 		num_clips = 1;
422f64122c1SDave Airlie 		clips = &norect;
423f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
424f64122c1SDave Airlie 		norect.x2 = fb->width;
425f64122c1SDave Airlie 		norect.y2 = fb->height;
426f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
427f64122c1SDave Airlie 		num_clips /= 2;
428f64122c1SDave Airlie 		inc = 2; /* skip source rects */
429f64122c1SDave Airlie 	}
430f64122c1SDave Airlie 
431bf8744e4SPeter Wu 	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
43290adda2cSGerd Hoffmann 			  clips, num_clips, inc, 0);
43373e9efd4SVille Syrjälä 
434bbaac135SSidong Yang out_lock_end:
435ce5c207cSDave Airlie 	DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
43673e9efd4SVille Syrjälä 
437f64122c1SDave Airlie 	return 0;
438f64122c1SDave Airlie }
439f64122c1SDave Airlie 
440f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
441bf8744e4SPeter Wu 	.destroy = drm_gem_fb_destroy,
442f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
443bf8744e4SPeter Wu 	.create_handle = drm_gem_fb_create_handle,
444f64122c1SDave Airlie };
445f64122c1SDave Airlie 
4460b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
447351f950dSMaxime Ripard 				   struct drm_atomic_state *state)
448f64122c1SDave Airlie {
449a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "enable");
450f64122c1SDave Airlie }
451f64122c1SDave Airlie 
45264581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
453351f950dSMaxime Ripard 				    struct drm_atomic_state *state)
45407f8d9bdSDave Airlie {
455a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "disable");
45607f8d9bdSDave Airlie }
45707f8d9bdSDave Airlie 
458f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
459c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4600b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
46164581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
462f64122c1SDave Airlie };
463f64122c1SDave Airlie 
46445dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
465c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
466c2ff6632SGabriel Krisman Bertazi {
467e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(plane->dev);
468c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
469c2ff6632SGabriel Krisman Bertazi 
470c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
471c2ff6632SGabriel Krisman Bertazi 		return 0;
472c2ff6632SGabriel Krisman Bertazi 
473bf8744e4SPeter Wu 	bo = gem_to_qxl_bo(state->fb->obj[0]);
474c2ff6632SGabriel Krisman Bertazi 
475feba24deSGerd Hoffmann 	return qxl_check_framebuffer(qdev, bo);
476c2ff6632SGabriel Krisman Bertazi }
477c2ff6632SGabriel Krisman Bertazi 
4789428088cSRay Strode static int qxl_primary_apply_cursor(struct drm_plane *plane)
4799428088cSRay Strode {
4809428088cSRay Strode 	struct drm_device *dev = plane->dev;
481e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
4829428088cSRay Strode 	struct drm_framebuffer *fb = plane->state->fb;
4839428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
4849428088cSRay Strode 	struct qxl_cursor_cmd *cmd;
4859428088cSRay Strode 	struct qxl_release *release;
4869428088cSRay Strode 	int ret = 0;
4879428088cSRay Strode 
4889428088cSRay Strode 	if (!qcrtc->cursor_bo)
4899428088cSRay Strode 		return 0;
4909428088cSRay Strode 
4919428088cSRay Strode 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
4929428088cSRay Strode 					 QXL_RELEASE_CURSOR_CMD,
4939428088cSRay Strode 					 &release, NULL);
4949428088cSRay Strode 	if (ret)
4959428088cSRay Strode 		return ret;
4969428088cSRay Strode 
4979428088cSRay Strode 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
4989428088cSRay Strode 	if (ret)
4999428088cSRay Strode 		goto out_free_release;
5009428088cSRay Strode 
5019428088cSRay Strode 	ret = qxl_release_reserve_list(release, false);
5029428088cSRay Strode 	if (ret)
5039428088cSRay Strode 		goto out_free_release;
5049428088cSRay Strode 
5059428088cSRay Strode 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5069428088cSRay Strode 	cmd->type = QXL_CURSOR_SET;
5079428088cSRay Strode 	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
5089428088cSRay Strode 	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
5099428088cSRay Strode 
5109428088cSRay Strode 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5119428088cSRay Strode 
5129428088cSRay Strode 	cmd->u.set.visible = 1;
5139428088cSRay Strode 	qxl_release_unmap(qdev, release, &cmd->release_info);
5149428088cSRay Strode 
5159428088cSRay Strode 	qxl_release_fence_buffer_objects(release);
516933db733SVasily Averin 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5179428088cSRay Strode 
5189428088cSRay Strode 	return ret;
5199428088cSRay Strode 
5209428088cSRay Strode out_free_release:
5219428088cSRay Strode 	qxl_release_free(qdev, release);
5229428088cSRay Strode 	return ret;
5239428088cSRay Strode }
5249428088cSRay Strode 
525c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
526c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
527c2ff6632SGabriel Krisman Bertazi {
528e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(plane->dev);
529bf8744e4SPeter Wu 	struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
530fb8cd60cSYueHaibing 	struct qxl_bo *primary;
531c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
532c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
533c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
534bf8744e4SPeter Wu 	    .x2 = plane->state->fb->width,
535bf8744e4SPeter Wu 	    .y2 = plane->state->fb->height
536c2ff6632SGabriel Krisman Bertazi 	};
53790adda2cSGerd Hoffmann 	uint32_t dumb_shadow_offset = 0;
538c2ff6632SGabriel Krisman Bertazi 
5394979904cSGerd Hoffmann 	primary = bo->shadow ? bo->shadow : bo;
540b0e07da3SGerd Hoffmann 
5414979904cSGerd Hoffmann 	if (!primary->is_primary) {
5424979904cSGerd Hoffmann 		if (qdev->primary_bo)
543b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
5444979904cSGerd Hoffmann 		qxl_io_create_primary(qdev, primary);
5451f85535cSGerd Hoffmann 		qxl_primary_apply_cursor(plane);
5461f85535cSGerd Hoffmann 	}
54762676d10SGerd Hoffmann 
54890adda2cSGerd Hoffmann 	if (bo->is_dumb)
54990adda2cSGerd Hoffmann 		dumb_shadow_offset =
55090adda2cSGerd Hoffmann 			qdev->dumb_heads[plane->state->crtc->index].x;
55190adda2cSGerd Hoffmann 
55290adda2cSGerd Hoffmann 	qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
55390adda2cSGerd Hoffmann 			  dumb_shadow_offset);
554c2ff6632SGabriel Krisman Bertazi }
555c2ff6632SGabriel Krisman Bertazi 
556c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
557c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
558c2ff6632SGabriel Krisman Bertazi {
559e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(plane->dev);
560c2ff6632SGabriel Krisman Bertazi 
561b0e07da3SGerd Hoffmann 	if (old_state->fb) {
562bf8744e4SPeter Wu 		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
563c2ff6632SGabriel Krisman Bertazi 
564b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
565c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
566c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
567c2ff6632SGabriel Krisman Bertazi 		}
568c2ff6632SGabriel Krisman Bertazi 	}
569b0e07da3SGerd Hoffmann }
570c2ff6632SGabriel Krisman Bertazi 
5711277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
5721277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
5731277eed5SGabriel Krisman Bertazi {
5741277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
575e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
5761277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
5779428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
5781277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
5791277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
5801277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
5811277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
582889ad63dSJeremy Cline 	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
5831277eed5SGabriel Krisman Bertazi 	int ret;
5841277eed5SGabriel Krisman Bertazi 	void *user_ptr;
5851277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
5861277eed5SGabriel Krisman Bertazi 
5871277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5881277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
5891277eed5SGabriel Krisman Bertazi 					 &release, NULL);
590ee5cb7c4SDan Carpenter 	if (ret)
591ee5cb7c4SDan Carpenter 		return;
5921277eed5SGabriel Krisman Bertazi 
5931277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
594bf8744e4SPeter Wu 		obj = fb->obj[0];
5951277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
5961277eed5SGabriel Krisman Bertazi 
5971277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
5981277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
5991277eed5SGabriel Krisman Bertazi 		if (ret)
6001277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6011277eed5SGabriel Krisman Bertazi 
6021277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
6031277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6041277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6051277eed5SGabriel Krisman Bertazi 		if (ret)
6061277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6071277eed5SGabriel Krisman Bertazi 
6080081cdfeSChristophe Fergeau 		ret = qxl_bo_pin(cursor_bo);
6091277eed5SGabriel Krisman Bertazi 		if (ret)
6101277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6111277eed5SGabriel Krisman Bertazi 
6120081cdfeSChristophe Fergeau 		ret = qxl_release_reserve_list(release, true);
6130081cdfeSChristophe Fergeau 		if (ret)
6140081cdfeSChristophe Fergeau 			goto out_unpin;
6150081cdfeSChristophe Fergeau 
6161277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6171277eed5SGabriel Krisman Bertazi 		if (ret)
6181277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6191277eed5SGabriel Krisman Bertazi 
6201277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6211277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6221277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6231277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6241277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6251277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6261277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6271277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6281277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6291277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6301277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6311277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6321277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6331277eed5SGabriel Krisman Bertazi 
634429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6351277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6361277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6371277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6381277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6399428088cSRay Strode 
640889ad63dSJeremy Cline 		old_cursor_bo = qcrtc->cursor_bo;
6419428088cSRay Strode 		qcrtc->cursor_bo = cursor_bo;
6429428088cSRay Strode 		cursor_bo = NULL;
6431277eed5SGabriel Krisman Bertazi 	} else {
6441277eed5SGabriel Krisman Bertazi 
6451277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6461277eed5SGabriel Krisman Bertazi 		if (ret)
6471277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6481277eed5SGabriel Krisman Bertazi 
649429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6501277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
6511277eed5SGabriel Krisman Bertazi 	}
6521277eed5SGabriel Krisman Bertazi 
6531277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
6541277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
6551277eed5SGabriel Krisman Bertazi 
6561277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
6571277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
658933db733SVasily Averin 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
6591277eed5SGabriel Krisman Bertazi 
6600081cdfeSChristophe Fergeau 	if (old_cursor_bo != NULL)
6610081cdfeSChristophe Fergeau 		qxl_bo_unpin(old_cursor_bo);
662889ad63dSJeremy Cline 	qxl_bo_unref(&old_cursor_bo);
66316c6db36SRay Strode 	qxl_bo_unref(&cursor_bo);
66416c6db36SRay Strode 
6651277eed5SGabriel Krisman Bertazi 	return;
6661277eed5SGabriel Krisman Bertazi 
6671277eed5SGabriel Krisman Bertazi out_backoff:
6681277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
6690081cdfeSChristophe Fergeau out_unpin:
6700081cdfeSChristophe Fergeau 	qxl_bo_unpin(cursor_bo);
6711277eed5SGabriel Krisman Bertazi out_free_bo:
6721277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
6731277eed5SGabriel Krisman Bertazi out_kunmap:
6741277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
6751277eed5SGabriel Krisman Bertazi out_free_release:
6761277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
6771277eed5SGabriel Krisman Bertazi 	return;
6781277eed5SGabriel Krisman Bertazi 
6791277eed5SGabriel Krisman Bertazi }
6801277eed5SGabriel Krisman Bertazi 
68145dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
6821277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
6831277eed5SGabriel Krisman Bertazi {
684e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(plane->dev);
6851277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6861277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6871277eed5SGabriel Krisman Bertazi 	int ret;
6881277eed5SGabriel Krisman Bertazi 
6891277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6901277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
6911277eed5SGabriel Krisman Bertazi 					 &release, NULL);
6921277eed5SGabriel Krisman Bertazi 	if (ret)
6931277eed5SGabriel Krisman Bertazi 		return;
6941277eed5SGabriel Krisman Bertazi 
6951277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
6961277eed5SGabriel Krisman Bertazi 	if (ret) {
6971277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
6981277eed5SGabriel Krisman Bertazi 		return;
6991277eed5SGabriel Krisman Bertazi 	}
7001277eed5SGabriel Krisman Bertazi 
7011277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7021277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
7031277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7041277eed5SGabriel Krisman Bertazi 
7051277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
706933db733SVasily Averin 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7071277eed5SGabriel Krisman Bertazi }
7081277eed5SGabriel Krisman Bertazi 
70990adda2cSGerd Hoffmann static void qxl_update_dumb_head(struct qxl_device *qdev,
71090adda2cSGerd Hoffmann 				 int index, struct qxl_bo *bo)
71190adda2cSGerd Hoffmann {
71290adda2cSGerd Hoffmann 	uint32_t width, height;
71390adda2cSGerd Hoffmann 
71490adda2cSGerd Hoffmann 	if (index >= qdev->monitors_config->max_allowed)
71590adda2cSGerd Hoffmann 		return;
71690adda2cSGerd Hoffmann 
71790adda2cSGerd Hoffmann 	if (bo && bo->is_dumb) {
71890adda2cSGerd Hoffmann 		width = bo->surf.width;
71990adda2cSGerd Hoffmann 		height = bo->surf.height;
72090adda2cSGerd Hoffmann 	} else {
72190adda2cSGerd Hoffmann 		width = 0;
72290adda2cSGerd Hoffmann 		height = 0;
72390adda2cSGerd Hoffmann 	}
72490adda2cSGerd Hoffmann 
72590adda2cSGerd Hoffmann 	if (qdev->dumb_heads[index].width == width &&
72690adda2cSGerd Hoffmann 	    qdev->dumb_heads[index].height == height)
72790adda2cSGerd Hoffmann 		return;
72890adda2cSGerd Hoffmann 
72990adda2cSGerd Hoffmann 	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
73090adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].width,
73190adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].height,
73290adda2cSGerd Hoffmann 		  width, height);
73390adda2cSGerd Hoffmann 	qdev->dumb_heads[index].width = width;
73490adda2cSGerd Hoffmann 	qdev->dumb_heads[index].height = height;
73590adda2cSGerd Hoffmann }
73690adda2cSGerd Hoffmann 
73790adda2cSGerd Hoffmann static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
73890adda2cSGerd Hoffmann 				 struct qxl_surface *surf)
73990adda2cSGerd Hoffmann {
74090adda2cSGerd Hoffmann 	struct qxl_head *head;
74190adda2cSGerd Hoffmann 	int i;
74290adda2cSGerd Hoffmann 
74390adda2cSGerd Hoffmann 	memset(surf, 0, sizeof(*surf));
74490adda2cSGerd Hoffmann 	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
74590adda2cSGerd Hoffmann 		head = qdev->dumb_heads + i;
74690adda2cSGerd Hoffmann 		head->x = surf->width;
74790adda2cSGerd Hoffmann 		surf->width += head->width;
74890adda2cSGerd Hoffmann 		if (surf->height < head->height)
74990adda2cSGerd Hoffmann 			surf->height = head->height;
75090adda2cSGerd Hoffmann 	}
75190adda2cSGerd Hoffmann 	if (surf->width < 64)
75290adda2cSGerd Hoffmann 		surf->width = 64;
75390adda2cSGerd Hoffmann 	if (surf->height < 64)
75490adda2cSGerd Hoffmann 		surf->height = 64;
75590adda2cSGerd Hoffmann 	surf->format = SPICE_SURFACE_FMT_32_xRGB;
75690adda2cSGerd Hoffmann 	surf->stride = surf->width * 4;
75790adda2cSGerd Hoffmann 
75890adda2cSGerd Hoffmann 	if (!qdev->dumb_shadow_bo ||
75990adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.width != surf->width ||
76090adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.height != surf->height)
76190adda2cSGerd Hoffmann 		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
76290adda2cSGerd Hoffmann }
76390adda2cSGerd Hoffmann 
76445dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
7651277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
7661277eed5SGabriel Krisman Bertazi {
767e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(plane->dev);
7681277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
76990adda2cSGerd Hoffmann 	struct qxl_bo *user_bo;
77090adda2cSGerd Hoffmann 	struct qxl_surface surf;
7711277eed5SGabriel Krisman Bertazi 
7721277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
7731277eed5SGabriel Krisman Bertazi 		return 0;
7741277eed5SGabriel Krisman Bertazi 
775bf8744e4SPeter Wu 	obj = new_state->fb->obj[0];
7761277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7771277eed5SGabriel Krisman Bertazi 
77862676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
77990adda2cSGerd Hoffmann 	    user_bo->is_dumb) {
78090adda2cSGerd Hoffmann 		qxl_update_dumb_head(qdev, new_state->crtc->index,
78190adda2cSGerd Hoffmann 				     user_bo);
78290adda2cSGerd Hoffmann 		qxl_calc_dumb_shadow(qdev, &surf);
78390adda2cSGerd Hoffmann 		if (!qdev->dumb_shadow_bo ||
78490adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.width  != surf.width ||
78590adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.height != surf.height) {
78690adda2cSGerd Hoffmann 			if (qdev->dumb_shadow_bo) {
7879c86fb18SEmil Velikov 				drm_gem_object_put
788e0828d54SGerd Hoffmann 					(&qdev->dumb_shadow_bo->tbo.base);
78990adda2cSGerd Hoffmann 				qdev->dumb_shadow_bo = NULL;
79062676d10SGerd Hoffmann 			}
79190adda2cSGerd Hoffmann 			qxl_bo_create(qdev, surf.height * surf.stride,
79290adda2cSGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
79390adda2cSGerd Hoffmann 				      &qdev->dumb_shadow_bo);
79490adda2cSGerd Hoffmann 		}
79590adda2cSGerd Hoffmann 		if (user_bo->shadow != qdev->dumb_shadow_bo) {
79690adda2cSGerd Hoffmann 			if (user_bo->shadow) {
7979c86fb18SEmil Velikov 				drm_gem_object_put
798e0828d54SGerd Hoffmann 					(&user_bo->shadow->tbo.base);
79990adda2cSGerd Hoffmann 				user_bo->shadow = NULL;
80090adda2cSGerd Hoffmann 			}
801e0828d54SGerd Hoffmann 			drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
80290adda2cSGerd Hoffmann 			user_bo->shadow = qdev->dumb_shadow_bo;
80362676d10SGerd Hoffmann 		}
80462676d10SGerd Hoffmann 	}
80562676d10SGerd Hoffmann 
80679b4d4d2SQinglang Miao 	return qxl_bo_pin(user_bo);
8071277eed5SGabriel Krisman Bertazi }
8081277eed5SGabriel Krisman Bertazi 
8091277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8101277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
8111277eed5SGabriel Krisman Bertazi {
8121277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
8131277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
8141277eed5SGabriel Krisman Bertazi 
8155f3d862aSGerd Hoffmann 	if (!old_state->fb) {
8165f3d862aSGerd Hoffmann 		/*
8175f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
8181277eed5SGabriel Krisman Bertazi 		 * unpin.
8191277eed5SGabriel Krisman Bertazi 		 */
8201277eed5SGabriel Krisman Bertazi 		return;
8211277eed5SGabriel Krisman Bertazi 	}
8221277eed5SGabriel Krisman Bertazi 
823bf8744e4SPeter Wu 	obj = old_state->fb->obj[0];
8241277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8251277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
82662676d10SGerd Hoffmann 
82790adda2cSGerd Hoffmann 	if (old_state->fb != plane->state->fb && user_bo->shadow) {
8289c86fb18SEmil Velikov 		drm_gem_object_put(&user_bo->shadow->tbo.base);
82962676d10SGerd Hoffmann 		user_bo->shadow = NULL;
83062676d10SGerd Hoffmann 	}
8311277eed5SGabriel Krisman Bertazi }
8321277eed5SGabriel Krisman Bertazi 
8331277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8341277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
8351277eed5SGabriel Krisman Bertazi };
8361277eed5SGabriel Krisman Bertazi 
8371277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8381277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
8391277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
8401277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
8411277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
8421277eed5SGabriel Krisman Bertazi };
8431277eed5SGabriel Krisman Bertazi 
8441277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
845472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
846472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
8471277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8489ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8499ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8509ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
8511277eed5SGabriel Krisman Bertazi };
8521277eed5SGabriel Krisman Bertazi 
853d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
854d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
855d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
856d3e7e42dSGabriel Krisman Bertazi };
857d3e7e42dSGabriel Krisman Bertazi 
858c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
859c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
860c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
861c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
862c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
863c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
864c2ff6632SGabriel Krisman Bertazi };
865c2ff6632SGabriel Krisman Bertazi 
866d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
867472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
868472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
869d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8709ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8719ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8729ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
873d3e7e42dSGabriel Krisman Bertazi };
874d3e7e42dSGabriel Krisman Bertazi 
875d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
876d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
877d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
878d3e7e42dSGabriel Krisman Bertazi {
879d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
880d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
881d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
882d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
883d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
884d3e7e42dSGabriel Krisman Bertazi 	int err;
885d3e7e42dSGabriel Krisman Bertazi 
886d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
887d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
888d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
889d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
890c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
8911277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
8921277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
8931277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
8941277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
8951277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
896d3e7e42dSGabriel Krisman Bertazi 	} else {
897d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
898d3e7e42dSGabriel Krisman Bertazi 	}
899d3e7e42dSGabriel Krisman Bertazi 
900d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
901d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
902d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
903d3e7e42dSGabriel Krisman Bertazi 
904d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
905d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
906e6fc3b68SBen Widawsky 				       NULL, type, NULL);
907d3e7e42dSGabriel Krisman Bertazi 	if (err)
908d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
909d3e7e42dSGabriel Krisman Bertazi 
910d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
911d3e7e42dSGabriel Krisman Bertazi 
912d3e7e42dSGabriel Krisman Bertazi 	return plane;
913d3e7e42dSGabriel Krisman Bertazi 
914d3e7e42dSGabriel Krisman Bertazi free_plane:
915d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
916d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
917d3e7e42dSGabriel Krisman Bertazi }
918d3e7e42dSGabriel Krisman Bertazi 
91907f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
920f64122c1SDave Airlie {
921f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
9221277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
923e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
924d3e7e42dSGabriel Krisman Bertazi 	int r;
925f64122c1SDave Airlie 
926f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
927f64122c1SDave Airlie 	if (!qxl_crtc)
928f64122c1SDave Airlie 		return -ENOMEM;
929f64122c1SDave Airlie 
930d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
931d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
932d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
933d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
934d3e7e42dSGabriel Krisman Bertazi 	}
935d3e7e42dSGabriel Krisman Bertazi 
9361277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9371277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
9381277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
9391277eed5SGabriel Krisman Bertazi 		goto clean_primary;
9401277eed5SGabriel Krisman Bertazi 	}
9411277eed5SGabriel Krisman Bertazi 
9421277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
943d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
944d3e7e42dSGabriel Krisman Bertazi 	if (r)
9451277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
946d3e7e42dSGabriel Krisman Bertazi 
94707f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
948f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
949f64122c1SDave Airlie 	return 0;
950d3e7e42dSGabriel Krisman Bertazi 
9511277eed5SGabriel Krisman Bertazi clean_cursor:
9521277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
9531277eed5SGabriel Krisman Bertazi 	kfree(cursor);
954d3e7e42dSGabriel Krisman Bertazi clean_primary:
955d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
956d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
957d3e7e42dSGabriel Krisman Bertazi free_mem:
958d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
959d3e7e42dSGabriel Krisman Bertazi 	return r;
960f64122c1SDave Airlie }
961f64122c1SDave Airlie 
962f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
963f64122c1SDave Airlie {
9641b043677SGerd Hoffmann 	struct drm_device *dev = connector->dev;
965e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
9661b043677SGerd Hoffmann 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
9671b000494SShayenne da Luz Moura 	unsigned int pwidth = 1024;
9681b000494SShayenne da Luz Moura 	unsigned int pheight = 768;
9692d856f94SGerd Hoffmann 	int ret = 0;
970f64122c1SDave Airlie 
9711b043677SGerd Hoffmann 	if (qdev->client_monitors_config) {
9721b043677SGerd Hoffmann 		struct qxl_head *head;
9731b043677SGerd Hoffmann 		head = &qdev->client_monitors_config->heads[output->index];
9741b043677SGerd Hoffmann 		if (head->width)
9751b043677SGerd Hoffmann 			pwidth = head->width;
9761b043677SGerd Hoffmann 		if (head->height)
9771b043677SGerd Hoffmann 			pheight = head->height;
9781b043677SGerd Hoffmann 	}
9791b043677SGerd Hoffmann 
980b5f030b7SGerd Hoffmann 	ret += drm_add_modes_noedid(connector, 8192, 8192);
981b5f030b7SGerd Hoffmann 	ret += qxl_add_extra_modes(connector);
9821b043677SGerd Hoffmann 	ret += qxl_add_monitors_config_modes(connector);
9831b043677SGerd Hoffmann 	drm_set_preferred_mode(connector, pwidth, pheight);
984f64122c1SDave Airlie 	return ret;
985f64122c1SDave Airlie }
986f64122c1SDave Airlie 
987e0d92e16SLuc Van Oostenryck static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
988f64122c1SDave Airlie 			       struct drm_display_mode *mode)
989f64122c1SDave Airlie {
990bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
991e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(ddev);
992bd3e1c7cSJonathon Jongsma 
993feba24deSGerd Hoffmann 	if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
994bd3e1c7cSJonathon Jongsma 		return MODE_BAD;
995feba24deSGerd Hoffmann 
996feba24deSGerd Hoffmann 	return MODE_OK;
997f64122c1SDave Airlie }
998f64122c1SDave Airlie 
9996d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1000f64122c1SDave Airlie {
1001f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1002f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1003f64122c1SDave Airlie 
1004f64122c1SDave Airlie 	DRM_DEBUG("\n");
1005f64122c1SDave Airlie 	return &qxl_output->enc;
1006f64122c1SDave Airlie }
1007f64122c1SDave Airlie 
1008f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1009f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1010f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1011f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1012f64122c1SDave Airlie };
1013f64122c1SDave Airlie 
1014f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1015f64122c1SDave Airlie 			struct drm_connector *connector,
1016f64122c1SDave Airlie 			bool force)
1017f64122c1SDave Airlie {
1018f64122c1SDave Airlie 	struct qxl_output *output =
1019f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1020f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1021e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(ddev);
102269e5d3f8SDave Airlie 	bool connected = false;
1023f64122c1SDave Airlie 
1024f64122c1SDave Airlie 	/* The first monitor is always connected */
102569e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
102669e5d3f8SDave Airlie 		if (output->index == 0)
102769e5d3f8SDave Airlie 			connected = true;
102869e5d3f8SDave Airlie 	} else
102969e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
103069e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1031f64122c1SDave Airlie 
10325cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10335cab51cbSMarc-André Lureau 
1034f64122c1SDave Airlie 	return connected ? connector_status_connected
1035f64122c1SDave Airlie 			 : connector_status_disconnected;
1036f64122c1SDave Airlie }
1037f64122c1SDave Airlie 
1038f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1039f64122c1SDave Airlie {
1040f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1041f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1042f64122c1SDave Airlie 
104334ea3d38SThomas Wood 	drm_connector_unregister(connector);
1044f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1045f64122c1SDave Airlie 	kfree(qxl_output);
1046f64122c1SDave Airlie }
1047f64122c1SDave Airlie 
1048f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1049f64122c1SDave Airlie 	.detect = qxl_conn_detect,
10506af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1051f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
10529ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
10539ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10549ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1055f64122c1SDave Airlie };
1056f64122c1SDave Airlie 
10574695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
10584695b039SDave Airlie {
10594695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
10604695b039SDave Airlie 		return 0;
10614695b039SDave Airlie 
10624695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1063cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
10644695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
10654695b039SDave Airlie 
10664695b039SDave Airlie 	return 0;
10674695b039SDave Airlie }
10684695b039SDave Airlie 
10696d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1070f64122c1SDave Airlie {
1071e304f8a0SDaniel Vetter 	struct qxl_device *qdev = to_qxl(dev);
1072f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1073f64122c1SDave Airlie 	struct drm_connector *connector;
1074f64122c1SDave Airlie 	struct drm_encoder *encoder;
10756f2bb119SThomas Zimmermann 	int ret;
1076f64122c1SDave Airlie 
1077f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1078f64122c1SDave Airlie 	if (!qxl_output)
1079f64122c1SDave Airlie 		return -ENOMEM;
1080f64122c1SDave Airlie 
1081f64122c1SDave Airlie 	qxl_output->index = num_output;
1082f64122c1SDave Airlie 
1083f64122c1SDave Airlie 	connector = &qxl_output->base;
1084f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1085f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1086f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1087f64122c1SDave Airlie 
10886f2bb119SThomas Zimmermann 	ret = drm_simple_encoder_init(dev, &qxl_output->enc,
10896f2bb119SThomas Zimmermann 				      DRM_MODE_ENCODER_VIRTUAL);
10906f2bb119SThomas Zimmermann 	if (ret) {
10916f2bb119SThomas Zimmermann 		drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
10926f2bb119SThomas Zimmermann 			ret);
10936f2bb119SThomas Zimmermann 		goto err_drm_connector_cleanup;
10946f2bb119SThomas Zimmermann 	}
1095f64122c1SDave Airlie 
10965ff91e44SDave Airlie 	/* we get HPD via client monitors config */
10975ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1098f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1099cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&qxl_output->base,
1100f64122c1SDave Airlie 					  &qxl_output->enc);
1101f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1102f64122c1SDave Airlie 
11034695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11044695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11057dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11067dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11077dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11087dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1109f64122c1SDave Airlie 	return 0;
11106f2bb119SThomas Zimmermann 
11116f2bb119SThomas Zimmermann err_drm_connector_cleanup:
11126f2bb119SThomas Zimmermann 	drm_connector_cleanup(&qxl_output->base);
11136f2bb119SThomas Zimmermann 	kfree(qxl_output);
11146f2bb119SThomas Zimmermann 	return ret;
1115f64122c1SDave Airlie }
1116f64122c1SDave Airlie 
1117f64122c1SDave Airlie static struct drm_framebuffer *
1118f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1119f64122c1SDave Airlie 			    struct drm_file *file_priv,
11201eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1121f64122c1SDave Airlie {
1122bf8744e4SPeter Wu 	return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1123bf8744e4SPeter Wu 					    &qxl_fb_funcs);
1124f64122c1SDave Airlie }
1125f64122c1SDave Airlie 
1126f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1127f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1128472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1129472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1130f64122c1SDave Airlie };
1131f64122c1SDave Airlie 
11322bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1133f64122c1SDave Airlie {
1134f64122c1SDave Airlie 	int ret;
1135f64122c1SDave Airlie 	struct drm_gem_object *gobj;
1136f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
113721c76bd1SGerd Hoffmann 		qxl_num_crtc * sizeof(struct qxl_head);
1138f64122c1SDave Airlie 
1139f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1140f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1141f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1142f64122c1SDave Airlie 	if (ret) {
1143f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1144f64122c1SDave Airlie 		return -ENOMEM;
1145f64122c1SDave Airlie 	}
1146f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
11472bd6ce84SDave Airlie 
1148545592fbSChristophe Fergeau 	ret = qxl_bo_pin(qdev->monitors_config_bo);
11492bd6ce84SDave Airlie 	if (ret)
11502bd6ce84SDave Airlie 		return ret;
11512bd6ce84SDave Airlie 
1152f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
11532bd6ce84SDave Airlie 
1154f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1155f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1156f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1157f64122c1SDave Airlie 
1158f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
115921c76bd1SGerd Hoffmann 	qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
116021c76bd1SGerd Hoffmann 				   GFP_KERNEL);
116190adda2cSGerd Hoffmann 	if (!qdev->dumb_heads) {
116290adda2cSGerd Hoffmann 		qxl_destroy_monitors_object(qdev);
116390adda2cSGerd Hoffmann 		return -ENOMEM;
116490adda2cSGerd Hoffmann 	}
11652bd6ce84SDave Airlie 	return 0;
11662bd6ce84SDave Airlie }
11672bd6ce84SDave Airlie 
11682bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
11692bd6ce84SDave Airlie {
11702bd6ce84SDave Airlie 	int ret;
11712bd6ce84SDave Airlie 
11722bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
11732bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
11742bd6ce84SDave Airlie 
11752bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
1176715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_unpin(qdev->monitors_config_bo);
11772bd6ce84SDave Airlie 	if (ret)
11782bd6ce84SDave Airlie 		return ret;
11792bd6ce84SDave Airlie 
11802bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
11812bd6ce84SDave Airlie 	return 0;
11822bd6ce84SDave Airlie }
11832bd6ce84SDave Airlie 
11842bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
11852bd6ce84SDave Airlie {
11862bd6ce84SDave Airlie 	int i;
11872bd6ce84SDave Airlie 	int ret;
11882bd6ce84SDave Airlie 
1189cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_init(&qdev->ddev);
11902bd6ce84SDave Airlie 
11912bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
11922bd6ce84SDave Airlie 	if (ret)
11932bd6ce84SDave Airlie 		return ret;
1194f64122c1SDave Airlie 
1195cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1196f64122c1SDave Airlie 
1197f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
11981277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_width = 0;
11991277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_height = 0;
1200cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_width = 8192;
1201cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_height = 8192;
1202f64122c1SDave Airlie 
1203cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.fb_base = qdev->vram_base;
12044695b039SDave Airlie 
1205cbdded7fSGabriel Krisman Bertazi 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
12064695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
12074695b039SDave Airlie 
120807f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1209cbdded7fSGabriel Krisman Bertazi 		qdev_crtc_init(&qdev->ddev, i);
1210cbdded7fSGabriel Krisman Bertazi 		qdev_output_init(&qdev->ddev, i);
1211f64122c1SDave Airlie 	}
1212f64122c1SDave Airlie 
1213c50fad8fSGerd Hoffmann 	qxl_display_read_client_monitors_config(qdev);
1214f64122c1SDave Airlie 
12159ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
1216f64122c1SDave Airlie 	return 0;
1217f64122c1SDave Airlie }
1218f64122c1SDave Airlie 
1219f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1220f64122c1SDave Airlie {
12212bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1222cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_cleanup(&qdev->ddev);
1223f64122c1SDave Airlie }
1224