xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision 62676d10b483a2ff6e8b08c5e7c7d63a831343f5)
1f64122c1SDave Airlie /*
2f64122c1SDave Airlie  * Copyright 2013 Red Hat Inc.
3f64122c1SDave Airlie  *
4f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
7f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10f64122c1SDave Airlie  *
11f64122c1SDave Airlie  * The above copyright notice and this permission notice shall be included in
12f64122c1SDave Airlie  * all copies or substantial portions of the Software.
13f64122c1SDave Airlie  *
14f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f64122c1SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
21f64122c1SDave Airlie  *
22f64122c1SDave Airlie  * Authors: Dave Airlie
23f64122c1SDave Airlie  *          Alon Levy
24f64122c1SDave Airlie  */
25f64122c1SDave Airlie 
26c5416d66SRandy Dunlap #include <linux/crc32.h>
27edaf492cSMasahiro Yamada #include <drm/drm_crtc_helper.h>
283cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
291277eed5SGabriel Krisman Bertazi #include <drm/drm_atomic_helper.h>
3010a0bd89SGabriel Krisman Bertazi #include <drm/drm_atomic.h>
31f64122c1SDave Airlie 
32edaf492cSMasahiro Yamada #include "qxl_drv.h"
33edaf492cSMasahiro Yamada #include "qxl_object.h"
34edaf492cSMasahiro Yamada 
3507f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
3607f8d9bdSDave Airlie {
3707f8d9bdSDave Airlie 	return head->width && head->height;
3807f8d9bdSDave Airlie }
3907f8d9bdSDave Airlie 
40e4a76442SChristophe Fergeau static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
41f64122c1SDave Airlie {
42f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
43f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
44f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
4562c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
46f64122c1SDave Airlie 	}
47f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
48f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
49f64122c1SDave Airlie 				sizeof(struct qxl_monitors_config) +
50f64122c1SDave Airlie 				sizeof(struct qxl_head) * count, GFP_KERNEL);
51f64122c1SDave Airlie 		if (!qdev->client_monitors_config) {
52f64122c1SDave Airlie 			qxl_io_log(qdev,
53f64122c1SDave Airlie 				   "%s: allocation failure for %u heads\n",
54f64122c1SDave Airlie 				   __func__, count);
55f64122c1SDave Airlie 			return;
56f64122c1SDave Airlie 		}
57f64122c1SDave Airlie 	}
58f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
59f64122c1SDave Airlie }
60f64122c1SDave Airlie 
619e3b3178SChristophe Fergeau enum {
629e3b3178SChristophe Fergeau 	MONITORS_CONFIG_MODIFIED,
639e3b3178SChristophe Fergeau 	MONITORS_CONFIG_UNCHANGED,
649e3b3178SChristophe Fergeau 	MONITORS_CONFIG_BAD_CRC,
659e3b3178SChristophe Fergeau };
669e3b3178SChristophe Fergeau 
67f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
68f64122c1SDave Airlie {
69f64122c1SDave Airlie 	int i;
70f64122c1SDave Airlie 	int num_monitors;
71f64122c1SDave Airlie 	uint32_t crc;
729e3b3178SChristophe Fergeau 	int status = MONITORS_CONFIG_UNCHANGED;
73f64122c1SDave Airlie 
74f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
75f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
76f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
77f64122c1SDave Airlie 	if (crc != qdev->rom->client_monitors_config_crc) {
7872ec5650SFrediano Ziglio 		qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
79f64122c1SDave Airlie 			   sizeof(qdev->rom->client_monitors_config),
80f64122c1SDave Airlie 			   qdev->rom->client_monitors_config_crc);
819e3b3178SChristophe Fergeau 		return MONITORS_CONFIG_BAD_CRC;
82f64122c1SDave Airlie 	}
83c50fad8fSGerd Hoffmann 	if (!num_monitors) {
84c50fad8fSGerd Hoffmann 		DRM_DEBUG_KMS("no client monitors configured\n");
85c50fad8fSGerd Hoffmann 		return status;
86c50fad8fSGerd Hoffmann 	}
87f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
885b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
89f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
90f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
91f64122c1SDave Airlie 	} else {
92f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
93f64122c1SDave Airlie 	}
949e3b3178SChristophe Fergeau 	if (qdev->client_monitors_config
959e3b3178SChristophe Fergeau 	      && (num_monitors != qdev->client_monitors_config->count)) {
969e3b3178SChristophe Fergeau 		status = MONITORS_CONFIG_MODIFIED;
979e3b3178SChristophe Fergeau 	}
98f64122c1SDave Airlie 	qxl_alloc_client_monitors_config(qdev, num_monitors);
99f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
100f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
101f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
102f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
103f64122c1SDave Airlie 		struct qxl_urect *c_rect =
104f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
105f64122c1SDave Airlie 		struct qxl_head *client_head =
106f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
1079e3b3178SChristophe Fergeau 		if (client_head->x != c_rect->left) {
10807f8d9bdSDave Airlie 			client_head->x = c_rect->left;
1099e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1109e3b3178SChristophe Fergeau 		}
1119e3b3178SChristophe Fergeau 		if (client_head->y != c_rect->top) {
11207f8d9bdSDave Airlie 			client_head->y = c_rect->top;
1139e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1149e3b3178SChristophe Fergeau 		}
1159e3b3178SChristophe Fergeau 		if (client_head->width != c_rect->right - c_rect->left) {
11607f8d9bdSDave Airlie 			client_head->width = c_rect->right - c_rect->left;
1179e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1189e3b3178SChristophe Fergeau 		}
1199e3b3178SChristophe Fergeau 		if (client_head->height != c_rect->bottom - c_rect->top) {
12007f8d9bdSDave Airlie 			client_head->height = c_rect->bottom - c_rect->top;
1219e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1229e3b3178SChristophe Fergeau 		}
1239e3b3178SChristophe Fergeau 		if (client_head->surface_id != 0) {
12407f8d9bdSDave Airlie 			client_head->surface_id = 0;
1259e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1269e3b3178SChristophe Fergeau 		}
1279e3b3178SChristophe Fergeau 		if (client_head->id != i) {
12807f8d9bdSDave Airlie 			client_head->id = i;
1299e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1309e3b3178SChristophe Fergeau 		}
1319e3b3178SChristophe Fergeau 		if (client_head->flags != 0) {
13207f8d9bdSDave Airlie 			client_head->flags = 0;
1339e3b3178SChristophe Fergeau 			status = MONITORS_CONFIG_MODIFIED;
1349e3b3178SChristophe Fergeau 		}
13507f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
13607f8d9bdSDave Airlie 			  client_head->x, client_head->y);
137f64122c1SDave Airlie 	}
1389e3b3178SChristophe Fergeau 
1399e3b3178SChristophe Fergeau 	return status;
140f64122c1SDave Airlie }
141f64122c1SDave Airlie 
1427dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1437dea0941SDave Airlie {
144cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1457dea0941SDave Airlie 	struct drm_connector *connector;
1467dea0941SDave Airlie 	struct qxl_output *output;
1477dea0941SDave Airlie 	struct qxl_head *head;
1487dea0941SDave Airlie 
1497dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1507dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1517dea0941SDave Airlie 
1527dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1537dea0941SDave Airlie 
1547dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1557dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1567dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1577dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1587dea0941SDave Airlie 	}
1597dea0941SDave Airlie }
1607dea0941SDave Airlie 
161f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
162f64122c1SDave Airlie {
163cbdded7fSGabriel Krisman Bertazi 	struct drm_device *dev = &qdev->ddev;
1649062155dSGerd Hoffmann 	int status, retries;
1659e3b3178SChristophe Fergeau 
1669062155dSGerd Hoffmann 	for (retries = 0; retries < 10; retries++) {
1679e3b3178SChristophe Fergeau 		status = qxl_display_copy_rom_client_monitors_config(qdev);
1689062155dSGerd Hoffmann 		if (status != MONITORS_CONFIG_BAD_CRC)
1699062155dSGerd Hoffmann 			break;
1709062155dSGerd Hoffmann 		udelay(5);
1719062155dSGerd Hoffmann 	}
1729062155dSGerd Hoffmann 	if (status == MONITORS_CONFIG_BAD_CRC) {
1739062155dSGerd Hoffmann 		qxl_io_log(qdev, "config: bad crc\n");
1749062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1759062155dSGerd Hoffmann 		return;
1769e3b3178SChristophe Fergeau 	}
1779e3b3178SChristophe Fergeau 	if (status == MONITORS_CONFIG_UNCHANGED) {
1789062155dSGerd Hoffmann 		qxl_io_log(qdev, "config: unchanged\n");
1799062155dSGerd Hoffmann 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1809e3b3178SChristophe Fergeau 		return;
181f64122c1SDave Airlie 	}
1824fdb0869SMarc-André Lureau 
1837dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1847dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1857dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
186cbdded7fSGabriel Krisman Bertazi 	if (!drm_helper_hpd_irq_event(dev)) {
1874fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1884fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
189cbdded7fSGabriel Krisman Bertazi 		drm_kms_helper_hotplug_event(dev);
1904fdb0869SMarc-André Lureau 	}
191f64122c1SDave Airlie }
192f64122c1SDave Airlie 
193b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector,
194b0807423SMarc-André Lureau                                          unsigned *pwidth,
195b0807423SMarc-André Lureau                                          unsigned *pheight)
196f64122c1SDave Airlie {
197f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
198f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
199f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
200f64122c1SDave Airlie 	int h = output->index;
201f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
202f64122c1SDave Airlie 	struct qxl_head *head;
203f64122c1SDave Airlie 
2042d856f94SGerd Hoffmann 	if (!qdev->monitors_config)
2052d856f94SGerd Hoffmann 		return 0;
2062d856f94SGerd Hoffmann 	if (h >= qdev->monitors_config->max_allowed)
2072d856f94SGerd Hoffmann 		return 0;
20807f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
209f64122c1SDave Airlie 		return 0;
2102d856f94SGerd Hoffmann 	if (h >= qdev->client_monitors_config->count)
2112d856f94SGerd Hoffmann 		return 0;
2122d856f94SGerd Hoffmann 
21307f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
2142d856f94SGerd Hoffmann 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
215f64122c1SDave Airlie 
216f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
217f64122c1SDave Airlie 			    false);
218f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
219ff996e72SChristophe Fergeau 	mode->hdisplay = head->width;
220ff996e72SChristophe Fergeau 	mode->vdisplay = head->height;
221ff996e72SChristophe Fergeau 	drm_mode_set_name(mode);
222b0807423SMarc-André Lureau 	*pwidth = head->width;
223b0807423SMarc-André Lureau 	*pheight = head->height;
224f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
225bd3e1c7cSJonathon Jongsma 	/* remember the last custom size for mode validation */
226bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_width = mode->hdisplay;
227bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_height = mode->vdisplay;
228f64122c1SDave Airlie 	return 1;
229f64122c1SDave Airlie }
230f64122c1SDave Airlie 
231bd3e1c7cSJonathon Jongsma static struct mode_size {
232f64122c1SDave Airlie 	int w;
233f64122c1SDave Airlie 	int h;
234f64122c1SDave Airlie } common_modes[] = {
235f64122c1SDave Airlie 	{ 640,  480},
236f64122c1SDave Airlie 	{ 720,  480},
237f64122c1SDave Airlie 	{ 800,  600},
238f64122c1SDave Airlie 	{ 848,  480},
239f64122c1SDave Airlie 	{1024,  768},
240f64122c1SDave Airlie 	{1152,  768},
241f64122c1SDave Airlie 	{1280,  720},
242f64122c1SDave Airlie 	{1280,  800},
243f64122c1SDave Airlie 	{1280,  854},
244f64122c1SDave Airlie 	{1280,  960},
245f64122c1SDave Airlie 	{1280, 1024},
246f64122c1SDave Airlie 	{1440,  900},
247f64122c1SDave Airlie 	{1400, 1050},
248f64122c1SDave Airlie 	{1680, 1050},
249f64122c1SDave Airlie 	{1600, 1200},
250f64122c1SDave Airlie 	{1920, 1080},
251f64122c1SDave Airlie 	{1920, 1200}
252f64122c1SDave Airlie };
253f64122c1SDave Airlie 
254bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector,
255bd3e1c7cSJonathon Jongsma                                 unsigned pwidth,
256bd3e1c7cSJonathon Jongsma                                 unsigned pheight)
257bd3e1c7cSJonathon Jongsma {
258bd3e1c7cSJonathon Jongsma 	struct drm_device *dev = connector->dev;
259bd3e1c7cSJonathon Jongsma 	struct drm_display_mode *mode = NULL;
260bd3e1c7cSJonathon Jongsma 	int i;
261f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
262f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
263f64122c1SDave Airlie 				    60, false, false, false);
264b0807423SMarc-André Lureau 		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
265f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
266f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
267f64122c1SDave Airlie 	}
268f64122c1SDave Airlie 	return i - 1;
269f64122c1SDave Airlie }
270f64122c1SDave Airlie 
271c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
272c2ff6632SGabriel Krisman Bertazi 				  struct drm_crtc_state *old_crtc_state)
273c2ff6632SGabriel Krisman Bertazi {
274c2ff6632SGabriel Krisman Bertazi 	struct drm_device *dev = crtc->dev;
275c2ff6632SGabriel Krisman Bertazi 	struct drm_pending_vblank_event *event;
276c2ff6632SGabriel Krisman Bertazi 	unsigned long flags;
277c2ff6632SGabriel Krisman Bertazi 
278c2ff6632SGabriel Krisman Bertazi 	if (crtc->state && crtc->state->event) {
279c2ff6632SGabriel Krisman Bertazi 		event = crtc->state->event;
280c2ff6632SGabriel Krisman Bertazi 		crtc->state->event = NULL;
281c2ff6632SGabriel Krisman Bertazi 
282c2ff6632SGabriel Krisman Bertazi 		spin_lock_irqsave(&dev->event_lock, flags);
283c2ff6632SGabriel Krisman Bertazi 		drm_crtc_send_vblank_event(crtc, event);
284c2ff6632SGabriel Krisman Bertazi 		spin_unlock_irqrestore(&dev->event_lock, flags);
285c2ff6632SGabriel Krisman Bertazi 	}
286c2ff6632SGabriel Krisman Bertazi }
287c2ff6632SGabriel Krisman Bertazi 
288f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
289f64122c1SDave Airlie {
290f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
291f64122c1SDave Airlie 
292f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
293f64122c1SDave Airlie 	kfree(qxl_crtc);
294f64122c1SDave Airlie }
295f64122c1SDave Airlie 
296f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
297bc8a00d9SGabriel Krisman Bertazi 	.set_config = drm_atomic_helper_set_config,
298f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
2999973c879SGabriel Krisman Bertazi 	.page_flip = drm_atomic_helper_page_flip,
3009ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_crtc_reset,
3019ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
3029ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
303f64122c1SDave Airlie };
304f64122c1SDave Airlie 
3056819c3c2SNoralf Trønnes void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
306f64122c1SDave Airlie {
307f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
308*62676d10SGerd Hoffmann 	struct qxl_bo *bo = gem_to_qxl_bo(qxl_fb->obj);
309f64122c1SDave Airlie 
310*62676d10SGerd Hoffmann 	WARN_ON(bo->shadow);
311f64122c1SDave Airlie 	drm_gem_object_unreference_unlocked(qxl_fb->obj);
312f64122c1SDave Airlie 	drm_framebuffer_cleanup(fb);
313f64122c1SDave Airlie 	kfree(qxl_fb);
314f64122c1SDave Airlie }
315f64122c1SDave Airlie 
3166d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
317f64122c1SDave Airlie 					 struct drm_file *file_priv,
318f64122c1SDave Airlie 					 unsigned flags, unsigned color,
319f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
320f64122c1SDave Airlie 					 unsigned num_clips)
321f64122c1SDave Airlie {
322f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
323f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
324f64122c1SDave Airlie 	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
325f64122c1SDave Airlie 	struct drm_clip_rect norect;
326f64122c1SDave Airlie 	struct qxl_bo *qobj;
327f64122c1SDave Airlie 	int inc = 1;
328f64122c1SDave Airlie 
32973e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
33073e9efd4SVille Syrjälä 
331f64122c1SDave Airlie 	qobj = gem_to_qxl_bo(qxl_fb->obj);
332b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
33373e9efd4SVille Syrjälä 	if (!qobj->is_primary) {
33473e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
335b2b4465dSDave Airlie 		return 0;
33673e9efd4SVille Syrjälä 	}
337b2b4465dSDave Airlie 
338f64122c1SDave Airlie 	if (!num_clips) {
339f64122c1SDave Airlie 		num_clips = 1;
340f64122c1SDave Airlie 		clips = &norect;
341f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
342f64122c1SDave Airlie 		norect.x2 = fb->width;
343f64122c1SDave Airlie 		norect.y2 = fb->height;
344f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
345f64122c1SDave Airlie 		num_clips /= 2;
346f64122c1SDave Airlie 		inc = 2; /* skip source rects */
347f64122c1SDave Airlie 	}
348f64122c1SDave Airlie 
349f64122c1SDave Airlie 	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
350f64122c1SDave Airlie 			  clips, num_clips, inc);
35173e9efd4SVille Syrjälä 
35273e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
35373e9efd4SVille Syrjälä 
354f64122c1SDave Airlie 	return 0;
355f64122c1SDave Airlie }
356f64122c1SDave Airlie 
357f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
358f64122c1SDave Airlie 	.destroy = qxl_user_framebuffer_destroy,
359f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
360f64122c1SDave Airlie /*	TODO?
361f64122c1SDave Airlie  *	.create_handle = qxl_user_framebuffer_create_handle, */
362f64122c1SDave Airlie };
363f64122c1SDave Airlie 
364f64122c1SDave Airlie int
365f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev,
366f64122c1SDave Airlie 		     struct qxl_framebuffer *qfb,
3671eb83451SVille Syrjälä 		     const struct drm_mode_fb_cmd2 *mode_cmd,
3686819c3c2SNoralf Trønnes 		     struct drm_gem_object *obj,
3696819c3c2SNoralf Trønnes 		     const struct drm_framebuffer_funcs *funcs)
370f64122c1SDave Airlie {
371f64122c1SDave Airlie 	int ret;
372f64122c1SDave Airlie 
373f64122c1SDave Airlie 	qfb->obj = obj;
3745360943fSVille Syrjälä 	drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
3756819c3c2SNoralf Trønnes 	ret = drm_framebuffer_init(dev, &qfb->base, funcs);
376f64122c1SDave Airlie 	if (ret) {
377f64122c1SDave Airlie 		qfb->obj = NULL;
378f64122c1SDave Airlie 		return ret;
379f64122c1SDave Airlie 	}
380f64122c1SDave Airlie 	return 0;
381f64122c1SDave Airlie }
382f64122c1SDave Airlie 
383f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
384f64122c1SDave Airlie 				  const struct drm_display_mode *mode,
385f64122c1SDave Airlie 				  struct drm_display_mode *adjusted_mode)
386f64122c1SDave Airlie {
387f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
388f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
389f64122c1SDave Airlie 
390f64122c1SDave Airlie 	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
391f64122c1SDave Airlie 		   __func__,
392f64122c1SDave Airlie 		   mode->hdisplay, mode->vdisplay,
393f64122c1SDave Airlie 		   adjusted_mode->hdisplay,
394f64122c1SDave Airlie 		   adjusted_mode->vdisplay);
395f64122c1SDave Airlie 	return true;
396f64122c1SDave Airlie }
397f64122c1SDave Airlie 
398e4a76442SChristophe Fergeau static void
399f64122c1SDave Airlie qxl_send_monitors_config(struct qxl_device *qdev)
400f64122c1SDave Airlie {
401f64122c1SDave Airlie 	int i;
402f64122c1SDave Airlie 
403f64122c1SDave Airlie 	BUG_ON(!qdev->ram_header->monitors_config);
404f64122c1SDave Airlie 
405f64122c1SDave Airlie 	if (qdev->monitors_config->count == 0) {
406f64122c1SDave Airlie 		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
407f64122c1SDave Airlie 		return;
408f64122c1SDave Airlie 	}
409f64122c1SDave Airlie 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
410f64122c1SDave Airlie 		struct qxl_head *head = &qdev->monitors_config->heads[i];
411f64122c1SDave Airlie 
41207f8d9bdSDave Airlie 		if (head->y > 8192 || head->x > 8192 ||
413f64122c1SDave Airlie 		    head->width > 8192 || head->height > 8192) {
414f64122c1SDave Airlie 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
415f64122c1SDave Airlie 				  i, head->width, head->height,
416f64122c1SDave Airlie 				  head->x, head->y);
417f64122c1SDave Airlie 			return;
418f64122c1SDave Airlie 		}
419f64122c1SDave Airlie 	}
420f64122c1SDave Airlie 	qxl_io_monitors_config(qdev);
421f64122c1SDave Airlie }
422f64122c1SDave Airlie 
42307f8d9bdSDave Airlie static void qxl_monitors_config_set(struct qxl_device *qdev,
42407f8d9bdSDave Airlie 				    int index,
425f64122c1SDave Airlie 				    unsigned x, unsigned y,
42607f8d9bdSDave Airlie 				    unsigned width, unsigned height,
42707f8d9bdSDave Airlie 				    unsigned surf_id)
428f64122c1SDave Airlie {
42907f8d9bdSDave Airlie 	DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
43007f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].x = x;
43107f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].y = y;
43207f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].width = width;
43307f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].height = height;
43407f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].surface_id = surf_id;
43507f8d9bdSDave Airlie 
436f64122c1SDave Airlie }
437f64122c1SDave Airlie 
43845dfe577SGerd Hoffmann static void qxl_mode_set_nofb(struct drm_crtc *crtc)
4393538e80aSGabriel Krisman Bertazi {
4403538e80aSGabriel Krisman Bertazi 	struct qxl_device *qdev = crtc->dev->dev_private;
4413538e80aSGabriel Krisman Bertazi 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
4423538e80aSGabriel Krisman Bertazi 	struct drm_display_mode *mode = &crtc->mode;
4433538e80aSGabriel Krisman Bertazi 
4443538e80aSGabriel Krisman Bertazi 	DRM_DEBUG("Mode set (%d,%d)\n",
4453538e80aSGabriel Krisman Bertazi 		  mode->hdisplay, mode->vdisplay);
4463538e80aSGabriel Krisman Bertazi 
4473538e80aSGabriel Krisman Bertazi 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
4483538e80aSGabriel Krisman Bertazi 				mode->hdisplay,	mode->vdisplay, 0);
4493538e80aSGabriel Krisman Bertazi 
4503538e80aSGabriel Krisman Bertazi }
4513538e80aSGabriel Krisman Bertazi 
4520b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
4530b20a0f8SLaurent Pinchart 				   struct drm_crtc_state *old_state)
454f64122c1SDave Airlie {
455f64122c1SDave Airlie 	DRM_DEBUG("\n");
456f64122c1SDave Airlie }
457f64122c1SDave Airlie 
45864581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
45964581714SLaurent Pinchart 				    struct drm_crtc_state *old_state)
46007f8d9bdSDave Airlie {
46107f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
46237235451SGabriel Krisman Bertazi 	struct qxl_device *qdev = crtc->dev->dev_private;
46307f8d9bdSDave Airlie 
46407f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
46507f8d9bdSDave Airlie 
46607f8d9bdSDave Airlie 	qxl_send_monitors_config(qdev);
46707f8d9bdSDave Airlie }
46807f8d9bdSDave Airlie 
469f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
470f64122c1SDave Airlie 	.mode_fixup = qxl_crtc_mode_fixup,
4713538e80aSGabriel Krisman Bertazi 	.mode_set_nofb = qxl_mode_set_nofb,
472c2ff6632SGabriel Krisman Bertazi 	.atomic_flush = qxl_crtc_atomic_flush,
4730b20a0f8SLaurent Pinchart 	.atomic_enable = qxl_crtc_atomic_enable,
47464581714SLaurent Pinchart 	.atomic_disable = qxl_crtc_atomic_disable,
475f64122c1SDave Airlie };
476f64122c1SDave Airlie 
47745dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
478c2ff6632SGabriel Krisman Bertazi 				    struct drm_plane_state *state)
479c2ff6632SGabriel Krisman Bertazi {
480c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
481c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb;
482c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo;
483c2ff6632SGabriel Krisman Bertazi 
484c2ff6632SGabriel Krisman Bertazi 	if (!state->crtc || !state->fb)
485c2ff6632SGabriel Krisman Bertazi 		return 0;
486c2ff6632SGabriel Krisman Bertazi 
487c2ff6632SGabriel Krisman Bertazi 	qfb = to_qxl_framebuffer(state->fb);
488c2ff6632SGabriel Krisman Bertazi 	bo = gem_to_qxl_bo(qfb->obj);
489c2ff6632SGabriel Krisman Bertazi 
490c2ff6632SGabriel Krisman Bertazi 	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
491c2ff6632SGabriel Krisman Bertazi 		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
492c2ff6632SGabriel Krisman Bertazi 		return -EINVAL;
493c2ff6632SGabriel Krisman Bertazi 	}
494c2ff6632SGabriel Krisman Bertazi 
495c2ff6632SGabriel Krisman Bertazi 	return 0;
496c2ff6632SGabriel Krisman Bertazi }
497c2ff6632SGabriel Krisman Bertazi 
498c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
499c2ff6632SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
500c2ff6632SGabriel Krisman Bertazi {
501c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
502c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb =
503c2ff6632SGabriel Krisman Bertazi 		to_qxl_framebuffer(plane->state->fb);
504c2ff6632SGabriel Krisman Bertazi 	struct qxl_framebuffer *qfb_old;
505c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
506c2ff6632SGabriel Krisman Bertazi 	struct qxl_bo *bo_old;
507c2ff6632SGabriel Krisman Bertazi 	struct drm_clip_rect norect = {
508c2ff6632SGabriel Krisman Bertazi 	    .x1 = 0,
509c2ff6632SGabriel Krisman Bertazi 	    .y1 = 0,
510c2ff6632SGabriel Krisman Bertazi 	    .x2 = qfb->base.width,
511c2ff6632SGabriel Krisman Bertazi 	    .y2 = qfb->base.height
512c2ff6632SGabriel Krisman Bertazi 	};
513*62676d10SGerd Hoffmann 	bool same_shadow = false;
514c2ff6632SGabriel Krisman Bertazi 
515b0e07da3SGerd Hoffmann 	if (old_state->fb) {
516c2ff6632SGabriel Krisman Bertazi 		qfb_old = to_qxl_framebuffer(old_state->fb);
517c2ff6632SGabriel Krisman Bertazi 		bo_old = gem_to_qxl_bo(qfb_old->obj);
518b0e07da3SGerd Hoffmann 	} else {
519b0e07da3SGerd Hoffmann 		bo_old = NULL;
520b0e07da3SGerd Hoffmann 	}
521b0e07da3SGerd Hoffmann 
522b0e07da3SGerd Hoffmann 	if (bo == bo_old)
523b0e07da3SGerd Hoffmann 		return;
524b0e07da3SGerd Hoffmann 
525*62676d10SGerd Hoffmann 	if (bo_old && bo_old->shadow && bo->shadow &&
526*62676d10SGerd Hoffmann 	    bo_old->shadow == bo->shadow) {
527*62676d10SGerd Hoffmann 		same_shadow = true;
528*62676d10SGerd Hoffmann 	}
529*62676d10SGerd Hoffmann 
530b0e07da3SGerd Hoffmann 	if (bo_old && bo_old->is_primary) {
531*62676d10SGerd Hoffmann 		if (!same_shadow)
532b0e07da3SGerd Hoffmann 			qxl_io_destroy_primary(qdev);
533c2ff6632SGabriel Krisman Bertazi 		bo_old->is_primary = false;
534c2ff6632SGabriel Krisman Bertazi 	}
535c2ff6632SGabriel Krisman Bertazi 
536b0e07da3SGerd Hoffmann 	if (!bo->is_primary) {
537*62676d10SGerd Hoffmann 		if (!same_shadow)
538b0e07da3SGerd Hoffmann 			qxl_io_create_primary(qdev, 0, bo);
539c2ff6632SGabriel Krisman Bertazi 		bo->is_primary = true;
540b0e07da3SGerd Hoffmann 	}
541*62676d10SGerd Hoffmann 
542c2ff6632SGabriel Krisman Bertazi 	qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
543c2ff6632SGabriel Krisman Bertazi }
544c2ff6632SGabriel Krisman Bertazi 
545c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
546c2ff6632SGabriel Krisman Bertazi 				       struct drm_plane_state *old_state)
547c2ff6632SGabriel Krisman Bertazi {
548c2ff6632SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
549c2ff6632SGabriel Krisman Bertazi 
550b0e07da3SGerd Hoffmann 	if (old_state->fb) {
551b0e07da3SGerd Hoffmann 		struct qxl_framebuffer *qfb =
552c2ff6632SGabriel Krisman Bertazi 			to_qxl_framebuffer(old_state->fb);
553c2ff6632SGabriel Krisman Bertazi 		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
554c2ff6632SGabriel Krisman Bertazi 
555b0e07da3SGerd Hoffmann 		if (bo->is_primary) {
556c2ff6632SGabriel Krisman Bertazi 			qxl_io_destroy_primary(qdev);
557c2ff6632SGabriel Krisman Bertazi 			bo->is_primary = false;
558c2ff6632SGabriel Krisman Bertazi 		}
559c2ff6632SGabriel Krisman Bertazi 	}
560b0e07da3SGerd Hoffmann }
561c2ff6632SGabriel Krisman Bertazi 
56245dfe577SGerd Hoffmann static int qxl_plane_atomic_check(struct drm_plane *plane,
5631277eed5SGabriel Krisman Bertazi 				  struct drm_plane_state *state)
5641277eed5SGabriel Krisman Bertazi {
5651277eed5SGabriel Krisman Bertazi 	return 0;
5661277eed5SGabriel Krisman Bertazi }
5671277eed5SGabriel Krisman Bertazi 
5681277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
5691277eed5SGabriel Krisman Bertazi 				     struct drm_plane_state *old_state)
5701277eed5SGabriel Krisman Bertazi {
5711277eed5SGabriel Krisman Bertazi 	struct drm_device *dev = plane->dev;
5721277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
5731277eed5SGabriel Krisman Bertazi 	struct drm_framebuffer *fb = plane->state->fb;
5741277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
5751277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
5761277eed5SGabriel Krisman Bertazi 	struct qxl_cursor *cursor;
5771277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
5781277eed5SGabriel Krisman Bertazi 	struct qxl_bo *cursor_bo, *user_bo = NULL;
5791277eed5SGabriel Krisman Bertazi 	int ret;
5801277eed5SGabriel Krisman Bertazi 	void *user_ptr;
5811277eed5SGabriel Krisman Bertazi 	int size = 64*64*4;
5821277eed5SGabriel Krisman Bertazi 
5831277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5841277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
5851277eed5SGabriel Krisman Bertazi 					 &release, NULL);
586ee5cb7c4SDan Carpenter 	if (ret)
587ee5cb7c4SDan Carpenter 		return;
5881277eed5SGabriel Krisman Bertazi 
5891277eed5SGabriel Krisman Bertazi 	if (fb != old_state->fb) {
5901277eed5SGabriel Krisman Bertazi 		obj = to_qxl_framebuffer(fb)->obj;
5911277eed5SGabriel Krisman Bertazi 		user_bo = gem_to_qxl_bo(obj);
5921277eed5SGabriel Krisman Bertazi 
5931277eed5SGabriel Krisman Bertazi 		/* pinning is done in the prepare/cleanup framevbuffer */
5941277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(user_bo, &user_ptr);
5951277eed5SGabriel Krisman Bertazi 		if (ret)
5961277eed5SGabriel Krisman Bertazi 			goto out_free_release;
5971277eed5SGabriel Krisman Bertazi 
5981277eed5SGabriel Krisman Bertazi 		ret = qxl_alloc_bo_reserved(qdev, release,
5991277eed5SGabriel Krisman Bertazi 					    sizeof(struct qxl_cursor) + size,
6001277eed5SGabriel Krisman Bertazi 					    &cursor_bo);
6011277eed5SGabriel Krisman Bertazi 		if (ret)
6021277eed5SGabriel Krisman Bertazi 			goto out_kunmap;
6031277eed5SGabriel Krisman Bertazi 
6041277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6051277eed5SGabriel Krisman Bertazi 		if (ret)
6061277eed5SGabriel Krisman Bertazi 			goto out_free_bo;
6071277eed5SGabriel Krisman Bertazi 
6081277eed5SGabriel Krisman Bertazi 		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
6091277eed5SGabriel Krisman Bertazi 		if (ret)
6101277eed5SGabriel Krisman Bertazi 			goto out_backoff;
6111277eed5SGabriel Krisman Bertazi 
6121277eed5SGabriel Krisman Bertazi 		cursor->header.unique = 0;
6131277eed5SGabriel Krisman Bertazi 		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
6141277eed5SGabriel Krisman Bertazi 		cursor->header.width = 64;
6151277eed5SGabriel Krisman Bertazi 		cursor->header.height = 64;
6161277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_x = fb->hot_x;
6171277eed5SGabriel Krisman Bertazi 		cursor->header.hot_spot_y = fb->hot_y;
6181277eed5SGabriel Krisman Bertazi 		cursor->data_size = size;
6191277eed5SGabriel Krisman Bertazi 		cursor->chunk.next_chunk = 0;
6201277eed5SGabriel Krisman Bertazi 		cursor->chunk.prev_chunk = 0;
6211277eed5SGabriel Krisman Bertazi 		cursor->chunk.data_size = size;
6221277eed5SGabriel Krisman Bertazi 		memcpy(cursor->chunk.data, user_ptr, size);
6231277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(cursor_bo);
6241277eed5SGabriel Krisman Bertazi 		qxl_bo_kunmap(user_bo);
6251277eed5SGabriel Krisman Bertazi 
626429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6271277eed5SGabriel Krisman Bertazi 		cmd->u.set.visible = 1;
6281277eed5SGabriel Krisman Bertazi 		cmd->u.set.shape = qxl_bo_physical_address(qdev,
6291277eed5SGabriel Krisman Bertazi 							   cursor_bo, 0);
6301277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_SET;
6311277eed5SGabriel Krisman Bertazi 	} else {
6321277eed5SGabriel Krisman Bertazi 
6331277eed5SGabriel Krisman Bertazi 		ret = qxl_release_reserve_list(release, true);
6341277eed5SGabriel Krisman Bertazi 		if (ret)
6351277eed5SGabriel Krisman Bertazi 			goto out_free_release;
6361277eed5SGabriel Krisman Bertazi 
637429030bcSGabriel Krisman Bertazi 		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
6381277eed5SGabriel Krisman Bertazi 		cmd->type = QXL_CURSOR_MOVE;
6391277eed5SGabriel Krisman Bertazi 	}
6401277eed5SGabriel Krisman Bertazi 
6411277eed5SGabriel Krisman Bertazi 	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
6421277eed5SGabriel Krisman Bertazi 	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
6431277eed5SGabriel Krisman Bertazi 
6441277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
6451277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
6461277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
6471277eed5SGabriel Krisman Bertazi 
6481277eed5SGabriel Krisman Bertazi 	return;
6491277eed5SGabriel Krisman Bertazi 
6501277eed5SGabriel Krisman Bertazi out_backoff:
6511277eed5SGabriel Krisman Bertazi 	qxl_release_backoff_reserve_list(release);
6521277eed5SGabriel Krisman Bertazi out_free_bo:
6531277eed5SGabriel Krisman Bertazi 	qxl_bo_unref(&cursor_bo);
6541277eed5SGabriel Krisman Bertazi out_kunmap:
6551277eed5SGabriel Krisman Bertazi 	qxl_bo_kunmap(user_bo);
6561277eed5SGabriel Krisman Bertazi out_free_release:
6571277eed5SGabriel Krisman Bertazi 	qxl_release_free(qdev, release);
6581277eed5SGabriel Krisman Bertazi 	return;
6591277eed5SGabriel Krisman Bertazi 
6601277eed5SGabriel Krisman Bertazi }
6611277eed5SGabriel Krisman Bertazi 
66245dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
6631277eed5SGabriel Krisman Bertazi 				      struct drm_plane_state *old_state)
6641277eed5SGabriel Krisman Bertazi {
6651277eed5SGabriel Krisman Bertazi 	struct qxl_device *qdev = plane->dev->dev_private;
6661277eed5SGabriel Krisman Bertazi 	struct qxl_release *release;
6671277eed5SGabriel Krisman Bertazi 	struct qxl_cursor_cmd *cmd;
6681277eed5SGabriel Krisman Bertazi 	int ret;
6691277eed5SGabriel Krisman Bertazi 
6701277eed5SGabriel Krisman Bertazi 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
6711277eed5SGabriel Krisman Bertazi 					 QXL_RELEASE_CURSOR_CMD,
6721277eed5SGabriel Krisman Bertazi 					 &release, NULL);
6731277eed5SGabriel Krisman Bertazi 	if (ret)
6741277eed5SGabriel Krisman Bertazi 		return;
6751277eed5SGabriel Krisman Bertazi 
6761277eed5SGabriel Krisman Bertazi 	ret = qxl_release_reserve_list(release, true);
6771277eed5SGabriel Krisman Bertazi 	if (ret) {
6781277eed5SGabriel Krisman Bertazi 		qxl_release_free(qdev, release);
6791277eed5SGabriel Krisman Bertazi 		return;
6801277eed5SGabriel Krisman Bertazi 	}
6811277eed5SGabriel Krisman Bertazi 
6821277eed5SGabriel Krisman Bertazi 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
6831277eed5SGabriel Krisman Bertazi 	cmd->type = QXL_CURSOR_HIDE;
6841277eed5SGabriel Krisman Bertazi 	qxl_release_unmap(qdev, release, &cmd->release_info);
6851277eed5SGabriel Krisman Bertazi 
6861277eed5SGabriel Krisman Bertazi 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
6871277eed5SGabriel Krisman Bertazi 	qxl_release_fence_buffer_objects(release);
6881277eed5SGabriel Krisman Bertazi }
6891277eed5SGabriel Krisman Bertazi 
69045dfe577SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
6911277eed5SGabriel Krisman Bertazi 				struct drm_plane_state *new_state)
6921277eed5SGabriel Krisman Bertazi {
693*62676d10SGerd Hoffmann 	struct qxl_device *qdev = plane->dev->dev_private;
6941277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
695*62676d10SGerd Hoffmann 	struct qxl_bo *user_bo, *old_bo = NULL;
6961277eed5SGabriel Krisman Bertazi 	int ret;
6971277eed5SGabriel Krisman Bertazi 
6981277eed5SGabriel Krisman Bertazi 	if (!new_state->fb)
6991277eed5SGabriel Krisman Bertazi 		return 0;
7001277eed5SGabriel Krisman Bertazi 
7011277eed5SGabriel Krisman Bertazi 	obj = to_qxl_framebuffer(new_state->fb)->obj;
7021277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7031277eed5SGabriel Krisman Bertazi 
704*62676d10SGerd Hoffmann 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
705*62676d10SGerd Hoffmann 	    user_bo->is_dumb && !user_bo->shadow) {
706*62676d10SGerd Hoffmann 		if (plane->state->fb) {
707*62676d10SGerd Hoffmann 			obj = to_qxl_framebuffer(plane->state->fb)->obj;
708*62676d10SGerd Hoffmann 			old_bo = gem_to_qxl_bo(obj);
709*62676d10SGerd Hoffmann 		}
710*62676d10SGerd Hoffmann 		if (old_bo && old_bo->shadow &&
711*62676d10SGerd Hoffmann 		    user_bo->gem_base.size == old_bo->gem_base.size &&
712*62676d10SGerd Hoffmann 		    plane->state->crtc     == new_state->crtc &&
713*62676d10SGerd Hoffmann 		    plane->state->crtc_w   == new_state->crtc_w &&
714*62676d10SGerd Hoffmann 		    plane->state->crtc_h   == new_state->crtc_h &&
715*62676d10SGerd Hoffmann 		    plane->state->src_x    == new_state->src_x &&
716*62676d10SGerd Hoffmann 		    plane->state->src_y    == new_state->src_y &&
717*62676d10SGerd Hoffmann 		    plane->state->src_w    == new_state->src_w &&
718*62676d10SGerd Hoffmann 		    plane->state->src_h    == new_state->src_h &&
719*62676d10SGerd Hoffmann 		    plane->state->rotation == new_state->rotation &&
720*62676d10SGerd Hoffmann 		    plane->state->zpos     == new_state->zpos) {
721*62676d10SGerd Hoffmann 			drm_gem_object_get(&old_bo->shadow->gem_base);
722*62676d10SGerd Hoffmann 			user_bo->shadow = old_bo->shadow;
723*62676d10SGerd Hoffmann 		} else {
724*62676d10SGerd Hoffmann 			qxl_bo_create(qdev, user_bo->gem_base.size,
725*62676d10SGerd Hoffmann 				      true, true, QXL_GEM_DOMAIN_VRAM, NULL,
726*62676d10SGerd Hoffmann 				      &user_bo->shadow);
727*62676d10SGerd Hoffmann 		}
728*62676d10SGerd Hoffmann 	}
729*62676d10SGerd Hoffmann 
7301277eed5SGabriel Krisman Bertazi 	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
7311277eed5SGabriel Krisman Bertazi 	if (ret)
7321277eed5SGabriel Krisman Bertazi 		return ret;
7331277eed5SGabriel Krisman Bertazi 
7341277eed5SGabriel Krisman Bertazi 	return 0;
7351277eed5SGabriel Krisman Bertazi }
7361277eed5SGabriel Krisman Bertazi 
7371277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
7381277eed5SGabriel Krisman Bertazi 				 struct drm_plane_state *old_state)
7391277eed5SGabriel Krisman Bertazi {
7401277eed5SGabriel Krisman Bertazi 	struct drm_gem_object *obj;
7411277eed5SGabriel Krisman Bertazi 	struct qxl_bo *user_bo;
7421277eed5SGabriel Krisman Bertazi 
7435f3d862aSGerd Hoffmann 	if (!old_state->fb) {
7445f3d862aSGerd Hoffmann 		/*
7455f3d862aSGerd Hoffmann 		 * we never executed prepare_fb, so there's nothing to
7461277eed5SGabriel Krisman Bertazi 		 * unpin.
7471277eed5SGabriel Krisman Bertazi 		 */
7481277eed5SGabriel Krisman Bertazi 		return;
7491277eed5SGabriel Krisman Bertazi 	}
7501277eed5SGabriel Krisman Bertazi 
7515f3d862aSGerd Hoffmann 	obj = to_qxl_framebuffer(old_state->fb)->obj;
7521277eed5SGabriel Krisman Bertazi 	user_bo = gem_to_qxl_bo(obj);
7531277eed5SGabriel Krisman Bertazi 	qxl_bo_unpin(user_bo);
754*62676d10SGerd Hoffmann 
755*62676d10SGerd Hoffmann 	if (user_bo->shadow && !user_bo->is_primary) {
756*62676d10SGerd Hoffmann 		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
757*62676d10SGerd Hoffmann 		user_bo->shadow = NULL;
758*62676d10SGerd Hoffmann 	}
7591277eed5SGabriel Krisman Bertazi }
7601277eed5SGabriel Krisman Bertazi 
7611277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
7621277eed5SGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
7631277eed5SGabriel Krisman Bertazi };
7641277eed5SGabriel Krisman Bertazi 
7651277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
7661277eed5SGabriel Krisman Bertazi 	.atomic_check = qxl_plane_atomic_check,
7671277eed5SGabriel Krisman Bertazi 	.atomic_update = qxl_cursor_atomic_update,
7681277eed5SGabriel Krisman Bertazi 	.atomic_disable = qxl_cursor_atomic_disable,
7691277eed5SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
7701277eed5SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
7711277eed5SGabriel Krisman Bertazi };
7721277eed5SGabriel Krisman Bertazi 
7731277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
774472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
775472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
7761277eed5SGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
7779ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
7789ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
7799ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
7801277eed5SGabriel Krisman Bertazi };
7811277eed5SGabriel Krisman Bertazi 
782d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
783d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_XRGB8888,
784d3e7e42dSGabriel Krisman Bertazi 	DRM_FORMAT_ARGB8888,
785d3e7e42dSGabriel Krisman Bertazi };
786d3e7e42dSGabriel Krisman Bertazi 
787c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
788c2ff6632SGabriel Krisman Bertazi 	.atomic_check = qxl_primary_atomic_check,
789c2ff6632SGabriel Krisman Bertazi 	.atomic_update = qxl_primary_atomic_update,
790c2ff6632SGabriel Krisman Bertazi 	.atomic_disable = qxl_primary_atomic_disable,
791c2ff6632SGabriel Krisman Bertazi 	.prepare_fb = qxl_plane_prepare_fb,
792c2ff6632SGabriel Krisman Bertazi 	.cleanup_fb = qxl_plane_cleanup_fb,
793c2ff6632SGabriel Krisman Bertazi };
794c2ff6632SGabriel Krisman Bertazi 
795d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
796472e6d46SGabriel Krisman Bertazi 	.update_plane	= drm_atomic_helper_update_plane,
797472e6d46SGabriel Krisman Bertazi 	.disable_plane	= drm_atomic_helper_disable_plane,
798d3e7e42dSGabriel Krisman Bertazi 	.destroy	= drm_primary_helper_destroy,
7999ade8b98SGabriel Krisman Bertazi 	.reset		= drm_atomic_helper_plane_reset,
8009ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
8019ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
802d3e7e42dSGabriel Krisman Bertazi };
803d3e7e42dSGabriel Krisman Bertazi 
804d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
805d3e7e42dSGabriel Krisman Bertazi 					  unsigned int possible_crtcs,
806d3e7e42dSGabriel Krisman Bertazi 					  enum drm_plane_type type)
807d3e7e42dSGabriel Krisman Bertazi {
808d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
809d3e7e42dSGabriel Krisman Bertazi 	struct drm_plane *plane;
810d3e7e42dSGabriel Krisman Bertazi 	const struct drm_plane_funcs *funcs;
811d3e7e42dSGabriel Krisman Bertazi 	const uint32_t *formats;
812d3e7e42dSGabriel Krisman Bertazi 	int num_formats;
813d3e7e42dSGabriel Krisman Bertazi 	int err;
814d3e7e42dSGabriel Krisman Bertazi 
815d3e7e42dSGabriel Krisman Bertazi 	if (type == DRM_PLANE_TYPE_PRIMARY) {
816d3e7e42dSGabriel Krisman Bertazi 		funcs = &qxl_primary_plane_funcs;
817d3e7e42dSGabriel Krisman Bertazi 		formats = qxl_primary_plane_formats;
818d3e7e42dSGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
819c2ff6632SGabriel Krisman Bertazi 		helper_funcs = &primary_helper_funcs;
8201277eed5SGabriel Krisman Bertazi 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
8211277eed5SGabriel Krisman Bertazi 		funcs = &qxl_cursor_plane_funcs;
8221277eed5SGabriel Krisman Bertazi 		formats = qxl_cursor_plane_formats;
8231277eed5SGabriel Krisman Bertazi 		helper_funcs = &qxl_cursor_helper_funcs;
8241277eed5SGabriel Krisman Bertazi 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
825d3e7e42dSGabriel Krisman Bertazi 	} else {
826d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-EINVAL);
827d3e7e42dSGabriel Krisman Bertazi 	}
828d3e7e42dSGabriel Krisman Bertazi 
829d3e7e42dSGabriel Krisman Bertazi 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
830d3e7e42dSGabriel Krisman Bertazi 	if (!plane)
831d3e7e42dSGabriel Krisman Bertazi 		return ERR_PTR(-ENOMEM);
832d3e7e42dSGabriel Krisman Bertazi 
833d3e7e42dSGabriel Krisman Bertazi 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
834d3e7e42dSGabriel Krisman Bertazi 				       funcs, formats, num_formats,
835e6fc3b68SBen Widawsky 				       NULL, type, NULL);
836d3e7e42dSGabriel Krisman Bertazi 	if (err)
837d3e7e42dSGabriel Krisman Bertazi 		goto free_plane;
838d3e7e42dSGabriel Krisman Bertazi 
839d3e7e42dSGabriel Krisman Bertazi 	drm_plane_helper_add(plane, helper_funcs);
840d3e7e42dSGabriel Krisman Bertazi 
841d3e7e42dSGabriel Krisman Bertazi 	return plane;
842d3e7e42dSGabriel Krisman Bertazi 
843d3e7e42dSGabriel Krisman Bertazi free_plane:
844d3e7e42dSGabriel Krisman Bertazi 	kfree(plane);
845d3e7e42dSGabriel Krisman Bertazi 	return ERR_PTR(-EINVAL);
846d3e7e42dSGabriel Krisman Bertazi }
847d3e7e42dSGabriel Krisman Bertazi 
84807f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
849f64122c1SDave Airlie {
850f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
8511277eed5SGabriel Krisman Bertazi 	struct drm_plane *primary, *cursor;
852d3e7e42dSGabriel Krisman Bertazi 	struct qxl_device *qdev = dev->dev_private;
853d3e7e42dSGabriel Krisman Bertazi 	int r;
854f64122c1SDave Airlie 
855f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
856f64122c1SDave Airlie 	if (!qxl_crtc)
857f64122c1SDave Airlie 		return -ENOMEM;
858f64122c1SDave Airlie 
859d3e7e42dSGabriel Krisman Bertazi 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
860d3e7e42dSGabriel Krisman Bertazi 	if (IS_ERR(primary)) {
861d3e7e42dSGabriel Krisman Bertazi 		r = -ENOMEM;
862d3e7e42dSGabriel Krisman Bertazi 		goto free_mem;
863d3e7e42dSGabriel Krisman Bertazi 	}
864d3e7e42dSGabriel Krisman Bertazi 
8651277eed5SGabriel Krisman Bertazi 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
8661277eed5SGabriel Krisman Bertazi 	if (IS_ERR(cursor)) {
8671277eed5SGabriel Krisman Bertazi 		r = -ENOMEM;
8681277eed5SGabriel Krisman Bertazi 		goto clean_primary;
8691277eed5SGabriel Krisman Bertazi 	}
8701277eed5SGabriel Krisman Bertazi 
8711277eed5SGabriel Krisman Bertazi 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
872d3e7e42dSGabriel Krisman Bertazi 				      &qxl_crtc_funcs, NULL);
873d3e7e42dSGabriel Krisman Bertazi 	if (r)
8741277eed5SGabriel Krisman Bertazi 		goto clean_cursor;
875d3e7e42dSGabriel Krisman Bertazi 
87607f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
877f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
878f64122c1SDave Airlie 	return 0;
879d3e7e42dSGabriel Krisman Bertazi 
8801277eed5SGabriel Krisman Bertazi clean_cursor:
8811277eed5SGabriel Krisman Bertazi 	drm_plane_cleanup(cursor);
8821277eed5SGabriel Krisman Bertazi 	kfree(cursor);
883d3e7e42dSGabriel Krisman Bertazi clean_primary:
884d3e7e42dSGabriel Krisman Bertazi 	drm_plane_cleanup(primary);
885d3e7e42dSGabriel Krisman Bertazi 	kfree(primary);
886d3e7e42dSGabriel Krisman Bertazi free_mem:
887d3e7e42dSGabriel Krisman Bertazi 	kfree(qxl_crtc);
888d3e7e42dSGabriel Krisman Bertazi 	return r;
889f64122c1SDave Airlie }
890f64122c1SDave Airlie 
891f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
892f64122c1SDave Airlie {
893f64122c1SDave Airlie 	DRM_DEBUG("\n");
894f64122c1SDave Airlie }
895f64122c1SDave Airlie 
896f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder)
897f64122c1SDave Airlie {
898f64122c1SDave Airlie 	DRM_DEBUG("\n");
899f64122c1SDave Airlie }
900f64122c1SDave Airlie 
901f64122c1SDave Airlie static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
902f64122c1SDave Airlie 		struct drm_encoder *encoder)
903f64122c1SDave Airlie {
904f64122c1SDave Airlie 	int i;
90507f8d9bdSDave Airlie 	struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
906f64122c1SDave Airlie 	struct qxl_head *head;
907f64122c1SDave Airlie 	struct drm_display_mode *mode;
908f64122c1SDave Airlie 
909f64122c1SDave Airlie 	BUG_ON(!encoder);
910f64122c1SDave Airlie 	/* TODO: ugly, do better */
91107f8d9bdSDave Airlie 	i = output->index;
912f64122c1SDave Airlie 	if (!qdev->monitors_config ||
913f64122c1SDave Airlie 	    qdev->monitors_config->max_allowed <= i) {
914f64122c1SDave Airlie 		DRM_ERROR(
915f64122c1SDave Airlie 		"head number too large or missing monitors config: %p, %d",
916f64122c1SDave Airlie 		qdev->monitors_config,
917f64122c1SDave Airlie 		qdev->monitors_config ?
918f64122c1SDave Airlie 			qdev->monitors_config->max_allowed : -1);
919f64122c1SDave Airlie 		return;
920f64122c1SDave Airlie 	}
921f64122c1SDave Airlie 	if (!encoder->crtc) {
922f64122c1SDave Airlie 		DRM_ERROR("missing crtc on encoder %p\n", encoder);
923f64122c1SDave Airlie 		return;
924f64122c1SDave Airlie 	}
925f64122c1SDave Airlie 	if (i != 0)
926f64122c1SDave Airlie 		DRM_DEBUG("missing for multiple monitors: no head holes\n");
927f64122c1SDave Airlie 	head = &qdev->monitors_config->heads[i];
928f64122c1SDave Airlie 	head->id = i;
929f64122c1SDave Airlie 	if (encoder->crtc->enabled) {
930f64122c1SDave Airlie 		mode = &encoder->crtc->mode;
931f64122c1SDave Airlie 		head->width = mode->hdisplay;
932f64122c1SDave Airlie 		head->height = mode->vdisplay;
933f64122c1SDave Airlie 		head->x = encoder->crtc->x;
934f64122c1SDave Airlie 		head->y = encoder->crtc->y;
935f64122c1SDave Airlie 		if (qdev->monitors_config->count < i + 1)
936f64122c1SDave Airlie 			qdev->monitors_config->count = i + 1;
937f64122c1SDave Airlie 	} else {
938f64122c1SDave Airlie 		head->width = 0;
939f64122c1SDave Airlie 		head->height = 0;
940f64122c1SDave Airlie 		head->x = 0;
941f64122c1SDave Airlie 		head->y = 0;
942f64122c1SDave Airlie 	}
94307f8d9bdSDave Airlie 	DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
94407f8d9bdSDave Airlie 		      i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
945f64122c1SDave Airlie 	head->flags = 0;
946f64122c1SDave Airlie 	/* TODO - somewhere else to call this for multiple monitors
947f64122c1SDave Airlie 	 * (config_commit?) */
948f64122c1SDave Airlie 	qxl_send_monitors_config(qdev);
949f64122c1SDave Airlie }
950f64122c1SDave Airlie 
951f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder)
952f64122c1SDave Airlie {
953f64122c1SDave Airlie 	struct qxl_device *qdev = encoder->dev->dev_private;
954f64122c1SDave Airlie 
955f64122c1SDave Airlie 	qxl_write_monitors_config_for_encoder(qdev, encoder);
956f64122c1SDave Airlie 	DRM_DEBUG("\n");
957f64122c1SDave Airlie }
958f64122c1SDave Airlie 
959f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder,
960f64122c1SDave Airlie 				struct drm_display_mode *mode,
961f64122c1SDave Airlie 				struct drm_display_mode *adjusted_mode)
962f64122c1SDave Airlie {
963f64122c1SDave Airlie 	DRM_DEBUG("\n");
964f64122c1SDave Airlie }
965f64122c1SDave Airlie 
966f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
967f64122c1SDave Airlie {
968b0807423SMarc-André Lureau 	unsigned pwidth = 1024;
969b0807423SMarc-André Lureau 	unsigned pheight = 768;
9702d856f94SGerd Hoffmann 	int ret = 0;
971f64122c1SDave Airlie 
972b0807423SMarc-André Lureau 	ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
973f64122c1SDave Airlie 	if (ret < 0)
974f64122c1SDave Airlie 		return ret;
975b0807423SMarc-André Lureau 	ret += qxl_add_common_modes(connector, pwidth, pheight);
976f64122c1SDave Airlie 	return ret;
977f64122c1SDave Airlie }
978f64122c1SDave Airlie 
979f64122c1SDave Airlie static int qxl_conn_mode_valid(struct drm_connector *connector,
980f64122c1SDave Airlie 			       struct drm_display_mode *mode)
981f64122c1SDave Airlie {
982bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
983bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
984bd3e1c7cSJonathon Jongsma 	int i;
985bd3e1c7cSJonathon Jongsma 
986f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
987f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
988bd3e1c7cSJonathon Jongsma 
989bd3e1c7cSJonathon Jongsma 	if(qdev->monitors_config_width == mode->hdisplay &&
990bd3e1c7cSJonathon Jongsma 	   qdev->monitors_config_height == mode->vdisplay)
991f64122c1SDave Airlie 		return MODE_OK;
992bd3e1c7cSJonathon Jongsma 
993bd3e1c7cSJonathon Jongsma 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
994bd3e1c7cSJonathon Jongsma 		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
995bd3e1c7cSJonathon Jongsma 			return MODE_OK;
996bd3e1c7cSJonathon Jongsma 	}
997bd3e1c7cSJonathon Jongsma 	return MODE_BAD;
998f64122c1SDave Airlie }
999f64122c1SDave Airlie 
10006d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1001f64122c1SDave Airlie {
1002f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1003f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1004f64122c1SDave Airlie 
1005f64122c1SDave Airlie 	DRM_DEBUG("\n");
1006f64122c1SDave Airlie 	return &qxl_output->enc;
1007f64122c1SDave Airlie }
1008f64122c1SDave Airlie 
1009f64122c1SDave Airlie 
1010f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1011f64122c1SDave Airlie 	.dpms = qxl_enc_dpms,
1012f64122c1SDave Airlie 	.prepare = qxl_enc_prepare,
1013f64122c1SDave Airlie 	.mode_set = qxl_enc_mode_set,
1014f64122c1SDave Airlie 	.commit = qxl_enc_commit,
1015f64122c1SDave Airlie };
1016f64122c1SDave Airlie 
1017f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1018f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
1019f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
1020f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
1021f64122c1SDave Airlie };
1022f64122c1SDave Airlie 
1023f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1024f64122c1SDave Airlie 			struct drm_connector *connector,
1025f64122c1SDave Airlie 			bool force)
1026f64122c1SDave Airlie {
1027f64122c1SDave Airlie 	struct qxl_output *output =
1028f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1029f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
1030f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
103169e5d3f8SDave Airlie 	bool connected = false;
1032f64122c1SDave Airlie 
1033f64122c1SDave Airlie 	/* The first monitor is always connected */
103469e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
103569e5d3f8SDave Airlie 		if (output->index == 0)
103669e5d3f8SDave Airlie 			connected = true;
103769e5d3f8SDave Airlie 	} else
103869e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
103969e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1040f64122c1SDave Airlie 
10415cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10425cab51cbSMarc-André Lureau 	if (!connected)
10435cab51cbSMarc-André Lureau 		qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
10445cab51cbSMarc-André Lureau 
1045f64122c1SDave Airlie 	return connected ? connector_status_connected
1046f64122c1SDave Airlie 			 : connector_status_disconnected;
1047f64122c1SDave Airlie }
1048f64122c1SDave Airlie 
1049f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector,
1050f64122c1SDave Airlie 				   struct drm_property *property,
1051f64122c1SDave Airlie 				   uint64_t value)
1052f64122c1SDave Airlie {
1053f64122c1SDave Airlie 	DRM_DEBUG("\n");
1054f64122c1SDave Airlie 	return 0;
1055f64122c1SDave Airlie }
1056f64122c1SDave Airlie 
1057f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1058f64122c1SDave Airlie {
1059f64122c1SDave Airlie 	struct qxl_output *qxl_output =
1060f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
1061f64122c1SDave Airlie 
106234ea3d38SThomas Wood 	drm_connector_unregister(connector);
1063f64122c1SDave Airlie 	drm_connector_cleanup(connector);
1064f64122c1SDave Airlie 	kfree(qxl_output);
1065f64122c1SDave Airlie }
1066f64122c1SDave Airlie 
1067f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1068f64122c1SDave Airlie 	.dpms = drm_helper_connector_dpms,
1069f64122c1SDave Airlie 	.detect = qxl_conn_detect,
10706af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
1071f64122c1SDave Airlie 	.set_property = qxl_conn_set_property,
1072f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
10739ade8b98SGabriel Krisman Bertazi 	.reset = drm_atomic_helper_connector_reset,
10749ade8b98SGabriel Krisman Bertazi 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
10759ade8b98SGabriel Krisman Bertazi 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1076f64122c1SDave Airlie };
1077f64122c1SDave Airlie 
1078f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
1079f64122c1SDave Airlie {
1080f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
1081f64122c1SDave Airlie }
1082f64122c1SDave Airlie 
1083f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
1084f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
1085f64122c1SDave Airlie };
1086f64122c1SDave Airlie 
10874695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
10884695b039SDave Airlie {
10894695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
10904695b039SDave Airlie 		return 0;
10914695b039SDave Airlie 
10924695b039SDave Airlie 	qdev->hotplug_mode_update_property =
1093cbdded7fSGabriel Krisman Bertazi 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
10944695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
10954695b039SDave Airlie 
10964695b039SDave Airlie 	return 0;
10974695b039SDave Airlie }
10984695b039SDave Airlie 
10996d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1100f64122c1SDave Airlie {
11014695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
1102f64122c1SDave Airlie 	struct qxl_output *qxl_output;
1103f64122c1SDave Airlie 	struct drm_connector *connector;
1104f64122c1SDave Airlie 	struct drm_encoder *encoder;
1105f64122c1SDave Airlie 
1106f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1107f64122c1SDave Airlie 	if (!qxl_output)
1108f64122c1SDave Airlie 		return -ENOMEM;
1109f64122c1SDave Airlie 
1110f64122c1SDave Airlie 	qxl_output->index = num_output;
1111f64122c1SDave Airlie 
1112f64122c1SDave Airlie 	connector = &qxl_output->base;
1113f64122c1SDave Airlie 	encoder = &qxl_output->enc;
1114f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
1115f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1116f64122c1SDave Airlie 
1117f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
111813a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
1119f64122c1SDave Airlie 
11205ff91e44SDave Airlie 	/* we get HPD via client monitors config */
11215ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1122f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
1123f64122c1SDave Airlie 	drm_mode_connector_attach_encoder(&qxl_output->base,
1124f64122c1SDave Airlie 					  &qxl_output->enc);
1125f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1126f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1127f64122c1SDave Airlie 
11284695b039SDave Airlie 	drm_object_attach_property(&connector->base,
11294695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
11307dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11317dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
11327dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
11337dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
1134f64122c1SDave Airlie 	return 0;
1135f64122c1SDave Airlie }
1136f64122c1SDave Airlie 
1137f64122c1SDave Airlie static struct drm_framebuffer *
1138f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1139f64122c1SDave Airlie 			    struct drm_file *file_priv,
11401eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1141f64122c1SDave Airlie {
1142f64122c1SDave Airlie 	struct drm_gem_object *obj;
1143f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb;
1144f64122c1SDave Airlie 	int ret;
1145f64122c1SDave Airlie 
1146a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1147a8ad0bd8SChris Wilson 	if (!obj)
1148a8ad0bd8SChris Wilson 		return NULL;
1149f64122c1SDave Airlie 
1150f64122c1SDave Airlie 	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1151f64122c1SDave Airlie 	if (qxl_fb == NULL)
1152f64122c1SDave Airlie 		return NULL;
1153f64122c1SDave Airlie 
11546819c3c2SNoralf Trønnes 	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1155f64122c1SDave Airlie 	if (ret) {
1156f64122c1SDave Airlie 		kfree(qxl_fb);
1157f64122c1SDave Airlie 		drm_gem_object_unreference_unlocked(obj);
1158f64122c1SDave Airlie 		return NULL;
1159f64122c1SDave Airlie 	}
1160f64122c1SDave Airlie 
1161f64122c1SDave Airlie 	return &qxl_fb->base;
1162f64122c1SDave Airlie }
1163f64122c1SDave Airlie 
1164f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1165f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1166472e6d46SGabriel Krisman Bertazi 	.atomic_check = drm_atomic_helper_check,
1167472e6d46SGabriel Krisman Bertazi 	.atomic_commit = drm_atomic_helper_commit,
1168f64122c1SDave Airlie };
1169f64122c1SDave Airlie 
11702bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1171f64122c1SDave Airlie {
1172f64122c1SDave Airlie 	int ret;
1173f64122c1SDave Airlie 	struct drm_gem_object *gobj;
117407f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
1175f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1176f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
1177f64122c1SDave Airlie 
1178f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1179f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1180f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1181f64122c1SDave Airlie 	if (ret) {
1182f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1183f64122c1SDave Airlie 		return -ENOMEM;
1184f64122c1SDave Airlie 	}
1185f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
11862bd6ce84SDave Airlie 
1187715a11faSGabriel Krisman Bertazi 	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
11882bd6ce84SDave Airlie 	if (ret)
11892bd6ce84SDave Airlie 		return ret;
11902bd6ce84SDave Airlie 
1191f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
11922bd6ce84SDave Airlie 
1193f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1194f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1195f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1196f64122c1SDave Airlie 
1197f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
1198f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
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 	qdev->mode_info.mode_config_initialized = true;
1249f64122c1SDave Airlie 
12509ade8b98SGabriel Krisman Bertazi 	drm_mode_config_reset(&qdev->ddev);
12519ade8b98SGabriel Krisman Bertazi 
1252f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
1253f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
1254f64122c1SDave Airlie 	 * spice server. */
1255f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
1256f64122c1SDave Airlie 	return 0;
1257f64122c1SDave Airlie }
1258f64122c1SDave Airlie 
1259f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1260f64122c1SDave Airlie {
1261f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
12622bd6ce84SDave Airlie 
12632bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1264f64122c1SDave Airlie 	if (qdev->mode_info.mode_config_initialized) {
1265cbdded7fSGabriel Krisman Bertazi 		drm_mode_config_cleanup(&qdev->ddev);
1266f64122c1SDave Airlie 		qdev->mode_info.mode_config_initialized = false;
1267f64122c1SDave Airlie 	}
1268f64122c1SDave Airlie }
1269