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