1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
27 #include "vmwgfx_bo.h"
28 #include "vmwgfx_drv.h"
30 #include "device_include/svga_overlay.h"
31 #include "device_include/svga_escape.h"
33 #include <drm/ttm/ttm_placement.h>
35 #define VMW_MAX_NUM_STREAMS 1
36 #define VMW_OVERLAY_CAP_MASK (SVGA_FIFO_CAP_VIDEO | SVGA_FIFO_CAP_ESCAPE)
42 struct drm_vmw_control_stream_arg saved;
50 * Each stream is a single overlay. In Xv these are called ports.
53 struct vmw_stream stream[VMW_MAX_NUM_STREAMS];
56 struct vmw_escape_header {
58 SVGAFifoCmdEscape body;
61 struct vmw_escape_video_flush {
62 struct vmw_escape_header escape;
63 SVGAEscapeVideoFlush flush;
66 static inline void fill_escape(struct vmw_escape_header *header,
69 header->cmd = SVGA_CMD_ESCAPE;
70 header->body.nsid = SVGA_ESCAPE_NSID_VMWARE;
71 header->body.size = size;
74 static inline void fill_flush(struct vmw_escape_video_flush *cmd,
77 fill_escape(&cmd->escape, sizeof(cmd->flush));
78 cmd->flush.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_FLUSH;
79 cmd->flush.streamId = stream_id;
83 * Send put command to hw.
86 * -ERESTARTSYS if interrupted by a signal.
88 static int vmw_overlay_send_put(struct vmw_private *dev_priv,
90 struct drm_vmw_control_stream_arg *arg,
93 struct vmw_escape_video_flush *flush;
95 bool have_so = (dev_priv->active_display_unit == vmw_du_screen_object);
100 struct vmw_escape_header escape;
111 /* defines are a index needs + 1 */
113 num_items = SVGA_VIDEO_DST_SCREEN_ID + 1;
115 num_items = SVGA_VIDEO_PITCH_3 + 1;
117 fifo_size = sizeof(*cmds) + sizeof(*flush) + sizeof(*items) * num_items;
119 cmds = VMW_CMD_RESERVE(dev_priv, fifo_size);
120 /* hardware has hung, can't do anything here */
124 items = (typeof(items))&cmds[1];
125 flush = (struct vmw_escape_video_flush *)&items[num_items];
127 /* the size is header + number of items */
128 fill_escape(&cmds->escape, sizeof(*items) * (num_items + 1));
130 cmds->header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
131 cmds->header.streamId = arg->stream_id;
133 /* the IDs are neatly numbered */
134 for (i = 0; i < num_items; i++)
135 items[i].registerId = i;
137 vmw_bo_get_guest_ptr(&buf->tbo, &ptr);
138 ptr.offset += arg->offset;
140 items[SVGA_VIDEO_ENABLED].value = true;
141 items[SVGA_VIDEO_FLAGS].value = arg->flags;
142 items[SVGA_VIDEO_DATA_OFFSET].value = ptr.offset;
143 items[SVGA_VIDEO_FORMAT].value = arg->format;
144 items[SVGA_VIDEO_COLORKEY].value = arg->color_key;
145 items[SVGA_VIDEO_SIZE].value = arg->size;
146 items[SVGA_VIDEO_WIDTH].value = arg->width;
147 items[SVGA_VIDEO_HEIGHT].value = arg->height;
148 items[SVGA_VIDEO_SRC_X].value = arg->src.x;
149 items[SVGA_VIDEO_SRC_Y].value = arg->src.y;
150 items[SVGA_VIDEO_SRC_WIDTH].value = arg->src.w;
151 items[SVGA_VIDEO_SRC_HEIGHT].value = arg->src.h;
152 items[SVGA_VIDEO_DST_X].value = arg->dst.x;
153 items[SVGA_VIDEO_DST_Y].value = arg->dst.y;
154 items[SVGA_VIDEO_DST_WIDTH].value = arg->dst.w;
155 items[SVGA_VIDEO_DST_HEIGHT].value = arg->dst.h;
156 items[SVGA_VIDEO_PITCH_1].value = arg->pitch[0];
157 items[SVGA_VIDEO_PITCH_2].value = arg->pitch[1];
158 items[SVGA_VIDEO_PITCH_3].value = arg->pitch[2];
160 items[SVGA_VIDEO_DATA_GMRID].value = ptr.gmrId;
161 items[SVGA_VIDEO_DST_SCREEN_ID].value = SVGA_ID_INVALID;
164 fill_flush(flush, arg->stream_id);
166 vmw_cmd_commit(dev_priv, fifo_size);
172 * Send stop command to hw.
175 * -ERESTARTSYS if interrupted by a signal.
177 static int vmw_overlay_send_stop(struct vmw_private *dev_priv,
182 struct vmw_escape_header escape;
183 SVGAEscapeVideoSetRegs body;
184 struct vmw_escape_video_flush flush;
189 cmds = VMW_CMD_RESERVE(dev_priv, sizeof(*cmds));
193 ret = vmw_fallback_wait(dev_priv, false, true, 0,
194 interruptible, 3*HZ);
195 if (interruptible && ret == -ERESTARTSYS)
201 fill_escape(&cmds->escape, sizeof(cmds->body));
202 cmds->body.header.cmdType = SVGA_ESCAPE_VMWARE_VIDEO_SET_REGS;
203 cmds->body.header.streamId = stream_id;
204 cmds->body.items[0].registerId = SVGA_VIDEO_ENABLED;
205 cmds->body.items[0].value = false;
206 fill_flush(&cmds->flush, stream_id);
208 vmw_cmd_commit(dev_priv, sizeof(*cmds));
214 * Move a buffer to vram or gmr if @pin is set, else unpin the buffer.
216 * With the introduction of screen objects buffers could now be
217 * used with GMRs instead of being locked to vram.
219 static int vmw_overlay_move_buffer(struct vmw_private *dev_priv,
221 bool pin, bool inter)
224 return vmw_bo_unpin(dev_priv, buf, inter);
226 if (dev_priv->active_display_unit == vmw_du_legacy)
227 return vmw_bo_pin_in_vram(dev_priv, buf, inter);
229 return vmw_bo_pin_in_vram_or_gmr(dev_priv, buf, inter);
233 * Stop or pause a stream.
235 * If the stream is paused the no evict flag is removed from the buffer
236 * but left in vram. This allows for instance mode_set to evict it
239 * The caller must hold the overlay lock.
241 * @stream_id which stream to stop/pause.
242 * @pause true to pause, false to stop completely.
244 static int vmw_overlay_stop(struct vmw_private *dev_priv,
245 uint32_t stream_id, bool pause,
248 struct vmw_overlay *overlay = dev_priv->overlay_priv;
249 struct vmw_stream *stream = &overlay->stream[stream_id];
252 /* no buffer attached the stream is completely stopped */
256 /* If the stream is paused this is already done */
257 if (!stream->paused) {
258 ret = vmw_overlay_send_stop(dev_priv, stream_id,
263 /* We just remove the NO_EVICT flag so no -ENOMEM */
264 ret = vmw_overlay_move_buffer(dev_priv, stream->buf, false,
266 if (interruptible && ret == -ERESTARTSYS)
273 vmw_bo_unreference(&stream->buf);
274 stream->paused = false;
276 stream->paused = true;
283 * Update a stream and send any put or stop fifo commands needed.
285 * The caller must hold the overlay lock.
288 * -ENOMEM if buffer doesn't fit in vram.
289 * -ERESTARTSYS if interrupted.
291 static int vmw_overlay_update_stream(struct vmw_private *dev_priv,
293 struct drm_vmw_control_stream_arg *arg,
296 struct vmw_overlay *overlay = dev_priv->overlay_priv;
297 struct vmw_stream *stream = &overlay->stream[arg->stream_id];
303 DRM_DEBUG(" %s: old %p, new %p, %spaused\n", __func__,
304 stream->buf, buf, stream->paused ? "" : "not ");
306 if (stream->buf != buf) {
307 ret = vmw_overlay_stop(dev_priv, arg->stream_id,
308 false, interruptible);
311 } else if (!stream->paused) {
312 /* If the buffers match and not paused then just send
313 * the put command, no need to do anything else.
315 ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
317 stream->saved = *arg;
319 BUG_ON(!interruptible);
324 /* We don't start the old stream if we are interrupted.
325 * Might return -ENOMEM if it can't fit the buffer in vram.
327 ret = vmw_overlay_move_buffer(dev_priv, buf, true, interruptible);
331 ret = vmw_overlay_send_put(dev_priv, buf, arg, interruptible);
333 /* This one needs to happen no matter what. We only remove
334 * the NO_EVICT flag so this is safe from -ENOMEM.
336 BUG_ON(vmw_overlay_move_buffer(dev_priv, buf, false, false)
341 if (stream->buf != buf)
342 stream->buf = vmw_bo_reference(buf);
343 stream->saved = *arg;
344 /* stream is no longer stopped/paused */
345 stream->paused = false;
351 * Try to resume all paused streams.
353 * Used by the kms code after moving a new scanout buffer to vram.
355 * Takes the overlay lock.
357 int vmw_overlay_resume_all(struct vmw_private *dev_priv)
359 struct vmw_overlay *overlay = dev_priv->overlay_priv;
365 mutex_lock(&overlay->mutex);
367 for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
368 struct vmw_stream *stream = &overlay->stream[i];
372 ret = vmw_overlay_update_stream(dev_priv, stream->buf,
373 &stream->saved, false);
375 DRM_INFO("%s: *warning* failed to resume stream %i\n",
379 mutex_unlock(&overlay->mutex);
385 * Pauses all active streams.
387 * Used by the kms code when moving a new scanout buffer to vram.
389 * Takes the overlay lock.
391 int vmw_overlay_pause_all(struct vmw_private *dev_priv)
393 struct vmw_overlay *overlay = dev_priv->overlay_priv;
399 mutex_lock(&overlay->mutex);
401 for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
402 if (overlay->stream[i].paused)
403 DRM_INFO("%s: *warning* stream %i already paused\n",
405 ret = vmw_overlay_stop(dev_priv, i, true, false);
409 mutex_unlock(&overlay->mutex);
415 static bool vmw_overlay_available(const struct vmw_private *dev_priv)
417 return (dev_priv->overlay_priv != NULL &&
418 ((vmw_fifo_caps(dev_priv) & VMW_OVERLAY_CAP_MASK) ==
419 VMW_OVERLAY_CAP_MASK));
422 int vmw_overlay_ioctl(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
425 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
426 struct vmw_private *dev_priv = vmw_priv(dev);
427 struct vmw_overlay *overlay = dev_priv->overlay_priv;
428 struct drm_vmw_control_stream_arg *arg =
429 (struct drm_vmw_control_stream_arg *)data;
431 struct vmw_resource *res;
434 if (!vmw_overlay_available(dev_priv))
437 ret = vmw_user_stream_lookup(dev_priv, tfile, &arg->stream_id, &res);
441 mutex_lock(&overlay->mutex);
444 ret = vmw_overlay_stop(dev_priv, arg->stream_id, false, true);
448 ret = vmw_user_bo_lookup(file_priv, arg->handle, &buf);
452 ret = vmw_overlay_update_stream(dev_priv, buf, arg, true);
454 vmw_user_bo_unref(&buf);
457 mutex_unlock(&overlay->mutex);
458 vmw_resource_unreference(&res);
463 int vmw_overlay_num_overlays(struct vmw_private *dev_priv)
465 if (!vmw_overlay_available(dev_priv))
468 return VMW_MAX_NUM_STREAMS;
471 int vmw_overlay_num_free_overlays(struct vmw_private *dev_priv)
473 struct vmw_overlay *overlay = dev_priv->overlay_priv;
476 if (!vmw_overlay_available(dev_priv))
479 mutex_lock(&overlay->mutex);
481 for (i = 0, k = 0; i < VMW_MAX_NUM_STREAMS; i++)
482 if (!overlay->stream[i].claimed)
485 mutex_unlock(&overlay->mutex);
490 int vmw_overlay_claim(struct vmw_private *dev_priv, uint32_t *out)
492 struct vmw_overlay *overlay = dev_priv->overlay_priv;
498 mutex_lock(&overlay->mutex);
500 for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
502 if (overlay->stream[i].claimed)
505 overlay->stream[i].claimed = true;
507 mutex_unlock(&overlay->mutex);
511 mutex_unlock(&overlay->mutex);
515 int vmw_overlay_unref(struct vmw_private *dev_priv, uint32_t stream_id)
517 struct vmw_overlay *overlay = dev_priv->overlay_priv;
519 BUG_ON(stream_id >= VMW_MAX_NUM_STREAMS);
524 mutex_lock(&overlay->mutex);
526 WARN_ON(!overlay->stream[stream_id].claimed);
527 vmw_overlay_stop(dev_priv, stream_id, false, false);
528 overlay->stream[stream_id].claimed = false;
530 mutex_unlock(&overlay->mutex);
534 int vmw_overlay_init(struct vmw_private *dev_priv)
536 struct vmw_overlay *overlay;
539 if (dev_priv->overlay_priv)
542 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
546 mutex_init(&overlay->mutex);
547 for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
548 overlay->stream[i].buf = NULL;
549 overlay->stream[i].paused = false;
550 overlay->stream[i].claimed = false;
553 dev_priv->overlay_priv = overlay;
558 int vmw_overlay_close(struct vmw_private *dev_priv)
560 struct vmw_overlay *overlay = dev_priv->overlay_priv;
561 bool forgotten_buffer = false;
567 for (i = 0; i < VMW_MAX_NUM_STREAMS; i++) {
568 if (overlay->stream[i].buf) {
569 forgotten_buffer = true;
570 vmw_overlay_stop(dev_priv, i, false, false);
574 WARN_ON(forgotten_buffer);
576 dev_priv->overlay_priv = NULL;