xref: /linux/drivers/gpu/drm/qxl/qxl_display.c (revision b67f408a0c2427d5d7c682cfbdad87c4ff73265b)
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_blend.h>
34 #include <drm/drm_edid.h>
35 #include <drm/drm_encoder.h>
36 #include <drm/drm_framebuffer.h>
37 #include <drm/drm_gem_framebuffer_helper.h>
38 #include <drm/drm_plane_helper.h>
39 #include <drm/drm_print.h>
40 #include <drm/drm_probe_helper.h>
41 #include <drm/drm_gem_atomic_helper.h>
42 #include <drm/drm_vblank.h>
43 #include <drm/drm_vblank_helper.h>
44 
45 #include "qxl_drv.h"
46 #include "qxl_object.h"
47 
48 static bool qxl_head_enabled(struct qxl_head *head)
49 {
50 	return head->width && head->height;
51 }
52 
53 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
54 		unsigned int count)
55 {
56 	if (qdev->client_monitors_config &&
57 	    count > qdev->client_monitors_config->count) {
58 		kfree(qdev->client_monitors_config);
59 		qdev->client_monitors_config = NULL;
60 	}
61 	if (!qdev->client_monitors_config) {
62 		qdev->client_monitors_config = kzalloc_flex(*qdev->client_monitors_config,
63 							    heads, count);
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_commit *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_commit *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_commit *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_commit *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_commit *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_commit *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_commit *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_commit *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_obj(*plane);
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 	drm_plane_create_blend_mode_property(plane,
998 					     BIT(DRM_MODE_BLEND_PREMULTI));
999 
1000 	return plane;
1001 
1002 free_plane:
1003 	kfree(plane);
1004 	return ERR_PTR(-EINVAL);
1005 }
1006 
1007 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
1008 {
1009 	struct qxl_crtc *qxl_crtc;
1010 	struct drm_plane *primary, *cursor;
1011 	struct qxl_device *qdev = to_qxl(dev);
1012 	int r;
1013 
1014 	qxl_crtc = kzalloc_obj(struct qxl_crtc);
1015 	if (!qxl_crtc)
1016 		return -ENOMEM;
1017 
1018 	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
1019 	if (IS_ERR(primary)) {
1020 		r = -ENOMEM;
1021 		goto free_mem;
1022 	}
1023 
1024 	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
1025 	if (IS_ERR(cursor)) {
1026 		r = -ENOMEM;
1027 		goto clean_primary;
1028 	}
1029 
1030 	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
1031 				      &qxl_crtc_funcs, NULL);
1032 	if (r)
1033 		goto clean_cursor;
1034 
1035 	qxl_crtc->index = crtc_id;
1036 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
1037 	return 0;
1038 
1039 clean_cursor:
1040 	drm_plane_cleanup(cursor);
1041 	kfree(cursor);
1042 clean_primary:
1043 	drm_plane_cleanup(primary);
1044 	kfree(primary);
1045 free_mem:
1046 	kfree(qxl_crtc);
1047 	return r;
1048 }
1049 
1050 static int qxl_conn_get_modes(struct drm_connector *connector)
1051 {
1052 	struct drm_device *dev = connector->dev;
1053 	struct qxl_device *qdev = to_qxl(dev);
1054 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
1055 	unsigned int pwidth = 1024;
1056 	unsigned int pheight = 768;
1057 	int ret = 0;
1058 
1059 	if (qdev->client_monitors_config) {
1060 		struct qxl_head *head;
1061 		head = &qdev->client_monitors_config->heads[output->index];
1062 		if (head->width)
1063 			pwidth = head->width;
1064 		if (head->height)
1065 			pheight = head->height;
1066 	}
1067 
1068 	ret += drm_add_modes_noedid(connector, 8192, 8192);
1069 	ret += qxl_add_extra_modes(connector);
1070 	ret += qxl_add_monitors_config_modes(connector);
1071 	drm_set_preferred_mode(connector, pwidth, pheight);
1072 	return ret;
1073 }
1074 
1075 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1076 			       const struct drm_display_mode *mode)
1077 {
1078 	struct drm_device *ddev = connector->dev;
1079 	struct qxl_device *qdev = to_qxl(ddev);
1080 
1081 	if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1082 		return MODE_BAD;
1083 
1084 	return MODE_OK;
1085 }
1086 
1087 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1088 {
1089 	struct qxl_output *qxl_output =
1090 		drm_connector_to_qxl_output(connector);
1091 
1092 	DRM_DEBUG("\n");
1093 	return &qxl_output->enc;
1094 }
1095 
1096 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1097 	.get_modes = qxl_conn_get_modes,
1098 	.mode_valid = qxl_conn_mode_valid,
1099 	.best_encoder = qxl_best_encoder,
1100 };
1101 
1102 static const struct drm_encoder_funcs qxl_encoder_funcs = {
1103 	.destroy = drm_encoder_cleanup,
1104 };
1105 
1106 static enum drm_connector_status qxl_conn_detect(
1107 			struct drm_connector *connector,
1108 			bool force)
1109 {
1110 	struct qxl_output *output =
1111 		drm_connector_to_qxl_output(connector);
1112 	struct drm_device *ddev = connector->dev;
1113 	struct qxl_device *qdev = to_qxl(ddev);
1114 	bool connected = false;
1115 
1116 	/* The first monitor is always connected */
1117 	if (!qdev->client_monitors_config) {
1118 		if (output->index == 0)
1119 			connected = true;
1120 	} else
1121 		connected = qdev->client_monitors_config->count > output->index &&
1122 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1123 
1124 	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1125 
1126 	return connected ? connector_status_connected
1127 			 : connector_status_disconnected;
1128 }
1129 
1130 static void qxl_conn_destroy(struct drm_connector *connector)
1131 {
1132 	struct qxl_output *qxl_output =
1133 		drm_connector_to_qxl_output(connector);
1134 
1135 	drm_connector_unregister(connector);
1136 	drm_connector_cleanup(connector);
1137 	kfree(qxl_output);
1138 }
1139 
1140 static const struct drm_connector_funcs qxl_connector_funcs = {
1141 	.detect = qxl_conn_detect,
1142 	.fill_modes = drm_helper_probe_single_connector_modes,
1143 	.destroy = qxl_conn_destroy,
1144 	.reset = drm_atomic_helper_connector_reset,
1145 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1146 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1147 };
1148 
1149 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1150 {
1151 	if (qdev->hotplug_mode_update_property)
1152 		return 0;
1153 
1154 	qdev->hotplug_mode_update_property =
1155 		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1156 					  "hotplug_mode_update", 0, 1);
1157 
1158 	return 0;
1159 }
1160 
1161 static int qdev_output_init(struct drm_device *dev, int num_output)
1162 {
1163 	struct qxl_device *qdev = to_qxl(dev);
1164 	struct qxl_output *qxl_output;
1165 	struct drm_connector *connector;
1166 	struct drm_encoder *encoder;
1167 	int ret;
1168 
1169 	qxl_output = kzalloc_obj(struct qxl_output);
1170 	if (!qxl_output)
1171 		return -ENOMEM;
1172 
1173 	qxl_output->index = num_output;
1174 
1175 	connector = &qxl_output->base;
1176 	encoder = &qxl_output->enc;
1177 	drm_connector_init(dev, &qxl_output->base,
1178 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1179 
1180 	ret = drm_encoder_init(dev, &qxl_output->enc, &qxl_encoder_funcs,
1181 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
1182 	if (ret) {
1183 		drm_err(dev, "drm_encoder_init() failed, error %d\n",
1184 			ret);
1185 		goto err_drm_connector_cleanup;
1186 	}
1187 
1188 	/* we get HPD via client monitors config */
1189 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1190 	encoder->possible_crtcs = 1 << num_output;
1191 	drm_connector_attach_encoder(&qxl_output->base,
1192 					  &qxl_output->enc);
1193 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1194 
1195 	drm_object_attach_property(&connector->base,
1196 				   qdev->hotplug_mode_update_property, 0);
1197 	drm_object_attach_property(&connector->base,
1198 				   dev->mode_config.suggested_x_property, 0);
1199 	drm_object_attach_property(&connector->base,
1200 				   dev->mode_config.suggested_y_property, 0);
1201 	return 0;
1202 
1203 err_drm_connector_cleanup:
1204 	drm_connector_cleanup(&qxl_output->base);
1205 	kfree(qxl_output);
1206 	return ret;
1207 }
1208 
1209 static struct drm_framebuffer *
1210 qxl_user_framebuffer_create(struct drm_device *dev,
1211 			    struct drm_file *file_priv,
1212 			    const struct drm_format_info *info,
1213 			    const struct drm_mode_fb_cmd2 *mode_cmd)
1214 {
1215 	return drm_gem_fb_create_with_funcs(dev, file_priv, info, mode_cmd,
1216 					    &qxl_fb_funcs);
1217 }
1218 
1219 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1220 	.fb_create = qxl_user_framebuffer_create,
1221 	.atomic_check = drm_atomic_helper_check,
1222 	.atomic_commit = drm_atomic_helper_commit,
1223 };
1224 
1225 int qxl_create_monitors_object(struct qxl_device *qdev)
1226 {
1227 	int ret;
1228 	struct drm_gem_object *gobj;
1229 	struct iosys_map map;
1230 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1231 		qxl_num_crtc * sizeof(struct qxl_head);
1232 
1233 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1234 				    QXL_GEM_DOMAIN_VRAM,
1235 				    false, false, NULL, &gobj);
1236 	if (ret) {
1237 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1238 		return -ENOMEM;
1239 	}
1240 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1241 
1242 	ret = qxl_bo_pin_and_vmap(qdev->monitors_config_bo, &map);
1243 	if (ret)
1244 		return ret;
1245 
1246 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1247 	qdev->ram_header->monitors_config =
1248 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1249 
1250 	memset(qdev->monitors_config, 0, monitors_config_size);
1251 	qdev->dumb_heads = kzalloc_objs(qdev->dumb_heads[0], qxl_num_crtc);
1252 	if (!qdev->dumb_heads) {
1253 		qxl_destroy_monitors_object(qdev);
1254 		return -ENOMEM;
1255 	}
1256 	return 0;
1257 }
1258 
1259 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1260 {
1261 	int ret;
1262 
1263 	if (!qdev->monitors_config_bo)
1264 		return 0;
1265 
1266 	kfree(qdev->dumb_heads);
1267 	qdev->dumb_heads = NULL;
1268 
1269 	qdev->monitors_config = NULL;
1270 	qdev->ram_header->monitors_config = 0;
1271 
1272 	ret = qxl_bo_vunmap_and_unpin(qdev->monitors_config_bo);
1273 	if (ret)
1274 		return ret;
1275 
1276 	qxl_bo_unref(&qdev->monitors_config_bo);
1277 	return 0;
1278 }
1279 
1280 int qxl_modeset_init(struct qxl_device *qdev)
1281 {
1282 	int i;
1283 	int ret;
1284 
1285 	ret = drmm_mode_config_init(&qdev->ddev);
1286 	if (ret)
1287 		return ret;
1288 
1289 	ret = qxl_create_monitors_object(qdev);
1290 	if (ret)
1291 		return ret;
1292 
1293 	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1294 
1295 	/* modes will be validated against the framebuffer size */
1296 	qdev->ddev.mode_config.min_width = 0;
1297 	qdev->ddev.mode_config.min_height = 0;
1298 	qdev->ddev.mode_config.max_width = 8192;
1299 	qdev->ddev.mode_config.max_height = 8192;
1300 
1301 	drm_mode_create_suggested_offset_properties(&qdev->ddev);
1302 	qxl_mode_create_hotplug_mode_update_property(qdev);
1303 
1304 	for (i = 0 ; i < qxl_num_crtc; ++i) {
1305 		qdev_crtc_init(&qdev->ddev, i);
1306 		qdev_output_init(&qdev->ddev, i);
1307 	}
1308 
1309 	qxl_display_read_client_monitors_config(qdev);
1310 
1311 	ret = drm_vblank_init(&qdev->ddev, qxl_num_crtc);
1312 	if (ret)
1313 		return ret;
1314 
1315 	drm_mode_config_reset(&qdev->ddev);
1316 	return 0;
1317 }
1318 
1319 void qxl_modeset_fini(struct qxl_device *qdev)
1320 {
1321 	if (qdev->dumb_shadow_bo) {
1322 		qxl_bo_unpin(qdev->dumb_shadow_bo);
1323 		drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
1324 		qdev->dumb_shadow_bo = NULL;
1325 	}
1326 	qxl_destroy_monitors_object(qdev);
1327 }
1328