xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision 1b043677d4be206c96b51811855502e50057f343)
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 	}
8321c76bd1SGerd Hoffmann 	if (num_monitors > qxl_num_crtc) {
845b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
8521c76bd1SGerd Hoffmann 			      qxl_num_crtc, num_monitors);
8621c76bd1SGerd Hoffmann 		num_monitors = qxl_num_crtc;
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 */
9921c76bd1SGerd Hoffmann 	qdev->client_monitors_config->max_allowed = qxl_num_crtc;
100f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
101f64122c1SDave Airlie 		struct qxl_urect *c_rect =
102f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
103f64122c1SDave Airlie 		struct qxl_head *client_head =
104f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
1059e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
10607f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1079e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1089e3b3178SChristophe Fergeau 		}
1099e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
11007f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1119e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1129e3b3178SChristophe Fergeau 		}
1139e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
11407f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1159e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1169e3b3178SChristophe Fergeau 		}
1179e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
11807f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1199e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1209e3b3178SChristophe Fergeau 		}
1219e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
12207f8d9bdSDave Airlie 			client_head->surface_id = 0;
1239e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1249e3b3178SChristophe Fergeau 		}
1259e3b3178SChristophe Fergeau 		if (client_head->id != i) {
12607f8d9bdSDave Airlie 			client_head->id = i;
1279e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1289e3b3178SChristophe Fergeau 		}
1299e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
13007f8d9bdSDave Airlie 			client_head->flags = 0;
1319e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1329e3b3178SChristophe Fergeau 		}
13307f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
13407f8d9bdSDave Airlie 			  client_head->x, client_head->y);
135f64122c1SDave Airlie 	}
1369e3b3178SChristophe Fergeau 
1379e3b3178SChristophe Fergeau 	return status;
138f64122c1SDave Airlie }
139f64122c1SDave Airlie 
1407dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1417dea0941SDave Airlie {
142cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1437dea0941SDave Airlie 	struct drm_connector *connector;
1447dea0941SDave Airlie 	struct qxl_output *output;
1457dea0941SDave Airlie 	struct qxl_head *head;
1467dea0941SDave Airlie 
1477dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1487dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1497dea0941SDave Airlie 
1507dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1517dea0941SDave Airlie 
1527dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1537dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1547dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1557dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1567dea0941SDave Airlie 	}
1577dea0941SDave Airlie }
1587dea0941SDave Airlie 
159f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
160f64122c1SDave Airlie {
161cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1629062155dSGerd Hoffmann 	int status, retries;
1639e3b3178SChristophe Fergeau 
1649062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1659e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1669062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1679062155dSGerd Hoffmann 			break;
1689062155dSGerd Hoffmann 		udelay(5);
1699062155dSGerd Hoffmann 	}
17066e0c8a5SAnton Vasilyev 	if (status == MONITORS_CONFIG_ERROR) {
17166e0c8a5SAnton Vasilyev 		DRM_DEBUG_KMS("ignoring client monitors config: error");
17266e0c8a5SAnton Vasilyev 		return;
17366e0c8a5SAnton Vasilyev 	}
1749062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1759062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1769062155dSGerd Hoffmann 		return;
1779e3b3178SChristophe Fergeau 	}
1789e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1799062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1809e3b3178SChristophe Fergeau 		return;
181f64122c1SDave Airlie 	}
1824fdb0869SMarc-André Lureau 
1837dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1847dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1857dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
186cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1874fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1884fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
189cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1904fdb0869SMarc-André Lureau 	}
191f64122c1SDave Airlie }
192f64122c1SDave Airlie 
193feba24deSGerd Hoffmann static int qxl_check_mode(struct qxl_device *qdev,
194feba24deSGerd Hoffmann 			  unsigned int width,
195feba24deSGerd Hoffmann 			  unsigned int height)
196feba24deSGerd Hoffmann {
197feba24deSGerd Hoffmann 	unsigned int stride;
198feba24deSGerd Hoffmann 	unsigned int size;
199feba24deSGerd Hoffmann 
200feba24deSGerd Hoffmann 	if (check_mul_overflow(width, 4u, &stride))
201feba24deSGerd Hoffmann 		return -EINVAL;
202feba24deSGerd Hoffmann 	if (check_mul_overflow(stride, height, &size))
203feba24deSGerd Hoffmann 		return -EINVAL;
204feba24deSGerd Hoffmann 	if (size > qdev->vram_size)
205feba24deSGerd Hoffmann 		return -ENOMEM;
206feba24deSGerd Hoffmann 	return 0;
207feba24deSGerd Hoffmann }
208feba24deSGerd Hoffmann 
209feba24deSGerd Hoffmann static int qxl_check_framebuffer(struct qxl_device *qdev,
210feba24deSGerd Hoffmann 				 struct qxl_bo *bo)
211feba24deSGerd Hoffmann {
212feba24deSGerd Hoffmann 	return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
213feba24deSGerd Hoffmann }
214feba24deSGerd Hoffmann 
215*1b043677SGerd Hoffmann static int qxl_add_mode(struct drm_connector *connector,
216*1b043677SGerd Hoffmann 			unsigned int width,
217*1b043677SGerd Hoffmann 			unsigned int height,
218*1b043677SGerd Hoffmann 			bool preferred)
219*1b043677SGerd Hoffmann {
220*1b043677SGerd Hoffmann 	struct drm_device *dev = connector->dev;
221*1b043677SGerd Hoffmann 	struct qxl_device *qdev = dev->dev_private;
222*1b043677SGerd Hoffmann 	struct drm_display_mode *mode = NULL;
223*1b043677SGerd Hoffmann 	int rc;
224*1b043677SGerd Hoffmann 
225*1b043677SGerd Hoffmann 	rc = qxl_check_mode(qdev, width, height);
226*1b043677SGerd Hoffmann 	if (rc != 0)
227*1b043677SGerd Hoffmann 		return 0;
228*1b043677SGerd Hoffmann 
229*1b043677SGerd Hoffmann 	mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
230*1b043677SGerd Hoffmann 	if (preferred)
231*1b043677SGerd Hoffmann 		mode->type |= DRM_MODE_TYPE_PREFERRED;
232*1b043677SGerd Hoffmann 	mode->hdisplay = width;
233*1b043677SGerd Hoffmann 	mode->vdisplay = height;
234*1b043677SGerd Hoffmann 	drm_mode_set_name(mode);
235*1b043677SGerd Hoffmann 	drm_mode_probed_add(connector, mode);
236*1b043677SGerd Hoffmann 	return 1;
237*1b043677SGerd Hoffmann }
238*1b043677SGerd Hoffmann 
239*1b043677SGerd Hoffmann static int qxl_add_monitors_config_modes(struct drm_connector *connector)
240f64122c1SDave Airlie {
241f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
242f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
243f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
244f64122c1SDave Airlie 	int h = output->index;
245f64122c1SDave Airlie 	struct qxl_head *head;
246f64122c1SDave Airlie 
2472d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
2482d856f94SGerd Hoffmann 		return 0;
24921c76bd1SGerd Hoffmann 	if (h >= qxl_num_crtc)
2502d856f94SGerd Hoffmann 		return 0;
25107f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
252f64122c1SDave Airlie 		return 0;
2532d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2542d856f94SGerd Hoffmann 		return 0;
2552d856f94SGerd Hoffmann 
25607f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2572d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
258f64122c1SDave Airlie 
259*1b043677SGerd Hoffmann 	return qxl_add_mode(connector, head->width, head->height, true);
260f64122c1SDave Airlie }
261f64122c1SDave Airlie 
262bd3e1c7cSJonathon Jongsma static struct mode_size {
263f64122c1SDave Airlie 	int w;
264f64122c1SDave Airlie 	int h;
265f64122c1SDave Airlie } common_modes[] = {
266f64122c1SDave Airlie 	{ 640,  480},
267f64122c1SDave Airlie 	{ 720,  480},
268f64122c1SDave Airlie 	{ 800,  600},
269f64122c1SDave Airlie 	{ 848,  480},
270f64122c1SDave Airlie 	{1024,  768},
271f64122c1SDave Airlie 	{1152,  768},
272f64122c1SDave Airlie 	{1280,  720},
273f64122c1SDave Airlie 	{1280,  800},
274f64122c1SDave Airlie 	{1280,  854},
275f64122c1SDave Airlie 	{1280,  960},
276f64122c1SDave Airlie 	{1280, 1024},
277f64122c1SDave Airlie 	{1440,  900},
278f64122c1SDave Airlie 	{1400, 1050},
279f64122c1SDave Airlie 	{1680, 1050},
280f64122c1SDave Airlie 	{1600, 1200},
281f64122c1SDave Airlie 	{1920, 1080},
282f64122c1SDave Airlie 	{1920, 1200}
283f64122c1SDave Airlie };
284f64122c1SDave Airlie 
285*1b043677SGerd Hoffmann static int qxl_add_common_modes(struct drm_connector *connector)
286bd3e1c7cSJonathon Jongsma {
287*1b043677SGerd Hoffmann 	int i, ret = 0;
288408799ebSShayenne da Luz Moura 
289*1b043677SGerd Hoffmann 	for (i = 0; i < ARRAY_SIZE(common_modes); i++)
290*1b043677SGerd Hoffmann 		ret += qxl_add_mode(connector,
291*1b043677SGerd Hoffmann 				    common_modes[i].w,
292*1b043677SGerd Hoffmann 				    common_modes[i].h,
293*1b043677SGerd Hoffmann 				    false);
294*1b043677SGerd Hoffmann 	return ret;
295f64122c1SDave Airlie }
296f64122c1SDave Airlie 
297998010bfSGerd Hoffmann static void qxl_send_monitors_config(struct qxl_device *qdev)
298998010bfSGerd Hoffmann {
299998010bfSGerd Hoffmann 	int i;
300998010bfSGerd Hoffmann 
301998010bfSGerd Hoffmann 	BUG_ON(!qdev->ram_header->monitors_config);
302998010bfSGerd Hoffmann 
303998010bfSGerd Hoffmann 	if (qdev->monitors_config->count == 0)
304998010bfSGerd Hoffmann 		return;
305998010bfSGerd Hoffmann 
306998010bfSGerd Hoffmann 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
307998010bfSGerd Hoffmann 		struct qxl_head *head = &qdev->monitors_config->heads[i];
308998010bfSGerd Hoffmann 
309998010bfSGerd Hoffmann 		if (head->y > 8192 || head->x > 8192 ||
310998010bfSGerd Hoffmann 		    head->width > 8192 || head->height > 8192) {
311998010bfSGerd Hoffmann 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
312998010bfSGerd Hoffmann 				  i, head->width, head->height,
313998010bfSGerd Hoffmann 				  head->x, head->y);
314998010bfSGerd Hoffmann 			return;
315998010bfSGerd Hoffmann 		}
316998010bfSGerd Hoffmann 	}
317998010bfSGerd Hoffmann 	qxl_io_monitors_config(qdev);
318998010bfSGerd Hoffmann }
319998010bfSGerd Hoffmann 
320a6d3c4d7SGerd Hoffmann static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
321a6d3c4d7SGerd Hoffmann 					    const char *reason)
322a6d3c4d7SGerd Hoffmann {
323a6d3c4d7SGerd Hoffmann 	struct drm_device *dev = crtc->dev;
324a6d3c4d7SGerd Hoffmann 	struct qxl_device *qdev = dev->dev_private;
325a6d3c4d7SGerd Hoffmann 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
326a6d3c4d7SGerd Hoffmann 	struct qxl_head head;
327a6d3c4d7SGerd Hoffmann 	int oldcount, i = qcrtc->index;
328a6d3c4d7SGerd Hoffmann 
32916620544SGerd Hoffmann 	if (!qdev->primary_bo) {
330a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
331a6d3c4d7SGerd Hoffmann 		return;
332a6d3c4d7SGerd Hoffmann 	}
333a6d3c4d7SGerd Hoffmann 
33421c76bd1SGerd Hoffmann 	if (!qdev->monitors_config || qxl_num_crtc <= i)
335a6d3c4d7SGerd Hoffmann 		return;
336a6d3c4d7SGerd Hoffmann 
337a6d3c4d7SGerd Hoffmann 	head.id = i;
338a6d3c4d7SGerd Hoffmann 	head.flags = 0;
339a6d3c4d7SGerd Hoffmann 	oldcount = qdev->monitors_config->count;
340a6d3c4d7SGerd Hoffmann 	if (crtc->state->active) {
341a6d3c4d7SGerd Hoffmann 		struct drm_display_mode *mode = &crtc->mode;
342408799ebSShayenne da Luz Moura 
343a6d3c4d7SGerd Hoffmann 		head.width = mode->hdisplay;
344a6d3c4d7SGerd Hoffmann 		head.height = mode->vdisplay;
345a6d3c4d7SGerd Hoffmann 		head.x = crtc->x;
346a6d3c4d7SGerd Hoffmann 		head.y = crtc->y;
347a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count < i + 1)
348a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i + 1;
34990adda2cSGerd Hoffmann 		if (qdev->primary_bo == qdev->dumb_shadow_bo)
35090adda2cSGerd Hoffmann 			head.x += qdev->dumb_heads[i].x;
351a6d3c4d7SGerd Hoffmann 	} else if (i > 0) {
352a6d3c4d7SGerd Hoffmann 		head.width = 0;
353a6d3c4d7SGerd Hoffmann 		head.height = 0;
354a6d3c4d7SGerd Hoffmann 		head.x = 0;
355a6d3c4d7SGerd Hoffmann 		head.y = 0;
356a6d3c4d7SGerd Hoffmann 		if (qdev->monitors_config->count == i + 1)
357a6d3c4d7SGerd Hoffmann 			qdev->monitors_config->count = i;
358a6d3c4d7SGerd Hoffmann 	} else {
359a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
360a6d3c4d7SGerd Hoffmann 		return;
361a6d3c4d7SGerd Hoffmann 	}
362a6d3c4d7SGerd Hoffmann 
363a6d3c4d7SGerd Hoffmann 	if (head.width  == qdev->monitors_config->heads[i].width  &&
364a6d3c4d7SGerd Hoffmann 	    head.height == qdev->monitors_config->heads[i].height &&
365a6d3c4d7SGerd Hoffmann 	    head.x      == qdev->monitors_config->heads[i].x      &&
366a6d3c4d7SGerd Hoffmann 	    head.y      == qdev->monitors_config->heads[i].y      &&
367a6d3c4d7SGerd Hoffmann 	    oldcount    == qdev->monitors_config->count)
368a6d3c4d7SGerd Hoffmann 		return;
369a6d3c4d7SGerd Hoffmann 
370a6d3c4d7SGerd Hoffmann 	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
371a6d3c4d7SGerd Hoffmann 		      i, head.width, head.height, head.x, head.y,
372a6d3c4d7SGerd Hoffmann 		      crtc->state->active ? "on" : "off", reason);
373a6d3c4d7SGerd Hoffmann 	if (oldcount != qdev->monitors_config->count)
374a6d3c4d7SGerd Hoffmann 		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
375a6d3c4d7SGerd Hoffmann 			      oldcount, qdev->monitors_config->count,
37621c76bd1SGerd Hoffmann 			      qxl_num_crtc);
377a6d3c4d7SGerd Hoffmann 
378a6d3c4d7SGerd Hoffmann 	qdev->monitors_config->heads[i] = head;
37921c76bd1SGerd Hoffmann 	qdev->monitors_config->max_allowed = qxl_num_crtc;
380a6d3c4d7SGerd Hoffmann 	qxl_send_monitors_config(qdev);
381a6d3c4d7SGerd Hoffmann }
382a6d3c4d7SGerd Hoffmann 
383c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
384c2ff6632SGabriel Krisman Bertazi 				  struct drm_crtc_state *old_crtc_state)
385c2ff6632SGabriel Krisman Bertazi {
386c2ff6632SGabriel Krisman Bertazi 	struct drm_device *dev = crtc->dev;
387c2ff6632SGabriel Krisman Bertazi 	struct drm_pending_vblank_event *event;
388c2ff6632SGabriel Krisman Bertazi 	unsigned long flags;
389c2ff6632SGabriel Krisman Bertazi 
390c2ff6632SGabriel Krisman Bertazi 	if (crtc->state && crtc->state->event) {
391c2ff6632SGabriel Krisman Bertazi 		event = crtc->state->event;
392c2ff6632SGabriel Krisman Bertazi 		crtc->state->event = NULL;
393c2ff6632SGabriel Krisman Bertazi 
394c2ff6632SGabriel Krisman Bertazi 		spin_lock_irqsave(&dev->event_lock, flags);
395c2ff6632SGabriel Krisman Bertazi 		drm_crtc_send_vblank_event(crtc, event);
396c2ff6632SGabriel Krisman Bertazi 		spin_unlock_irqrestore(&dev->event_lock, flags);
397c2ff6632SGabriel Krisman Bertazi 	}
398a6d3c4d7SGerd Hoffmann 
399a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "flush");
400c2ff6632SGabriel Krisman Bertazi }
401c2ff6632SGabriel Krisman Bertazi 
402f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
403f64122c1SDave Airlie {
404f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
405f64122c1SDave Airlie 
4069428088cSRay Strode 	qxl_bo_unref(&qxl_crtc->cursor_bo);
407f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
408f64122c1SDave Airlie 	kfree(qxl_crtc);
409f64122c1SDave Airlie }
410f64122c1SDave Airlie 
411f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
412bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
413f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
4149973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
4159ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
4169ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
4179ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
418f64122c1SDave Airlie };
419f64122c1SDave Airlie 
4206d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
421f64122c1SDave Airlie 					 struct drm_file *file_priv,
4221b000494SShayenne da Luz Moura 					 unsigned int flags, unsigned int color,
423f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
4241b000494SShayenne da Luz Moura 					 unsigned int num_clips)
425f64122c1SDave Airlie {
426f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
427bf8744e4SPeter Wu 	struct qxl_device *qdev = fb->dev->dev_private;
428f64122c1SDave Airlie 	struct drm_clip_rect norect;
429f64122c1SDave Airlie 	struct qxl_bo *qobj;
4304979904cSGerd Hoffmann 	bool is_primary;
431f64122c1SDave Airlie 	int inc = 1;
432f64122c1SDave Airlie 
43373e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
43473e9efd4SVille Syrjälä 
435bf8744e4SPeter Wu 	qobj = gem_to_qxl_bo(fb->obj[0]);
436b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
4374979904cSGerd Hoffmann 	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
4384979904cSGerd Hoffmann 	if (!is_primary) {
43973e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
440b2b4465dSDave Airlie 		return 0;
44173e9efd4SVille Syrjälä 	}
442b2b4465dSDave Airlie 
443f64122c1SDave Airlie 	if (!num_clips) {
444f64122c1SDave Airlie 		num_clips = 1;
445f64122c1SDave Airlie 		clips = &norect;
446f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
447f64122c1SDave Airlie 		norect.x2 = fb->width;
448f64122c1SDave Airlie 		norect.y2 = fb->height;
449f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
450f64122c1SDave Airlie 		num_clips /= 2;
451f64122c1SDave Airlie 		inc = 2; /* skip source rects */
452f64122c1SDave Airlie 	}
453f64122c1SDave Airlie 
454bf8744e4SPeter Wu 	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
45590adda2cSGerd Hoffmann 			  clips, num_clips, inc, 0);
45673e9efd4SVille Syrjälä 
45773e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
45873e9efd4SVille Syrjälä 
459f64122c1SDave Airlie 	return 0;
460f64122c1SDave Airlie }
461f64122c1SDave Airlie 
462f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
463bf8744e4SPeter Wu 	.destroy = drm_gem_fb_destroy,
464f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
465bf8744e4SPeter Wu 	.create_handle = drm_gem_fb_create_handle,
466f64122c1SDave Airlie };
467f64122c1SDave Airlie 
4680b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
4690b20a0f8SLaurent Pinchart 				   struct drm_crtc_state *old_state)
470f64122c1SDave Airlie {
471a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "enable");
472f64122c1SDave Airlie }
473f64122c1SDave Airlie 
47464581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
47564581714SLaurent Pinchart 				    struct drm_crtc_state *old_state)
47607f8d9bdSDave Airlie {
477a6d3c4d7SGerd Hoffmann 	qxl_crtc_update_monitors_config(crtc, "disable");
47807f8d9bdSDave Airlie }
47907f8d9bdSDave Airlie 
480f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
481c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4820b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
48364581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
484f64122c1SDave Airlie };
485f64122c1SDave Airlie 
48645dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
487c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
488c2ff6632SGabriel Krisman Bertazi {
489c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
490c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
491c2ff6632SGabriel Krisman Bertazi 
492c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
493c2ff6632SGabriel Krisman Bertazi 		return 0;
494c2ff6632SGabriel Krisman Bertazi 
495bf8744e4SPeter Wu 	bo = gem_to_qxl_bo(state->fb->obj[0]);
496c2ff6632SGabriel Krisman Bertazi 
497feba24deSGerd Hoffmann 	return qxl_check_framebuffer(qdev, bo);
498c2ff6632SGabriel Krisman Bertazi }
499c2ff6632SGabriel Krisman Bertazi 
5009428088cSRay Strode static int qxl_primary_apply_cursor(struct drm_plane *plane)
5019428088cSRay Strode {
5029428088cSRay Strode 	struct drm_device *dev = plane->dev;
5039428088cSRay Strode 	struct qxl_device *qdev = dev->dev_private;
5049428088cSRay Strode 	struct drm_framebuffer *fb = plane->state->fb;
5059428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
5069428088cSRay Strode 	struct qxl_cursor_cmd *cmd;
5079428088cSRay Strode 	struct qxl_release *release;
5089428088cSRay Strode 	int ret = 0;
5099428088cSRay Strode 
5109428088cSRay Strode 	if (!qcrtc->cursor_bo)
5119428088cSRay Strode 		return 0;
5129428088cSRay Strode 
5139428088cSRay Strode 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5149428088cSRay Strode 					 QXL_RELEASE_CURSOR_CMD,
5159428088cSRay Strode 					 &release, NULL);
5169428088cSRay Strode 	if (ret)
5179428088cSRay Strode 		return ret;
5189428088cSRay Strode 
5199428088cSRay Strode 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
5209428088cSRay Strode 	if (ret)
5219428088cSRay Strode 		goto out_free_release;
5229428088cSRay Strode 
5239428088cSRay Strode 	ret = qxl_release_reserve_list(release, false);
5249428088cSRay Strode 	if (ret)
5259428088cSRay Strode 		goto out_free_release;
5269428088cSRay Strode 
5279428088cSRay Strode 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5289428088cSRay Strode 	cmd->type = QXL_CURSOR_SET;
5299428088cSRay Strode 	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
5309428088cSRay Strode 	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
5319428088cSRay Strode 
5329428088cSRay Strode 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5339428088cSRay Strode 
5349428088cSRay Strode 	cmd->u.set.visible = 1;
5359428088cSRay Strode 	qxl_release_unmap(qdev, release, &cmd->release_info);
5369428088cSRay Strode 
5379428088cSRay Strode 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5389428088cSRay Strode 	qxl_release_fence_buffer_objects(release);
5399428088cSRay Strode 
5409428088cSRay Strode 	return ret;
5419428088cSRay Strode 
5429428088cSRay Strode out_free_release:
5439428088cSRay Strode 	qxl_release_free(qdev, release);
5449428088cSRay Strode 	return ret;
5459428088cSRay Strode }
5469428088cSRay Strode 
547c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
548c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
549c2ff6632SGabriel Krisman Bertazi {
550c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
551bf8744e4SPeter Wu 	struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
5524979904cSGerd Hoffmann 	struct qxl_bo *bo_old, *primary;
553c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
554c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
555c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
556bf8744e4SPeter Wu 	    .x2 = plane->state->fb->width,
557bf8744e4SPeter Wu 	    .y2 = plane->state->fb->height
558c2ff6632SGabriel Krisman Bertazi 	};
55990adda2cSGerd Hoffmann 	uint32_t dumb_shadow_offset = 0;
560c2ff6632SGabriel Krisman Bertazi 
561b0e07da3SGerd Hoffmann 	if (old_state->fb) {
562bf8744e4SPeter Wu 		bo_old = gem_to_qxl_bo(old_state->fb->obj[0]);
563b0e07da3SGerd Hoffmann 	} else {
564b0e07da3SGerd Hoffmann 		bo_old = NULL;
565b0e07da3SGerd Hoffmann 	}
566b0e07da3SGerd Hoffmann 
5674979904cSGerd Hoffmann 	primary = bo->shadow ? bo->shadow : bo;
568b0e07da3SGerd Hoffmann 
5694979904cSGerd Hoffmann 	if (!primary->is_primary) {
5704979904cSGerd Hoffmann 		if (qdev->primary_bo)
571b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
5724979904cSGerd Hoffmann 		qxl_io_create_primary(qdev, primary);
5731f85535cSGerd Hoffmann 		qxl_primary_apply_cursor(plane);
5741f85535cSGerd Hoffmann 	}
57562676d10SGerd Hoffmann 
57690adda2cSGerd Hoffmann 	if (bo->is_dumb)
57790adda2cSGerd Hoffmann 		dumb_shadow_offset =
57890adda2cSGerd Hoffmann 			qdev->dumb_heads[plane->state->crtc->index].x;
57990adda2cSGerd Hoffmann 
58090adda2cSGerd Hoffmann 	qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
58190adda2cSGerd Hoffmann 			  dumb_shadow_offset);
582c2ff6632SGabriel Krisman Bertazi }
583c2ff6632SGabriel Krisman Bertazi 
584c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
585c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
586c2ff6632SGabriel Krisman Bertazi {
587c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
588c2ff6632SGabriel Krisman Bertazi 
589b0e07da3SGerd Hoffmann 	if (old_state->fb) {
590bf8744e4SPeter Wu 		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
591c2ff6632SGabriel Krisman Bertazi 
592b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
593c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
594c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
595c2ff6632SGabriel Krisman Bertazi 		}
596c2ff6632SGabriel Krisman Bertazi 	}
597b0e07da3SGerd Hoffmann }
598c2ff6632SGabriel Krisman Bertazi 
5991277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
6001277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
6011277eed5SGabriel Krisman Bertazi {
6021277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
6031277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
6041277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
6059428088cSRay Strode 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
6061277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6071277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6081277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
6091277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
610889ad63dSJeremy Cline 	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
6111277eed5SGabriel Krisman Bertazi 	int ret;
6121277eed5SGabriel Krisman Bertazi 	void *user_ptr;
6131277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
6141277eed5SGabriel Krisman Bertazi 
6151277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6161277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
6171277eed5SGabriel Krisman Bertazi 					 &release, NULL);
618ee5cb7c4SDan Carpenter 	if (ret)
619ee5cb7c4SDan Carpenter 		return;
6201277eed5SGabriel Krisman Bertazi 
6211277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
622bf8744e4SPeter Wu 		obj = fb->obj[0];
6231277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
6241277eed5SGabriel Krisman Bertazi 
6251277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
6261277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
6271277eed5SGabriel Krisman Bertazi 		if (ret)
6281277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6291277eed5SGabriel Krisman Bertazi 
6301277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
6311277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6321277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6331277eed5SGabriel Krisman Bertazi 		if (ret)
6341277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6351277eed5SGabriel Krisman Bertazi 
6360081cdfeSChristophe Fergeau 		ret = qxl_bo_pin(cursor_bo);
6371277eed5SGabriel Krisman Bertazi 		if (ret)
6381277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6391277eed5SGabriel Krisman Bertazi 
6400081cdfeSChristophe Fergeau 		ret = qxl_release_reserve_list(release, true);
6410081cdfeSChristophe Fergeau 		if (ret)
6420081cdfeSChristophe Fergeau 			goto out_unpin;
6430081cdfeSChristophe Fergeau 
6441277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6451277eed5SGabriel Krisman Bertazi 		if (ret)
6461277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6471277eed5SGabriel Krisman Bertazi 
6481277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6491277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6501277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6511277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6521277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6531277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6541277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6551277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6561277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6571277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6581277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6591277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6601277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6611277eed5SGabriel Krisman Bertazi 
662429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6631277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6641277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6651277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6661277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6679428088cSRay Strode 
668889ad63dSJeremy Cline 		old_cursor_bo = qcrtc->cursor_bo;
6699428088cSRay Strode 		qcrtc->cursor_bo = cursor_bo;
6709428088cSRay Strode 		cursor_bo = NULL;
6711277eed5SGabriel Krisman Bertazi 	} else {
6721277eed5SGabriel Krisman Bertazi 
6731277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6741277eed5SGabriel Krisman Bertazi 		if (ret)
6751277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6761277eed5SGabriel Krisman Bertazi 
677429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6781277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
6791277eed5SGabriel Krisman Bertazi 	}
6801277eed5SGabriel Krisman Bertazi 
6811277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
6821277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
6831277eed5SGabriel Krisman Bertazi 
6841277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
6851277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
6861277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
6871277eed5SGabriel Krisman Bertazi 
6880081cdfeSChristophe Fergeau 	if (old_cursor_bo != NULL)
6890081cdfeSChristophe Fergeau 		qxl_bo_unpin(old_cursor_bo);
690889ad63dSJeremy Cline 	qxl_bo_unref(&old_cursor_bo);
69116c6db36SRay Strode 	qxl_bo_unref(&cursor_bo);
69216c6db36SRay Strode 
6931277eed5SGabriel Krisman Bertazi 	return;
6941277eed5SGabriel Krisman Bertazi 
6951277eed5SGabriel Krisman Bertazi out_backoff:
6961277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
6970081cdfeSChristophe Fergeau out_unpin:
6980081cdfeSChristophe Fergeau 	qxl_bo_unpin(cursor_bo);
6991277eed5SGabriel Krisman Bertazi out_free_bo:
7001277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
7011277eed5SGabriel Krisman Bertazi out_kunmap:
7021277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
7031277eed5SGabriel Krisman Bertazi out_free_release:
7041277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
7051277eed5SGabriel Krisman Bertazi 	return;
7061277eed5SGabriel Krisman Bertazi 
7071277eed5SGabriel Krisman Bertazi }
7081277eed5SGabriel Krisman Bertazi 
70945dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
7101277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
7111277eed5SGabriel Krisman Bertazi {
7121277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
7131277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
7141277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
7151277eed5SGabriel Krisman Bertazi 	int ret;
7161277eed5SGabriel Krisman Bertazi 
7171277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
7181277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
7191277eed5SGabriel Krisman Bertazi 					 &release, NULL);
7201277eed5SGabriel Krisman Bertazi 	if (ret)
7211277eed5SGabriel Krisman Bertazi 		return;
7221277eed5SGabriel Krisman Bertazi 
7231277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
7241277eed5SGabriel Krisman Bertazi 	if (ret) {
7251277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
7261277eed5SGabriel Krisman Bertazi 		return;
7271277eed5SGabriel Krisman Bertazi 	}
7281277eed5SGabriel Krisman Bertazi 
7291277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7301277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
7311277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
7321277eed5SGabriel Krisman Bertazi 
7331277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
7341277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
7351277eed5SGabriel Krisman Bertazi }
7361277eed5SGabriel Krisman Bertazi 
73790adda2cSGerd Hoffmann static void qxl_update_dumb_head(struct qxl_device *qdev,
73890adda2cSGerd Hoffmann 				 int index, struct qxl_bo *bo)
73990adda2cSGerd Hoffmann {
74090adda2cSGerd Hoffmann 	uint32_t width, height;
74190adda2cSGerd Hoffmann 
74290adda2cSGerd Hoffmann 	if (index >= qdev->monitors_config->max_allowed)
74390adda2cSGerd Hoffmann 		return;
74490adda2cSGerd Hoffmann 
74590adda2cSGerd Hoffmann 	if (bo && bo->is_dumb) {
74690adda2cSGerd Hoffmann 		width = bo->surf.width;
74790adda2cSGerd Hoffmann 		height = bo->surf.height;
74890adda2cSGerd Hoffmann 	} else {
74990adda2cSGerd Hoffmann 		width = 0;
75090adda2cSGerd Hoffmann 		height = 0;
75190adda2cSGerd Hoffmann 	}
75290adda2cSGerd Hoffmann 
75390adda2cSGerd Hoffmann 	if (qdev->dumb_heads[index].width == width &&
75490adda2cSGerd Hoffmann 	    qdev->dumb_heads[index].height == height)
75590adda2cSGerd Hoffmann 		return;
75690adda2cSGerd Hoffmann 
75790adda2cSGerd Hoffmann 	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
75890adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].width,
75990adda2cSGerd Hoffmann 		  qdev->dumb_heads[index].height,
76090adda2cSGerd Hoffmann 		  width, height);
76190adda2cSGerd Hoffmann 	qdev->dumb_heads[index].width = width;
76290adda2cSGerd Hoffmann 	qdev->dumb_heads[index].height = height;
76390adda2cSGerd Hoffmann }
76490adda2cSGerd Hoffmann 
76590adda2cSGerd Hoffmann static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
76690adda2cSGerd Hoffmann 				 struct qxl_surface *surf)
76790adda2cSGerd Hoffmann {
76890adda2cSGerd Hoffmann 	struct qxl_head *head;
76990adda2cSGerd Hoffmann 	int i;
77090adda2cSGerd Hoffmann 
77190adda2cSGerd Hoffmann 	memset(surf, 0, sizeof(*surf));
77290adda2cSGerd Hoffmann 	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
77390adda2cSGerd Hoffmann 		head = qdev->dumb_heads + i;
77490adda2cSGerd Hoffmann 		head->x = surf->width;
77590adda2cSGerd Hoffmann 		surf->width += head->width;
77690adda2cSGerd Hoffmann 		if (surf->height < head->height)
77790adda2cSGerd Hoffmann 			surf->height = head->height;
77890adda2cSGerd Hoffmann 	}
77990adda2cSGerd Hoffmann 	if (surf->width < 64)
78090adda2cSGerd Hoffmann 		surf->width = 64;
78190adda2cSGerd Hoffmann 	if (surf->height < 64)
78290adda2cSGerd Hoffmann 		surf->height = 64;
78390adda2cSGerd Hoffmann 	surf->format = SPICE_SURFACE_FMT_32_xRGB;
78490adda2cSGerd Hoffmann 	surf->stride = surf->width * 4;
78590adda2cSGerd Hoffmann 
78690adda2cSGerd Hoffmann 	if (!qdev->dumb_shadow_bo ||
78790adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.width != surf->width ||
78890adda2cSGerd Hoffmann 	    qdev->dumb_shadow_bo->surf.height != surf->height)
78990adda2cSGerd Hoffmann 		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
79090adda2cSGerd Hoffmann }
79190adda2cSGerd Hoffmann 
79245dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
7931277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
7941277eed5SGabriel Krisman Bertazi {
79562676d10SGerd Hoffmann 	struct qxl_device *qdev = plane->dev->dev_private;
7961277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
79790adda2cSGerd Hoffmann 	struct qxl_bo *user_bo;
79890adda2cSGerd Hoffmann 	struct qxl_surface surf;
7991277eed5SGabriel Krisman Bertazi 	int ret;
8001277eed5SGabriel Krisman Bertazi 
8011277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
8021277eed5SGabriel Krisman Bertazi 		return 0;
8031277eed5SGabriel Krisman Bertazi 
804bf8744e4SPeter Wu 	obj = new_state->fb->obj[0];
8051277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8061277eed5SGabriel Krisman Bertazi 
80762676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
80890adda2cSGerd Hoffmann 	    user_bo->is_dumb) {
80990adda2cSGerd Hoffmann 		qxl_update_dumb_head(qdev, new_state->crtc->index,
81090adda2cSGerd Hoffmann 				     user_bo);
81190adda2cSGerd Hoffmann 		qxl_calc_dumb_shadow(qdev, &surf);
81290adda2cSGerd Hoffmann 		if (!qdev->dumb_shadow_bo ||
81390adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.width  != surf.width ||
81490adda2cSGerd Hoffmann 		    qdev->dumb_shadow_bo->surf.height != surf.height) {
81590adda2cSGerd Hoffmann 			if (qdev->dumb_shadow_bo) {
81690adda2cSGerd Hoffmann 				drm_gem_object_put_unlocked
81790adda2cSGerd Hoffmann 					(&qdev->dumb_shadow_bo->gem_base);
81890adda2cSGerd Hoffmann 				qdev->dumb_shadow_bo = NULL;
81962676d10SGerd Hoffmann 			}
82090adda2cSGerd Hoffmann 			qxl_bo_create(qdev, surf.height * surf.stride,
82190adda2cSGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
82290adda2cSGerd Hoffmann 				      &qdev->dumb_shadow_bo);
82390adda2cSGerd Hoffmann 		}
82490adda2cSGerd Hoffmann 		if (user_bo->shadow != qdev->dumb_shadow_bo) {
82590adda2cSGerd Hoffmann 			if (user_bo->shadow) {
82690adda2cSGerd Hoffmann 				drm_gem_object_put_unlocked
82790adda2cSGerd Hoffmann 					(&user_bo->shadow->gem_base);
82890adda2cSGerd Hoffmann 				user_bo->shadow = NULL;
82990adda2cSGerd Hoffmann 			}
83090adda2cSGerd Hoffmann 			drm_gem_object_get(&qdev->dumb_shadow_bo->gem_base);
83190adda2cSGerd Hoffmann 			user_bo->shadow = qdev->dumb_shadow_bo;
83262676d10SGerd Hoffmann 		}
83362676d10SGerd Hoffmann 	}
83462676d10SGerd Hoffmann 
835545592fbSChristophe Fergeau 	ret = qxl_bo_pin(user_bo);
8361277eed5SGabriel Krisman Bertazi 	if (ret)
8371277eed5SGabriel Krisman Bertazi 		return ret;
8381277eed5SGabriel Krisman Bertazi 
8391277eed5SGabriel Krisman Bertazi 	return 0;
8401277eed5SGabriel Krisman Bertazi }
8411277eed5SGabriel Krisman Bertazi 
8421277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8431277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
8441277eed5SGabriel Krisman Bertazi {
8451277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
8461277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
8471277eed5SGabriel Krisman Bertazi 
8485f3d862aSGerd Hoffmann 	if (!old_state->fb) {
8495f3d862aSGerd Hoffmann 		/*
8505f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
8511277eed5SGabriel Krisman Bertazi 		 * unpin.
8521277eed5SGabriel Krisman Bertazi 		 */
8531277eed5SGabriel Krisman Bertazi 		return;
8541277eed5SGabriel Krisman Bertazi 	}
8551277eed5SGabriel Krisman Bertazi 
856bf8744e4SPeter Wu 	obj = old_state->fb->obj[0];
8571277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
8581277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
85962676d10SGerd Hoffmann 
86090adda2cSGerd Hoffmann 	if (old_state->fb != plane->state->fb && user_bo->shadow) {
86162676d10SGerd Hoffmann 		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
86262676d10SGerd Hoffmann 		user_bo->shadow = NULL;
86362676d10SGerd Hoffmann 	}
8641277eed5SGabriel Krisman Bertazi }
8651277eed5SGabriel Krisman Bertazi 
8661277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8671277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
8681277eed5SGabriel Krisman Bertazi };
8691277eed5SGabriel Krisman Bertazi 
8701277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8711277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
8721277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
8731277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
8741277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
8751277eed5SGabriel Krisman Bertazi };
8761277eed5SGabriel Krisman Bertazi 
8771277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
878472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
879472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
8801277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
8819ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8829ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8839ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
8841277eed5SGabriel Krisman Bertazi };
8851277eed5SGabriel Krisman Bertazi 
886d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
887d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
888d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
889d3e7e42dSGabriel Krisman Bertazi };
890d3e7e42dSGabriel Krisman Bertazi 
891c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
892c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
893c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
894c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
895c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
896c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
897c2ff6632SGabriel Krisman Bertazi };
898c2ff6632SGabriel Krisman Bertazi 
899d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
900472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
901472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
902d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
9039ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
9049ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
9059ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
906d3e7e42dSGabriel Krisman Bertazi };
907d3e7e42dSGabriel Krisman Bertazi 
908d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
909d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
910d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
911d3e7e42dSGabriel Krisman Bertazi {
912d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
913d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
914d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
915d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
916d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
917d3e7e42dSGabriel Krisman Bertazi 	int err;
918d3e7e42dSGabriel Krisman Bertazi 
919d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
920d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
921d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
922d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
923c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
9241277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
9251277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
9261277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
9271277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
9281277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
929d3e7e42dSGabriel Krisman Bertazi 	} else {
930d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
931d3e7e42dSGabriel Krisman Bertazi 	}
932d3e7e42dSGabriel Krisman Bertazi 
933d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
934d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
935d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
936d3e7e42dSGabriel Krisman Bertazi 
937d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
938d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
939e6fc3b68SBen Widawsky 				       NULL, type, NULL);
940d3e7e42dSGabriel Krisman Bertazi 	if (err)
941d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
942d3e7e42dSGabriel Krisman Bertazi 
943d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
944d3e7e42dSGabriel Krisman Bertazi 
945d3e7e42dSGabriel Krisman Bertazi 	return plane;
946d3e7e42dSGabriel Krisman Bertazi 
947d3e7e42dSGabriel Krisman Bertazi free_plane:
948d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
949d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
950d3e7e42dSGabriel Krisman Bertazi }
951d3e7e42dSGabriel Krisman Bertazi 
95207f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
953f64122c1SDave Airlie {
954f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
9551277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
956d3e7e42dSGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
957d3e7e42dSGabriel Krisman Bertazi 	int r;
958f64122c1SDave Airlie 
959f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
960f64122c1SDave Airlie 	if (!qxl_crtc)
961f64122c1SDave Airlie 		return -ENOMEM;
962f64122c1SDave Airlie 
963d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
964d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
965d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
966d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
967d3e7e42dSGabriel Krisman Bertazi 	}
968d3e7e42dSGabriel Krisman Bertazi 
9691277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9701277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
9711277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
9721277eed5SGabriel Krisman Bertazi 		goto clean_primary;
9731277eed5SGabriel Krisman Bertazi 	}
9741277eed5SGabriel Krisman Bertazi 
9751277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
976d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
977d3e7e42dSGabriel Krisman Bertazi 	if (r)
9781277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
979d3e7e42dSGabriel Krisman Bertazi 
98007f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
981f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
982f64122c1SDave Airlie 	return 0;
983d3e7e42dSGabriel Krisman Bertazi 
9841277eed5SGabriel Krisman Bertazi clean_cursor:
9851277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
9861277eed5SGabriel Krisman Bertazi 	kfree(cursor);
987d3e7e42dSGabriel Krisman Bertazi clean_primary:
988d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
989d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
990d3e7e42dSGabriel Krisman Bertazi free_mem:
991d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
992d3e7e42dSGabriel Krisman Bertazi 	return r;
993f64122c1SDave Airlie }
994f64122c1SDave Airlie 
995f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
996f64122c1SDave Airlie {
997*1b043677SGerd Hoffmann 	struct drm_device *dev = connector->dev;
998*1b043677SGerd Hoffmann 	struct qxl_device *qdev = dev->dev_private;
999*1b043677SGerd Hoffmann 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
10001b000494SShayenne da Luz Moura 	unsigned int pwidth = 1024;
10011b000494SShayenne da Luz Moura 	unsigned int pheight = 768;
10022d856f94SGerd Hoffmann 	int ret = 0;
1003f64122c1SDave Airlie 
1004*1b043677SGerd Hoffmann 	if (qdev->client_monitors_config) {
1005*1b043677SGerd Hoffmann 		struct qxl_head *head;
1006*1b043677SGerd Hoffmann 		head = &qdev->client_monitors_config->heads[output->index];
1007*1b043677SGerd Hoffmann 		if (head->width)
1008*1b043677SGerd Hoffmann 			pwidth = head->width;
1009*1b043677SGerd Hoffmann 		if (head->height)
1010*1b043677SGerd Hoffmann 			pheight = head->height;
1011*1b043677SGerd Hoffmann 	}
1012*1b043677SGerd Hoffmann 
1013*1b043677SGerd Hoffmann 	ret += qxl_add_common_modes(connector);
1014*1b043677SGerd Hoffmann 	ret += qxl_add_monitors_config_modes(connector);
1015*1b043677SGerd Hoffmann 	drm_set_preferred_mode(connector, pwidth, pheight);
1016f64122c1SDave Airlie 	return ret;
1017f64122c1SDave Airlie }
1018f64122c1SDave Airlie 
1019e0d92e16SLuc Van Oostenryck static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1020f64122c1SDave Airlie 			       struct drm_display_mode *mode)
1021f64122c1SDave Airlie {
1022bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
1023bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
1024bd3e1c7cSJonathon Jongsma 
1025feba24deSGerd Hoffmann 	if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1026bd3e1c7cSJonathon Jongsma 		return MODE_BAD;
1027feba24deSGerd Hoffmann 
1028feba24deSGerd Hoffmann 	return MODE_OK;
1029f64122c1SDave Airlie }
1030f64122c1SDave Airlie 
10316d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1032f64122c1SDave Airlie {
1033f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1034f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1035f64122c1SDave Airlie 
1036f64122c1SDave Airlie 	DRM_DEBUG("\n");
1037f64122c1SDave Airlie 	return &qxl_output->enc;
1038f64122c1SDave Airlie }
1039f64122c1SDave Airlie 
1040f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1041f64122c1SDave Airlie };
1042f64122c1SDave Airlie 
1043f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1044f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1045f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1046f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1047f64122c1SDave Airlie };
1048f64122c1SDave Airlie 
1049f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1050f64122c1SDave Airlie 			struct drm_connector *connector,
1051f64122c1SDave Airlie 			bool force)
1052f64122c1SDave Airlie {
1053f64122c1SDave Airlie 	struct qxl_output *output =
1054f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1055f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1056f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
105769e5d3f8SDave Airlie 	bool connected = false;
1058f64122c1SDave Airlie 
1059f64122c1SDave Airlie 	/* The first monitor is always connected */
106069e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
106169e5d3f8SDave Airlie 		if (output->index == 0)
106269e5d3f8SDave Airlie 			connected = true;
106369e5d3f8SDave Airlie 	} else
106469e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
106569e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1066f64122c1SDave Airlie 
10675cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10685cab51cbSMarc-André Lureau 
1069f64122c1SDave Airlie 	return connected ? connector_status_connected
1070f64122c1SDave Airlie 			 : connector_status_disconnected;
1071f64122c1SDave Airlie }
1072f64122c1SDave Airlie 
1073f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1074f64122c1SDave Airlie {
1075f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1076f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1077f64122c1SDave Airlie 
107834ea3d38SThomas Wood 	drm_connector_unregister(connector);
1079f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1080f64122c1SDave Airlie 	kfree(qxl_output);
1081f64122c1SDave Airlie }
1082f64122c1SDave Airlie 
1083f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1084f64122c1SDave Airlie 	.detect = qxl_conn_detect,
10856af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1086f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
10879ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
10889ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10899ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1090f64122c1SDave Airlie };
1091f64122c1SDave Airlie 
1092f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
1093f64122c1SDave Airlie {
1094f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
1095f64122c1SDave Airlie }
1096f64122c1SDave Airlie 
1097f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
1098f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
1099f64122c1SDave Airlie };
1100f64122c1SDave Airlie 
11014695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
11024695b039SDave Airlie {
11034695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
11044695b039SDave Airlie 		return 0;
11054695b039SDave Airlie 
11064695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1107cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
11084695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
11094695b039SDave Airlie 
11104695b039SDave Airlie 	return 0;
11114695b039SDave Airlie }
11124695b039SDave Airlie 
11136d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1114f64122c1SDave Airlie {
11154695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
1116f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1117f64122c1SDave Airlie 	struct drm_connector *connector;
1118f64122c1SDave Airlie 	struct drm_encoder *encoder;
1119f64122c1SDave Airlie 
1120f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1121f64122c1SDave Airlie 	if (!qxl_output)
1122f64122c1SDave Airlie 		return -ENOMEM;
1123f64122c1SDave Airlie 
1124f64122c1SDave Airlie 	qxl_output->index = num_output;
1125f64122c1SDave Airlie 
1126f64122c1SDave Airlie 	connector = &qxl_output->base;
1127f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1128f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1129f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1130f64122c1SDave Airlie 
1131f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
113213a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
1133f64122c1SDave Airlie 
11345ff91e44SDave Airlie 	/* we get HPD via client monitors config */
11355ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1136f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1137cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(&qxl_output->base,
1138f64122c1SDave Airlie 					  &qxl_output->enc);
1139f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1140f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1141f64122c1SDave Airlie 
11424695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11434695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11447dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11457dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11467dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11477dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1148f64122c1SDave Airlie 	return 0;
1149f64122c1SDave Airlie }
1150f64122c1SDave Airlie 
1151f64122c1SDave Airlie static struct drm_framebuffer *
1152f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1153f64122c1SDave Airlie 			    struct drm_file *file_priv,
11541eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1155f64122c1SDave Airlie {
1156bf8744e4SPeter Wu 	return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1157bf8744e4SPeter Wu 					    &qxl_fb_funcs);
1158f64122c1SDave Airlie }
1159f64122c1SDave Airlie 
1160f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1161f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1162472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1163472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1164f64122c1SDave Airlie };
1165f64122c1SDave Airlie 
11662bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1167f64122c1SDave Airlie {
1168f64122c1SDave Airlie 	int ret;
1169f64122c1SDave Airlie 	struct drm_gem_object *gobj;
1170f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
117121c76bd1SGerd Hoffmann 		qxl_num_crtc * sizeof(struct qxl_head);
1172f64122c1SDave Airlie 
1173f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1174f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1175f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1176f64122c1SDave Airlie 	if (ret) {
1177f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1178f64122c1SDave Airlie 		return -ENOMEM;
1179f64122c1SDave Airlie 	}
1180f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
11812bd6ce84SDave Airlie 
1182545592fbSChristophe Fergeau 	ret = qxl_bo_pin(qdev->monitors_config_bo);
11832bd6ce84SDave Airlie 	if (ret)
11842bd6ce84SDave Airlie 		return ret;
11852bd6ce84SDave Airlie 
1186f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
11872bd6ce84SDave Airlie 
1188f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1189f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1190f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1191f64122c1SDave Airlie 
1192f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
119321c76bd1SGerd Hoffmann 	qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
119421c76bd1SGerd Hoffmann 				   GFP_KERNEL);
119590adda2cSGerd Hoffmann 	if (!qdev->dumb_heads) {
119690adda2cSGerd Hoffmann 		qxl_destroy_monitors_object(qdev);
119790adda2cSGerd Hoffmann 		return -ENOMEM;
119890adda2cSGerd Hoffmann 	}
11992bd6ce84SDave Airlie 	return 0;
12002bd6ce84SDave Airlie }
12012bd6ce84SDave Airlie 
12022bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
12032bd6ce84SDave Airlie {
12042bd6ce84SDave Airlie 	int ret;
12052bd6ce84SDave Airlie 
12062bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
12072bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
12082bd6ce84SDave Airlie 
12092bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
1210715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_unpin(qdev->monitors_config_bo);
12112bd6ce84SDave Airlie 	if (ret)
12122bd6ce84SDave Airlie 		return ret;
12132bd6ce84SDave Airlie 
12142bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
12152bd6ce84SDave Airlie 	return 0;
12162bd6ce84SDave Airlie }
12172bd6ce84SDave Airlie 
12182bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
12192bd6ce84SDave Airlie {
12202bd6ce84SDave Airlie 	int i;
12212bd6ce84SDave Airlie 	int ret;
12222bd6ce84SDave Airlie 
1223cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_init(&qdev->ddev);
12242bd6ce84SDave Airlie 
12252bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
12262bd6ce84SDave Airlie 	if (ret)
12272bd6ce84SDave Airlie 		return ret;
1228f64122c1SDave Airlie 
1229cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1230f64122c1SDave Airlie 
1231f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
12321277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_width = 0;
12331277eed5SGabriel Krisman Bertazi 	qdev->ddev.mode_config.min_height = 0;
1234cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_width = 8192;
1235cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.max_height = 8192;
1236f64122c1SDave Airlie 
1237cbdded7fSGabriel Krisman Bertazi 	qdev->ddev.mode_config.fb_base = qdev->vram_base;
12384695b039SDave Airlie 
1239cbdded7fSGabriel Krisman Bertazi 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
12404695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
12414695b039SDave Airlie 
124207f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1243cbdded7fSGabriel Krisman Bertazi 		qdev_crtc_init(&qdev->ddev, i);
1244cbdded7fSGabriel Krisman Bertazi 		qdev_output_init(&qdev->ddev, i);
1245f64122c1SDave Airlie 	}
1246f64122c1SDave Airlie 
1247c50fad8fSGerd Hoffmann 	qxl_display_read_client_monitors_config(qdev);
1248f64122c1SDave Airlie 
12499ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
1250f64122c1SDave Airlie 	return 0;
1251f64122c1SDave Airlie }
1252f64122c1SDave Airlie 
1253f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1254f64122c1SDave Airlie {
12552bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1256cbdded7fSGabriel Krisman Bertazi 	drm_mode_config_cleanup(&qdev->ddev);
1257f64122c1SDave Airlie }
1258