qxl_display.c 32.7 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 26
/*
 * 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 已提交
27
#include <linux/crc32.h>
D
Dave Airlie 已提交
28 29 30 31

#include "qxl_drv.h"
#include "qxl_object.h"
#include "drm_crtc_helper.h"
32
#include <drm/drm_plane_helper.h>
33
#include <drm/drm_atomic_helper.h>
D
Dave Airlie 已提交
34

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

40
static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
D
Dave Airlie 已提交
41 42 43 44
{
	if (qdev->client_monitors_config &&
	    count > qdev->client_monitors_config->count) {
		kfree(qdev->client_monitors_config);
D
Dave Airlie 已提交
45
		qdev->client_monitors_config = NULL;
D
Dave Airlie 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	}
	if (!qdev->client_monitors_config) {
		qdev->client_monitors_config = kzalloc(
				sizeof(struct qxl_monitors_config) +
				sizeof(struct qxl_head) * count, GFP_KERNEL);
		if (!qdev->client_monitors_config) {
			qxl_io_log(qdev,
				   "%s: allocation failure for %u heads\n",
				   __func__, count);
			return;
		}
	}
	qdev->client_monitors_config->count = count;
}

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

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 77

	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));
	if (crc != qdev->rom->client_monitors_config_crc) {
78
		qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
D
Dave Airlie 已提交
79 80
			   sizeof(qdev->rom->client_monitors_config),
			   qdev->rom->client_monitors_config_crc);
81
		return MONITORS_CONFIG_BAD_CRC;
D
Dave Airlie 已提交
82 83
	}
	if (num_monitors > qdev->monitors_config->max_allowed) {
84 85
		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
			      qdev->monitors_config->max_allowed, num_monitors);
D
Dave Airlie 已提交
86 87 88 89
		num_monitors = qdev->monitors_config->max_allowed;
	} 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;
	}
D
Dave Airlie 已提交
94 95 96 97 98 99 100 101 102
	qxl_alloc_client_monitors_config(qdev, num_monitors);
	/* we copy max from the client but it isn't used */
	qdev->client_monitors_config->max_allowed =
				qdev->monitors_config->max_allowed;
	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];
103 104 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
		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;
		}
131 132
		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
			  client_head->x, client_head->y);
D
Dave Airlie 已提交
133
	}
134 135

	return status;
D
Dave Airlie 已提交
136 137
}

138 139
static void qxl_update_offset_props(struct qxl_device *qdev)
{
140
	struct drm_device *dev = &qdev->ddev;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	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 已提交
157 158 159
void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
{

160
	struct drm_device *dev = &qdev->ddev;
161 162 163 164
	int status;

	status = qxl_display_copy_rom_client_monitors_config(qdev);
	while (status == MONITORS_CONFIG_BAD_CRC) {
D
Dave Airlie 已提交
165 166
		qxl_io_log(qdev, "failed crc check for client_monitors_config,"
				 " retrying\n");
167 168 169 170 171 172
		status = qxl_display_copy_rom_client_monitors_config(qdev);
	}
	if (status == MONITORS_CONFIG_UNCHANGED) {
		qxl_io_log(qdev, "config unchanged\n");
		DRM_DEBUG("ignoring unchanged client monitors config");
		return;
D
Dave Airlie 已提交
173
	}
174

175 176 177
	drm_modeset_lock_all(dev);
	qxl_update_offset_props(qdev);
	drm_modeset_unlock_all(dev);
178
	if (!drm_helper_hpd_irq_event(dev)) {
179 180
		/* notify that the monitor configuration changed, to
		   adjust at the arbitrary resolution */
181
		drm_kms_helper_hotplug_event(dev);
182
	}
D
Dave Airlie 已提交
183 184
}

185 186 187
static int qxl_add_monitors_config_modes(struct drm_connector *connector,
                                         unsigned *pwidth,
                                         unsigned *pheight)
D
Dave Airlie 已提交
188 189 190 191 192 193 194 195
{
	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;

196
	if (!qdev->client_monitors_config)
D
Dave Airlie 已提交
197
		return 0;
198
	head = &qdev->client_monitors_config->heads[h];
D
Dave Airlie 已提交
199 200 201 202

	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
			    false);
	mode->type |= DRM_MODE_TYPE_PREFERRED;
203 204 205
	mode->hdisplay = head->width;
	mode->vdisplay = head->height;
	drm_mode_set_name(mode);
206 207
	*pwidth = head->width;
	*pheight = head->height;
D
Dave Airlie 已提交
208
	drm_mode_probed_add(connector, mode);
209 210 211
	/* remember the last custom size for mode validation */
	qdev->monitors_config_width = mode->hdisplay;
	qdev->monitors_config_height = mode->vdisplay;
D
Dave Airlie 已提交
212 213 214
	return 1;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
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}
};

238 239 240
static int qxl_add_common_modes(struct drm_connector *connector,
                                unsigned pwidth,
                                unsigned pheight)
D
Dave Airlie 已提交
241 242 243 244 245 246 247
{
	struct drm_device *dev = connector->dev;
	struct drm_display_mode *mode = NULL;
	int i;
	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);
248
		if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
D
Dave Airlie 已提交
249 250 251 252 253 254
			mode->type |= DRM_MODE_TYPE_PREFERRED;
		drm_mode_probed_add(connector, mode);
	}
	return i - 1;
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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);
	}
}

D
Dave Airlie 已提交
272 273 274 275 276 277 278 279
static void qxl_crtc_destroy(struct drm_crtc *crtc)
{
	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);

	drm_crtc_cleanup(crtc);
	kfree(qxl_crtc);
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static int qxl_crtc_page_flip(struct drm_crtc *crtc,
                              struct drm_framebuffer *fb,
                              struct drm_pending_vblank_event *event,
                              uint32_t page_flip_flags)
{
	struct drm_device *dev = crtc->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
	struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
	struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
	struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
	unsigned long flags;
	struct drm_clip_rect norect = {
	    .x1 = 0,
	    .y1 = 0,
	    .x2 = fb->width,
	    .y2 = fb->height
	};
	int inc = 1;
	int one_clip_rect = 1;
	int ret = 0;

	crtc->primary->fb = fb;
	bo_old->is_primary = false;
	bo->is_primary = true;

306
	ret = qxl_bo_pin(bo, bo->type, NULL);
307 308 309 310 311 312 313 314
	if (ret)
		return ret;

	qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
			  &norect, one_clip_rect, inc);

	if (event) {
		spin_lock_irqsave(&dev->event_lock, flags);
315
		drm_crtc_send_vblank_event(crtc, event);
316 317 318
		spin_unlock_irqrestore(&dev->event_lock, flags);
	}

319
	qxl_bo_unpin(bo);
320 321 322 323

	return 0;
}

D
Dave Airlie 已提交
324 325 326
static const struct drm_crtc_funcs qxl_crtc_funcs = {
	.set_config = drm_crtc_helper_set_config,
	.destroy = qxl_crtc_destroy,
327
	.page_flip = qxl_crtc_page_flip,
D
Dave Airlie 已提交
328 329
};

330
void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
D
Dave Airlie 已提交
331 332 333
{
	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);

334
	drm_gem_object_unreference_unlocked(qxl_fb->obj);
D
Dave Airlie 已提交
335 336 337 338
	drm_framebuffer_cleanup(fb);
	kfree(qxl_fb);
}

339 340 341 342 343
static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
					 struct drm_file *file_priv,
					 unsigned flags, unsigned color,
					 struct drm_clip_rect *clips,
					 unsigned num_clips)
D
Dave Airlie 已提交
344 345 346 347 348 349 350 351
{
	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
	struct drm_clip_rect norect;
	struct qxl_bo *qobj;
	int inc = 1;

352 353
	drm_modeset_lock_all(fb->dev);

D
Dave Airlie 已提交
354
	qobj = gem_to_qxl_bo(qxl_fb->obj);
355
	/* if we aren't primary surface ignore this */
356 357
	if (!qobj->is_primary) {
		drm_modeset_unlock_all(fb->dev);
358
		return 0;
359
	}
360

D
Dave Airlie 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373
	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 */
	}

	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
			  clips, num_clips, inc);
374 375 376

	drm_modeset_unlock_all(fb->dev);

D
Dave Airlie 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389
	return 0;
}

static const struct drm_framebuffer_funcs qxl_fb_funcs = {
	.destroy = qxl_user_framebuffer_destroy,
	.dirty = qxl_framebuffer_surface_dirty,
/*	TODO?
 *	.create_handle = qxl_user_framebuffer_create_handle, */
};

int
qxl_framebuffer_init(struct drm_device *dev,
		     struct qxl_framebuffer *qfb,
390
		     const struct drm_mode_fb_cmd2 *mode_cmd,
391 392
		     struct drm_gem_object *obj,
		     const struct drm_framebuffer_funcs *funcs)
D
Dave Airlie 已提交
393 394 395 396
{
	int ret;

	qfb->obj = obj;
397
	drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
398
	ret = drm_framebuffer_init(dev, &qfb->base, funcs);
D
Dave Airlie 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	if (ret) {
		qfb->obj = NULL;
		return ret;
	}
	return 0;
}

static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
{
}

static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
				  const struct drm_display_mode *mode,
				  struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = crtc->dev;
	struct qxl_device *qdev = dev->dev_private;

	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
		   __func__,
		   mode->hdisplay, mode->vdisplay,
		   adjusted_mode->hdisplay,
		   adjusted_mode->vdisplay);
	return true;
}

425
static void
D
Dave Airlie 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438
qxl_send_monitors_config(struct qxl_device *qdev)
{
	int i;

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

	if (qdev->monitors_config->count == 0) {
		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
		return;
	}
	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
		struct qxl_head *head = &qdev->monitors_config->heads[i];

439
		if (head->y > 8192 || head->x > 8192 ||
D
Dave Airlie 已提交
440 441 442 443 444 445 446 447 448 449
		    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);
}

450 451 452 453 454
static void qxl_monitors_config_set(struct qxl_device *qdev,
				    int index,
				    unsigned x, unsigned y,
				    unsigned width, unsigned height,
				    unsigned surf_id)
D
Dave Airlie 已提交
455
{
456 457 458 459 460 461 462
	DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
	qdev->monitors_config->heads[index].x = x;
	qdev->monitors_config->heads[index].y = y;
	qdev->monitors_config->heads[index].width = width;
	qdev->monitors_config->heads[index].height = height;
	qdev->monitors_config->heads[index].surface_id = surf_id;

D
Dave Airlie 已提交
463 464 465 466 467 468 469 470 471
}

static void qxl_crtc_prepare(struct drm_crtc *crtc)
{
	DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
		  crtc->mode.hdisplay, crtc->mode.vdisplay,
		  crtc->x, crtc->y, crtc->enabled);
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485
void qxl_mode_set_nofb(struct drm_crtc *crtc)
{
	struct qxl_device *qdev = crtc->dev->dev_private;
	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
	struct drm_display_mode *mode = &crtc->mode;

	DRM_DEBUG("Mode set (%d,%d)\n",
		  mode->hdisplay, mode->vdisplay);

	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
				mode->hdisplay,	mode->vdisplay, 0);

}

D
Dave Airlie 已提交
486 487 488 489 490
static void qxl_crtc_commit(struct drm_crtc *crtc)
{
	DRM_DEBUG("\n");
}

491 492 493
static void qxl_crtc_disable(struct drm_crtc *crtc)
{
	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
494
	struct qxl_device *qdev = crtc->dev->dev_private;
495 496 497 498 499 500

	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);

	qxl_send_monitors_config(qdev);
}

D
Dave Airlie 已提交
501 502
static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
	.dpms = qxl_crtc_dpms,
503
	.disable = qxl_crtc_disable,
D
Dave Airlie 已提交
504
	.mode_fixup = qxl_crtc_mode_fixup,
505 506
	.mode_set = drm_helper_crtc_mode_set,
	.mode_set_nofb = qxl_mode_set_nofb,
D
Dave Airlie 已提交
507 508
	.prepare = qxl_crtc_prepare,
	.commit = qxl_crtc_commit,
509
	.atomic_flush = qxl_crtc_atomic_flush,
D
Dave Airlie 已提交
510 511
};

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
int qxl_primary_atomic_check(struct drm_plane *plane,
			     struct drm_plane_state *state)
{
	struct qxl_device *qdev = plane->dev->dev_private;
	struct qxl_framebuffer *qfb;
	struct qxl_bo *bo;

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

	qfb = to_qxl_framebuffer(state->fb);
	bo = gem_to_qxl_bo(qfb->obj);

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

	return 0;
}

static void qxl_primary_atomic_update(struct drm_plane *plane,
				      struct drm_plane_state *old_state)
{
	struct qxl_device *qdev = plane->dev->dev_private;
	struct qxl_framebuffer *qfb =
		to_qxl_framebuffer(plane->state->fb);
	struct qxl_framebuffer *qfb_old;
	struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
	struct qxl_bo *bo_old;
	struct drm_clip_rect norect = {
	    .x1 = 0,
	    .y1 = 0,
	    .x2 = qfb->base.width,
	    .y2 = qfb->base.height
	};

	if (!old_state->fb) {
		qxl_io_log(qdev,
			   "create primary fb: %dx%d,%d,%d\n",
			   bo->surf.width, bo->surf.height,
			   bo->surf.stride, bo->surf.format);

		qxl_io_create_primary(qdev, 0, bo);
		bo->is_primary = true;
		return;

	} else {
		qfb_old = to_qxl_framebuffer(old_state->fb);
		bo_old = gem_to_qxl_bo(qfb_old->obj);
		bo_old->is_primary = false;
	}

	bo->is_primary = true;
	qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
}

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

	if (old_state->fb)
	{	struct qxl_framebuffer *qfb =
			to_qxl_framebuffer(old_state->fb);
		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);

		qxl_io_destroy_primary(qdev);
		bo->is_primary = false;
	}
}

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 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 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
int qxl_plane_atomic_check(struct drm_plane *plane,
			   struct drm_plane_state *state)
{
	return 0;
}

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;
	struct qxl_release *release;
	struct qxl_cursor_cmd *cmd;
	struct qxl_cursor *cursor;
	struct drm_gem_object *obj;
	struct qxl_bo *cursor_bo, *user_bo = NULL;
	int ret;
	void *user_ptr;
	int size = 64*64*4;

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

	cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);

	if (fb != old_state->fb) {
		obj = to_qxl_framebuffer(fb)->obj;
		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;

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

		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);

		cmd->u.set.visible = 1;
		cmd->u.set.shape = qxl_bo_physical_address(qdev,
							   cursor_bo, 0);
		cmd->type = QXL_CURSOR_SET;
	} else {

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

		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);

	return;

out_backoff:
	qxl_release_backoff_reserve_list(release);
out_free_bo:
	qxl_bo_unref(&cursor_bo);
out_kunmap:
	qxl_bo_kunmap(user_bo);
out_free_release:
	qxl_release_free(qdev, release);
	return;

}

void qxl_cursor_atomic_disable(struct drm_plane *plane,
			       struct drm_plane_state *old_state)
{
	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);
}

int qxl_plane_prepare_fb(struct drm_plane *plane,
			 struct drm_plane_state *new_state)
{
	struct drm_gem_object *obj;
	struct qxl_bo *user_bo;
	int ret;

	if (!new_state->fb)
		return 0;

	obj = to_qxl_framebuffer(new_state->fb)->obj;
	user_bo = gem_to_qxl_bo(obj);

	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
	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;

	if (!plane->state->fb) {
		/* we never executed prepare_fb, so there's nothing to
		 * unpin.
		 */
		return;
	}

	obj = to_qxl_framebuffer(plane->state->fb)->obj;
	user_bo = gem_to_qxl_bo(obj);
	qxl_bo_unpin(user_bo);
}

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

static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
	.atomic_check = qxl_plane_atomic_check,
	.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 = {
	.update_plane	= drm_plane_helper_update,
	.disable_plane	= drm_plane_helper_disable,
	.destroy	= drm_primary_helper_destroy,
};

766 767 768 769 770
static const uint32_t qxl_primary_plane_formats[] = {
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_ARGB8888,
};

771 772 773 774 775 776 777 778
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,
};

779
static const struct drm_plane_funcs qxl_primary_plane_funcs = {
780
	.update_plane	= drm_plane_helper_update,
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	.disable_plane	= drm_primary_helper_disable,
	.destroy	= drm_primary_helper_destroy,
};

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);
800
		helper_funcs = &primary_helper_funcs;
801 802 803 804 805
	} 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);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	} 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,
				       type, NULL);
	if (err)
		goto free_plane;

	drm_plane_helper_add(plane, helper_funcs);

	return plane;

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

829
static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
D
Dave Airlie 已提交
830 831
{
	struct qxl_crtc *qxl_crtc;
832
	struct drm_plane *primary, *cursor;
833 834
	struct qxl_device *qdev = dev->dev_private;
	int r;
D
Dave Airlie 已提交
835 836 837 838 839

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

840 841 842 843 844 845
	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
	if (IS_ERR(primary)) {
		r = -ENOMEM;
		goto free_mem;
	}

846 847 848 849 850 851 852
	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,
853 854
				      &qxl_crtc_funcs, NULL);
	if (r)
855
		goto clean_cursor;
856

857
	qxl_crtc->index = crtc_id;
D
Dave Airlie 已提交
858 859
	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
	return 0;
860

861 862 863
clean_cursor:
	drm_plane_cleanup(cursor);
	kfree(cursor);
864 865 866 867 868 869
clean_primary:
	drm_plane_cleanup(primary);
	kfree(primary);
free_mem:
	kfree(qxl_crtc);
	return r;
D
Dave Airlie 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
}

static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
{
	DRM_DEBUG("\n");
}

static void qxl_enc_prepare(struct drm_encoder *encoder)
{
	DRM_DEBUG("\n");
}

static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
		struct drm_encoder *encoder)
{
	int i;
886
	struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
D
Dave Airlie 已提交
887 888 889 890 891
	struct qxl_head *head;
	struct drm_display_mode *mode;

	BUG_ON(!encoder);
	/* TODO: ugly, do better */
892
	i = output->index;
D
Dave Airlie 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
	if (!qdev->monitors_config ||
	    qdev->monitors_config->max_allowed <= i) {
		DRM_ERROR(
		"head number too large or missing monitors config: %p, %d",
		qdev->monitors_config,
		qdev->monitors_config ?
			qdev->monitors_config->max_allowed : -1);
		return;
	}
	if (!encoder->crtc) {
		DRM_ERROR("missing crtc on encoder %p\n", encoder);
		return;
	}
	if (i != 0)
		DRM_DEBUG("missing for multiple monitors: no head holes\n");
	head = &qdev->monitors_config->heads[i];
	head->id = i;
	if (encoder->crtc->enabled) {
		mode = &encoder->crtc->mode;
		head->width = mode->hdisplay;
		head->height = mode->vdisplay;
		head->x = encoder->crtc->x;
		head->y = encoder->crtc->y;
		if (qdev->monitors_config->count < i + 1)
			qdev->monitors_config->count = i + 1;
	} else {
		head->width = 0;
		head->height = 0;
		head->x = 0;
		head->y = 0;
	}
924 925
	DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
		      i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
D
Dave Airlie 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	head->flags = 0;
	/* TODO - somewhere else to call this for multiple monitors
	 * (config_commit?) */
	qxl_send_monitors_config(qdev);
}

static void qxl_enc_commit(struct drm_encoder *encoder)
{
	struct qxl_device *qdev = encoder->dev->dev_private;

	qxl_write_monitors_config_for_encoder(qdev, encoder);
	DRM_DEBUG("\n");
}

static void qxl_enc_mode_set(struct drm_encoder *encoder,
				struct drm_display_mode *mode,
				struct drm_display_mode *adjusted_mode)
{
	DRM_DEBUG("\n");
}

static int qxl_conn_get_modes(struct drm_connector *connector)
{
	int ret = 0;
	struct qxl_device *qdev = connector->dev->dev_private;
951 952
	unsigned pwidth = 1024;
	unsigned pheight = 768;
D
Dave Airlie 已提交
953 954 955 956 957

	DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
	/* TODO: what should we do here? only show the configured modes for the
	 * device, or allow the full list, or both? */
	if (qdev->monitors_config && qdev->monitors_config->count) {
958
		ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
D
Dave Airlie 已提交
959 960 961
		if (ret < 0)
			return ret;
	}
962
	ret += qxl_add_common_modes(connector, pwidth, pheight);
D
Dave Airlie 已提交
963 964 965 966 967 968
	return ret;
}

static int qxl_conn_mode_valid(struct drm_connector *connector,
			       struct drm_display_mode *mode)
{
969 970 971 972
	struct drm_device *ddev = connector->dev;
	struct qxl_device *qdev = ddev->dev_private;
	int i;

D
Dave Airlie 已提交
973 974
	/* TODO: is this called for user defined modes? (xrandr --add-mode)
	 * TODO: check that the mode fits in the framebuffer */
975 976 977 978 979 980 981 982 983 984

	if(qdev->monitors_config_width == mode->hdisplay &&
	   qdev->monitors_config_height == mode->vdisplay)
		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 已提交
985 986
}

987
static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
D
Dave Airlie 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
{
	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 = {
	.dpms = qxl_enc_dpms,
	.prepare = qxl_enc_prepare,
	.mode_set = qxl_enc_mode_set,
	.commit = qxl_enc_commit,
};

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;
1018
	bool connected = false;
D
Dave Airlie 已提交
1019 1020

	/* The first monitor is always connected */
1021 1022 1023 1024 1025 1026
	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 已提交
1027

1028 1029 1030 1031
	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
	if (!connected)
		qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);

D
Dave Airlie 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	return connected ? connector_status_connected
			 : connector_status_disconnected;
}

static int qxl_conn_set_property(struct drm_connector *connector,
				   struct drm_property *property,
				   uint64_t value)
{
	DRM_DEBUG("\n");
	return 0;
}

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

1049
	drm_connector_unregister(connector);
D
Dave Airlie 已提交
1050 1051 1052 1053 1054 1055 1056
	drm_connector_cleanup(connector);
	kfree(qxl_output);
}

static const struct drm_connector_funcs qxl_connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.detect = qxl_conn_detect,
1057
	.fill_modes = drm_helper_probe_single_connector_modes,
D
Dave Airlie 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	.set_property = qxl_conn_set_property,
	.destroy = qxl_conn_destroy,
};

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,
};

1071 1072 1073 1074 1075 1076
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 =
1077
		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1078 1079 1080 1081 1082
					  "hotplug_mode_update", 0, 1);

	return 0;
}

1083
static int qdev_output_init(struct drm_device *dev, int num_output)
D
Dave Airlie 已提交
1084
{
1085
	struct qxl_device *qdev = dev->dev_private;
D
Dave Airlie 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	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,
1102
			 DRM_MODE_ENCODER_VIRTUAL, NULL);
D
Dave Airlie 已提交
1103

1104 1105
	/* we get HPD via client monitors config */
	connector->polled = DRM_CONNECTOR_POLL_HPD;
D
Dave Airlie 已提交
1106 1107 1108 1109 1110 1111
	encoder->possible_crtcs = 1 << num_output;
	drm_mode_connector_attach_encoder(&qxl_output->base,
					  &qxl_output->enc);
	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);

1112 1113
	drm_object_attach_property(&connector->base,
				   qdev->hotplug_mode_update_property, 0);
1114 1115 1116 1117
	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 已提交
1118 1119 1120 1121 1122 1123
	return 0;
}

static struct drm_framebuffer *
qxl_user_framebuffer_create(struct drm_device *dev,
			    struct drm_file *file_priv,
1124
			    const struct drm_mode_fb_cmd2 *mode_cmd)
D
Dave Airlie 已提交
1125 1126 1127 1128 1129
{
	struct drm_gem_object *obj;
	struct qxl_framebuffer *qxl_fb;
	int ret;

1130 1131 1132
	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
	if (!obj)
		return NULL;
D
Dave Airlie 已提交
1133 1134 1135 1136 1137

	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
	if (qxl_fb == NULL)
		return NULL;

1138
	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
D
Dave Airlie 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	if (ret) {
		kfree(qxl_fb);
		drm_gem_object_unreference_unlocked(obj);
		return NULL;
	}

	return &qxl_fb->base;
}

static const struct drm_mode_config_funcs qxl_mode_funcs = {
	.fb_create = qxl_user_framebuffer_create,
};

1152
int qxl_create_monitors_object(struct qxl_device *qdev)
D
Dave Airlie 已提交
1153 1154 1155
{
	int ret;
	struct drm_gem_object *gobj;
1156
	int max_allowed = qxl_num_crtc;
D
Dave Airlie 已提交
1157
	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1158
		max_allowed * sizeof(struct qxl_head);
D
Dave Airlie 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167

	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);
1168 1169

	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1170
	if (ret)
1171 1172
		return ret;

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

D
Dave Airlie 已提交
1175 1176 1177 1178 1179 1180
	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);
	qdev->monitors_config->max_allowed = max_allowed;
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	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);
1192
	ret = qxl_bo_unpin(qdev->monitors_config_bo);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	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;

1205
	drm_mode_config_init(&qdev->ddev);
1206 1207 1208 1209

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

1211
	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
D
Dave Airlie 已提交
1212 1213

	/* modes will be validated against the framebuffer size */
1214 1215
	qdev->ddev.mode_config.min_width = 0;
	qdev->ddev.mode_config.min_height = 0;
1216 1217
	qdev->ddev.mode_config.max_width = 8192;
	qdev->ddev.mode_config.max_height = 8192;
D
Dave Airlie 已提交
1218

1219
	qdev->ddev.mode_config.fb_base = qdev->vram_base;
1220

1221
	drm_mode_create_suggested_offset_properties(&qdev->ddev);
1222 1223
	qxl_mode_create_hotplug_mode_update_property(qdev);

1224
	for (i = 0 ; i < qxl_num_crtc; ++i) {
1225 1226
		qdev_crtc_init(&qdev->ddev, i);
		qdev_output_init(&qdev->ddev, i);
D
Dave Airlie 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	}

	qdev->mode_info.mode_config_initialized = true;

	/* primary surface must be created by this point, to allow
	 * issuing command queue commands and having them read by
	 * spice server. */
	qxl_fbdev_init(qdev);
	return 0;
}

void qxl_modeset_fini(struct qxl_device *qdev)
{
	qxl_fbdev_fini(qdev);
1241 1242

	qxl_destroy_monitors_object(qdev);
D
Dave Airlie 已提交
1243
	if (qdev->mode_info.mode_config_initialized) {
1244
		drm_mode_config_cleanup(&qdev->ddev);
D
Dave Airlie 已提交
1245 1246 1247
		qdev->mode_info.mode_config_initialized = false;
	}
}