forked from Minki/linux
05ca530268
This patch avoid the warning in vkms_get_vblank_timestamp when vblanks aren't enabled. When running igt test kms_cursor_crc just after vkms module, the warning raised like below. Initial value of vblank time is zero and hrtimer.node.expires is also zero if vblank aren't enabled before. vkms module isn't real hardware but just virtual hardware module. so vkms can't generate a resonable timestamp when hrtimer is off. it's best to grab the current time. [106444.464503] [IGT] kms_cursor_crc: starting subtest pipe-A-cursor-size-change [106444.471475] WARNING: CPU: 0 PID: 10109 at vkms_get_vblank_timestamp+0x42/0x50 [vkms] [106444.471511] CPU: 0 PID: 10109 Comm: kms_cursor_crc Tainted: G W OE 5.9.0-rc1+ #6 [106444.471514] RIP: 0010:vkms_get_vblank_timestamp+0x42/0x50 [vkms] [106444.471528] Call Trace: [106444.471551] drm_get_last_vbltimestamp+0xb9/0xd0 [drm] [106444.471566] drm_reset_vblank_timestamp+0x63/0xe0 [drm] [106444.471579] drm_crtc_vblank_on+0x85/0x150 [drm] [106444.471582] vkms_crtc_atomic_enable+0xe/0x10 [vkms] [106444.471592] drm_atomic_helper_commit_modeset_enables+0x1db/0x230 [drm_kms_helper] [106444.471594] vkms_atomic_commit_tail+0x38/0xc0 [vkms] [106444.471601] commit_tail+0x97/0x130 [drm_kms_helper] [106444.471608] drm_atomic_helper_commit+0x117/0x140 [drm_kms_helper] [106444.471622] drm_atomic_commit+0x4a/0x50 [drm] [106444.471629] drm_atomic_helper_set_config+0x63/0xb0 [drm_kms_helper] [106444.471642] drm_mode_setcrtc+0x1d9/0x7b0 [drm] [106444.471654] ? drm_mode_getcrtc+0x1a0/0x1a0 [drm] [106444.471666] drm_ioctl_kernel+0xb6/0x100 [drm] [106444.471677] drm_ioctl+0x3ad/0x470 [drm] [106444.471688] ? drm_mode_getcrtc+0x1a0/0x1a0 [drm] [106444.471692] ? tomoyo_file_ioctl+0x19/0x20 [106444.471694] __x64_sys_ioctl+0x96/0xd0 [106444.471697] do_syscall_64+0x37/0x80 [106444.471699] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Cc: Daniel Vetter <daniel@ffwll.ch> Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com> Cc: Haneen Mohammed <hamohammed.sa@gmail.com> Cc: Melissa Wen <melissa.srw@gmail.com> Signed-off-by: Sidong Yang <realwakka@gmail.com> Reviewed-by: Melissa Wen <melissa.srw@gmail.com> Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com> Link: https://patchwork.freedesktop.org/patch/msgid/20200828124553.2178-1-realwakka@gmail.com
294 lines
7.9 KiB
C
294 lines
7.9 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
#include <drm/drm_atomic.h>
|
|
#include <drm/drm_atomic_helper.h>
|
|
#include <drm/drm_probe_helper.h>
|
|
#include <drm/drm_vblank.h>
|
|
|
|
#include "vkms_drv.h"
|
|
|
|
static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
|
|
{
|
|
struct vkms_output *output = container_of(timer, struct vkms_output,
|
|
vblank_hrtimer);
|
|
struct drm_crtc *crtc = &output->crtc;
|
|
struct vkms_crtc_state *state;
|
|
u64 ret_overrun;
|
|
bool ret;
|
|
|
|
ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
|
|
output->period_ns);
|
|
WARN_ON(ret_overrun != 1);
|
|
|
|
spin_lock(&output->lock);
|
|
ret = drm_crtc_handle_vblank(crtc);
|
|
if (!ret)
|
|
DRM_ERROR("vkms failure on handling vblank");
|
|
|
|
state = output->composer_state;
|
|
spin_unlock(&output->lock);
|
|
|
|
if (state && output->composer_enabled) {
|
|
u64 frame = drm_crtc_accurate_vblank_count(crtc);
|
|
|
|
/* update frame_start only if a queued vkms_composer_worker()
|
|
* has read the data
|
|
*/
|
|
spin_lock(&output->composer_lock);
|
|
if (!state->crc_pending)
|
|
state->frame_start = frame;
|
|
else
|
|
DRM_DEBUG_DRIVER("crc worker falling behind, frame_start: %llu, frame_end: %llu\n",
|
|
state->frame_start, frame);
|
|
state->frame_end = frame;
|
|
state->crc_pending = true;
|
|
spin_unlock(&output->composer_lock);
|
|
|
|
ret = queue_work(output->composer_workq, &state->composer_work);
|
|
if (!ret)
|
|
DRM_DEBUG_DRIVER("Composer worker already queued\n");
|
|
}
|
|
|
|
return HRTIMER_RESTART;
|
|
}
|
|
|
|
static int vkms_enable_vblank(struct drm_crtc *crtc)
|
|
{
|
|
struct drm_device *dev = crtc->dev;
|
|
unsigned int pipe = drm_crtc_index(crtc);
|
|
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
|
|
struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
|
|
|
|
drm_calc_timestamping_constants(crtc, &crtc->mode);
|
|
|
|
hrtimer_init(&out->vblank_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
|
|
out->vblank_hrtimer.function = &vkms_vblank_simulate;
|
|
out->period_ns = ktime_set(0, vblank->framedur_ns);
|
|
hrtimer_start(&out->vblank_hrtimer, out->period_ns, HRTIMER_MODE_REL);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void vkms_disable_vblank(struct drm_crtc *crtc)
|
|
{
|
|
struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
|
|
|
|
hrtimer_cancel(&out->vblank_hrtimer);
|
|
}
|
|
|
|
static bool 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;
|
|
unsigned int pipe = crtc->index;
|
|
struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
|
|
struct vkms_output *output = &vkmsdev->output;
|
|
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
|
|
|
|
if (!READ_ONCE(vblank->enabled)) {
|
|
*vblank_time = ktime_get();
|
|
return true;
|
|
}
|
|
|
|
*vblank_time = READ_ONCE(output->vblank_hrtimer.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 -= output->period_ns;
|
|
|
|
return true;
|
|
}
|
|
|
|
static struct drm_crtc_state *
|
|
vkms_atomic_crtc_duplicate_state(struct drm_crtc *crtc)
|
|
{
|
|
struct vkms_crtc_state *vkms_state;
|
|
|
|
if (WARN_ON(!crtc->state))
|
|
return NULL;
|
|
|
|
vkms_state = kzalloc(sizeof(*vkms_state), GFP_KERNEL);
|
|
if (!vkms_state)
|
|
return NULL;
|
|
|
|
__drm_atomic_helper_crtc_duplicate_state(crtc, &vkms_state->base);
|
|
|
|
INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
|
|
|
|
return &vkms_state->base;
|
|
}
|
|
|
|
static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *state)
|
|
{
|
|
struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
|
|
|
|
__drm_atomic_helper_crtc_destroy_state(state);
|
|
|
|
WARN_ON(work_pending(&vkms_state->composer_work));
|
|
kfree(vkms_state->active_planes);
|
|
kfree(vkms_state);
|
|
}
|
|
|
|
static void vkms_atomic_crtc_reset(struct drm_crtc *crtc)
|
|
{
|
|
struct vkms_crtc_state *vkms_state =
|
|
kzalloc(sizeof(*vkms_state), GFP_KERNEL);
|
|
|
|
if (crtc->state)
|
|
vkms_atomic_crtc_destroy_state(crtc, crtc->state);
|
|
|
|
__drm_atomic_helper_crtc_reset(crtc, &vkms_state->base);
|
|
if (vkms_state)
|
|
INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
|
|
}
|
|
|
|
static const struct drm_crtc_funcs vkms_crtc_funcs = {
|
|
.set_config = drm_atomic_helper_set_config,
|
|
.destroy = drm_crtc_cleanup,
|
|
.page_flip = drm_atomic_helper_page_flip,
|
|
.reset = vkms_atomic_crtc_reset,
|
|
.atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
|
|
.atomic_destroy_state = vkms_atomic_crtc_destroy_state,
|
|
.enable_vblank = vkms_enable_vblank,
|
|
.disable_vblank = vkms_disable_vblank,
|
|
.get_vblank_timestamp = vkms_get_vblank_timestamp,
|
|
.get_crc_sources = vkms_get_crc_sources,
|
|
.set_crc_source = vkms_set_crc_source,
|
|
.verify_crc_source = vkms_verify_crc_source,
|
|
};
|
|
|
|
static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *state)
|
|
{
|
|
struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
|
|
struct drm_plane *plane;
|
|
struct drm_plane_state *plane_state;
|
|
int i = 0, ret;
|
|
|
|
if (vkms_state->active_planes)
|
|
return 0;
|
|
|
|
ret = drm_atomic_add_affected_planes(state->state, crtc);
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
drm_for_each_plane_mask(plane, crtc->dev, state->plane_mask) {
|
|
plane_state = drm_atomic_get_existing_plane_state(state->state,
|
|
plane);
|
|
WARN_ON(!plane_state);
|
|
|
|
if (!plane_state->visible)
|
|
continue;
|
|
|
|
i++;
|
|
}
|
|
|
|
vkms_state->active_planes = kcalloc(i, sizeof(plane), GFP_KERNEL);
|
|
if (!vkms_state->active_planes)
|
|
return -ENOMEM;
|
|
vkms_state->num_active_planes = i;
|
|
|
|
i = 0;
|
|
drm_for_each_plane_mask(plane, crtc->dev, state->plane_mask) {
|
|
plane_state = drm_atomic_get_existing_plane_state(state->state,
|
|
plane);
|
|
|
|
if (!plane_state->visible)
|
|
continue;
|
|
|
|
vkms_state->active_planes[i++] =
|
|
to_vkms_plane_state(plane_state);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void vkms_crtc_atomic_enable(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *old_state)
|
|
{
|
|
drm_crtc_vblank_on(crtc);
|
|
}
|
|
|
|
static void vkms_crtc_atomic_disable(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *old_state)
|
|
{
|
|
drm_crtc_vblank_off(crtc);
|
|
}
|
|
|
|
static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *old_crtc_state)
|
|
{
|
|
struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
|
|
|
|
/* This lock is held across the atomic commit to block vblank timer
|
|
* from scheduling vkms_composer_worker until the composer is updated
|
|
*/
|
|
spin_lock_irq(&vkms_output->lock);
|
|
}
|
|
|
|
static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
|
|
struct drm_crtc_state *old_crtc_state)
|
|
{
|
|
struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
|
|
|
|
if (crtc->state->event) {
|
|
spin_lock(&crtc->dev->event_lock);
|
|
|
|
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(&crtc->dev->event_lock);
|
|
|
|
crtc->state->event = NULL;
|
|
}
|
|
|
|
vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
|
|
|
|
spin_unlock_irq(&vkms_output->lock);
|
|
}
|
|
|
|
static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
|
|
.atomic_check = vkms_crtc_atomic_check,
|
|
.atomic_begin = vkms_crtc_atomic_begin,
|
|
.atomic_flush = vkms_crtc_atomic_flush,
|
|
.atomic_enable = vkms_crtc_atomic_enable,
|
|
.atomic_disable = vkms_crtc_atomic_disable,
|
|
};
|
|
|
|
int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
|
|
struct drm_plane *primary, struct drm_plane *cursor)
|
|
{
|
|
struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
|
|
int ret;
|
|
|
|
ret = drm_crtc_init_with_planes(dev, crtc, primary, cursor,
|
|
&vkms_crtc_funcs, NULL);
|
|
if (ret) {
|
|
DRM_ERROR("Failed to init CRTC\n");
|
|
return ret;
|
|
}
|
|
|
|
drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
|
|
|
|
spin_lock_init(&vkms_out->lock);
|
|
spin_lock_init(&vkms_out->composer_lock);
|
|
|
|
vkms_out->composer_workq = alloc_ordered_workqueue("vkms_composer", 0);
|
|
if (!vkms_out->composer_workq)
|
|
return -ENOMEM;
|
|
|
|
return ret;
|
|
}
|