vmwgfx_ldu.c 12.3 KB
Newer Older
1 2
/**************************************************************************
 *
S
Sinclair Yeh 已提交
3
 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * All Rights Reserved.
 *
 * 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

32 33 34 35 36 37 38 39 40 41 42
#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;
43
	unsigned last_num_active;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

	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);
60
	vmw_du_cleanup(&ldu->base);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	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;
78
	struct vmw_display_unit *du = NULL;
79 80
	struct drm_framebuffer *fb = NULL;
	struct drm_crtc *crtc = NULL;
81
	int i = 0, ret;
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96
	/* 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;
97
		fb = entry->base.crtc.primary->fb;
98

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

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

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

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

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	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++;
	}

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

	lds->last_num_active = lds->num_active;

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	/* Find the first du with a cursor. */
	list_for_each_entry(entry, &lds->active, active) {
		du = &entry->base;

		if (!du->cursor_dmabuf)
			continue;

		ret = vmw_cursor_update_dmabuf(dev_priv,
					       du->cursor_dmabuf,
					       64, 64,
					       du->hotspot_x,
					       du->hotspot_y);
		if (ret == 0)
			break;

		DRM_ERROR("Could not update cursor image\n");
	}

154 155 156 157 158 159 160 161 162 163
	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;

164
	/* Must init otherwise list_empty(&ldu->active) will not work. */
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	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;

184 185 186 187 188 189 190 191 192
	BUG_ON(!ld->num_active && ld->fb);
	if (vfb != ld->fb) {
		if (ld->fb && ld->fb->unpin)
			ld->fb->unpin(ld->fb);
		if (vfb->pin)
			vfb->pin(vfb);
		ld->fb = vfb;
	}

193 194 195 196 197
	if (!list_empty(&ldu->active))
		return 0;

	at = &ld->active;
	list_for_each_entry(entry, &ld->active, active) {
198
		if (entry->base.unit > ldu->base.unit)
199 200 201 202 203 204
			break;

		at = &entry->active;
	}

	list_add(&ldu->active, at);
205 206

	ld->num_active++;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

	return 0;
}

static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
{
	struct vmw_private *dev_priv;
	struct vmw_legacy_display_unit *ldu;
	struct drm_connector *connector;
	struct drm_display_mode *mode;
	struct drm_encoder *encoder;
	struct vmw_framebuffer *vfb;
	struct drm_framebuffer *fb;
	struct drm_crtc *crtc;

	if (!set)
		return -EINVAL;

	if (!set->crtc)
		return -EINVAL;

	/* get the ldu */
	crtc = set->crtc;
	ldu = vmw_crtc_to_ldu(crtc);
	vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
	dev_priv = vmw_priv(crtc->dev);

	if (set->num_connectors > 1) {
		DRM_ERROR("to many connectors\n");
		return -EINVAL;
	}

	if (set->num_connectors == 1 &&
	    set->connectors[0] != &ldu->base.connector) {
		DRM_ERROR("connector doesn't match %p %p\n",
			set->connectors[0], &ldu->base.connector);
		return -EINVAL;
	}

	/* ldu only supports one fb active at the time */
	if (dev_priv->ldu_priv->fb && vfb &&
248 249
	    !(dev_priv->ldu_priv->num_active == 1 &&
	      !list_empty(&ldu->active)) &&
250 251 252 253 254 255 256 257 258 259 260 261 262 263
	    dev_priv->ldu_priv->fb != vfb) {
		DRM_ERROR("Multiple framebuffers not supported\n");
		return -EINVAL;
	}

	/* since they always map one to one these are safe */
	connector = &ldu->base.connector;
	encoder = &ldu->base.encoder;

	/* should we turn the crtc off? */
	if (set->num_connectors == 0 || !set->mode || !set->fb) {

		connector->encoder = NULL;
		encoder->crtc = NULL;
264
		crtc->primary->fb = NULL;
265
		crtc->enabled = false;
266 267 268

		vmw_ldu_del_active(dev_priv, ldu);

269
		return vmw_ldu_commit_list(dev_priv);
270 271 272 273 274 275 276 277 278 279 280 281 282
	}


	/* we now know we want to set a mode */
	mode = set->mode;
	fb = set->fb;

	if (set->x + mode->hdisplay > fb->width ||
	    set->y + mode->vdisplay > fb->height) {
		DRM_ERROR("set outside of framebuffer\n");
		return -EINVAL;
	}

283
	vmw_svga_enable(dev_priv);
284

285
	crtc->primary->fb = fb;
286 287 288 289 290
	encoder->crtc = crtc;
	connector->encoder = encoder;
	crtc->x = set->x;
	crtc->y = set->y;
	crtc->mode = *mode;
291
	crtc->enabled = true;
292 293
	ldu->base.set_gui_x = set->x;
	ldu->base.set_gui_y = set->y;
294 295 296

	vmw_ldu_add_active(dev_priv, ldu, vfb);

297
	return vmw_ldu_commit_list(dev_priv);
298 299
}

300
static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
301
	.cursor_set2 = vmw_du_crtc_cursor_set2,
302
	.cursor_move = vmw_du_crtc_cursor_move,
303
	.gamma_set = vmw_du_crtc_gamma_set,
304 305 306 307
	.destroy = vmw_ldu_crtc_destroy,
	.set_config = vmw_ldu_crtc_set_config,
};

308

309 310 311 312 313 314 315 316 317
/*
 * Legacy Display Unit encoder functions
 */

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

318
static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
319 320 321 322 323 324 325 326 327 328 329 330
	.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));
}

331
static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
332 333 334 335
	.dpms = vmw_du_connector_dpms,
	.detect = vmw_du_connector_detect,
	.fill_modes = vmw_du_connector_fill_modes,
	.set_property = vmw_du_connector_set_property,
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	.destroy = vmw_ldu_connector_destroy,
};

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;
	struct drm_crtc *crtc;

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

351
	ldu->base.unit = unit;
352 353 354 355
	crtc = &ldu->base.crtc;
	encoder = &ldu->base.encoder;
	connector = &ldu->base.connector;

356 357
	INIT_LIST_HEAD(&ldu->active);

358
	ldu->base.pref_active = (unit == 0);
359 360
	ldu->base.pref_width = dev_priv->initial_width;
	ldu->base.pref_height = dev_priv->initial_height;
361
	ldu->base.pref_mode = NULL;
362
	ldu->base.is_implicit = true;
363

364
	drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
365
			   DRM_MODE_CONNECTOR_VIRTUAL);
366
	connector->status = vmw_du_connector_detect(connector, true);
367 368

	drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
369
			 DRM_MODE_ENCODER_VIRTUAL, NULL);
370 371 372 373
	drm_mode_connector_attach_encoder(connector, encoder);
	encoder->possible_crtcs = (1 << unit);
	encoder->possible_clones = 0;

374
	(void) drm_connector_register(connector);
375

376 377
	drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);

M
Michel Dänzer 已提交
378 379
	drm_mode_crtc_set_gamma_size(crtc, 256);

380 381 382 383 384 385
	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);
386 387 388 389 390
	if (dev_priv->implicit_placement_property)
		drm_object_attach_property
			(&connector->base,
			 dev_priv->implicit_placement_property,
			 1);
391 392 393 394

	return 0;
}

395
int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
396
{
397
	struct drm_device *dev = dev_priv->dev;
398
	int i, ret;
399

400 401 402 403 404
	if (dev_priv->ldu_priv) {
		DRM_INFO("ldu system already on\n");
		return -EINVAL;
	}

405
	dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
406 407 408 409 410
	if (!dev_priv->ldu_priv)
		return -ENOMEM;

	INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
	dev_priv->ldu_priv->num_active = 0;
411
	dev_priv->ldu_priv->last_num_active = 0;
412 413
	dev_priv->ldu_priv->fb = NULL;

414 415 416 417 418 419 420
	/* 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;
421

422 423
	vmw_kms_create_implicit_placement_property(dev_priv, true);

424
	if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
425
		for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
426
			vmw_ldu_init(dev_priv, i);
427
	else
428
		vmw_ldu_init(dev_priv, 0);
429

430 431 432 433
	dev_priv->active_display_unit = vmw_du_legacy;

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

434 435 436 437 438
	return 0;

err_free:
	kfree(dev_priv->ldu_priv);
	dev_priv->ldu_priv = NULL;
439
	return ret;
440 441
}

442
int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
443
{
444 445
	struct drm_device *dev = dev_priv->dev;

446 447 448
	if (!dev_priv->ldu_priv)
		return -ENOSYS;

449 450
	drm_vblank_cleanup(dev);

451 452 453 454 455 456
	BUG_ON(!list_empty(&dev_priv->ldu_priv->active));

	kfree(dev_priv->ldu_priv);

	return 0;
}
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481


int vmw_kms_ldu_do_dmabuf_dirty(struct vmw_private *dev_priv,
				struct vmw_framebuffer *framebuffer,
				unsigned flags, unsigned color,
				struct drm_clip_rect *clips,
				unsigned num_clips, int increment)
{
	size_t fifo_size;
	int i;

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

	fifo_size = sizeof(*cmd) * num_clips;
	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Fifo reserve failed.\n");
		return -ENOMEM;
	}

	memset(cmd, 0, fifo_size);
	for (i = 0; i < num_clips; i++, clips += increment) {
482 483 484 485 486
		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;
487 488 489 490 491
	}

	vmw_fifo_commit(dev_priv, fifo_size);
	return 0;
}