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