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