// SPDX-License-Identifier: GPL-2.0 OR MIT
/**************************************************************************
*
* Copyright (c) 2024 Broadcom. All Rights Reserved. The term
* “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
#include "vmwgfx_vkms.h"
#include "vmwgfx_bo.h"
#include "vmwgfx_drv.h"
#include "vmwgfx_kms.h"
#include "vmw_surface_cache.h"
#include <drm/drm_crtc.h>
#include <drm/drm_debugfs_crc.h>
#include <drm/drm_print.h>
#include <drm/drm_vblank.h>
#include <linux/crc32.h>
#include <linux/delay.h>
#define GUESTINFO_VBLANK "guestinfo.vmwgfx.vkms_enable"
static int
vmw_surface_sync(struct vmw_private *vmw,
struct vmw_surface *surf)
{
int ret;
struct vmw_fence_obj *fence = NULL;
struct vmw_bo *bo = surf->res.guest_memory_bo;
vmw_resource_clean(&surf->res);
ret = ttm_bo_reserve(&bo->tbo, false, false, NULL);
if (ret != 0) {
drm_warn(&vmw->drm, "%s: failed reserve\n", __func__);
goto done;
}
ret = vmw_execbuf_fence_commands(NULL, vmw, &fence, NULL);
if (ret != 0) {
drm_warn(&vmw->drm, "%s: failed execbuf\n", __func__);
ttm_bo_unreserve(&bo->tbo);
goto done;
}
dma_fence_wait(&fence->base, false);
dma_fence_put(&fence->base);
ttm_bo_unreserve(&bo->tbo);
done:
return ret;
}
static void
compute_crc(struct drm_crtc *crtc,
struct vmw_surface *surf,
u32 *crc)
{
u8 *mapped_surface;
struct vmw_bo *bo = surf->res.guest_memory_bo;
const struct SVGA3dSurfaceDesc *desc =
vmw_surface_get_desc(surf->metadata.format);
u32 row_pitch_bytes;
SVGA3dSize blocks;
u32 y;
*crc = 0;
vmw_surface_get_size_in_blocks(desc, &surf->metadata.base_size, &blocks);
row_pitch_bytes = blocks.width * desc->pitchBytesPerBlock;
WARN_ON(!bo);
mapped_surface = vmw_bo_map_and_cache(bo);
for (y = 0; y < blocks.height; y++) {
*crc = crc32_le(*crc, mapped_surface, row_pitch_bytes);
mapped_surface += row_pitch_bytes;
}
vmw_bo_unmap(bo);
}
static void
crc_generate_worker(struct work_struct *work)
{
struct vmw_display_unit *du =
container_of(work, struct vmw_display_unit, vkms.crc_generator_work);
struct drm_crtc *crtc = &du->crtc;
struct vmw_private *vmw = vmw_priv(crtc->dev);
bool crc_pending;
u64 frame_start, frame_end;
u32 crc32 = 0;
struct vmw_surface *surf = 0;
spin_lock_irq(&du->vkms.crc_state_lock);
crc_pending = du->vkms.crc_pending;
spin_unlock_irq(&du->vkms.crc_state_lock);
/*
* We raced with the vblank hrtimer and previous work already computed
* the crc, nothing to do.
*/
if (!crc_pending)
return;
spin_lock_irq(&du->vkms.crc_state_lock);
surf = vmw_surface_reference(du->vkms.surface);
spin_unlock_irq(&du->vkms.crc_state_lock);
if (surf) {
if (vmw_surface_sync(vmw, surf)) {
drm_warn(
crtc->dev,
"CRC worker wasn't able to sync the crc surface!\n");
return;
}
compute_crc(crtc, surf, &crc32);
vmw_surface_unreference(&surf);
}
spin_lock_irq(&du->vkms.crc_state_lock);
frame_start = du->vkms.frame_start;
frame_end = du->vkms.frame_end;
du->vkms.frame_start = 0;
du->vkms.frame_end = 0;
du->vkms.crc_pending = false;
spin_unlock_irq(&du->vkms.crc_state_lock);
/*
* The worker can fall behind the vblank hrtimer, make sure we catch up.
*/
while (frame_start <= frame_end)
drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
}
static enum hrtimer_restart
vmw_vkms_vblank_simulate(struct hrtimer *timer)
{
struct vmw_display_unit *du = container_of(timer, struct vmw_display_unit, vkms.timer);
struct drm_crtc *crtc = &du->crtc;
struct vmw_private *vmw = vmw_priv(crtc->dev);
bool has_surface = false;
u64 ret_overrun;
bool locked, ret;
ret_overrun = hrtimer_forward_now(&du->vkms.timer,
du->vkms.period_ns);
if (ret_overrun != 1)
drm_dbg_driver(crtc->dev, "vblank timer missed %lld frames.\n",
ret_overrun - 1);
locked = vmw_vkms_vblank_trylock(crtc);
ret = drm_crtc_handle_vblank(crtc);
WARN_ON(!ret);
if (!locked)
return HRTIMER_RESTART;
has_surface = du->vkms.surface != NULL;
vmw_vkms_unlock(crtc);
if (du->vkms.crc_enabled && has_surface) {
u64 frame = drm_crtc_accurate_vblank_count(crtc);
spin_lock(&du->vkms.crc_state_lock);
if (!du->vkms.crc_pending)
du->vkms.frame_start = frame;
else
drm_dbg_driver(crtc->dev,
"crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
du->vkms.frame_start, frame);
du->vkms.frame_end = frame;
du->vkms.crc_pending = true;
spin_unlock(&du->vkms.crc_state_lock);
ret = queue_work(vmw->crc_workq, &du->vkms.crc_generator_work);
if (!ret)
drm_dbg_driver(crtc->dev, "Composer worker already queued\n");
}
return HRTIMER_RESTART;
}
void
vmw_vkms_init(struct vmw_private *vmw)
{
char buffer[64];
const size_t max_buf_len = sizeof(buffer) - 1;
size_t buf_len = max_buf_len;
int ret;
vmw->vkms_enabled = false;
ret = vmw_host_get_guestinfo(GUESTINFO_VBLANK, buffer, &buf_len);
if (ret || buf_len > max_buf_len)
return;
buffer[buf_len] = '\0';
ret = kstrtobool(buffer, &vmw->vkms_enabled);
if (!ret && vmw->vkms_enabled) {
ret = drm_vblank_init(&vmw->drm, VMWGFX_NUM_DISPLAY_UNITS);
vmw->vkms_enabled = (ret == 0);
}
vmw->crc_workq = alloc_ordered_workqueue("vmwgfx_crc_generator", 0);
if (!vmw->crc_workq) {
drm_warn(&vmw->drm, "crc workqueue allocation failed. Disabling vkms.");
vmw->vkms_enabled = false;
}
if (vmw->vkms_enabled)
drm_info(&vmw->drm, "VKMS enabled\n");
}
void
vmw_vkms_cleanup(struct vmw_private *vmw)
{
destroy_workqueue(vmw->crc_workq);
}
bool
vmw_vkms_get_vblank_timestamp(struct drm_crtc *crtc,
int *max_error,
ktime_t *vblank_time,
bool in_vblank_irq)
{
struct drm_device *dev = crtc->dev;
struct vmw_private *vmw = vmw_priv(dev);
unsigned int pipe = crtc->index;
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
if (!vmw->vkms_enabled)
return false;
if (!READ_ONCE(vblank->enabled)) {
*vblank_time = ktime_get();
return true;
}
*vblank_time = READ_ONCE(du->vkms.timer.node.expires);
if (WARN_ON(*vblank_time == vblank->time))
return true;
/*
* To prevent races we roll the hrtimer forward before we do any
* interrupt processing - this is how real hw works (the interrupt is
* only generated after all the vblank registers are updated) and what
* the vblank core expects. Therefore we need to always correct the
* timestampe by one frame.
*/
*vblank_time -= du->vkms.period_ns;
return true;
}
int
vmw_vkms_enable_vblank(struct drm_crtc *crtc)
{
struct drm_device *dev = crtc->dev;
struct vmw_private *vmw = vmw_priv(dev);
unsigned int pipe = drm_crtc_index(crtc);
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
if (!vmw->vkms_enabled)
return -EINVAL;
drm_calc_timestamping_constants(crtc, &crtc->mode);
hrtimer_init(&du->vkms.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
du->vkms.timer.function = &vmw_vkms_vblank_simulate;
du->vkms.period_ns = ktime_set(0, vblank->framedur_ns);
hrtimer_start(&du->vkms.timer, du->vkms.period_ns, HRTIMER_MODE_REL);
return 0;
}
void
vmw_vkms_disable_vblank(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (!vmw->vkms_enabled)
return;
hrtimer_cancel(&du->vkms.timer);
du->vkms.surface = NULL;
du->vkms.period_ns = ktime_set(0, 0);
}
enum vmw_vkms_lock_state {
VMW_VKMS_LOCK_UNLOCKED = 0,
VMW_VKMS_LOCK_MODESET = 1,
VMW_VKMS_LOCK_VBLANK = 2
};
void
vmw_vkms_crtc_init(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
spin_lock_init(&du->vkms.crc_state_lock);
INIT_WORK(&du->vkms.crc_generator_work, crc_generate_worker);
du->vkms.surface = NULL;
}
void
vmw_vkms_crtc_cleanup(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
if (du->vkms.surface)
vmw_surface_unreference(&du->vkms.surface);
WARN_ON(work_pending(&du->vkms.crc_generator_work));
hrtimer_cancel(&du->vkms.timer);
}
void
vmw_vkms_crtc_atomic_begin(struct drm_crtc *crtc,
struct drm_atomic_state *state)
{
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (vmw->vkms_enabled)
vmw_vkms_modeset_lock(crtc);
}
void
vmw_vkms_crtc_atomic_flush(struct drm_crtc *crtc,
struct drm_atomic_state *state)
{
unsigned long flags;
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (!vmw->vkms_enabled)
return;
if (crtc->state->event) {
spin_lock_irqsave(&crtc->dev->event_lock, flags);
if (drm_crtc_vblank_get(crtc) != 0)
drm_crtc_send_vblank_event(crtc, crtc->state->event);
else
drm_crtc_arm_vblank_event(crtc, crtc->state->event);
spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
crtc->state->event = NULL;
}
vmw_vkms_unlock(crtc);
}
void
vmw_vkms_crtc_atomic_enable(struct drm_crtc *crtc,
struct drm_atomic_state *state)
{
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (vmw->vkms_enabled)
drm_crtc_vblank_on(crtc);
}
void
vmw_vkms_crtc_atomic_disable(struct drm_crtc *crtc,
struct drm_atomic_state *state)
{
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (vmw->vkms_enabled)
drm_crtc_vblank_off(crtc);
}
static bool
is_crc_supported(struct drm_crtc *crtc)
{
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (!vmw->vkms_enabled)
return false;
if (vmw->active_display_unit != vmw_du_screen_target)
return false;
return true;
}
static const char * const pipe_crc_sources[] = {"auto"};
static int
crc_parse_source(const char *src_name,
bool *enabled)
{
int ret = 0;
if (!src_name) {
*enabled = false;
} else if (strcmp(src_name, "auto") == 0) {
*enabled = true;
} else {
*enabled = false;
ret = -EINVAL;
}
return ret;
}
const char *const *
vmw_vkms_get_crc_sources(struct drm_crtc *crtc,
size_t *count)
{
*count = 0;
if (!is_crc_supported(crtc))
return NULL;
*count = ARRAY_SIZE(pipe_crc_sources);
return pipe_crc_sources;
}
int
vmw_vkms_verify_crc_source(struct drm_crtc *crtc,
const char *src_name,
size_t *values_cnt)
{
bool enabled;
if (!is_crc_supported(crtc))
return -EINVAL;
if (crc_parse_source(src_name, &enabled) < 0) {
drm_dbg_driver(crtc->dev, "unknown source '%s'\n", src_name);
return -EINVAL;
}
*values_cnt = 1;
return 0;
}
int
vmw_vkms_set_crc_source(struct drm_crtc *crtc,
const char *src_name)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
bool enabled, prev_enabled, locked;
int ret;
if (!is_crc_supported(crtc))
return -EINVAL;
ret = crc_parse_source(src_name, &enabled);
if (enabled)
drm_crtc_vblank_get(crtc);
locked = vmw_vkms_modeset_lock_relaxed(crtc);
prev_enabled = du->vkms.crc_enabled;
du->vkms.crc_enabled = enabled;
if (locked)
vmw_vkms_unlock(crtc);
if (prev_enabled)
drm_crtc_vblank_put(crtc);
return ret;
}
void
vmw_vkms_set_crc_surface(struct drm_crtc *crtc,
struct vmw_surface *surf)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
struct vmw_private *vmw = vmw_priv(crtc->dev);
if (vmw->vkms_enabled && du->vkms.surface != surf) {
WARN_ON(atomic_read(&du->vkms.atomic_lock) != VMW_VKMS_LOCK_MODESET);
if (du->vkms.surface)
vmw_surface_unreference(&du->vkms.surface);
if (surf)
du->vkms.surface = vmw_surface_reference(surf);
}
}
/**
* vmw_vkms_lock_max_wait_ns - Return the max wait for the vkms lock
* @du: The vmw_display_unit from which to grab the vblank timings
*
* Returns the maximum wait time used to acquire the vkms lock. By
* default uses a time of a single frame and in case where vblank
* was not initialized for the display unit 1/60th of a second.
*/
static inline u64
vmw_vkms_lock_max_wait_ns(struct vmw_display_unit *du)
{
s64 nsecs = ktime_to_ns(du->vkms.period_ns);
return (nsecs > 0) ? nsecs : 16666666;
}
/**
* vmw_vkms_modeset_lock - Protects access to crtc during modeset
* @crtc: The crtc to lock for vkms
*
* This function prevents the VKMS timers/callbacks from being called
* while a modeset operation is in process. We don't want the callbacks
* e.g. the vblank simulator to be trying to access incomplete state
* so we need to make sure they execute only when the modeset has
* finished.
*
* Normally this would have been done with a spinlock but locking the
* entire atomic modeset with vmwgfx is impossible because kms prepare
* executes non-atomic ops (e.g. vmw_validation_prepare holds a mutex to
* guard various bits of state). Which means that we need to synchronize
* atomic context (the vblank handler) with the non-atomic entirity
* of kms - so use an atomic_t to track which part of vkms has access
* to the basic vkms state.
*/
void
vmw_vkms_modeset_lock(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
const u64 nsecs_delay = 10;
const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
u64 total_delay = 0;
int ret;
do {
ret = atomic_cmpxchg(&du->vkms.atomic_lock,
VMW_VKMS_LOCK_UNLOCKED,
VMW_VKMS_LOCK_MODESET);
if (ret == VMW_VKMS_LOCK_UNLOCKED || total_delay >= MAX_NSECS_DELAY)
break;
ndelay(nsecs_delay);
total_delay += nsecs_delay;
} while (1);
if (total_delay >= MAX_NSECS_DELAY) {
drm_warn(crtc->dev, "VKMS lock expired! total_delay = %lld, ret = %d, cur = %d\n",
total_delay, ret, atomic_read(&du->vkms.atomic_lock));
}
}
/**
* vmw_vkms_modeset_lock_relaxed - Protects access to crtc during modeset
* @crtc: The crtc to lock for vkms
*
* Much like vmw_vkms_modeset_lock except that when the crtc is currently
* in a modeset it will return immediately.
*
* Returns true if actually locked vkms to modeset or false otherwise.
*/
bool
vmw_vkms_modeset_lock_relaxed(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
const u64 nsecs_delay = 10;
const u64 MAX_NSECS_DELAY = vmw_vkms_lock_max_wait_ns(du);
u64 total_delay = 0;
int ret;
do {
ret = atomic_cmpxchg(&du->vkms.atomic_lock,
VMW_VKMS_LOCK_UNLOCKED,
VMW_VKMS_LOCK_MODESET);
if (ret == VMW_VKMS_LOCK_UNLOCKED ||
ret == VMW_VKMS_LOCK_MODESET ||
total_delay >= MAX_NSECS_DELAY)
break;
ndelay(nsecs_delay);
total_delay += nsecs_delay;
} while (1);
if (total_delay >= MAX_NSECS_DELAY) {
drm_warn(crtc->dev, "VKMS relaxed lock expired!\n");
return false;
}
return ret == VMW_VKMS_LOCK_UNLOCKED;
}
/**
* vmw_vkms_vblank_trylock - Protects access to crtc during vblank
* @crtc: The crtc to lock for vkms
*
* Tries to lock vkms for vblank, returns immediately.
*
* Returns true if locked vkms to vblank or false otherwise.
*/
bool
vmw_vkms_vblank_trylock(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
u32 ret;
ret = atomic_cmpxchg(&du->vkms.atomic_lock,
VMW_VKMS_LOCK_UNLOCKED,
VMW_VKMS_LOCK_VBLANK);
return ret == VMW_VKMS_LOCK_UNLOCKED;
}
void
vmw_vkms_unlock(struct drm_crtc *crtc)
{
struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
/* Release flag; mark it as unlocked. */
atomic_set(&du->vkms.atomic_lock, VMW_VKMS_LOCK_UNLOCKED);
}