1 From d7f32f81cf7579ac7d13127db88e789ea864824c Mon Sep 17 00:00:00 2001
2 From: Derek Foreman <derekf@osg.samsung.com>
3 Date: Thu, 24 Nov 2016 12:11:55 -0600
4 Subject: [PATCH] drm/vc4: Fix race between page flip completion event and
7 There was a small window where a userspace program could submit
8 a pageflip after receiving a pageflip completion event yet still
11 Signed-off-by: Derek Foreman <derekf@osg.samsung.com>
12 Signed-off-by: Eric Anholt <eric@anholt.net>
13 Reviewed-by: Eric Anholt <eric@anholt.net>
14 Reviewed-by: Daniel Stone <daniels@collabora.com>
15 (cherry picked from commit 26fc78f6fef39b9d7a15def5e7e9826ff68303f4)
17 drivers/gpu/drm/vc4/vc4_crtc.c | 8 ++++++++
18 drivers/gpu/drm/vc4/vc4_drv.h | 1 +
19 drivers/gpu/drm/vc4/vc4_kms.c | 33 +++++++++++++++++++++++++--------
20 3 files changed, 34 insertions(+), 8 deletions(-)
22 --- a/drivers/gpu/drm/vc4/vc4_crtc.c
23 +++ b/drivers/gpu/drm/vc4/vc4_crtc.c
24 @@ -682,6 +682,14 @@ void vc4_disable_vblank(struct drm_devic
25 CRTC_WRITE(PV_INTEN, 0);
28 +/* Must be called with the event lock held */
29 +bool vc4_event_pending(struct drm_crtc *crtc)
31 + struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
33 + return !!vc4_crtc->event;
36 static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
38 struct drm_crtc *crtc = &vc4_crtc->base;
39 --- a/drivers/gpu/drm/vc4/vc4_drv.h
40 +++ b/drivers/gpu/drm/vc4/vc4_drv.h
41 @@ -445,6 +445,7 @@ int vc4_bo_stats_debugfs(struct seq_file
42 extern struct platform_driver vc4_crtc_driver;
43 int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
44 void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
45 +bool vc4_event_pending(struct drm_crtc *crtc);
46 int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
47 int vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
48 unsigned int flags, int *vpos, int *hpos,
49 --- a/drivers/gpu/drm/vc4/vc4_kms.c
50 +++ b/drivers/gpu/drm/vc4/vc4_kms.c
51 @@ -119,17 +119,34 @@ static int vc4_atomic_commit(struct drm_
53 /* Make sure that any outstanding modesets have finished. */
55 - ret = down_trylock(&vc4->async_modeset);
57 + struct drm_crtc *crtc;
58 + struct drm_crtc_state *crtc_state;
59 + unsigned long flags;
63 + * If there's an undispatched event to send then we're
64 + * obviously still busy. If there isn't, then we can
65 + * unconditionally wait for the semaphore because it
66 + * shouldn't be contended (for long).
68 + * This is to prevent a race where queuing a new flip
69 + * from userspace immediately on receipt of an event
70 + * beats our clean-up and returns EBUSY.
72 + spin_lock_irqsave(&dev->event_lock, flags);
73 + for_each_crtc_in_state(state, crtc, crtc_state, i)
74 + busy |= vc4_event_pending(crtc);
75 + spin_unlock_irqrestore(&dev->event_lock, flags);
81 - ret = down_interruptible(&vc4->async_modeset);
87 + ret = down_interruptible(&vc4->async_modeset);
93 ret = drm_atomic_helper_prepare_planes(dev, state);