Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR MIT
   2/**************************************************************************
   3 *
   4 * Copyright 2011-2023 VMware, Inc., Palo Alto, CA., USA
 
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a
   7 * copy of this software and associated documentation files (the
   8 * "Software"), to deal in the Software without restriction, including
   9 * without limitation the rights to use, copy, modify, merge, publish,
  10 * distribute, sub license, and/or sell copies of the Software, and to
  11 * permit persons to whom the Software is furnished to do so, subject to
  12 * the following conditions:
  13 *
  14 * The above copyright notice and this permission notice (including the
  15 * next paragraph) shall be included in all copies or substantial portions
  16 * of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25 *
  26 **************************************************************************/
  27
  28#include "vmwgfx_bo.h"
  29#include "vmwgfx_kms.h"
  30
  31#include <drm/drm_atomic.h>
  32#include <drm/drm_atomic_helper.h>
  33#include <drm/drm_damage_helper.h>
  34#include <drm/drm_fourcc.h>
  35
  36#define vmw_crtc_to_sou(x) \
  37	container_of(x, struct vmw_screen_object_unit, base.crtc)
  38#define vmw_encoder_to_sou(x) \
  39	container_of(x, struct vmw_screen_object_unit, base.encoder)
  40#define vmw_connector_to_sou(x) \
  41	container_of(x, struct vmw_screen_object_unit, base.connector)
  42
  43/**
  44 * struct vmw_kms_sou_surface_dirty - Closure structure for
  45 * blit surface to screen command.
  46 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
  47 * @left: Left side of bounding box.
  48 * @right: Right side of bounding box.
  49 * @top: Top side of bounding box.
  50 * @bottom: Bottom side of bounding box.
  51 * @dst_x: Difference between source clip rects and framebuffer coordinates.
  52 * @dst_y: Difference between source clip rects and framebuffer coordinates.
  53 * @sid: Surface id of surface to copy from.
  54 */
  55struct vmw_kms_sou_surface_dirty {
  56	struct vmw_kms_dirty base;
  57	s32 left, right, top, bottom;
  58	s32 dst_x, dst_y;
  59	u32 sid;
  60};
  61
  62/*
  63 * SVGA commands that are used by this code. Please see the device headers
  64 * for explanation.
  65 */
  66struct vmw_kms_sou_readback_blit {
  67	uint32 header;
  68	SVGAFifoCmdBlitScreenToGMRFB body;
  69};
  70
  71struct vmw_kms_sou_bo_blit {
  72	uint32 header;
  73	SVGAFifoCmdBlitGMRFBToScreen body;
  74};
  75
  76struct vmw_kms_sou_dirty_cmd {
  77	SVGA3dCmdHeader header;
  78	SVGA3dCmdBlitSurfaceToScreen body;
  79};
  80
  81struct vmw_kms_sou_define_gmrfb {
  82	uint32_t header;
  83	SVGAFifoCmdDefineGMRFB body;
  84};
  85
  86/*
  87 * Display unit using screen objects.
  88 */
  89struct vmw_screen_object_unit {
  90	struct vmw_display_unit base;
  91
  92	unsigned long buffer_size; /**< Size of allocated buffer */
  93	struct vmw_bo *buffer; /**< Backing store buffer */
  94
  95	bool defined;
  96};
  97
  98static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
  99{
 100	vmw_du_cleanup(&sou->base);
 101	kfree(sou);
 102}
 103
 104
 105/*
 106 * Screen Object Display Unit CRTC functions
 107 */
 108
 109static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
 110{
 111	vmw_sou_destroy(vmw_crtc_to_sou(crtc));
 112}
 113
 114/*
 115 * Send the fifo command to create a screen.
 116 */
 117static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
 118			       struct vmw_screen_object_unit *sou,
 119			       int x, int y,
 120			       struct drm_display_mode *mode)
 121{
 122	size_t fifo_size;
 123
 124	struct {
 125		struct {
 126			uint32_t cmdType;
 127		} header;
 128		SVGAScreenObject obj;
 129	} *cmd;
 130
 131	BUG_ON(!sou->buffer);
 132
 133	fifo_size = sizeof(*cmd);
 134	cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
 135	if (unlikely(cmd == NULL))
 
 
 136		return -ENOMEM;
 
 137
 138	memset(cmd, 0, fifo_size);
 139	cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
 140	cmd->obj.structSize = sizeof(SVGAScreenObject);
 141	cmd->obj.id = sou->base.unit;
 142	cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
 143		(sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
 144	cmd->obj.size.width = mode->hdisplay;
 145	cmd->obj.size.height = mode->vdisplay;
 146	cmd->obj.root.x = x;
 147	cmd->obj.root.y = y;
 
 
 
 
 
 148	sou->base.set_gui_x = cmd->obj.root.x;
 149	sou->base.set_gui_y = cmd->obj.root.y;
 150
 151	/* Ok to assume that buffer is pinned in vram */
 152	vmw_bo_get_guest_ptr(&sou->buffer->tbo, &cmd->obj.backingStore.ptr);
 153	cmd->obj.backingStore.pitch = mode->hdisplay * 4;
 154
 155	vmw_cmd_commit(dev_priv, fifo_size);
 156
 157	sou->defined = true;
 158
 159	return 0;
 160}
 161
 162/*
 163 * Send the fifo command to destroy a screen.
 164 */
 165static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
 166				struct vmw_screen_object_unit *sou)
 167{
 168	size_t fifo_size;
 169	int ret;
 170
 171	struct {
 172		struct {
 173			uint32_t cmdType;
 174		} header;
 175		SVGAFifoCmdDestroyScreen body;
 176	} *cmd;
 177
 178	/* no need to do anything */
 179	if (unlikely(!sou->defined))
 180		return 0;
 181
 182	fifo_size = sizeof(*cmd);
 183	cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
 184	if (unlikely(cmd == NULL))
 
 
 185		return -ENOMEM;
 
 186
 187	memset(cmd, 0, fifo_size);
 188	cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
 189	cmd->body.screenId = sou->base.unit;
 190
 191	vmw_cmd_commit(dev_priv, fifo_size);
 192
 193	/* Force sync */
 194	ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
 195	if (unlikely(ret != 0))
 196		DRM_ERROR("Failed to sync with HW");
 197	else
 198		sou->defined = false;
 199
 200	return ret;
 201}
 202
 203/**
 204 * vmw_sou_crtc_mode_set_nofb - Create new screen
 205 *
 206 * @crtc: CRTC associated with the new screen
 207 *
 208 * This function creates/destroys a screen.  This function cannot fail, so if
 209 * somehow we run into a failure, just do the best we can to get out.
 210 */
 211static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
 212{
 213	struct vmw_private *dev_priv;
 214	struct vmw_screen_object_unit *sou;
 215	struct vmw_framebuffer *vfb;
 216	struct drm_framebuffer *fb;
 217	struct drm_plane_state *ps;
 218	struct vmw_plane_state *vps;
 219	int ret;
 220
 221	sou = vmw_crtc_to_sou(crtc);
 
 222	dev_priv = vmw_priv(crtc->dev);
 223	ps = crtc->primary->state;
 224	fb = ps->fb;
 225	vps = vmw_plane_state_to_vps(ps);
 226
 227	vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
 228
 229	if (sou->defined) {
 230		ret = vmw_sou_fifo_destroy(dev_priv, sou);
 231		if (ret) {
 232			DRM_ERROR("Failed to destroy Screen Object\n");
 233			return;
 234		}
 235	}
 236
 237	if (vfb) {
 238		struct drm_connector_state *conn_state;
 239		struct vmw_connector_state *vmw_conn_state;
 240		int x, y;
 241
 242		sou->buffer = vps->bo;
 243		sou->buffer_size = vps->bo_size;
 244
 245		conn_state = sou->base.connector.state;
 246		vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
 247
 248		x = vmw_conn_state->gui_x;
 249		y = vmw_conn_state->gui_y;
 250
 251		ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
 252		if (ret)
 253			DRM_ERROR("Failed to define Screen Object %dx%d\n",
 254				  crtc->x, crtc->y);
 255
 
 256	} else {
 257		sou->buffer = NULL;
 258		sou->buffer_size = 0;
 
 
 259	}
 260}
 261
 262/**
 263 * vmw_sou_crtc_helper_prepare - Noop
 264 *
 265 * @crtc:  CRTC associated with the new screen
 266 *
 267 * Prepares the CRTC for a mode set, but we don't need to do anything here.
 268 */
 269static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
 270{
 271}
 272
 273/**
 274 * vmw_sou_crtc_atomic_enable - Noop
 275 *
 276 * @crtc: CRTC associated with the new screen
 277 * @state: Unused
 278 *
 279 * This is called after a mode set has been completed.
 280 */
 281static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
 282				       struct drm_atomic_state *state)
 283{
 284}
 285
 286/**
 287 * vmw_sou_crtc_atomic_disable - Turns off CRTC
 288 *
 289 * @crtc: CRTC to be turned off
 290 * @state: Unused
 291 */
 292static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
 293					struct drm_atomic_state *state)
 294{
 295	struct vmw_private *dev_priv;
 296	struct vmw_screen_object_unit *sou;
 297	int ret;
 298
 299
 300	if (!crtc) {
 301		DRM_ERROR("CRTC is NULL\n");
 302		return;
 303	}
 304
 305	sou = vmw_crtc_to_sou(crtc);
 306	dev_priv = vmw_priv(crtc->dev);
 307
 308	if (sou->defined) {
 309		ret = vmw_sou_fifo_destroy(dev_priv, sou);
 310		if (ret)
 311			DRM_ERROR("Failed to destroy Screen Object\n");
 312	}
 313}
 314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 315static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
 316	.gamma_set = vmw_du_crtc_gamma_set,
 317	.destroy = vmw_sou_crtc_destroy,
 318	.reset = vmw_du_crtc_reset,
 319	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
 320	.atomic_destroy_state = vmw_du_crtc_destroy_state,
 321	.set_config = drm_atomic_helper_set_config,
 322	.page_flip = drm_atomic_helper_page_flip,
 323};
 324
 325/*
 326 * Screen Object Display Unit encoder functions
 327 */
 328
 329static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
 330{
 331	vmw_sou_destroy(vmw_encoder_to_sou(encoder));
 332}
 333
 334static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
 335	.destroy = vmw_sou_encoder_destroy,
 336};
 337
 338/*
 339 * Screen Object Display Unit connector functions
 340 */
 341
 342static void vmw_sou_connector_destroy(struct drm_connector *connector)
 343{
 344	vmw_sou_destroy(vmw_connector_to_sou(connector));
 345}
 346
 347static const struct drm_connector_funcs vmw_sou_connector_funcs = {
 348	.dpms = vmw_du_connector_dpms,
 349	.detect = vmw_du_connector_detect,
 350	.fill_modes = vmw_du_connector_fill_modes,
 
 351	.destroy = vmw_sou_connector_destroy,
 352	.reset = vmw_du_connector_reset,
 353	.atomic_duplicate_state = vmw_du_connector_duplicate_state,
 354	.atomic_destroy_state = vmw_du_connector_destroy_state,
 
 
 355};
 356
 357
 358static const struct
 359drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
 
 360};
 361
 362
 363
 364/*
 365 * Screen Object Display Plane Functions
 366 */
 367
 368/**
 369 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
 370 *
 371 * @plane:  display plane
 372 * @old_state: Contains the FB to clean up
 373 *
 374 * Unpins the display surface
 375 *
 376 * Returns 0 on success
 377 */
 378static void
 379vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
 380				 struct drm_plane_state *old_state)
 381{
 382	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
 383	struct drm_crtc *crtc = plane->state->crtc ?
 384		plane->state->crtc : old_state->crtc;
 385
 386	if (vps->bo)
 387		vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
 388	vmw_bo_unreference(&vps->bo);
 389	vps->bo_size = 0;
 390
 391	vmw_du_plane_cleanup_fb(plane, old_state);
 392}
 393
 394
 395/**
 396 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
 397 *
 398 * @plane:  display plane
 399 * @new_state: info on the new plane state, including the FB
 400 *
 401 * The SOU backing buffer is our equivalent of the display plane.
 402 *
 403 * Returns 0 on success
 404 */
 405static int
 406vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
 407				 struct drm_plane_state *new_state)
 408{
 409	struct drm_framebuffer *new_fb = new_state->fb;
 410	struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
 411	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
 412	struct vmw_private *dev_priv;
 
 413	int ret;
 414	struct vmw_bo_params bo_params = {
 415		.domain = VMW_BO_DOMAIN_VRAM,
 416		.busy_domain = VMW_BO_DOMAIN_VRAM,
 417		.bo_type = ttm_bo_type_device,
 418		.pin = true
 419	};
 420
 421	if (!new_fb) {
 422		vmw_bo_unreference(&vps->bo);
 423		vps->bo_size = 0;
 424
 425		return 0;
 426	}
 427
 428	bo_params.size = new_state->crtc_w * new_state->crtc_h * 4;
 429	dev_priv = vmw_priv(crtc->dev);
 430
 431	if (vps->bo) {
 432		if (vps->bo_size == bo_params.size) {
 433			/*
 434			 * Note that this might temporarily up the pin-count
 435			 * to 2, until cleanup_fb() is called.
 436			 */
 437			return vmw_bo_pin_in_vram(dev_priv, vps->bo,
 438						      true);
 439		}
 440
 441		vmw_bo_unreference(&vps->bo);
 442		vps->bo_size = 0;
 443	}
 444
 
 
 
 
 445	vmw_svga_enable(dev_priv);
 446
 447	/* After we have alloced the backing store might not be able to
 448	 * resume the overlays, this is preferred to failing to alloc.
 449	 */
 450	vmw_overlay_pause_all(dev_priv);
 451	ret = vmw_bo_create(dev_priv, &bo_params, &vps->bo);
 
 
 452	vmw_overlay_resume_all(dev_priv);
 453	if (ret)
 
 454		return ret;
 
 455
 456	vps->bo_size = bo_params.size;
 457
 458	/*
 459	 * TTM already thinks the buffer is pinned, but make sure the
 460	 * pin_count is upped.
 461	 */
 462	return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
 463}
 464
 465static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
 466				     uint32_t num_hits)
 467{
 468	return sizeof(struct vmw_kms_sou_define_gmrfb) +
 469		sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
 470}
 471
 472static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
 473					void *cmd)
 474{
 475	struct vmw_framebuffer_bo *vfbbo =
 476		container_of(update->vfb, typeof(*vfbbo), base);
 477	struct vmw_kms_sou_define_gmrfb *gmr = cmd;
 478	int depth = update->vfb->base.format->depth;
 479
 480	/* Emulate RGBA support, contrary to svga_reg.h this is not
 481	 * supported by hosts. This is only a problem if we are reading
 482	 * this value later and expecting what we uploaded back.
 483	 */
 484	if (depth == 32)
 485		depth = 24;
 486
 487	gmr->header = SVGA_CMD_DEFINE_GMRFB;
 488
 489	gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
 490	gmr->body.format.colorDepth = depth;
 491	gmr->body.format.reserved = 0;
 492	gmr->body.bytesPerLine = update->vfb->base.pitches[0];
 493	vmw_bo_get_guest_ptr(&vfbbo->buffer->tbo, &gmr->body.ptr);
 494
 495	return sizeof(*gmr);
 496}
 497
 498static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane  *update,
 499					 void *cmd, struct drm_rect *clip,
 500					 uint32_t fb_x, uint32_t fb_y)
 501{
 502	struct vmw_kms_sou_bo_blit *blit = cmd;
 503
 504	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
 505	blit->body.destScreenId = update->du->unit;
 506	blit->body.srcOrigin.x = fb_x;
 507	blit->body.srcOrigin.y = fb_y;
 508	blit->body.destRect.left = clip->x1;
 509	blit->body.destRect.top = clip->y1;
 510	blit->body.destRect.right = clip->x2;
 511	blit->body.destRect.bottom = clip->y2;
 512
 513	return sizeof(*blit);
 514}
 515
 516static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane  *update,
 517				      void *cmd, struct drm_rect *bb)
 518{
 519	return 0;
 520}
 521
 522/**
 523 * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
 524 * @dev_priv: Device private.
 525 * @plane: Plane state.
 526 * @old_state: Old plane state.
 527 * @vfb: Framebuffer which is blitted to display unit.
 528 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
 529 *             The returned fence pointer may be NULL in which case the device
 530 *             has already synchronized.
 531 *
 532 * Return: 0 on success or a negative error code on failure.
 533 */
 534static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
 535				   struct drm_plane *plane,
 536				   struct drm_plane_state *old_state,
 537				   struct vmw_framebuffer *vfb,
 538				   struct vmw_fence_obj **out_fence)
 539{
 540	struct vmw_du_update_plane_buffer bo_update;
 541
 542	memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
 543	bo_update.base.plane = plane;
 544	bo_update.base.old_state = old_state;
 545	bo_update.base.dev_priv = dev_priv;
 546	bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
 547	bo_update.base.vfb = vfb;
 548	bo_update.base.out_fence = out_fence;
 549	bo_update.base.mutex = NULL;
 550	bo_update.base.intr = true;
 551
 552	bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
 553	bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
 554	bo_update.base.clip = vmw_sou_bo_populate_clip;
 555	bo_update.base.post_clip = vmw_stud_bo_post_clip;
 556
 557	return vmw_du_helper_plane_update(&bo_update.base);
 558}
 559
 560static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
 561					  uint32_t num_hits)
 562{
 563	return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
 564		num_hits;
 565}
 566
 567static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
 568					     void *cmd)
 569{
 570	struct vmw_du_update_plane_surface *srf_update;
 571
 572	srf_update = container_of(update, typeof(*srf_update), base);
 573
 574	/*
 575	 * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
 576	 * its bounding box is filled before iterating over all the clips. So
 577	 * store the FIFO start address and revisit to fill the details.
 578	 */
 579	srf_update->cmd_start = cmd;
 580
 581	return 0;
 582}
 583
 584static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
 585					 void *cmd, uint32_t num_hits)
 586{
 587	struct vmw_kms_sou_dirty_cmd *blit = cmd;
 588	struct vmw_framebuffer_surface *vfbs;
 589
 590	vfbs = container_of(update->vfb, typeof(*vfbs), base);
 591
 592	blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
 593	blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
 594		num_hits;
 595
 596	blit->body.srcImage.sid = vfbs->surface->res.id;
 597	blit->body.destScreenId = update->du->unit;
 598
 599	/* Update the source and destination bounding box later in post_clip */
 600	blit->body.srcRect.left = 0;
 601	blit->body.srcRect.top = 0;
 602	blit->body.srcRect.right = 0;
 603	blit->body.srcRect.bottom = 0;
 604
 605	blit->body.destRect.left = 0;
 606	blit->body.destRect.top = 0;
 607	blit->body.destRect.right = 0;
 608	blit->body.destRect.bottom = 0;
 609
 610	return sizeof(*blit);
 611}
 612
 613static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
 614					  void *cmd, struct drm_rect *clip,
 615					  uint32_t src_x, uint32_t src_y)
 616{
 617	SVGASignedRect *rect = cmd;
 618
 619	/*
 620	 * rects are relative to dest bounding box rect on screen object, so
 621	 * translate to it later in post_clip
 622	 */
 623	rect->left = clip->x1;
 624	rect->top = clip->y1;
 625	rect->right = clip->x2;
 626	rect->bottom = clip->y2;
 627
 628	return sizeof(*rect);
 629}
 630
 631static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
 632					  void *cmd, struct drm_rect *bb)
 633{
 634	struct vmw_du_update_plane_surface *srf_update;
 635	struct drm_plane_state *state = update->plane->state;
 636	struct drm_rect src_bb;
 637	struct vmw_kms_sou_dirty_cmd *blit;
 638	SVGASignedRect *rect;
 639	uint32_t num_hits;
 640	int translate_src_x;
 641	int translate_src_y;
 642	int i;
 643
 644	srf_update = container_of(update, typeof(*srf_update), base);
 645
 646	blit = srf_update->cmd_start;
 647	rect = (SVGASignedRect *)&blit[1];
 648
 649	num_hits = (blit->header.size - sizeof(blit->body))/
 650		sizeof(SVGASignedRect);
 651
 652	src_bb = *bb;
 653
 654	/* To translate bb back to fb src coord */
 655	translate_src_x = (state->src_x >> 16) - state->crtc_x;
 656	translate_src_y = (state->src_y >> 16) - state->crtc_y;
 657
 658	drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
 659
 660	blit->body.srcRect.left = src_bb.x1;
 661	blit->body.srcRect.top = src_bb.y1;
 662	blit->body.srcRect.right = src_bb.x2;
 663	blit->body.srcRect.bottom = src_bb.y2;
 664
 665	blit->body.destRect.left = bb->x1;
 666	blit->body.destRect.top = bb->y1;
 667	blit->body.destRect.right = bb->x2;
 668	blit->body.destRect.bottom = bb->y2;
 669
 670	/* rects are relative to dest bb rect */
 671	for (i = 0; i < num_hits; i++) {
 672		rect->left -= bb->x1;
 673		rect->top -= bb->y1;
 674		rect->right -= bb->x1;
 675		rect->bottom -= bb->y1;
 676		rect++;
 677	}
 678
 679	return 0;
 680}
 681
 682/**
 683 * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
 684 * @dev_priv: Device private.
 685 * @plane: Plane state.
 686 * @old_state: Old plane state.
 687 * @vfb: Framebuffer which is blitted to display unit
 688 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
 689 *             The returned fence pointer may be NULL in which case the device
 690 *             has already synchronized.
 691 *
 692 * Return: 0 on success or a negative error code on failure.
 693 */
 694static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
 695					struct drm_plane *plane,
 696					struct drm_plane_state *old_state,
 697					struct vmw_framebuffer *vfb,
 698					struct vmw_fence_obj **out_fence)
 699{
 700	struct vmw_du_update_plane_surface srf_update;
 701
 702	memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
 703	srf_update.base.plane = plane;
 704	srf_update.base.old_state = old_state;
 705	srf_update.base.dev_priv = dev_priv;
 706	srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
 707	srf_update.base.vfb = vfb;
 708	srf_update.base.out_fence = out_fence;
 709	srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
 710	srf_update.base.intr = true;
 711
 712	srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
 713	srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
 714	srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
 715	srf_update.base.clip = vmw_sou_surface_clip_rect;
 716	srf_update.base.post_clip = vmw_sou_surface_post_clip;
 717
 718	return vmw_du_helper_plane_update(&srf_update.base);
 719}
 720
 721static void
 722vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
 723				    struct drm_atomic_state *state)
 724{
 725	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
 726	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
 727	struct drm_crtc *crtc = new_state->crtc;
 728	struct vmw_fence_obj *fence = NULL;
 729	int ret;
 730
 731	/* In case of device error, maintain consistent atomic state */
 732	if (crtc && new_state->fb) {
 733		struct vmw_private *dev_priv = vmw_priv(crtc->dev);
 734		struct vmw_framebuffer *vfb =
 735			vmw_framebuffer_to_vfb(new_state->fb);
 
 736
 737		if (vfb->bo)
 738			ret = vmw_sou_plane_update_bo(dev_priv, plane,
 739						      old_state, vfb, &fence);
 
 
 
 
 
 
 740		else
 741			ret = vmw_sou_plane_update_surface(dev_priv, plane,
 742							   old_state, vfb,
 743							   &fence);
 
 
 
 
 
 744		if (ret != 0)
 745			DRM_ERROR("Failed to update screen.\n");
 
 
 746	} else {
 747		/* Do nothing when fb and crtc is NULL (blank crtc) */
 
 
 
 
 
 748		return;
 749	}
 750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 751	if (fence)
 752		vmw_fence_obj_unreference(&fence);
 753}
 754
 755
 756static const struct drm_plane_funcs vmw_sou_plane_funcs = {
 757	.update_plane = drm_atomic_helper_update_plane,
 758	.disable_plane = drm_atomic_helper_disable_plane,
 759	.destroy = vmw_du_primary_plane_destroy,
 760	.reset = vmw_du_plane_reset,
 761	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
 762	.atomic_destroy_state = vmw_du_plane_destroy_state,
 763};
 764
 765static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
 766	.update_plane = drm_atomic_helper_update_plane,
 767	.disable_plane = drm_atomic_helper_disable_plane,
 768	.destroy = vmw_du_cursor_plane_destroy,
 769	.reset = vmw_du_plane_reset,
 770	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
 771	.atomic_destroy_state = vmw_du_plane_destroy_state,
 772};
 773
 774/*
 775 * Atomic Helpers
 776 */
 777static const struct
 778drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
 779	.atomic_check = vmw_du_cursor_plane_atomic_check,
 780	.atomic_update = vmw_du_cursor_plane_atomic_update,
 781	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
 782	.cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
 783};
 784
 785static const struct
 786drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
 787	.atomic_check = vmw_du_primary_plane_atomic_check,
 788	.atomic_update = vmw_sou_primary_plane_atomic_update,
 789	.prepare_fb = vmw_sou_primary_plane_prepare_fb,
 790	.cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
 791};
 792
 793static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
 794	.prepare = vmw_sou_crtc_helper_prepare,
 795	.mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
 796	.atomic_check = vmw_du_crtc_atomic_check,
 797	.atomic_begin = vmw_du_crtc_atomic_begin,
 798	.atomic_flush = vmw_du_crtc_atomic_flush,
 799	.atomic_enable = vmw_sou_crtc_atomic_enable,
 800	.atomic_disable = vmw_sou_crtc_atomic_disable,
 801};
 802
 803
 804static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
 805{
 806	struct vmw_screen_object_unit *sou;
 807	struct drm_device *dev = &dev_priv->drm;
 808	struct drm_connector *connector;
 809	struct drm_encoder *encoder;
 810	struct drm_plane *primary;
 811	struct vmw_cursor_plane *cursor;
 812	struct drm_crtc *crtc;
 813	int ret;
 814
 815	sou = kzalloc(sizeof(*sou), GFP_KERNEL);
 816	if (!sou)
 817		return -ENOMEM;
 818
 819	sou->base.unit = unit;
 820	crtc = &sou->base.crtc;
 821	encoder = &sou->base.encoder;
 822	connector = &sou->base.connector;
 823	primary = &sou->base.primary;
 824	cursor = &sou->base.cursor;
 825
 
 826	sou->base.pref_active = (unit == 0);
 827	sou->base.pref_width = dev_priv->initial_width;
 828	sou->base.pref_height = dev_priv->initial_height;
 829	sou->base.pref_mode = NULL;
 830
 831	/*
 832	 * Remove this after enabling atomic because property values can
 833	 * only exist in a state object
 834	 */
 835	sou->base.is_implicit = false;
 836
 837	/* Initialize primary plane */
 838	ret = drm_universal_plane_init(dev, primary,
 
 
 839				       0, &vmw_sou_plane_funcs,
 840				       vmw_primary_plane_formats,
 841				       ARRAY_SIZE(vmw_primary_plane_formats),
 842				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
 843	if (ret) {
 844		DRM_ERROR("Failed to initialize primary plane");
 845		goto err_free;
 846	}
 847
 848	drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
 849	drm_plane_enable_fb_damage_clips(primary);
 850
 851	/* Initialize cursor plane */
 852	ret = drm_universal_plane_init(dev, &cursor->base,
 
 
 853			0, &vmw_sou_cursor_funcs,
 854			vmw_cursor_plane_formats,
 855			ARRAY_SIZE(vmw_cursor_plane_formats),
 856			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
 857	if (ret) {
 858		DRM_ERROR("Failed to initialize cursor plane");
 859		drm_plane_cleanup(&sou->base.primary);
 860		goto err_free;
 861	}
 862
 863	drm_plane_helper_add(&cursor->base, &vmw_sou_cursor_plane_helper_funcs);
 864
 
 865	ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
 866				 DRM_MODE_CONNECTOR_VIRTUAL);
 867	if (ret) {
 868		DRM_ERROR("Failed to initialize connector\n");
 869		goto err_free;
 870	}
 871
 872	drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
 873	connector->status = vmw_du_connector_detect(connector, true);
 
 
 874
 875	ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
 876			       DRM_MODE_ENCODER_VIRTUAL, NULL);
 877	if (ret) {
 878		DRM_ERROR("Failed to initialize encoder\n");
 879		goto err_free_connector;
 880	}
 881
 882	(void) drm_connector_attach_encoder(connector, encoder);
 883	encoder->possible_crtcs = (1 << unit);
 884	encoder->possible_clones = 0;
 885
 886	ret = drm_connector_register(connector);
 887	if (ret) {
 888		DRM_ERROR("Failed to register connector\n");
 889		goto err_free_encoder;
 890	}
 891
 892	ret = drm_crtc_init_with_planes(dev, crtc, primary,
 893					&cursor->base,
 
 
 894					&vmw_screen_object_crtc_funcs, NULL);
 895	if (ret) {
 896		DRM_ERROR("Failed to initialize CRTC\n");
 897		goto err_free_unregister;
 898	}
 899
 900	drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
 901
 902	drm_mode_crtc_set_gamma_size(crtc, 256);
 903
 904	drm_object_attach_property(&connector->base,
 905				   dev_priv->hotplug_mode_update_property, 1);
 906	drm_object_attach_property(&connector->base,
 907				   dev->mode_config.suggested_x_property, 0);
 908	drm_object_attach_property(&connector->base,
 909				   dev->mode_config.suggested_y_property, 0);
 
 
 
 
 
 
 910	return 0;
 911
 912err_free_unregister:
 913	drm_connector_unregister(connector);
 914err_free_encoder:
 915	drm_encoder_cleanup(encoder);
 916err_free_connector:
 917	drm_connector_cleanup(connector);
 918err_free:
 919	kfree(sou);
 920	return ret;
 921}
 922
 923int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
 924{
 925	struct drm_device *dev = &dev_priv->drm;
 926	int i;
 927
 928	/* Screen objects won't work if GMR's aren't available */
 929	if (!dev_priv->has_gmr)
 930		return -ENOSYS;
 931
 932	if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
 
 
 933		return -ENOSYS;
 934	}
 935
 
 
 
 
 
 
 
 
 
 
 936	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
 937		vmw_sou_init(dev_priv, i);
 938
 939	dev_priv->active_display_unit = vmw_du_screen_object;
 940
 941	drm_mode_config_reset(dev);
 942
 943	return 0;
 944}
 945
 946static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
 947				  struct vmw_framebuffer *framebuffer)
 948{
 949	struct vmw_bo *buf =
 950		container_of(framebuffer, struct vmw_framebuffer_bo,
 951			     base)->buffer;
 952	int depth = framebuffer->base.format->depth;
 953	struct {
 954		uint32_t header;
 955		SVGAFifoCmdDefineGMRFB body;
 956	} *cmd;
 957
 958	/* Emulate RGBA support, contrary to svga_reg.h this is not
 959	 * supported by hosts. This is only a problem if we are reading
 960	 * this value later and expecting what we uploaded back.
 961	 */
 962	if (depth == 32)
 963		depth = 24;
 964
 965	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
 966	if (!cmd)
 
 967		return -ENOMEM;
 
 968
 969	cmd->header = SVGA_CMD_DEFINE_GMRFB;
 970	cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
 971	cmd->body.format.colorDepth = depth;
 972	cmd->body.format.reserved = 0;
 973	cmd->body.bytesPerLine = framebuffer->base.pitches[0];
 974	/* Buffer is reserved in vram or GMR */
 975	vmw_bo_get_guest_ptr(&buf->tbo, &cmd->body.ptr);
 976	vmw_cmd_commit(dev_priv, sizeof(*cmd));
 977
 978	return 0;
 979}
 980
 981/**
 982 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
 983 * blit surface to screen command.
 984 *
 985 * @dirty: The closure structure.
 986 *
 987 * Fills in the missing fields in the command, and translates the cliprects
 988 * to match the destination bounding box encoded.
 989 */
 990static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
 991{
 992	struct vmw_kms_sou_surface_dirty *sdirty =
 993		container_of(dirty, typeof(*sdirty), base);
 994	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
 995	s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
 996	s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
 997	size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
 998	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
 999	int i;
1000
1001	if (!dirty->num_hits) {
1002		vmw_cmd_commit(dirty->dev_priv, 0);
1003		return;
1004	}
1005
1006	cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1007	cmd->header.size = sizeof(cmd->body) + region_size;
1008
1009	/*
1010	 * Use the destination bounding box to specify destination - and
1011	 * source bounding regions.
1012	 */
1013	cmd->body.destRect.left = sdirty->left;
1014	cmd->body.destRect.right = sdirty->right;
1015	cmd->body.destRect.top = sdirty->top;
1016	cmd->body.destRect.bottom = sdirty->bottom;
1017
1018	cmd->body.srcRect.left = sdirty->left + trans_x;
1019	cmd->body.srcRect.right = sdirty->right + trans_x;
1020	cmd->body.srcRect.top = sdirty->top + trans_y;
1021	cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1022
1023	cmd->body.srcImage.sid = sdirty->sid;
1024	cmd->body.destScreenId = dirty->unit->unit;
1025
1026	/* Blits are relative to the destination rect. Translate. */
1027	for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1028		blit->left -= sdirty->left;
1029		blit->right -= sdirty->left;
1030		blit->top -= sdirty->top;
1031		blit->bottom -= sdirty->top;
1032	}
1033
1034	vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1035
1036	sdirty->left = sdirty->top = S32_MAX;
1037	sdirty->right = sdirty->bottom = S32_MIN;
1038}
1039
1040/**
1041 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1042 *
1043 * @dirty: The closure structure
1044 *
1045 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1046 * BLIT_SURFACE_TO_SCREEN command.
1047 */
1048static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1049{
1050	struct vmw_kms_sou_surface_dirty *sdirty =
1051		container_of(dirty, typeof(*sdirty), base);
1052	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1053	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1054
1055	/* Destination rect. */
1056	blit += dirty->num_hits;
1057	blit->left = dirty->unit_x1;
1058	blit->top = dirty->unit_y1;
1059	blit->right = dirty->unit_x2;
1060	blit->bottom = dirty->unit_y2;
1061
1062	/* Destination bounding box */
1063	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1064	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1065	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1066	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1067
1068	dirty->num_hits++;
1069}
1070
1071/**
1072 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1073 *
1074 * @dev_priv: Pointer to the device private structure.
1075 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1076 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1077 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1078 * be NULL.
1079 * @srf: Pointer to surface to blit from. If NULL, the surface attached
1080 * to @framebuffer will be used.
1081 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1082 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1083 * @num_clips: Number of clip rects in @clips.
1084 * @inc: Increment to use when looping over @clips.
1085 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1086 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1087 * case the device has already synchronized.
1088 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1089 *
1090 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1091 * interrupted.
1092 */
1093int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1094				 struct vmw_framebuffer *framebuffer,
1095				 struct drm_clip_rect *clips,
1096				 struct drm_vmw_rect *vclips,
1097				 struct vmw_resource *srf,
1098				 s32 dest_x,
1099				 s32 dest_y,
1100				 unsigned num_clips, int inc,
1101				 struct vmw_fence_obj **out_fence,
1102				 struct drm_crtc *crtc)
1103{
1104	struct vmw_framebuffer_surface *vfbs =
1105		container_of(framebuffer, typeof(*vfbs), base);
1106	struct vmw_kms_sou_surface_dirty sdirty;
1107	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1108	int ret;
1109
1110	if (!srf)
1111		srf = &vfbs->surface->res;
1112
1113	ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
1114					  NULL, NULL);
1115	if (ret)
1116		return ret;
1117
1118	ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1119	if (ret)
1120		goto out_unref;
1121
1122	sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1123	sdirty.base.clip = vmw_sou_surface_clip;
1124	sdirty.base.dev_priv = dev_priv;
1125	sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1126	  sizeof(SVGASignedRect) * num_clips;
1127	sdirty.base.crtc = crtc;
1128
1129	sdirty.sid = srf->id;
1130	sdirty.left = sdirty.top = S32_MAX;
1131	sdirty.right = sdirty.bottom = S32_MIN;
1132	sdirty.dst_x = dest_x;
1133	sdirty.dst_y = dest_y;
1134
1135	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1136				   dest_x, dest_y, num_clips, inc,
1137				   &sdirty.base);
1138	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1139					 NULL);
1140
1141	return ret;
1142
1143out_unref:
1144	vmw_validation_unref_lists(&val_ctx);
1145	return ret;
1146}
1147
1148/**
1149 * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1150 *
1151 * @dirty: The closure structure.
1152 *
1153 * Commits a previously built command buffer of readback clips.
1154 */
1155static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1156{
1157	if (!dirty->num_hits) {
1158		vmw_cmd_commit(dirty->dev_priv, 0);
1159		return;
1160	}
1161
1162	vmw_cmd_commit(dirty->dev_priv,
1163			sizeof(struct vmw_kms_sou_bo_blit) *
1164			dirty->num_hits);
1165}
1166
1167/**
1168 * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1169 *
1170 * @dirty: The closure structure
1171 *
1172 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1173 */
1174static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1175{
1176	struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1177
1178	blit += dirty->num_hits;
1179	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1180	blit->body.destScreenId = dirty->unit->unit;
1181	blit->body.srcOrigin.x = dirty->fb_x;
1182	blit->body.srcOrigin.y = dirty->fb_y;
1183	blit->body.destRect.left = dirty->unit_x1;
1184	blit->body.destRect.top = dirty->unit_y1;
1185	blit->body.destRect.right = dirty->unit_x2;
1186	blit->body.destRect.bottom = dirty->unit_y2;
1187	dirty->num_hits++;
1188}
1189
1190/**
1191 * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1192 *
1193 * @dev_priv: Pointer to the device private structure.
1194 * @framebuffer: Pointer to the buffer-object backed framebuffer.
1195 * @clips: Array of clip rects.
1196 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1197 * be NULL.
1198 * @num_clips: Number of clip rects in @clips.
1199 * @increment: Increment to use when looping over @clips.
1200 * @interruptible: Whether to perform waits interruptible if possible.
1201 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1202 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1203 * case the device has already synchronized.
1204 * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1205 *
1206 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1207 * interrupted.
1208 */
1209int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1210				struct vmw_framebuffer *framebuffer,
1211				struct drm_clip_rect *clips,
1212				struct drm_vmw_rect *vclips,
1213				unsigned num_clips, int increment,
1214				bool interruptible,
1215				struct vmw_fence_obj **out_fence,
1216				struct drm_crtc *crtc)
1217{
1218	struct vmw_bo *buf =
1219		container_of(framebuffer, struct vmw_framebuffer_bo,
1220			     base)->buffer;
1221	struct vmw_kms_dirty dirty;
1222	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1223	int ret;
1224
1225	vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
1226			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
1227	ret = vmw_validation_add_bo(&val_ctx, buf);
1228	if (ret)
1229		return ret;
1230
1231	ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1232	if (ret)
1233		goto out_unref;
1234
1235	ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1236	if (unlikely(ret != 0))
1237		goto out_revert;
1238
1239	dirty.crtc = crtc;
1240	dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1241	dirty.clip = vmw_sou_bo_clip;
1242	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1243		num_clips;
1244	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1245				   0, 0, num_clips, increment, &dirty);
1246	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1247					 NULL);
1248
1249	return ret;
1250
1251out_revert:
1252	vmw_validation_revert(&val_ctx);
1253out_unref:
1254	vmw_validation_unref_lists(&val_ctx);
1255
1256	return ret;
1257}
1258
1259
1260/**
1261 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1262 *
1263 * @dirty: The closure structure.
1264 *
1265 * Commits a previously built command buffer of readback clips.
1266 */
1267static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1268{
1269	if (!dirty->num_hits) {
1270		vmw_cmd_commit(dirty->dev_priv, 0);
1271		return;
1272	}
1273
1274	vmw_cmd_commit(dirty->dev_priv,
1275			sizeof(struct vmw_kms_sou_readback_blit) *
1276			dirty->num_hits);
1277}
1278
1279/**
1280 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1281 *
1282 * @dirty: The closure structure
1283 *
1284 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1285 */
1286static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1287{
1288	struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1289
1290	blit += dirty->num_hits;
1291	blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1292	blit->body.srcScreenId = dirty->unit->unit;
1293	blit->body.destOrigin.x = dirty->fb_x;
1294	blit->body.destOrigin.y = dirty->fb_y;
1295	blit->body.srcRect.left = dirty->unit_x1;
1296	blit->body.srcRect.top = dirty->unit_y1;
1297	blit->body.srcRect.right = dirty->unit_x2;
1298	blit->body.srcRect.bottom = dirty->unit_y2;
1299	dirty->num_hits++;
1300}
1301
1302/**
1303 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1304 * a buffer-object backed framebuffer.
1305 *
1306 * @dev_priv: Pointer to the device private structure.
1307 * @file_priv: Pointer to a struct drm_file identifying the caller.
1308 * Must be set to NULL if @user_fence_rep is NULL.
1309 * @vfb: Pointer to the buffer-object backed framebuffer.
1310 * @user_fence_rep: User-space provided structure for fence information.
1311 * Must be set to non-NULL if @file_priv is non-NULL.
1312 * @vclips: Array of clip rects.
1313 * @num_clips: Number of clip rects in @vclips.
1314 * @crtc: If crtc is passed, readback on that crtc only.
1315 *
1316 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1317 * interrupted.
1318 */
1319int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1320			 struct drm_file *file_priv,
1321			 struct vmw_framebuffer *vfb,
1322			 struct drm_vmw_fence_rep __user *user_fence_rep,
1323			 struct drm_vmw_rect *vclips,
1324			 uint32_t num_clips,
1325			 struct drm_crtc *crtc)
1326{
1327	struct vmw_bo *buf =
1328		container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1329	struct vmw_kms_dirty dirty;
1330	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1331	int ret;
1332
1333	vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
1334			     VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
1335	ret = vmw_validation_add_bo(&val_ctx, buf);
1336	if (ret)
1337		return ret;
1338
1339	ret = vmw_validation_prepare(&val_ctx, NULL, true);
1340	if (ret)
1341		goto out_unref;
1342
1343	ret = do_bo_define_gmrfb(dev_priv, vfb);
1344	if (unlikely(ret != 0))
1345		goto out_revert;
1346
1347	dirty.crtc = crtc;
1348	dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1349	dirty.clip = vmw_sou_readback_clip;
1350	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1351		num_clips;
1352	ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1353				   0, 0, num_clips, 1, &dirty);
1354	vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1355					 user_fence_rep);
1356
1357	return ret;
1358
1359out_revert:
1360	vmw_validation_revert(&val_ctx);
1361out_unref:
1362	vmw_validation_unref_lists(&val_ctx);
1363
1364	return ret;
1365}
v4.17
 
   1/**************************************************************************
   2 *
   3 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
   4 * All Rights Reserved.
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a
   7 * copy of this software and associated documentation files (the
   8 * "Software"), to deal in the Software without restriction, including
   9 * without limitation the rights to use, copy, modify, merge, publish,
  10 * distribute, sub license, and/or sell copies of the Software, and to
  11 * permit persons to whom the Software is furnished to do so, subject to
  12 * the following conditions:
  13 *
  14 * The above copyright notice and this permission notice (including the
  15 * next paragraph) shall be included in all copies or substantial portions
  16 * of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25 *
  26 **************************************************************************/
  27
 
  28#include "vmwgfx_kms.h"
  29#include <drm/drm_plane_helper.h>
  30#include <drm/drm_atomic.h>
  31#include <drm/drm_atomic_helper.h>
  32
 
  33
  34#define vmw_crtc_to_sou(x) \
  35	container_of(x, struct vmw_screen_object_unit, base.crtc)
  36#define vmw_encoder_to_sou(x) \
  37	container_of(x, struct vmw_screen_object_unit, base.encoder)
  38#define vmw_connector_to_sou(x) \
  39	container_of(x, struct vmw_screen_object_unit, base.connector)
  40
  41/**
  42 * struct vmw_kms_sou_surface_dirty - Closure structure for
  43 * blit surface to screen command.
  44 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
  45 * @left: Left side of bounding box.
  46 * @right: Right side of bounding box.
  47 * @top: Top side of bounding box.
  48 * @bottom: Bottom side of bounding box.
  49 * @dst_x: Difference between source clip rects and framebuffer coordinates.
  50 * @dst_y: Difference between source clip rects and framebuffer coordinates.
  51 * @sid: Surface id of surface to copy from.
  52 */
  53struct vmw_kms_sou_surface_dirty {
  54	struct vmw_kms_dirty base;
  55	s32 left, right, top, bottom;
  56	s32 dst_x, dst_y;
  57	u32 sid;
  58};
  59
  60/*
  61 * SVGA commands that are used by this code. Please see the device headers
  62 * for explanation.
  63 */
  64struct vmw_kms_sou_readback_blit {
  65	uint32 header;
  66	SVGAFifoCmdBlitScreenToGMRFB body;
  67};
  68
  69struct vmw_kms_sou_dmabuf_blit {
  70	uint32 header;
  71	SVGAFifoCmdBlitGMRFBToScreen body;
  72};
  73
  74struct vmw_kms_sou_dirty_cmd {
  75	SVGA3dCmdHeader header;
  76	SVGA3dCmdBlitSurfaceToScreen body;
  77};
  78
  79/**
 
 
 
 
 
  80 * Display unit using screen objects.
  81 */
  82struct vmw_screen_object_unit {
  83	struct vmw_display_unit base;
  84
  85	unsigned long buffer_size; /**< Size of allocated buffer */
  86	struct vmw_dma_buffer *buffer; /**< Backing store buffer */
  87
  88	bool defined;
  89};
  90
  91static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
  92{
  93	vmw_du_cleanup(&sou->base);
  94	kfree(sou);
  95}
  96
  97
  98/*
  99 * Screen Object Display Unit CRTC functions
 100 */
 101
 102static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
 103{
 104	vmw_sou_destroy(vmw_crtc_to_sou(crtc));
 105}
 106
 107/**
 108 * Send the fifo command to create a screen.
 109 */
 110static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
 111			       struct vmw_screen_object_unit *sou,
 112			       uint32_t x, uint32_t y,
 113			       struct drm_display_mode *mode)
 114{
 115	size_t fifo_size;
 116
 117	struct {
 118		struct {
 119			uint32_t cmdType;
 120		} header;
 121		SVGAScreenObject obj;
 122	} *cmd;
 123
 124	BUG_ON(!sou->buffer);
 125
 126	fifo_size = sizeof(*cmd);
 127	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
 128	/* The hardware has hung, nothing we can do about it here. */
 129	if (unlikely(cmd == NULL)) {
 130		DRM_ERROR("Fifo reserve failed.\n");
 131		return -ENOMEM;
 132	}
 133
 134	memset(cmd, 0, fifo_size);
 135	cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
 136	cmd->obj.structSize = sizeof(SVGAScreenObject);
 137	cmd->obj.id = sou->base.unit;
 138	cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
 139		(sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
 140	cmd->obj.size.width = mode->hdisplay;
 141	cmd->obj.size.height = mode->vdisplay;
 142	if (sou->base.is_implicit) {
 143		cmd->obj.root.x = x;
 144		cmd->obj.root.y = y;
 145	} else {
 146		cmd->obj.root.x = sou->base.gui_x;
 147		cmd->obj.root.y = sou->base.gui_y;
 148	}
 149	sou->base.set_gui_x = cmd->obj.root.x;
 150	sou->base.set_gui_y = cmd->obj.root.y;
 151
 152	/* Ok to assume that buffer is pinned in vram */
 153	vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
 154	cmd->obj.backingStore.pitch = mode->hdisplay * 4;
 155
 156	vmw_fifo_commit(dev_priv, fifo_size);
 157
 158	sou->defined = true;
 159
 160	return 0;
 161}
 162
 163/**
 164 * Send the fifo command to destroy a screen.
 165 */
 166static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
 167				struct vmw_screen_object_unit *sou)
 168{
 169	size_t fifo_size;
 170	int ret;
 171
 172	struct {
 173		struct {
 174			uint32_t cmdType;
 175		} header;
 176		SVGAFifoCmdDestroyScreen body;
 177	} *cmd;
 178
 179	/* no need to do anything */
 180	if (unlikely(!sou->defined))
 181		return 0;
 182
 183	fifo_size = sizeof(*cmd);
 184	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
 185	/* the hardware has hung, nothing we can do about it here */
 186	if (unlikely(cmd == NULL)) {
 187		DRM_ERROR("Fifo reserve failed.\n");
 188		return -ENOMEM;
 189	}
 190
 191	memset(cmd, 0, fifo_size);
 192	cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
 193	cmd->body.screenId = sou->base.unit;
 194
 195	vmw_fifo_commit(dev_priv, fifo_size);
 196
 197	/* Force sync */
 198	ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
 199	if (unlikely(ret != 0))
 200		DRM_ERROR("Failed to sync with HW");
 201	else
 202		sou->defined = false;
 203
 204	return ret;
 205}
 206
 207/**
 208 * vmw_sou_crtc_mode_set_nofb - Create new screen
 209 *
 210 * @crtc: CRTC associated with the new screen
 211 *
 212 * This function creates/destroys a screen.  This function cannot fail, so if
 213 * somehow we run into a failure, just do the best we can to get out.
 214 */
 215static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
 216{
 217	struct vmw_private *dev_priv;
 218	struct vmw_screen_object_unit *sou;
 219	struct vmw_framebuffer *vfb;
 220	struct drm_framebuffer *fb;
 221	struct drm_plane_state *ps;
 222	struct vmw_plane_state *vps;
 223	int ret;
 224
 225
 226	sou      = vmw_crtc_to_sou(crtc);
 227	dev_priv = vmw_priv(crtc->dev);
 228	ps       = crtc->primary->state;
 229	fb       = ps->fb;
 230	vps      = vmw_plane_state_to_vps(ps);
 231
 232	vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
 233
 234	if (sou->defined) {
 235		ret = vmw_sou_fifo_destroy(dev_priv, sou);
 236		if (ret) {
 237			DRM_ERROR("Failed to destroy Screen Object\n");
 238			return;
 239		}
 240	}
 241
 242	if (vfb) {
 243		sou->buffer = vps->dmabuf;
 244		sou->buffer_size = vps->dmabuf_size;
 
 
 
 
 
 
 
 245
 246		ret = vmw_sou_fifo_create(dev_priv, sou, crtc->x, crtc->y,
 247					  &crtc->mode);
 
 
 248		if (ret)
 249			DRM_ERROR("Failed to define Screen Object %dx%d\n",
 250				  crtc->x, crtc->y);
 251
 252		vmw_kms_add_active(dev_priv, &sou->base, vfb);
 253	} else {
 254		sou->buffer = NULL;
 255		sou->buffer_size = 0;
 256
 257		vmw_kms_del_active(dev_priv, &sou->base);
 258	}
 259}
 260
 261/**
 262 * vmw_sou_crtc_helper_prepare - Noop
 263 *
 264 * @crtc: CRTC associated with the new screen
 265 *
 266 * Prepares the CRTC for a mode set, but we don't need to do anything here.
 267 */
 268static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
 269{
 270}
 271
 272/**
 273 * vmw_sou_crtc_atomic_enable - Noop
 274 *
 275 * @crtc: CRTC associated with the new screen
 
 276 *
 277 * This is called after a mode set has been completed.
 278 */
 279static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
 280				       struct drm_crtc_state *old_state)
 281{
 282}
 283
 284/**
 285 * vmw_sou_crtc_atomic_disable - Turns off CRTC
 286 *
 287 * @crtc: CRTC to be turned off
 
 288 */
 289static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
 290					struct drm_crtc_state *old_state)
 291{
 292	struct vmw_private *dev_priv;
 293	struct vmw_screen_object_unit *sou;
 294	int ret;
 295
 296
 297	if (!crtc) {
 298		DRM_ERROR("CRTC is NULL\n");
 299		return;
 300	}
 301
 302	sou = vmw_crtc_to_sou(crtc);
 303	dev_priv = vmw_priv(crtc->dev);
 304
 305	if (sou->defined) {
 306		ret = vmw_sou_fifo_destroy(dev_priv, sou);
 307		if (ret)
 308			DRM_ERROR("Failed to destroy Screen Object\n");
 309	}
 310}
 311
 312static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
 313				  struct drm_framebuffer *new_fb,
 314				  struct drm_pending_vblank_event *event,
 315				  uint32_t flags,
 316				  struct drm_modeset_acquire_ctx *ctx)
 317{
 318	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
 319	int ret;
 320
 321	if (!vmw_kms_crtc_flippable(dev_priv, crtc))
 322		return -EINVAL;
 323
 324	ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
 325	if (ret) {
 326		DRM_ERROR("Page flip error %d.\n", ret);
 327		return ret;
 328	}
 329
 330	if (vmw_crtc_to_du(crtc)->is_implicit)
 331		vmw_kms_update_implicit_fb(dev_priv, crtc);
 332
 333	return ret;
 334}
 335
 336static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
 337	.gamma_set = vmw_du_crtc_gamma_set,
 338	.destroy = vmw_sou_crtc_destroy,
 339	.reset = vmw_du_crtc_reset,
 340	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
 341	.atomic_destroy_state = vmw_du_crtc_destroy_state,
 342	.set_config = vmw_kms_set_config,
 343	.page_flip = vmw_sou_crtc_page_flip,
 344};
 345
 346/*
 347 * Screen Object Display Unit encoder functions
 348 */
 349
 350static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
 351{
 352	vmw_sou_destroy(vmw_encoder_to_sou(encoder));
 353}
 354
 355static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
 356	.destroy = vmw_sou_encoder_destroy,
 357};
 358
 359/*
 360 * Screen Object Display Unit connector functions
 361 */
 362
 363static void vmw_sou_connector_destroy(struct drm_connector *connector)
 364{
 365	vmw_sou_destroy(vmw_connector_to_sou(connector));
 366}
 367
 368static const struct drm_connector_funcs vmw_sou_connector_funcs = {
 369	.dpms = vmw_du_connector_dpms,
 370	.detect = vmw_du_connector_detect,
 371	.fill_modes = vmw_du_connector_fill_modes,
 372	.set_property = vmw_du_connector_set_property,
 373	.destroy = vmw_sou_connector_destroy,
 374	.reset = vmw_du_connector_reset,
 375	.atomic_duplicate_state = vmw_du_connector_duplicate_state,
 376	.atomic_destroy_state = vmw_du_connector_destroy_state,
 377	.atomic_set_property = vmw_du_connector_atomic_set_property,
 378	.atomic_get_property = vmw_du_connector_atomic_get_property,
 379};
 380
 381
 382static const struct
 383drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
 384	.best_encoder = drm_atomic_helper_best_encoder,
 385};
 386
 387
 388
 389/*
 390 * Screen Object Display Plane Functions
 391 */
 392
 393/**
 394 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
 395 *
 396 * @plane:  display plane
 397 * @old_state: Contains the FB to clean up
 398 *
 399 * Unpins the display surface
 400 *
 401 * Returns 0 on success
 402 */
 403static void
 404vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
 405				 struct drm_plane_state *old_state)
 406{
 407	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
 408	struct drm_crtc *crtc = plane->state->crtc ?
 409		plane->state->crtc : old_state->crtc;
 410
 411	if (vps->dmabuf)
 412		vmw_dmabuf_unpin(vmw_priv(crtc->dev), vps->dmabuf, false);
 413	vmw_dmabuf_unreference(&vps->dmabuf);
 414	vps->dmabuf_size = 0;
 415
 416	vmw_du_plane_cleanup_fb(plane, old_state);
 417}
 418
 419
 420/**
 421 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
 422 *
 423 * @plane:  display plane
 424 * @new_state: info on the new plane state, including the FB
 425 *
 426 * The SOU backing buffer is our equivalent of the display plane.
 427 *
 428 * Returns 0 on success
 429 */
 430static int
 431vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
 432				 struct drm_plane_state *new_state)
 433{
 434	struct drm_framebuffer *new_fb = new_state->fb;
 435	struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
 436	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
 437	struct vmw_private *dev_priv;
 438	size_t size;
 439	int ret;
 440
 
 
 
 
 
 441
 442	if (!new_fb) {
 443		vmw_dmabuf_unreference(&vps->dmabuf);
 444		vps->dmabuf_size = 0;
 445
 446		return 0;
 447	}
 448
 449	size = new_state->crtc_w * new_state->crtc_h * 4;
 450	dev_priv = vmw_priv(crtc->dev);
 451
 452	if (vps->dmabuf) {
 453		if (vps->dmabuf_size == size) {
 454			/*
 455			 * Note that this might temporarily up the pin-count
 456			 * to 2, until cleanup_fb() is called.
 457			 */
 458			return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf,
 459						      true);
 460		}
 461
 462		vmw_dmabuf_unreference(&vps->dmabuf);
 463		vps->dmabuf_size = 0;
 464	}
 465
 466	vps->dmabuf = kzalloc(sizeof(*vps->dmabuf), GFP_KERNEL);
 467	if (!vps->dmabuf)
 468		return -ENOMEM;
 469
 470	vmw_svga_enable(dev_priv);
 471
 472	/* After we have alloced the backing store might not be able to
 473	 * resume the overlays, this is preferred to failing to alloc.
 474	 */
 475	vmw_overlay_pause_all(dev_priv);
 476	ret = vmw_dmabuf_init(dev_priv, vps->dmabuf, size,
 477			      &vmw_vram_ne_placement,
 478			      false, &vmw_dmabuf_bo_free);
 479	vmw_overlay_resume_all(dev_priv);
 480	if (ret) {
 481		vps->dmabuf = NULL; /* vmw_dmabuf_init frees on error */
 482		return ret;
 483	}
 484
 485	vps->dmabuf_size = size;
 486
 487	/*
 488	 * TTM already thinks the buffer is pinned, but make sure the
 489	 * pin_count is upped.
 490	 */
 491	return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf, true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 492}
 493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 494
 495static void
 496vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
 497				    struct drm_plane_state *old_state)
 498{
 499	struct drm_crtc *crtc = plane->state->crtc;
 500	struct drm_pending_vblank_event *event = NULL;
 
 501	struct vmw_fence_obj *fence = NULL;
 502	int ret;
 503
 504	if (crtc && plane->state->fb) {
 
 505		struct vmw_private *dev_priv = vmw_priv(crtc->dev);
 506		struct vmw_framebuffer *vfb =
 507			vmw_framebuffer_to_vfb(plane->state->fb);
 508		struct drm_vmw_rect vclips;
 509
 510		vclips.x = crtc->x;
 511		vclips.y = crtc->y;
 512		vclips.w = crtc->mode.hdisplay;
 513		vclips.h = crtc->mode.vdisplay;
 514
 515		if (vfb->dmabuf)
 516			ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb, NULL,
 517							  &vclips, 1, 1, true,
 518							  &fence, crtc);
 519		else
 520			ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL,
 521							   &vclips, NULL, 0, 0,
 522							   1, 1, &fence, crtc);
 523
 524		/*
 525		 * We cannot really fail this function, so if we do, then output
 526		 * an error and maintain consistent atomic state.
 527		 */
 528		if (ret != 0)
 529			DRM_ERROR("Failed to update screen.\n");
 530
 531		crtc->primary->fb = plane->state->fb;
 532	} else {
 533		/*
 534		 * When disabling a plane, CRTC and FB should always be NULL
 535		 * together, otherwise it's an error.
 536		 * Here primary plane is being disable so should really blank
 537		 * the screen object display unit, if not already done.
 538		 */
 539		return;
 540	}
 541
 542	event = crtc->state->event;
 543	/*
 544	 * In case of failure and other cases, vblank event will be sent in
 545	 * vmw_du_crtc_atomic_flush.
 546	 */
 547	if (event && fence) {
 548		struct drm_file *file_priv = event->base.file_priv;
 549
 550		ret = vmw_event_fence_action_queue(file_priv,
 551						   fence,
 552						   &event->base,
 553						   &event->event.vbl.tv_sec,
 554						   &event->event.vbl.tv_usec,
 555						   true);
 556
 557		if (unlikely(ret != 0))
 558			DRM_ERROR("Failed to queue event on fence.\n");
 559		else
 560			crtc->state->event = NULL;
 561	}
 562
 563	if (fence)
 564		vmw_fence_obj_unreference(&fence);
 565}
 566
 567
 568static const struct drm_plane_funcs vmw_sou_plane_funcs = {
 569	.update_plane = drm_atomic_helper_update_plane,
 570	.disable_plane = drm_atomic_helper_disable_plane,
 571	.destroy = vmw_du_primary_plane_destroy,
 572	.reset = vmw_du_plane_reset,
 573	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
 574	.atomic_destroy_state = vmw_du_plane_destroy_state,
 575};
 576
 577static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
 578	.update_plane = drm_atomic_helper_update_plane,
 579	.disable_plane = drm_atomic_helper_disable_plane,
 580	.destroy = vmw_du_cursor_plane_destroy,
 581	.reset = vmw_du_plane_reset,
 582	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
 583	.atomic_destroy_state = vmw_du_plane_destroy_state,
 584};
 585
 586/*
 587 * Atomic Helpers
 588 */
 589static const struct
 590drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
 591	.atomic_check = vmw_du_cursor_plane_atomic_check,
 592	.atomic_update = vmw_du_cursor_plane_atomic_update,
 593	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
 594	.cleanup_fb = vmw_du_plane_cleanup_fb,
 595};
 596
 597static const struct
 598drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
 599	.atomic_check = vmw_du_primary_plane_atomic_check,
 600	.atomic_update = vmw_sou_primary_plane_atomic_update,
 601	.prepare_fb = vmw_sou_primary_plane_prepare_fb,
 602	.cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
 603};
 604
 605static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
 606	.prepare = vmw_sou_crtc_helper_prepare,
 607	.mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
 608	.atomic_check = vmw_du_crtc_atomic_check,
 609	.atomic_begin = vmw_du_crtc_atomic_begin,
 610	.atomic_flush = vmw_du_crtc_atomic_flush,
 611	.atomic_enable = vmw_sou_crtc_atomic_enable,
 612	.atomic_disable = vmw_sou_crtc_atomic_disable,
 613};
 614
 615
 616static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
 617{
 618	struct vmw_screen_object_unit *sou;
 619	struct drm_device *dev = dev_priv->dev;
 620	struct drm_connector *connector;
 621	struct drm_encoder *encoder;
 622	struct drm_plane *primary, *cursor;
 
 623	struct drm_crtc *crtc;
 624	int ret;
 625
 626	sou = kzalloc(sizeof(*sou), GFP_KERNEL);
 627	if (!sou)
 628		return -ENOMEM;
 629
 630	sou->base.unit = unit;
 631	crtc = &sou->base.crtc;
 632	encoder = &sou->base.encoder;
 633	connector = &sou->base.connector;
 634	primary = &sou->base.primary;
 635	cursor = &sou->base.cursor;
 636
 637	sou->base.active_implicit = false;
 638	sou->base.pref_active = (unit == 0);
 639	sou->base.pref_width = dev_priv->initial_width;
 640	sou->base.pref_height = dev_priv->initial_height;
 641	sou->base.pref_mode = NULL;
 642
 643	/*
 644	 * Remove this after enabling atomic because property values can
 645	 * only exist in a state object
 646	 */
 647	sou->base.is_implicit = false;
 648
 649	/* Initialize primary plane */
 650	vmw_du_plane_reset(primary);
 651
 652	ret = drm_universal_plane_init(dev, &sou->base.primary,
 653				       0, &vmw_sou_plane_funcs,
 654				       vmw_primary_plane_formats,
 655				       ARRAY_SIZE(vmw_primary_plane_formats),
 656				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
 657	if (ret) {
 658		DRM_ERROR("Failed to initialize primary plane");
 659		goto err_free;
 660	}
 661
 662	drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
 
 663
 664	/* Initialize cursor plane */
 665	vmw_du_plane_reset(cursor);
 666
 667	ret = drm_universal_plane_init(dev, &sou->base.cursor,
 668			0, &vmw_sou_cursor_funcs,
 669			vmw_cursor_plane_formats,
 670			ARRAY_SIZE(vmw_cursor_plane_formats),
 671			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
 672	if (ret) {
 673		DRM_ERROR("Failed to initialize cursor plane");
 674		drm_plane_cleanup(&sou->base.primary);
 675		goto err_free;
 676	}
 677
 678	drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
 679
 680	vmw_du_connector_reset(connector);
 681	ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
 682				 DRM_MODE_CONNECTOR_VIRTUAL);
 683	if (ret) {
 684		DRM_ERROR("Failed to initialize connector\n");
 685		goto err_free;
 686	}
 687
 688	drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
 689	connector->status = vmw_du_connector_detect(connector, true);
 690	vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
 691
 692
 693	ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
 694			       DRM_MODE_ENCODER_VIRTUAL, NULL);
 695	if (ret) {
 696		DRM_ERROR("Failed to initialize encoder\n");
 697		goto err_free_connector;
 698	}
 699
 700	(void) drm_mode_connector_attach_encoder(connector, encoder);
 701	encoder->possible_crtcs = (1 << unit);
 702	encoder->possible_clones = 0;
 703
 704	ret = drm_connector_register(connector);
 705	if (ret) {
 706		DRM_ERROR("Failed to register connector\n");
 707		goto err_free_encoder;
 708	}
 709
 710
 711	vmw_du_crtc_reset(crtc);
 712	ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
 713					&sou->base.cursor,
 714					&vmw_screen_object_crtc_funcs, NULL);
 715	if (ret) {
 716		DRM_ERROR("Failed to initialize CRTC\n");
 717		goto err_free_unregister;
 718	}
 719
 720	drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
 721
 722	drm_mode_crtc_set_gamma_size(crtc, 256);
 723
 724	drm_object_attach_property(&connector->base,
 725				   dev_priv->hotplug_mode_update_property, 1);
 726	drm_object_attach_property(&connector->base,
 727				   dev->mode_config.suggested_x_property, 0);
 728	drm_object_attach_property(&connector->base,
 729				   dev->mode_config.suggested_y_property, 0);
 730	if (dev_priv->implicit_placement_property)
 731		drm_object_attach_property
 732			(&connector->base,
 733			 dev_priv->implicit_placement_property,
 734			 sou->base.is_implicit);
 735
 736	return 0;
 737
 738err_free_unregister:
 739	drm_connector_unregister(connector);
 740err_free_encoder:
 741	drm_encoder_cleanup(encoder);
 742err_free_connector:
 743	drm_connector_cleanup(connector);
 744err_free:
 745	kfree(sou);
 746	return ret;
 747}
 748
 749int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
 750{
 751	struct drm_device *dev = dev_priv->dev;
 752	int i, ret;
 
 
 
 
 753
 754	if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
 755		DRM_INFO("Not using screen objects,"
 756			 " missing cap SCREEN_OBJECT_2\n");
 757		return -ENOSYS;
 758	}
 759
 760	ret = -ENOMEM;
 761	dev_priv->num_implicit = 0;
 762	dev_priv->implicit_fb = NULL;
 763
 764	ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
 765	if (unlikely(ret != 0))
 766		return ret;
 767
 768	vmw_kms_create_implicit_placement_property(dev_priv, false);
 769
 770	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
 771		vmw_sou_init(dev_priv, i);
 772
 773	dev_priv->active_display_unit = vmw_du_screen_object;
 774
 775	DRM_INFO("Screen Objects Display Unit initialized\n");
 776
 777	return 0;
 778}
 779
 780static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
 781				  struct vmw_framebuffer *framebuffer)
 782{
 783	struct vmw_dma_buffer *buf =
 784		container_of(framebuffer, struct vmw_framebuffer_dmabuf,
 785			     base)->buffer;
 786	int depth = framebuffer->base.format->depth;
 787	struct {
 788		uint32_t header;
 789		SVGAFifoCmdDefineGMRFB body;
 790	} *cmd;
 791
 792	/* Emulate RGBA support, contrary to svga_reg.h this is not
 793	 * supported by hosts. This is only a problem if we are reading
 794	 * this value later and expecting what we uploaded back.
 795	 */
 796	if (depth == 32)
 797		depth = 24;
 798
 799	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
 800	if (!cmd) {
 801		DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
 802		return -ENOMEM;
 803	}
 804
 805	cmd->header = SVGA_CMD_DEFINE_GMRFB;
 806	cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
 807	cmd->body.format.colorDepth = depth;
 808	cmd->body.format.reserved = 0;
 809	cmd->body.bytesPerLine = framebuffer->base.pitches[0];
 810	/* Buffer is reserved in vram or GMR */
 811	vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
 812	vmw_fifo_commit(dev_priv, sizeof(*cmd));
 813
 814	return 0;
 815}
 816
 817/**
 818 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
 819 * blit surface to screen command.
 820 *
 821 * @dirty: The closure structure.
 822 *
 823 * Fills in the missing fields in the command, and translates the cliprects
 824 * to match the destination bounding box encoded.
 825 */
 826static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
 827{
 828	struct vmw_kms_sou_surface_dirty *sdirty =
 829		container_of(dirty, typeof(*sdirty), base);
 830	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
 831	s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
 832	s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
 833	size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
 834	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
 835	int i;
 836
 837	if (!dirty->num_hits) {
 838		vmw_fifo_commit(dirty->dev_priv, 0);
 839		return;
 840	}
 841
 842	cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
 843	cmd->header.size = sizeof(cmd->body) + region_size;
 844
 845	/*
 846	 * Use the destination bounding box to specify destination - and
 847	 * source bounding regions.
 848	 */
 849	cmd->body.destRect.left = sdirty->left;
 850	cmd->body.destRect.right = sdirty->right;
 851	cmd->body.destRect.top = sdirty->top;
 852	cmd->body.destRect.bottom = sdirty->bottom;
 853
 854	cmd->body.srcRect.left = sdirty->left + trans_x;
 855	cmd->body.srcRect.right = sdirty->right + trans_x;
 856	cmd->body.srcRect.top = sdirty->top + trans_y;
 857	cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
 858
 859	cmd->body.srcImage.sid = sdirty->sid;
 860	cmd->body.destScreenId = dirty->unit->unit;
 861
 862	/* Blits are relative to the destination rect. Translate. */
 863	for (i = 0; i < dirty->num_hits; ++i, ++blit) {
 864		blit->left -= sdirty->left;
 865		blit->right -= sdirty->left;
 866		blit->top -= sdirty->top;
 867		blit->bottom -= sdirty->top;
 868	}
 869
 870	vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
 871
 872	sdirty->left = sdirty->top = S32_MAX;
 873	sdirty->right = sdirty->bottom = S32_MIN;
 874}
 875
 876/**
 877 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
 878 *
 879 * @dirty: The closure structure
 880 *
 881 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
 882 * BLIT_SURFACE_TO_SCREEN command.
 883 */
 884static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
 885{
 886	struct vmw_kms_sou_surface_dirty *sdirty =
 887		container_of(dirty, typeof(*sdirty), base);
 888	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
 889	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
 890
 891	/* Destination rect. */
 892	blit += dirty->num_hits;
 893	blit->left = dirty->unit_x1;
 894	blit->top = dirty->unit_y1;
 895	blit->right = dirty->unit_x2;
 896	blit->bottom = dirty->unit_y2;
 897
 898	/* Destination bounding box */
 899	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
 900	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
 901	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
 902	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
 903
 904	dirty->num_hits++;
 905}
 906
 907/**
 908 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
 909 *
 910 * @dev_priv: Pointer to the device private structure.
 911 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
 912 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
 913 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
 914 * be NULL.
 915 * @srf: Pointer to surface to blit from. If NULL, the surface attached
 916 * to @framebuffer will be used.
 917 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
 918 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
 919 * @num_clips: Number of clip rects in @clips.
 920 * @inc: Increment to use when looping over @clips.
 921 * @out_fence: If non-NULL, will return a ref-counted pointer to a
 922 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
 923 * case the device has already synchronized.
 924 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
 925 *
 926 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
 927 * interrupted.
 928 */
 929int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
 930				 struct vmw_framebuffer *framebuffer,
 931				 struct drm_clip_rect *clips,
 932				 struct drm_vmw_rect *vclips,
 933				 struct vmw_resource *srf,
 934				 s32 dest_x,
 935				 s32 dest_y,
 936				 unsigned num_clips, int inc,
 937				 struct vmw_fence_obj **out_fence,
 938				 struct drm_crtc *crtc)
 939{
 940	struct vmw_framebuffer_surface *vfbs =
 941		container_of(framebuffer, typeof(*vfbs), base);
 942	struct vmw_kms_sou_surface_dirty sdirty;
 943	struct vmw_validation_ctx ctx;
 944	int ret;
 945
 946	if (!srf)
 947		srf = &vfbs->surface->res;
 948
 949	ret = vmw_kms_helper_resource_prepare(srf, true, &ctx);
 
 950	if (ret)
 951		return ret;
 952
 
 
 
 
 953	sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
 954	sdirty.base.clip = vmw_sou_surface_clip;
 955	sdirty.base.dev_priv = dev_priv;
 956	sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
 957	  sizeof(SVGASignedRect) * num_clips;
 958	sdirty.base.crtc = crtc;
 959
 960	sdirty.sid = srf->id;
 961	sdirty.left = sdirty.top = S32_MAX;
 962	sdirty.right = sdirty.bottom = S32_MIN;
 963	sdirty.dst_x = dest_x;
 964	sdirty.dst_y = dest_y;
 965
 966	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
 967				   dest_x, dest_y, num_clips, inc,
 968				   &sdirty.base);
 969	vmw_kms_helper_resource_finish(&ctx, out_fence);
 
 
 
 970
 
 
 971	return ret;
 972}
 973
 974/**
 975 * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
 976 *
 977 * @dirty: The closure structure.
 978 *
 979 * Commits a previously built command buffer of readback clips.
 980 */
 981static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
 982{
 983	if (!dirty->num_hits) {
 984		vmw_fifo_commit(dirty->dev_priv, 0);
 985		return;
 986	}
 987
 988	vmw_fifo_commit(dirty->dev_priv,
 989			sizeof(struct vmw_kms_sou_dmabuf_blit) *
 990			dirty->num_hits);
 991}
 992
 993/**
 994 * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
 995 *
 996 * @dirty: The closure structure
 997 *
 998 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
 999 */
1000static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
1001{
1002	struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
1003
1004	blit += dirty->num_hits;
1005	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1006	blit->body.destScreenId = dirty->unit->unit;
1007	blit->body.srcOrigin.x = dirty->fb_x;
1008	blit->body.srcOrigin.y = dirty->fb_y;
1009	blit->body.destRect.left = dirty->unit_x1;
1010	blit->body.destRect.top = dirty->unit_y1;
1011	blit->body.destRect.right = dirty->unit_x2;
1012	blit->body.destRect.bottom = dirty->unit_y2;
1013	dirty->num_hits++;
1014}
1015
1016/**
1017 * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
1018 *
1019 * @dev_priv: Pointer to the device private structure.
1020 * @framebuffer: Pointer to the dma-buffer backed framebuffer.
1021 * @clips: Array of clip rects.
1022 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1023 * be NULL.
1024 * @num_clips: Number of clip rects in @clips.
1025 * @increment: Increment to use when looping over @clips.
1026 * @interruptible: Whether to perform waits interruptible if possible.
1027 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1028 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1029 * case the device has already synchronized.
1030 * @crtc: If crtc is passed, perform dmabuf dirty on that crtc only.
1031 *
1032 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1033 * interrupted.
1034 */
1035int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
1036				struct vmw_framebuffer *framebuffer,
1037				struct drm_clip_rect *clips,
1038				struct drm_vmw_rect *vclips,
1039				unsigned num_clips, int increment,
1040				bool interruptible,
1041				struct vmw_fence_obj **out_fence,
1042				struct drm_crtc *crtc)
1043{
1044	struct vmw_dma_buffer *buf =
1045		container_of(framebuffer, struct vmw_framebuffer_dmabuf,
1046			     base)->buffer;
1047	struct vmw_kms_dirty dirty;
 
1048	int ret;
1049
1050	ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
1051					    false, false);
 
1052	if (ret)
1053		return ret;
1054
1055	ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
 
 
 
 
1056	if (unlikely(ret != 0))
1057		goto out_revert;
1058
1059	dirty.crtc = crtc;
1060	dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
1061	dirty.clip = vmw_sou_dmabuf_clip;
1062	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
1063		num_clips;
1064	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1065				   0, 0, num_clips, increment, &dirty);
1066	vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
 
1067
1068	return ret;
1069
1070out_revert:
1071	vmw_kms_helper_buffer_revert(buf);
 
 
1072
1073	return ret;
1074}
1075
1076
1077/**
1078 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1079 *
1080 * @dirty: The closure structure.
1081 *
1082 * Commits a previously built command buffer of readback clips.
1083 */
1084static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1085{
1086	if (!dirty->num_hits) {
1087		vmw_fifo_commit(dirty->dev_priv, 0);
1088		return;
1089	}
1090
1091	vmw_fifo_commit(dirty->dev_priv,
1092			sizeof(struct vmw_kms_sou_readback_blit) *
1093			dirty->num_hits);
1094}
1095
1096/**
1097 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1098 *
1099 * @dirty: The closure structure
1100 *
1101 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1102 */
1103static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1104{
1105	struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1106
1107	blit += dirty->num_hits;
1108	blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1109	blit->body.srcScreenId = dirty->unit->unit;
1110	blit->body.destOrigin.x = dirty->fb_x;
1111	blit->body.destOrigin.y = dirty->fb_y;
1112	blit->body.srcRect.left = dirty->unit_x1;
1113	blit->body.srcRect.top = dirty->unit_y1;
1114	blit->body.srcRect.right = dirty->unit_x2;
1115	blit->body.srcRect.bottom = dirty->unit_y2;
1116	dirty->num_hits++;
1117}
1118
1119/**
1120 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1121 * a dma-buffer backed framebuffer.
1122 *
1123 * @dev_priv: Pointer to the device private structure.
1124 * @file_priv: Pointer to a struct drm_file identifying the caller.
1125 * Must be set to NULL if @user_fence_rep is NULL.
1126 * @vfb: Pointer to the dma-buffer backed framebuffer.
1127 * @user_fence_rep: User-space provided structure for fence information.
1128 * Must be set to non-NULL if @file_priv is non-NULL.
1129 * @vclips: Array of clip rects.
1130 * @num_clips: Number of clip rects in @vclips.
1131 * @crtc: If crtc is passed, readback on that crtc only.
1132 *
1133 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1134 * interrupted.
1135 */
1136int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1137			 struct drm_file *file_priv,
1138			 struct vmw_framebuffer *vfb,
1139			 struct drm_vmw_fence_rep __user *user_fence_rep,
1140			 struct drm_vmw_rect *vclips,
1141			 uint32_t num_clips,
1142			 struct drm_crtc *crtc)
1143{
1144	struct vmw_dma_buffer *buf =
1145		container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
1146	struct vmw_kms_dirty dirty;
 
1147	int ret;
1148
1149	ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false,
1150					    false);
 
1151	if (ret)
1152		return ret;
1153
1154	ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
 
 
 
 
1155	if (unlikely(ret != 0))
1156		goto out_revert;
1157
1158	dirty.crtc = crtc;
1159	dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1160	dirty.clip = vmw_sou_readback_clip;
1161	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1162		num_clips;
1163	ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1164				   0, 0, num_clips, 1, &dirty);
1165	vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
1166				     user_fence_rep);
1167
1168	return ret;
1169
1170out_revert:
1171	vmw_kms_helper_buffer_revert(buf);
 
 
1172
1173	return ret;
1174}