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