xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision a8ad0bd84f986072314595d05444719fdf29e412)
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 
26f64122c1SDave Airlie 
27c5416d66SRandy Dunlap #include <linux/crc32.h>
28f64122c1SDave Airlie 
29f64122c1SDave Airlie #include "qxl_drv.h"
30f64122c1SDave Airlie #include "qxl_object.h"
31f64122c1SDave Airlie #include "drm_crtc_helper.h"
323cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h>
33f64122c1SDave Airlie 
3407f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
3507f8d9bdSDave Airlie {
3607f8d9bdSDave Airlie 	return head->width && head->height;
3707f8d9bdSDave Airlie }
3807f8d9bdSDave Airlie 
39f64122c1SDave Airlie void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
40f64122c1SDave Airlie {
41f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
42f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
43f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
4462c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
45f64122c1SDave Airlie 	}
46f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
47f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
48f64122c1SDave Airlie 				sizeof(struct qxl_monitors_config) +
49f64122c1SDave Airlie 				sizeof(struct qxl_head) * count, GFP_KERNEL);
50f64122c1SDave Airlie 		if (!qdev->client_monitors_config) {
51f64122c1SDave Airlie 			qxl_io_log(qdev,
52f64122c1SDave Airlie 				   "%s: allocation failure for %u heads\n",
53f64122c1SDave Airlie 				   __func__, count);
54f64122c1SDave Airlie 			return;
55f64122c1SDave Airlie 		}
56f64122c1SDave Airlie 	}
57f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
58f64122c1SDave Airlie }
59f64122c1SDave Airlie 
60f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
61f64122c1SDave Airlie {
62f64122c1SDave Airlie 	int i;
63f64122c1SDave Airlie 	int num_monitors;
64f64122c1SDave Airlie 	uint32_t crc;
65f64122c1SDave Airlie 
66f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
67f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
68f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
69f64122c1SDave Airlie 	if (crc != qdev->rom->client_monitors_config_crc) {
7072ec5650SFrediano Ziglio 		qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
71f64122c1SDave Airlie 			   sizeof(qdev->rom->client_monitors_config),
72f64122c1SDave Airlie 			   qdev->rom->client_monitors_config_crc);
73f64122c1SDave Airlie 		return 1;
74f64122c1SDave Airlie 	}
75f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
765b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
77f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
78f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
79f64122c1SDave Airlie 	} else {
80f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
81f64122c1SDave Airlie 	}
82f64122c1SDave Airlie 	qxl_alloc_client_monitors_config(qdev, num_monitors);
83f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
84f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
85f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
86f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
87f64122c1SDave Airlie 		struct qxl_urect *c_rect =
88f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
89f64122c1SDave Airlie 		struct qxl_head *client_head =
90f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
9107f8d9bdSDave Airlie 		client_head->x = c_rect->left;
9207f8d9bdSDave Airlie 		client_head->y = c_rect->top;
9307f8d9bdSDave Airlie 		client_head->width = c_rect->right - c_rect->left;
9407f8d9bdSDave Airlie 		client_head->height = c_rect->bottom - c_rect->top;
9507f8d9bdSDave Airlie 		client_head->surface_id = 0;
9607f8d9bdSDave Airlie 		client_head->id = i;
9707f8d9bdSDave Airlie 		client_head->flags = 0;
9807f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
9907f8d9bdSDave Airlie 			  client_head->x, client_head->y);
100f64122c1SDave Airlie 	}
101f64122c1SDave Airlie 	return 0;
102f64122c1SDave Airlie }
103f64122c1SDave Airlie 
1047dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1057dea0941SDave Airlie {
1067dea0941SDave Airlie 	struct drm_device *dev = qdev->ddev;
1077dea0941SDave Airlie 	struct drm_connector *connector;
1087dea0941SDave Airlie 	struct qxl_output *output;
1097dea0941SDave Airlie 	struct qxl_head *head;
1107dea0941SDave Airlie 
1117dea0941SDave Airlie 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1127dea0941SDave Airlie 		output = drm_connector_to_qxl_output(connector);
1137dea0941SDave Airlie 
1147dea0941SDave Airlie 		head = &qdev->client_monitors_config->heads[output->index];
1157dea0941SDave Airlie 
1167dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1177dea0941SDave Airlie 			dev->mode_config.suggested_x_property, head->x);
1187dea0941SDave Airlie 		drm_object_property_set_value(&connector->base,
1197dea0941SDave Airlie 			dev->mode_config.suggested_y_property, head->y);
1207dea0941SDave Airlie 	}
1217dea0941SDave Airlie }
1227dea0941SDave Airlie 
123f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
124f64122c1SDave Airlie {
125f64122c1SDave Airlie 
1267dea0941SDave Airlie 	struct drm_device *dev = qdev->ddev;
127f64122c1SDave Airlie 	while (qxl_display_copy_rom_client_monitors_config(qdev)) {
128f64122c1SDave Airlie 		qxl_io_log(qdev, "failed crc check for client_monitors_config,"
129f64122c1SDave Airlie 				 " retrying\n");
130f64122c1SDave Airlie 	}
1314fdb0869SMarc-André Lureau 
1327dea0941SDave Airlie 	drm_modeset_lock_all(dev);
1337dea0941SDave Airlie 	qxl_update_offset_props(qdev);
1347dea0941SDave Airlie 	drm_modeset_unlock_all(dev);
1354fdb0869SMarc-André Lureau 	if (!drm_helper_hpd_irq_event(qdev->ddev)) {
1364fdb0869SMarc-André Lureau 		/* notify that the monitor configuration changed, to
1374fdb0869SMarc-André Lureau 		   adjust at the arbitrary resolution */
1384fdb0869SMarc-André Lureau 		drm_kms_helper_hotplug_event(qdev->ddev);
1394fdb0869SMarc-André Lureau 	}
140f64122c1SDave Airlie }
141f64122c1SDave Airlie 
142b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector,
143b0807423SMarc-André Lureau                                          unsigned *pwidth,
144b0807423SMarc-André Lureau                                          unsigned *pheight)
145f64122c1SDave Airlie {
146f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
147f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
148f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
149f64122c1SDave Airlie 	int h = output->index;
150f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
151f64122c1SDave Airlie 	struct qxl_head *head;
152f64122c1SDave Airlie 
15307f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
154f64122c1SDave Airlie 		return 0;
15507f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
156f64122c1SDave Airlie 
157f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
158f64122c1SDave Airlie 			    false);
159f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
160b0807423SMarc-André Lureau 	*pwidth = head->width;
161b0807423SMarc-André Lureau 	*pheight = head->height;
162f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
163bd3e1c7cSJonathon Jongsma 	/* remember the last custom size for mode validation */
164bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_width = mode->hdisplay;
165bd3e1c7cSJonathon Jongsma 	qdev->monitors_config_height = mode->vdisplay;
166f64122c1SDave Airlie 	return 1;
167f64122c1SDave Airlie }
168f64122c1SDave Airlie 
169bd3e1c7cSJonathon Jongsma static struct mode_size {
170f64122c1SDave Airlie 	int w;
171f64122c1SDave Airlie 	int h;
172f64122c1SDave Airlie } common_modes[] = {
173f64122c1SDave Airlie 	{ 640,  480},
174f64122c1SDave Airlie 	{ 720,  480},
175f64122c1SDave Airlie 	{ 800,  600},
176f64122c1SDave Airlie 	{ 848,  480},
177f64122c1SDave Airlie 	{1024,  768},
178f64122c1SDave Airlie 	{1152,  768},
179f64122c1SDave Airlie 	{1280,  720},
180f64122c1SDave Airlie 	{1280,  800},
181f64122c1SDave Airlie 	{1280,  854},
182f64122c1SDave Airlie 	{1280,  960},
183f64122c1SDave Airlie 	{1280, 1024},
184f64122c1SDave Airlie 	{1440,  900},
185f64122c1SDave Airlie 	{1400, 1050},
186f64122c1SDave Airlie 	{1680, 1050},
187f64122c1SDave Airlie 	{1600, 1200},
188f64122c1SDave Airlie 	{1920, 1080},
189f64122c1SDave Airlie 	{1920, 1200}
190f64122c1SDave Airlie };
191f64122c1SDave Airlie 
192bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector,
193bd3e1c7cSJonathon Jongsma                                 unsigned pwidth,
194bd3e1c7cSJonathon Jongsma                                 unsigned pheight)
195bd3e1c7cSJonathon Jongsma {
196bd3e1c7cSJonathon Jongsma 	struct drm_device *dev = connector->dev;
197bd3e1c7cSJonathon Jongsma 	struct drm_display_mode *mode = NULL;
198bd3e1c7cSJonathon Jongsma 	int i;
199f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
200f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
201f64122c1SDave Airlie 				    60, false, false, false);
202b0807423SMarc-André Lureau 		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
203f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
204f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
205f64122c1SDave Airlie 	}
206f64122c1SDave Airlie 	return i - 1;
207f64122c1SDave Airlie }
208f64122c1SDave Airlie 
209f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
210f64122c1SDave Airlie {
211f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
212f64122c1SDave Airlie 
213f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
214f64122c1SDave Airlie 	kfree(qxl_crtc);
215f64122c1SDave Airlie }
216f64122c1SDave Airlie 
217058e9f5cSAndreas Pokorny static int qxl_crtc_page_flip(struct drm_crtc *crtc,
218058e9f5cSAndreas Pokorny                               struct drm_framebuffer *fb,
219058e9f5cSAndreas Pokorny                               struct drm_pending_vblank_event *event,
220058e9f5cSAndreas Pokorny                               uint32_t page_flip_flags)
221058e9f5cSAndreas Pokorny {
222058e9f5cSAndreas Pokorny 	struct drm_device *dev = crtc->dev;
223058e9f5cSAndreas Pokorny 	struct qxl_device *qdev = dev->dev_private;
224058e9f5cSAndreas Pokorny 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
225058e9f5cSAndreas Pokorny 	struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
226058e9f5cSAndreas Pokorny 	struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
227058e9f5cSAndreas Pokorny 	struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
228058e9f5cSAndreas Pokorny 	struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
229058e9f5cSAndreas Pokorny 	unsigned long flags;
230058e9f5cSAndreas Pokorny 	struct drm_clip_rect norect = {
231058e9f5cSAndreas Pokorny 	    .x1 = 0,
232058e9f5cSAndreas Pokorny 	    .y1 = 0,
233058e9f5cSAndreas Pokorny 	    .x2 = fb->width,
234058e9f5cSAndreas Pokorny 	    .y2 = fb->height
235058e9f5cSAndreas Pokorny 	};
236058e9f5cSAndreas Pokorny 	int inc = 1;
237058e9f5cSAndreas Pokorny 	int one_clip_rect = 1;
238058e9f5cSAndreas Pokorny 	int ret = 0;
239058e9f5cSAndreas Pokorny 
240058e9f5cSAndreas Pokorny 	crtc->primary->fb = fb;
241058e9f5cSAndreas Pokorny 	bo_old->is_primary = false;
242058e9f5cSAndreas Pokorny 	bo->is_primary = true;
243058e9f5cSAndreas Pokorny 
244058e9f5cSAndreas Pokorny 	ret = qxl_bo_reserve(bo, false);
245058e9f5cSAndreas Pokorny 	if (ret)
246058e9f5cSAndreas Pokorny 		return ret;
2477eb9974fSFrediano Ziglio 	ret = qxl_bo_pin(bo, bo->type, NULL);
2487eb9974fSFrediano Ziglio 	qxl_bo_unreserve(bo);
2497eb9974fSFrediano Ziglio 	if (ret)
2507eb9974fSFrediano Ziglio 		return ret;
251058e9f5cSAndreas Pokorny 
252058e9f5cSAndreas Pokorny 	qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
253058e9f5cSAndreas Pokorny 			  &norect, one_clip_rect, inc);
254058e9f5cSAndreas Pokorny 
255058e9f5cSAndreas Pokorny 	drm_vblank_get(dev, qcrtc->index);
256058e9f5cSAndreas Pokorny 
257058e9f5cSAndreas Pokorny 	if (event) {
258058e9f5cSAndreas Pokorny 		spin_lock_irqsave(&dev->event_lock, flags);
259058e9f5cSAndreas Pokorny 		drm_send_vblank_event(dev, qcrtc->index, event);
260058e9f5cSAndreas Pokorny 		spin_unlock_irqrestore(&dev->event_lock, flags);
261058e9f5cSAndreas Pokorny 	}
262058e9f5cSAndreas Pokorny 	drm_vblank_put(dev, qcrtc->index);
263058e9f5cSAndreas Pokorny 
2647eb9974fSFrediano Ziglio 	ret = qxl_bo_reserve(bo, false);
2657eb9974fSFrediano Ziglio 	if (!ret) {
2667eb9974fSFrediano Ziglio 		qxl_bo_unpin(bo);
267058e9f5cSAndreas Pokorny 		qxl_bo_unreserve(bo);
2687eb9974fSFrediano Ziglio 	}
269058e9f5cSAndreas Pokorny 
270058e9f5cSAndreas Pokorny 	return 0;
271058e9f5cSAndreas Pokorny }
272058e9f5cSAndreas Pokorny 
2738002db63SDave Airlie static int
274f64122c1SDave Airlie qxl_hide_cursor(struct qxl_device *qdev)
275f64122c1SDave Airlie {
276f64122c1SDave Airlie 	struct qxl_release *release;
277f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
278f64122c1SDave Airlie 	int ret;
279f64122c1SDave Airlie 
280f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
281f64122c1SDave Airlie 					 &release, NULL);
2828002db63SDave Airlie 	if (ret)
2838002db63SDave Airlie 		return ret;
2848002db63SDave Airlie 
2858002db63SDave Airlie 	ret = qxl_release_reserve_list(release, true);
2868002db63SDave Airlie 	if (ret) {
2878002db63SDave Airlie 		qxl_release_free(qdev, release);
2888002db63SDave Airlie 		return ret;
2898002db63SDave Airlie 	}
290f64122c1SDave Airlie 
291f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
292f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_HIDE;
293f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
294f64122c1SDave Airlie 
295f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
2968002db63SDave Airlie 	qxl_release_fence_buffer_objects(release);
2978002db63SDave Airlie 	return 0;
298f64122c1SDave Airlie }
299f64122c1SDave Airlie 
300c0a60802SDave Airlie static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
301f64122c1SDave Airlie 				struct drm_file *file_priv,
302f64122c1SDave Airlie 				uint32_t handle,
303f64122c1SDave Airlie 				uint32_t width,
304c0a60802SDave Airlie 				uint32_t height, int32_t hot_x, int32_t hot_y)
305f64122c1SDave Airlie {
306f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
307f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
308f64122c1SDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
309f64122c1SDave Airlie 	struct drm_gem_object *obj;
310f64122c1SDave Airlie 	struct qxl_cursor *cursor;
311f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
312f64122c1SDave Airlie 	struct qxl_bo *cursor_bo, *user_bo;
313f64122c1SDave Airlie 	struct qxl_release *release;
314f64122c1SDave Airlie 	void *user_ptr;
315f64122c1SDave Airlie 
316f64122c1SDave Airlie 	int size = 64*64*4;
317f64122c1SDave Airlie 	int ret = 0;
3188002db63SDave Airlie 	if (!handle)
3198002db63SDave Airlie 		return qxl_hide_cursor(qdev);
320f64122c1SDave Airlie 
321*a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, handle);
322f64122c1SDave Airlie 	if (!obj) {
323f64122c1SDave Airlie 		DRM_ERROR("cannot find cursor object\n");
324f64122c1SDave Airlie 		return -ENOENT;
325f64122c1SDave Airlie 	}
326f64122c1SDave Airlie 
327f64122c1SDave Airlie 	user_bo = gem_to_qxl_bo(obj);
328f64122c1SDave Airlie 
329f64122c1SDave Airlie 	ret = qxl_bo_reserve(user_bo, false);
330f64122c1SDave Airlie 	if (ret)
331f64122c1SDave Airlie 		goto out_unref;
332f64122c1SDave Airlie 
333f64122c1SDave Airlie 	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
3348002db63SDave Airlie 	qxl_bo_unreserve(user_bo);
335f64122c1SDave Airlie 	if (ret)
3368002db63SDave Airlie 		goto out_unref;
337f64122c1SDave Airlie 
338f64122c1SDave Airlie 	ret = qxl_bo_kmap(user_bo, &user_ptr);
339f64122c1SDave Airlie 	if (ret)
340f64122c1SDave Airlie 		goto out_unpin;
341f64122c1SDave Airlie 
342f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
343f64122c1SDave Airlie 					 QXL_RELEASE_CURSOR_CMD,
344f64122c1SDave Airlie 					 &release, NULL);
345f64122c1SDave Airlie 	if (ret)
346f64122c1SDave Airlie 		goto out_kunmap;
3478002db63SDave Airlie 
3488002db63SDave Airlie 	ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
349f64122c1SDave Airlie 			   &cursor_bo);
350f64122c1SDave Airlie 	if (ret)
351f64122c1SDave Airlie 		goto out_free_release;
3528002db63SDave Airlie 
3538002db63SDave Airlie 	ret = qxl_release_reserve_list(release, false);
354f64122c1SDave Airlie 	if (ret)
355f64122c1SDave Airlie 		goto out_free_bo;
356f64122c1SDave Airlie 
3578002db63SDave Airlie 	ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
3588002db63SDave Airlie 	if (ret)
3598002db63SDave Airlie 		goto out_backoff;
3608002db63SDave Airlie 
361f64122c1SDave Airlie 	cursor->header.unique = 0;
362f64122c1SDave Airlie 	cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
363f64122c1SDave Airlie 	cursor->header.width = 64;
364f64122c1SDave Airlie 	cursor->header.height = 64;
365c0a60802SDave Airlie 	cursor->header.hot_spot_x = hot_x;
366c0a60802SDave Airlie 	cursor->header.hot_spot_y = hot_y;
367f64122c1SDave Airlie 	cursor->data_size = size;
368f64122c1SDave Airlie 	cursor->chunk.next_chunk = 0;
369f64122c1SDave Airlie 	cursor->chunk.prev_chunk = 0;
370f64122c1SDave Airlie 	cursor->chunk.data_size = size;
371f64122c1SDave Airlie 
372f64122c1SDave Airlie 	memcpy(cursor->chunk.data, user_ptr, size);
373f64122c1SDave Airlie 
374f64122c1SDave Airlie 	qxl_bo_kunmap(cursor_bo);
375f64122c1SDave Airlie 
376f64122c1SDave Airlie 	qxl_bo_kunmap(user_bo);
377f64122c1SDave Airlie 
378d59a1f71SJohn Keeping 	qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
379d59a1f71SJohn Keeping 	qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
380d59a1f71SJohn Keeping 	qcrtc->hot_spot_x = hot_x;
381d59a1f71SJohn Keeping 	qcrtc->hot_spot_y = hot_y;
382d59a1f71SJohn Keeping 
383f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
384f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_SET;
385d59a1f71SJohn Keeping 	cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
386d59a1f71SJohn Keeping 	cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
387f64122c1SDave Airlie 
388f64122c1SDave Airlie 	cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
389f64122c1SDave Airlie 
390f64122c1SDave Airlie 	cmd->u.set.visible = 1;
391f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
392f64122c1SDave Airlie 
393f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
3948002db63SDave Airlie 	qxl_release_fence_buffer_objects(release);
395f64122c1SDave Airlie 
3968002db63SDave Airlie 	/* finish with the userspace bo */
3978002db63SDave Airlie 	ret = qxl_bo_reserve(user_bo, false);
3988002db63SDave Airlie 	if (!ret) {
3998002db63SDave Airlie 		qxl_bo_unpin(user_bo);
4008002db63SDave Airlie 		qxl_bo_unreserve(user_bo);
4018002db63SDave Airlie 	}
4028002db63SDave Airlie 	drm_gem_object_unreference_unlocked(obj);
4038002db63SDave Airlie 
404f64122c1SDave Airlie 	qxl_bo_unref(&cursor_bo);
405f64122c1SDave Airlie 
406f64122c1SDave Airlie 	return ret;
4078002db63SDave Airlie 
4088002db63SDave Airlie out_backoff:
4098002db63SDave Airlie 	qxl_release_backoff_reserve_list(release);
410f64122c1SDave Airlie out_free_bo:
411f64122c1SDave Airlie 	qxl_bo_unref(&cursor_bo);
412f64122c1SDave Airlie out_free_release:
413f64122c1SDave Airlie 	qxl_release_free(qdev, release);
414f64122c1SDave Airlie out_kunmap:
415f64122c1SDave Airlie 	qxl_bo_kunmap(user_bo);
416f64122c1SDave Airlie out_unpin:
417f64122c1SDave Airlie 	qxl_bo_unpin(user_bo);
418f64122c1SDave Airlie out_unref:
419f64122c1SDave Airlie 	drm_gem_object_unreference_unlocked(obj);
420f64122c1SDave Airlie 	return ret;
421f64122c1SDave Airlie }
422f64122c1SDave Airlie 
423f64122c1SDave Airlie static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
424f64122c1SDave Airlie 				int x, int y)
425f64122c1SDave Airlie {
426f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
427f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
428f64122c1SDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
429f64122c1SDave Airlie 	struct qxl_release *release;
430f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
431f64122c1SDave Airlie 	int ret;
432f64122c1SDave Airlie 
433f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
434f64122c1SDave Airlie 				   &release, NULL);
4358002db63SDave Airlie 	if (ret)
4368002db63SDave Airlie 		return ret;
4378002db63SDave Airlie 
4388002db63SDave Airlie 	ret = qxl_release_reserve_list(release, true);
4398002db63SDave Airlie 	if (ret) {
4408002db63SDave Airlie 		qxl_release_free(qdev, release);
4418002db63SDave Airlie 		return ret;
4428002db63SDave Airlie 	}
443f64122c1SDave Airlie 
444f64122c1SDave Airlie 	qcrtc->cur_x = x;
445f64122c1SDave Airlie 	qcrtc->cur_y = y;
446f64122c1SDave Airlie 
447f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
448f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_MOVE;
449d59a1f71SJohn Keeping 	cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
450d59a1f71SJohn Keeping 	cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
451f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
452f64122c1SDave Airlie 
453f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
4548002db63SDave Airlie 	qxl_release_fence_buffer_objects(release);
4558002db63SDave Airlie 
456f64122c1SDave Airlie 	return 0;
457f64122c1SDave Airlie }
458f64122c1SDave Airlie 
459f64122c1SDave Airlie 
460f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
461c0a60802SDave Airlie 	.cursor_set2 = qxl_crtc_cursor_set2,
462f64122c1SDave Airlie 	.cursor_move = qxl_crtc_cursor_move,
463f64122c1SDave Airlie 	.set_config = drm_crtc_helper_set_config,
464f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
465058e9f5cSAndreas Pokorny 	.page_flip = qxl_crtc_page_flip,
466f64122c1SDave Airlie };
467f64122c1SDave Airlie 
4686819c3c2SNoralf Trønnes void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
469f64122c1SDave Airlie {
470f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
471f64122c1SDave Airlie 
472f64122c1SDave Airlie 	if (qxl_fb->obj)
473f64122c1SDave Airlie 		drm_gem_object_unreference_unlocked(qxl_fb->obj);
474f64122c1SDave Airlie 	drm_framebuffer_cleanup(fb);
475f64122c1SDave Airlie 	kfree(qxl_fb);
476f64122c1SDave Airlie }
477f64122c1SDave Airlie 
4786d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
479f64122c1SDave Airlie 					 struct drm_file *file_priv,
480f64122c1SDave Airlie 					 unsigned flags, unsigned color,
481f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
482f64122c1SDave Airlie 					 unsigned num_clips)
483f64122c1SDave Airlie {
484f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
485f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
486f64122c1SDave Airlie 	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
487f64122c1SDave Airlie 	struct drm_clip_rect norect;
488f64122c1SDave Airlie 	struct qxl_bo *qobj;
489f64122c1SDave Airlie 	int inc = 1;
490f64122c1SDave Airlie 
49173e9efd4SVille Syrjälä 	drm_modeset_lock_all(fb->dev);
49273e9efd4SVille Syrjälä 
493f64122c1SDave Airlie 	qobj = gem_to_qxl_bo(qxl_fb->obj);
494b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
49573e9efd4SVille Syrjälä 	if (!qobj->is_primary) {
49673e9efd4SVille Syrjälä 		drm_modeset_unlock_all(fb->dev);
497b2b4465dSDave Airlie 		return 0;
49873e9efd4SVille Syrjälä 	}
499b2b4465dSDave Airlie 
500f64122c1SDave Airlie 	if (!num_clips) {
501f64122c1SDave Airlie 		num_clips = 1;
502f64122c1SDave Airlie 		clips = &norect;
503f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
504f64122c1SDave Airlie 		norect.x2 = fb->width;
505f64122c1SDave Airlie 		norect.y2 = fb->height;
506f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
507f64122c1SDave Airlie 		num_clips /= 2;
508f64122c1SDave Airlie 		inc = 2; /* skip source rects */
509f64122c1SDave Airlie 	}
510f64122c1SDave Airlie 
511f64122c1SDave Airlie 	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
512f64122c1SDave Airlie 			  clips, num_clips, inc);
51373e9efd4SVille Syrjälä 
51473e9efd4SVille Syrjälä 	drm_modeset_unlock_all(fb->dev);
51573e9efd4SVille Syrjälä 
516f64122c1SDave Airlie 	return 0;
517f64122c1SDave Airlie }
518f64122c1SDave Airlie 
519f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
520f64122c1SDave Airlie 	.destroy = qxl_user_framebuffer_destroy,
521f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
522f64122c1SDave Airlie /*	TODO?
523f64122c1SDave Airlie  *	.create_handle = qxl_user_framebuffer_create_handle, */
524f64122c1SDave Airlie };
525f64122c1SDave Airlie 
526f64122c1SDave Airlie int
527f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev,
528f64122c1SDave Airlie 		     struct qxl_framebuffer *qfb,
5291eb83451SVille Syrjälä 		     const struct drm_mode_fb_cmd2 *mode_cmd,
5306819c3c2SNoralf Trønnes 		     struct drm_gem_object *obj,
5316819c3c2SNoralf Trønnes 		     const struct drm_framebuffer_funcs *funcs)
532f64122c1SDave Airlie {
533f64122c1SDave Airlie 	int ret;
534f64122c1SDave Airlie 
535f64122c1SDave Airlie 	qfb->obj = obj;
5366819c3c2SNoralf Trønnes 	ret = drm_framebuffer_init(dev, &qfb->base, funcs);
537f64122c1SDave Airlie 	if (ret) {
538f64122c1SDave Airlie 		qfb->obj = NULL;
539f64122c1SDave Airlie 		return ret;
540f64122c1SDave Airlie 	}
541f64122c1SDave Airlie 	drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
542f64122c1SDave Airlie 	return 0;
543f64122c1SDave Airlie }
544f64122c1SDave Airlie 
545f64122c1SDave Airlie static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
546f64122c1SDave Airlie {
547f64122c1SDave Airlie }
548f64122c1SDave Airlie 
549f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
550f64122c1SDave Airlie 				  const struct drm_display_mode *mode,
551f64122c1SDave Airlie 				  struct drm_display_mode *adjusted_mode)
552f64122c1SDave Airlie {
553f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
554f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
555f64122c1SDave Airlie 
556f64122c1SDave Airlie 	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
557f64122c1SDave Airlie 		   __func__,
558f64122c1SDave Airlie 		   mode->hdisplay, mode->vdisplay,
559f64122c1SDave Airlie 		   adjusted_mode->hdisplay,
560f64122c1SDave Airlie 		   adjusted_mode->vdisplay);
561f64122c1SDave Airlie 	return true;
562f64122c1SDave Airlie }
563f64122c1SDave Airlie 
564f64122c1SDave Airlie void
565f64122c1SDave Airlie qxl_send_monitors_config(struct qxl_device *qdev)
566f64122c1SDave Airlie {
567f64122c1SDave Airlie 	int i;
568f64122c1SDave Airlie 
569f64122c1SDave Airlie 	BUG_ON(!qdev->ram_header->monitors_config);
570f64122c1SDave Airlie 
571f64122c1SDave Airlie 	if (qdev->monitors_config->count == 0) {
572f64122c1SDave Airlie 		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
573f64122c1SDave Airlie 		return;
574f64122c1SDave Airlie 	}
575f64122c1SDave Airlie 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
576f64122c1SDave Airlie 		struct qxl_head *head = &qdev->monitors_config->heads[i];
577f64122c1SDave Airlie 
57807f8d9bdSDave Airlie 		if (head->y > 8192 || head->x > 8192 ||
579f64122c1SDave Airlie 		    head->width > 8192 || head->height > 8192) {
580f64122c1SDave Airlie 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
581f64122c1SDave Airlie 				  i, head->width, head->height,
582f64122c1SDave Airlie 				  head->x, head->y);
583f64122c1SDave Airlie 			return;
584f64122c1SDave Airlie 		}
585f64122c1SDave Airlie 	}
586f64122c1SDave Airlie 	qxl_io_monitors_config(qdev);
587f64122c1SDave Airlie }
588f64122c1SDave Airlie 
58907f8d9bdSDave Airlie static void qxl_monitors_config_set(struct qxl_device *qdev,
59007f8d9bdSDave Airlie 				    int index,
591f64122c1SDave Airlie 				    unsigned x, unsigned y,
59207f8d9bdSDave Airlie 				    unsigned width, unsigned height,
59307f8d9bdSDave Airlie 				    unsigned surf_id)
594f64122c1SDave Airlie {
59507f8d9bdSDave Airlie 	DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
59607f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].x = x;
59707f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].y = y;
59807f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].width = width;
59907f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].height = height;
60007f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].surface_id = surf_id;
60107f8d9bdSDave Airlie 
602f64122c1SDave Airlie }
603f64122c1SDave Airlie 
604f64122c1SDave Airlie static int qxl_crtc_mode_set(struct drm_crtc *crtc,
605f64122c1SDave Airlie 			       struct drm_display_mode *mode,
606f64122c1SDave Airlie 			       struct drm_display_mode *adjusted_mode,
607f64122c1SDave Airlie 			       int x, int y,
608f64122c1SDave Airlie 			       struct drm_framebuffer *old_fb)
609f64122c1SDave Airlie {
610f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
611f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
612f64122c1SDave Airlie 	struct qxl_framebuffer *qfb;
613f64122c1SDave Airlie 	struct qxl_bo *bo, *old_bo = NULL;
61407f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
615f64122c1SDave Airlie 	bool recreate_primary = false;
616f64122c1SDave Airlie 	int ret;
61707f8d9bdSDave Airlie 	int surf_id;
618f4510a27SMatt Roper 	if (!crtc->primary->fb) {
619f64122c1SDave Airlie 		DRM_DEBUG_KMS("No FB bound\n");
620f64122c1SDave Airlie 		return 0;
621f64122c1SDave Airlie 	}
622f64122c1SDave Airlie 
623f64122c1SDave Airlie 	if (old_fb) {
624f64122c1SDave Airlie 		qfb = to_qxl_framebuffer(old_fb);
625f64122c1SDave Airlie 		old_bo = gem_to_qxl_bo(qfb->obj);
626f64122c1SDave Airlie 	}
627f4510a27SMatt Roper 	qfb = to_qxl_framebuffer(crtc->primary->fb);
628f64122c1SDave Airlie 	bo = gem_to_qxl_bo(qfb->obj);
629f64122c1SDave Airlie 	DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
630f64122c1SDave Airlie 		  x, y,
631f64122c1SDave Airlie 		  mode->hdisplay, mode->vdisplay,
632f64122c1SDave Airlie 		  adjusted_mode->hdisplay,
633f64122c1SDave Airlie 		  adjusted_mode->vdisplay);
634f64122c1SDave Airlie 
6358d0d9401SFabiano Fidêncio 	if (bo->is_primary == false)
636f64122c1SDave Airlie 		recreate_primary = true;
637f64122c1SDave Airlie 
638c572aaf4SMarc-André Lureau 	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
639c572aaf4SMarc-André Lureau 		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
640c572aaf4SMarc-André Lureau 		return -EINVAL;
641c572aaf4SMarc-André Lureau         }
642f64122c1SDave Airlie 
643f64122c1SDave Airlie 	ret = qxl_bo_reserve(bo, false);
644f64122c1SDave Airlie 	if (ret != 0)
645f64122c1SDave Airlie 		return ret;
646f64122c1SDave Airlie 	ret = qxl_bo_pin(bo, bo->type, NULL);
647f64122c1SDave Airlie 	if (ret != 0) {
648f64122c1SDave Airlie 		qxl_bo_unreserve(bo);
649f64122c1SDave Airlie 		return -EINVAL;
650f64122c1SDave Airlie 	}
651f64122c1SDave Airlie 	qxl_bo_unreserve(bo);
652f64122c1SDave Airlie 	if (recreate_primary) {
653f64122c1SDave Airlie 		qxl_io_destroy_primary(qdev);
654f64122c1SDave Airlie 		qxl_io_log(qdev,
655c572aaf4SMarc-André Lureau 			   "recreate primary: %dx%d,%d,%d\n",
656c572aaf4SMarc-André Lureau 			   bo->surf.width, bo->surf.height,
657c572aaf4SMarc-André Lureau 			   bo->surf.stride, bo->surf.format);
658c572aaf4SMarc-André Lureau 		qxl_io_create_primary(qdev, 0, bo);
659f64122c1SDave Airlie 		bo->is_primary = true;
66052571ad5SDavid Mansfield 	}
66152571ad5SDavid Mansfield 
66252571ad5SDavid Mansfield 	if (bo->is_primary) {
66352571ad5SDavid Mansfield 		DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
66407f8d9bdSDave Airlie 		surf_id = 0;
66507f8d9bdSDave Airlie 	} else {
66607f8d9bdSDave Airlie 		surf_id = bo->surface_id;
667f64122c1SDave Airlie 	}
668f64122c1SDave Airlie 
669f64122c1SDave Airlie 	if (old_bo && old_bo != bo) {
670f64122c1SDave Airlie 		old_bo->is_primary = false;
671f64122c1SDave Airlie 		ret = qxl_bo_reserve(old_bo, false);
672f64122c1SDave Airlie 		qxl_bo_unpin(old_bo);
673f64122c1SDave Airlie 		qxl_bo_unreserve(old_bo);
674f64122c1SDave Airlie 	}
675f64122c1SDave Airlie 
67607f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, x, y,
677f64122c1SDave Airlie 				mode->hdisplay,
67807f8d9bdSDave Airlie 				mode->vdisplay, surf_id);
679f64122c1SDave Airlie 	return 0;
680f64122c1SDave Airlie }
681f64122c1SDave Airlie 
682f64122c1SDave Airlie static void qxl_crtc_prepare(struct drm_crtc *crtc)
683f64122c1SDave Airlie {
684f64122c1SDave Airlie 	DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
685f64122c1SDave Airlie 		  crtc->mode.hdisplay, crtc->mode.vdisplay,
686f64122c1SDave Airlie 		  crtc->x, crtc->y, crtc->enabled);
687f64122c1SDave Airlie }
688f64122c1SDave Airlie 
689f64122c1SDave Airlie static void qxl_crtc_commit(struct drm_crtc *crtc)
690f64122c1SDave Airlie {
691f64122c1SDave Airlie 	DRM_DEBUG("\n");
692f64122c1SDave Airlie }
693f64122c1SDave Airlie 
69407f8d9bdSDave Airlie static void qxl_crtc_disable(struct drm_crtc *crtc)
69507f8d9bdSDave Airlie {
69607f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
69707f8d9bdSDave Airlie 	struct drm_device *dev = crtc->dev;
69807f8d9bdSDave Airlie 	struct qxl_device *qdev = dev->dev_private;
699f4510a27SMatt Roper 	if (crtc->primary->fb) {
700f4510a27SMatt Roper 		struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
70107f8d9bdSDave Airlie 		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
70207f8d9bdSDave Airlie 		int ret;
70307f8d9bdSDave Airlie 		ret = qxl_bo_reserve(bo, false);
70407f8d9bdSDave Airlie 		qxl_bo_unpin(bo);
70507f8d9bdSDave Airlie 		qxl_bo_unreserve(bo);
706f4510a27SMatt Roper 		crtc->primary->fb = NULL;
70707f8d9bdSDave Airlie 	}
70807f8d9bdSDave Airlie 
70907f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
71007f8d9bdSDave Airlie 
71107f8d9bdSDave Airlie 	qxl_send_monitors_config(qdev);
71207f8d9bdSDave Airlie }
71307f8d9bdSDave Airlie 
714f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
715f64122c1SDave Airlie 	.dpms = qxl_crtc_dpms,
71607f8d9bdSDave Airlie 	.disable = qxl_crtc_disable,
717f64122c1SDave Airlie 	.mode_fixup = qxl_crtc_mode_fixup,
718f64122c1SDave Airlie 	.mode_set = qxl_crtc_mode_set,
719f64122c1SDave Airlie 	.prepare = qxl_crtc_prepare,
720f64122c1SDave Airlie 	.commit = qxl_crtc_commit,
721f64122c1SDave Airlie };
722f64122c1SDave Airlie 
72307f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
724f64122c1SDave Airlie {
725f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
726f64122c1SDave Airlie 
727f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
728f64122c1SDave Airlie 	if (!qxl_crtc)
729f64122c1SDave Airlie 		return -ENOMEM;
730f64122c1SDave Airlie 
731f64122c1SDave Airlie 	drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
73207f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
733f64122c1SDave Airlie 	drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
734f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
735f64122c1SDave Airlie 	return 0;
736f64122c1SDave Airlie }
737f64122c1SDave Airlie 
738f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
739f64122c1SDave Airlie {
740f64122c1SDave Airlie 	DRM_DEBUG("\n");
741f64122c1SDave Airlie }
742f64122c1SDave Airlie 
743f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder)
744f64122c1SDave Airlie {
745f64122c1SDave Airlie 	DRM_DEBUG("\n");
746f64122c1SDave Airlie }
747f64122c1SDave Airlie 
748f64122c1SDave Airlie static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
749f64122c1SDave Airlie 		struct drm_encoder *encoder)
750f64122c1SDave Airlie {
751f64122c1SDave Airlie 	int i;
75207f8d9bdSDave Airlie 	struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
753f64122c1SDave Airlie 	struct qxl_head *head;
754f64122c1SDave Airlie 	struct drm_display_mode *mode;
755f64122c1SDave Airlie 
756f64122c1SDave Airlie 	BUG_ON(!encoder);
757f64122c1SDave Airlie 	/* TODO: ugly, do better */
75807f8d9bdSDave Airlie 	i = output->index;
759f64122c1SDave Airlie 	if (!qdev->monitors_config ||
760f64122c1SDave Airlie 	    qdev->monitors_config->max_allowed <= i) {
761f64122c1SDave Airlie 		DRM_ERROR(
762f64122c1SDave Airlie 		"head number too large or missing monitors config: %p, %d",
763f64122c1SDave Airlie 		qdev->monitors_config,
764f64122c1SDave Airlie 		qdev->monitors_config ?
765f64122c1SDave Airlie 			qdev->monitors_config->max_allowed : -1);
766f64122c1SDave Airlie 		return;
767f64122c1SDave Airlie 	}
768f64122c1SDave Airlie 	if (!encoder->crtc) {
769f64122c1SDave Airlie 		DRM_ERROR("missing crtc on encoder %p\n", encoder);
770f64122c1SDave Airlie 		return;
771f64122c1SDave Airlie 	}
772f64122c1SDave Airlie 	if (i != 0)
773f64122c1SDave Airlie 		DRM_DEBUG("missing for multiple monitors: no head holes\n");
774f64122c1SDave Airlie 	head = &qdev->monitors_config->heads[i];
775f64122c1SDave Airlie 	head->id = i;
776f64122c1SDave Airlie 	if (encoder->crtc->enabled) {
777f64122c1SDave Airlie 		mode = &encoder->crtc->mode;
778f64122c1SDave Airlie 		head->width = mode->hdisplay;
779f64122c1SDave Airlie 		head->height = mode->vdisplay;
780f64122c1SDave Airlie 		head->x = encoder->crtc->x;
781f64122c1SDave Airlie 		head->y = encoder->crtc->y;
782f64122c1SDave Airlie 		if (qdev->monitors_config->count < i + 1)
783f64122c1SDave Airlie 			qdev->monitors_config->count = i + 1;
784f64122c1SDave Airlie 	} else {
785f64122c1SDave Airlie 		head->width = 0;
786f64122c1SDave Airlie 		head->height = 0;
787f64122c1SDave Airlie 		head->x = 0;
788f64122c1SDave Airlie 		head->y = 0;
789f64122c1SDave Airlie 	}
79007f8d9bdSDave Airlie 	DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
79107f8d9bdSDave Airlie 		      i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
792f64122c1SDave Airlie 	head->flags = 0;
793f64122c1SDave Airlie 	/* TODO - somewhere else to call this for multiple monitors
794f64122c1SDave Airlie 	 * (config_commit?) */
795f64122c1SDave Airlie 	qxl_send_monitors_config(qdev);
796f64122c1SDave Airlie }
797f64122c1SDave Airlie 
798f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder)
799f64122c1SDave Airlie {
800f64122c1SDave Airlie 	struct qxl_device *qdev = encoder->dev->dev_private;
801f64122c1SDave Airlie 
802f64122c1SDave Airlie 	qxl_write_monitors_config_for_encoder(qdev, encoder);
803f64122c1SDave Airlie 	DRM_DEBUG("\n");
804f64122c1SDave Airlie }
805f64122c1SDave Airlie 
806f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder,
807f64122c1SDave Airlie 				struct drm_display_mode *mode,
808f64122c1SDave Airlie 				struct drm_display_mode *adjusted_mode)
809f64122c1SDave Airlie {
810f64122c1SDave Airlie 	DRM_DEBUG("\n");
811f64122c1SDave Airlie }
812f64122c1SDave Airlie 
813f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
814f64122c1SDave Airlie {
815f64122c1SDave Airlie 	int ret = 0;
816f64122c1SDave Airlie 	struct qxl_device *qdev = connector->dev->dev_private;
817b0807423SMarc-André Lureau 	unsigned pwidth = 1024;
818b0807423SMarc-André Lureau 	unsigned pheight = 768;
819f64122c1SDave Airlie 
820f64122c1SDave Airlie 	DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
821f64122c1SDave Airlie 	/* TODO: what should we do here? only show the configured modes for the
822f64122c1SDave Airlie 	 * device, or allow the full list, or both? */
823f64122c1SDave Airlie 	if (qdev->monitors_config && qdev->monitors_config->count) {
824b0807423SMarc-André Lureau 		ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
825f64122c1SDave Airlie 		if (ret < 0)
826f64122c1SDave Airlie 			return ret;
827f64122c1SDave Airlie 	}
828b0807423SMarc-André Lureau 	ret += qxl_add_common_modes(connector, pwidth, pheight);
829f64122c1SDave Airlie 	return ret;
830f64122c1SDave Airlie }
831f64122c1SDave Airlie 
832f64122c1SDave Airlie static int qxl_conn_mode_valid(struct drm_connector *connector,
833f64122c1SDave Airlie 			       struct drm_display_mode *mode)
834f64122c1SDave Airlie {
835bd3e1c7cSJonathon Jongsma 	struct drm_device *ddev = connector->dev;
836bd3e1c7cSJonathon Jongsma 	struct qxl_device *qdev = ddev->dev_private;
837bd3e1c7cSJonathon Jongsma 	int i;
838bd3e1c7cSJonathon Jongsma 
839f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
840f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
841bd3e1c7cSJonathon Jongsma 
842bd3e1c7cSJonathon Jongsma 	if(qdev->monitors_config_width == mode->hdisplay &&
843bd3e1c7cSJonathon Jongsma 	   qdev->monitors_config_height == mode->vdisplay)
844f64122c1SDave Airlie 		return MODE_OK;
845bd3e1c7cSJonathon Jongsma 
846bd3e1c7cSJonathon Jongsma 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
847bd3e1c7cSJonathon Jongsma 		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
848bd3e1c7cSJonathon Jongsma 			return MODE_OK;
849bd3e1c7cSJonathon Jongsma 	}
850bd3e1c7cSJonathon Jongsma 	return MODE_BAD;
851f64122c1SDave Airlie }
852f64122c1SDave Airlie 
8536d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
854f64122c1SDave Airlie {
855f64122c1SDave Airlie 	struct qxl_output *qxl_output =
856f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
857f64122c1SDave Airlie 
858f64122c1SDave Airlie 	DRM_DEBUG("\n");
859f64122c1SDave Airlie 	return &qxl_output->enc;
860f64122c1SDave Airlie }
861f64122c1SDave Airlie 
862f64122c1SDave Airlie 
863f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
864f64122c1SDave Airlie 	.dpms = qxl_enc_dpms,
865f64122c1SDave Airlie 	.prepare = qxl_enc_prepare,
866f64122c1SDave Airlie 	.mode_set = qxl_enc_mode_set,
867f64122c1SDave Airlie 	.commit = qxl_enc_commit,
868f64122c1SDave Airlie };
869f64122c1SDave Airlie 
870f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
871f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
872f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
873f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
874f64122c1SDave Airlie };
875f64122c1SDave Airlie 
876f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
877f64122c1SDave Airlie 			struct drm_connector *connector,
878f64122c1SDave Airlie 			bool force)
879f64122c1SDave Airlie {
880f64122c1SDave Airlie 	struct qxl_output *output =
881f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
882f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
883f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
88469e5d3f8SDave Airlie 	bool connected = false;
885f64122c1SDave Airlie 
886f64122c1SDave Airlie 	/* The first monitor is always connected */
88769e5d3f8SDave Airlie 	if (!qdev->client_monitors_config) {
88869e5d3f8SDave Airlie 		if (output->index == 0)
88969e5d3f8SDave Airlie 			connected = true;
89069e5d3f8SDave Airlie 	} else
89169e5d3f8SDave Airlie 		connected = qdev->client_monitors_config->count > output->index &&
89269e5d3f8SDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
893f64122c1SDave Airlie 
8945cab51cbSMarc-André Lureau 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
8955cab51cbSMarc-André Lureau 	if (!connected)
8965cab51cbSMarc-André Lureau 		qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
8975cab51cbSMarc-André Lureau 
898f64122c1SDave Airlie 	return connected ? connector_status_connected
899f64122c1SDave Airlie 			 : connector_status_disconnected;
900f64122c1SDave Airlie }
901f64122c1SDave Airlie 
902f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector,
903f64122c1SDave Airlie 				   struct drm_property *property,
904f64122c1SDave Airlie 				   uint64_t value)
905f64122c1SDave Airlie {
906f64122c1SDave Airlie 	DRM_DEBUG("\n");
907f64122c1SDave Airlie 	return 0;
908f64122c1SDave Airlie }
909f64122c1SDave Airlie 
910f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
911f64122c1SDave Airlie {
912f64122c1SDave Airlie 	struct qxl_output *qxl_output =
913f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
914f64122c1SDave Airlie 
91534ea3d38SThomas Wood 	drm_connector_unregister(connector);
916f64122c1SDave Airlie 	drm_connector_cleanup(connector);
917f64122c1SDave Airlie 	kfree(qxl_output);
918f64122c1SDave Airlie }
919f64122c1SDave Airlie 
920f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
921f64122c1SDave Airlie 	.dpms = drm_helper_connector_dpms,
922f64122c1SDave Airlie 	.detect = qxl_conn_detect,
9236af3e656SVille Syrjälä 	.fill_modes = drm_helper_probe_single_connector_modes,
924f64122c1SDave Airlie 	.set_property = qxl_conn_set_property,
925f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
926f64122c1SDave Airlie };
927f64122c1SDave Airlie 
928f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
929f64122c1SDave Airlie {
930f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
931f64122c1SDave Airlie }
932f64122c1SDave Airlie 
933f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
934f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
935f64122c1SDave Airlie };
936f64122c1SDave Airlie 
9374695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
9384695b039SDave Airlie {
9394695b039SDave Airlie 	if (qdev->hotplug_mode_update_property)
9404695b039SDave Airlie 		return 0;
9414695b039SDave Airlie 
9424695b039SDave Airlie 	qdev->hotplug_mode_update_property =
9434695b039SDave Airlie 		drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
9444695b039SDave Airlie 					  "hotplug_mode_update", 0, 1);
9454695b039SDave Airlie 
9464695b039SDave Airlie 	return 0;
9474695b039SDave Airlie }
9484695b039SDave Airlie 
9496d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
950f64122c1SDave Airlie {
9514695b039SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
952f64122c1SDave Airlie 	struct qxl_output *qxl_output;
953f64122c1SDave Airlie 	struct drm_connector *connector;
954f64122c1SDave Airlie 	struct drm_encoder *encoder;
955f64122c1SDave Airlie 
956f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
957f64122c1SDave Airlie 	if (!qxl_output)
958f64122c1SDave Airlie 		return -ENOMEM;
959f64122c1SDave Airlie 
960f64122c1SDave Airlie 	qxl_output->index = num_output;
961f64122c1SDave Airlie 
962f64122c1SDave Airlie 	connector = &qxl_output->base;
963f64122c1SDave Airlie 	encoder = &qxl_output->enc;
964f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
965f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
966f64122c1SDave Airlie 
967f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
96813a3d91fSVille Syrjälä 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
969f64122c1SDave Airlie 
9705ff91e44SDave Airlie 	/* we get HPD via client monitors config */
9715ff91e44SDave Airlie 	connector->polled = DRM_CONNECTOR_POLL_HPD;
972f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
973f64122c1SDave Airlie 	drm_mode_connector_attach_encoder(&qxl_output->base,
974f64122c1SDave Airlie 					  &qxl_output->enc);
975f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
976f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
977f64122c1SDave Airlie 
9784695b039SDave Airlie 	drm_object_attach_property(&connector->base,
9794695b039SDave Airlie 				   qdev->hotplug_mode_update_property, 0);
9807dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
9817dea0941SDave Airlie 				   dev->mode_config.suggested_x_property, 0);
9827dea0941SDave Airlie 	drm_object_attach_property(&connector->base,
9837dea0941SDave Airlie 				   dev->mode_config.suggested_y_property, 0);
98434ea3d38SThomas Wood 	drm_connector_register(connector);
985f64122c1SDave Airlie 	return 0;
986f64122c1SDave Airlie }
987f64122c1SDave Airlie 
988f64122c1SDave Airlie static struct drm_framebuffer *
989f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
990f64122c1SDave Airlie 			    struct drm_file *file_priv,
9911eb83451SVille Syrjälä 			    const struct drm_mode_fb_cmd2 *mode_cmd)
992f64122c1SDave Airlie {
993f64122c1SDave Airlie 	struct drm_gem_object *obj;
994f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb;
995f64122c1SDave Airlie 	int ret;
996f64122c1SDave Airlie 
997*a8ad0bd8SChris Wilson 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
998*a8ad0bd8SChris Wilson 	if (!obj)
999*a8ad0bd8SChris Wilson 		return NULL;
1000f64122c1SDave Airlie 
1001f64122c1SDave Airlie 	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1002f64122c1SDave Airlie 	if (qxl_fb == NULL)
1003f64122c1SDave Airlie 		return NULL;
1004f64122c1SDave Airlie 
10056819c3c2SNoralf Trønnes 	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1006f64122c1SDave Airlie 	if (ret) {
1007f64122c1SDave Airlie 		kfree(qxl_fb);
1008f64122c1SDave Airlie 		drm_gem_object_unreference_unlocked(obj);
1009f64122c1SDave Airlie 		return NULL;
1010f64122c1SDave Airlie 	}
1011f64122c1SDave Airlie 
1012f64122c1SDave Airlie 	return &qxl_fb->base;
1013f64122c1SDave Airlie }
1014f64122c1SDave Airlie 
1015f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1016f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
1017f64122c1SDave Airlie };
1018f64122c1SDave Airlie 
10192bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1020f64122c1SDave Airlie {
1021f64122c1SDave Airlie 	int ret;
1022f64122c1SDave Airlie 	struct drm_gem_object *gobj;
102307f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
1024f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1025f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
1026f64122c1SDave Airlie 
1027f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1028f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
1029f64122c1SDave Airlie 				    false, false, NULL, &gobj);
1030f64122c1SDave Airlie 	if (ret) {
1031f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1032f64122c1SDave Airlie 		return -ENOMEM;
1033f64122c1SDave Airlie 	}
1034f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
10352bd6ce84SDave Airlie 
10362bd6ce84SDave Airlie 	ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
10372bd6ce84SDave Airlie 	if (ret)
10382bd6ce84SDave Airlie 		return ret;
10392bd6ce84SDave Airlie 
10402bd6ce84SDave Airlie 	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
10412bd6ce84SDave Airlie 	if (ret) {
10422bd6ce84SDave Airlie 		qxl_bo_unreserve(qdev->monitors_config_bo);
10432bd6ce84SDave Airlie 		return ret;
10442bd6ce84SDave Airlie 	}
10452bd6ce84SDave Airlie 
10462bd6ce84SDave Airlie 	qxl_bo_unreserve(qdev->monitors_config_bo);
10472bd6ce84SDave Airlie 
1048f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
10492bd6ce84SDave Airlie 
1050f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1051f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
1052f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1053f64122c1SDave Airlie 
1054f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
1055f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
10562bd6ce84SDave Airlie 	return 0;
10572bd6ce84SDave Airlie }
10582bd6ce84SDave Airlie 
10592bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
10602bd6ce84SDave Airlie {
10612bd6ce84SDave Airlie 	int ret;
10622bd6ce84SDave Airlie 
10632bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
10642bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
10652bd6ce84SDave Airlie 
10662bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
10672bd6ce84SDave Airlie 	ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
10682bd6ce84SDave Airlie 	if (ret)
10692bd6ce84SDave Airlie 		return ret;
10702bd6ce84SDave Airlie 
10712bd6ce84SDave Airlie 	qxl_bo_unpin(qdev->monitors_config_bo);
10722bd6ce84SDave Airlie 	qxl_bo_unreserve(qdev->monitors_config_bo);
10732bd6ce84SDave Airlie 
10742bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
10752bd6ce84SDave Airlie 	return 0;
10762bd6ce84SDave Airlie }
10772bd6ce84SDave Airlie 
10782bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
10792bd6ce84SDave Airlie {
10802bd6ce84SDave Airlie 	int i;
10812bd6ce84SDave Airlie 	int ret;
10822bd6ce84SDave Airlie 
10832bd6ce84SDave Airlie 	drm_mode_config_init(qdev->ddev);
10842bd6ce84SDave Airlie 
10852bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
10862bd6ce84SDave Airlie 	if (ret)
10872bd6ce84SDave Airlie 		return ret;
1088f64122c1SDave Airlie 
1089f64122c1SDave Airlie 	qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
1090f64122c1SDave Airlie 
1091f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
1092f64122c1SDave Airlie 	qdev->ddev->mode_config.min_width = 320;
1093f64122c1SDave Airlie 	qdev->ddev->mode_config.min_height = 200;
1094f64122c1SDave Airlie 	qdev->ddev->mode_config.max_width = 8192;
1095f64122c1SDave Airlie 	qdev->ddev->mode_config.max_height = 8192;
1096f64122c1SDave Airlie 
1097f64122c1SDave Airlie 	qdev->ddev->mode_config.fb_base = qdev->vram_base;
10984695b039SDave Airlie 
10997dea0941SDave Airlie 	drm_mode_create_suggested_offset_properties(qdev->ddev);
11004695b039SDave Airlie 	qxl_mode_create_hotplug_mode_update_property(qdev);
11014695b039SDave Airlie 
110207f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1103f64122c1SDave Airlie 		qdev_crtc_init(qdev->ddev, i);
1104f64122c1SDave Airlie 		qdev_output_init(qdev->ddev, i);
1105f64122c1SDave Airlie 	}
1106f64122c1SDave Airlie 
1107f64122c1SDave Airlie 	qdev->mode_info.mode_config_initialized = true;
1108f64122c1SDave Airlie 
1109f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
1110f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
1111f64122c1SDave Airlie 	 * spice server. */
1112f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
1113f64122c1SDave Airlie 	return 0;
1114f64122c1SDave Airlie }
1115f64122c1SDave Airlie 
1116f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1117f64122c1SDave Airlie {
1118f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
11192bd6ce84SDave Airlie 
11202bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
1121f64122c1SDave Airlie 	if (qdev->mode_info.mode_config_initialized) {
1122f64122c1SDave Airlie 		drm_mode_config_cleanup(qdev->ddev);
1123f64122c1SDave Airlie 		qdev->mode_info.mode_config_initialized = false;
1124f64122c1SDave Airlie 	}
1125f64122c1SDave Airlie }
1126