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