qxl_display.c 32.9 KB
Newer Older
D
Dave Airlie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2013 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Dave Airlie
 *          Alon Levy
 */

R
Randy Dunlap 已提交
26
#include <linux/crc32.h>
27
#include <drm/drm_atomic.h>
28
#include <drm/drm_atomic_helper.h>
29
#include <drm/drm_gem_framebuffer_helper.h>
30 31
#include <drm/drm_plane_helper.h>
#include <drm/drm_probe_helper.h>
D
Dave Airlie 已提交
32

33 34 35
#include "qxl_drv.h"
#include "qxl_object.h"

36 37 38 39 40
static bool qxl_head_enabled(struct qxl_head *head)
{
	return head->width && head->height;
}

41 42
static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
		unsigned int count)
D
Dave Airlie 已提交
43 44 45 46
{
	if (qdev->client_monitors_config &&
	    count > qdev->client_monitors_config->count) {
		kfree(qdev->client_monitors_config);
D
Dave Airlie 已提交
47
		qdev->client_monitors_config = NULL;
D
Dave Airlie 已提交
48 49 50
	}
	if (!qdev->client_monitors_config) {
		qdev->client_monitors_config = kzalloc(
51 52
				struct_size(qdev->client_monitors_config,
				heads, count), GFP_KERNEL);
G
Gerd Hoffmann 已提交
53
		if (!qdev->client_monitors_config)
54
			return -ENOMEM;
D
Dave Airlie 已提交
55 56
	}
	qdev->client_monitors_config->count = count;
57
	return 0;
D
Dave Airlie 已提交
58 59
}

60 61 62 63
enum {
	MONITORS_CONFIG_MODIFIED,
	MONITORS_CONFIG_UNCHANGED,
	MONITORS_CONFIG_BAD_CRC,
64
	MONITORS_CONFIG_ERROR,
65 66
};

D
Dave Airlie 已提交
67 68 69 70 71
static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
{
	int i;
	int num_monitors;
	uint32_t crc;
72
	int status = MONITORS_CONFIG_UNCHANGED;
D
Dave Airlie 已提交
73 74 75 76

	num_monitors = qdev->rom->client_monitors_config.count;
	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
		  sizeof(qdev->rom->client_monitors_config));
G
Gerd Hoffmann 已提交
77
	if (crc != qdev->rom->client_monitors_config_crc)
78
		return MONITORS_CONFIG_BAD_CRC;
79 80 81 82
	if (!num_monitors) {
		DRM_DEBUG_KMS("no client monitors configured\n");
		return status;
	}
83
	if (num_monitors > qxl_num_crtc) {
84
		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
85 86
			      qxl_num_crtc, num_monitors);
		num_monitors = qxl_num_crtc;
D
Dave Airlie 已提交
87 88 89
	} else {
		num_monitors = qdev->rom->client_monitors_config.count;
	}
90 91 92 93
	if (qdev->client_monitors_config
	      && (num_monitors != qdev->client_monitors_config->count)) {
		status = MONITORS_CONFIG_MODIFIED;
	}
94 95 96 97
	if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
		status = MONITORS_CONFIG_ERROR;
		return status;
	}
D
Dave Airlie 已提交
98
	/* we copy max from the client but it isn't used */
99
	qdev->client_monitors_config->max_allowed = qxl_num_crtc;
D
Dave Airlie 已提交
100 101 102 103 104
	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
		struct qxl_urect *c_rect =
			&qdev->rom->client_monitors_config.heads[i];
		struct qxl_head *client_head =
			&qdev->client_monitors_config->heads[i];
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		if (client_head->x != c_rect->left) {
			client_head->x = c_rect->left;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->y != c_rect->top) {
			client_head->y = c_rect->top;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->width != c_rect->right - c_rect->left) {
			client_head->width = c_rect->right - c_rect->left;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->height != c_rect->bottom - c_rect->top) {
			client_head->height = c_rect->bottom - c_rect->top;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->surface_id != 0) {
			client_head->surface_id = 0;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->id != i) {
			client_head->id = i;
			status = MONITORS_CONFIG_MODIFIED;
		}
		if (client_head->flags != 0) {
			client_head->flags = 0;
			status = MONITORS_CONFIG_MODIFIED;
		}
133 134
		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
			  client_head->x, client_head->y);
D
Dave Airlie 已提交
135
	}
136 137

	return status;
D
Dave Airlie 已提交
138 139
}

140 141
static void qxl_update_offset_props(struct qxl_device *qdev)
{
142
	struct drm_device *dev = &qdev->ddev;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	struct drm_connector *connector;
	struct qxl_output *output;
	struct qxl_head *head;

	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
		output = drm_connector_to_qxl_output(connector);

		head = &qdev->client_monitors_config->heads[output->index];

		drm_object_property_set_value(&connector->base,
			dev->mode_config.suggested_x_property, head->x);
		drm_object_property_set_value(&connector->base,
			dev->mode_config.suggested_y_property, head->y);
	}
}

D
Dave Airlie 已提交
159 160
void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
{
161
	struct drm_device *dev = &qdev->ddev;
162
	int status, retries;
163

164
	for (retries = 0; retries < 10; retries++) {
165
		status = qxl_display_copy_rom_client_monitors_config(qdev);
166 167 168 169
		if (status != MONITORS_CONFIG_BAD_CRC)
			break;
		udelay(5);
	}
170 171 172 173
	if (status == MONITORS_CONFIG_ERROR) {
		DRM_DEBUG_KMS("ignoring client monitors config: error");
		return;
	}
174 175 176
	if (status == MONITORS_CONFIG_BAD_CRC) {
		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
		return;
177 178
	}
	if (status == MONITORS_CONFIG_UNCHANGED) {
179
		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
180
		return;
D
Dave Airlie 已提交
181
	}
182

183 184 185
	drm_modeset_lock_all(dev);
	qxl_update_offset_props(qdev);
	drm_modeset_unlock_all(dev);
186
	if (!drm_helper_hpd_irq_event(dev)) {
187 188
		/* notify that the monitor configuration changed, to
		   adjust at the arbitrary resolution */
189
		drm_kms_helper_hotplug_event(dev);
190
	}
D
Dave Airlie 已提交
191 192
}

193 194 195
static int qxl_add_monitors_config_modes(struct drm_connector *connector,
                                         unsigned *pwidth,
                                         unsigned *pheight)
D
Dave Airlie 已提交
196 197 198 199 200 201 202 203
{
	struct drm_device *dev = connector->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct qxl_output *output = drm_connector_to_qxl_output(connector);
	int h = output->index;
	struct drm_display_mode *mode = NULL;
	struct qxl_head *head;

G
Gerd Hoffmann 已提交
204 205
	if (!qdev->monitors_config)
		return 0;
206
	if (h >= qxl_num_crtc)
G
Gerd Hoffmann 已提交
207
		return 0;
208
	if (!qdev->client_monitors_config)
D
Dave Airlie 已提交
209
		return 0;
G
Gerd Hoffmann 已提交
210 211 212
	if (h >= qdev->client_monitors_config->count)
		return 0;

213
	head = &qdev->client_monitors_config->heads[h];
G
Gerd Hoffmann 已提交
214
	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
D
Dave Airlie 已提交
215 216 217 218

	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
			    false);
	mode->type |= DRM_MODE_TYPE_PREFERRED;
219 220 221
	mode->hdisplay = head->width;
	mode->vdisplay = head->height;
	drm_mode_set_name(mode);
222 223
	*pwidth = head->width;
	*pheight = head->height;
D
Dave Airlie 已提交
224
	drm_mode_probed_add(connector, mode);
225 226 227
	/* remember the last custom size for mode validation */
	qdev->monitors_config_width = mode->hdisplay;
	qdev->monitors_config_height = mode->vdisplay;
D
Dave Airlie 已提交
228 229 230
	return 1;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static struct mode_size {
	int w;
	int h;
} common_modes[] = {
	{ 640,  480},
	{ 720,  480},
	{ 800,  600},
	{ 848,  480},
	{1024,  768},
	{1152,  768},
	{1280,  720},
	{1280,  800},
	{1280,  854},
	{1280,  960},
	{1280, 1024},
	{1440,  900},
	{1400, 1050},
	{1680, 1050},
	{1600, 1200},
	{1920, 1080},
	{1920, 1200}
};

254
static int qxl_add_common_modes(struct drm_connector *connector,
255 256
                                unsigned int pwidth,
                                unsigned int pheight)
D
Dave Airlie 已提交
257 258 259 260
{
	struct drm_device *dev = connector->dev;
	struct drm_display_mode *mode = NULL;
	int i;
261

D
Dave Airlie 已提交
262 263 264
	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
				    60, false, false, false);
265
		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
D
Dave Airlie 已提交
266 267 268 269 270 271
			mode->type |= DRM_MODE_TYPE_PREFERRED;
		drm_mode_probed_add(connector, mode);
	}
	return i - 1;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static void qxl_send_monitors_config(struct qxl_device *qdev)
{
	int i;

	BUG_ON(!qdev->ram_header->monitors_config);

	if (qdev->monitors_config->count == 0)
		return;

	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
		struct qxl_head *head = &qdev->monitors_config->heads[i];

		if (head->y > 8192 || head->x > 8192 ||
		    head->width > 8192 || head->height > 8192) {
			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
				  i, head->width, head->height,
				  head->x, head->y);
			return;
		}
	}
	qxl_io_monitors_config(qdev);
}

295 296 297 298 299 300 301 302 303
static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
					    const char *reason)
{
	struct drm_device *dev = crtc->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
	struct qxl_head head;
	int oldcount, i = qcrtc->index;

G
Gerd Hoffmann 已提交
304
	if (!qdev->primary_bo) {
305 306 307 308
		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
		return;
	}

309
	if (!qdev->monitors_config || qxl_num_crtc <= i)
310 311 312 313 314 315 316
		return;

	head.id = i;
	head.flags = 0;
	oldcount = qdev->monitors_config->count;
	if (crtc->state->active) {
		struct drm_display_mode *mode = &crtc->mode;
317

318 319 320 321 322 323
		head.width = mode->hdisplay;
		head.height = mode->vdisplay;
		head.x = crtc->x;
		head.y = crtc->y;
		if (qdev->monitors_config->count < i + 1)
			qdev->monitors_config->count = i + 1;
324 325
		if (qdev->primary_bo == qdev->dumb_shadow_bo)
			head.x += qdev->dumb_heads[i].x;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	} else if (i > 0) {
		head.width = 0;
		head.height = 0;
		head.x = 0;
		head.y = 0;
		if (qdev->monitors_config->count == i + 1)
			qdev->monitors_config->count = i;
	} else {
		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
		return;
	}

	if (head.width  == qdev->monitors_config->heads[i].width  &&
	    head.height == qdev->monitors_config->heads[i].height &&
	    head.x      == qdev->monitors_config->heads[i].x      &&
	    head.y      == qdev->monitors_config->heads[i].y      &&
	    oldcount    == qdev->monitors_config->count)
		return;

	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
		      i, head.width, head.height, head.x, head.y,
		      crtc->state->active ? "on" : "off", reason);
	if (oldcount != qdev->monitors_config->count)
		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
			      oldcount, qdev->monitors_config->count,
351
			      qxl_num_crtc);
352 353

	qdev->monitors_config->heads[i] = head;
354
	qdev->monitors_config->max_allowed = qxl_num_crtc;
355 356 357
	qxl_send_monitors_config(qdev);
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
				  struct drm_crtc_state *old_crtc_state)
{
	struct drm_device *dev = crtc->dev;
	struct drm_pending_vblank_event *event;
	unsigned long flags;

	if (crtc->state && crtc->state->event) {
		event = crtc->state->event;
		crtc->state->event = NULL;

		spin_lock_irqsave(&dev->event_lock, flags);
		drm_crtc_send_vblank_event(crtc, event);
		spin_unlock_irqrestore(&dev->event_lock, flags);
	}
373 374

	qxl_crtc_update_monitors_config(crtc, "flush");
375 376
}

D
Dave Airlie 已提交
377 378 379 380
static void qxl_crtc_destroy(struct drm_crtc *crtc)
{
	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);

381
	qxl_bo_unref(&qxl_crtc->cursor_bo);
D
Dave Airlie 已提交
382 383 384 385 386
	drm_crtc_cleanup(crtc);
	kfree(qxl_crtc);
}

static const struct drm_crtc_funcs qxl_crtc_funcs = {
387
	.set_config = drm_atomic_helper_set_config,
D
Dave Airlie 已提交
388
	.destroy = qxl_crtc_destroy,
389
	.page_flip = drm_atomic_helper_page_flip,
390 391 392
	.reset = drm_atomic_helper_crtc_reset,
	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
D
Dave Airlie 已提交
393 394
};

395 396
static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
					 struct drm_file *file_priv,
397
					 unsigned int flags, unsigned int color,
398
					 struct drm_clip_rect *clips,
399
					 unsigned int num_clips)
D
Dave Airlie 已提交
400 401
{
	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
402
	struct qxl_device *qdev = fb->dev->dev_private;
D
Dave Airlie 已提交
403 404
	struct drm_clip_rect norect;
	struct qxl_bo *qobj;
G
Gerd Hoffmann 已提交
405
	bool is_primary;
D
Dave Airlie 已提交
406 407
	int inc = 1;

408 409
	drm_modeset_lock_all(fb->dev);

410
	qobj = gem_to_qxl_bo(fb->obj[0]);
411
	/* if we aren't primary surface ignore this */
G
Gerd Hoffmann 已提交
412 413
	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
	if (!is_primary) {
414
		drm_modeset_unlock_all(fb->dev);
415
		return 0;
416
	}
417

D
Dave Airlie 已提交
418 419 420 421 422 423 424 425 426 427 428
	if (!num_clips) {
		num_clips = 1;
		clips = &norect;
		norect.x1 = norect.y1 = 0;
		norect.x2 = fb->width;
		norect.y2 = fb->height;
	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
		num_clips /= 2;
		inc = 2; /* skip source rects */
	}

429
	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
430
			  clips, num_clips, inc, 0);
431 432 433

	drm_modeset_unlock_all(fb->dev);

D
Dave Airlie 已提交
434 435 436 437
	return 0;
}

static const struct drm_framebuffer_funcs qxl_fb_funcs = {
438
	.destroy = drm_gem_fb_destroy,
D
Dave Airlie 已提交
439
	.dirty = qxl_framebuffer_surface_dirty,
440
	.create_handle = drm_gem_fb_create_handle,
D
Dave Airlie 已提交
441 442
};

443 444
static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
				   struct drm_crtc_state *old_state)
D
Dave Airlie 已提交
445
{
446
	qxl_crtc_update_monitors_config(crtc, "enable");
D
Dave Airlie 已提交
447 448
}

449 450
static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_state)
451
{
452
	qxl_crtc_update_monitors_config(crtc, "disable");
453 454
}

D
Dave Airlie 已提交
455
static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
456
	.atomic_flush = qxl_crtc_atomic_flush,
457
	.atomic_enable = qxl_crtc_atomic_enable,
458
	.atomic_disable = qxl_crtc_atomic_disable,
D
Dave Airlie 已提交
459 460
};

461 462
static int qxl_primary_atomic_check(struct drm_plane *plane,
				    struct drm_plane_state *state)
463 464 465 466 467 468 469
{
	struct qxl_device *qdev = plane->dev->dev_private;
	struct qxl_bo *bo;

	if (!state->crtc || !state->fb)
		return 0;

470
	bo = gem_to_qxl_bo(state->fb->obj[0]);
471 472 473 474 475 476 477 478 479

	if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
		DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
		return -EINVAL;
	}

	return 0;
}

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
static int qxl_primary_apply_cursor(struct drm_plane *plane)
{
	struct drm_device *dev = plane->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct drm_framebuffer *fb = plane->state->fb;
	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
	struct qxl_cursor_cmd *cmd;
	struct qxl_release *release;
	int ret = 0;

	if (!qcrtc->cursor_bo)
		return 0;

	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
					 QXL_RELEASE_CURSOR_CMD,
					 &release, NULL);
	if (ret)
		return ret;

	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
	if (ret)
		goto out_free_release;

	ret = qxl_release_reserve_list(release, false);
	if (ret)
		goto out_free_release;

	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
	cmd->type = QXL_CURSOR_SET;
	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;

	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);

	cmd->u.set.visible = 1;
	qxl_release_unmap(qdev, release, &cmd->release_info);

	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
	qxl_release_fence_buffer_objects(release);

	return ret;

out_free_release:
	qxl_release_free(qdev, release);
	return ret;
}

527 528 529 530
static void qxl_primary_atomic_update(struct drm_plane *plane,
				      struct drm_plane_state *old_state)
{
	struct qxl_device *qdev = plane->dev->dev_private;
531
	struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
G
Gerd Hoffmann 已提交
532
	struct qxl_bo *bo_old, *primary;
533 534 535
	struct drm_clip_rect norect = {
	    .x1 = 0,
	    .y1 = 0,
536 537
	    .x2 = plane->state->fb->width,
	    .y2 = plane->state->fb->height
538
	};
539
	uint32_t dumb_shadow_offset = 0;
540

541
	if (old_state->fb) {
542
		bo_old = gem_to_qxl_bo(old_state->fb->obj[0]);
543 544 545
	} else {
		bo_old = NULL;
	}
546

G
Gerd Hoffmann 已提交
547
	primary = bo->shadow ? bo->shadow : bo;
548

G
Gerd Hoffmann 已提交
549 550
	if (!primary->is_primary) {
		if (qdev->primary_bo)
551
			qxl_io_destroy_primary(qdev);
G
Gerd Hoffmann 已提交
552 553
		qxl_io_create_primary(qdev, primary);
		qxl_primary_apply_cursor(plane);
554
	}
555

556 557 558 559 560 561
	if (bo->is_dumb)
		dumb_shadow_offset =
			qdev->dumb_heads[plane->state->crtc->index].x;

	qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
			  dumb_shadow_offset);
562 563 564 565 566 567 568
}

static void qxl_primary_atomic_disable(struct drm_plane *plane,
				       struct drm_plane_state *old_state)
{
	struct qxl_device *qdev = plane->dev->dev_private;

569
	if (old_state->fb) {
570
		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
571

572 573 574 575
		if (bo->is_primary) {
			qxl_io_destroy_primary(qdev);
			bo->is_primary = false;
		}
576 577 578
	}
}

579 580 581 582 583 584
static void qxl_cursor_atomic_update(struct drm_plane *plane,
				     struct drm_plane_state *old_state)
{
	struct drm_device *dev = plane->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct drm_framebuffer *fb = plane->state->fb;
585
	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
586 587 588 589
	struct qxl_release *release;
	struct qxl_cursor_cmd *cmd;
	struct qxl_cursor *cursor;
	struct drm_gem_object *obj;
590
	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
591 592 593 594 595 596 597
	int ret;
	void *user_ptr;
	int size = 64*64*4;

	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
					 QXL_RELEASE_CURSOR_CMD,
					 &release, NULL);
598 599
	if (ret)
		return;
600 601

	if (fb != old_state->fb) {
602
		obj = fb->obj[0];
603 604 605 606 607 608 609 610 611 612 613 614 615
		user_bo = gem_to_qxl_bo(obj);

		/* pinning is done in the prepare/cleanup framevbuffer */
		ret = qxl_bo_kmap(user_bo, &user_ptr);
		if (ret)
			goto out_free_release;

		ret = qxl_alloc_bo_reserved(qdev, release,
					    sizeof(struct qxl_cursor) + size,
					    &cursor_bo);
		if (ret)
			goto out_kunmap;

616
		ret = qxl_bo_pin(cursor_bo);
617 618 619
		if (ret)
			goto out_free_bo;

620 621 622 623
		ret = qxl_release_reserve_list(release, true);
		if (ret)
			goto out_unpin;

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
		if (ret)
			goto out_backoff;

		cursor->header.unique = 0;
		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
		cursor->header.width = 64;
		cursor->header.height = 64;
		cursor->header.hot_spot_x = fb->hot_x;
		cursor->header.hot_spot_y = fb->hot_y;
		cursor->data_size = size;
		cursor->chunk.next_chunk = 0;
		cursor->chunk.prev_chunk = 0;
		cursor->chunk.data_size = size;
		memcpy(cursor->chunk.data, user_ptr, size);
		qxl_bo_kunmap(cursor_bo);
		qxl_bo_kunmap(user_bo);

642
		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
643 644 645 646
		cmd->u.set.visible = 1;
		cmd->u.set.shape = qxl_bo_physical_address(qdev,
							   cursor_bo, 0);
		cmd->type = QXL_CURSOR_SET;
647

648
		old_cursor_bo = qcrtc->cursor_bo;
649 650
		qcrtc->cursor_bo = cursor_bo;
		cursor_bo = NULL;
651 652 653 654 655 656
	} else {

		ret = qxl_release_reserve_list(release, true);
		if (ret)
			goto out_free_release;

657
		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
658 659 660 661 662 663 664 665 666 667
		cmd->type = QXL_CURSOR_MOVE;
	}

	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;

	qxl_release_unmap(qdev, release, &cmd->release_info);
	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
	qxl_release_fence_buffer_objects(release);

668 669
	if (old_cursor_bo != NULL)
		qxl_bo_unpin(old_cursor_bo);
670
	qxl_bo_unref(&old_cursor_bo);
671 672
	qxl_bo_unref(&cursor_bo);

673 674 675 676
	return;

out_backoff:
	qxl_release_backoff_reserve_list(release);
677 678
out_unpin:
	qxl_bo_unpin(cursor_bo);
679 680 681 682 683 684 685 686 687 688
out_free_bo:
	qxl_bo_unref(&cursor_bo);
out_kunmap:
	qxl_bo_kunmap(user_bo);
out_free_release:
	qxl_release_free(qdev, release);
	return;

}

689 690
static void qxl_cursor_atomic_disable(struct drm_plane *plane,
				      struct drm_plane_state *old_state)
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
{
	struct qxl_device *qdev = plane->dev->dev_private;
	struct qxl_release *release;
	struct qxl_cursor_cmd *cmd;
	int ret;

	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
					 QXL_RELEASE_CURSOR_CMD,
					 &release, NULL);
	if (ret)
		return;

	ret = qxl_release_reserve_list(release, true);
	if (ret) {
		qxl_release_free(qdev, release);
		return;
	}

	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
	cmd->type = QXL_CURSOR_HIDE;
	qxl_release_unmap(qdev, release, &cmd->release_info);

	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
	qxl_release_fence_buffer_objects(release);
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
static void qxl_update_dumb_head(struct qxl_device *qdev,
				 int index, struct qxl_bo *bo)
{
	uint32_t width, height;

	if (index >= qdev->monitors_config->max_allowed)
		return;

	if (bo && bo->is_dumb) {
		width = bo->surf.width;
		height = bo->surf.height;
	} else {
		width = 0;
		height = 0;
	}

	if (qdev->dumb_heads[index].width == width &&
	    qdev->dumb_heads[index].height == height)
		return;

	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
		  qdev->dumb_heads[index].width,
		  qdev->dumb_heads[index].height,
		  width, height);
	qdev->dumb_heads[index].width = width;
	qdev->dumb_heads[index].height = height;
}

static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
				 struct qxl_surface *surf)
{
	struct qxl_head *head;
	int i;

	memset(surf, 0, sizeof(*surf));
	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
		head = qdev->dumb_heads + i;
		head->x = surf->width;
		surf->width += head->width;
		if (surf->height < head->height)
			surf->height = head->height;
	}
	if (surf->width < 64)
		surf->width = 64;
	if (surf->height < 64)
		surf->height = 64;
	surf->format = SPICE_SURFACE_FMT_32_xRGB;
	surf->stride = surf->width * 4;

	if (!qdev->dumb_shadow_bo ||
	    qdev->dumb_shadow_bo->surf.width != surf->width ||
	    qdev->dumb_shadow_bo->surf.height != surf->height)
		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
}

772 773
static int qxl_plane_prepare_fb(struct drm_plane *plane,
				struct drm_plane_state *new_state)
774
{
775
	struct qxl_device *qdev = plane->dev->dev_private;
776
	struct drm_gem_object *obj;
777 778
	struct qxl_bo *user_bo;
	struct qxl_surface surf;
779 780 781 782 783
	int ret;

	if (!new_state->fb)
		return 0;

784
	obj = new_state->fb->obj[0];
785 786
	user_bo = gem_to_qxl_bo(obj);

787
	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	    user_bo->is_dumb) {
		qxl_update_dumb_head(qdev, new_state->crtc->index,
				     user_bo);
		qxl_calc_dumb_shadow(qdev, &surf);
		if (!qdev->dumb_shadow_bo ||
		    qdev->dumb_shadow_bo->surf.width  != surf.width ||
		    qdev->dumb_shadow_bo->surf.height != surf.height) {
			if (qdev->dumb_shadow_bo) {
				drm_gem_object_put_unlocked
					(&qdev->dumb_shadow_bo->gem_base);
				qdev->dumb_shadow_bo = NULL;
			}
			qxl_bo_create(qdev, surf.height * surf.stride,
				      true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
				      &qdev->dumb_shadow_bo);
803
		}
804 805 806 807 808 809 810 811
		if (user_bo->shadow != qdev->dumb_shadow_bo) {
			if (user_bo->shadow) {
				drm_gem_object_put_unlocked
					(&user_bo->shadow->gem_base);
				user_bo->shadow = NULL;
			}
			drm_gem_object_get(&qdev->dumb_shadow_bo->gem_base);
			user_bo->shadow = qdev->dumb_shadow_bo;
812 813 814
		}
	}

815
	ret = qxl_bo_pin(user_bo);
816 817 818 819 820 821 822 823 824 825 826 827
	if (ret)
		return ret;

	return 0;
}

static void qxl_plane_cleanup_fb(struct drm_plane *plane,
				 struct drm_plane_state *old_state)
{
	struct drm_gem_object *obj;
	struct qxl_bo *user_bo;

G
Gerd Hoffmann 已提交
828 829 830
	if (!old_state->fb) {
		/*
		 * we never executed prepare_fb, so there's nothing to
831 832 833 834 835
		 * unpin.
		 */
		return;
	}

836
	obj = old_state->fb->obj[0];
837 838
	user_bo = gem_to_qxl_bo(obj);
	qxl_bo_unpin(user_bo);
839

840
	if (old_state->fb != plane->state->fb && user_bo->shadow) {
841 842 843
		drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
		user_bo->shadow = NULL;
	}
844 845 846 847 848 849 850 851 852 853 854 855 856 857
}

static const uint32_t qxl_cursor_plane_formats[] = {
	DRM_FORMAT_ARGB8888,
};

static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
	.atomic_update = qxl_cursor_atomic_update,
	.atomic_disable = qxl_cursor_atomic_disable,
	.prepare_fb = qxl_plane_prepare_fb,
	.cleanup_fb = qxl_plane_cleanup_fb,
};

static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
858 859
	.update_plane	= drm_atomic_helper_update_plane,
	.disable_plane	= drm_atomic_helper_disable_plane,
860
	.destroy	= drm_primary_helper_destroy,
861 862 863
	.reset		= drm_atomic_helper_plane_reset,
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
864 865
};

866 867 868 869 870
static const uint32_t qxl_primary_plane_formats[] = {
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_ARGB8888,
};

871 872 873 874 875 876 877 878
static const struct drm_plane_helper_funcs primary_helper_funcs = {
	.atomic_check = qxl_primary_atomic_check,
	.atomic_update = qxl_primary_atomic_update,
	.atomic_disable = qxl_primary_atomic_disable,
	.prepare_fb = qxl_plane_prepare_fb,
	.cleanup_fb = qxl_plane_cleanup_fb,
};

879
static const struct drm_plane_funcs qxl_primary_plane_funcs = {
880 881
	.update_plane	= drm_atomic_helper_update_plane,
	.disable_plane	= drm_atomic_helper_disable_plane,
882
	.destroy	= drm_primary_helper_destroy,
883 884 885
	.reset		= drm_atomic_helper_plane_reset,
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
};

static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
					  unsigned int possible_crtcs,
					  enum drm_plane_type type)
{
	const struct drm_plane_helper_funcs *helper_funcs = NULL;
	struct drm_plane *plane;
	const struct drm_plane_funcs *funcs;
	const uint32_t *formats;
	int num_formats;
	int err;

	if (type == DRM_PLANE_TYPE_PRIMARY) {
		funcs = &qxl_primary_plane_funcs;
		formats = qxl_primary_plane_formats;
		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
903
		helper_funcs = &primary_helper_funcs;
904 905 906 907 908
	} else if (type == DRM_PLANE_TYPE_CURSOR) {
		funcs = &qxl_cursor_plane_funcs;
		formats = qxl_cursor_plane_formats;
		helper_funcs = &qxl_cursor_helper_funcs;
		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
909 910 911 912 913 914 915 916 917 918
	} else {
		return ERR_PTR(-EINVAL);
	}

	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
	if (!plane)
		return ERR_PTR(-ENOMEM);

	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
				       funcs, formats, num_formats,
919
				       NULL, type, NULL);
920 921 922 923 924 925 926 927 928 929 930 931
	if (err)
		goto free_plane;

	drm_plane_helper_add(plane, helper_funcs);

	return plane;

free_plane:
	kfree(plane);
	return ERR_PTR(-EINVAL);
}

932
static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
D
Dave Airlie 已提交
933 934
{
	struct qxl_crtc *qxl_crtc;
935
	struct drm_plane *primary, *cursor;
936 937
	struct qxl_device *qdev = dev->dev_private;
	int r;
D
Dave Airlie 已提交
938 939 940 941 942

	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
	if (!qxl_crtc)
		return -ENOMEM;

943 944 945 946 947 948
	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
	if (IS_ERR(primary)) {
		r = -ENOMEM;
		goto free_mem;
	}

949 950 951 952 953 954 955
	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
	if (IS_ERR(cursor)) {
		r = -ENOMEM;
		goto clean_primary;
	}

	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
956 957
				      &qxl_crtc_funcs, NULL);
	if (r)
958
		goto clean_cursor;
959

960
	qxl_crtc->index = crtc_id;
D
Dave Airlie 已提交
961 962
	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
	return 0;
963

964 965 966
clean_cursor:
	drm_plane_cleanup(cursor);
	kfree(cursor);
967 968 969 970 971 972
clean_primary:
	drm_plane_cleanup(primary);
	kfree(primary);
free_mem:
	kfree(qxl_crtc);
	return r;
D
Dave Airlie 已提交
973 974 975 976
}

static int qxl_conn_get_modes(struct drm_connector *connector)
{
977 978
	unsigned int pwidth = 1024;
	unsigned int pheight = 768;
G
Gerd Hoffmann 已提交
979
	int ret = 0;
D
Dave Airlie 已提交
980

G
Gerd Hoffmann 已提交
981 982 983
	ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
	if (ret < 0)
		return ret;
984
	ret += qxl_add_common_modes(connector, pwidth, pheight);
D
Dave Airlie 已提交
985 986 987
	return ret;
}

988
static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
D
Dave Airlie 已提交
989 990
			       struct drm_display_mode *mode)
{
991 992 993 994
	struct drm_device *ddev = connector->dev;
	struct qxl_device *qdev = ddev->dev_private;
	int i;

D
Dave Airlie 已提交
995 996
	/* TODO: is this called for user defined modes? (xrandr --add-mode)
	 * TODO: check that the mode fits in the framebuffer */
997

998 999
	if (qdev->monitors_config_width == mode->hdisplay &&
	    qdev->monitors_config_height == mode->vdisplay)
1000 1001 1002 1003 1004 1005 1006
		return MODE_OK;

	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
		if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
			return MODE_OK;
	}
	return MODE_BAD;
D
Dave Airlie 已提交
1007 1008
}

1009
static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
D
Dave Airlie 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
{
	struct qxl_output *qxl_output =
		drm_connector_to_qxl_output(connector);

	DRM_DEBUG("\n");
	return &qxl_output->enc;
}

static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
};

static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
	.get_modes = qxl_conn_get_modes,
	.mode_valid = qxl_conn_mode_valid,
	.best_encoder = qxl_best_encoder,
};

static enum drm_connector_status qxl_conn_detect(
			struct drm_connector *connector,
			bool force)
{
	struct qxl_output *output =
		drm_connector_to_qxl_output(connector);
	struct drm_device *ddev = connector->dev;
	struct qxl_device *qdev = ddev->dev_private;
1035
	bool connected = false;
D
Dave Airlie 已提交
1036 1037

	/* The first monitor is always connected */
1038 1039 1040 1041 1042 1043
	if (!qdev->client_monitors_config) {
		if (output->index == 0)
			connected = true;
	} else
		connected = qdev->client_monitors_config->count > output->index &&
		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
D
Dave Airlie 已提交
1044

1045 1046
	DRM_DEBUG("#%d connected: %d\n", output->index, connected);

D
Dave Airlie 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055
	return connected ? connector_status_connected
			 : connector_status_disconnected;
}

static void qxl_conn_destroy(struct drm_connector *connector)
{
	struct qxl_output *qxl_output =
		drm_connector_to_qxl_output(connector);

1056
	drm_connector_unregister(connector);
D
Dave Airlie 已提交
1057 1058 1059 1060 1061 1062
	drm_connector_cleanup(connector);
	kfree(qxl_output);
}

static const struct drm_connector_funcs qxl_connector_funcs = {
	.detect = qxl_conn_detect,
1063
	.fill_modes = drm_helper_probe_single_connector_modes,
D
Dave Airlie 已提交
1064
	.destroy = qxl_conn_destroy,
1065 1066 1067
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
D
Dave Airlie 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
};

static void qxl_enc_destroy(struct drm_encoder *encoder)
{
	drm_encoder_cleanup(encoder);
}

static const struct drm_encoder_funcs qxl_enc_funcs = {
	.destroy = qxl_enc_destroy,
};

1079 1080 1081 1082 1083 1084
static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
{
	if (qdev->hotplug_mode_update_property)
		return 0;

	qdev->hotplug_mode_update_property =
1085
		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1086 1087 1088 1089 1090
					  "hotplug_mode_update", 0, 1);

	return 0;
}

1091
static int qdev_output_init(struct drm_device *dev, int num_output)
D
Dave Airlie 已提交
1092
{
1093
	struct qxl_device *qdev = dev->dev_private;
D
Dave Airlie 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	struct qxl_output *qxl_output;
	struct drm_connector *connector;
	struct drm_encoder *encoder;

	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
	if (!qxl_output)
		return -ENOMEM;

	qxl_output->index = num_output;

	connector = &qxl_output->base;
	encoder = &qxl_output->enc;
	drm_connector_init(dev, &qxl_output->base,
			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);

	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1110
			 DRM_MODE_ENCODER_VIRTUAL, NULL);
D
Dave Airlie 已提交
1111

1112 1113
	/* we get HPD via client monitors config */
	connector->polled = DRM_CONNECTOR_POLL_HPD;
D
Dave Airlie 已提交
1114
	encoder->possible_crtcs = 1 << num_output;
1115
	drm_connector_attach_encoder(&qxl_output->base,
D
Dave Airlie 已提交
1116 1117 1118 1119
					  &qxl_output->enc);
	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);

1120 1121
	drm_object_attach_property(&connector->base,
				   qdev->hotplug_mode_update_property, 0);
1122 1123 1124 1125
	drm_object_attach_property(&connector->base,
				   dev->mode_config.suggested_x_property, 0);
	drm_object_attach_property(&connector->base,
				   dev->mode_config.suggested_y_property, 0);
D
Dave Airlie 已提交
1126 1127 1128 1129 1130 1131
	return 0;
}

static struct drm_framebuffer *
qxl_user_framebuffer_create(struct drm_device *dev,
			    struct drm_file *file_priv,
1132
			    const struct drm_mode_fb_cmd2 *mode_cmd)
D
Dave Airlie 已提交
1133
{
1134 1135
	return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
					    &qxl_fb_funcs);
D
Dave Airlie 已提交
1136 1137 1138 1139
}

static const struct drm_mode_config_funcs qxl_mode_funcs = {
	.fb_create = qxl_user_framebuffer_create,
1140 1141
	.atomic_check = drm_atomic_helper_check,
	.atomic_commit = drm_atomic_helper_commit,
D
Dave Airlie 已提交
1142 1143
};

1144
int qxl_create_monitors_object(struct qxl_device *qdev)
D
Dave Airlie 已提交
1145 1146 1147 1148
{
	int ret;
	struct drm_gem_object *gobj;
	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1149
		qxl_num_crtc * sizeof(struct qxl_head);
D
Dave Airlie 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158

	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
				    QXL_GEM_DOMAIN_VRAM,
				    false, false, NULL, &gobj);
	if (ret) {
		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
		return -ENOMEM;
	}
	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1159

1160
	ret = qxl_bo_pin(qdev->monitors_config_bo);
1161
	if (ret)
1162 1163
		return ret;

D
Dave Airlie 已提交
1164
	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1165

D
Dave Airlie 已提交
1166 1167 1168 1169 1170
	qdev->monitors_config = qdev->monitors_config_bo->kptr;
	qdev->ram_header->monitors_config =
		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);

	memset(qdev->monitors_config, 0, monitors_config_size);
1171 1172
	qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
				   GFP_KERNEL);
1173 1174 1175 1176
	if (!qdev->dumb_heads) {
		qxl_destroy_monitors_object(qdev);
		return -ENOMEM;
	}
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	return 0;
}

int qxl_destroy_monitors_object(struct qxl_device *qdev)
{
	int ret;

	qdev->monitors_config = NULL;
	qdev->ram_header->monitors_config = 0;

	qxl_bo_kunmap(qdev->monitors_config_bo);
1188
	ret = qxl_bo_unpin(qdev->monitors_config_bo);
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	if (ret)
		return ret;

	qxl_bo_unref(&qdev->monitors_config_bo);
	return 0;
}

int qxl_modeset_init(struct qxl_device *qdev)
{
	int i;
	int ret;

1201
	drm_mode_config_init(&qdev->ddev);
1202 1203 1204 1205

	ret = qxl_create_monitors_object(qdev);
	if (ret)
		return ret;
D
Dave Airlie 已提交
1206

1207
	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
D
Dave Airlie 已提交
1208 1209

	/* modes will be validated against the framebuffer size */
1210 1211
	qdev->ddev.mode_config.min_width = 0;
	qdev->ddev.mode_config.min_height = 0;
1212 1213
	qdev->ddev.mode_config.max_width = 8192;
	qdev->ddev.mode_config.max_height = 8192;
D
Dave Airlie 已提交
1214

1215
	qdev->ddev.mode_config.fb_base = qdev->vram_base;
1216

1217
	drm_mode_create_suggested_offset_properties(&qdev->ddev);
1218 1219
	qxl_mode_create_hotplug_mode_update_property(qdev);

1220
	for (i = 0 ; i < qxl_num_crtc; ++i) {
1221 1222
		qdev_crtc_init(&qdev->ddev, i);
		qdev_output_init(&qdev->ddev, i);
D
Dave Airlie 已提交
1223 1224
	}

1225
	qxl_display_read_client_monitors_config(qdev);
D
Dave Airlie 已提交
1226

1227
	drm_mode_config_reset(&qdev->ddev);
D
Dave Airlie 已提交
1228 1229 1230 1231 1232
	return 0;
}

void qxl_modeset_fini(struct qxl_device *qdev)
{
1233
	qxl_destroy_monitors_object(qdev);
1234
	drm_mode_config_cleanup(&qdev->ddev);
D
Dave Airlie 已提交
1235
}