xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_overlay.c (revision 29b4a6996c244f0d360537d6a4a0996468372c17)
1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
2fb1d9738SJakob Bornecrantz /**************************************************************************
3fb1d9738SJakob Bornecrantz  *
409881d29SZack Rusin  * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
5fb1d9738SJakob Bornecrantz  *
6fb1d9738SJakob Bornecrantz  * Permission is hereby granted, free of charge, to any person obtaining a
7fb1d9738SJakob Bornecrantz  * copy of this software and associated documentation files (the
8fb1d9738SJakob Bornecrantz  * "Software"), to deal in the Software without restriction, including
9fb1d9738SJakob Bornecrantz  * without limitation the rights to use, copy, modify, merge, publish,
10fb1d9738SJakob Bornecrantz  * distribute, sub license, and/or sell copies of the Software, and to
11fb1d9738SJakob Bornecrantz  * permit persons to whom the Software is furnished to do so, subject to
12fb1d9738SJakob Bornecrantz  * the following conditions:
13fb1d9738SJakob Bornecrantz  *
14fb1d9738SJakob Bornecrantz  * The above copyright notice and this permission notice (including the
15fb1d9738SJakob Bornecrantz  * next paragraph) shall be included in all copies or substantial portions
16fb1d9738SJakob Bornecrantz  * of the Software.
17fb1d9738SJakob Bornecrantz  *
18fb1d9738SJakob Bornecrantz  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19fb1d9738SJakob Bornecrantz  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20fb1d9738SJakob Bornecrantz  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21fb1d9738SJakob Bornecrantz  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22fb1d9738SJakob Bornecrantz  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23fb1d9738SJakob Bornecrantz  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24fb1d9738SJakob Bornecrantz  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25fb1d9738SJakob Bornecrantz  *
26fb1d9738SJakob Bornecrantz  **************************************************************************/
2709881d29SZack Rusin #include "vmwgfx_bo.h"
2809881d29SZack Rusin #include "vmwgfx_drv.h"
29fb1d9738SJakob Bornecrantz 
308ce75f8aSSinclair Yeh #include "device_include/svga_overlay.h"
318ce75f8aSSinclair Yeh #include "device_include/svga_escape.h"
32fb1d9738SJakob Bornecrantz 
3309881d29SZack Rusin #include <drm/ttm/ttm_placement.h>
346ae8748bSSam Ravnborg 
35fb1d9738SJakob Bornecrantz #define VMW_MAX_NUM_STREAMS 1
36dcb52271SThomas Hellstrom #define VMW_OVERLAY_CAP_MASK (SVGA_FIFO_CAP_VIDEO | SVGA_FIFO_CAP_ESCAPE)
37fb1d9738SJakob Bornecrantz 
38fb1d9738SJakob Bornecrantz struct vmw_stream {
3909881d29SZack Rusin 	struct vmw_bo *buf;
40fb1d9738SJakob Bornecrantz 	bool claimed;
41fb1d9738SJakob Bornecrantz 	bool paused;
42fb1d9738SJakob Bornecrantz 	struct drm_vmw_control_stream_arg saved;
43fb1d9738SJakob Bornecrantz };
44fb1d9738SJakob Bornecrantz 
453a839da0SLee Jones /*
46fb1d9738SJakob Bornecrantz  * Overlay control
47fb1d9738SJakob Bornecrantz  */
48fb1d9738SJakob Bornecrantz struct vmw_overlay {
49fb1d9738SJakob Bornecrantz 	/*
50fb1d9738SJakob Bornecrantz 	 * Each stream is a single overlay. In Xv these are called ports.
51fb1d9738SJakob Bornecrantz 	 */
52fb1d9738SJakob Bornecrantz 	struct mutex mutex;
53fb1d9738SJakob Bornecrantz 	struct vmw_stream stream[VMW_MAX_NUM_STREAMS];
54fb1d9738SJakob Bornecrantz };
55fb1d9738SJakob Bornecrantz 
56fb1d9738SJakob Bornecrantz struct vmw_escape_header {
57fb1d9738SJakob Bornecrantz 	uint32_t cmd;
58fb1d9738SJakob Bornecrantz 	SVGAFifoCmdEscape body;
59fb1d9738SJakob Bornecrantz };
60fb1d9738SJakob Bornecrantz 
61fb1d9738SJakob Bornecrantz struct vmw_escape_video_flush {
62fb1d9738SJakob Bornecrantz 	struct vmw_escape_header escape;
63fb1d9738SJakob Bornecrantz 	SVGAEscapeVideoFlush flush;
64fb1d9738SJakob Bornecrantz };
65fb1d9738SJakob Bornecrantz 
fill_escape(struct vmw_escape_header * header,uint32_t size)66fb1d9738SJakob Bornecrantz static inline void fill_escape(struct vmw_escape_header *header,
67fb1d9738SJakob Bornecrantz 			       uint32_t size)
68fb1d9738SJakob Bornecrantz {
69fb1d9738SJakob Bornecrantz 	header->cmd = SVGA_CMD_ESCAPE;
70fb1d9738SJakob Bornecrantz 	header->body.nsid = SVGA_ESCAPE_NSID_VMWARE;
71fb1d9738SJakob Bornecrantz 	header->body.size = size;
72fb1d9738SJakob Bornecrantz }
73fb1d9738SJakob Bornecrantz 
fill_flush(struct vmw_escape_video_flush * cmd,uint32_t stream_id)74fb1d9738SJakob Bornecrantz static inline void fill_flush(struct vmw_escape_video_flush *cmd,
75fb1d9738SJakob Bornecrantz 			      uint32_t stream_id)
76fb1d9738SJakob Bornecrantz {
77fb1d9738SJakob Bornecrantz 	fill_escape(&cmd->escape, sizeof(cmd->flush));
78fb1d9738SJakob Bornecrantz 	cmd->flush.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_FLUSH;
79fb1d9738SJakob Bornecrantz 	cmd->flush.streamId = stream_id;
80fb1d9738SJakob Bornecrantz }
81fb1d9738SJakob Bornecrantz 
823a839da0SLee Jones /*
83fb1d9738SJakob Bornecrantz  * Send put command to hw.
84fb1d9738SJakob Bornecrantz  *
85fb1d9738SJakob Bornecrantz  * Returns
86fb1d9738SJakob Bornecrantz  * -ERESTARTSYS if interrupted by a signal.
87fb1d9738SJakob Bornecrantz  */
vmw_overlay_send_put(struct vmw_private * dev_priv,struct vmw_bo * buf,struct drm_vmw_control_stream_arg * arg,bool interruptible)88fb1d9738SJakob Bornecrantz static int vmw_overlay_send_put(struct vmw_private *dev_priv,
8909881d29SZack Rusin 				struct vmw_bo *buf,
90fb1d9738SJakob Bornecrantz 				struct drm_vmw_control_stream_arg *arg,
91fb1d9738SJakob Bornecrantz 				bool interruptible)
92fb1d9738SJakob Bornecrantz {
9344031d25SJakob Bornecrantz 	struct vmw_escape_video_flush *flush;
9444031d25SJakob Bornecrantz 	size_t fifo_size;
95*cb372a50SIan Forbes 	bool have_so = (dev_priv->active_display_unit != vmw_du_legacy);
9644031d25SJakob Bornecrantz 	int i, num_items;
97b37a6b9aSThomas Hellstrom 	SVGAGuestPtr ptr;
9844031d25SJakob Bornecrantz 
99fb1d9738SJakob Bornecrantz 	struct {
100fb1d9738SJakob Bornecrantz 		struct vmw_escape_header escape;
101fb1d9738SJakob Bornecrantz 		struct {
102fb1d9738SJakob Bornecrantz 			uint32_t cmdType;
103fb1d9738SJakob Bornecrantz 			uint32_t streamId;
104fb1d9738SJakob Bornecrantz 		} header;
10544031d25SJakob Bornecrantz 	} *cmds;
106fb1d9738SJakob Bornecrantz 	struct {
107fb1d9738SJakob Bornecrantz 		uint32_t registerId;
108fb1d9738SJakob Bornecrantz 		uint32_t value;
10944031d25SJakob Bornecrantz 	} *items;
110fb1d9738SJakob Bornecrantz 
11144031d25SJakob Bornecrantz 	/* defines are a index needs + 1 */
11244031d25SJakob Bornecrantz 	if (have_so)
11344031d25SJakob Bornecrantz 		num_items = SVGA_VIDEO_DST_SCREEN_ID + 1;
114fb1d9738SJakob Bornecrantz 	else
11544031d25SJakob Bornecrantz 		num_items = SVGA_VIDEO_PITCH_3 + 1;
11644031d25SJakob Bornecrantz 
11744031d25SJakob Bornecrantz 	fifo_size = sizeof(*cmds) + sizeof(*flush) + sizeof(*items) * num_items;
11844031d25SJakob Bornecrantz 
1198426ed9cSZack Rusin 	cmds = VMW_CMD_RESERVE(dev_priv, fifo_size);
12044031d25SJakob Bornecrantz 	/* hardware has hung, can't do anything here */
12144031d25SJakob Bornecrantz 	if (!cmds)
12244031d25SJakob Bornecrantz 		return -ENOMEM;
12344031d25SJakob Bornecrantz 
12444031d25SJakob Bornecrantz 	items = (typeof(items))&cmds[1];
12544031d25SJakob Bornecrantz 	flush = (struct vmw_escape_video_flush *)&items[num_items];
12644031d25SJakob Bornecrantz 
12744031d25SJakob Bornecrantz 	/* the size is header + number of items */
12844031d25SJakob Bornecrantz 	fill_escape(&cmds->escape, sizeof(*items) * (num_items + 1));
12944031d25SJakob Bornecrantz 
13044031d25SJakob Bornecrantz 	cmds->header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
13144031d25SJakob Bornecrantz 	cmds->header.streamId = arg->stream_id;
13244031d25SJakob Bornecrantz 
13344031d25SJakob Bornecrantz 	/* the IDs are neatly numbered */
13444031d25SJakob Bornecrantz 	for (i = 0; i < num_items; i++)
13544031d25SJakob Bornecrantz 		items[i].registerId = i;
13644031d25SJakob Bornecrantz 
137668b2066SZack Rusin 	vmw_bo_get_guest_ptr(&buf->tbo, &ptr);
138b37a6b9aSThomas Hellstrom 	ptr.offset += arg->offset;
13944031d25SJakob Bornecrantz 
14044031d25SJakob Bornecrantz 	items[SVGA_VIDEO_ENABLED].value     = true;
14144031d25SJakob Bornecrantz 	items[SVGA_VIDEO_FLAGS].value       = arg->flags;
142b37a6b9aSThomas Hellstrom 	items[SVGA_VIDEO_DATA_OFFSET].value = ptr.offset;
14344031d25SJakob Bornecrantz 	items[SVGA_VIDEO_FORMAT].value      = arg->format;
14444031d25SJakob Bornecrantz 	items[SVGA_VIDEO_COLORKEY].value    = arg->color_key;
14544031d25SJakob Bornecrantz 	items[SVGA_VIDEO_SIZE].value        = arg->size;
14644031d25SJakob Bornecrantz 	items[SVGA_VIDEO_WIDTH].value       = arg->width;
14744031d25SJakob Bornecrantz 	items[SVGA_VIDEO_HEIGHT].value      = arg->height;
14844031d25SJakob Bornecrantz 	items[SVGA_VIDEO_SRC_X].value       = arg->src.x;
14944031d25SJakob Bornecrantz 	items[SVGA_VIDEO_SRC_Y].value       = arg->src.y;
15044031d25SJakob Bornecrantz 	items[SVGA_VIDEO_SRC_WIDTH].value   = arg->src.w;
15144031d25SJakob Bornecrantz 	items[SVGA_VIDEO_SRC_HEIGHT].value  = arg->src.h;
15244031d25SJakob Bornecrantz 	items[SVGA_VIDEO_DST_X].value       = arg->dst.x;
15344031d25SJakob Bornecrantz 	items[SVGA_VIDEO_DST_Y].value       = arg->dst.y;
15444031d25SJakob Bornecrantz 	items[SVGA_VIDEO_DST_WIDTH].value   = arg->dst.w;
15544031d25SJakob Bornecrantz 	items[SVGA_VIDEO_DST_HEIGHT].value  = arg->dst.h;
15644031d25SJakob Bornecrantz 	items[SVGA_VIDEO_PITCH_1].value     = arg->pitch[0];
15744031d25SJakob Bornecrantz 	items[SVGA_VIDEO_PITCH_2].value     = arg->pitch[1];
15844031d25SJakob Bornecrantz 	items[SVGA_VIDEO_PITCH_3].value     = arg->pitch[2];
15944031d25SJakob Bornecrantz 	if (have_so) {
160b37a6b9aSThomas Hellstrom 		items[SVGA_VIDEO_DATA_GMRID].value    = ptr.gmrId;
16144031d25SJakob Bornecrantz 		items[SVGA_VIDEO_DST_SCREEN_ID].value = SVGA_ID_INVALID;
162fb1d9738SJakob Bornecrantz 	}
163fb1d9738SJakob Bornecrantz 
16444031d25SJakob Bornecrantz 	fill_flush(flush, arg->stream_id);
165fb1d9738SJakob Bornecrantz 
1668426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, fifo_size);
167fb1d9738SJakob Bornecrantz 
168fb1d9738SJakob Bornecrantz 	return 0;
169fb1d9738SJakob Bornecrantz }
170fb1d9738SJakob Bornecrantz 
1713a839da0SLee Jones /*
172fb1d9738SJakob Bornecrantz  * Send stop command to hw.
173fb1d9738SJakob Bornecrantz  *
174fb1d9738SJakob Bornecrantz  * Returns
175fb1d9738SJakob Bornecrantz  * -ERESTARTSYS if interrupted by a signal.
176fb1d9738SJakob Bornecrantz  */
vmw_overlay_send_stop(struct vmw_private * dev_priv,uint32_t stream_id,bool interruptible)177fb1d9738SJakob Bornecrantz static int vmw_overlay_send_stop(struct vmw_private *dev_priv,
178fb1d9738SJakob Bornecrantz 				 uint32_t stream_id,
179fb1d9738SJakob Bornecrantz 				 bool interruptible)
180fb1d9738SJakob Bornecrantz {
181fb1d9738SJakob Bornecrantz 	struct {
182fb1d9738SJakob Bornecrantz 		struct vmw_escape_header escape;
183fb1d9738SJakob Bornecrantz 		SVGAEscapeVideoSetRegs body;
184fb1d9738SJakob Bornecrantz 		struct vmw_escape_video_flush flush;
185fb1d9738SJakob Bornecrantz 	} *cmds;
186fb1d9738SJakob Bornecrantz 	int ret;
187fb1d9738SJakob Bornecrantz 
188fb1d9738SJakob Bornecrantz 	for (;;) {
1898426ed9cSZack Rusin 		cmds = VMW_CMD_RESERVE(dev_priv, sizeof(*cmds));
190fb1d9738SJakob Bornecrantz 		if (cmds)
191fb1d9738SJakob Bornecrantz 			break;
192fb1d9738SJakob Bornecrantz 
193fb1d9738SJakob Bornecrantz 		ret = vmw_fallback_wait(dev_priv, false, true, 0,
194fb1d9738SJakob Bornecrantz 					interruptible, 3*HZ);
195fb1d9738SJakob Bornecrantz 		if (interruptible && ret == -ERESTARTSYS)
196fb1d9738SJakob Bornecrantz 			return ret;
197fb1d9738SJakob Bornecrantz 		else
198fb1d9738SJakob Bornecrantz 			BUG_ON(ret != 0);
199fb1d9738SJakob Bornecrantz 	}
200fb1d9738SJakob Bornecrantz 
201fb1d9738SJakob Bornecrantz 	fill_escape(&cmds->escape, sizeof(cmds->body));
202fb1d9738SJakob Bornecrantz 	cmds->body.header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
203fb1d9738SJakob Bornecrantz 	cmds->body.header.streamId = stream_id;
204fb1d9738SJakob Bornecrantz 	cmds->body.items[0].registerId = SVGA_VIDEO_ENABLED;
205fb1d9738SJakob Bornecrantz 	cmds->body.items[0].value = false;
206fb1d9738SJakob Bornecrantz 	fill_flush(&cmds->flush, stream_id);
207fb1d9738SJakob Bornecrantz 
2088426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmds));
209fb1d9738SJakob Bornecrantz 
210fb1d9738SJakob Bornecrantz 	return 0;
211fb1d9738SJakob Bornecrantz }
212fb1d9738SJakob Bornecrantz 
2133a839da0SLee Jones /*
21444031d25SJakob Bornecrantz  * Move a buffer to vram or gmr if @pin is set, else unpin the buffer.
215d991ef03SJakob Bornecrantz  *
21644031d25SJakob Bornecrantz  * With the introduction of screen objects buffers could now be
21744031d25SJakob Bornecrantz  * used with GMRs instead of being locked to vram.
218d991ef03SJakob Bornecrantz  */
vmw_overlay_move_buffer(struct vmw_private * dev_priv,struct vmw_bo * buf,bool pin,bool inter)219d991ef03SJakob Bornecrantz static int vmw_overlay_move_buffer(struct vmw_private *dev_priv,
22009881d29SZack Rusin 				   struct vmw_bo *buf,
221d991ef03SJakob Bornecrantz 				   bool pin, bool inter)
222d991ef03SJakob Bornecrantz {
22344031d25SJakob Bornecrantz 	if (!pin)
224f1d34bfdSThomas Hellstrom 		return vmw_bo_unpin(dev_priv, buf, inter);
22544031d25SJakob Bornecrantz 
226c8261a96SSinclair Yeh 	if (dev_priv->active_display_unit == vmw_du_legacy)
227f1d34bfdSThomas Hellstrom 		return vmw_bo_pin_in_vram(dev_priv, buf, inter);
22844031d25SJakob Bornecrantz 
229f1d34bfdSThomas Hellstrom 	return vmw_bo_pin_in_vram_or_gmr(dev_priv, buf, inter);
230d991ef03SJakob Bornecrantz }
231d991ef03SJakob Bornecrantz 
2323a839da0SLee Jones /*
233fb1d9738SJakob Bornecrantz  * Stop or pause a stream.
234fb1d9738SJakob Bornecrantz  *
235fb1d9738SJakob Bornecrantz  * If the stream is paused the no evict flag is removed from the buffer
236fb1d9738SJakob Bornecrantz  * but left in vram. This allows for instance mode_set to evict it
237fb1d9738SJakob Bornecrantz  * should it need to.
238fb1d9738SJakob Bornecrantz  *
239fb1d9738SJakob Bornecrantz  * The caller must hold the overlay lock.
240fb1d9738SJakob Bornecrantz  *
241fb1d9738SJakob Bornecrantz  * @stream_id which stream to stop/pause.
242fb1d9738SJakob Bornecrantz  * @pause true to pause, false to stop completely.
243fb1d9738SJakob Bornecrantz  */
vmw_overlay_stop(struct vmw_private * dev_priv,uint32_t stream_id,bool pause,bool interruptible)244fb1d9738SJakob Bornecrantz static int vmw_overlay_stop(struct vmw_private *dev_priv,
245fb1d9738SJakob Bornecrantz 			    uint32_t stream_id, bool pause,
246fb1d9738SJakob Bornecrantz 			    bool interruptible)
247fb1d9738SJakob Bornecrantz {
248fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
249fb1d9738SJakob Bornecrantz 	struct vmw_stream *stream = &overlay->stream[stream_id];
250fb1d9738SJakob Bornecrantz 	int ret;
251fb1d9738SJakob Bornecrantz 
252fb1d9738SJakob Bornecrantz 	/* no buffer attached the stream is completely stopped */
253fb1d9738SJakob Bornecrantz 	if (!stream->buf)
254fb1d9738SJakob Bornecrantz 		return 0;
255fb1d9738SJakob Bornecrantz 
256fb1d9738SJakob Bornecrantz 	/* If the stream is paused this is already done */
257fb1d9738SJakob Bornecrantz 	if (!stream->paused) {
258fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_send_stop(dev_priv, stream_id,
259fb1d9738SJakob Bornecrantz 					    interruptible);
260fb1d9738SJakob Bornecrantz 		if (ret)
261fb1d9738SJakob Bornecrantz 			return ret;
262fb1d9738SJakob Bornecrantz 
263fb1d9738SJakob Bornecrantz 		/* We just remove the NO_EVICT flag so no -ENOMEM */
264d991ef03SJakob Bornecrantz 		ret = vmw_overlay_move_buffer(dev_priv, stream->buf, false,
265fb1d9738SJakob Bornecrantz 					      interruptible);
266fb1d9738SJakob Bornecrantz 		if (interruptible && ret == -ERESTARTSYS)
267fb1d9738SJakob Bornecrantz 			return ret;
268fb1d9738SJakob Bornecrantz 		else
269fb1d9738SJakob Bornecrantz 			BUG_ON(ret != 0);
270fb1d9738SJakob Bornecrantz 	}
271fb1d9738SJakob Bornecrantz 
272fb1d9738SJakob Bornecrantz 	if (!pause) {
273f1d34bfdSThomas Hellstrom 		vmw_bo_unreference(&stream->buf);
274fb1d9738SJakob Bornecrantz 		stream->paused = false;
275fb1d9738SJakob Bornecrantz 	} else {
276fb1d9738SJakob Bornecrantz 		stream->paused = true;
277fb1d9738SJakob Bornecrantz 	}
278fb1d9738SJakob Bornecrantz 
279fb1d9738SJakob Bornecrantz 	return 0;
280fb1d9738SJakob Bornecrantz }
281fb1d9738SJakob Bornecrantz 
2823a839da0SLee Jones /*
283fb1d9738SJakob Bornecrantz  * Update a stream and send any put or stop fifo commands needed.
284fb1d9738SJakob Bornecrantz  *
285fb1d9738SJakob Bornecrantz  * The caller must hold the overlay lock.
286fb1d9738SJakob Bornecrantz  *
287fb1d9738SJakob Bornecrantz  * Returns
288fb1d9738SJakob Bornecrantz  * -ENOMEM if buffer doesn't fit in vram.
289fb1d9738SJakob Bornecrantz  * -ERESTARTSYS if interrupted.
290fb1d9738SJakob Bornecrantz  */
vmw_overlay_update_stream(struct vmw_private * dev_priv,struct vmw_bo * buf,struct drm_vmw_control_stream_arg * arg,bool interruptible)291fb1d9738SJakob Bornecrantz static int vmw_overlay_update_stream(struct vmw_private *dev_priv,
29209881d29SZack Rusin 				     struct vmw_bo *buf,
293fb1d9738SJakob Bornecrantz 				     struct drm_vmw_control_stream_arg *arg,
294fb1d9738SJakob Bornecrantz 				     bool interruptible)
295fb1d9738SJakob Bornecrantz {
296fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
297fb1d9738SJakob Bornecrantz 	struct vmw_stream *stream = &overlay->stream[arg->stream_id];
298fb1d9738SJakob Bornecrantz 	int ret = 0;
299fb1d9738SJakob Bornecrantz 
300fb1d9738SJakob Bornecrantz 	if (!buf)
301fb1d9738SJakob Bornecrantz 		return -EINVAL;
302fb1d9738SJakob Bornecrantz 
303fb1d9738SJakob Bornecrantz 	DRM_DEBUG("   %s: old %p, new %p, %spaused\n", __func__,
304fb1d9738SJakob Bornecrantz 		  stream->buf, buf, stream->paused ? "" : "not ");
305fb1d9738SJakob Bornecrantz 
306fb1d9738SJakob Bornecrantz 	if (stream->buf != buf) {
307fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_stop(dev_priv, arg->stream_id,
308fb1d9738SJakob Bornecrantz 				       false, interruptible);
309fb1d9738SJakob Bornecrantz 		if (ret)
310fb1d9738SJakob Bornecrantz 			return ret;
311fb1d9738SJakob Bornecrantz 	} else if (!stream->paused) {
312fb1d9738SJakob Bornecrantz 		/* If the buffers match and not paused then just send
313fb1d9738SJakob Bornecrantz 		 * the put command, no need to do anything else.
314fb1d9738SJakob Bornecrantz 		 */
315fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
316fb1d9738SJakob Bornecrantz 		if (ret == 0)
317fb1d9738SJakob Bornecrantz 			stream->saved = *arg;
318fb1d9738SJakob Bornecrantz 		else
319fb1d9738SJakob Bornecrantz 			BUG_ON(!interruptible);
320fb1d9738SJakob Bornecrantz 
321fb1d9738SJakob Bornecrantz 		return ret;
322fb1d9738SJakob Bornecrantz 	}
323fb1d9738SJakob Bornecrantz 
324fb1d9738SJakob Bornecrantz 	/* We don't start the old stream if we are interrupted.
325fb1d9738SJakob Bornecrantz 	 * Might return -ENOMEM if it can't fit the buffer in vram.
326fb1d9738SJakob Bornecrantz 	 */
327d991ef03SJakob Bornecrantz 	ret = vmw_overlay_move_buffer(dev_priv, buf, true, interruptible);
328fb1d9738SJakob Bornecrantz 	if (ret)
329fb1d9738SJakob Bornecrantz 		return ret;
330fb1d9738SJakob Bornecrantz 
331fb1d9738SJakob Bornecrantz 	ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
332fb1d9738SJakob Bornecrantz 	if (ret) {
333fb1d9738SJakob Bornecrantz 		/* This one needs to happen no matter what. We only remove
334fb1d9738SJakob Bornecrantz 		 * the NO_EVICT flag so this is safe from -ENOMEM.
335fb1d9738SJakob Bornecrantz 		 */
336d991ef03SJakob Bornecrantz 		BUG_ON(vmw_overlay_move_buffer(dev_priv, buf, false, false)
337d991ef03SJakob Bornecrantz 		       != 0);
338fb1d9738SJakob Bornecrantz 		return ret;
339fb1d9738SJakob Bornecrantz 	}
340fb1d9738SJakob Bornecrantz 
341fb1d9738SJakob Bornecrantz 	if (stream->buf != buf)
342f1d34bfdSThomas Hellstrom 		stream->buf = vmw_bo_reference(buf);
343fb1d9738SJakob Bornecrantz 	stream->saved = *arg;
344792778e8SJakob Bornecrantz 	/* stream is no longer stopped/paused */
345792778e8SJakob Bornecrantz 	stream->paused = false;
346fb1d9738SJakob Bornecrantz 
347fb1d9738SJakob Bornecrantz 	return 0;
348fb1d9738SJakob Bornecrantz }
349fb1d9738SJakob Bornecrantz 
3503a839da0SLee Jones /*
351fb1d9738SJakob Bornecrantz  * Try to resume all paused streams.
352fb1d9738SJakob Bornecrantz  *
353fb1d9738SJakob Bornecrantz  * Used by the kms code after moving a new scanout buffer to vram.
354fb1d9738SJakob Bornecrantz  *
355fb1d9738SJakob Bornecrantz  * Takes the overlay lock.
356fb1d9738SJakob Bornecrantz  */
vmw_overlay_resume_all(struct vmw_private * dev_priv)357fb1d9738SJakob Bornecrantz int vmw_overlay_resume_all(struct vmw_private *dev_priv)
358fb1d9738SJakob Bornecrantz {
359fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
360fb1d9738SJakob Bornecrantz 	int i, ret;
361fb1d9738SJakob Bornecrantz 
362fb1d9738SJakob Bornecrantz 	if (!overlay)
363fb1d9738SJakob Bornecrantz 		return 0;
364fb1d9738SJakob Bornecrantz 
365fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
366fb1d9738SJakob Bornecrantz 
367fb1d9738SJakob Bornecrantz 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
368fb1d9738SJakob Bornecrantz 		struct vmw_stream *stream = &overlay->stream[i];
369fb1d9738SJakob Bornecrantz 		if (!stream->paused)
370fb1d9738SJakob Bornecrantz 			continue;
371fb1d9738SJakob Bornecrantz 
372fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_update_stream(dev_priv, stream->buf,
373fb1d9738SJakob Bornecrantz 						&stream->saved, false);
374fb1d9738SJakob Bornecrantz 		if (ret != 0)
375fb1d9738SJakob Bornecrantz 			DRM_INFO("%s: *warning* failed to resume stream %i\n",
376fb1d9738SJakob Bornecrantz 				 __func__, i);
377fb1d9738SJakob Bornecrantz 	}
378fb1d9738SJakob Bornecrantz 
379fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
380fb1d9738SJakob Bornecrantz 
381fb1d9738SJakob Bornecrantz 	return 0;
382fb1d9738SJakob Bornecrantz }
383fb1d9738SJakob Bornecrantz 
3843a839da0SLee Jones /*
385fb1d9738SJakob Bornecrantz  * Pauses all active streams.
386fb1d9738SJakob Bornecrantz  *
387fb1d9738SJakob Bornecrantz  * Used by the kms code when moving a new scanout buffer to vram.
388fb1d9738SJakob Bornecrantz  *
389fb1d9738SJakob Bornecrantz  * Takes the overlay lock.
390fb1d9738SJakob Bornecrantz  */
vmw_overlay_pause_all(struct vmw_private * dev_priv)391fb1d9738SJakob Bornecrantz int vmw_overlay_pause_all(struct vmw_private *dev_priv)
392fb1d9738SJakob Bornecrantz {
393fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
394fb1d9738SJakob Bornecrantz 	int i, ret;
395fb1d9738SJakob Bornecrantz 
396fb1d9738SJakob Bornecrantz 	if (!overlay)
397fb1d9738SJakob Bornecrantz 		return 0;
398fb1d9738SJakob Bornecrantz 
399fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
400fb1d9738SJakob Bornecrantz 
401fb1d9738SJakob Bornecrantz 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
402fb1d9738SJakob Bornecrantz 		if (overlay->stream[i].paused)
403fb1d9738SJakob Bornecrantz 			DRM_INFO("%s: *warning* stream %i already paused\n",
404fb1d9738SJakob Bornecrantz 				 __func__, i);
405fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_stop(dev_priv, i, true, false);
406fb1d9738SJakob Bornecrantz 		WARN_ON(ret != 0);
407fb1d9738SJakob Bornecrantz 	}
408fb1d9738SJakob Bornecrantz 
409fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
410fb1d9738SJakob Bornecrantz 
411fb1d9738SJakob Bornecrantz 	return 0;
412fb1d9738SJakob Bornecrantz }
413fb1d9738SJakob Bornecrantz 
414dcb52271SThomas Hellstrom 
vmw_overlay_available(const struct vmw_private * dev_priv)415dcb52271SThomas Hellstrom static bool vmw_overlay_available(const struct vmw_private *dev_priv)
416dcb52271SThomas Hellstrom {
417dcb52271SThomas Hellstrom 	return (dev_priv->overlay_priv != NULL &&
4182cd80dbdSZack Rusin 		((vmw_fifo_caps(dev_priv) & VMW_OVERLAY_CAP_MASK) ==
419dcb52271SThomas Hellstrom 		 VMW_OVERLAY_CAP_MASK));
420dcb52271SThomas Hellstrom }
421dcb52271SThomas Hellstrom 
vmw_overlay_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)422fb1d9738SJakob Bornecrantz int vmw_overlay_ioctl(struct drm_device *dev, void *data,
423fb1d9738SJakob Bornecrantz 		      struct drm_file *file_priv)
424fb1d9738SJakob Bornecrantz {
425fb1d9738SJakob Bornecrantz 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
426fb1d9738SJakob Bornecrantz 	struct vmw_private *dev_priv = vmw_priv(dev);
427fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
428fb1d9738SJakob Bornecrantz 	struct drm_vmw_control_stream_arg *arg =
429fb1d9738SJakob Bornecrantz 	    (struct drm_vmw_control_stream_arg *)data;
43009881d29SZack Rusin 	struct vmw_bo *buf;
431fb1d9738SJakob Bornecrantz 	struct vmw_resource *res;
432fb1d9738SJakob Bornecrantz 	int ret;
433fb1d9738SJakob Bornecrantz 
434dcb52271SThomas Hellstrom 	if (!vmw_overlay_available(dev_priv))
435fb1d9738SJakob Bornecrantz 		return -ENOSYS;
436fb1d9738SJakob Bornecrantz 
437fb1d9738SJakob Bornecrantz 	ret = vmw_user_stream_lookup(dev_priv, tfile, &arg->stream_id, &res);
438fb1d9738SJakob Bornecrantz 	if (ret)
439fb1d9738SJakob Bornecrantz 		return ret;
440fb1d9738SJakob Bornecrantz 
441fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
442fb1d9738SJakob Bornecrantz 
443fb1d9738SJakob Bornecrantz 	if (!arg->enabled) {
444fb1d9738SJakob Bornecrantz 		ret = vmw_overlay_stop(dev_priv, arg->stream_id, false, true);
445fb1d9738SJakob Bornecrantz 		goto out_unlock;
446fb1d9738SJakob Bornecrantz 	}
447fb1d9738SJakob Bornecrantz 
4488afa13a0SZack Rusin 	ret = vmw_user_bo_lookup(file_priv, arg->handle, &buf);
449fb1d9738SJakob Bornecrantz 	if (ret)
450fb1d9738SJakob Bornecrantz 		goto out_unlock;
451fb1d9738SJakob Bornecrantz 
452fb1d9738SJakob Bornecrantz 	ret = vmw_overlay_update_stream(dev_priv, buf, arg, true);
453fb1d9738SJakob Bornecrantz 
45491398b41SZack Rusin 	vmw_user_bo_unref(&buf);
455fb1d9738SJakob Bornecrantz 
456fb1d9738SJakob Bornecrantz out_unlock:
457fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
458fb1d9738SJakob Bornecrantz 	vmw_resource_unreference(&res);
459fb1d9738SJakob Bornecrantz 
460fb1d9738SJakob Bornecrantz 	return ret;
461fb1d9738SJakob Bornecrantz }
462fb1d9738SJakob Bornecrantz 
vmw_overlay_num_overlays(struct vmw_private * dev_priv)463fb1d9738SJakob Bornecrantz int vmw_overlay_num_overlays(struct vmw_private *dev_priv)
464fb1d9738SJakob Bornecrantz {
465dcb52271SThomas Hellstrom 	if (!vmw_overlay_available(dev_priv))
466fb1d9738SJakob Bornecrantz 		return 0;
467fb1d9738SJakob Bornecrantz 
468fb1d9738SJakob Bornecrantz 	return VMW_MAX_NUM_STREAMS;
469fb1d9738SJakob Bornecrantz }
470fb1d9738SJakob Bornecrantz 
vmw_overlay_num_free_overlays(struct vmw_private * dev_priv)471fb1d9738SJakob Bornecrantz int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv)
472fb1d9738SJakob Bornecrantz {
473fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
474fb1d9738SJakob Bornecrantz 	int i, k;
475fb1d9738SJakob Bornecrantz 
476dcb52271SThomas Hellstrom 	if (!vmw_overlay_available(dev_priv))
477fb1d9738SJakob Bornecrantz 		return 0;
478fb1d9738SJakob Bornecrantz 
479fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
480fb1d9738SJakob Bornecrantz 
481fb1d9738SJakob Bornecrantz 	for (i = 0, k = 0; i < VMW_MAX_NUM_STREAMS; i++)
482fb1d9738SJakob Bornecrantz 		if (!overlay->stream[i].claimed)
483fb1d9738SJakob Bornecrantz 			k++;
484fb1d9738SJakob Bornecrantz 
485fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
486fb1d9738SJakob Bornecrantz 
487fb1d9738SJakob Bornecrantz 	return k;
488fb1d9738SJakob Bornecrantz }
489fb1d9738SJakob Bornecrantz 
vmw_overlay_claim(struct vmw_private * dev_priv,uint32_t * out)490fb1d9738SJakob Bornecrantz int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out)
491fb1d9738SJakob Bornecrantz {
492fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
493fb1d9738SJakob Bornecrantz 	int i;
494fb1d9738SJakob Bornecrantz 
495fb1d9738SJakob Bornecrantz 	if (!overlay)
496fb1d9738SJakob Bornecrantz 		return -ENOSYS;
497fb1d9738SJakob Bornecrantz 
498fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
499fb1d9738SJakob Bornecrantz 
500fb1d9738SJakob Bornecrantz 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
501fb1d9738SJakob Bornecrantz 
502fb1d9738SJakob Bornecrantz 		if (overlay->stream[i].claimed)
503fb1d9738SJakob Bornecrantz 			continue;
504fb1d9738SJakob Bornecrantz 
505fb1d9738SJakob Bornecrantz 		overlay->stream[i].claimed = true;
506fb1d9738SJakob Bornecrantz 		*out = i;
507fb1d9738SJakob Bornecrantz 		mutex_unlock(&overlay->mutex);
508fb1d9738SJakob Bornecrantz 		return 0;
509fb1d9738SJakob Bornecrantz 	}
510fb1d9738SJakob Bornecrantz 
511fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
512fb1d9738SJakob Bornecrantz 	return -ESRCH;
513fb1d9738SJakob Bornecrantz }
514fb1d9738SJakob Bornecrantz 
vmw_overlay_unref(struct vmw_private * dev_priv,uint32_t stream_id)515fb1d9738SJakob Bornecrantz int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id)
516fb1d9738SJakob Bornecrantz {
517fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
518fb1d9738SJakob Bornecrantz 
519fb1d9738SJakob Bornecrantz 	BUG_ON(stream_id >= VMW_MAX_NUM_STREAMS);
520fb1d9738SJakob Bornecrantz 
521fb1d9738SJakob Bornecrantz 	if (!overlay)
522fb1d9738SJakob Bornecrantz 		return -ENOSYS;
523fb1d9738SJakob Bornecrantz 
524fb1d9738SJakob Bornecrantz 	mutex_lock(&overlay->mutex);
525fb1d9738SJakob Bornecrantz 
526fb1d9738SJakob Bornecrantz 	WARN_ON(!overlay->stream[stream_id].claimed);
527fb1d9738SJakob Bornecrantz 	vmw_overlay_stop(dev_priv, stream_id, false, false);
528fb1d9738SJakob Bornecrantz 	overlay->stream[stream_id].claimed = false;
529fb1d9738SJakob Bornecrantz 
530fb1d9738SJakob Bornecrantz 	mutex_unlock(&overlay->mutex);
531fb1d9738SJakob Bornecrantz 	return 0;
532fb1d9738SJakob Bornecrantz }
533fb1d9738SJakob Bornecrantz 
vmw_overlay_init(struct vmw_private * dev_priv)534fb1d9738SJakob Bornecrantz int vmw_overlay_init(struct vmw_private *dev_priv)
535fb1d9738SJakob Bornecrantz {
536fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay;
537fb1d9738SJakob Bornecrantz 	int i;
538fb1d9738SJakob Bornecrantz 
539fb1d9738SJakob Bornecrantz 	if (dev_priv->overlay_priv)
540fb1d9738SJakob Bornecrantz 		return -EINVAL;
541fb1d9738SJakob Bornecrantz 
542f35119d6SRakib Mullick 	overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
543fb1d9738SJakob Bornecrantz 	if (!overlay)
544fb1d9738SJakob Bornecrantz 		return -ENOMEM;
545fb1d9738SJakob Bornecrantz 
546fb1d9738SJakob Bornecrantz 	mutex_init(&overlay->mutex);
547fb1d9738SJakob Bornecrantz 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
548fb1d9738SJakob Bornecrantz 		overlay->stream[i].buf = NULL;
549fb1d9738SJakob Bornecrantz 		overlay->stream[i].paused = false;
550fb1d9738SJakob Bornecrantz 		overlay->stream[i].claimed = false;
551fb1d9738SJakob Bornecrantz 	}
552fb1d9738SJakob Bornecrantz 
553fb1d9738SJakob Bornecrantz 	dev_priv->overlay_priv = overlay;
554fb1d9738SJakob Bornecrantz 
555fb1d9738SJakob Bornecrantz 	return 0;
556fb1d9738SJakob Bornecrantz }
557fb1d9738SJakob Bornecrantz 
vmw_overlay_close(struct vmw_private * dev_priv)558fb1d9738SJakob Bornecrantz int vmw_overlay_close(struct vmw_private *dev_priv)
559fb1d9738SJakob Bornecrantz {
560fb1d9738SJakob Bornecrantz 	struct vmw_overlay *overlay = dev_priv->overlay_priv;
561fb1d9738SJakob Bornecrantz 	bool forgotten_buffer = false;
562fb1d9738SJakob Bornecrantz 	int i;
563fb1d9738SJakob Bornecrantz 
564fb1d9738SJakob Bornecrantz 	if (!overlay)
565fb1d9738SJakob Bornecrantz 		return -ENOSYS;
566fb1d9738SJakob Bornecrantz 
567fb1d9738SJakob Bornecrantz 	for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
568fb1d9738SJakob Bornecrantz 		if (overlay->stream[i].buf) {
569fb1d9738SJakob Bornecrantz 			forgotten_buffer = true;
570fb1d9738SJakob Bornecrantz 			vmw_overlay_stop(dev_priv, i, false, false);
571fb1d9738SJakob Bornecrantz 		}
572fb1d9738SJakob Bornecrantz 	}
573fb1d9738SJakob Bornecrantz 
574fb1d9738SJakob Bornecrantz 	WARN_ON(forgotten_buffer);
575fb1d9738SJakob Bornecrantz 
576fb1d9738SJakob Bornecrantz 	dev_priv->overlay_priv = NULL;
577fb1d9738SJakob Bornecrantz 	kfree(overlay);
578fb1d9738SJakob Bornecrantz 
579fb1d9738SJakob Bornecrantz 	return 0;
580fb1d9738SJakob Bornecrantz }
581