2019-02-06 21:01:16 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2018-05-17 06:56:21 +07:00
|
|
|
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
#include <drm/drm_atomic.h>
|
2018-05-17 06:56:21 +07:00
|
|
|
#include <drm/drm_atomic_helper.h>
|
2019-01-18 04:03:34 +07:00
|
|
|
#include <drm/drm_probe_helper.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
#include <drm/drm_vblank.h>
|
|
|
|
|
|
|
|
#include "vkms_drv.h"
|
2018-05-17 06:56:21 +07:00
|
|
|
|
2019-01-30 23:07:11 +07:00
|
|
|
static enum hrtimer_restart vkms_vblank_simulate(struct hrtimer *timer)
|
2018-07-12 09:02:26 +07:00
|
|
|
{
|
2019-01-30 23:07:11 +07:00
|
|
|
struct vkms_output *output = container_of(timer, struct vkms_output,
|
|
|
|
vblank_hrtimer);
|
2018-07-12 09:02:26 +07:00
|
|
|
struct drm_crtc *crtc = &output->crtc;
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
struct vkms_crtc_state *state;
|
2019-02-07 03:08:13 +07:00
|
|
|
u64 ret_overrun;
|
2018-07-12 09:02:26 +07:00
|
|
|
bool ret;
|
|
|
|
|
2019-06-06 15:44:04 +07:00
|
|
|
ret_overrun = hrtimer_forward_now(&output->vblank_hrtimer,
|
|
|
|
output->period_ns);
|
|
|
|
WARN_ON(ret_overrun != 1);
|
|
|
|
|
2019-07-19 22:23:14 +07:00
|
|
|
spin_lock(&output->lock);
|
2018-07-12 09:02:26 +07:00
|
|
|
ret = drm_crtc_handle_vblank(crtc);
|
|
|
|
if (!ret)
|
|
|
|
DRM_ERROR("vkms failure on handling vblank");
|
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
state = output->composer_state;
|
2019-07-19 22:23:14 +07:00
|
|
|
spin_unlock(&output->lock);
|
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
if (state && output->composer_enabled) {
|
2018-09-04 04:18:17 +07:00
|
|
|
u64 frame = drm_crtc_accurate_vblank_count(crtc);
|
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
/* update frame_start only if a queued vkms_composer_worker()
|
2018-09-04 04:18:17 +07:00
|
|
|
* has read the data
|
|
|
|
*/
|
2019-06-26 08:37:05 +07:00
|
|
|
spin_lock(&output->composer_lock);
|
drm/vkms: Fix crc worker races
The issue we have is that the crc worker might fall behind. We've
tried to handle this by tracking both the earliest frame for which it
still needs to compute a crc, and the last one. Plus when the
crtc_state changes, we have a new work item, which are all run in
order due to the ordered workqueue we allocate for each vkms crtc.
Trouble is there's been a few small issues in the current code:
- we need to capture frame_end in the vblank hrtimer, not in the
worker. The worker might run much later, and then we generate a lot
of crc for which there's already a different worker queued up.
- frame number might be 0, so create a new crc_pending boolean to
track this without confusion.
- we need to atomically grab frame_start/end and clear it, so do that
all in one go. This is not going to create a new race, because if we
race with the hrtimer then our work will be re-run.
- only race that can happen is the following:
1. worker starts
2. hrtimer runs and updates frame_end
3. worker grabs frame_start/end, already reading the new frame_end,
and clears crc_pending
4. hrtimer calls queue_work()
5. worker completes
6. worker gets re-run, crc_pending is false
Explain this case a bit better by rewording the comment.
v2: Demote warning level output to debug when we fail to requeue, this
is expected under high load when the crc worker can't quite keep up.
Cc: Shayenne Moura <shayenneluzmoura@gmail.com>
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-2-daniel.vetter@ffwll.ch
2019-06-07 05:27:42 +07:00
|
|
|
if (!state->crc_pending)
|
2018-09-04 04:18:17 +07:00
|
|
|
state->frame_start = frame;
|
drm/vkms: Fix crc worker races
The issue we have is that the crc worker might fall behind. We've
tried to handle this by tracking both the earliest frame for which it
still needs to compute a crc, and the last one. Plus when the
crtc_state changes, we have a new work item, which are all run in
order due to the ordered workqueue we allocate for each vkms crtc.
Trouble is there's been a few small issues in the current code:
- we need to capture frame_end in the vblank hrtimer, not in the
worker. The worker might run much later, and then we generate a lot
of crc for which there's already a different worker queued up.
- frame number might be 0, so create a new crc_pending boolean to
track this without confusion.
- we need to atomically grab frame_start/end and clear it, so do that
all in one go. This is not going to create a new race, because if we
race with the hrtimer then our work will be re-run.
- only race that can happen is the following:
1. worker starts
2. hrtimer runs and updates frame_end
3. worker grabs frame_start/end, already reading the new frame_end,
and clears crc_pending
4. hrtimer calls queue_work()
5. worker completes
6. worker gets re-run, crc_pending is false
Explain this case a bit better by rewording the comment.
v2: Demote warning level output to debug when we fail to requeue, this
is expected under high load when the crc worker can't quite keep up.
Cc: Shayenne Moura <shayenneluzmoura@gmail.com>
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-2-daniel.vetter@ffwll.ch
2019-06-07 05:27:42 +07:00
|
|
|
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;
|
2019-06-26 08:37:05 +07:00
|
|
|
spin_unlock(&output->composer_lock);
|
2018-09-04 04:18:17 +07:00
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
ret = queue_work(output->composer_workq, &state->composer_work);
|
2018-09-04 04:18:17 +07:00
|
|
|
if (!ret)
|
2019-06-26 08:37:05 +07:00
|
|
|
DRM_DEBUG_DRIVER("Composer worker already queued\n");
|
2018-08-02 08:10:26 +07:00
|
|
|
}
|
|
|
|
|
2018-07-12 09:02:26 +07: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);
|
|
|
|
}
|
|
|
|
|
2020-01-23 20:59:40 +07:00
|
|
|
static bool vkms_get_vblank_timestamp(struct drm_crtc *crtc,
|
|
|
|
int *max_error, ktime_t *vblank_time,
|
|
|
|
bool in_vblank_irq)
|
2018-07-12 09:02:26 +07:00
|
|
|
{
|
2020-01-23 20:59:40 +07:00
|
|
|
struct drm_device *dev = crtc->dev;
|
|
|
|
unsigned int pipe = crtc->index;
|
2018-07-12 09:02:26 +07:00
|
|
|
struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
|
|
|
|
struct vkms_output *output = &vkmsdev->output;
|
2019-06-06 15:44:04 +07:00
|
|
|
struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
|
2018-07-12 09:02:26 +07:00
|
|
|
|
2019-07-19 22:23:14 +07:00
|
|
|
*vblank_time = READ_ONCE(output->vblank_hrtimer.node.expires);
|
2018-07-12 09:02:26 +07:00
|
|
|
|
2019-06-06 15:44:04 +07: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-30 23:06:36 +07:00
|
|
|
|
2018-07-12 09:02:26 +07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-24 23:31:05 +07: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);
|
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
|
2018-08-02 08:10:26 +07:00
|
|
|
|
2018-07-24 23:31:05 +07:00
|
|
|
return &vkms_state->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
|
|
|
|
struct drm_crtc_state *state)
|
|
|
|
{
|
2018-08-02 08:10:26 +07:00
|
|
|
struct vkms_crtc_state *vkms_state = to_vkms_crtc_state(state);
|
2018-07-24 23:31:05 +07:00
|
|
|
|
|
|
|
__drm_atomic_helper_crtc_destroy_state(state);
|
2018-08-02 08:10:26 +07:00
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
WARN_ON(work_pending(&vkms_state->composer_work));
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
kfree(vkms_state->active_planes);
|
|
|
|
kfree(vkms_state);
|
2018-07-24 23:31:05 +07:00
|
|
|
}
|
|
|
|
|
2019-03-01 19:56:26 +07: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)
|
2019-06-26 08:37:05 +07:00
|
|
|
INIT_WORK(&vkms_state->composer_work, vkms_composer_worker);
|
2019-03-01 19:56:26 +07:00
|
|
|
}
|
|
|
|
|
2018-05-17 06:56:21 +07: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-24 23:31:05 +07: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 09:02:26 +07:00
|
|
|
.enable_vblank = vkms_enable_vblank,
|
|
|
|
.disable_vblank = vkms_disable_vblank,
|
2020-01-23 20:59:40 +07:00
|
|
|
.get_vblank_timestamp = vkms_get_vblank_timestamp,
|
2019-06-13 19:18:02 +07:00
|
|
|
.get_crc_sources = vkms_get_crc_sources,
|
2018-08-02 08:10:26 +07:00
|
|
|
.set_crc_source = vkms_set_crc_source,
|
2018-08-21 15:38:55 +07:00
|
|
|
.verify_crc_source = vkms_verify_crc_source,
|
2018-07-12 09:02:26 +07:00
|
|
|
};
|
|
|
|
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:02:26 +07: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 08:10:26 +07: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
|
2019-06-26 08:37:05 +07:00
|
|
|
* from scheduling vkms_composer_worker until the composer is updated
|
2018-08-02 08:10:26 +07:00
|
|
|
*/
|
|
|
|
spin_lock_irq(&vkms_output->lock);
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:02:26 +07:00
|
|
|
static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
|
|
|
|
struct drm_crtc_state *old_crtc_state)
|
|
|
|
{
|
2018-08-02 08:10:26 +07:00
|
|
|
struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
|
2018-07-12 09:02:26 +07:00
|
|
|
|
|
|
|
if (crtc->state->event) {
|
2019-06-07 05:27:49 +07:00
|
|
|
spin_lock(&crtc->dev->event_lock);
|
2018-07-12 09:02:26 +07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-06-07 05:27:49 +07:00
|
|
|
spin_unlock(&crtc->dev->event_lock);
|
2018-07-12 09:02:26 +07:00
|
|
|
|
|
|
|
crtc->state->event = NULL;
|
|
|
|
}
|
2018-08-02 08:10:26 +07:00
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
|
2018-08-02 08:10:26 +07:00
|
|
|
spin_unlock_irq(&vkms_output->lock);
|
2018-07-12 09:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct drm_crtc_helper_funcs vkms_crtc_helper_funcs = {
|
drm/vkms: totally reworked crc data tracking
The crc computation worker needs to be able to get at some data
structures and framebuffer mappings, while potentially more atomic
updates are going on. The solution thus far is to copy relevant bits
around, but that's very tedious.
Here's a new approach, which tries to be more clever, but relies on a
few not-so-obvious things:
- crtc_state is always updated when a plane_state changes. Therefore
we can just stuff plane_state pointers into a crtc_state. That
solves the problem of easily getting at the needed plane_states.
- with the flushing changes from previous patches the above also holds
without races due to the next atomic update being a bit eager with
cleaning up pending work - we always wait for all crc work items to
complete before unmapping framebuffers.
- we also need to make sure that the hrtimer fires off the right
worker. Keep a new distinct crc_state pointer, under the
vkms_output->lock protection for this. Note that crtc->state is
updated very early in the atomic commit, way before we arm the
vblank event - the vblank event should always match the buffers we
use to compute the crc. This also solves an issue in the hrtimer,
where we've accessed drm_crtc->state without holding the right locks
(we held none - oops).
- in the worker itself we can then just access the plane states we
need, again solving a bunch of ordering and locking issues.
Accessing plane->state requires locks, accessing the private
vkms_crtc_state->active_planes pointer only requires that the memory
doesn't get freed too early.
The idea behind vkms_crtc_state->active_planes is that this would
contain all visible planes, in z-order, as a first step towards a more
generic blending implementation.
Note that this patch also fixes races between prepare_fb/cleanup_fb
and the crc worker accessing ->vaddr.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-10-daniel.vetter@ffwll.ch
2019-06-07 05:27:50 +07:00
|
|
|
.atomic_check = vkms_crtc_atomic_check,
|
2018-08-02 08:10:26 +07:00
|
|
|
.atomic_begin = vkms_crtc_atomic_begin,
|
2018-07-12 09:02:26 +07:00
|
|
|
.atomic_flush = vkms_crtc_atomic_flush,
|
|
|
|
.atomic_enable = vkms_crtc_atomic_enable,
|
|
|
|
.atomic_disable = vkms_crtc_atomic_disable,
|
2018-05-17 06:56:21 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
|
|
|
|
struct drm_plane *primary, struct drm_plane *cursor)
|
|
|
|
{
|
2018-08-02 08:10:26 +07:00
|
|
|
struct vkms_output *vkms_out = drm_crtc_to_vkms_output(crtc);
|
2018-05-17 06:56:21 +07: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 09:02:26 +07:00
|
|
|
drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
|
|
|
|
|
2018-08-02 08:10:26 +07:00
|
|
|
spin_lock_init(&vkms_out->lock);
|
2019-06-26 08:37:05 +07:00
|
|
|
spin_lock_init(&vkms_out->composer_lock);
|
2018-08-02 08:10:26 +07:00
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
vkms_out->composer_workq = alloc_ordered_workqueue("vkms_composer", 0);
|
|
|
|
if (!vkms_out->composer_workq)
|
2019-03-09 11:36:27 +07:00
|
|
|
return -ENOMEM;
|
2018-08-02 08:10:26 +07:00
|
|
|
|
2018-05-17 06:56:21 +07:00
|
|
|
return ret;
|
|
|
|
}
|