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