vmwgfx_ldu.c 15.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0 OR MIT
2 3
/**************************************************************************
 *
4
 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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.
 *
 **************************************************************************/

#include "vmwgfx_kms.h"
29
#include <drm/drm_plane_helper.h>
30 31
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
32

33

34 35 36 37 38 39 40 41 42 43 44
#define vmw_crtc_to_ldu(x) \
	container_of(x, struct vmw_legacy_display_unit, base.crtc)
#define vmw_encoder_to_ldu(x) \
	container_of(x, struct vmw_legacy_display_unit, base.encoder)
#define vmw_connector_to_ldu(x) \
	container_of(x, struct vmw_legacy_display_unit, base.connector)

struct vmw_legacy_display {
	struct list_head active;

	unsigned num_active;
45
	unsigned last_num_active;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

	struct vmw_framebuffer *fb;
};

/**
 * Display unit using the legacy register interface.
 */
struct vmw_legacy_display_unit {
	struct vmw_display_unit base;

	struct list_head active;
};

static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
{
	list_del_init(&ldu->active);
62
	vmw_du_cleanup(&ldu->base);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	kfree(ldu);
}


/*
 * Legacy Display Unit CRTC functions
 */

static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
{
	vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
}

static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
{
	struct vmw_legacy_display *lds = dev_priv->ldu_priv;
	struct vmw_legacy_display_unit *entry;
80 81
	struct drm_framebuffer *fb = NULL;
	struct drm_crtc *crtc = NULL;
82
	int i = 0;
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
	/* If there is no display topology the host just assumes
	 * that the guest will set the same layout as the host.
	 */
	if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
		int w = 0, h = 0;
		list_for_each_entry(entry, &lds->active, active) {
			crtc = &entry->base.crtc;
			w = max(w, crtc->x + crtc->mode.hdisplay);
			h = max(h, crtc->y + crtc->mode.vdisplay);
			i++;
		}

		if (crtc == NULL)
			return 0;
98
		fb = entry->base.crtc.primary->state->fb;
99

100
		return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
V
Ville Syrjälä 已提交
101
					  fb->format->cpp[0] * 8,
V
Ville Syrjälä 已提交
102
					  fb->format->depth);
103 104 105 106
	}

	if (!list_empty(&lds->active)) {
		entry = list_entry(lds->active.next, typeof(*entry), active);
107
		fb = entry->base.crtc.primary->state->fb;
108

109
		vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
V
Ville Syrjälä 已提交
110
				   fb->format->cpp[0] * 8, fb->format->depth);
111 112
	}

113 114 115 116
	/* Make sure we always show something. */
	vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
		  lds->num_active ? lds->num_active : 1);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	i = 0;
	list_for_each_entry(entry, &lds->active, active) {
		crtc = &entry->base.crtc;

		vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
		vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);

		i++;
	}

132 133 134 135
	BUG_ON(i != lds->num_active);

	lds->last_num_active = lds->num_active;

136 137 138 139 140 141 142 143 144 145
	return 0;
}

static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
			      struct vmw_legacy_display_unit *ldu)
{
	struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
	if (list_empty(&ldu->active))
		return 0;

146
	/* Must init otherwise list_empty(&ldu->active) will not work. */
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	list_del_init(&ldu->active);
	if (--(ld->num_active) == 0) {
		BUG_ON(!ld->fb);
		if (ld->fb->unpin)
			ld->fb->unpin(ld->fb);
		ld->fb = NULL;
	}

	return 0;
}

static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
			      struct vmw_legacy_display_unit *ldu,
			      struct vmw_framebuffer *vfb)
{
	struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
	struct vmw_legacy_display_unit *entry;
	struct list_head *at;

166 167 168 169
	BUG_ON(!ld->num_active && ld->fb);
	if (vfb != ld->fb) {
		if (ld->fb && ld->fb->unpin)
			ld->fb->unpin(ld->fb);
170
		vmw_svga_enable(vmw_priv);
171 172 173 174 175
		if (vfb->pin)
			vfb->pin(vfb);
		ld->fb = vfb;
	}

176 177 178 179 180
	if (!list_empty(&ldu->active))
		return 0;

	at = &ld->active;
	list_for_each_entry(entry, &ld->active, active) {
181
		if (entry->base.unit > ldu->base.unit)
182 183 184 185 186 187
			break;

		at = &entry->active;
	}

	list_add(&ldu->active, at);
188 189

	ld->num_active++;
190 191 192 193

	return 0;
}

194 195 196 197 198 199 200 201 202 203 204 205
/**
 * vmw_ldu_crtc_mode_set_nofb - Enable svga
 *
 * @crtc: CRTC associated with the new screen
 *
 * For LDU, just enable the svga
 */
static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
{
}

/**
206
 * vmw_ldu_crtc_atomic_enable - Noop
207 208 209 210 211 212 213 214
 *
 * @crtc: CRTC associated with the new screen
 *
 * This is called after a mode set has been completed.  Here's
 * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
 * but since for LDU the display plane is closely tied to the
 * CRTC, it makes more sense to do those at plane update time.
 */
215 216
static void vmw_ldu_crtc_atomic_enable(struct drm_crtc *crtc,
				       struct drm_crtc_state *old_state)
217 218 219 220
{
}

/**
221
 * vmw_ldu_crtc_atomic_disable - Turns off CRTC
222 223 224
 *
 * @crtc: CRTC to be turned off
 */
225 226
static void vmw_ldu_crtc_atomic_disable(struct drm_crtc *crtc,
					struct drm_crtc_state *old_state)
227 228 229
{
}

230
static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
231
	.gamma_set = vmw_du_crtc_gamma_set,
232
	.destroy = vmw_ldu_crtc_destroy,
S
Sinclair Yeh 已提交
233 234 235
	.reset = vmw_du_crtc_reset,
	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
	.atomic_destroy_state = vmw_du_crtc_destroy_state,
236
	.set_config = drm_atomic_helper_set_config,
237 238
};

239

240 241 242 243 244 245 246 247 248
/*
 * Legacy Display Unit encoder functions
 */

static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
{
	vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
}

249
static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
250 251 252 253 254 255 256 257 258 259 260 261
	.destroy = vmw_ldu_encoder_destroy,
};

/*
 * Legacy Display Unit connector functions
 */

static void vmw_ldu_connector_destroy(struct drm_connector *connector)
{
	vmw_ldu_destroy(vmw_connector_to_ldu(connector));
}

262
static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
263 264 265
	.dpms = vmw_du_connector_dpms,
	.detect = vmw_du_connector_detect,
	.fill_modes = vmw_du_connector_fill_modes,
266
	.destroy = vmw_ldu_connector_destroy,
267
	.reset = vmw_du_connector_reset,
268 269
	.atomic_duplicate_state = vmw_du_connector_duplicate_state,
	.atomic_destroy_state = vmw_du_connector_destroy_state,
270 271
};

272 273 274 275
static const struct
drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
};

276 277 278 279
/*
 * Legacy Display Plane Functions
 */

280 281 282 283
static void
vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane,
				    struct drm_plane_state *old_state)
{
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	struct vmw_private *dev_priv;
	struct vmw_legacy_display_unit *ldu;
	struct vmw_framebuffer *vfb;
	struct drm_framebuffer *fb;
	struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;


	ldu = vmw_crtc_to_ldu(crtc);
	dev_priv = vmw_priv(plane->dev);
	fb       = plane->state->fb;

	vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;

	if (vfb)
		vmw_ldu_add_active(dev_priv, ldu, vfb);
	else
		vmw_ldu_del_active(dev_priv, ldu);

	vmw_ldu_commit_list(dev_priv);
303 304 305
}


306
static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
307 308
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
309
	.destroy = vmw_du_primary_plane_destroy,
S
Sinclair Yeh 已提交
310 311 312
	.reset = vmw_du_plane_reset,
	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
	.atomic_destroy_state = vmw_du_plane_destroy_state,
313 314 315
};

static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
316 317
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
318
	.destroy = vmw_du_cursor_plane_destroy,
S
Sinclair Yeh 已提交
319 320 321
	.reset = vmw_du_plane_reset,
	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
	.atomic_destroy_state = vmw_du_plane_destroy_state,
322 323
};

324 325 326
/*
 * Atomic Helpers
 */
327 328 329 330 331 332 333 334 335 336 337 338 339 340
static const struct
drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = {
	.atomic_check = vmw_du_cursor_plane_atomic_check,
	.atomic_update = vmw_du_cursor_plane_atomic_update,
	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
	.cleanup_fb = vmw_du_plane_cleanup_fb,
};

static const struct
drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = {
	.atomic_check = vmw_du_primary_plane_atomic_check,
	.atomic_update = vmw_ldu_primary_plane_atomic_update,
};

341 342 343 344 345
static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = {
	.mode_set_nofb = vmw_ldu_crtc_mode_set_nofb,
	.atomic_check = vmw_du_crtc_atomic_check,
	.atomic_begin = vmw_du_crtc_atomic_begin,
	.atomic_flush = vmw_du_crtc_atomic_flush,
346
	.atomic_enable = vmw_ldu_crtc_atomic_enable,
347
	.atomic_disable = vmw_ldu_crtc_atomic_disable,
348 349
};

350

351 352 353 354 355 356
static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
{
	struct vmw_legacy_display_unit *ldu;
	struct drm_device *dev = dev_priv->dev;
	struct drm_connector *connector;
	struct drm_encoder *encoder;
S
Sinclair Yeh 已提交
357
	struct drm_plane *primary, *cursor;
358
	struct drm_crtc *crtc;
359
	int ret;
360 361 362 363 364

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

365
	ldu->base.unit = unit;
366 367 368
	crtc = &ldu->base.crtc;
	encoder = &ldu->base.encoder;
	connector = &ldu->base.connector;
S
Sinclair Yeh 已提交
369 370
	primary = &ldu->base.primary;
	cursor = &ldu->base.cursor;
371

372 373
	INIT_LIST_HEAD(&ldu->active);

374
	ldu->base.pref_active = (unit == 0);
375 376
	ldu->base.pref_width = dev_priv->initial_width;
	ldu->base.pref_height = dev_priv->initial_height;
377
	ldu->base.pref_mode = NULL;
S
Sinclair Yeh 已提交
378 379 380 381 382

	/*
	 * Remove this after enabling atomic because property values can
	 * only exist in a state object
	 */
383
	ldu->base.is_implicit = true;
384

385
	/* Initialize primary plane */
S
Sinclair Yeh 已提交
386 387
	vmw_du_plane_reset(primary);

388 389 390 391
	ret = drm_universal_plane_init(dev, &ldu->base.primary,
				       0, &vmw_ldu_plane_funcs,
				       vmw_primary_plane_formats,
				       ARRAY_SIZE(vmw_primary_plane_formats),
392
				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
393 394 395 396 397
	if (ret) {
		DRM_ERROR("Failed to initialize primary plane");
		goto err_free;
	}

398 399
	drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs);

400
	/* Initialize cursor plane */
S
Sinclair Yeh 已提交
401 402
	vmw_du_plane_reset(cursor);

403 404 405 406
	ret = drm_universal_plane_init(dev, &ldu->base.cursor,
			0, &vmw_ldu_cursor_funcs,
			vmw_cursor_plane_formats,
			ARRAY_SIZE(vmw_cursor_plane_formats),
407
			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
408 409 410 411 412 413
	if (ret) {
		DRM_ERROR("Failed to initialize cursor plane");
		drm_plane_cleanup(&ldu->base.primary);
		goto err_free;
	}

414 415 416
	drm_plane_helper_add(cursor, &vmw_ldu_cursor_plane_helper_funcs);

	vmw_du_connector_reset(connector);
417 418 419 420 421 422
	ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
				 DRM_MODE_CONNECTOR_VIRTUAL);
	if (ret) {
		DRM_ERROR("Failed to initialize connector\n");
		goto err_free;
	}
423 424

	drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
425
	connector->status = vmw_du_connector_detect(connector, true);
426

427 428 429 430 431 432 433
	ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
			       DRM_MODE_ENCODER_VIRTUAL, NULL);
	if (ret) {
		DRM_ERROR("Failed to initialize encoder\n");
		goto err_free_connector;
	}

434
	(void) drm_connector_attach_encoder(connector, encoder);
435 436 437
	encoder->possible_crtcs = (1 << unit);
	encoder->possible_clones = 0;

438 439 440 441 442
	ret = drm_connector_register(connector);
	if (ret) {
		DRM_ERROR("Failed to register connector\n");
		goto err_free_encoder;
	}
443

444
	vmw_du_crtc_reset(crtc);
445 446 447 448 449 450 451
	ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary,
					&ldu->base.cursor,
					&vmw_legacy_crtc_funcs, NULL);
	if (ret) {
		DRM_ERROR("Failed to initialize CRTC\n");
		goto err_free_unregister;
	}
452

453 454
	drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);

M
Michel Dänzer 已提交
455 456
	drm_mode_crtc_set_gamma_size(crtc, 256);

457 458 459 460 461 462
	drm_object_attach_property(&connector->base,
				   dev_priv->hotplug_mode_update_property, 1);
	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);
463 464 465 466 467
	if (dev_priv->implicit_placement_property)
		drm_object_attach_property
			(&connector->base,
			 dev_priv->implicit_placement_property,
			 1);
468 469

	return 0;
470 471 472 473 474 475 476 477 478 479

err_free_unregister:
	drm_connector_unregister(connector);
err_free_encoder:
	drm_encoder_cleanup(encoder);
err_free_connector:
	drm_connector_cleanup(connector);
err_free:
	kfree(ldu);
	return ret;
480 481
}

482
int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
483
{
484
	struct drm_device *dev = dev_priv->dev;
485
	int i, ret;
486

487 488 489 490 491
	if (dev_priv->ldu_priv) {
		DRM_INFO("ldu system already on\n");
		return -EINVAL;
	}

492
	dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
493 494 495 496 497
	if (!dev_priv->ldu_priv)
		return -ENOMEM;

	INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
	dev_priv->ldu_priv->num_active = 0;
498
	dev_priv->ldu_priv->last_num_active = 0;
499 500
	dev_priv->ldu_priv->fb = NULL;

501 502 503 504 505 506 507
	/* for old hardware without multimon only enable one display */
	if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
		ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
	else
		ret = drm_vblank_init(dev, 1);
	if (ret != 0)
		goto err_free;
508

509
	vmw_kms_create_implicit_placement_property(dev_priv);
510

511
	if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
512
		for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
513
			vmw_ldu_init(dev_priv, i);
514
	else
515
		vmw_ldu_init(dev_priv, 0);
516

517 518 519 520
	dev_priv->active_display_unit = vmw_du_legacy;

	DRM_INFO("Legacy Display Unit initialized\n");

521 522 523 524 525
	return 0;

err_free:
	kfree(dev_priv->ldu_priv);
	dev_priv->ldu_priv = NULL;
526
	return ret;
527 528
}

529
int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
530 531 532 533 534 535 536 537 538 539
{
	if (!dev_priv->ldu_priv)
		return -ENOSYS;

	BUG_ON(!list_empty(&dev_priv->ldu_priv->active));

	kfree(dev_priv->ldu_priv);

	return 0;
}
540 541


542 543 544 545 546
int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
			    struct vmw_framebuffer *framebuffer,
			    unsigned int flags, unsigned int color,
			    struct drm_clip_rect *clips,
			    unsigned int num_clips, int increment)
547 548 549 550 551 552 553 554 555 556
{
	size_t fifo_size;
	int i;

	struct {
		uint32_t header;
		SVGAFifoCmdUpdate body;
	} *cmd;

	fifo_size = sizeof(*cmd) * num_clips;
557 558
	cmd = VMW_FIFO_RESERVE(dev_priv, fifo_size);
	if (unlikely(cmd == NULL))
559 560 561 562
		return -ENOMEM;

	memset(cmd, 0, fifo_size);
	for (i = 0; i < num_clips; i++, clips += increment) {
563 564 565 566 567
		cmd[i].header = SVGA_CMD_UPDATE;
		cmd[i].body.x = clips->x1;
		cmd[i].body.y = clips->y1;
		cmd[i].body.width = clips->x2 - clips->x1;
		cmd[i].body.height = clips->y2 - clips->y1;
568 569 570 571 572
	}

	vmw_fifo_commit(dev_priv, fifo_size);
	return 0;
}