xref: /linux/drivers/gpu/drm/qxl/qxl_display.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Dave Airlie
23  *          Alon Levy
24  */
25 
26 #include <linux/crc32.h>
27 #include <linux/delay.h>
28 #include <linux/iosys-map.h>
29 
30 #include <drm/drm_drv.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_edid.h>
34 #include <drm/drm_framebuffer.h>
35 #include <drm/drm_gem_framebuffer_helper.h>
36 #include <drm/drm_plane_helper.h>
37 #include <drm/drm_print.h>
38 #include <drm/drm_probe_helper.h>
39 #include <drm/drm_simple_kms_helper.h>
40 #include <drm/drm_gem_atomic_helper.h>
41 #include <drm/drm_vblank.h>
42 #include <drm/drm_vblank_helper.h>
43 
44 #include "qxl_drv.h"
45 #include "qxl_object.h"
46 
47 static bool qxl_head_enabled(struct qxl_head *head)
48 {
49 	return head->width && head->height;
50 }
51 
52 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
53 		unsigned int count)
54 {
55 	if (qdev->client_monitors_config &&
56 	    count > qdev->client_monitors_config->count) {
57 		kfree(qdev->client_monitors_config);
58 		qdev->client_monitors_config = NULL;
59 	}
60 	if (!qdev->client_monitors_config) {
61 		qdev->client_monitors_config = kzalloc(
62 				struct_size(qdev->client_monitors_config,
63 				heads, count), GFP_KERNEL);
64 		if (!qdev->client_monitors_config)
65 			return -ENOMEM;
66 	}
67 	qdev->client_monitors_config->count = count;
68 	return 0;
69 }
70 
71 enum {
72 	MONITORS_CONFIG_MODIFIED,
73 	MONITORS_CONFIG_UNCHANGED,
74 	MONITORS_CONFIG_BAD_CRC,
75 	MONITORS_CONFIG_ERROR,
76 };
77 
78 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
79 {
80 	int i;
81 	int num_monitors;
82 	uint32_t crc;
83 	int status = MONITORS_CONFIG_UNCHANGED;
84 
85 	num_monitors = qdev->rom->client_monitors_config.count;
86 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
87 		  sizeof(qdev->rom->client_monitors_config));
88 	if (crc != qdev->rom->client_monitors_config_crc)
89 		return MONITORS_CONFIG_BAD_CRC;
90 	if (!num_monitors) {
91 		DRM_DEBUG_KMS("no client monitors configured\n");
92 		return status;
93 	}
94 	if (num_monitors > qxl_num_crtc) {
95 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
96 			      qxl_num_crtc, num_monitors);
97 		num_monitors = qxl_num_crtc;
98 	} else {
99 		num_monitors = qdev->rom->client_monitors_config.count;
100 	}
101 	if (qdev->client_monitors_config
102 	      && (num_monitors != qdev->client_monitors_config->count)) {
103 		status = MONITORS_CONFIG_MODIFIED;
104 	}
105 	if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
106 		status = MONITORS_CONFIG_ERROR;
107 		return status;
108 	}
109 	/* we copy max from the client but it isn't used */
110 	qdev->client_monitors_config->max_allowed = qxl_num_crtc;
111 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
112 		struct qxl_urect *c_rect =
113 			&qdev->rom->client_monitors_config.heads[i];
114 		struct qxl_head *client_head =
115 			&qdev->client_monitors_config->heads[i];
116 		if (client_head->x != c_rect->left) {
117 			client_head->x = c_rect->left;
118 			status = MONITORS_CONFIG_MODIFIED;
119 		}
120 		if (client_head->y != c_rect->top) {
121 			client_head->y = c_rect->top;
122 			status = MONITORS_CONFIG_MODIFIED;
123 		}
124 		if (client_head->width != c_rect->right - c_rect->left) {
125 			client_head->width = c_rect->right - c_rect->left;
126 			status = MONITORS_CONFIG_MODIFIED;
127 		}
128 		if (client_head->height != c_rect->bottom - c_rect->top) {
129 			client_head->height = c_rect->bottom - c_rect->top;
130 			status = MONITORS_CONFIG_MODIFIED;
131 		}
132 		if (client_head->surface_id != 0) {
133 			client_head->surface_id = 0;
134 			status = MONITORS_CONFIG_MODIFIED;
135 		}
136 		if (client_head->id != i) {
137 			client_head->id = i;
138 			status = MONITORS_CONFIG_MODIFIED;
139 		}
140 		if (client_head->flags != 0) {
141 			client_head->flags = 0;
142 			status = MONITORS_CONFIG_MODIFIED;
143 		}
144 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
145 			  client_head->x, client_head->y);
146 	}
147 
148 	return status;
149 }
150 
151 static void qxl_update_offset_props(struct qxl_device *qdev)
152 {
153 	struct drm_device *dev = &qdev->ddev;
154 	struct drm_connector *connector;
155 	struct qxl_output *output;
156 	struct qxl_head *head;
157 
158 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
159 		output = drm_connector_to_qxl_output(connector);
160 
161 		head = &qdev->client_monitors_config->heads[output->index];
162 
163 		drm_object_property_set_value(&connector->base,
164 			dev->mode_config.suggested_x_property, head->x);
165 		drm_object_property_set_value(&connector->base,
166 			dev->mode_config.suggested_y_property, head->y);
167 	}
168 }
169 
170 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
171 {
172 	struct drm_device *dev = &qdev->ddev;
173 	struct drm_modeset_acquire_ctx ctx;
174 	int status, retries, ret;
175 
176 	for (retries = 0; retries < 10; retries++) {
177 		status = qxl_display_copy_rom_client_monitors_config(qdev);
178 		if (status != MONITORS_CONFIG_BAD_CRC)
179 			break;
180 		udelay(5);
181 	}
182 	if (status == MONITORS_CONFIG_ERROR) {
183 		DRM_DEBUG_KMS("ignoring client monitors config: error");
184 		return;
185 	}
186 	if (status == MONITORS_CONFIG_BAD_CRC) {
187 		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
188 		return;
189 	}
190 	if (status == MONITORS_CONFIG_UNCHANGED) {
191 		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
192 		return;
193 	}
194 
195 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
196 	qxl_update_offset_props(qdev);
197 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
198 	if (!drm_helper_hpd_irq_event(dev)) {
199 		/* notify that the monitor configuration changed, to
200 		   adjust at the arbitrary resolution */
201 		drm_kms_helper_hotplug_event(dev);
202 	}
203 }
204 
205 static int qxl_check_mode(struct qxl_device *qdev,
206 			  unsigned int width,
207 			  unsigned int height)
208 {
209 	unsigned int stride;
210 	unsigned int size;
211 
212 	if (check_mul_overflow(width, 4u, &stride))
213 		return -EINVAL;
214 	if (check_mul_overflow(stride, height, &size))
215 		return -EINVAL;
216 	if (size > qdev->vram_size)
217 		return -ENOMEM;
218 	return 0;
219 }
220 
221 static int qxl_check_framebuffer(struct qxl_device *qdev,
222 				 struct qxl_bo *bo)
223 {
224 	return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
225 }
226 
227 static int qxl_add_mode(struct drm_connector *connector,
228 			unsigned int width,
229 			unsigned int height,
230 			bool preferred)
231 {
232 	struct drm_device *dev = connector->dev;
233 	struct qxl_device *qdev = to_qxl(dev);
234 	struct drm_display_mode *mode = NULL;
235 	int rc;
236 
237 	rc = qxl_check_mode(qdev, width, height);
238 	if (rc != 0)
239 		return 0;
240 
241 	mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
242 	if (!mode)
243 		return 0;
244 
245 	if (preferred)
246 		mode->type |= DRM_MODE_TYPE_PREFERRED;
247 	mode->hdisplay = width;
248 	mode->vdisplay = height;
249 	drm_mode_set_name(mode);
250 	drm_mode_probed_add(connector, mode);
251 	return 1;
252 }
253 
254 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
255 {
256 	struct drm_device *dev = connector->dev;
257 	struct qxl_device *qdev = to_qxl(dev);
258 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
259 	int h = output->index;
260 	struct qxl_head *head;
261 
262 	if (!qdev->monitors_config)
263 		return 0;
264 	if (h >= qxl_num_crtc)
265 		return 0;
266 	if (!qdev->client_monitors_config)
267 		return 0;
268 	if (h >= qdev->client_monitors_config->count)
269 		return 0;
270 
271 	head = &qdev->client_monitors_config->heads[h];
272 	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
273 
274 	return qxl_add_mode(connector, head->width, head->height, true);
275 }
276 
277 static struct mode_size {
278 	int w;
279 	int h;
280 } extra_modes[] = {
281 	{ 720,  480},
282 	{1152,  768},
283 	{1280,  854},
284 };
285 
286 static int qxl_add_extra_modes(struct drm_connector *connector)
287 {
288 	int i, ret = 0;
289 
290 	for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
291 		ret += qxl_add_mode(connector,
292 				    extra_modes[i].w,
293 				    extra_modes[i].h,
294 				    false);
295 	return ret;
296 }
297 
298 static void qxl_send_monitors_config(struct qxl_device *qdev)
299 {
300 	int i;
301 
302 	BUG_ON(!qdev->ram_header->monitors_config);
303 
304 	if (qdev->monitors_config->count == 0)
305 		return;
306 
307 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
308 		struct qxl_head *head = &qdev->monitors_config->heads[i];
309 
310 		if (head->y > 8192 || head->x > 8192 ||
311 		    head->width > 8192 || head->height > 8192) {
312 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
313 				  i, head->width, head->height,
314 				  head->x, head->y);
315 			return;
316 		}
317 	}
318 	qxl_io_monitors_config(qdev);
319 }
320 
321 static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
322 					    const char *reason)
323 {
324 	struct drm_device *dev = crtc->dev;
325 	struct qxl_device *qdev = to_qxl(dev);
326 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
327 	struct qxl_head head;
328 	int oldcount, i = qcrtc->index;
329 
330 	if (!qdev->primary_bo) {
331 		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
332 		return;
333 	}
334 
335 	if (!qdev->monitors_config || qxl_num_crtc <= i)
336 		return;
337 
338 	head.id = i;
339 	head.flags = 0;
340 	head.surface_id = 0;
341 	oldcount = qdev->monitors_config->count;
342 	if (crtc->state->active) {
343 		struct drm_display_mode *mode = &crtc->mode;
344 
345 		head.width = mode->hdisplay;
346 		head.height = mode->vdisplay;
347 		head.x = crtc->x;
348 		head.y = crtc->y;
349 		if (qdev->monitors_config->count < i + 1)
350 			qdev->monitors_config->count = i + 1;
351 		if (qdev->primary_bo == qdev->dumb_shadow_bo)
352 			head.x += qdev->dumb_heads[i].x;
353 	} else if (i > 0) {
354 		head.width = 0;
355 		head.height = 0;
356 		head.x = 0;
357 		head.y = 0;
358 		if (qdev->monitors_config->count == i + 1)
359 			qdev->monitors_config->count = i;
360 	} else {
361 		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
362 		return;
363 	}
364 
365 	if (head.width  == qdev->monitors_config->heads[i].width  &&
366 	    head.height == qdev->monitors_config->heads[i].height &&
367 	    head.x      == qdev->monitors_config->heads[i].x      &&
368 	    head.y      == qdev->monitors_config->heads[i].y      &&
369 	    oldcount    == qdev->monitors_config->count)
370 		return;
371 
372 	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
373 		      i, head.width, head.height, head.x, head.y,
374 		      crtc->state->active ? "on" : "off", reason);
375 	if (oldcount != qdev->monitors_config->count)
376 		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
377 			      oldcount, qdev->monitors_config->count,
378 			      qxl_num_crtc);
379 
380 	qdev->monitors_config->heads[i] = head;
381 	qdev->monitors_config->max_allowed = qxl_num_crtc;
382 	qxl_send_monitors_config(qdev);
383 }
384 
385 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
386 				  struct drm_atomic_state *state)
387 {
388 	struct drm_device *dev = crtc->dev;
389 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
390 	struct drm_pending_vblank_event *event;
391 
392 	qxl_crtc_update_monitors_config(crtc, "flush");
393 
394 	spin_lock_irq(&dev->event_lock);
395 
396 	event = crtc_state->event;
397 	crtc_state->event = NULL;
398 
399 	if (event) {
400 		if (drm_crtc_vblank_get(crtc) == 0)
401 			drm_crtc_arm_vblank_event(crtc, event);
402 		else
403 			drm_crtc_send_vblank_event(crtc, event);
404 	}
405 
406 	spin_unlock_irq(&dev->event_lock);
407 }
408 
409 static void qxl_crtc_destroy(struct drm_crtc *crtc)
410 {
411 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
412 
413 	qxl_bo_unref(&qxl_crtc->cursor_bo);
414 	drm_crtc_cleanup(crtc);
415 	kfree(qxl_crtc);
416 }
417 
418 static const struct drm_crtc_funcs qxl_crtc_funcs = {
419 	.set_config = drm_atomic_helper_set_config,
420 	.destroy = qxl_crtc_destroy,
421 	.page_flip = drm_atomic_helper_page_flip,
422 	.reset = drm_atomic_helper_crtc_reset,
423 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
424 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
425 	DRM_CRTC_VBLANK_TIMER_FUNCS,
426 };
427 
428 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
429 					 struct drm_file *file_priv,
430 					 unsigned int flags, unsigned int color,
431 					 struct drm_clip_rect *clips,
432 					 unsigned int num_clips)
433 {
434 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
435 	struct qxl_device *qdev = to_qxl(fb->dev);
436 	struct drm_clip_rect norect;
437 	struct qxl_bo *qobj;
438 	struct drm_modeset_acquire_ctx ctx;
439 	bool is_primary;
440 	int inc = 1, ret;
441 
442 	DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
443 
444 	qobj = gem_to_qxl_bo(fb->obj[0]);
445 	/* if we aren't primary surface ignore this */
446 	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
447 	if (!is_primary)
448 		goto out_lock_end;
449 
450 	if (!num_clips) {
451 		num_clips = 1;
452 		clips = &norect;
453 		norect.x1 = norect.y1 = 0;
454 		norect.x2 = fb->width;
455 		norect.y2 = fb->height;
456 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
457 		num_clips /= 2;
458 		inc = 2; /* skip source rects */
459 	}
460 
461 	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
462 			  clips, num_clips, inc, 0);
463 
464 out_lock_end:
465 	DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
466 
467 	return 0;
468 }
469 
470 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
471 	.destroy = drm_gem_fb_destroy,
472 	.dirty = qxl_framebuffer_surface_dirty,
473 	.create_handle = drm_gem_fb_create_handle,
474 };
475 
476 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
477 				   struct drm_atomic_state *state)
478 {
479 	qxl_crtc_update_monitors_config(crtc, "enable");
480 
481 	drm_crtc_vblank_on(crtc);
482 }
483 
484 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
485 				    struct drm_atomic_state *state)
486 {
487 	drm_crtc_vblank_off(crtc);
488 
489 	qxl_crtc_update_monitors_config(crtc, "disable");
490 }
491 
492 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
493 	.atomic_flush = qxl_crtc_atomic_flush,
494 	.atomic_enable = qxl_crtc_atomic_enable,
495 	.atomic_disable = qxl_crtc_atomic_disable,
496 };
497 
498 static int qxl_primary_atomic_check(struct drm_plane *plane,
499 				    struct drm_atomic_state *state)
500 {
501 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
502 										 plane);
503 	struct qxl_device *qdev = to_qxl(plane->dev);
504 	struct qxl_bo *bo;
505 
506 	if (!new_plane_state->crtc || !new_plane_state->fb)
507 		return 0;
508 
509 	bo = gem_to_qxl_bo(new_plane_state->fb->obj[0]);
510 
511 	return qxl_check_framebuffer(qdev, bo);
512 }
513 
514 static int qxl_primary_apply_cursor(struct qxl_device *qdev,
515 				    struct drm_plane_state *plane_state)
516 {
517 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
518 	struct qxl_cursor_cmd *cmd;
519 	struct qxl_release *release;
520 	int ret = 0;
521 
522 	if (!qcrtc->cursor_bo)
523 		return 0;
524 
525 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
526 					 QXL_RELEASE_CURSOR_CMD,
527 					 &release, NULL);
528 	if (ret)
529 		return ret;
530 
531 	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
532 	if (ret)
533 		goto out_free_release;
534 
535 	ret = qxl_release_reserve_list(release, false);
536 	if (ret)
537 		goto out_free_release;
538 
539 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
540 	cmd->type = QXL_CURSOR_SET;
541 	cmd->u.set.position.x = plane_state->crtc_x + plane_state->hotspot_x;
542 	cmd->u.set.position.y = plane_state->crtc_y + plane_state->hotspot_y;
543 
544 	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
545 
546 	cmd->u.set.visible = 1;
547 	qxl_release_unmap(qdev, release, &cmd->release_info);
548 
549 	qxl_release_fence_buffer_objects(release);
550 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
551 
552 	return ret;
553 
554 out_free_release:
555 	qxl_release_free(qdev, release);
556 	return ret;
557 }
558 
559 static int qxl_primary_move_cursor(struct qxl_device *qdev,
560 				   struct drm_plane_state *plane_state)
561 {
562 	struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
563 	struct qxl_cursor_cmd *cmd;
564 	struct qxl_release *release;
565 	int ret = 0;
566 
567 	if (!qcrtc->cursor_bo)
568 		return 0;
569 
570 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
571 					 QXL_RELEASE_CURSOR_CMD,
572 					 &release, NULL);
573 	if (ret)
574 		return ret;
575 
576 	ret = qxl_release_reserve_list(release, true);
577 	if (ret) {
578 		qxl_release_free(qdev, release);
579 		return ret;
580 	}
581 
582 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
583 	cmd->type = QXL_CURSOR_MOVE;
584 	cmd->u.position.x = plane_state->crtc_x + plane_state->hotspot_x;
585 	cmd->u.position.y = plane_state->crtc_y + plane_state->hotspot_y;
586 	qxl_release_unmap(qdev, release, &cmd->release_info);
587 
588 	qxl_release_fence_buffer_objects(release);
589 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
590 	return ret;
591 }
592 
593 static struct qxl_bo *qxl_create_cursor(struct qxl_device *qdev,
594 					struct qxl_bo *user_bo,
595 					int hot_x, int hot_y)
596 {
597 	static const u32 size = 64 * 64 * 4;
598 	struct qxl_bo *cursor_bo;
599 	struct iosys_map cursor_map;
600 	struct iosys_map user_map;
601 	struct qxl_cursor cursor;
602 	int ret;
603 
604 	if (!user_bo)
605 		return NULL;
606 
607 	ret = qxl_bo_create(qdev, sizeof(struct qxl_cursor) + size,
608 			    false, true, QXL_GEM_DOMAIN_VRAM, 1,
609 			    NULL, &cursor_bo);
610 	if (ret)
611 		goto err;
612 
613 	ret = qxl_bo_pin_and_vmap(cursor_bo, &cursor_map);
614 	if (ret)
615 		goto err_unref;
616 
617 	ret = qxl_bo_pin_and_vmap(user_bo, &user_map);
618 	if (ret)
619 		goto err_unmap;
620 
621 	cursor.header.unique = 0;
622 	cursor.header.type = SPICE_CURSOR_TYPE_ALPHA;
623 	cursor.header.width = 64;
624 	cursor.header.height = 64;
625 	cursor.header.hot_spot_x = hot_x;
626 	cursor.header.hot_spot_y = hot_y;
627 	cursor.data_size = size;
628 	cursor.chunk.next_chunk = 0;
629 	cursor.chunk.prev_chunk = 0;
630 	cursor.chunk.data_size = size;
631 	if (cursor_map.is_iomem) {
632 		memcpy_toio(cursor_map.vaddr_iomem,
633 			    &cursor, sizeof(cursor));
634 		memcpy_toio(cursor_map.vaddr_iomem + sizeof(cursor),
635 			    user_map.vaddr, size);
636 	} else {
637 		memcpy(cursor_map.vaddr,
638 		       &cursor, sizeof(cursor));
639 		memcpy(cursor_map.vaddr + sizeof(cursor),
640 		       user_map.vaddr, size);
641 	}
642 
643 	qxl_bo_vunmap_and_unpin(user_bo);
644 	qxl_bo_vunmap_and_unpin(cursor_bo);
645 	return cursor_bo;
646 
647 err_unmap:
648 	qxl_bo_vunmap_and_unpin(cursor_bo);
649 err_unref:
650 	qxl_bo_unpin(cursor_bo);
651 	qxl_bo_unref(&cursor_bo);
652 err:
653 	return NULL;
654 }
655 
656 static void qxl_free_cursor(struct qxl_bo *cursor_bo)
657 {
658 	if (!cursor_bo)
659 		return;
660 
661 	qxl_bo_unpin(cursor_bo);
662 	qxl_bo_unref(&cursor_bo);
663 }
664 
665 static void qxl_primary_atomic_update(struct drm_plane *plane,
666 				      struct drm_atomic_state *state)
667 {
668 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
669 									   plane);
670 	struct qxl_device *qdev = to_qxl(plane->dev);
671 	struct qxl_bo *bo = gem_to_qxl_bo(new_state->fb->obj[0]);
672 	struct qxl_bo *primary;
673 	struct drm_clip_rect norect = {
674 	    .x1 = 0,
675 	    .y1 = 0,
676 	    .x2 = new_state->fb->width,
677 	    .y2 = new_state->fb->height
678 	};
679 	uint32_t dumb_shadow_offset = 0;
680 
681 	primary = bo->shadow ? bo->shadow : bo;
682 
683 	if (!primary->is_primary) {
684 		if (qdev->primary_bo)
685 			qxl_io_destroy_primary(qdev);
686 		qxl_io_create_primary(qdev, primary);
687 		qxl_primary_apply_cursor(qdev, plane->state);
688 	}
689 
690 	if (bo->is_dumb)
691 		dumb_shadow_offset =
692 			qdev->dumb_heads[new_state->crtc->index].x;
693 
694 	qxl_draw_dirty_fb(qdev, new_state->fb, bo, 0, 0, &norect, 1, 1,
695 			  dumb_shadow_offset);
696 }
697 
698 static void qxl_primary_atomic_disable(struct drm_plane *plane,
699 				       struct drm_atomic_state *state)
700 {
701 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
702 									   plane);
703 	struct qxl_device *qdev = to_qxl(plane->dev);
704 
705 	if (old_state->fb) {
706 		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
707 
708 		if (bo->shadow)
709 			bo = bo->shadow;
710 		if (bo->is_primary)
711 			qxl_io_destroy_primary(qdev);
712 	}
713 }
714 
715 static void qxl_cursor_atomic_update(struct drm_plane *plane,
716 				     struct drm_atomic_state *state)
717 {
718 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
719 									   plane);
720 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
721 									   plane);
722 	struct qxl_device *qdev = to_qxl(plane->dev);
723 	struct drm_framebuffer *fb = new_state->fb;
724 
725 	if (fb != old_state->fb) {
726 		qxl_primary_apply_cursor(qdev, new_state);
727 	} else {
728 		qxl_primary_move_cursor(qdev, new_state);
729 	}
730 }
731 
732 static void qxl_cursor_atomic_disable(struct drm_plane *plane,
733 				      struct drm_atomic_state *state)
734 {
735 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
736 									   plane);
737 	struct qxl_device *qdev = to_qxl(plane->dev);
738 	struct qxl_crtc *qcrtc;
739 	struct qxl_release *release;
740 	struct qxl_cursor_cmd *cmd;
741 	int ret;
742 
743 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
744 					 QXL_RELEASE_CURSOR_CMD,
745 					 &release, NULL);
746 	if (ret)
747 		return;
748 
749 	ret = qxl_release_reserve_list(release, true);
750 	if (ret) {
751 		qxl_release_free(qdev, release);
752 		return;
753 	}
754 
755 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
756 	cmd->type = QXL_CURSOR_HIDE;
757 	qxl_release_unmap(qdev, release, &cmd->release_info);
758 
759 	qxl_release_fence_buffer_objects(release);
760 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
761 
762 	qcrtc = to_qxl_crtc(old_state->crtc);
763 	qxl_free_cursor(qcrtc->cursor_bo);
764 	qcrtc->cursor_bo = NULL;
765 }
766 
767 static void qxl_update_dumb_head(struct qxl_device *qdev,
768 				 int index, struct qxl_bo *bo)
769 {
770 	uint32_t width, height;
771 
772 	if (index >= qdev->monitors_config->max_allowed)
773 		return;
774 
775 	if (bo && bo->is_dumb) {
776 		width = bo->surf.width;
777 		height = bo->surf.height;
778 	} else {
779 		width = 0;
780 		height = 0;
781 	}
782 
783 	if (qdev->dumb_heads[index].width == width &&
784 	    qdev->dumb_heads[index].height == height)
785 		return;
786 
787 	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
788 		  qdev->dumb_heads[index].width,
789 		  qdev->dumb_heads[index].height,
790 		  width, height);
791 	qdev->dumb_heads[index].width = width;
792 	qdev->dumb_heads[index].height = height;
793 }
794 
795 static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
796 				 struct qxl_surface *surf)
797 {
798 	struct qxl_head *head;
799 	int i;
800 
801 	memset(surf, 0, sizeof(*surf));
802 	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
803 		head = qdev->dumb_heads + i;
804 		head->x = surf->width;
805 		surf->width += head->width;
806 		if (surf->height < head->height)
807 			surf->height = head->height;
808 	}
809 	if (surf->width < 64)
810 		surf->width = 64;
811 	if (surf->height < 64)
812 		surf->height = 64;
813 	surf->format = SPICE_SURFACE_FMT_32_xRGB;
814 	surf->stride = surf->width * 4;
815 
816 	if (!qdev->dumb_shadow_bo ||
817 	    qdev->dumb_shadow_bo->surf.width != surf->width ||
818 	    qdev->dumb_shadow_bo->surf.height != surf->height)
819 		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
820 }
821 
822 static void qxl_prepare_shadow(struct qxl_device *qdev, struct qxl_bo *user_bo,
823 			       int crtc_index)
824 {
825 	struct qxl_surface surf;
826 
827 	qxl_update_dumb_head(qdev, crtc_index,
828 			     user_bo);
829 	qxl_calc_dumb_shadow(qdev, &surf);
830 	if (!qdev->dumb_shadow_bo ||
831 	    qdev->dumb_shadow_bo->surf.width  != surf.width ||
832 	    qdev->dumb_shadow_bo->surf.height != surf.height) {
833 		if (qdev->dumb_shadow_bo) {
834 			qxl_bo_unpin(qdev->dumb_shadow_bo);
835 			drm_gem_object_put
836 				(&qdev->dumb_shadow_bo->tbo.base);
837 			qdev->dumb_shadow_bo = NULL;
838 		}
839 		qxl_bo_create(qdev, surf.height * surf.stride,
840 			      true, true, QXL_GEM_DOMAIN_SURFACE, 0,
841 			      &surf, &qdev->dumb_shadow_bo);
842 	}
843 	if (user_bo->shadow != qdev->dumb_shadow_bo) {
844 		if (user_bo->shadow) {
845 			qxl_bo_unpin(user_bo->shadow);
846 			drm_gem_object_put
847 				(&user_bo->shadow->tbo.base);
848 			user_bo->shadow = NULL;
849 		}
850 		drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
851 		user_bo->shadow = qdev->dumb_shadow_bo;
852 		qxl_bo_pin(user_bo->shadow);
853 	}
854 }
855 
856 static int qxl_plane_prepare_fb(struct drm_plane *plane,
857 				struct drm_plane_state *new_state)
858 {
859 	struct qxl_device *qdev = to_qxl(plane->dev);
860 	struct drm_gem_object *obj;
861 	struct qxl_bo *user_bo;
862 	int ret;
863 
864 	if (!new_state->fb)
865 		return 0;
866 
867 	obj = new_state->fb->obj[0];
868 	user_bo = gem_to_qxl_bo(obj);
869 
870 	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
871 	    user_bo->is_dumb) {
872 		qxl_prepare_shadow(qdev, user_bo, new_state->crtc->index);
873 	}
874 
875 	if (plane->type == DRM_PLANE_TYPE_CURSOR &&
876 	    plane->state->fb != new_state->fb) {
877 		struct qxl_crtc *qcrtc = to_qxl_crtc(new_state->crtc);
878 		struct qxl_bo *old_cursor_bo = qcrtc->cursor_bo;
879 
880 		qcrtc->cursor_bo = qxl_create_cursor(qdev, user_bo,
881 						     new_state->hotspot_x,
882 						     new_state->hotspot_y);
883 		qxl_free_cursor(old_cursor_bo);
884 	}
885 
886 	ret = qxl_bo_pin(user_bo);
887 	if (ret)
888 		return ret;
889 
890 	return drm_gem_plane_helper_prepare_fb(plane, new_state);
891 }
892 
893 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
894 				 struct drm_plane_state *old_state)
895 {
896 	struct drm_gem_object *obj;
897 	struct qxl_bo *user_bo;
898 
899 	if (!old_state->fb) {
900 		/*
901 		 * we never executed prepare_fb, so there's nothing to
902 		 * unpin.
903 		 */
904 		return;
905 	}
906 
907 	obj = old_state->fb->obj[0];
908 	user_bo = gem_to_qxl_bo(obj);
909 	qxl_bo_unpin(user_bo);
910 
911 	if (old_state->fb != plane->state->fb && user_bo->shadow) {
912 		qxl_bo_unpin(user_bo->shadow);
913 		drm_gem_object_put(&user_bo->shadow->tbo.base);
914 		user_bo->shadow = NULL;
915 	}
916 }
917 
918 static const uint32_t qxl_cursor_plane_formats[] = {
919 	DRM_FORMAT_ARGB8888,
920 };
921 
922 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
923 	.atomic_update = qxl_cursor_atomic_update,
924 	.atomic_disable = qxl_cursor_atomic_disable,
925 	.prepare_fb = qxl_plane_prepare_fb,
926 	.cleanup_fb = qxl_plane_cleanup_fb,
927 };
928 
929 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
930 	.update_plane	= drm_atomic_helper_update_plane,
931 	.disable_plane	= drm_atomic_helper_disable_plane,
932 	.destroy	= drm_plane_helper_destroy,
933 	.reset		= drm_atomic_helper_plane_reset,
934 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
935 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
936 };
937 
938 static const uint32_t qxl_primary_plane_formats[] = {
939 	DRM_FORMAT_XRGB8888,
940 	DRM_FORMAT_ARGB8888,
941 };
942 
943 static const struct drm_plane_helper_funcs primary_helper_funcs = {
944 	.atomic_check = qxl_primary_atomic_check,
945 	.atomic_update = qxl_primary_atomic_update,
946 	.atomic_disable = qxl_primary_atomic_disable,
947 	.prepare_fb = qxl_plane_prepare_fb,
948 	.cleanup_fb = qxl_plane_cleanup_fb,
949 };
950 
951 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
952 	.update_plane	= drm_atomic_helper_update_plane,
953 	.disable_plane	= drm_atomic_helper_disable_plane,
954 	.destroy	= drm_plane_helper_destroy,
955 	.reset		= drm_atomic_helper_plane_reset,
956 	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
957 	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
958 };
959 
960 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
961 					  unsigned int possible_crtcs,
962 					  enum drm_plane_type type)
963 {
964 	const struct drm_plane_helper_funcs *helper_funcs = NULL;
965 	struct drm_plane *plane;
966 	const struct drm_plane_funcs *funcs;
967 	const uint32_t *formats;
968 	int num_formats;
969 	int err;
970 
971 	if (type == DRM_PLANE_TYPE_PRIMARY) {
972 		funcs = &qxl_primary_plane_funcs;
973 		formats = qxl_primary_plane_formats;
974 		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
975 		helper_funcs = &primary_helper_funcs;
976 	} else if (type == DRM_PLANE_TYPE_CURSOR) {
977 		funcs = &qxl_cursor_plane_funcs;
978 		formats = qxl_cursor_plane_formats;
979 		helper_funcs = &qxl_cursor_helper_funcs;
980 		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
981 	} else {
982 		return ERR_PTR(-EINVAL);
983 	}
984 
985 	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
986 	if (!plane)
987 		return ERR_PTR(-ENOMEM);
988 
989 	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
990 				       funcs, formats, num_formats,
991 				       NULL, type, NULL);
992 	if (err)
993 		goto free_plane;
994 
995 	drm_plane_helper_add(plane, helper_funcs);
996 
997 	return plane;
998 
999 free_plane:
1000 	kfree(plane);
1001 	return ERR_PTR(-EINVAL);
1002 }
1003 
1004 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
1005 {
1006 	struct qxl_crtc *qxl_crtc;
1007 	struct drm_plane *primary, *cursor;
1008 	struct qxl_device *qdev = to_qxl(dev);
1009 	int r;
1010 
1011 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
1012 	if (!qxl_crtc)
1013 		return -ENOMEM;
1014 
1015 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
1016 	if (IS_ERR(primary)) {
1017 		r = -ENOMEM;
1018 		goto free_mem;
1019 	}
1020 
1021 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
1022 	if (IS_ERR(cursor)) {
1023 		r = -ENOMEM;
1024 		goto clean_primary;
1025 	}
1026 
1027 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
1028 				      &qxl_crtc_funcs, NULL);
1029 	if (r)
1030 		goto clean_cursor;
1031 
1032 	qxl_crtc->index = crtc_id;
1033 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
1034 	return 0;
1035 
1036 clean_cursor:
1037 	drm_plane_cleanup(cursor);
1038 	kfree(cursor);
1039 clean_primary:
1040 	drm_plane_cleanup(primary);
1041 	kfree(primary);
1042 free_mem:
1043 	kfree(qxl_crtc);
1044 	return r;
1045 }
1046 
1047 static int qxl_conn_get_modes(struct drm_connector *connector)
1048 {
1049 	struct drm_device *dev = connector->dev;
1050 	struct qxl_device *qdev = to_qxl(dev);
1051 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
1052 	unsigned int pwidth = 1024;
1053 	unsigned int pheight = 768;
1054 	int ret = 0;
1055 
1056 	if (qdev->client_monitors_config) {
1057 		struct qxl_head *head;
1058 		head = &qdev->client_monitors_config->heads[output->index];
1059 		if (head->width)
1060 			pwidth = head->width;
1061 		if (head->height)
1062 			pheight = head->height;
1063 	}
1064 
1065 	ret += drm_add_modes_noedid(connector, 8192, 8192);
1066 	ret += qxl_add_extra_modes(connector);
1067 	ret += qxl_add_monitors_config_modes(connector);
1068 	drm_set_preferred_mode(connector, pwidth, pheight);
1069 	return ret;
1070 }
1071 
1072 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1073 			       const struct drm_display_mode *mode)
1074 {
1075 	struct drm_device *ddev = connector->dev;
1076 	struct qxl_device *qdev = to_qxl(ddev);
1077 
1078 	if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1079 		return MODE_BAD;
1080 
1081 	return MODE_OK;
1082 }
1083 
1084 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1085 {
1086 	struct qxl_output *qxl_output =
1087 		drm_connector_to_qxl_output(connector);
1088 
1089 	DRM_DEBUG("\n");
1090 	return &qxl_output->enc;
1091 }
1092 
1093 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1094 	.get_modes = qxl_conn_get_modes,
1095 	.mode_valid = qxl_conn_mode_valid,
1096 	.best_encoder = qxl_best_encoder,
1097 };
1098 
1099 static enum drm_connector_status qxl_conn_detect(
1100 			struct drm_connector *connector,
1101 			bool force)
1102 {
1103 	struct qxl_output *output =
1104 		drm_connector_to_qxl_output(connector);
1105 	struct drm_device *ddev = connector->dev;
1106 	struct qxl_device *qdev = to_qxl(ddev);
1107 	bool connected = false;
1108 
1109 	/* The first monitor is always connected */
1110 	if (!qdev->client_monitors_config) {
1111 		if (output->index == 0)
1112 			connected = true;
1113 	} else
1114 		connected = qdev->client_monitors_config->count > output->index &&
1115 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1116 
1117 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1118 
1119 	return connected ? connector_status_connected
1120 			 : connector_status_disconnected;
1121 }
1122 
1123 static void qxl_conn_destroy(struct drm_connector *connector)
1124 {
1125 	struct qxl_output *qxl_output =
1126 		drm_connector_to_qxl_output(connector);
1127 
1128 	drm_connector_unregister(connector);
1129 	drm_connector_cleanup(connector);
1130 	kfree(qxl_output);
1131 }
1132 
1133 static const struct drm_connector_funcs qxl_connector_funcs = {
1134 	.detect = qxl_conn_detect,
1135 	.fill_modes = drm_helper_probe_single_connector_modes,
1136 	.destroy = qxl_conn_destroy,
1137 	.reset = drm_atomic_helper_connector_reset,
1138 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1139 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1140 };
1141 
1142 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1143 {
1144 	if (qdev->hotplug_mode_update_property)
1145 		return 0;
1146 
1147 	qdev->hotplug_mode_update_property =
1148 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1149 					  "hotplug_mode_update", 0, 1);
1150 
1151 	return 0;
1152 }
1153 
1154 static int qdev_output_init(struct drm_device *dev, int num_output)
1155 {
1156 	struct qxl_device *qdev = to_qxl(dev);
1157 	struct qxl_output *qxl_output;
1158 	struct drm_connector *connector;
1159 	struct drm_encoder *encoder;
1160 	int ret;
1161 
1162 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1163 	if (!qxl_output)
1164 		return -ENOMEM;
1165 
1166 	qxl_output->index = num_output;
1167 
1168 	connector = &qxl_output->base;
1169 	encoder = &qxl_output->enc;
1170 	drm_connector_init(dev, &qxl_output->base,
1171 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1172 
1173 	ret = drm_simple_encoder_init(dev, &qxl_output->enc,
1174 				      DRM_MODE_ENCODER_VIRTUAL);
1175 	if (ret) {
1176 		drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
1177 			ret);
1178 		goto err_drm_connector_cleanup;
1179 	}
1180 
1181 	/* we get HPD via client monitors config */
1182 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1183 	encoder->possible_crtcs = 1 << num_output;
1184 	drm_connector_attach_encoder(&qxl_output->base,
1185 					  &qxl_output->enc);
1186 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1187 
1188 	drm_object_attach_property(&connector->base,
1189 				   qdev->hotplug_mode_update_property, 0);
1190 	drm_object_attach_property(&connector->base,
1191 				   dev->mode_config.suggested_x_property, 0);
1192 	drm_object_attach_property(&connector->base,
1193 				   dev->mode_config.suggested_y_property, 0);
1194 	return 0;
1195 
1196 err_drm_connector_cleanup:
1197 	drm_connector_cleanup(&qxl_output->base);
1198 	kfree(qxl_output);
1199 	return ret;
1200 }
1201 
1202 static struct drm_framebuffer *
1203 qxl_user_framebuffer_create(struct drm_device *dev,
1204 			    struct drm_file *file_priv,
1205 			    const struct drm_format_info *info,
1206 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1207 {
1208 	return drm_gem_fb_create_with_funcs(dev, file_priv, info, mode_cmd,
1209 					    &qxl_fb_funcs);
1210 }
1211 
1212 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1213 	.fb_create = qxl_user_framebuffer_create,
1214 	.atomic_check = drm_atomic_helper_check,
1215 	.atomic_commit = drm_atomic_helper_commit,
1216 };
1217 
1218 int qxl_create_monitors_object(struct qxl_device *qdev)
1219 {
1220 	int ret;
1221 	struct drm_gem_object *gobj;
1222 	struct iosys_map map;
1223 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1224 		qxl_num_crtc * sizeof(struct qxl_head);
1225 
1226 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1227 				    QXL_GEM_DOMAIN_VRAM,
1228 				    false, false, NULL, &gobj);
1229 	if (ret) {
1230 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1231 		return -ENOMEM;
1232 	}
1233 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1234 
1235 	ret = qxl_bo_pin_and_vmap(qdev->monitors_config_bo, &map);
1236 	if (ret)
1237 		return ret;
1238 
1239 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1240 	qdev->ram_header->monitors_config =
1241 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1242 
1243 	memset(qdev->monitors_config, 0, monitors_config_size);
1244 	qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1245 				   GFP_KERNEL);
1246 	if (!qdev->dumb_heads) {
1247 		qxl_destroy_monitors_object(qdev);
1248 		return -ENOMEM;
1249 	}
1250 	return 0;
1251 }
1252 
1253 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1254 {
1255 	int ret;
1256 
1257 	if (!qdev->monitors_config_bo)
1258 		return 0;
1259 
1260 	kfree(qdev->dumb_heads);
1261 	qdev->dumb_heads = NULL;
1262 
1263 	qdev->monitors_config = NULL;
1264 	qdev->ram_header->monitors_config = 0;
1265 
1266 	ret = qxl_bo_vunmap_and_unpin(qdev->monitors_config_bo);
1267 	if (ret)
1268 		return ret;
1269 
1270 	qxl_bo_unref(&qdev->monitors_config_bo);
1271 	return 0;
1272 }
1273 
1274 int qxl_modeset_init(struct qxl_device *qdev)
1275 {
1276 	int i;
1277 	int ret;
1278 
1279 	ret = drmm_mode_config_init(&qdev->ddev);
1280 	if (ret)
1281 		return ret;
1282 
1283 	ret = qxl_create_monitors_object(qdev);
1284 	if (ret)
1285 		return ret;
1286 
1287 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1288 
1289 	/* modes will be validated against the framebuffer size */
1290 	qdev->ddev.mode_config.min_width = 0;
1291 	qdev->ddev.mode_config.min_height = 0;
1292 	qdev->ddev.mode_config.max_width = 8192;
1293 	qdev->ddev.mode_config.max_height = 8192;
1294 
1295 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
1296 	qxl_mode_create_hotplug_mode_update_property(qdev);
1297 
1298 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1299 		qdev_crtc_init(&qdev->ddev, i);
1300 		qdev_output_init(&qdev->ddev, i);
1301 	}
1302 
1303 	qxl_display_read_client_monitors_config(qdev);
1304 
1305 	ret = drm_vblank_init(&qdev->ddev, qxl_num_crtc);
1306 	if (ret)
1307 		return ret;
1308 
1309 	drm_mode_config_reset(&qdev->ddev);
1310 	return 0;
1311 }
1312 
1313 void qxl_modeset_fini(struct qxl_device *qdev)
1314 {
1315 	if (qdev->dumb_shadow_bo) {
1316 		qxl_bo_unpin(qdev->dumb_shadow_bo);
1317 		drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
1318 		qdev->dumb_shadow_bo = NULL;
1319 	}
1320 	qxl_destroy_monitors_object(qdev);
1321 }
1322