Home | History | Annotate | Line # | Download | only in vmwgfx
      1  1.1  riastrad /*	$NetBSD: vmwgfx_stdu.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.3  riastrad // SPDX-License-Identifier: GPL-2.0 OR MIT
      4  1.1  riastrad /******************************************************************************
      5  1.1  riastrad  *
      6  1.3  riastrad  * COPYRIGHT (C) 2014-2015 VMware, Inc., Palo Alto, CA., USA
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the
     10  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     11  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     12  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     13  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     14  1.1  riastrad  * the following conditions:
     15  1.1  riastrad  *
     16  1.1  riastrad  * The above copyright notice and this permission notice (including the
     17  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     18  1.1  riastrad  * of the Software.
     19  1.1  riastrad  *
     20  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  1.1  riastrad  *
     28  1.1  riastrad  ******************************************************************************/
     29  1.1  riastrad 
     30  1.1  riastrad #include <sys/cdefs.h>
     31  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: vmwgfx_stdu.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
     32  1.1  riastrad 
     33  1.3  riastrad #include <drm/drm_atomic.h>
     34  1.3  riastrad #include <drm/drm_atomic_helper.h>
     35  1.3  riastrad #include <drm/drm_damage_helper.h>
     36  1.3  riastrad #include <drm/drm_fourcc.h>
     37  1.3  riastrad #include <drm/drm_plane_helper.h>
     38  1.3  riastrad #include <drm/drm_vblank.h>
     39  1.3  riastrad 
     40  1.1  riastrad #include "vmwgfx_kms.h"
     41  1.1  riastrad #include "device_include/svga3d_surfacedefs.h"
     42  1.1  riastrad 
     43  1.1  riastrad #define vmw_crtc_to_stdu(x) \
     44  1.1  riastrad 	container_of(x, struct vmw_screen_target_display_unit, base.crtc)
     45  1.1  riastrad #define vmw_encoder_to_stdu(x) \
     46  1.1  riastrad 	container_of(x, struct vmw_screen_target_display_unit, base.encoder)
     47  1.1  riastrad #define vmw_connector_to_stdu(x) \
     48  1.1  riastrad 	container_of(x, struct vmw_screen_target_display_unit, base.connector)
     49  1.1  riastrad 
     50  1.1  riastrad 
     51  1.1  riastrad 
     52  1.1  riastrad enum stdu_content_type {
     53  1.1  riastrad 	SAME_AS_DISPLAY = 0,
     54  1.1  riastrad 	SEPARATE_SURFACE,
     55  1.3  riastrad 	SEPARATE_BO
     56  1.1  riastrad };
     57  1.1  riastrad 
     58  1.1  riastrad /**
     59  1.1  riastrad  * struct vmw_stdu_dirty - closure structure for the update functions
     60  1.1  riastrad  *
     61  1.1  riastrad  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
     62  1.1  riastrad  * @transfer: Transfer direction for DMA command.
     63  1.1  riastrad  * @left: Left side of bounding box.
     64  1.1  riastrad  * @right: Right side of bounding box.
     65  1.1  riastrad  * @top: Top side of bounding box.
     66  1.1  riastrad  * @bottom: Bottom side of bounding box.
     67  1.3  riastrad  * @fb_left: Left side of the framebuffer/content bounding box
     68  1.3  riastrad  * @fb_top: Top of the framebuffer/content bounding box
     69  1.3  riastrad  * @buf: buffer object when DMA-ing between buffer and screen targets.
     70  1.1  riastrad  * @sid: Surface ID when copying between surface and screen targets.
     71  1.1  riastrad  */
     72  1.1  riastrad struct vmw_stdu_dirty {
     73  1.1  riastrad 	struct vmw_kms_dirty base;
     74  1.1  riastrad 	SVGA3dTransferType  transfer;
     75  1.1  riastrad 	s32 left, right, top, bottom;
     76  1.3  riastrad 	s32 fb_left, fb_top;
     77  1.1  riastrad 	u32 pitch;
     78  1.1  riastrad 	union {
     79  1.3  riastrad 		struct vmw_buffer_object *buf;
     80  1.1  riastrad 		u32 sid;
     81  1.1  riastrad 	};
     82  1.1  riastrad };
     83  1.1  riastrad 
     84  1.1  riastrad /*
     85  1.1  riastrad  * SVGA commands that are used by this code. Please see the device headers
     86  1.1  riastrad  * for explanation.
     87  1.1  riastrad  */
     88  1.1  riastrad struct vmw_stdu_update {
     89  1.1  riastrad 	SVGA3dCmdHeader header;
     90  1.1  riastrad 	SVGA3dCmdUpdateGBScreenTarget body;
     91  1.1  riastrad };
     92  1.1  riastrad 
     93  1.1  riastrad struct vmw_stdu_dma {
     94  1.1  riastrad 	SVGA3dCmdHeader     header;
     95  1.1  riastrad 	SVGA3dCmdSurfaceDMA body;
     96  1.1  riastrad };
     97  1.1  riastrad 
     98  1.1  riastrad struct vmw_stdu_surface_copy {
     99  1.1  riastrad 	SVGA3dCmdHeader      header;
    100  1.1  riastrad 	SVGA3dCmdSurfaceCopy body;
    101  1.1  riastrad };
    102  1.1  riastrad 
    103  1.3  riastrad struct vmw_stdu_update_gb_image {
    104  1.3  riastrad 	SVGA3dCmdHeader header;
    105  1.3  riastrad 	SVGA3dCmdUpdateGBImage body;
    106  1.3  riastrad };
    107  1.1  riastrad 
    108  1.1  riastrad /**
    109  1.1  riastrad  * struct vmw_screen_target_display_unit
    110  1.1  riastrad  *
    111  1.1  riastrad  * @base: VMW specific DU structure
    112  1.1  riastrad  * @display_srf: surface to be displayed.  The dimension of this will always
    113  1.1  riastrad  *               match the display mode.  If the display mode matches
    114  1.1  riastrad  *               content_vfbs dimensions, then this is a pointer into the
    115  1.1  riastrad  *               corresponding field in content_vfbs.  If not, then this
    116  1.1  riastrad  *               is a separate buffer to which content_vfbs will blit to.
    117  1.1  riastrad  * @content_type:  content_fb type
    118  1.1  riastrad  * @defined:  true if the current display unit has been initialized
    119  1.1  riastrad  */
    120  1.1  riastrad struct vmw_screen_target_display_unit {
    121  1.1  riastrad 	struct vmw_display_unit base;
    122  1.3  riastrad 	struct vmw_surface *display_srf;
    123  1.1  riastrad 	enum stdu_content_type content_fb_type;
    124  1.3  riastrad 	s32 display_width, display_height;
    125  1.1  riastrad 
    126  1.1  riastrad 	bool defined;
    127  1.3  riastrad 
    128  1.3  riastrad 	/* For CPU Blit */
    129  1.3  riastrad 	unsigned int cpp;
    130  1.1  riastrad };
    131  1.1  riastrad 
    132  1.1  riastrad 
    133  1.1  riastrad 
    134  1.1  riastrad static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
    135  1.1  riastrad 
    136  1.1  riastrad 
    137  1.1  riastrad 
    138  1.1  riastrad /******************************************************************************
    139  1.1  riastrad  * Screen Target Display Unit CRTC Functions
    140  1.1  riastrad  *****************************************************************************/
    141  1.1  riastrad 
    142  1.1  riastrad 
    143  1.1  riastrad /**
    144  1.1  riastrad  * vmw_stdu_crtc_destroy - cleans up the STDU
    145  1.1  riastrad  *
    146  1.1  riastrad  * @crtc: used to get a reference to the containing STDU
    147  1.1  riastrad  */
    148  1.1  riastrad static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
    149  1.1  riastrad {
    150  1.1  riastrad 	vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
    151  1.1  riastrad }
    152  1.1  riastrad 
    153  1.1  riastrad /**
    154  1.1  riastrad  * vmw_stdu_define_st - Defines a Screen Target
    155  1.1  riastrad  *
    156  1.1  riastrad  * @dev_priv:  VMW DRM device
    157  1.1  riastrad  * @stdu: display unit to create a Screen Target for
    158  1.3  riastrad  * @mode: The mode to set.
    159  1.3  riastrad  * @crtc_x: X coordinate of screen target relative to framebuffer origin.
    160  1.3  riastrad  * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
    161  1.1  riastrad  *
    162  1.1  riastrad  * Creates a STDU that we can used later.  This function is called whenever the
    163  1.1  riastrad  * framebuffer size changes.
    164  1.1  riastrad  *
    165  1.1  riastrad  * RETURNs:
    166  1.1  riastrad  * 0 on success, error code on failure
    167  1.1  riastrad  */
    168  1.1  riastrad static int vmw_stdu_define_st(struct vmw_private *dev_priv,
    169  1.3  riastrad 			      struct vmw_screen_target_display_unit *stdu,
    170  1.3  riastrad 			      struct drm_display_mode *mode,
    171  1.3  riastrad 			      int crtc_x, int crtc_y)
    172  1.1  riastrad {
    173  1.1  riastrad 	struct {
    174  1.1  riastrad 		SVGA3dCmdHeader header;
    175  1.1  riastrad 		SVGA3dCmdDefineGBScreenTarget body;
    176  1.1  riastrad 	} *cmd;
    177  1.1  riastrad 
    178  1.3  riastrad 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    179  1.3  riastrad 	if (unlikely(cmd == NULL))
    180  1.1  riastrad 		return -ENOMEM;
    181  1.1  riastrad 
    182  1.1  riastrad 	cmd->header.id   = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
    183  1.1  riastrad 	cmd->header.size = sizeof(cmd->body);
    184  1.1  riastrad 
    185  1.1  riastrad 	cmd->body.stid   = stdu->base.unit;
    186  1.3  riastrad 	cmd->body.width  = mode->hdisplay;
    187  1.3  riastrad 	cmd->body.height = mode->vdisplay;
    188  1.1  riastrad 	cmd->body.flags  = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
    189  1.1  riastrad 	cmd->body.dpi    = 0;
    190  1.3  riastrad 	cmd->body.xRoot  = crtc_x;
    191  1.3  riastrad 	cmd->body.yRoot  = crtc_y;
    192  1.1  riastrad 
    193  1.3  riastrad 	stdu->base.set_gui_x = cmd->body.xRoot;
    194  1.3  riastrad 	stdu->base.set_gui_y = cmd->body.yRoot;
    195  1.1  riastrad 
    196  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
    197  1.1  riastrad 
    198  1.1  riastrad 	stdu->defined = true;
    199  1.3  riastrad 	stdu->display_width  = mode->hdisplay;
    200  1.3  riastrad 	stdu->display_height = mode->vdisplay;
    201  1.1  riastrad 
    202  1.1  riastrad 	return 0;
    203  1.1  riastrad }
    204  1.1  riastrad 
    205  1.1  riastrad 
    206  1.1  riastrad 
    207  1.1  riastrad /**
    208  1.1  riastrad  * vmw_stdu_bind_st - Binds a surface to a Screen Target
    209  1.1  riastrad  *
    210  1.1  riastrad  * @dev_priv: VMW DRM device
    211  1.1  riastrad  * @stdu: display unit affected
    212  1.1  riastrad  * @res: Buffer to bind to the screen target.  Set to NULL to blank screen.
    213  1.1  riastrad  *
    214  1.1  riastrad  * Binding a surface to a Screen Target the same as flipping
    215  1.1  riastrad  */
    216  1.1  riastrad static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
    217  1.1  riastrad 			    struct vmw_screen_target_display_unit *stdu,
    218  1.3  riastrad 			    const struct vmw_resource *res)
    219  1.1  riastrad {
    220  1.1  riastrad 	SVGA3dSurfaceImageId image;
    221  1.1  riastrad 
    222  1.1  riastrad 	struct {
    223  1.1  riastrad 		SVGA3dCmdHeader header;
    224  1.1  riastrad 		SVGA3dCmdBindGBScreenTarget body;
    225  1.1  riastrad 	} *cmd;
    226  1.1  riastrad 
    227  1.1  riastrad 
    228  1.1  riastrad 	if (!stdu->defined) {
    229  1.1  riastrad 		DRM_ERROR("No screen target defined\n");
    230  1.1  riastrad 		return -EINVAL;
    231  1.1  riastrad 	}
    232  1.1  riastrad 
    233  1.1  riastrad 	/* Set up image using information in vfb */
    234  1.1  riastrad 	memset(&image, 0, sizeof(image));
    235  1.1  riastrad 	image.sid = res ? res->id : SVGA3D_INVALID_ID;
    236  1.1  riastrad 
    237  1.3  riastrad 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    238  1.3  riastrad 	if (unlikely(cmd == NULL))
    239  1.1  riastrad 		return -ENOMEM;
    240  1.1  riastrad 
    241  1.1  riastrad 	cmd->header.id   = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
    242  1.1  riastrad 	cmd->header.size = sizeof(cmd->body);
    243  1.1  riastrad 
    244  1.1  riastrad 	cmd->body.stid   = stdu->base.unit;
    245  1.1  riastrad 	cmd->body.image  = image;
    246  1.1  riastrad 
    247  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
    248  1.1  riastrad 
    249  1.1  riastrad 	return 0;
    250  1.1  riastrad }
    251  1.1  riastrad 
    252  1.1  riastrad /**
    253  1.1  riastrad  * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
    254  1.1  riastrad  * bounding box.
    255  1.1  riastrad  *
    256  1.1  riastrad  * @cmd: Pointer to command stream.
    257  1.1  riastrad  * @unit: Screen target unit.
    258  1.1  riastrad  * @left: Left side of bounding box.
    259  1.1  riastrad  * @right: Right side of bounding box.
    260  1.1  riastrad  * @top: Top side of bounding box.
    261  1.1  riastrad  * @bottom: Bottom side of bounding box.
    262  1.1  riastrad  */
    263  1.1  riastrad static void vmw_stdu_populate_update(void *cmd, int unit,
    264  1.1  riastrad 				     s32 left, s32 right, s32 top, s32 bottom)
    265  1.1  riastrad {
    266  1.1  riastrad 	struct vmw_stdu_update *update = cmd;
    267  1.1  riastrad 
    268  1.1  riastrad 	update->header.id   = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
    269  1.1  riastrad 	update->header.size = sizeof(update->body);
    270  1.1  riastrad 
    271  1.1  riastrad 	update->body.stid   = unit;
    272  1.1  riastrad 	update->body.rect.x = left;
    273  1.1  riastrad 	update->body.rect.y = top;
    274  1.1  riastrad 	update->body.rect.w = right - left;
    275  1.1  riastrad 	update->body.rect.h = bottom - top;
    276  1.1  riastrad }
    277  1.1  riastrad 
    278  1.1  riastrad /**
    279  1.1  riastrad  * vmw_stdu_update_st - Full update of a Screen Target
    280  1.1  riastrad  *
    281  1.1  riastrad  * @dev_priv: VMW DRM device
    282  1.1  riastrad  * @stdu: display unit affected
    283  1.1  riastrad  *
    284  1.1  riastrad  * This function needs to be called whenever the content of a screen
    285  1.1  riastrad  * target has changed completely. Typically as a result of a backing
    286  1.1  riastrad  * surface change.
    287  1.1  riastrad  *
    288  1.1  riastrad  * RETURNS:
    289  1.1  riastrad  * 0 on success, error code on failure
    290  1.1  riastrad  */
    291  1.1  riastrad static int vmw_stdu_update_st(struct vmw_private *dev_priv,
    292  1.1  riastrad 			      struct vmw_screen_target_display_unit *stdu)
    293  1.1  riastrad {
    294  1.1  riastrad 	struct vmw_stdu_update *cmd;
    295  1.1  riastrad 
    296  1.1  riastrad 	if (!stdu->defined) {
    297  1.1  riastrad 		DRM_ERROR("No screen target defined");
    298  1.1  riastrad 		return -EINVAL;
    299  1.1  riastrad 	}
    300  1.1  riastrad 
    301  1.3  riastrad 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    302  1.3  riastrad 	if (unlikely(cmd == NULL))
    303  1.1  riastrad 		return -ENOMEM;
    304  1.1  riastrad 
    305  1.3  riastrad 	vmw_stdu_populate_update(cmd, stdu->base.unit,
    306  1.3  riastrad 				 0, stdu->display_width,
    307  1.3  riastrad 				 0, stdu->display_height);
    308  1.1  riastrad 
    309  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
    310  1.1  riastrad 
    311  1.1  riastrad 	return 0;
    312  1.1  riastrad }
    313  1.1  riastrad 
    314  1.1  riastrad 
    315  1.1  riastrad 
    316  1.1  riastrad /**
    317  1.1  riastrad  * vmw_stdu_destroy_st - Destroy a Screen Target
    318  1.1  riastrad  *
    319  1.1  riastrad  * @dev_priv:  VMW DRM device
    320  1.1  riastrad  * @stdu: display unit to destroy
    321  1.1  riastrad  */
    322  1.1  riastrad static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
    323  1.1  riastrad 			       struct vmw_screen_target_display_unit *stdu)
    324  1.1  riastrad {
    325  1.1  riastrad 	int    ret;
    326  1.1  riastrad 
    327  1.1  riastrad 	struct {
    328  1.1  riastrad 		SVGA3dCmdHeader header;
    329  1.1  riastrad 		SVGA3dCmdDestroyGBScreenTarget body;
    330  1.1  riastrad 	} *cmd;
    331  1.1  riastrad 
    332  1.1  riastrad 
    333  1.1  riastrad 	/* Nothing to do if not successfully defined */
    334  1.1  riastrad 	if (unlikely(!stdu->defined))
    335  1.1  riastrad 		return 0;
    336  1.1  riastrad 
    337  1.3  riastrad 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    338  1.3  riastrad 	if (unlikely(cmd == NULL))
    339  1.1  riastrad 		return -ENOMEM;
    340  1.1  riastrad 
    341  1.1  riastrad 	cmd->header.id   = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
    342  1.1  riastrad 	cmd->header.size = sizeof(cmd->body);
    343  1.1  riastrad 
    344  1.1  riastrad 	cmd->body.stid   = stdu->base.unit;
    345  1.1  riastrad 
    346  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
    347  1.1  riastrad 
    348  1.1  riastrad 	/* Force sync */
    349  1.1  riastrad 	ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
    350  1.1  riastrad 	if (unlikely(ret != 0))
    351  1.1  riastrad 		DRM_ERROR("Failed to sync with HW");
    352  1.1  riastrad 
    353  1.1  riastrad 	stdu->defined = false;
    354  1.3  riastrad 	stdu->display_width  = 0;
    355  1.3  riastrad 	stdu->display_height = 0;
    356  1.1  riastrad 
    357  1.1  riastrad 	return ret;
    358  1.1  riastrad }
    359  1.1  riastrad 
    360  1.1  riastrad 
    361  1.1  riastrad /**
    362  1.3  riastrad  * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
    363  1.1  riastrad  *
    364  1.3  riastrad  * @crtc: CRTC associated with the screen target
    365  1.1  riastrad  *
    366  1.3  riastrad  * This function defines/destroys a screen target
    367  1.1  riastrad  *
    368  1.1  riastrad  */
    369  1.3  riastrad static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
    370  1.1  riastrad {
    371  1.1  riastrad 	struct vmw_private *dev_priv;
    372  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu;
    373  1.3  riastrad 	struct drm_connector_state *conn_state;
    374  1.3  riastrad 	struct vmw_connector_state *vmw_conn_state;
    375  1.3  riastrad 	int x, y, ret;
    376  1.1  riastrad 
    377  1.3  riastrad 	stdu = vmw_crtc_to_stdu(crtc);
    378  1.1  riastrad 	dev_priv = vmw_priv(crtc->dev);
    379  1.3  riastrad 	conn_state = stdu->base.connector.state;
    380  1.3  riastrad 	vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
    381  1.1  riastrad 
    382  1.1  riastrad 	if (stdu->defined) {
    383  1.1  riastrad 		ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
    384  1.3  riastrad 		if (ret)
    385  1.3  riastrad 			DRM_ERROR("Failed to blank CRTC\n");
    386  1.1  riastrad 
    387  1.3  riastrad 		(void) vmw_stdu_update_st(dev_priv, stdu);
    388  1.1  riastrad 
    389  1.3  riastrad 		ret = vmw_stdu_destroy_st(dev_priv, stdu);
    390  1.3  riastrad 		if (ret)
    391  1.3  riastrad 			DRM_ERROR("Failed to destroy Screen Target\n");
    392  1.1  riastrad 
    393  1.1  riastrad 		stdu->content_fb_type = SAME_AS_DISPLAY;
    394  1.1  riastrad 	}
    395  1.1  riastrad 
    396  1.3  riastrad 	if (!crtc->state->enable)
    397  1.3  riastrad 		return;
    398  1.1  riastrad 
    399  1.3  riastrad 	x = vmw_conn_state->gui_x;
    400  1.3  riastrad 	y = vmw_conn_state->gui_y;
    401  1.1  riastrad 
    402  1.1  riastrad 	vmw_svga_enable(dev_priv);
    403  1.3  riastrad 	ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, x, y);
    404  1.1  riastrad 
    405  1.3  riastrad 	if (ret)
    406  1.3  riastrad 		DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
    407  1.3  riastrad 			  crtc->x, crtc->y);
    408  1.3  riastrad }
    409  1.1  riastrad 
    410  1.1  riastrad 
    411  1.3  riastrad static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
    412  1.3  riastrad {
    413  1.3  riastrad }
    414  1.1  riastrad 
    415  1.3  riastrad static void vmw_stdu_crtc_atomic_enable(struct drm_crtc *crtc,
    416  1.3  riastrad 					struct drm_crtc_state *old_state)
    417  1.3  riastrad {
    418  1.1  riastrad }
    419  1.1  riastrad 
    420  1.3  riastrad static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
    421  1.3  riastrad 					 struct drm_crtc_state *old_state)
    422  1.1  riastrad {
    423  1.3  riastrad 	struct vmw_private *dev_priv;
    424  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu;
    425  1.1  riastrad 	int ret;
    426  1.1  riastrad 
    427  1.1  riastrad 
    428  1.3  riastrad 	if (!crtc) {
    429  1.3  riastrad 		DRM_ERROR("CRTC is NULL\n");
    430  1.3  riastrad 		return;
    431  1.3  riastrad 	}
    432  1.1  riastrad 
    433  1.3  riastrad 	stdu     = vmw_crtc_to_stdu(crtc);
    434  1.3  riastrad 	dev_priv = vmw_priv(crtc->dev);
    435  1.1  riastrad 
    436  1.3  riastrad 	if (stdu->defined) {
    437  1.3  riastrad 		ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
    438  1.3  riastrad 		if (ret)
    439  1.3  riastrad 			DRM_ERROR("Failed to blank CRTC\n");
    440  1.1  riastrad 
    441  1.1  riastrad 		(void) vmw_stdu_update_st(dev_priv, stdu);
    442  1.1  riastrad 
    443  1.3  riastrad 		ret = vmw_stdu_destroy_st(dev_priv, stdu);
    444  1.3  riastrad 		if (ret)
    445  1.3  riastrad 			DRM_ERROR("Failed to destroy Screen Target\n");
    446  1.1  riastrad 
    447  1.3  riastrad 		stdu->content_fb_type = SAME_AS_DISPLAY;
    448  1.1  riastrad 	}
    449  1.1  riastrad }
    450  1.1  riastrad 
    451  1.1  riastrad /**
    452  1.3  riastrad  * vmw_stdu_bo_clip - Callback to encode a suface DMA command cliprect
    453  1.1  riastrad  *
    454  1.1  riastrad  * @dirty: The closure structure.
    455  1.1  riastrad  *
    456  1.1  riastrad  * Encodes a surface DMA command cliprect and updates the bounding box
    457  1.1  riastrad  * for the DMA.
    458  1.1  riastrad  */
    459  1.3  riastrad static void vmw_stdu_bo_clip(struct vmw_kms_dirty *dirty)
    460  1.1  riastrad {
    461  1.1  riastrad 	struct vmw_stdu_dirty *ddirty =
    462  1.1  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    463  1.1  riastrad 	struct vmw_stdu_dma *cmd = dirty->cmd;
    464  1.1  riastrad 	struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
    465  1.1  riastrad 
    466  1.1  riastrad 	blit += dirty->num_hits;
    467  1.1  riastrad 	blit->srcx = dirty->fb_x;
    468  1.1  riastrad 	blit->srcy = dirty->fb_y;
    469  1.1  riastrad 	blit->x = dirty->unit_x1;
    470  1.1  riastrad 	blit->y = dirty->unit_y1;
    471  1.1  riastrad 	blit->d = 1;
    472  1.1  riastrad 	blit->w = dirty->unit_x2 - dirty->unit_x1;
    473  1.1  riastrad 	blit->h = dirty->unit_y2 - dirty->unit_y1;
    474  1.1  riastrad 	dirty->num_hits++;
    475  1.1  riastrad 
    476  1.1  riastrad 	if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
    477  1.1  riastrad 		return;
    478  1.1  riastrad 
    479  1.1  riastrad 	/* Destination bounding box */
    480  1.1  riastrad 	ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
    481  1.1  riastrad 	ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
    482  1.1  riastrad 	ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
    483  1.1  riastrad 	ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
    484  1.1  riastrad }
    485  1.1  riastrad 
    486  1.1  riastrad /**
    487  1.3  riastrad  * vmw_stdu_bo_fifo_commit - Callback to fill in and submit a DMA command.
    488  1.1  riastrad  *
    489  1.1  riastrad  * @dirty: The closure structure.
    490  1.1  riastrad  *
    491  1.1  riastrad  * Fills in the missing fields in a DMA command, and optionally encodes
    492  1.1  riastrad  * a screen target update command, depending on transfer direction.
    493  1.1  riastrad  */
    494  1.3  riastrad static void vmw_stdu_bo_fifo_commit(struct vmw_kms_dirty *dirty)
    495  1.1  riastrad {
    496  1.1  riastrad 	struct vmw_stdu_dirty *ddirty =
    497  1.1  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    498  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu =
    499  1.1  riastrad 		container_of(dirty->unit, typeof(*stdu), base);
    500  1.1  riastrad 	struct vmw_stdu_dma *cmd = dirty->cmd;
    501  1.1  riastrad 	struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
    502  1.1  riastrad 	SVGA3dCmdSurfaceDMASuffix *suffix =
    503  1.1  riastrad 		(SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
    504  1.1  riastrad 	size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
    505  1.1  riastrad 
    506  1.1  riastrad 	if (!dirty->num_hits) {
    507  1.1  riastrad 		vmw_fifo_commit(dirty->dev_priv, 0);
    508  1.1  riastrad 		return;
    509  1.1  riastrad 	}
    510  1.1  riastrad 
    511  1.1  riastrad 	cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
    512  1.1  riastrad 	cmd->header.size = sizeof(cmd->body) + blit_size;
    513  1.1  riastrad 	vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
    514  1.1  riastrad 	cmd->body.guest.pitch = ddirty->pitch;
    515  1.1  riastrad 	cmd->body.host.sid = stdu->display_srf->res.id;
    516  1.1  riastrad 	cmd->body.host.face = 0;
    517  1.1  riastrad 	cmd->body.host.mipmap = 0;
    518  1.1  riastrad 	cmd->body.transfer = ddirty->transfer;
    519  1.1  riastrad 	suffix->suffixSize = sizeof(*suffix);
    520  1.1  riastrad 	suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
    521  1.1  riastrad 
    522  1.1  riastrad 	if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
    523  1.1  riastrad 		blit_size += sizeof(struct vmw_stdu_update);
    524  1.1  riastrad 
    525  1.1  riastrad 		vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
    526  1.1  riastrad 					 ddirty->left, ddirty->right,
    527  1.1  riastrad 					 ddirty->top, ddirty->bottom);
    528  1.1  riastrad 	}
    529  1.1  riastrad 
    530  1.1  riastrad 	vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
    531  1.1  riastrad 
    532  1.3  riastrad 	stdu->display_srf->res.res_dirty = true;
    533  1.1  riastrad 	ddirty->left = ddirty->top = S32_MAX;
    534  1.1  riastrad 	ddirty->right = ddirty->bottom = S32_MIN;
    535  1.1  riastrad }
    536  1.1  riastrad 
    537  1.3  riastrad 
    538  1.3  riastrad /**
    539  1.3  riastrad  * vmw_stdu_bo_cpu_clip - Callback to encode a CPU blit
    540  1.3  riastrad  *
    541  1.3  riastrad  * @dirty: The closure structure.
    542  1.3  riastrad  *
    543  1.3  riastrad  * This function calculates the bounding box for all the incoming clips.
    544  1.3  riastrad  */
    545  1.3  riastrad static void vmw_stdu_bo_cpu_clip(struct vmw_kms_dirty *dirty)
    546  1.3  riastrad {
    547  1.3  riastrad 	struct vmw_stdu_dirty *ddirty =
    548  1.3  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    549  1.3  riastrad 
    550  1.3  riastrad 	dirty->num_hits = 1;
    551  1.3  riastrad 
    552  1.3  riastrad 	/* Calculate destination bounding box */
    553  1.3  riastrad 	ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
    554  1.3  riastrad 	ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
    555  1.3  riastrad 	ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
    556  1.3  riastrad 	ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
    557  1.3  riastrad 
    558  1.3  riastrad 	/*
    559  1.3  riastrad 	 * Calculate content bounding box.  We only need the top-left
    560  1.3  riastrad 	 * coordinate because width and height will be the same as the
    561  1.3  riastrad 	 * destination bounding box above
    562  1.3  riastrad 	 */
    563  1.3  riastrad 	ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
    564  1.3  riastrad 	ddirty->fb_top  = min_t(s32, ddirty->fb_top, dirty->fb_y);
    565  1.3  riastrad }
    566  1.3  riastrad 
    567  1.3  riastrad 
    568  1.3  riastrad /**
    569  1.3  riastrad  * vmw_stdu_bo_cpu_commit - Callback to do a CPU blit from buffer object
    570  1.3  riastrad  *
    571  1.3  riastrad  * @dirty: The closure structure.
    572  1.3  riastrad  *
    573  1.3  riastrad  * For the special case when we cannot create a proxy surface in a
    574  1.3  riastrad  * 2D VM, we have to do a CPU blit ourselves.
    575  1.3  riastrad  */
    576  1.3  riastrad static void vmw_stdu_bo_cpu_commit(struct vmw_kms_dirty *dirty)
    577  1.3  riastrad {
    578  1.3  riastrad 	struct vmw_stdu_dirty *ddirty =
    579  1.3  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    580  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu =
    581  1.3  riastrad 		container_of(dirty->unit, typeof(*stdu), base);
    582  1.3  riastrad 	s32 width, height;
    583  1.3  riastrad 	s32 src_pitch, dst_pitch;
    584  1.3  riastrad 	struct ttm_buffer_object *src_bo, *dst_bo;
    585  1.3  riastrad 	u32 src_offset, dst_offset;
    586  1.3  riastrad 	struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(stdu->cpp);
    587  1.3  riastrad 
    588  1.3  riastrad 	if (!dirty->num_hits)
    589  1.3  riastrad 		return;
    590  1.3  riastrad 
    591  1.3  riastrad 	width = ddirty->right - ddirty->left;
    592  1.3  riastrad 	height = ddirty->bottom - ddirty->top;
    593  1.3  riastrad 
    594  1.3  riastrad 	if (width == 0 || height == 0)
    595  1.3  riastrad 		return;
    596  1.3  riastrad 
    597  1.3  riastrad 	/* Assume we are blitting from Guest (bo) to Host (display_srf) */
    598  1.3  riastrad 	dst_pitch = stdu->display_srf->base_size.width * stdu->cpp;
    599  1.3  riastrad 	dst_bo = &stdu->display_srf->res.backup->base;
    600  1.3  riastrad 	dst_offset = ddirty->top * dst_pitch + ddirty->left * stdu->cpp;
    601  1.3  riastrad 
    602  1.3  riastrad 	src_pitch = ddirty->pitch;
    603  1.3  riastrad 	src_bo = &ddirty->buf->base;
    604  1.3  riastrad 	src_offset = ddirty->fb_top * src_pitch + ddirty->fb_left * stdu->cpp;
    605  1.3  riastrad 
    606  1.3  riastrad 	/* Swap src and dst if the assumption was wrong. */
    607  1.3  riastrad 	if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM) {
    608  1.3  riastrad 		swap(dst_pitch, src_pitch);
    609  1.3  riastrad 		swap(dst_bo, src_bo);
    610  1.3  riastrad 		swap(src_offset, dst_offset);
    611  1.3  riastrad 	}
    612  1.3  riastrad 
    613  1.3  riastrad 	(void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch,
    614  1.3  riastrad 			       src_bo, src_offset, src_pitch,
    615  1.3  riastrad 			       width * stdu->cpp, height, &diff);
    616  1.3  riastrad 
    617  1.3  riastrad 	if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM &&
    618  1.3  riastrad 	    drm_rect_visible(&diff.rect)) {
    619  1.3  riastrad 		struct vmw_private *dev_priv;
    620  1.3  riastrad 		struct vmw_stdu_update *cmd;
    621  1.3  riastrad 		struct drm_clip_rect region;
    622  1.3  riastrad 		int ret;
    623  1.3  riastrad 
    624  1.3  riastrad 		/* We are updating the actual surface, not a proxy */
    625  1.3  riastrad 		region.x1 = diff.rect.x1;
    626  1.3  riastrad 		region.x2 = diff.rect.x2;
    627  1.3  riastrad 		region.y1 = diff.rect.y1;
    628  1.3  riastrad 		region.y2 = diff.rect.y2;
    629  1.3  riastrad 		ret = vmw_kms_update_proxy(&stdu->display_srf->res, &region,
    630  1.3  riastrad 					   1, 1);
    631  1.3  riastrad 		if (ret)
    632  1.3  riastrad 			goto out_cleanup;
    633  1.3  riastrad 
    634  1.3  riastrad 
    635  1.3  riastrad 		dev_priv = vmw_priv(stdu->base.crtc.dev);
    636  1.3  riastrad 		cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    637  1.3  riastrad 		if (!cmd)
    638  1.3  riastrad 			goto out_cleanup;
    639  1.3  riastrad 
    640  1.3  riastrad 		vmw_stdu_populate_update(cmd, stdu->base.unit,
    641  1.3  riastrad 					 region.x1, region.x2,
    642  1.3  riastrad 					 region.y1, region.y2);
    643  1.3  riastrad 
    644  1.3  riastrad 		vmw_fifo_commit(dev_priv, sizeof(*cmd));
    645  1.3  riastrad 	}
    646  1.3  riastrad 
    647  1.3  riastrad out_cleanup:
    648  1.3  riastrad 	ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
    649  1.3  riastrad 	ddirty->right = ddirty->bottom = S32_MIN;
    650  1.3  riastrad }
    651  1.3  riastrad 
    652  1.1  riastrad /**
    653  1.3  riastrad  * vmw_kms_stdu_dma - Perform a DMA transfer between a buffer-object backed
    654  1.1  riastrad  * framebuffer and the screen target system.
    655  1.1  riastrad  *
    656  1.1  riastrad  * @dev_priv: Pointer to the device private structure.
    657  1.1  riastrad  * @file_priv: Pointer to a struct drm-file identifying the caller. May be
    658  1.1  riastrad  * set to NULL, but then @user_fence_rep must also be set to NULL.
    659  1.3  riastrad  * @vfb: Pointer to the buffer-object backed framebuffer.
    660  1.1  riastrad  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
    661  1.1  riastrad  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
    662  1.1  riastrad  * be NULL.
    663  1.1  riastrad  * @num_clips: Number of clip rects in @clips or @vclips.
    664  1.1  riastrad  * @increment: Increment to use when looping over @clips or @vclips.
    665  1.1  riastrad  * @to_surface: Whether to DMA to the screen target system as opposed to
    666  1.1  riastrad  * from the screen target system.
    667  1.1  riastrad  * @interruptible: Whether to perform waits interruptible if possible.
    668  1.3  riastrad  * @crtc: If crtc is passed, perform stdu dma on that crtc only.
    669  1.1  riastrad  *
    670  1.1  riastrad  * If DMA-ing till the screen target system, the function will also notify
    671  1.1  riastrad  * the screen target system that a bounding box of the cliprects has been
    672  1.1  riastrad  * updated.
    673  1.1  riastrad  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
    674  1.1  riastrad  * interrupted.
    675  1.1  riastrad  */
    676  1.1  riastrad int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
    677  1.1  riastrad 		     struct drm_file *file_priv,
    678  1.1  riastrad 		     struct vmw_framebuffer *vfb,
    679  1.1  riastrad 		     struct drm_vmw_fence_rep __user *user_fence_rep,
    680  1.1  riastrad 		     struct drm_clip_rect *clips,
    681  1.1  riastrad 		     struct drm_vmw_rect *vclips,
    682  1.1  riastrad 		     uint32_t num_clips,
    683  1.1  riastrad 		     int increment,
    684  1.1  riastrad 		     bool to_surface,
    685  1.3  riastrad 		     bool interruptible,
    686  1.3  riastrad 		     struct drm_crtc *crtc)
    687  1.1  riastrad {
    688  1.3  riastrad 	struct vmw_buffer_object *buf =
    689  1.3  riastrad 		container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
    690  1.1  riastrad 	struct vmw_stdu_dirty ddirty;
    691  1.1  riastrad 	int ret;
    692  1.3  riastrad 	bool cpu_blit = !(dev_priv->capabilities & SVGA_CAP_3D);
    693  1.3  riastrad 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
    694  1.1  riastrad 
    695  1.3  riastrad 	/*
    696  1.3  riastrad 	 * VMs without 3D support don't have the surface DMA command and
    697  1.3  riastrad 	 * we'll be using a CPU blit, and the framebuffer should be moved out
    698  1.3  riastrad 	 * of VRAM.
    699  1.3  riastrad 	 */
    700  1.3  riastrad 	ret = vmw_validation_add_bo(&val_ctx, buf, false, cpu_blit);
    701  1.1  riastrad 	if (ret)
    702  1.1  riastrad 		return ret;
    703  1.1  riastrad 
    704  1.3  riastrad 	ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
    705  1.3  riastrad 	if (ret)
    706  1.3  riastrad 		goto out_unref;
    707  1.3  riastrad 
    708  1.1  riastrad 	ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
    709  1.1  riastrad 		SVGA3D_READ_HOST_VRAM;
    710  1.1  riastrad 	ddirty.left = ddirty.top = S32_MAX;
    711  1.1  riastrad 	ddirty.right = ddirty.bottom = S32_MIN;
    712  1.3  riastrad 	ddirty.fb_left = ddirty.fb_top = S32_MAX;
    713  1.1  riastrad 	ddirty.pitch = vfb->base.pitches[0];
    714  1.1  riastrad 	ddirty.buf = buf;
    715  1.3  riastrad 	ddirty.base.fifo_commit = vmw_stdu_bo_fifo_commit;
    716  1.3  riastrad 	ddirty.base.clip = vmw_stdu_bo_clip;
    717  1.1  riastrad 	ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
    718  1.1  riastrad 		num_clips * sizeof(SVGA3dCopyBox) +
    719  1.1  riastrad 		sizeof(SVGA3dCmdSurfaceDMASuffix);
    720  1.1  riastrad 	if (to_surface)
    721  1.1  riastrad 		ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
    722  1.1  riastrad 
    723  1.3  riastrad 
    724  1.3  riastrad 	if (cpu_blit) {
    725  1.3  riastrad 		ddirty.base.fifo_commit = vmw_stdu_bo_cpu_commit;
    726  1.3  riastrad 		ddirty.base.clip = vmw_stdu_bo_cpu_clip;
    727  1.3  riastrad 		ddirty.base.fifo_reserve_size = 0;
    728  1.3  riastrad 	}
    729  1.3  riastrad 
    730  1.3  riastrad 	ddirty.base.crtc = crtc;
    731  1.3  riastrad 
    732  1.1  riastrad 	ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
    733  1.1  riastrad 				   0, 0, num_clips, increment, &ddirty.base);
    734  1.1  riastrad 
    735  1.3  riastrad 	vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
    736  1.3  riastrad 					 user_fence_rep);
    737  1.3  riastrad 	return ret;
    738  1.3  riastrad 
    739  1.3  riastrad out_unref:
    740  1.3  riastrad 	vmw_validation_unref_lists(&val_ctx);
    741  1.1  riastrad 	return ret;
    742  1.1  riastrad }
    743  1.1  riastrad 
    744  1.1  riastrad /**
    745  1.1  riastrad  * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
    746  1.1  riastrad  *
    747  1.1  riastrad  * @dirty: The closure structure.
    748  1.1  riastrad  *
    749  1.1  riastrad  * Encodes a surface copy command cliprect and updates the bounding box
    750  1.1  riastrad  * for the copy.
    751  1.1  riastrad  */
    752  1.1  riastrad static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
    753  1.1  riastrad {
    754  1.1  riastrad 	struct vmw_stdu_dirty *sdirty =
    755  1.1  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    756  1.1  riastrad 	struct vmw_stdu_surface_copy *cmd = dirty->cmd;
    757  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu =
    758  1.1  riastrad 		container_of(dirty->unit, typeof(*stdu), base);
    759  1.1  riastrad 
    760  1.1  riastrad 	if (sdirty->sid != stdu->display_srf->res.id) {
    761  1.1  riastrad 		struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
    762  1.1  riastrad 
    763  1.1  riastrad 		blit += dirty->num_hits;
    764  1.1  riastrad 		blit->srcx = dirty->fb_x;
    765  1.1  riastrad 		blit->srcy = dirty->fb_y;
    766  1.1  riastrad 		blit->x = dirty->unit_x1;
    767  1.1  riastrad 		blit->y = dirty->unit_y1;
    768  1.1  riastrad 		blit->d = 1;
    769  1.1  riastrad 		blit->w = dirty->unit_x2 - dirty->unit_x1;
    770  1.1  riastrad 		blit->h = dirty->unit_y2 - dirty->unit_y1;
    771  1.1  riastrad 	}
    772  1.1  riastrad 
    773  1.1  riastrad 	dirty->num_hits++;
    774  1.1  riastrad 
    775  1.1  riastrad 	/* Destination bounding box */
    776  1.1  riastrad 	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
    777  1.1  riastrad 	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
    778  1.1  riastrad 	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
    779  1.1  riastrad 	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
    780  1.1  riastrad }
    781  1.1  riastrad 
    782  1.1  riastrad /**
    783  1.1  riastrad  * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
    784  1.1  riastrad  * copy command.
    785  1.1  riastrad  *
    786  1.1  riastrad  * @dirty: The closure structure.
    787  1.1  riastrad  *
    788  1.1  riastrad  * Fills in the missing fields in a surface copy command, and encodes a screen
    789  1.1  riastrad  * target update command.
    790  1.1  riastrad  */
    791  1.1  riastrad static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
    792  1.1  riastrad {
    793  1.1  riastrad 	struct vmw_stdu_dirty *sdirty =
    794  1.1  riastrad 		container_of(dirty, struct vmw_stdu_dirty, base);
    795  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu =
    796  1.1  riastrad 		container_of(dirty->unit, typeof(*stdu), base);
    797  1.1  riastrad 	struct vmw_stdu_surface_copy *cmd = dirty->cmd;
    798  1.1  riastrad 	struct vmw_stdu_update *update;
    799  1.1  riastrad 	size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
    800  1.1  riastrad 	size_t commit_size;
    801  1.1  riastrad 
    802  1.1  riastrad 	if (!dirty->num_hits) {
    803  1.1  riastrad 		vmw_fifo_commit(dirty->dev_priv, 0);
    804  1.1  riastrad 		return;
    805  1.1  riastrad 	}
    806  1.1  riastrad 
    807  1.1  riastrad 	if (sdirty->sid != stdu->display_srf->res.id) {
    808  1.1  riastrad 		struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
    809  1.1  riastrad 
    810  1.1  riastrad 		cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
    811  1.1  riastrad 		cmd->header.size = sizeof(cmd->body) + blit_size;
    812  1.1  riastrad 		cmd->body.src.sid = sdirty->sid;
    813  1.1  riastrad 		cmd->body.dest.sid = stdu->display_srf->res.id;
    814  1.1  riastrad 		update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
    815  1.1  riastrad 		commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
    816  1.3  riastrad 		stdu->display_srf->res.res_dirty = true;
    817  1.1  riastrad 	} else {
    818  1.1  riastrad 		update = dirty->cmd;
    819  1.1  riastrad 		commit_size = sizeof(*update);
    820  1.1  riastrad 	}
    821  1.1  riastrad 
    822  1.1  riastrad 	vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
    823  1.1  riastrad 				 sdirty->right, sdirty->top, sdirty->bottom);
    824  1.1  riastrad 
    825  1.1  riastrad 	vmw_fifo_commit(dirty->dev_priv, commit_size);
    826  1.1  riastrad 
    827  1.1  riastrad 	sdirty->left = sdirty->top = S32_MAX;
    828  1.1  riastrad 	sdirty->right = sdirty->bottom = S32_MIN;
    829  1.1  riastrad }
    830  1.1  riastrad 
    831  1.1  riastrad /**
    832  1.1  riastrad  * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
    833  1.1  riastrad  *
    834  1.1  riastrad  * @dev_priv: Pointer to the device private structure.
    835  1.1  riastrad  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
    836  1.1  riastrad  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
    837  1.1  riastrad  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
    838  1.1  riastrad  * be NULL.
    839  1.1  riastrad  * @srf: Pointer to surface to blit from. If NULL, the surface attached
    840  1.1  riastrad  * to @framebuffer will be used.
    841  1.1  riastrad  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
    842  1.1  riastrad  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
    843  1.1  riastrad  * @num_clips: Number of clip rects in @clips.
    844  1.1  riastrad  * @inc: Increment to use when looping over @clips.
    845  1.1  riastrad  * @out_fence: If non-NULL, will return a ref-counted pointer to a
    846  1.1  riastrad  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
    847  1.1  riastrad  * case the device has already synchronized.
    848  1.3  riastrad  * @crtc: If crtc is passed, perform surface dirty on that crtc only.
    849  1.1  riastrad  *
    850  1.1  riastrad  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
    851  1.1  riastrad  * interrupted.
    852  1.1  riastrad  */
    853  1.1  riastrad int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
    854  1.1  riastrad 			       struct vmw_framebuffer *framebuffer,
    855  1.1  riastrad 			       struct drm_clip_rect *clips,
    856  1.1  riastrad 			       struct drm_vmw_rect *vclips,
    857  1.1  riastrad 			       struct vmw_resource *srf,
    858  1.1  riastrad 			       s32 dest_x,
    859  1.1  riastrad 			       s32 dest_y,
    860  1.1  riastrad 			       unsigned num_clips, int inc,
    861  1.3  riastrad 			       struct vmw_fence_obj **out_fence,
    862  1.3  riastrad 			       struct drm_crtc *crtc)
    863  1.1  riastrad {
    864  1.1  riastrad 	struct vmw_framebuffer_surface *vfbs =
    865  1.1  riastrad 		container_of(framebuffer, typeof(*vfbs), base);
    866  1.1  riastrad 	struct vmw_stdu_dirty sdirty;
    867  1.3  riastrad 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
    868  1.1  riastrad 	int ret;
    869  1.1  riastrad 
    870  1.1  riastrad 	if (!srf)
    871  1.1  riastrad 		srf = &vfbs->surface->res;
    872  1.1  riastrad 
    873  1.3  riastrad 	ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
    874  1.3  riastrad 					  NULL, NULL);
    875  1.1  riastrad 	if (ret)
    876  1.1  riastrad 		return ret;
    877  1.1  riastrad 
    878  1.3  riastrad 	ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
    879  1.3  riastrad 	if (ret)
    880  1.3  riastrad 		goto out_unref;
    881  1.3  riastrad 
    882  1.3  riastrad 	if (vfbs->is_bo_proxy) {
    883  1.1  riastrad 		ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
    884  1.1  riastrad 		if (ret)
    885  1.1  riastrad 			goto out_finish;
    886  1.1  riastrad 	}
    887  1.1  riastrad 
    888  1.1  riastrad 	sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
    889  1.1  riastrad 	sdirty.base.clip = vmw_kms_stdu_surface_clip;
    890  1.1  riastrad 	sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
    891  1.1  riastrad 		sizeof(SVGA3dCopyBox) * num_clips +
    892  1.1  riastrad 		sizeof(struct vmw_stdu_update);
    893  1.3  riastrad 	sdirty.base.crtc = crtc;
    894  1.1  riastrad 	sdirty.sid = srf->id;
    895  1.1  riastrad 	sdirty.left = sdirty.top = S32_MAX;
    896  1.1  riastrad 	sdirty.right = sdirty.bottom = S32_MIN;
    897  1.1  riastrad 
    898  1.1  riastrad 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
    899  1.1  riastrad 				   dest_x, dest_y, num_clips, inc,
    900  1.1  riastrad 				   &sdirty.base);
    901  1.1  riastrad out_finish:
    902  1.3  riastrad 	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
    903  1.3  riastrad 					 NULL);
    904  1.1  riastrad 
    905  1.1  riastrad 	return ret;
    906  1.3  riastrad 
    907  1.3  riastrad out_unref:
    908  1.3  riastrad 	vmw_validation_unref_lists(&val_ctx);
    909  1.3  riastrad 	return ret;
    910  1.1  riastrad }
    911  1.1  riastrad 
    912  1.1  riastrad 
    913  1.1  riastrad /*
    914  1.1  riastrad  *  Screen Target CRTC dispatch table
    915  1.1  riastrad  */
    916  1.3  riastrad static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
    917  1.1  riastrad 	.gamma_set = vmw_du_crtc_gamma_set,
    918  1.1  riastrad 	.destroy = vmw_stdu_crtc_destroy,
    919  1.3  riastrad 	.reset = vmw_du_crtc_reset,
    920  1.3  riastrad 	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
    921  1.3  riastrad 	.atomic_destroy_state = vmw_du_crtc_destroy_state,
    922  1.3  riastrad 	.set_config = drm_atomic_helper_set_config,
    923  1.3  riastrad 	.page_flip = drm_atomic_helper_page_flip,
    924  1.1  riastrad };
    925  1.1  riastrad 
    926  1.1  riastrad 
    927  1.1  riastrad 
    928  1.1  riastrad /******************************************************************************
    929  1.1  riastrad  * Screen Target Display Unit Encoder Functions
    930  1.1  riastrad  *****************************************************************************/
    931  1.1  riastrad 
    932  1.1  riastrad /**
    933  1.1  riastrad  * vmw_stdu_encoder_destroy - cleans up the STDU
    934  1.1  riastrad  *
    935  1.1  riastrad  * @encoder: used the get the containing STDU
    936  1.1  riastrad  *
    937  1.1  riastrad  * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
    938  1.1  riastrad  * this can be a no-op.  Nevertheless, it doesn't hurt of have this in case
    939  1.1  riastrad  * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
    940  1.1  riastrad  * get called.
    941  1.1  riastrad  */
    942  1.1  riastrad static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
    943  1.1  riastrad {
    944  1.1  riastrad 	vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
    945  1.1  riastrad }
    946  1.1  riastrad 
    947  1.3  riastrad static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
    948  1.1  riastrad 	.destroy = vmw_stdu_encoder_destroy,
    949  1.1  riastrad };
    950  1.1  riastrad 
    951  1.1  riastrad 
    952  1.1  riastrad 
    953  1.1  riastrad /******************************************************************************
    954  1.1  riastrad  * Screen Target Display Unit Connector Functions
    955  1.1  riastrad  *****************************************************************************/
    956  1.1  riastrad 
    957  1.1  riastrad /**
    958  1.1  riastrad  * vmw_stdu_connector_destroy - cleans up the STDU
    959  1.1  riastrad  *
    960  1.1  riastrad  * @connector: used to get the containing STDU
    961  1.1  riastrad  *
    962  1.1  riastrad  * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
    963  1.1  riastrad  * this can be a no-op.  Nevertheless, it doesn't hurt of have this in case
    964  1.1  riastrad  * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
    965  1.1  riastrad  * get called.
    966  1.1  riastrad  */
    967  1.1  riastrad static void vmw_stdu_connector_destroy(struct drm_connector *connector)
    968  1.1  riastrad {
    969  1.1  riastrad 	vmw_stdu_destroy(vmw_connector_to_stdu(connector));
    970  1.1  riastrad }
    971  1.1  riastrad 
    972  1.1  riastrad 
    973  1.1  riastrad 
    974  1.3  riastrad static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
    975  1.1  riastrad 	.dpms = vmw_du_connector_dpms,
    976  1.1  riastrad 	.detect = vmw_du_connector_detect,
    977  1.1  riastrad 	.fill_modes = vmw_du_connector_fill_modes,
    978  1.1  riastrad 	.destroy = vmw_stdu_connector_destroy,
    979  1.3  riastrad 	.reset = vmw_du_connector_reset,
    980  1.3  riastrad 	.atomic_duplicate_state = vmw_du_connector_duplicate_state,
    981  1.3  riastrad 	.atomic_destroy_state = vmw_du_connector_destroy_state,
    982  1.3  riastrad };
    983  1.3  riastrad 
    984  1.3  riastrad 
    985  1.3  riastrad static const struct
    986  1.3  riastrad drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
    987  1.3  riastrad };
    988  1.3  riastrad 
    989  1.3  riastrad 
    990  1.3  riastrad 
    991  1.3  riastrad /******************************************************************************
    992  1.3  riastrad  * Screen Target Display Plane Functions
    993  1.3  riastrad  *****************************************************************************/
    994  1.3  riastrad 
    995  1.3  riastrad 
    996  1.3  riastrad 
    997  1.3  riastrad /**
    998  1.3  riastrad  * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
    999  1.3  riastrad  *
   1000  1.3  riastrad  * @plane:  display plane
   1001  1.3  riastrad  * @old_state: Contains the FB to clean up
   1002  1.3  riastrad  *
   1003  1.3  riastrad  * Unpins the display surface
   1004  1.3  riastrad  *
   1005  1.3  riastrad  * Returns 0 on success
   1006  1.3  riastrad  */
   1007  1.3  riastrad static void
   1008  1.3  riastrad vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
   1009  1.3  riastrad 				  struct drm_plane_state *old_state)
   1010  1.3  riastrad {
   1011  1.3  riastrad 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
   1012  1.3  riastrad 
   1013  1.3  riastrad 	if (vps->surf)
   1014  1.3  riastrad 		WARN_ON(!vps->pinned);
   1015  1.3  riastrad 
   1016  1.3  riastrad 	vmw_du_plane_cleanup_fb(plane, old_state);
   1017  1.3  riastrad 
   1018  1.3  riastrad 	vps->content_fb_type = SAME_AS_DISPLAY;
   1019  1.3  riastrad 	vps->cpp = 0;
   1020  1.3  riastrad }
   1021  1.3  riastrad 
   1022  1.3  riastrad 
   1023  1.3  riastrad 
   1024  1.3  riastrad /**
   1025  1.3  riastrad  * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
   1026  1.3  riastrad  *
   1027  1.3  riastrad  * @plane:  display plane
   1028  1.3  riastrad  * @new_state: info on the new plane state, including the FB
   1029  1.3  riastrad  *
   1030  1.3  riastrad  * This function allocates a new display surface if the content is
   1031  1.3  riastrad  * backed by a buffer object.  The display surface is pinned here, and it'll
   1032  1.3  riastrad  * be unpinned in .cleanup_fb()
   1033  1.3  riastrad  *
   1034  1.3  riastrad  * Returns 0 on success
   1035  1.3  riastrad  */
   1036  1.3  riastrad static int
   1037  1.3  riastrad vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
   1038  1.3  riastrad 				  struct drm_plane_state *new_state)
   1039  1.3  riastrad {
   1040  1.3  riastrad 	struct vmw_private *dev_priv = vmw_priv(plane->dev);
   1041  1.3  riastrad 	struct drm_framebuffer *new_fb = new_state->fb;
   1042  1.3  riastrad 	struct vmw_framebuffer *vfb;
   1043  1.3  riastrad 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
   1044  1.3  riastrad 	enum stdu_content_type new_content_type;
   1045  1.3  riastrad 	struct vmw_framebuffer_surface *new_vfbs;
   1046  1.3  riastrad 	struct drm_crtc *crtc = new_state->crtc;
   1047  1.3  riastrad 	uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
   1048  1.3  riastrad 	int ret;
   1049  1.3  riastrad 
   1050  1.3  riastrad 	/* No FB to prepare */
   1051  1.3  riastrad 	if (!new_fb) {
   1052  1.3  riastrad 		if (vps->surf) {
   1053  1.3  riastrad 			WARN_ON(vps->pinned != 0);
   1054  1.3  riastrad 			vmw_surface_unreference(&vps->surf);
   1055  1.3  riastrad 		}
   1056  1.3  riastrad 
   1057  1.3  riastrad 		return 0;
   1058  1.3  riastrad 	}
   1059  1.3  riastrad 
   1060  1.3  riastrad 	vfb = vmw_framebuffer_to_vfb(new_fb);
   1061  1.3  riastrad 	new_vfbs = (vfb->bo) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
   1062  1.3  riastrad 
   1063  1.3  riastrad 	if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
   1064  1.3  riastrad 	    new_vfbs->surface->base_size.height == vdisplay)
   1065  1.3  riastrad 		new_content_type = SAME_AS_DISPLAY;
   1066  1.3  riastrad 	else if (vfb->bo)
   1067  1.3  riastrad 		new_content_type = SEPARATE_BO;
   1068  1.3  riastrad 	else
   1069  1.3  riastrad 		new_content_type = SEPARATE_SURFACE;
   1070  1.3  riastrad 
   1071  1.3  riastrad 	if (new_content_type != SAME_AS_DISPLAY) {
   1072  1.3  riastrad 		struct vmw_surface content_srf;
   1073  1.3  riastrad 		struct drm_vmw_size display_base_size = {0};
   1074  1.3  riastrad 
   1075  1.3  riastrad 		display_base_size.width  = hdisplay;
   1076  1.3  riastrad 		display_base_size.height = vdisplay;
   1077  1.3  riastrad 		display_base_size.depth  = 1;
   1078  1.3  riastrad 
   1079  1.3  riastrad 		/*
   1080  1.3  riastrad 		 * If content buffer is a buffer object, then we have to
   1081  1.3  riastrad 		 * construct surface info
   1082  1.3  riastrad 		 */
   1083  1.3  riastrad 		if (new_content_type == SEPARATE_BO) {
   1084  1.3  riastrad 
   1085  1.3  riastrad 			switch (new_fb->format->cpp[0]*8) {
   1086  1.3  riastrad 			case 32:
   1087  1.3  riastrad 				content_srf.format = SVGA3D_X8R8G8B8;
   1088  1.3  riastrad 				break;
   1089  1.3  riastrad 
   1090  1.3  riastrad 			case 16:
   1091  1.3  riastrad 				content_srf.format = SVGA3D_R5G6B5;
   1092  1.3  riastrad 				break;
   1093  1.3  riastrad 
   1094  1.3  riastrad 			case 8:
   1095  1.3  riastrad 				content_srf.format = SVGA3D_P8;
   1096  1.3  riastrad 				break;
   1097  1.3  riastrad 
   1098  1.3  riastrad 			default:
   1099  1.3  riastrad 				DRM_ERROR("Invalid format\n");
   1100  1.3  riastrad 				return -EINVAL;
   1101  1.3  riastrad 			}
   1102  1.3  riastrad 
   1103  1.3  riastrad 			content_srf.flags             = 0;
   1104  1.3  riastrad 			content_srf.mip_levels[0]     = 1;
   1105  1.3  riastrad 			content_srf.multisample_count = 0;
   1106  1.3  riastrad 			content_srf.multisample_pattern =
   1107  1.3  riastrad 				SVGA3D_MS_PATTERN_NONE;
   1108  1.3  riastrad 			content_srf.quality_level = SVGA3D_MS_QUALITY_NONE;
   1109  1.3  riastrad 		} else {
   1110  1.3  riastrad 			content_srf = *new_vfbs->surface;
   1111  1.3  riastrad 		}
   1112  1.3  riastrad 
   1113  1.3  riastrad 		if (vps->surf) {
   1114  1.3  riastrad 			struct drm_vmw_size cur_base_size = vps->surf->base_size;
   1115  1.3  riastrad 
   1116  1.3  riastrad 			if (cur_base_size.width != display_base_size.width ||
   1117  1.3  riastrad 			    cur_base_size.height != display_base_size.height ||
   1118  1.3  riastrad 			    vps->surf->format != content_srf.format) {
   1119  1.3  riastrad 				WARN_ON(vps->pinned != 0);
   1120  1.3  riastrad 				vmw_surface_unreference(&vps->surf);
   1121  1.3  riastrad 			}
   1122  1.3  riastrad 
   1123  1.3  riastrad 		}
   1124  1.3  riastrad 
   1125  1.3  riastrad 		if (!vps->surf) {
   1126  1.3  riastrad 			ret = vmw_surface_gb_priv_define
   1127  1.3  riastrad 				(crtc->dev,
   1128  1.3  riastrad 				 /* Kernel visible only */
   1129  1.3  riastrad 				 0,
   1130  1.3  riastrad 				 content_srf.flags,
   1131  1.3  riastrad 				 content_srf.format,
   1132  1.3  riastrad 				 true,  /* a scanout buffer */
   1133  1.3  riastrad 				 content_srf.mip_levels[0],
   1134  1.3  riastrad 				 content_srf.multisample_count,
   1135  1.3  riastrad 				 0,
   1136  1.3  riastrad 				 display_base_size,
   1137  1.3  riastrad 				 content_srf.multisample_pattern,
   1138  1.3  riastrad 				 content_srf.quality_level,
   1139  1.3  riastrad 				 &vps->surf);
   1140  1.3  riastrad 			if (ret != 0) {
   1141  1.3  riastrad 				DRM_ERROR("Couldn't allocate STDU surface.\n");
   1142  1.3  riastrad 				return ret;
   1143  1.3  riastrad 			}
   1144  1.3  riastrad 		}
   1145  1.3  riastrad 	} else {
   1146  1.3  riastrad 		/*
   1147  1.3  riastrad 		 * prepare_fb and clean_fb should only take care of pinning
   1148  1.3  riastrad 		 * and unpinning.  References are tracked by state objects.
   1149  1.3  riastrad 		 * The only time we add a reference in prepare_fb is if the
   1150  1.3  riastrad 		 * state object doesn't have a reference to begin with
   1151  1.3  riastrad 		 */
   1152  1.3  riastrad 		if (vps->surf) {
   1153  1.3  riastrad 			WARN_ON(vps->pinned != 0);
   1154  1.3  riastrad 			vmw_surface_unreference(&vps->surf);
   1155  1.3  riastrad 		}
   1156  1.3  riastrad 
   1157  1.3  riastrad 		vps->surf = vmw_surface_reference(new_vfbs->surface);
   1158  1.3  riastrad 	}
   1159  1.3  riastrad 
   1160  1.3  riastrad 	if (vps->surf) {
   1161  1.3  riastrad 
   1162  1.3  riastrad 		/* Pin new surface before flipping */
   1163  1.3  riastrad 		ret = vmw_resource_pin(&vps->surf->res, false);
   1164  1.3  riastrad 		if (ret)
   1165  1.3  riastrad 			goto out_srf_unref;
   1166  1.3  riastrad 
   1167  1.3  riastrad 		vps->pinned++;
   1168  1.3  riastrad 	}
   1169  1.3  riastrad 
   1170  1.3  riastrad 	vps->content_fb_type = new_content_type;
   1171  1.3  riastrad 
   1172  1.3  riastrad 	/*
   1173  1.3  riastrad 	 * This should only happen if the buffer object is too large to create a
   1174  1.3  riastrad 	 * proxy surface for.
   1175  1.3  riastrad 	 * If we are a 2D VM with a buffer object then we have to use CPU blit
   1176  1.3  riastrad 	 * so cache these mappings
   1177  1.3  riastrad 	 */
   1178  1.3  riastrad 	if (vps->content_fb_type == SEPARATE_BO &&
   1179  1.3  riastrad 	    !(dev_priv->capabilities & SVGA_CAP_3D))
   1180  1.3  riastrad 		vps->cpp = new_fb->pitches[0] / new_fb->width;
   1181  1.3  riastrad 
   1182  1.3  riastrad 	return 0;
   1183  1.3  riastrad 
   1184  1.3  riastrad out_srf_unref:
   1185  1.3  riastrad 	vmw_surface_unreference(&vps->surf);
   1186  1.3  riastrad 	return ret;
   1187  1.3  riastrad }
   1188  1.3  riastrad 
   1189  1.3  riastrad static uint32_t vmw_stdu_bo_fifo_size(struct vmw_du_update_plane *update,
   1190  1.3  riastrad 				      uint32_t num_hits)
   1191  1.3  riastrad {
   1192  1.3  riastrad 	return sizeof(struct vmw_stdu_dma) + sizeof(SVGA3dCopyBox) * num_hits +
   1193  1.3  riastrad 		sizeof(SVGA3dCmdSurfaceDMASuffix) +
   1194  1.3  riastrad 		sizeof(struct vmw_stdu_update);
   1195  1.3  riastrad }
   1196  1.3  riastrad 
   1197  1.3  riastrad static uint32_t vmw_stdu_bo_fifo_size_cpu(struct vmw_du_update_plane *update,
   1198  1.3  riastrad 					  uint32_t num_hits)
   1199  1.3  riastrad {
   1200  1.3  riastrad 	return sizeof(struct vmw_stdu_update_gb_image) +
   1201  1.3  riastrad 		sizeof(struct vmw_stdu_update);
   1202  1.3  riastrad }
   1203  1.3  riastrad 
   1204  1.3  riastrad static uint32_t vmw_stdu_bo_populate_dma(struct vmw_du_update_plane  *update,
   1205  1.3  riastrad 					 void *cmd, uint32_t num_hits)
   1206  1.3  riastrad {
   1207  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1208  1.3  riastrad 	struct vmw_framebuffer_bo *vfbbo;
   1209  1.3  riastrad 	struct vmw_stdu_dma *cmd_dma = cmd;
   1210  1.3  riastrad 
   1211  1.3  riastrad 	stdu = container_of(update->du, typeof(*stdu), base);
   1212  1.3  riastrad 	vfbbo = container_of(update->vfb, typeof(*vfbbo), base);
   1213  1.3  riastrad 
   1214  1.3  riastrad 	cmd_dma->header.id = SVGA_3D_CMD_SURFACE_DMA;
   1215  1.3  riastrad 	cmd_dma->header.size = sizeof(cmd_dma->body) +
   1216  1.3  riastrad 		sizeof(struct SVGA3dCopyBox) * num_hits +
   1217  1.3  riastrad 		sizeof(SVGA3dCmdSurfaceDMASuffix);
   1218  1.3  riastrad 	vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &cmd_dma->body.guest.ptr);
   1219  1.3  riastrad 	cmd_dma->body.guest.pitch = update->vfb->base.pitches[0];
   1220  1.3  riastrad 	cmd_dma->body.host.sid = stdu->display_srf->res.id;
   1221  1.3  riastrad 	cmd_dma->body.host.face = 0;
   1222  1.3  riastrad 	cmd_dma->body.host.mipmap = 0;
   1223  1.3  riastrad 	cmd_dma->body.transfer = SVGA3D_WRITE_HOST_VRAM;
   1224  1.3  riastrad 
   1225  1.3  riastrad 	return sizeof(*cmd_dma);
   1226  1.3  riastrad }
   1227  1.3  riastrad 
   1228  1.3  riastrad static uint32_t vmw_stdu_bo_populate_clip(struct vmw_du_update_plane  *update,
   1229  1.3  riastrad 					  void *cmd, struct drm_rect *clip,
   1230  1.3  riastrad 					  uint32_t fb_x, uint32_t fb_y)
   1231  1.3  riastrad {
   1232  1.3  riastrad 	struct SVGA3dCopyBox *box = cmd;
   1233  1.3  riastrad 
   1234  1.3  riastrad 	box->srcx = fb_x;
   1235  1.3  riastrad 	box->srcy = fb_y;
   1236  1.3  riastrad 	box->srcz = 0;
   1237  1.3  riastrad 	box->x = clip->x1;
   1238  1.3  riastrad 	box->y = clip->y1;
   1239  1.3  riastrad 	box->z = 0;
   1240  1.3  riastrad 	box->w = drm_rect_width(clip);
   1241  1.3  riastrad 	box->h = drm_rect_height(clip);
   1242  1.3  riastrad 	box->d = 1;
   1243  1.3  riastrad 
   1244  1.3  riastrad 	return sizeof(*box);
   1245  1.3  riastrad }
   1246  1.3  riastrad 
   1247  1.3  riastrad static uint32_t vmw_stdu_bo_populate_update(struct vmw_du_update_plane  *update,
   1248  1.3  riastrad 					    void *cmd, struct drm_rect *bb)
   1249  1.3  riastrad {
   1250  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1251  1.3  riastrad 	struct vmw_framebuffer_bo *vfbbo;
   1252  1.3  riastrad 	SVGA3dCmdSurfaceDMASuffix *suffix = cmd;
   1253  1.3  riastrad 
   1254  1.3  riastrad 	stdu = container_of(update->du, typeof(*stdu), base);
   1255  1.3  riastrad 	vfbbo = container_of(update->vfb, typeof(*vfbbo), base);
   1256  1.3  riastrad 
   1257  1.3  riastrad 	suffix->suffixSize = sizeof(*suffix);
   1258  1.3  riastrad 	suffix->maximumOffset = vfbbo->buffer->base.num_pages * PAGE_SIZE;
   1259  1.3  riastrad 
   1260  1.3  riastrad 	vmw_stdu_populate_update(&suffix[1], stdu->base.unit, bb->x1, bb->x2,
   1261  1.3  riastrad 				 bb->y1, bb->y2);
   1262  1.3  riastrad 
   1263  1.3  riastrad 	return sizeof(*suffix) + sizeof(struct vmw_stdu_update);
   1264  1.3  riastrad }
   1265  1.3  riastrad 
   1266  1.3  riastrad static uint32_t vmw_stdu_bo_pre_clip_cpu(struct vmw_du_update_plane  *update,
   1267  1.3  riastrad 					 void *cmd, uint32_t num_hits)
   1268  1.3  riastrad {
   1269  1.3  riastrad 	struct vmw_du_update_plane_buffer *bo_update =
   1270  1.3  riastrad 		container_of(update, typeof(*bo_update), base);
   1271  1.3  riastrad 
   1272  1.3  riastrad 	bo_update->fb_left = INT_MAX;
   1273  1.3  riastrad 	bo_update->fb_top = INT_MAX;
   1274  1.3  riastrad 
   1275  1.3  riastrad 	return 0;
   1276  1.3  riastrad }
   1277  1.3  riastrad 
   1278  1.3  riastrad static uint32_t vmw_stdu_bo_clip_cpu(struct vmw_du_update_plane  *update,
   1279  1.3  riastrad 				     void *cmd, struct drm_rect *clip,
   1280  1.3  riastrad 				     uint32_t fb_x, uint32_t fb_y)
   1281  1.3  riastrad {
   1282  1.3  riastrad 	struct vmw_du_update_plane_buffer *bo_update =
   1283  1.3  riastrad 		container_of(update, typeof(*bo_update), base);
   1284  1.3  riastrad 
   1285  1.3  riastrad 	bo_update->fb_left = min_t(int, bo_update->fb_left, fb_x);
   1286  1.3  riastrad 	bo_update->fb_top = min_t(int, bo_update->fb_top, fb_y);
   1287  1.3  riastrad 
   1288  1.3  riastrad 	return 0;
   1289  1.3  riastrad }
   1290  1.3  riastrad 
   1291  1.3  riastrad static uint32_t
   1292  1.3  riastrad vmw_stdu_bo_populate_update_cpu(struct vmw_du_update_plane  *update, void *cmd,
   1293  1.3  riastrad 				struct drm_rect *bb)
   1294  1.3  riastrad {
   1295  1.3  riastrad 	struct vmw_du_update_plane_buffer *bo_update;
   1296  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1297  1.3  riastrad 	struct vmw_framebuffer_bo *vfbbo;
   1298  1.3  riastrad 	struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(0);
   1299  1.3  riastrad 	struct vmw_stdu_update_gb_image *cmd_img = cmd;
   1300  1.3  riastrad 	struct vmw_stdu_update *cmd_update;
   1301  1.3  riastrad 	struct ttm_buffer_object *src_bo, *dst_bo;
   1302  1.3  riastrad 	u32 src_offset, dst_offset;
   1303  1.3  riastrad 	s32 src_pitch, dst_pitch;
   1304  1.3  riastrad 	s32 width, height;
   1305  1.3  riastrad 
   1306  1.3  riastrad 	bo_update = container_of(update, typeof(*bo_update), base);
   1307  1.3  riastrad 	stdu = container_of(update->du, typeof(*stdu), base);
   1308  1.3  riastrad 	vfbbo = container_of(update->vfb, typeof(*vfbbo), base);
   1309  1.3  riastrad 
   1310  1.3  riastrad 	width = bb->x2 - bb->x1;
   1311  1.3  riastrad 	height = bb->y2 - bb->y1;
   1312  1.3  riastrad 
   1313  1.3  riastrad 	diff.cpp = stdu->cpp;
   1314  1.3  riastrad 
   1315  1.3  riastrad 	dst_bo = &stdu->display_srf->res.backup->base;
   1316  1.3  riastrad 	dst_pitch = stdu->display_srf->base_size.width * stdu->cpp;
   1317  1.3  riastrad 	dst_offset = bb->y1 * dst_pitch + bb->x1 * stdu->cpp;
   1318  1.3  riastrad 
   1319  1.3  riastrad 	src_bo = &vfbbo->buffer->base;
   1320  1.3  riastrad 	src_pitch = update->vfb->base.pitches[0];
   1321  1.3  riastrad 	src_offset = bo_update->fb_top * src_pitch + bo_update->fb_left *
   1322  1.3  riastrad 		stdu->cpp;
   1323  1.3  riastrad 
   1324  1.3  riastrad 	(void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch, src_bo,
   1325  1.3  riastrad 			       src_offset, src_pitch, width * stdu->cpp, height,
   1326  1.3  riastrad 			       &diff);
   1327  1.3  riastrad 
   1328  1.3  riastrad 	if (drm_rect_visible(&diff.rect)) {
   1329  1.3  riastrad 		SVGA3dBox *box = &cmd_img->body.box;
   1330  1.3  riastrad 
   1331  1.3  riastrad 		cmd_img->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
   1332  1.3  riastrad 		cmd_img->header.size = sizeof(cmd_img->body);
   1333  1.3  riastrad 		cmd_img->body.image.sid = stdu->display_srf->res.id;
   1334  1.3  riastrad 		cmd_img->body.image.face = 0;
   1335  1.3  riastrad 		cmd_img->body.image.mipmap = 0;
   1336  1.3  riastrad 
   1337  1.3  riastrad 		box->x = diff.rect.x1;
   1338  1.3  riastrad 		box->y = diff.rect.y1;
   1339  1.3  riastrad 		box->z = 0;
   1340  1.3  riastrad 		box->w = drm_rect_width(&diff.rect);
   1341  1.3  riastrad 		box->h = drm_rect_height(&diff.rect);
   1342  1.3  riastrad 		box->d = 1;
   1343  1.3  riastrad 
   1344  1.3  riastrad 		cmd_update = (struct vmw_stdu_update *)&cmd_img[1];
   1345  1.3  riastrad 		vmw_stdu_populate_update(cmd_update, stdu->base.unit,
   1346  1.3  riastrad 					 diff.rect.x1, diff.rect.x2,
   1347  1.3  riastrad 					 diff.rect.y1, diff.rect.y2);
   1348  1.3  riastrad 
   1349  1.3  riastrad 		return sizeof(*cmd_img) + sizeof(*cmd_update);
   1350  1.3  riastrad 	}
   1351  1.3  riastrad 
   1352  1.3  riastrad 	return 0;
   1353  1.3  riastrad }
   1354  1.3  riastrad 
   1355  1.3  riastrad /**
   1356  1.3  riastrad  * vmw_stdu_plane_update_bo - Update display unit for bo backed fb.
   1357  1.3  riastrad  * @dev_priv: device private.
   1358  1.3  riastrad  * @plane: plane state.
   1359  1.3  riastrad  * @old_state: old plane state.
   1360  1.3  riastrad  * @vfb: framebuffer which is blitted to display unit.
   1361  1.3  riastrad  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
   1362  1.3  riastrad  *             The returned fence pointer may be NULL in which case the device
   1363  1.3  riastrad  *             has already synchronized.
   1364  1.3  riastrad  *
   1365  1.3  riastrad  * Return: 0 on success or a negative error code on failure.
   1366  1.3  riastrad  */
   1367  1.3  riastrad static int vmw_stdu_plane_update_bo(struct vmw_private *dev_priv,
   1368  1.3  riastrad 				    struct drm_plane *plane,
   1369  1.3  riastrad 				    struct drm_plane_state *old_state,
   1370  1.3  riastrad 				    struct vmw_framebuffer *vfb,
   1371  1.3  riastrad 				    struct vmw_fence_obj **out_fence)
   1372  1.3  riastrad {
   1373  1.3  riastrad 	struct vmw_du_update_plane_buffer bo_update;
   1374  1.3  riastrad 
   1375  1.3  riastrad 	memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
   1376  1.3  riastrad 	bo_update.base.plane = plane;
   1377  1.3  riastrad 	bo_update.base.old_state = old_state;
   1378  1.3  riastrad 	bo_update.base.dev_priv = dev_priv;
   1379  1.3  riastrad 	bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
   1380  1.3  riastrad 	bo_update.base.vfb = vfb;
   1381  1.3  riastrad 	bo_update.base.out_fence = out_fence;
   1382  1.3  riastrad 	bo_update.base.mutex = NULL;
   1383  1.3  riastrad 	bo_update.base.cpu_blit = !(dev_priv->capabilities & SVGA_CAP_3D);
   1384  1.3  riastrad 	bo_update.base.intr = false;
   1385  1.3  riastrad 
   1386  1.3  riastrad 	/*
   1387  1.3  riastrad 	 * VM without 3D support don't have surface DMA command and framebuffer
   1388  1.3  riastrad 	 * should be moved out of VRAM.
   1389  1.3  riastrad 	 */
   1390  1.3  riastrad 	if (bo_update.base.cpu_blit) {
   1391  1.3  riastrad 		bo_update.base.calc_fifo_size = vmw_stdu_bo_fifo_size_cpu;
   1392  1.3  riastrad 		bo_update.base.pre_clip = vmw_stdu_bo_pre_clip_cpu;
   1393  1.3  riastrad 		bo_update.base.clip = vmw_stdu_bo_clip_cpu;
   1394  1.3  riastrad 		bo_update.base.post_clip = vmw_stdu_bo_populate_update_cpu;
   1395  1.3  riastrad 	} else {
   1396  1.3  riastrad 		bo_update.base.calc_fifo_size = vmw_stdu_bo_fifo_size;
   1397  1.3  riastrad 		bo_update.base.pre_clip = vmw_stdu_bo_populate_dma;
   1398  1.3  riastrad 		bo_update.base.clip = vmw_stdu_bo_populate_clip;
   1399  1.3  riastrad 		bo_update.base.post_clip = vmw_stdu_bo_populate_update;
   1400  1.3  riastrad 	}
   1401  1.3  riastrad 
   1402  1.3  riastrad 	return vmw_du_helper_plane_update(&bo_update.base);
   1403  1.3  riastrad }
   1404  1.3  riastrad 
   1405  1.3  riastrad static uint32_t
   1406  1.3  riastrad vmw_stdu_surface_fifo_size_same_display(struct vmw_du_update_plane *update,
   1407  1.3  riastrad 					uint32_t num_hits)
   1408  1.3  riastrad {
   1409  1.3  riastrad 	struct vmw_framebuffer_surface *vfbs;
   1410  1.3  riastrad 	uint32_t size = 0;
   1411  1.3  riastrad 
   1412  1.3  riastrad 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
   1413  1.3  riastrad 
   1414  1.3  riastrad 	if (vfbs->is_bo_proxy)
   1415  1.3  riastrad 		size += sizeof(struct vmw_stdu_update_gb_image) * num_hits;
   1416  1.3  riastrad 
   1417  1.3  riastrad 	size += sizeof(struct vmw_stdu_update);
   1418  1.3  riastrad 
   1419  1.3  riastrad 	return size;
   1420  1.3  riastrad }
   1421  1.3  riastrad 
   1422  1.3  riastrad static uint32_t vmw_stdu_surface_fifo_size(struct vmw_du_update_plane *update,
   1423  1.3  riastrad 					   uint32_t num_hits)
   1424  1.3  riastrad {
   1425  1.3  riastrad 	struct vmw_framebuffer_surface *vfbs;
   1426  1.3  riastrad 	uint32_t size = 0;
   1427  1.3  riastrad 
   1428  1.3  riastrad 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
   1429  1.3  riastrad 
   1430  1.3  riastrad 	if (vfbs->is_bo_proxy)
   1431  1.3  riastrad 		size += sizeof(struct vmw_stdu_update_gb_image) * num_hits;
   1432  1.3  riastrad 
   1433  1.3  riastrad 	size += sizeof(struct vmw_stdu_surface_copy) + sizeof(SVGA3dCopyBox) *
   1434  1.3  riastrad 		num_hits + sizeof(struct vmw_stdu_update);
   1435  1.3  riastrad 
   1436  1.3  riastrad 	return size;
   1437  1.3  riastrad }
   1438  1.3  riastrad 
   1439  1.3  riastrad static uint32_t
   1440  1.3  riastrad vmw_stdu_surface_update_proxy(struct vmw_du_update_plane *update, void *cmd)
   1441  1.3  riastrad {
   1442  1.3  riastrad 	struct vmw_framebuffer_surface *vfbs;
   1443  1.3  riastrad 	struct drm_plane_state *state = update->plane->state;
   1444  1.3  riastrad 	struct drm_plane_state *old_state = update->old_state;
   1445  1.3  riastrad 	struct vmw_stdu_update_gb_image *cmd_update = cmd;
   1446  1.3  riastrad 	struct drm_atomic_helper_damage_iter iter;
   1447  1.3  riastrad 	struct drm_rect clip;
   1448  1.3  riastrad 	uint32_t copy_size = 0;
   1449  1.3  riastrad 
   1450  1.3  riastrad 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
   1451  1.3  riastrad 
   1452  1.3  riastrad 	/*
   1453  1.3  riastrad 	 * proxy surface is special where a buffer object type fb is wrapped
   1454  1.3  riastrad 	 * in a surface and need an update gb image command to sync with device.
   1455  1.3  riastrad 	 */
   1456  1.3  riastrad 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
   1457  1.3  riastrad 	drm_atomic_for_each_plane_damage(&iter, &clip) {
   1458  1.3  riastrad 		SVGA3dBox *box = &cmd_update->body.box;
   1459  1.3  riastrad 
   1460  1.3  riastrad 		cmd_update->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
   1461  1.3  riastrad 		cmd_update->header.size = sizeof(cmd_update->body);
   1462  1.3  riastrad 		cmd_update->body.image.sid = vfbs->surface->res.id;
   1463  1.3  riastrad 		cmd_update->body.image.face = 0;
   1464  1.3  riastrad 		cmd_update->body.image.mipmap = 0;
   1465  1.3  riastrad 
   1466  1.3  riastrad 		box->x = clip.x1;
   1467  1.3  riastrad 		box->y = clip.y1;
   1468  1.3  riastrad 		box->z = 0;
   1469  1.3  riastrad 		box->w = drm_rect_width(&clip);
   1470  1.3  riastrad 		box->h = drm_rect_height(&clip);
   1471  1.3  riastrad 		box->d = 1;
   1472  1.3  riastrad 
   1473  1.3  riastrad 		copy_size += sizeof(*cmd_update);
   1474  1.3  riastrad 		cmd_update++;
   1475  1.3  riastrad 	}
   1476  1.3  riastrad 
   1477  1.3  riastrad 	return copy_size;
   1478  1.3  riastrad }
   1479  1.3  riastrad 
   1480  1.3  riastrad static uint32_t
   1481  1.3  riastrad vmw_stdu_surface_populate_copy(struct vmw_du_update_plane  *update, void *cmd,
   1482  1.3  riastrad 			       uint32_t num_hits)
   1483  1.3  riastrad {
   1484  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1485  1.3  riastrad 	struct vmw_framebuffer_surface *vfbs;
   1486  1.3  riastrad 	struct vmw_stdu_surface_copy *cmd_copy = cmd;
   1487  1.3  riastrad 
   1488  1.3  riastrad 	stdu = container_of(update->du, typeof(*stdu), base);
   1489  1.3  riastrad 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
   1490  1.3  riastrad 
   1491  1.3  riastrad 	cmd_copy->header.id = SVGA_3D_CMD_SURFACE_COPY;
   1492  1.3  riastrad 	cmd_copy->header.size = sizeof(cmd_copy->body) + sizeof(SVGA3dCopyBox) *
   1493  1.3  riastrad 		num_hits;
   1494  1.3  riastrad 	cmd_copy->body.src.sid = vfbs->surface->res.id;
   1495  1.3  riastrad 	cmd_copy->body.dest.sid = stdu->display_srf->res.id;
   1496  1.3  riastrad 
   1497  1.3  riastrad 	return sizeof(*cmd_copy);
   1498  1.3  riastrad }
   1499  1.3  riastrad 
   1500  1.3  riastrad static uint32_t
   1501  1.3  riastrad vmw_stdu_surface_populate_clip(struct vmw_du_update_plane  *update, void *cmd,
   1502  1.3  riastrad 			       struct drm_rect *clip, uint32_t fb_x,
   1503  1.3  riastrad 			       uint32_t fb_y)
   1504  1.3  riastrad {
   1505  1.3  riastrad 	struct SVGA3dCopyBox *box = cmd;
   1506  1.3  riastrad 
   1507  1.3  riastrad 	box->srcx = fb_x;
   1508  1.3  riastrad 	box->srcy = fb_y;
   1509  1.3  riastrad 	box->srcz = 0;
   1510  1.3  riastrad 	box->x = clip->x1;
   1511  1.3  riastrad 	box->y = clip->y1;
   1512  1.3  riastrad 	box->z = 0;
   1513  1.3  riastrad 	box->w = drm_rect_width(clip);
   1514  1.3  riastrad 	box->h = drm_rect_height(clip);
   1515  1.3  riastrad 	box->d = 1;
   1516  1.3  riastrad 
   1517  1.3  riastrad 	return sizeof(*box);
   1518  1.3  riastrad }
   1519  1.3  riastrad 
   1520  1.3  riastrad static uint32_t
   1521  1.3  riastrad vmw_stdu_surface_populate_update(struct vmw_du_update_plane  *update, void *cmd,
   1522  1.3  riastrad 				 struct drm_rect *bb)
   1523  1.3  riastrad {
   1524  1.3  riastrad 	vmw_stdu_populate_update(cmd, update->du->unit, bb->x1, bb->x2, bb->y1,
   1525  1.3  riastrad 				 bb->y2);
   1526  1.3  riastrad 
   1527  1.3  riastrad 	return sizeof(struct vmw_stdu_update);
   1528  1.3  riastrad }
   1529  1.3  riastrad 
   1530  1.3  riastrad /**
   1531  1.3  riastrad  * vmw_stdu_plane_update_surface - Update display unit for surface backed fb
   1532  1.3  riastrad  * @dev_priv: Device private
   1533  1.3  riastrad  * @plane: Plane state
   1534  1.3  riastrad  * @old_state: Old plane state
   1535  1.3  riastrad  * @vfb: Framebuffer which is blitted to display unit
   1536  1.3  riastrad  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
   1537  1.3  riastrad  *             The returned fence pointer may be NULL in which case the device
   1538  1.3  riastrad  *             has already synchronized.
   1539  1.3  riastrad  *
   1540  1.3  riastrad  * Return: 0 on success or a negative error code on failure.
   1541  1.3  riastrad  */
   1542  1.3  riastrad static int vmw_stdu_plane_update_surface(struct vmw_private *dev_priv,
   1543  1.3  riastrad 					 struct drm_plane *plane,
   1544  1.3  riastrad 					 struct drm_plane_state *old_state,
   1545  1.3  riastrad 					 struct vmw_framebuffer *vfb,
   1546  1.3  riastrad 					 struct vmw_fence_obj **out_fence)
   1547  1.3  riastrad {
   1548  1.3  riastrad 	struct vmw_du_update_plane srf_update;
   1549  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1550  1.3  riastrad 	struct vmw_framebuffer_surface *vfbs;
   1551  1.3  riastrad 
   1552  1.3  riastrad 	stdu = vmw_crtc_to_stdu(plane->state->crtc);
   1553  1.3  riastrad 	vfbs = container_of(vfb, typeof(*vfbs), base);
   1554  1.3  riastrad 
   1555  1.3  riastrad 	memset(&srf_update, 0, sizeof(struct vmw_du_update_plane));
   1556  1.3  riastrad 	srf_update.plane = plane;
   1557  1.3  riastrad 	srf_update.old_state = old_state;
   1558  1.3  riastrad 	srf_update.dev_priv = dev_priv;
   1559  1.3  riastrad 	srf_update.du = vmw_crtc_to_du(plane->state->crtc);
   1560  1.3  riastrad 	srf_update.vfb = vfb;
   1561  1.3  riastrad 	srf_update.out_fence = out_fence;
   1562  1.3  riastrad 	srf_update.mutex = &dev_priv->cmdbuf_mutex;
   1563  1.3  riastrad 	srf_update.cpu_blit = false;
   1564  1.3  riastrad 	srf_update.intr = true;
   1565  1.3  riastrad 
   1566  1.3  riastrad 	if (vfbs->is_bo_proxy)
   1567  1.3  riastrad 		srf_update.post_prepare = vmw_stdu_surface_update_proxy;
   1568  1.3  riastrad 
   1569  1.3  riastrad 	if (vfbs->surface->res.id != stdu->display_srf->res.id) {
   1570  1.3  riastrad 		srf_update.calc_fifo_size = vmw_stdu_surface_fifo_size;
   1571  1.3  riastrad 		srf_update.pre_clip = vmw_stdu_surface_populate_copy;
   1572  1.3  riastrad 		srf_update.clip = vmw_stdu_surface_populate_clip;
   1573  1.3  riastrad 	} else {
   1574  1.3  riastrad 		srf_update.calc_fifo_size =
   1575  1.3  riastrad 			vmw_stdu_surface_fifo_size_same_display;
   1576  1.3  riastrad 	}
   1577  1.3  riastrad 
   1578  1.3  riastrad 	srf_update.post_clip = vmw_stdu_surface_populate_update;
   1579  1.3  riastrad 
   1580  1.3  riastrad 	return vmw_du_helper_plane_update(&srf_update);
   1581  1.3  riastrad }
   1582  1.3  riastrad 
   1583  1.3  riastrad /**
   1584  1.3  riastrad  * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
   1585  1.3  riastrad  * @plane: display plane
   1586  1.3  riastrad  * @old_state: Only used to get crtc info
   1587  1.3  riastrad  *
   1588  1.3  riastrad  * Formally update stdu->display_srf to the new plane, and bind the new
   1589  1.3  riastrad  * plane STDU.  This function is called during the commit phase when
   1590  1.3  riastrad  * all the preparation have been done and all the configurations have
   1591  1.3  riastrad  * been checked.
   1592  1.3  riastrad  */
   1593  1.3  riastrad static void
   1594  1.3  riastrad vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
   1595  1.3  riastrad 				     struct drm_plane_state *old_state)
   1596  1.3  riastrad {
   1597  1.3  riastrad 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
   1598  1.3  riastrad 	struct drm_crtc *crtc = plane->state->crtc;
   1599  1.3  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1600  1.3  riastrad 	struct drm_pending_vblank_event *event;
   1601  1.3  riastrad 	struct vmw_fence_obj *fence = NULL;
   1602  1.3  riastrad 	struct vmw_private *dev_priv;
   1603  1.3  riastrad 	int ret;
   1604  1.3  riastrad 
   1605  1.3  riastrad 	/* If case of device error, maintain consistent atomic state */
   1606  1.3  riastrad 	if (crtc && plane->state->fb) {
   1607  1.3  riastrad 		struct vmw_framebuffer *vfb =
   1608  1.3  riastrad 			vmw_framebuffer_to_vfb(plane->state->fb);
   1609  1.3  riastrad 		stdu = vmw_crtc_to_stdu(crtc);
   1610  1.3  riastrad 		dev_priv = vmw_priv(crtc->dev);
   1611  1.3  riastrad 
   1612  1.3  riastrad 		stdu->display_srf = vps->surf;
   1613  1.3  riastrad 		stdu->content_fb_type = vps->content_fb_type;
   1614  1.3  riastrad 		stdu->cpp = vps->cpp;
   1615  1.3  riastrad 
   1616  1.3  riastrad 		ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
   1617  1.3  riastrad 		if (ret)
   1618  1.3  riastrad 			DRM_ERROR("Failed to bind surface to STDU.\n");
   1619  1.3  riastrad 
   1620  1.3  riastrad 		if (vfb->bo)
   1621  1.3  riastrad 			ret = vmw_stdu_plane_update_bo(dev_priv, plane,
   1622  1.3  riastrad 						       old_state, vfb, &fence);
   1623  1.3  riastrad 		else
   1624  1.3  riastrad 			ret = vmw_stdu_plane_update_surface(dev_priv, plane,
   1625  1.3  riastrad 							    old_state, vfb,
   1626  1.3  riastrad 							    &fence);
   1627  1.3  riastrad 		if (ret)
   1628  1.3  riastrad 			DRM_ERROR("Failed to update STDU.\n");
   1629  1.3  riastrad 	} else {
   1630  1.3  riastrad 		crtc = old_state->crtc;
   1631  1.3  riastrad 		stdu = vmw_crtc_to_stdu(crtc);
   1632  1.3  riastrad 		dev_priv = vmw_priv(crtc->dev);
   1633  1.3  riastrad 
   1634  1.3  riastrad 		/* Blank STDU when fb and crtc are NULL */
   1635  1.3  riastrad 		if (!stdu->defined)
   1636  1.3  riastrad 			return;
   1637  1.3  riastrad 
   1638  1.3  riastrad 		ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
   1639  1.3  riastrad 		if (ret)
   1640  1.3  riastrad 			DRM_ERROR("Failed to blank STDU\n");
   1641  1.3  riastrad 
   1642  1.3  riastrad 		ret = vmw_stdu_update_st(dev_priv, stdu);
   1643  1.3  riastrad 		if (ret)
   1644  1.3  riastrad 			DRM_ERROR("Failed to update STDU.\n");
   1645  1.3  riastrad 
   1646  1.3  riastrad 		return;
   1647  1.3  riastrad 	}
   1648  1.3  riastrad 
   1649  1.3  riastrad 	/* In case of error, vblank event is send in vmw_du_crtc_atomic_flush */
   1650  1.3  riastrad 	event = crtc->state->event;
   1651  1.3  riastrad 	if (event && fence) {
   1652  1.3  riastrad 		struct drm_file *file_priv = event->base.file_priv;
   1653  1.3  riastrad 
   1654  1.3  riastrad 		ret = vmw_event_fence_action_queue(file_priv,
   1655  1.3  riastrad 						   fence,
   1656  1.3  riastrad 						   &event->base,
   1657  1.3  riastrad 						   &event->event.vbl.tv_sec,
   1658  1.3  riastrad 						   &event->event.vbl.tv_usec,
   1659  1.3  riastrad 						   true);
   1660  1.3  riastrad 		if (ret)
   1661  1.3  riastrad 			DRM_ERROR("Failed to queue event on fence.\n");
   1662  1.3  riastrad 		else
   1663  1.3  riastrad 			crtc->state->event = NULL;
   1664  1.3  riastrad 	}
   1665  1.3  riastrad 
   1666  1.3  riastrad 	if (fence)
   1667  1.3  riastrad 		vmw_fence_obj_unreference(&fence);
   1668  1.3  riastrad }
   1669  1.3  riastrad 
   1670  1.3  riastrad 
   1671  1.3  riastrad static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
   1672  1.3  riastrad 	.update_plane = drm_atomic_helper_update_plane,
   1673  1.3  riastrad 	.disable_plane = drm_atomic_helper_disable_plane,
   1674  1.3  riastrad 	.destroy = vmw_du_primary_plane_destroy,
   1675  1.3  riastrad 	.reset = vmw_du_plane_reset,
   1676  1.3  riastrad 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
   1677  1.3  riastrad 	.atomic_destroy_state = vmw_du_plane_destroy_state,
   1678  1.3  riastrad };
   1679  1.3  riastrad 
   1680  1.3  riastrad static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
   1681  1.3  riastrad 	.update_plane = drm_atomic_helper_update_plane,
   1682  1.3  riastrad 	.disable_plane = drm_atomic_helper_disable_plane,
   1683  1.3  riastrad 	.destroy = vmw_du_cursor_plane_destroy,
   1684  1.3  riastrad 	.reset = vmw_du_plane_reset,
   1685  1.3  riastrad 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
   1686  1.3  riastrad 	.atomic_destroy_state = vmw_du_plane_destroy_state,
   1687  1.1  riastrad };
   1688  1.1  riastrad 
   1689  1.1  riastrad 
   1690  1.3  riastrad /*
   1691  1.3  riastrad  * Atomic Helpers
   1692  1.3  riastrad  */
   1693  1.3  riastrad static const struct
   1694  1.3  riastrad drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
   1695  1.3  riastrad 	.atomic_check = vmw_du_cursor_plane_atomic_check,
   1696  1.3  riastrad 	.atomic_update = vmw_du_cursor_plane_atomic_update,
   1697  1.3  riastrad 	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
   1698  1.3  riastrad 	.cleanup_fb = vmw_du_plane_cleanup_fb,
   1699  1.3  riastrad };
   1700  1.3  riastrad 
   1701  1.3  riastrad static const struct
   1702  1.3  riastrad drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
   1703  1.3  riastrad 	.atomic_check = vmw_du_primary_plane_atomic_check,
   1704  1.3  riastrad 	.atomic_update = vmw_stdu_primary_plane_atomic_update,
   1705  1.3  riastrad 	.prepare_fb = vmw_stdu_primary_plane_prepare_fb,
   1706  1.3  riastrad 	.cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
   1707  1.3  riastrad };
   1708  1.3  riastrad 
   1709  1.3  riastrad static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
   1710  1.3  riastrad 	.prepare = vmw_stdu_crtc_helper_prepare,
   1711  1.3  riastrad 	.mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
   1712  1.3  riastrad 	.atomic_check = vmw_du_crtc_atomic_check,
   1713  1.3  riastrad 	.atomic_begin = vmw_du_crtc_atomic_begin,
   1714  1.3  riastrad 	.atomic_flush = vmw_du_crtc_atomic_flush,
   1715  1.3  riastrad 	.atomic_enable = vmw_stdu_crtc_atomic_enable,
   1716  1.3  riastrad 	.atomic_disable = vmw_stdu_crtc_atomic_disable,
   1717  1.3  riastrad };
   1718  1.3  riastrad 
   1719  1.1  riastrad 
   1720  1.1  riastrad /**
   1721  1.1  riastrad  * vmw_stdu_init - Sets up a Screen Target Display Unit
   1722  1.1  riastrad  *
   1723  1.1  riastrad  * @dev_priv: VMW DRM device
   1724  1.1  riastrad  * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
   1725  1.1  riastrad  *
   1726  1.1  riastrad  * This function is called once per CRTC, and allocates one Screen Target
   1727  1.1  riastrad  * display unit to represent that CRTC.  Since the SVGA device does not separate
   1728  1.1  riastrad  * out encoder and connector, they are represented as part of the STDU as well.
   1729  1.1  riastrad  */
   1730  1.1  riastrad static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
   1731  1.1  riastrad {
   1732  1.1  riastrad 	struct vmw_screen_target_display_unit *stdu;
   1733  1.1  riastrad 	struct drm_device *dev = dev_priv->dev;
   1734  1.1  riastrad 	struct drm_connector *connector;
   1735  1.1  riastrad 	struct drm_encoder *encoder;
   1736  1.3  riastrad 	struct drm_plane *primary, *cursor;
   1737  1.1  riastrad 	struct drm_crtc *crtc;
   1738  1.3  riastrad 	int    ret;
   1739  1.1  riastrad 
   1740  1.1  riastrad 
   1741  1.1  riastrad 	stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
   1742  1.1  riastrad 	if (!stdu)
   1743  1.1  riastrad 		return -ENOMEM;
   1744  1.1  riastrad 
   1745  1.1  riastrad 	stdu->base.unit = unit;
   1746  1.1  riastrad 	crtc = &stdu->base.crtc;
   1747  1.1  riastrad 	encoder = &stdu->base.encoder;
   1748  1.1  riastrad 	connector = &stdu->base.connector;
   1749  1.3  riastrad 	primary = &stdu->base.primary;
   1750  1.3  riastrad 	cursor = &stdu->base.cursor;
   1751  1.1  riastrad 
   1752  1.1  riastrad 	stdu->base.pref_active = (unit == 0);
   1753  1.1  riastrad 	stdu->base.pref_width  = dev_priv->initial_width;
   1754  1.1  riastrad 	stdu->base.pref_height = dev_priv->initial_height;
   1755  1.3  riastrad 	stdu->base.is_implicit = false;
   1756  1.3  riastrad 
   1757  1.3  riastrad 	/* Initialize primary plane */
   1758  1.3  riastrad 	vmw_du_plane_reset(primary);
   1759  1.3  riastrad 
   1760  1.3  riastrad 	ret = drm_universal_plane_init(dev, primary,
   1761  1.3  riastrad 				       0, &vmw_stdu_plane_funcs,
   1762  1.3  riastrad 				       vmw_primary_plane_formats,
   1763  1.3  riastrad 				       ARRAY_SIZE(vmw_primary_plane_formats),
   1764  1.3  riastrad 				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
   1765  1.3  riastrad 	if (ret) {
   1766  1.3  riastrad 		DRM_ERROR("Failed to initialize primary plane");
   1767  1.3  riastrad 		goto err_free;
   1768  1.3  riastrad 	}
   1769  1.3  riastrad 
   1770  1.3  riastrad 	drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
   1771  1.3  riastrad 	drm_plane_enable_fb_damage_clips(primary);
   1772  1.1  riastrad 
   1773  1.3  riastrad 	/* Initialize cursor plane */
   1774  1.3  riastrad 	vmw_du_plane_reset(cursor);
   1775  1.3  riastrad 
   1776  1.3  riastrad 	ret = drm_universal_plane_init(dev, cursor,
   1777  1.3  riastrad 			0, &vmw_stdu_cursor_funcs,
   1778  1.3  riastrad 			vmw_cursor_plane_formats,
   1779  1.3  riastrad 			ARRAY_SIZE(vmw_cursor_plane_formats),
   1780  1.3  riastrad 			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
   1781  1.3  riastrad 	if (ret) {
   1782  1.3  riastrad 		DRM_ERROR("Failed to initialize cursor plane");
   1783  1.3  riastrad 		drm_plane_cleanup(&stdu->base.primary);
   1784  1.3  riastrad 		goto err_free;
   1785  1.3  riastrad 	}
   1786  1.3  riastrad 
   1787  1.3  riastrad 	drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
   1788  1.3  riastrad 
   1789  1.3  riastrad 	vmw_du_connector_reset(connector);
   1790  1.3  riastrad 
   1791  1.3  riastrad 	ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
   1792  1.3  riastrad 				 DRM_MODE_CONNECTOR_VIRTUAL);
   1793  1.3  riastrad 	if (ret) {
   1794  1.3  riastrad 		DRM_ERROR("Failed to initialize connector\n");
   1795  1.3  riastrad 		goto err_free;
   1796  1.3  riastrad 	}
   1797  1.3  riastrad 
   1798  1.3  riastrad 	drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
   1799  1.1  riastrad 	connector->status = vmw_du_connector_detect(connector, false);
   1800  1.1  riastrad 
   1801  1.3  riastrad 	ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
   1802  1.3  riastrad 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
   1803  1.3  riastrad 	if (ret) {
   1804  1.3  riastrad 		DRM_ERROR("Failed to initialize encoder\n");
   1805  1.3  riastrad 		goto err_free_connector;
   1806  1.3  riastrad 	}
   1807  1.3  riastrad 
   1808  1.3  riastrad 	(void) drm_connector_attach_encoder(connector, encoder);
   1809  1.1  riastrad 	encoder->possible_crtcs = (1 << unit);
   1810  1.1  riastrad 	encoder->possible_clones = 0;
   1811  1.1  riastrad 
   1812  1.3  riastrad 	ret = drm_connector_register(connector);
   1813  1.3  riastrad 	if (ret) {
   1814  1.3  riastrad 		DRM_ERROR("Failed to register connector\n");
   1815  1.3  riastrad 		goto err_free_encoder;
   1816  1.3  riastrad 	}
   1817  1.3  riastrad 
   1818  1.3  riastrad 	vmw_du_crtc_reset(crtc);
   1819  1.3  riastrad 	ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
   1820  1.3  riastrad 					&stdu->base.cursor,
   1821  1.3  riastrad 					&vmw_stdu_crtc_funcs, NULL);
   1822  1.3  riastrad 	if (ret) {
   1823  1.3  riastrad 		DRM_ERROR("Failed to initialize CRTC\n");
   1824  1.3  riastrad 		goto err_free_unregister;
   1825  1.3  riastrad 	}
   1826  1.1  riastrad 
   1827  1.3  riastrad 	drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
   1828  1.1  riastrad 
   1829  1.1  riastrad 	drm_mode_crtc_set_gamma_size(crtc, 256);
   1830  1.1  riastrad 
   1831  1.1  riastrad 	drm_object_attach_property(&connector->base,
   1832  1.3  riastrad 				   dev_priv->hotplug_mode_update_property, 1);
   1833  1.3  riastrad 	drm_object_attach_property(&connector->base,
   1834  1.3  riastrad 				   dev->mode_config.suggested_x_property, 0);
   1835  1.3  riastrad 	drm_object_attach_property(&connector->base,
   1836  1.3  riastrad 				   dev->mode_config.suggested_y_property, 0);
   1837  1.3  riastrad 	return 0;
   1838  1.1  riastrad 
   1839  1.3  riastrad err_free_unregister:
   1840  1.3  riastrad 	drm_connector_unregister(connector);
   1841  1.3  riastrad err_free_encoder:
   1842  1.3  riastrad 	drm_encoder_cleanup(encoder);
   1843  1.3  riastrad err_free_connector:
   1844  1.3  riastrad 	drm_connector_cleanup(connector);
   1845  1.3  riastrad err_free:
   1846  1.3  riastrad 	kfree(stdu);
   1847  1.3  riastrad 	return ret;
   1848  1.1  riastrad }
   1849  1.1  riastrad 
   1850  1.1  riastrad 
   1851  1.1  riastrad 
   1852  1.1  riastrad /**
   1853  1.1  riastrad  *  vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
   1854  1.1  riastrad  *
   1855  1.1  riastrad  *  @stdu:  Screen Target Display Unit to be destroyed
   1856  1.1  riastrad  *
   1857  1.1  riastrad  *  Clean up after vmw_stdu_init
   1858  1.1  riastrad  */
   1859  1.1  riastrad static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
   1860  1.1  riastrad {
   1861  1.1  riastrad 	vmw_du_cleanup(&stdu->base);
   1862  1.1  riastrad 	kfree(stdu);
   1863  1.1  riastrad }
   1864  1.1  riastrad 
   1865  1.1  riastrad 
   1866  1.1  riastrad 
   1867  1.1  riastrad /******************************************************************************
   1868  1.1  riastrad  * Screen Target Display KMS Functions
   1869  1.1  riastrad  *
   1870  1.1  riastrad  * These functions are called by the common KMS code in vmwgfx_kms.c
   1871  1.1  riastrad  *****************************************************************************/
   1872  1.1  riastrad 
   1873  1.1  riastrad /**
   1874  1.1  riastrad  * vmw_kms_stdu_init_display - Initializes a Screen Target based display
   1875  1.1  riastrad  *
   1876  1.1  riastrad  * @dev_priv: VMW DRM device
   1877  1.1  riastrad  *
   1878  1.1  riastrad  * This function initialize a Screen Target based display device.  It checks
   1879  1.1  riastrad  * the capability bits to make sure the underlying hardware can support
   1880  1.1  riastrad  * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
   1881  1.1  riastrad  * Units, as supported by the display hardware.
   1882  1.1  riastrad  *
   1883  1.1  riastrad  * RETURNS:
   1884  1.1  riastrad  * 0 on success, error code otherwise
   1885  1.1  riastrad  */
   1886  1.1  riastrad int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
   1887  1.1  riastrad {
   1888  1.1  riastrad 	struct drm_device *dev = dev_priv->dev;
   1889  1.1  riastrad 	int i, ret;
   1890  1.1  riastrad 
   1891  1.1  riastrad 
   1892  1.1  riastrad 	/* Do nothing if Screen Target support is turned off */
   1893  1.1  riastrad 	if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
   1894  1.1  riastrad 		return -ENOSYS;
   1895  1.1  riastrad 
   1896  1.1  riastrad 	if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
   1897  1.1  riastrad 		return -ENOSYS;
   1898  1.1  riastrad 
   1899  1.1  riastrad 	ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
   1900  1.1  riastrad 	if (unlikely(ret != 0))
   1901  1.1  riastrad 		return ret;
   1902  1.1  riastrad 
   1903  1.1  riastrad 	dev_priv->active_display_unit = vmw_du_screen_target;
   1904  1.1  riastrad 
   1905  1.1  riastrad 	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
   1906  1.1  riastrad 		ret = vmw_stdu_init(dev_priv, i);
   1907  1.1  riastrad 
   1908  1.1  riastrad 		if (unlikely(ret != 0)) {
   1909  1.1  riastrad 			DRM_ERROR("Failed to initialize STDU %d", i);
   1910  1.3  riastrad 			return ret;
   1911  1.1  riastrad 		}
   1912  1.1  riastrad 	}
   1913  1.1  riastrad 
   1914  1.1  riastrad 	DRM_INFO("Screen Target Display device initialized\n");
   1915  1.1  riastrad 
   1916  1.1  riastrad 	return 0;
   1917  1.1  riastrad }
   1918