Home | History | Annotate | Line # | Download | only in vmwgfx
      1  1.4  riastrad /*	$NetBSD: vmwgfx_overlay.c,v 1.4 2022/10/25 23:35:43 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 2009-2014 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.4  riastrad __KERNEL_RCSID(0, "$NetBSD: vmwgfx_overlay.c,v 1.4 2022/10/25 23:35:43 riastradh Exp $");
     32  1.2  riastrad 
     33  1.1  riastrad #include <drm/ttm/ttm_placement.h>
     34  1.1  riastrad 
     35  1.2  riastrad #include "device_include/svga_overlay.h"
     36  1.2  riastrad #include "device_include/svga_escape.h"
     37  1.1  riastrad 
     38  1.3  riastrad #include "vmwgfx_drv.h"
     39  1.3  riastrad 
     40  1.4  riastrad #include <linux/nbsd-namespace.h>
     41  1.4  riastrad 
     42  1.1  riastrad #define VMW_MAX_NUM_STREAMS 1
     43  1.1  riastrad #define VMW_OVERLAY_CAP_MASK (SVGA_FIFO_CAP_VIDEO | SVGA_FIFO_CAP_ESCAPE)
     44  1.1  riastrad 
     45  1.1  riastrad struct vmw_stream {
     46  1.3  riastrad 	struct vmw_buffer_object *buf;
     47  1.1  riastrad 	bool claimed;
     48  1.1  riastrad 	bool paused;
     49  1.1  riastrad 	struct drm_vmw_control_stream_arg saved;
     50  1.1  riastrad };
     51  1.1  riastrad 
     52  1.1  riastrad /**
     53  1.1  riastrad  * Overlay control
     54  1.1  riastrad  */
     55  1.1  riastrad struct vmw_overlay {
     56  1.1  riastrad 	/*
     57  1.1  riastrad 	 * Each stream is a single overlay. In Xv these are called ports.
     58  1.1  riastrad 	 */
     59  1.1  riastrad 	struct mutex mutex;
     60  1.1  riastrad 	struct vmw_stream stream[VMW_MAX_NUM_STREAMS];
     61  1.1  riastrad };
     62  1.1  riastrad 
     63  1.1  riastrad static inline struct vmw_overlay *vmw_overlay(struct drm_device *dev)
     64  1.1  riastrad {
     65  1.1  riastrad 	struct vmw_private *dev_priv = vmw_priv(dev);
     66  1.1  riastrad 	return dev_priv ? dev_priv->overlay_priv : NULL;
     67  1.1  riastrad }
     68  1.1  riastrad 
     69  1.1  riastrad struct vmw_escape_header {
     70  1.1  riastrad 	uint32_t cmd;
     71  1.1  riastrad 	SVGAFifoCmdEscape body;
     72  1.1  riastrad };
     73  1.1  riastrad 
     74  1.1  riastrad struct vmw_escape_video_flush {
     75  1.1  riastrad 	struct vmw_escape_header escape;
     76  1.1  riastrad 	SVGAEscapeVideoFlush flush;
     77  1.1  riastrad };
     78  1.1  riastrad 
     79  1.1  riastrad static inline void fill_escape(struct vmw_escape_header *header,
     80  1.1  riastrad 			       uint32_t size)
     81  1.1  riastrad {
     82  1.1  riastrad 	header->cmd = SVGA_CMD_ESCAPE;
     83  1.1  riastrad 	header->body.nsid = SVGA_ESCAPE_NSID_VMWARE;
     84  1.1  riastrad 	header->body.size = size;
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad static inline void fill_flush(struct vmw_escape_video_flush *cmd,
     88  1.1  riastrad 			      uint32_t stream_id)
     89  1.1  riastrad {
     90  1.1  riastrad 	fill_escape(&cmd->escape, sizeof(cmd->flush));
     91  1.1  riastrad 	cmd->flush.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_FLUSH;
     92  1.1  riastrad 	cmd->flush.streamId = stream_id;
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad /**
     96  1.1  riastrad  * Send put command to hw.
     97  1.1  riastrad  *
     98  1.1  riastrad  * Returns
     99  1.1  riastrad  * -ERESTARTSYS if interrupted by a signal.
    100  1.1  riastrad  */
    101  1.1  riastrad static int vmw_overlay_send_put(struct vmw_private *dev_priv,
    102  1.3  riastrad 				struct vmw_buffer_object *buf,
    103  1.1  riastrad 				struct drm_vmw_control_stream_arg *arg,
    104  1.1  riastrad 				bool interruptible)
    105  1.1  riastrad {
    106  1.1  riastrad 	struct vmw_escape_video_flush *flush;
    107  1.1  riastrad 	size_t fifo_size;
    108  1.2  riastrad 	bool have_so = (dev_priv->active_display_unit == vmw_du_screen_object);
    109  1.1  riastrad 	int i, num_items;
    110  1.1  riastrad 	SVGAGuestPtr ptr;
    111  1.1  riastrad 
    112  1.1  riastrad 	struct {
    113  1.1  riastrad 		struct vmw_escape_header escape;
    114  1.1  riastrad 		struct {
    115  1.1  riastrad 			uint32_t cmdType;
    116  1.1  riastrad 			uint32_t streamId;
    117  1.1  riastrad 		} header;
    118  1.1  riastrad 	} *cmds;
    119  1.1  riastrad 	struct {
    120  1.1  riastrad 		uint32_t registerId;
    121  1.1  riastrad 		uint32_t value;
    122  1.1  riastrad 	} *items;
    123  1.1  riastrad 
    124  1.1  riastrad 	/* defines are a index needs + 1 */
    125  1.1  riastrad 	if (have_so)
    126  1.1  riastrad 		num_items = SVGA_VIDEO_DST_SCREEN_ID + 1;
    127  1.1  riastrad 	else
    128  1.1  riastrad 		num_items = SVGA_VIDEO_PITCH_3 + 1;
    129  1.1  riastrad 
    130  1.1  riastrad 	fifo_size = sizeof(*cmds) + sizeof(*flush) + sizeof(*items) * num_items;
    131  1.1  riastrad 
    132  1.3  riastrad 	cmds = VMW_FIFO_RESERVE(dev_priv, fifo_size);
    133  1.1  riastrad 	/* hardware has hung, can't do anything here */
    134  1.1  riastrad 	if (!cmds)
    135  1.1  riastrad 		return -ENOMEM;
    136  1.1  riastrad 
    137  1.1  riastrad 	items = (typeof(items))&cmds[1];
    138  1.1  riastrad 	flush = (struct vmw_escape_video_flush *)&items[num_items];
    139  1.1  riastrad 
    140  1.1  riastrad 	/* the size is header + number of items */
    141  1.1  riastrad 	fill_escape(&cmds->escape, sizeof(*items) * (num_items + 1));
    142  1.1  riastrad 
    143  1.1  riastrad 	cmds->header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
    144  1.1  riastrad 	cmds->header.streamId = arg->stream_id;
    145  1.1  riastrad 
    146  1.1  riastrad 	/* the IDs are neatly numbered */
    147  1.1  riastrad 	for (i = 0; i < num_items; i++)
    148  1.1  riastrad 		items[i].registerId = i;
    149  1.1  riastrad 
    150  1.1  riastrad 	vmw_bo_get_guest_ptr(&buf->base, &ptr);
    151  1.1  riastrad 	ptr.offset += arg->offset;
    152  1.1  riastrad 
    153  1.1  riastrad 	items[SVGA_VIDEO_ENABLED].value     = true;
    154  1.1  riastrad 	items[SVGA_VIDEO_FLAGS].value       = arg->flags;
    155  1.1  riastrad 	items[SVGA_VIDEO_DATA_OFFSET].value = ptr.offset;
    156  1.1  riastrad 	items[SVGA_VIDEO_FORMAT].value      = arg->format;
    157  1.1  riastrad 	items[SVGA_VIDEO_COLORKEY].value    = arg->color_key;
    158  1.1  riastrad 	items[SVGA_VIDEO_SIZE].value        = arg->size;
    159  1.1  riastrad 	items[SVGA_VIDEO_WIDTH].value       = arg->width;
    160  1.1  riastrad 	items[SVGA_VIDEO_HEIGHT].value      = arg->height;
    161  1.1  riastrad 	items[SVGA_VIDEO_SRC_X].value       = arg->src.x;
    162  1.1  riastrad 	items[SVGA_VIDEO_SRC_Y].value       = arg->src.y;
    163  1.1  riastrad 	items[SVGA_VIDEO_SRC_WIDTH].value   = arg->src.w;
    164  1.1  riastrad 	items[SVGA_VIDEO_SRC_HEIGHT].value  = arg->src.h;
    165  1.1  riastrad 	items[SVGA_VIDEO_DST_X].value       = arg->dst.x;
    166  1.1  riastrad 	items[SVGA_VIDEO_DST_Y].value       = arg->dst.y;
    167  1.1  riastrad 	items[SVGA_VIDEO_DST_WIDTH].value   = arg->dst.w;
    168  1.1  riastrad 	items[SVGA_VIDEO_DST_HEIGHT].value  = arg->dst.h;
    169  1.1  riastrad 	items[SVGA_VIDEO_PITCH_1].value     = arg->pitch[0];
    170  1.1  riastrad 	items[SVGA_VIDEO_PITCH_2].value     = arg->pitch[1];
    171  1.1  riastrad 	items[SVGA_VIDEO_PITCH_3].value     = arg->pitch[2];
    172  1.1  riastrad 	if (have_so) {
    173  1.1  riastrad 		items[SVGA_VIDEO_DATA_GMRID].value    = ptr.gmrId;
    174  1.1  riastrad 		items[SVGA_VIDEO_DST_SCREEN_ID].value = SVGA_ID_INVALID;
    175  1.1  riastrad 	}
    176  1.1  riastrad 
    177  1.1  riastrad 	fill_flush(flush, arg->stream_id);
    178  1.1  riastrad 
    179  1.1  riastrad 	vmw_fifo_commit(dev_priv, fifo_size);
    180  1.1  riastrad 
    181  1.1  riastrad 	return 0;
    182  1.1  riastrad }
    183  1.1  riastrad 
    184  1.1  riastrad /**
    185  1.1  riastrad  * Send stop command to hw.
    186  1.1  riastrad  *
    187  1.1  riastrad  * Returns
    188  1.1  riastrad  * -ERESTARTSYS if interrupted by a signal.
    189  1.1  riastrad  */
    190  1.1  riastrad static int vmw_overlay_send_stop(struct vmw_private *dev_priv,
    191  1.1  riastrad 				 uint32_t stream_id,
    192  1.1  riastrad 				 bool interruptible)
    193  1.1  riastrad {
    194  1.1  riastrad 	struct {
    195  1.1  riastrad 		struct vmw_escape_header escape;
    196  1.1  riastrad 		SVGAEscapeVideoSetRegs body;
    197  1.1  riastrad 		struct vmw_escape_video_flush flush;
    198  1.1  riastrad 	} *cmds;
    199  1.1  riastrad 	int ret;
    200  1.1  riastrad 
    201  1.1  riastrad 	for (;;) {
    202  1.3  riastrad 		cmds = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmds));
    203  1.1  riastrad 		if (cmds)
    204  1.1  riastrad 			break;
    205  1.1  riastrad 
    206  1.1  riastrad 		ret = vmw_fallback_wait(dev_priv, false, true, 0,
    207  1.1  riastrad 					interruptible, 3*HZ);
    208  1.1  riastrad 		if (interruptible && ret == -ERESTARTSYS)
    209  1.1  riastrad 			return ret;
    210  1.1  riastrad 		else
    211  1.1  riastrad 			BUG_ON(ret != 0);
    212  1.1  riastrad 	}
    213  1.1  riastrad 
    214  1.1  riastrad 	fill_escape(&cmds->escape, sizeof(cmds->body));
    215  1.1  riastrad 	cmds->body.header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
    216  1.1  riastrad 	cmds->body.header.streamId = stream_id;
    217  1.1  riastrad 	cmds->body.items[0].registerId = SVGA_VIDEO_ENABLED;
    218  1.1  riastrad 	cmds->body.items[0].value = false;
    219  1.1  riastrad 	fill_flush(&cmds->flush, stream_id);
    220  1.1  riastrad 
    221  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmds));
    222  1.1  riastrad 
    223  1.1  riastrad 	return 0;
    224  1.1  riastrad }
    225  1.1  riastrad 
    226  1.1  riastrad /**
    227  1.1  riastrad  * Move a buffer to vram or gmr if @pin is set, else unpin the buffer.
    228  1.1  riastrad  *
    229  1.1  riastrad  * With the introduction of screen objects buffers could now be
    230  1.1  riastrad  * used with GMRs instead of being locked to vram.
    231  1.1  riastrad  */
    232  1.1  riastrad static int vmw_overlay_move_buffer(struct vmw_private *dev_priv,
    233  1.3  riastrad 				   struct vmw_buffer_object *buf,
    234  1.1  riastrad 				   bool pin, bool inter)
    235  1.1  riastrad {
    236  1.1  riastrad 	if (!pin)
    237  1.3  riastrad 		return vmw_bo_unpin(dev_priv, buf, inter);
    238  1.1  riastrad 
    239  1.2  riastrad 	if (dev_priv->active_display_unit == vmw_du_legacy)
    240  1.3  riastrad 		return vmw_bo_pin_in_vram(dev_priv, buf, inter);
    241  1.1  riastrad 
    242  1.3  riastrad 	return vmw_bo_pin_in_vram_or_gmr(dev_priv, buf, inter);
    243  1.1  riastrad }
    244  1.1  riastrad 
    245  1.1  riastrad /**
    246  1.1  riastrad  * Stop or pause a stream.
    247  1.1  riastrad  *
    248  1.1  riastrad  * If the stream is paused the no evict flag is removed from the buffer
    249  1.1  riastrad  * but left in vram. This allows for instance mode_set to evict it
    250  1.1  riastrad  * should it need to.
    251  1.1  riastrad  *
    252  1.1  riastrad  * The caller must hold the overlay lock.
    253  1.1  riastrad  *
    254  1.1  riastrad  * @stream_id which stream to stop/pause.
    255  1.1  riastrad  * @pause true to pause, false to stop completely.
    256  1.1  riastrad  */
    257  1.1  riastrad static int vmw_overlay_stop(struct vmw_private *dev_priv,
    258  1.1  riastrad 			    uint32_t stream_id, bool pause,
    259  1.1  riastrad 			    bool interruptible)
    260  1.1  riastrad {
    261  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    262  1.1  riastrad 	struct vmw_stream *stream = &overlay->stream[stream_id];
    263  1.1  riastrad 	int ret;
    264  1.1  riastrad 
    265  1.1  riastrad 	/* no buffer attached the stream is completely stopped */
    266  1.1  riastrad 	if (!stream->buf)
    267  1.1  riastrad 		return 0;
    268  1.1  riastrad 
    269  1.1  riastrad 	/* If the stream is paused this is already done */
    270  1.1  riastrad 	if (!stream->paused) {
    271  1.1  riastrad 		ret = vmw_overlay_send_stop(dev_priv, stream_id,
    272  1.1  riastrad 					    interruptible);
    273  1.1  riastrad 		if (ret)
    274  1.1  riastrad 			return ret;
    275  1.1  riastrad 
    276  1.1  riastrad 		/* We just remove the NO_EVICT flag so no -ENOMEM */
    277  1.1  riastrad 		ret = vmw_overlay_move_buffer(dev_priv, stream->buf, false,
    278  1.1  riastrad 					      interruptible);
    279  1.1  riastrad 		if (interruptible && ret == -ERESTARTSYS)
    280  1.1  riastrad 			return ret;
    281  1.1  riastrad 		else
    282  1.1  riastrad 			BUG_ON(ret != 0);
    283  1.1  riastrad 	}
    284  1.1  riastrad 
    285  1.1  riastrad 	if (!pause) {
    286  1.3  riastrad 		vmw_bo_unreference(&stream->buf);
    287  1.1  riastrad 		stream->paused = false;
    288  1.1  riastrad 	} else {
    289  1.1  riastrad 		stream->paused = true;
    290  1.1  riastrad 	}
    291  1.1  riastrad 
    292  1.1  riastrad 	return 0;
    293  1.1  riastrad }
    294  1.1  riastrad 
    295  1.1  riastrad /**
    296  1.1  riastrad  * Update a stream and send any put or stop fifo commands needed.
    297  1.1  riastrad  *
    298  1.1  riastrad  * The caller must hold the overlay lock.
    299  1.1  riastrad  *
    300  1.1  riastrad  * Returns
    301  1.1  riastrad  * -ENOMEM if buffer doesn't fit in vram.
    302  1.1  riastrad  * -ERESTARTSYS if interrupted.
    303  1.1  riastrad  */
    304  1.1  riastrad static int vmw_overlay_update_stream(struct vmw_private *dev_priv,
    305  1.3  riastrad 				     struct vmw_buffer_object *buf,
    306  1.1  riastrad 				     struct drm_vmw_control_stream_arg *arg,
    307  1.1  riastrad 				     bool interruptible)
    308  1.1  riastrad {
    309  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    310  1.1  riastrad 	struct vmw_stream *stream = &overlay->stream[arg->stream_id];
    311  1.1  riastrad 	int ret = 0;
    312  1.1  riastrad 
    313  1.1  riastrad 	if (!buf)
    314  1.1  riastrad 		return -EINVAL;
    315  1.1  riastrad 
    316  1.1  riastrad 	DRM_DEBUG("   %s: old %p, new %p, %spaused\n", __func__,
    317  1.1  riastrad 		  stream->buf, buf, stream->paused ? "" : "not ");
    318  1.1  riastrad 
    319  1.1  riastrad 	if (stream->buf != buf) {
    320  1.1  riastrad 		ret = vmw_overlay_stop(dev_priv, arg->stream_id,
    321  1.1  riastrad 				       false, interruptible);
    322  1.1  riastrad 		if (ret)
    323  1.1  riastrad 			return ret;
    324  1.1  riastrad 	} else if (!stream->paused) {
    325  1.1  riastrad 		/* If the buffers match and not paused then just send
    326  1.1  riastrad 		 * the put command, no need to do anything else.
    327  1.1  riastrad 		 */
    328  1.1  riastrad 		ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
    329  1.1  riastrad 		if (ret == 0)
    330  1.1  riastrad 			stream->saved = *arg;
    331  1.1  riastrad 		else
    332  1.1  riastrad 			BUG_ON(!interruptible);
    333  1.1  riastrad 
    334  1.1  riastrad 		return ret;
    335  1.1  riastrad 	}
    336  1.1  riastrad 
    337  1.1  riastrad 	/* We don't start the old stream if we are interrupted.
    338  1.1  riastrad 	 * Might return -ENOMEM if it can't fit the buffer in vram.
    339  1.1  riastrad 	 */
    340  1.1  riastrad 	ret = vmw_overlay_move_buffer(dev_priv, buf, true, interruptible);
    341  1.1  riastrad 	if (ret)
    342  1.1  riastrad 		return ret;
    343  1.1  riastrad 
    344  1.1  riastrad 	ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
    345  1.1  riastrad 	if (ret) {
    346  1.1  riastrad 		/* This one needs to happen no matter what. We only remove
    347  1.1  riastrad 		 * the NO_EVICT flag so this is safe from -ENOMEM.
    348  1.1  riastrad 		 */
    349  1.1  riastrad 		BUG_ON(vmw_overlay_move_buffer(dev_priv, buf, false, false)
    350  1.1  riastrad 		       != 0);
    351  1.1  riastrad 		return ret;
    352  1.1  riastrad 	}
    353  1.1  riastrad 
    354  1.1  riastrad 	if (stream->buf != buf)
    355  1.3  riastrad 		stream->buf = vmw_bo_reference(buf);
    356  1.1  riastrad 	stream->saved = *arg;
    357  1.1  riastrad 	/* stream is no longer stopped/paused */
    358  1.1  riastrad 	stream->paused = false;
    359  1.1  riastrad 
    360  1.1  riastrad 	return 0;
    361  1.1  riastrad }
    362  1.1  riastrad 
    363  1.1  riastrad /**
    364  1.1  riastrad  * Stop all streams.
    365  1.1  riastrad  *
    366  1.1  riastrad  * Used by the fb code when starting.
    367  1.1  riastrad  *
    368  1.1  riastrad  * Takes the overlay lock.
    369  1.1  riastrad  */
    370  1.1  riastrad int vmw_overlay_stop_all(struct vmw_private *dev_priv)
    371  1.1  riastrad {
    372  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    373  1.1  riastrad 	int i, ret;
    374  1.1  riastrad 
    375  1.1  riastrad 	if (!overlay)
    376  1.1  riastrad 		return 0;
    377  1.1  riastrad 
    378  1.1  riastrad 	mutex_lock(&overlay->mutex);
    379  1.1  riastrad 
    380  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    381  1.1  riastrad 		struct vmw_stream *stream = &overlay->stream[i];
    382  1.1  riastrad 		if (!stream->buf)
    383  1.1  riastrad 			continue;
    384  1.1  riastrad 
    385  1.1  riastrad 		ret = vmw_overlay_stop(dev_priv, i, false, false);
    386  1.1  riastrad 		WARN_ON(ret != 0);
    387  1.1  riastrad 	}
    388  1.1  riastrad 
    389  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    390  1.1  riastrad 
    391  1.1  riastrad 	return 0;
    392  1.1  riastrad }
    393  1.1  riastrad 
    394  1.1  riastrad /**
    395  1.1  riastrad  * Try to resume all paused streams.
    396  1.1  riastrad  *
    397  1.1  riastrad  * Used by the kms code after moving a new scanout buffer to vram.
    398  1.1  riastrad  *
    399  1.1  riastrad  * Takes the overlay lock.
    400  1.1  riastrad  */
    401  1.1  riastrad int vmw_overlay_resume_all(struct vmw_private *dev_priv)
    402  1.1  riastrad {
    403  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    404  1.1  riastrad 	int i, ret;
    405  1.1  riastrad 
    406  1.1  riastrad 	if (!overlay)
    407  1.1  riastrad 		return 0;
    408  1.1  riastrad 
    409  1.1  riastrad 	mutex_lock(&overlay->mutex);
    410  1.1  riastrad 
    411  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    412  1.1  riastrad 		struct vmw_stream *stream = &overlay->stream[i];
    413  1.1  riastrad 		if (!stream->paused)
    414  1.1  riastrad 			continue;
    415  1.1  riastrad 
    416  1.1  riastrad 		ret = vmw_overlay_update_stream(dev_priv, stream->buf,
    417  1.1  riastrad 						&stream->saved, false);
    418  1.1  riastrad 		if (ret != 0)
    419  1.1  riastrad 			DRM_INFO("%s: *warning* failed to resume stream %i\n",
    420  1.1  riastrad 				 __func__, i);
    421  1.1  riastrad 	}
    422  1.1  riastrad 
    423  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    424  1.1  riastrad 
    425  1.1  riastrad 	return 0;
    426  1.1  riastrad }
    427  1.1  riastrad 
    428  1.1  riastrad /**
    429  1.1  riastrad  * Pauses all active streams.
    430  1.1  riastrad  *
    431  1.1  riastrad  * Used by the kms code when moving a new scanout buffer to vram.
    432  1.1  riastrad  *
    433  1.1  riastrad  * Takes the overlay lock.
    434  1.1  riastrad  */
    435  1.1  riastrad int vmw_overlay_pause_all(struct vmw_private *dev_priv)
    436  1.1  riastrad {
    437  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    438  1.1  riastrad 	int i, ret;
    439  1.1  riastrad 
    440  1.1  riastrad 	if (!overlay)
    441  1.1  riastrad 		return 0;
    442  1.1  riastrad 
    443  1.1  riastrad 	mutex_lock(&overlay->mutex);
    444  1.1  riastrad 
    445  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    446  1.1  riastrad 		if (overlay->stream[i].paused)
    447  1.1  riastrad 			DRM_INFO("%s: *warning* stream %i already paused\n",
    448  1.1  riastrad 				 __func__, i);
    449  1.1  riastrad 		ret = vmw_overlay_stop(dev_priv, i, true, false);
    450  1.1  riastrad 		WARN_ON(ret != 0);
    451  1.1  riastrad 	}
    452  1.1  riastrad 
    453  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    454  1.1  riastrad 
    455  1.1  riastrad 	return 0;
    456  1.1  riastrad }
    457  1.1  riastrad 
    458  1.1  riastrad 
    459  1.1  riastrad static bool vmw_overlay_available(const struct vmw_private *dev_priv)
    460  1.1  riastrad {
    461  1.2  riastrad 	return (dev_priv->overlay_priv != NULL &&
    462  1.1  riastrad 		((dev_priv->fifo.capabilities & VMW_OVERLAY_CAP_MASK) ==
    463  1.1  riastrad 		 VMW_OVERLAY_CAP_MASK));
    464  1.1  riastrad }
    465  1.1  riastrad 
    466  1.1  riastrad int vmw_overlay_ioctl(struct drm_device *dev, void *data,
    467  1.1  riastrad 		      struct drm_file *file_priv)
    468  1.1  riastrad {
    469  1.1  riastrad 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
    470  1.1  riastrad 	struct vmw_private *dev_priv = vmw_priv(dev);
    471  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    472  1.1  riastrad 	struct drm_vmw_control_stream_arg *arg =
    473  1.1  riastrad 	    (struct drm_vmw_control_stream_arg *)data;
    474  1.3  riastrad 	struct vmw_buffer_object *buf;
    475  1.1  riastrad 	struct vmw_resource *res;
    476  1.1  riastrad 	int ret;
    477  1.1  riastrad 
    478  1.1  riastrad 	if (!vmw_overlay_available(dev_priv))
    479  1.1  riastrad 		return -ENOSYS;
    480  1.1  riastrad 
    481  1.1  riastrad 	ret = vmw_user_stream_lookup(dev_priv, tfile, &arg->stream_id, &res);
    482  1.1  riastrad 	if (ret)
    483  1.1  riastrad 		return ret;
    484  1.1  riastrad 
    485  1.1  riastrad 	mutex_lock(&overlay->mutex);
    486  1.1  riastrad 
    487  1.1  riastrad 	if (!arg->enabled) {
    488  1.1  riastrad 		ret = vmw_overlay_stop(dev_priv, arg->stream_id, false, true);
    489  1.1  riastrad 		goto out_unlock;
    490  1.1  riastrad 	}
    491  1.1  riastrad 
    492  1.3  riastrad 	ret = vmw_user_bo_lookup(tfile, arg->handle, &buf, NULL);
    493  1.1  riastrad 	if (ret)
    494  1.1  riastrad 		goto out_unlock;
    495  1.1  riastrad 
    496  1.1  riastrad 	ret = vmw_overlay_update_stream(dev_priv, buf, arg, true);
    497  1.1  riastrad 
    498  1.3  riastrad 	vmw_bo_unreference(&buf);
    499  1.1  riastrad 
    500  1.1  riastrad out_unlock:
    501  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    502  1.1  riastrad 	vmw_resource_unreference(&res);
    503  1.1  riastrad 
    504  1.1  riastrad 	return ret;
    505  1.1  riastrad }
    506  1.1  riastrad 
    507  1.1  riastrad int vmw_overlay_num_overlays(struct vmw_private *dev_priv)
    508  1.1  riastrad {
    509  1.1  riastrad 	if (!vmw_overlay_available(dev_priv))
    510  1.1  riastrad 		return 0;
    511  1.1  riastrad 
    512  1.1  riastrad 	return VMW_MAX_NUM_STREAMS;
    513  1.1  riastrad }
    514  1.1  riastrad 
    515  1.1  riastrad int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv)
    516  1.1  riastrad {
    517  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    518  1.1  riastrad 	int i, k;
    519  1.1  riastrad 
    520  1.1  riastrad 	if (!vmw_overlay_available(dev_priv))
    521  1.1  riastrad 		return 0;
    522  1.1  riastrad 
    523  1.1  riastrad 	mutex_lock(&overlay->mutex);
    524  1.1  riastrad 
    525  1.1  riastrad 	for (i = 0, k = 0; i < VMW_MAX_NUM_STREAMS; i++)
    526  1.1  riastrad 		if (!overlay->stream[i].claimed)
    527  1.1  riastrad 			k++;
    528  1.1  riastrad 
    529  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    530  1.1  riastrad 
    531  1.1  riastrad 	return k;
    532  1.1  riastrad }
    533  1.1  riastrad 
    534  1.1  riastrad int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out)
    535  1.1  riastrad {
    536  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    537  1.1  riastrad 	int i;
    538  1.1  riastrad 
    539  1.1  riastrad 	if (!overlay)
    540  1.1  riastrad 		return -ENOSYS;
    541  1.1  riastrad 
    542  1.1  riastrad 	mutex_lock(&overlay->mutex);
    543  1.1  riastrad 
    544  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    545  1.1  riastrad 
    546  1.1  riastrad 		if (overlay->stream[i].claimed)
    547  1.1  riastrad 			continue;
    548  1.1  riastrad 
    549  1.1  riastrad 		overlay->stream[i].claimed = true;
    550  1.1  riastrad 		*out = i;
    551  1.1  riastrad 		mutex_unlock(&overlay->mutex);
    552  1.1  riastrad 		return 0;
    553  1.1  riastrad 	}
    554  1.1  riastrad 
    555  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    556  1.1  riastrad 	return -ESRCH;
    557  1.1  riastrad }
    558  1.1  riastrad 
    559  1.1  riastrad int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id)
    560  1.1  riastrad {
    561  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    562  1.1  riastrad 
    563  1.1  riastrad 	BUG_ON(stream_id >= VMW_MAX_NUM_STREAMS);
    564  1.1  riastrad 
    565  1.1  riastrad 	if (!overlay)
    566  1.1  riastrad 		return -ENOSYS;
    567  1.1  riastrad 
    568  1.1  riastrad 	mutex_lock(&overlay->mutex);
    569  1.1  riastrad 
    570  1.1  riastrad 	WARN_ON(!overlay->stream[stream_id].claimed);
    571  1.1  riastrad 	vmw_overlay_stop(dev_priv, stream_id, false, false);
    572  1.1  riastrad 	overlay->stream[stream_id].claimed = false;
    573  1.1  riastrad 
    574  1.1  riastrad 	mutex_unlock(&overlay->mutex);
    575  1.1  riastrad 	return 0;
    576  1.1  riastrad }
    577  1.1  riastrad 
    578  1.1  riastrad int vmw_overlay_init(struct vmw_private *dev_priv)
    579  1.1  riastrad {
    580  1.1  riastrad 	struct vmw_overlay *overlay;
    581  1.1  riastrad 	int i;
    582  1.1  riastrad 
    583  1.1  riastrad 	if (dev_priv->overlay_priv)
    584  1.1  riastrad 		return -EINVAL;
    585  1.1  riastrad 
    586  1.1  riastrad 	overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
    587  1.1  riastrad 	if (!overlay)
    588  1.1  riastrad 		return -ENOMEM;
    589  1.1  riastrad 
    590  1.1  riastrad 	mutex_init(&overlay->mutex);
    591  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    592  1.1  riastrad 		overlay->stream[i].buf = NULL;
    593  1.1  riastrad 		overlay->stream[i].paused = false;
    594  1.1  riastrad 		overlay->stream[i].claimed = false;
    595  1.1  riastrad 	}
    596  1.1  riastrad 
    597  1.1  riastrad 	dev_priv->overlay_priv = overlay;
    598  1.1  riastrad 
    599  1.1  riastrad 	return 0;
    600  1.1  riastrad }
    601  1.1  riastrad 
    602  1.1  riastrad int vmw_overlay_close(struct vmw_private *dev_priv)
    603  1.1  riastrad {
    604  1.1  riastrad 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
    605  1.1  riastrad 	bool forgotten_buffer = false;
    606  1.1  riastrad 	int i;
    607  1.1  riastrad 
    608  1.1  riastrad 	if (!overlay)
    609  1.1  riastrad 		return -ENOSYS;
    610  1.1  riastrad 
    611  1.1  riastrad 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
    612  1.1  riastrad 		if (overlay->stream[i].buf) {
    613  1.1  riastrad 			forgotten_buffer = true;
    614  1.1  riastrad 			vmw_overlay_stop(dev_priv, i, false, false);
    615  1.1  riastrad 		}
    616  1.1  riastrad 	}
    617  1.1  riastrad 
    618  1.1  riastrad 	WARN_ON(forgotten_buffer);
    619  1.1  riastrad 
    620  1.1  riastrad 	dev_priv->overlay_priv = NULL;
    621  1.1  riastrad 	kfree(overlay);
    622  1.1  riastrad 
    623  1.1  riastrad 	return 0;
    624  1.1  riastrad }
    625