Home | History | Annotate | Line # | Download | only in vmwgfx
vmwgfx_ldu.c revision 1.1.1.3
      1 /*	$NetBSD: vmwgfx_ldu.c,v 1.1.1.3 2018/08/27 01:35:00 riastradh Exp $	*/
      2 
      3 /**************************************************************************
      4  *
      5  * Copyright  2009-2015 VMware, Inc., Palo Alto, CA., USA
      6  * All Rights Reserved.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sub license, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the
     17  * next paragraph) shall be included in all copies or substantial portions
     18  * of the Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  **************************************************************************/
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_ldu.c,v 1.1.1.3 2018/08/27 01:35:00 riastradh Exp $");
     32 
     33 #include "vmwgfx_kms.h"
     34 #include <drm/drm_plane_helper.h>
     35 
     36 
     37 #define vmw_crtc_to_ldu(x) \
     38 	container_of(x, struct vmw_legacy_display_unit, base.crtc)
     39 #define vmw_encoder_to_ldu(x) \
     40 	container_of(x, struct vmw_legacy_display_unit, base.encoder)
     41 #define vmw_connector_to_ldu(x) \
     42 	container_of(x, struct vmw_legacy_display_unit, base.connector)
     43 
     44 struct vmw_legacy_display {
     45 	struct list_head active;
     46 
     47 	unsigned num_active;
     48 	unsigned last_num_active;
     49 
     50 	struct vmw_framebuffer *fb;
     51 };
     52 
     53 /**
     54  * Display unit using the legacy register interface.
     55  */
     56 struct vmw_legacy_display_unit {
     57 	struct vmw_display_unit base;
     58 
     59 	struct list_head active;
     60 };
     61 
     62 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
     63 {
     64 	list_del_init(&ldu->active);
     65 	vmw_du_cleanup(&ldu->base);
     66 	kfree(ldu);
     67 }
     68 
     69 
     70 /*
     71  * Legacy Display Unit CRTC functions
     72  */
     73 
     74 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
     75 {
     76 	vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
     77 }
     78 
     79 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
     80 {
     81 	struct vmw_legacy_display *lds = dev_priv->ldu_priv;
     82 	struct vmw_legacy_display_unit *entry;
     83 	struct vmw_display_unit *du = NULL;
     84 	struct drm_framebuffer *fb = NULL;
     85 	struct drm_crtc *crtc = NULL;
     86 	int i = 0, ret;
     87 
     88 	/* If there is no display topology the host just assumes
     89 	 * that the guest will set the same layout as the host.
     90 	 */
     91 	if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
     92 		int w = 0, h = 0;
     93 		list_for_each_entry(entry, &lds->active, active) {
     94 			crtc = &entry->base.crtc;
     95 			w = max(w, crtc->x + crtc->mode.hdisplay);
     96 			h = max(h, crtc->y + crtc->mode.vdisplay);
     97 			i++;
     98 		}
     99 
    100 		if (crtc == NULL)
    101 			return 0;
    102 		fb = entry->base.crtc.primary->fb;
    103 
    104 		return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
    105 					  fb->bits_per_pixel, fb->depth);
    106 	}
    107 
    108 	if (!list_empty(&lds->active)) {
    109 		entry = list_entry(lds->active.next, typeof(*entry), active);
    110 		fb = entry->base.crtc.primary->fb;
    111 
    112 		vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
    113 				   fb->bits_per_pixel, fb->depth);
    114 	}
    115 
    116 	/* Make sure we always show something. */
    117 	vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
    118 		  lds->num_active ? lds->num_active : 1);
    119 
    120 	i = 0;
    121 	list_for_each_entry(entry, &lds->active, active) {
    122 		crtc = &entry->base.crtc;
    123 
    124 		vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
    125 		vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
    126 		vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
    127 		vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
    128 		vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
    129 		vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
    130 		vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
    131 
    132 		i++;
    133 	}
    134 
    135 	BUG_ON(i != lds->num_active);
    136 
    137 	lds->last_num_active = lds->num_active;
    138 
    139 
    140 	/* Find the first du with a cursor. */
    141 	list_for_each_entry(entry, &lds->active, active) {
    142 		du = &entry->base;
    143 
    144 		if (!du->cursor_dmabuf)
    145 			continue;
    146 
    147 		ret = vmw_cursor_update_dmabuf(dev_priv,
    148 					       du->cursor_dmabuf,
    149 					       64, 64,
    150 					       du->hotspot_x,
    151 					       du->hotspot_y);
    152 		if (ret == 0)
    153 			break;
    154 
    155 		DRM_ERROR("Could not update cursor image\n");
    156 	}
    157 
    158 	return 0;
    159 }
    160 
    161 static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
    162 			      struct vmw_legacy_display_unit *ldu)
    163 {
    164 	struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
    165 	if (list_empty(&ldu->active))
    166 		return 0;
    167 
    168 	/* Must init otherwise list_empty(&ldu->active) will not work. */
    169 	list_del_init(&ldu->active);
    170 	if (--(ld->num_active) == 0) {
    171 		BUG_ON(!ld->fb);
    172 		if (ld->fb->unpin)
    173 			ld->fb->unpin(ld->fb);
    174 		ld->fb = NULL;
    175 	}
    176 
    177 	return 0;
    178 }
    179 
    180 static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
    181 			      struct vmw_legacy_display_unit *ldu,
    182 			      struct vmw_framebuffer *vfb)
    183 {
    184 	struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
    185 	struct vmw_legacy_display_unit *entry;
    186 	struct list_head *at;
    187 
    188 	BUG_ON(!ld->num_active && ld->fb);
    189 	if (vfb != ld->fb) {
    190 		if (ld->fb && ld->fb->unpin)
    191 			ld->fb->unpin(ld->fb);
    192 		if (vfb->pin)
    193 			vfb->pin(vfb);
    194 		ld->fb = vfb;
    195 	}
    196 
    197 	if (!list_empty(&ldu->active))
    198 		return 0;
    199 
    200 	at = &ld->active;
    201 	list_for_each_entry(entry, &ld->active, active) {
    202 		if (entry->base.unit > ldu->base.unit)
    203 			break;
    204 
    205 		at = &entry->active;
    206 	}
    207 
    208 	list_add(&ldu->active, at);
    209 
    210 	ld->num_active++;
    211 
    212 	return 0;
    213 }
    214 
    215 static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
    216 {
    217 	struct vmw_private *dev_priv;
    218 	struct vmw_legacy_display_unit *ldu;
    219 	struct drm_connector *connector;
    220 	struct drm_display_mode *mode;
    221 	struct drm_encoder *encoder;
    222 	struct vmw_framebuffer *vfb;
    223 	struct drm_framebuffer *fb;
    224 	struct drm_crtc *crtc;
    225 
    226 	if (!set)
    227 		return -EINVAL;
    228 
    229 	if (!set->crtc)
    230 		return -EINVAL;
    231 
    232 	/* get the ldu */
    233 	crtc = set->crtc;
    234 	ldu = vmw_crtc_to_ldu(crtc);
    235 	vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
    236 	dev_priv = vmw_priv(crtc->dev);
    237 
    238 	if (set->num_connectors > 1) {
    239 		DRM_ERROR("to many connectors\n");
    240 		return -EINVAL;
    241 	}
    242 
    243 	if (set->num_connectors == 1 &&
    244 	    set->connectors[0] != &ldu->base.connector) {
    245 		DRM_ERROR("connector doesn't match %p %p\n",
    246 			set->connectors[0], &ldu->base.connector);
    247 		return -EINVAL;
    248 	}
    249 
    250 	/* ldu only supports one fb active at the time */
    251 	if (dev_priv->ldu_priv->fb && vfb &&
    252 	    !(dev_priv->ldu_priv->num_active == 1 &&
    253 	      !list_empty(&ldu->active)) &&
    254 	    dev_priv->ldu_priv->fb != vfb) {
    255 		DRM_ERROR("Multiple framebuffers not supported\n");
    256 		return -EINVAL;
    257 	}
    258 
    259 	/* since they always map one to one these are safe */
    260 	connector = &ldu->base.connector;
    261 	encoder = &ldu->base.encoder;
    262 
    263 	/* should we turn the crtc off? */
    264 	if (set->num_connectors == 0 || !set->mode || !set->fb) {
    265 
    266 		connector->encoder = NULL;
    267 		encoder->crtc = NULL;
    268 		crtc->primary->fb = NULL;
    269 		crtc->enabled = false;
    270 
    271 		vmw_ldu_del_active(dev_priv, ldu);
    272 
    273 		return vmw_ldu_commit_list(dev_priv);
    274 	}
    275 
    276 
    277 	/* we now know we want to set a mode */
    278 	mode = set->mode;
    279 	fb = set->fb;
    280 
    281 	if (set->x + mode->hdisplay > fb->width ||
    282 	    set->y + mode->vdisplay > fb->height) {
    283 		DRM_ERROR("set outside of framebuffer\n");
    284 		return -EINVAL;
    285 	}
    286 
    287 	vmw_svga_enable(dev_priv);
    288 
    289 	crtc->primary->fb = fb;
    290 	encoder->crtc = crtc;
    291 	connector->encoder = encoder;
    292 	crtc->x = set->x;
    293 	crtc->y = set->y;
    294 	crtc->mode = *mode;
    295 	crtc->enabled = true;
    296 
    297 	vmw_ldu_add_active(dev_priv, ldu, vfb);
    298 
    299 	return vmw_ldu_commit_list(dev_priv);
    300 }
    301 
    302 static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
    303 	.save = vmw_du_crtc_save,
    304 	.restore = vmw_du_crtc_restore,
    305 	.cursor_set2 = vmw_du_crtc_cursor_set2,
    306 	.cursor_move = vmw_du_crtc_cursor_move,
    307 	.gamma_set = vmw_du_crtc_gamma_set,
    308 	.destroy = vmw_ldu_crtc_destroy,
    309 	.set_config = vmw_ldu_crtc_set_config,
    310 };
    311 
    312 
    313 /*
    314  * Legacy Display Unit encoder functions
    315  */
    316 
    317 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
    318 {
    319 	vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
    320 }
    321 
    322 static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
    323 	.destroy = vmw_ldu_encoder_destroy,
    324 };
    325 
    326 /*
    327  * Legacy Display Unit connector functions
    328  */
    329 
    330 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
    331 {
    332 	vmw_ldu_destroy(vmw_connector_to_ldu(connector));
    333 }
    334 
    335 static struct drm_connector_funcs vmw_legacy_connector_funcs = {
    336 	.dpms = vmw_du_connector_dpms,
    337 	.save = vmw_du_connector_save,
    338 	.restore = vmw_du_connector_restore,
    339 	.detect = vmw_du_connector_detect,
    340 	.fill_modes = vmw_du_connector_fill_modes,
    341 	.set_property = vmw_du_connector_set_property,
    342 	.destroy = vmw_ldu_connector_destroy,
    343 };
    344 
    345 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
    346 {
    347 	struct vmw_legacy_display_unit *ldu;
    348 	struct drm_device *dev = dev_priv->dev;
    349 	struct drm_connector *connector;
    350 	struct drm_encoder *encoder;
    351 	struct drm_crtc *crtc;
    352 
    353 	ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
    354 	if (!ldu)
    355 		return -ENOMEM;
    356 
    357 	ldu->base.unit = unit;
    358 	crtc = &ldu->base.crtc;
    359 	encoder = &ldu->base.encoder;
    360 	connector = &ldu->base.connector;
    361 
    362 	INIT_LIST_HEAD(&ldu->active);
    363 
    364 	ldu->base.pref_active = (unit == 0);
    365 	ldu->base.pref_width = dev_priv->initial_width;
    366 	ldu->base.pref_height = dev_priv->initial_height;
    367 	ldu->base.pref_mode = NULL;
    368 	ldu->base.is_implicit = true;
    369 
    370 	drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
    371 			   DRM_MODE_CONNECTOR_VIRTUAL);
    372 	connector->status = vmw_du_connector_detect(connector, true);
    373 
    374 	drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
    375 			 DRM_MODE_ENCODER_VIRTUAL);
    376 	drm_mode_connector_attach_encoder(connector, encoder);
    377 	encoder->possible_crtcs = (1 << unit);
    378 	encoder->possible_clones = 0;
    379 
    380 	(void) drm_connector_register(connector);
    381 
    382 	drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
    383 
    384 	drm_mode_crtc_set_gamma_size(crtc, 256);
    385 
    386 	drm_object_attach_property(&connector->base,
    387 				      dev->mode_config.dirty_info_property,
    388 				      1);
    389 
    390 	return 0;
    391 }
    392 
    393 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
    394 {
    395 	struct drm_device *dev = dev_priv->dev;
    396 	int i, ret;
    397 
    398 	if (dev_priv->ldu_priv) {
    399 		DRM_INFO("ldu system already on\n");
    400 		return -EINVAL;
    401 	}
    402 
    403 	dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
    404 	if (!dev_priv->ldu_priv)
    405 		return -ENOMEM;
    406 
    407 	INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
    408 	dev_priv->ldu_priv->num_active = 0;
    409 	dev_priv->ldu_priv->last_num_active = 0;
    410 	dev_priv->ldu_priv->fb = NULL;
    411 
    412 	/* for old hardware without multimon only enable one display */
    413 	if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
    414 		ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
    415 	else
    416 		ret = drm_vblank_init(dev, 1);
    417 	if (ret != 0)
    418 		goto err_free;
    419 
    420 	ret = drm_mode_create_dirty_info_property(dev);
    421 	if (ret != 0)
    422 		goto err_vblank_cleanup;
    423 
    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 	dev_priv->active_display_unit = vmw_du_legacy;
    431 
    432 	DRM_INFO("Legacy Display Unit initialized\n");
    433 
    434 	return 0;
    435 
    436 err_vblank_cleanup:
    437 	drm_vblank_cleanup(dev);
    438 err_free:
    439 	kfree(dev_priv->ldu_priv);
    440 	dev_priv->ldu_priv = NULL;
    441 	return ret;
    442 }
    443 
    444 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
    445 {
    446 	struct drm_device *dev = dev_priv->dev;
    447 
    448 	if (!dev_priv->ldu_priv)
    449 		return -ENOSYS;
    450 
    451 	drm_vblank_cleanup(dev);
    452 
    453 	BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
    454 
    455 	kfree(dev_priv->ldu_priv);
    456 
    457 	return 0;
    458 }
    459 
    460 
    461 int vmw_kms_ldu_do_dmabuf_dirty(struct vmw_private *dev_priv,
    462 				struct vmw_framebuffer *framebuffer,
    463 				unsigned flags, unsigned color,
    464 				struct drm_clip_rect *clips,
    465 				unsigned num_clips, int increment)
    466 {
    467 	size_t fifo_size;
    468 	int i;
    469 
    470 	struct {
    471 		uint32_t header;
    472 		SVGAFifoCmdUpdate body;
    473 	} *cmd;
    474 
    475 	fifo_size = sizeof(*cmd) * num_clips;
    476 	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
    477 	if (unlikely(cmd == NULL)) {
    478 		DRM_ERROR("Fifo reserve failed.\n");
    479 		return -ENOMEM;
    480 	}
    481 
    482 	memset(cmd, 0, fifo_size);
    483 	for (i = 0; i < num_clips; i++, clips += increment) {
    484 		cmd[i].header = SVGA_CMD_UPDATE;
    485 		cmd[i].body.x = clips->x1;
    486 		cmd[i].body.y = clips->y1;
    487 		cmd[i].body.width = clips->x2 - clips->x1;
    488 		cmd[i].body.height = clips->y2 - clips->y1;
    489 	}
    490 
    491 	vmw_fifo_commit(dev_priv, fifo_size);
    492 	return 0;
    493 }
    494