2019-02-06 22:01:16 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2018-05-17 07:56:21 +08:00
|
|
|
|
|
|
|
#include "vkms_drv.h"
|
|
|
|
#include <drm/drm_atomic_helper.h>
|
2019-01-18 05:03:34 +08:00
|
|
|
#include <drm/drm_probe_helper.h>
|
2018-05-17 07:56:21 +08:00
|
|
|
|
2019-01-31 00:07:11 +08:00
|
|
|
static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
|
2018-07-12 10:02:26 +08:00
|
|
|
{
|
2019-01-31 00:07:11 +08:00
|
|
|
struct vkms_output *output = container_of(timer, struct vkms_output,
|
|
|
|
vblank_hrtimer);
|
2018-07-12 10:02:26 +08:00
|
|
|
struct drm_crtc *crtc = &output->crtc;
|
2018-08-02 09:10:26 +08:00
|
|
|
struct vkms_crtc_state *state = to_vkms_crtc_state(crtc->state);
|
2019-02-07 04:08:13 +08:00
|
|
|
u64 ret_overrun;
|
2018-07-12 10:02:26 +08:00
|
|
|
bool ret;
|
|
|
|
|
2018-08-02 09:10:26 +08:00
|
|
|
spin_lock(&output->lock);
|
2019-01-31 00:07:11 +08:00
|
|
|
|
2019-06-06 16:44:04 +08:00
|
|
|
ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
|
|
|
|
output->period_ns);
|
|
|
|
WARN_ON(ret_overrun != 1);
|
|
|
|
|
2018-07-12 10:02:26 +08:00
|
|
|
ret = drm_crtc_handle_vblank(crtc);
|
|
|
|
if (!ret)
|
|
|
|
DRM_ERROR("vkms failure on handling vblank");
|
|
|
|
|
2018-08-02 09:10:26 +08:00
|
|
|
if (state && output->crc_enabled) {
|
2018-09-04 05:18:17 +08:00
|
|
|
u64 frame = drm_crtc_accurate_vblank_count(crtc);
|
|
|
|
|
|
|
|
/* update frame_start only if a queued vkms_crc_work_handle()
|
|
|
|
* has read the data
|
|
|
|
*/
|
|
|
|
spin_lock(&output->state_lock);
|
|
|
|
if (!state->frame_start)
|
|
|
|
state->frame_start = frame;
|
|
|
|
spin_unlock(&output->state_lock);
|
|
|
|
|
|
|
|
ret = queue_work(output->crc_workq, &state->crc_work);
|
|
|
|
if (!ret)
|
|
|
|
DRM_WARN("failed to queue vkms_crc_work_handle");
|
2018-08-02 09:10:26 +08:00
|
|
|
}
|
|
|
|
|
2019-01-31 00:07:11 +08:00
|
|
|
spin_unlock(&output->lock);
|
2018-07-12 10:02:26 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vkms_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
|
|
|
|
int *max_error, ktime_t *vblank_time,
|
|
|
|
bool in_vblank_irq)
|
|
|
|
{
|
|
|
|
struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
|
|
|
|
struct vkms_output *output = &vkmsdev->output;
|
2019-06-06 16:44:04 +08:00
|
|
|
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
|
2018-07-12 10:02:26 +08:00
|
|
|
|
|
|
|
*vblank_time = output->vblank_hrtimer.node.expires;
|
|
|
|
|
2019-06-06 16:44:04 +08:00
|
|
|
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;
|
drm/vkms: Bugfix extra vblank frame
kms_flip tests are breaking on vkms when simulate vblank because vblank
event sequence count returns one extra frame after arm vblank event to
make a page flip.
When vblank interrupt happens, userspace processes the vblank event and
issues the next page flip command. Kernel calls queue_work to call
commit_planes and arm the new page flip. The next vblank picks up the
newly armed vblank event and vblank interrupt happens again.
The arm and vblank event are asynchronous, then, on the next vblank, we
receive x+2 from `get_vblank_timestamp`, instead x+1, although timestamp
and vblank seqno matches.
Function `get_vblank_timestamp` is reached by 2 ways:
- from `drm_mode_page_flip_ioctl`: driver is doing one atomic
operation to synchronize planes in the same output. There is no
vblank simulation, the `drm_crtc_arm_vblank_event` function adds 1
on vblank count, and the variable in_vblank_irq is false
- from `vkms_vblank_simulate`: since the driver is doing a vblank
simulation, the variable in_vblank_irq is true.
Fix this problem subtracting one vblank period from vblank_time when
`get_vblank_timestamp` is called from trace `drm_mode_page_flip_ioctl`,
i.e., is not a real vblank interrupt, and getting the timestamp and
vblank seqno when it is a real vblank interrupt.
The reason for all this is that get_vblank_timestamp always supplies the
timestamp for the next vblank event. The hrtimer is the vblank
simulator, and it needs the correct previous value to present the next
vblank. Since this is how hw timestamp registers work and what the
vblank core expects.
Signed-off-by: Shayenne Moura <shayenneluzmoura@gmail.com>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/171e6e1c239cbca0c3df7183ed8acdfeeace9cf4.1548856186.git.shayenneluzmoura@gmail.com
2019-01-31 00:06:36 +08:00
|
|
|
|
2018-07-12 10:02:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:31:05 +08:00
|
|
|
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);
|
|
|
|
|
2018-08-02 09:10:26 +08:00
|
|
|
INIT_WORK(&vkms_state->crc_work, vkms_crc_work_handle);
|
|
|
|
|
2018-07-25 00:31:05 +08:00
|
|
|
return &vkms_state->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
|
|
|
|
struct drm_crtc_state *state)
|
|
|
|
{
|
2018-08-02 09:10:26 +08:00
|
|
|
struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
|
2018-07-25 00:31:05 +08:00
|
|
|
|
|
|
|
__drm_atomic_helper_crtc_destroy_state(state);
|
2018-08-02 09:10:26 +08:00
|
|
|
|
|
|
|
if (vkms_state) {
|
|
|
|
flush_work(&vkms_state->crc_work);
|
|
|
|
kfree(vkms_state);
|
|
|
|
}
|
2018-07-25 00:31:05 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:56:26 +08:00
|
|
|
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->crc_work, vkms_crc_work_handle);
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:56:21 +08:00
|
|
|
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,
|
2018-07-25 00:31:05 +08:00
|
|
|
.reset = vkms_atomic_crtc_reset,
|
|
|
|
.atomic_duplicate_state = vkms_atomic_crtc_duplicate_state,
|
|
|
|
.atomic_destroy_state = vkms_atomic_crtc_destroy_state,
|
2018-07-12 10:02:26 +08:00
|
|
|
.enable_vblank = vkms_enable_vblank,
|
|
|
|
.disable_vblank = vkms_disable_vblank,
|
2019-06-13 20:18:02 +08:00
|
|
|
.get_crc_sources = vkms_get_crc_sources,
|
2018-08-02 09:10:26 +08:00
|
|
|
.set_crc_source = vkms_set_crc_source,
|
2018-08-21 16:38:55 +08:00
|
|
|
.verify_crc_source = vkms_verify_crc_source,
|
2018-07-12 10:02:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:10:26 +08:00
|
|
|
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_crc_work_handle until the crc_data is updated
|
|
|
|
*/
|
|
|
|
spin_lock_irq(&vkms_output->lock);
|
|
|
|
}
|
|
|
|
|
2018-07-12 10:02:26 +08:00
|
|
|
static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
|
|
|
|
struct drm_crtc_state *old_crtc_state)
|
|
|
|
{
|
2018-08-02 09:10:26 +08:00
|
|
|
struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
|
2018-07-12 10:02:26 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-08-02 09:10:26 +08:00
|
|
|
|
|
|
|
spin_unlock_irq(&vkms_output->lock);
|
2018-07-12 10:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
|
2018-08-02 09:10:26 +08:00
|
|
|
.atomic_begin = vkms_crtc_atomic_begin,
|
2018-07-12 10:02:26 +08:00
|
|
|
.atomic_flush = vkms_crtc_atomic_flush,
|
|
|
|
.atomic_enable = vkms_crtc_atomic_enable,
|
|
|
|
.atomic_disable = vkms_crtc_atomic_disable,
|
2018-05-17 07:56:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
|
|
|
|
struct drm_plane *primary, struct drm_plane *cursor)
|
|
|
|
{
|
2018-08-02 09:10:26 +08:00
|
|
|
struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
|
2018-05-17 07:56:21 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-12 10:02:26 +08:00
|
|
|
drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
|
|
|
|
|
2018-08-02 09:10:26 +08:00
|
|
|
spin_lock_init(&vkms_out->lock);
|
2018-09-04 05:18:17 +08:00
|
|
|
spin_lock_init(&vkms_out->state_lock);
|
2018-08-02 09:10:26 +08:00
|
|
|
|
|
|
|
vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0);
|
2019-03-09 12:36:27 +08:00
|
|
|
if (!vkms_out->crc_workq)
|
|
|
|
return -ENOMEM;
|
2018-08-02 09:10:26 +08:00
|
|
|
|
2018-05-17 07:56:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|