Skip to content

Commit d059f65

Browse files
committed
drm: Handle legacy per-crtc locking with full acquire ctx
So drivers using the atomic interfaces expect that they can acquire additional locks internal to the driver as-needed. Examples would be locks to protect shared state like shared display PLLs. Unfortunately the legacy ioctls assume that all locking is fully done by the drm core. Now for those paths which grab all locks we already have to keep around an acquire context in dev->mode_config. Helper functions that implement legacy interfaces in terms of atomic support can therefore grab this acquire contexts and reuse it. The only interfaces left are the cursor and pageflip ioctls. So add functions to grab the crtc lock these need using an acquire context and preserve it for atomic drivers to reuse. v2: - Fixup comments&kerneldoc. - Drop the WARNING from modeset_lock_all_crtcs since that can be used in legacy paths with crtc locking. v3: Fix a type on the kerneldoc Dave spotted. Cc: Dave Airlie <airlied@redhat.com> Reviewed-by: Dave Airlie <airlied@redhat.com> Reviewed-by: Matt Roper <matthew.d.roper@intel.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
1 parent a6a8bb8 commit d059f65

4 files changed

Lines changed: 99 additions & 4 deletions

File tree

‎drivers/gpu/drm/drm_crtc.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,7 @@ static int drm_mode_cursor_common(struct drm_device *dev,
28012801
if (crtc->cursor)
28022802
return drm_mode_cursor_universal(crtc, req, file_priv);
28032803

2804-
drm_modeset_lock(&crtc->mutex, NULL);
2804+
drm_modeset_lock_crtc(crtc);
28052805
if (req->flags & DRM_MODE_CURSOR_BO) {
28062806
if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
28072807
ret = -ENXIO;
@@ -2825,7 +2825,7 @@ static int drm_mode_cursor_common(struct drm_device *dev,
28252825
}
28262826
}
28272827
out:
2828-
drm_modeset_unlock(&crtc->mutex);
2828+
drm_modeset_unlock_crtc(crtc);
28292829

28302830
return ret;
28312831

@@ -4561,7 +4561,7 @@ int drm_mode_page_flip_ioctl(struct drm_device *dev,
45614561
if (!crtc)
45624562
return -ENOENT;
45634563

4564-
drm_modeset_lock(&crtc->mutex, NULL);
4564+
drm_modeset_lock_crtc(crtc);
45654565
if (crtc->primary->fb == NULL) {
45664566
/* The framebuffer is currently unbound, presumably
45674567
* due to a hotplug event, that userspace has not
@@ -4645,7 +4645,7 @@ int drm_mode_page_flip_ioctl(struct drm_device *dev,
46454645
drm_framebuffer_unreference(fb);
46464646
if (old_fb)
46474647
drm_framebuffer_unreference(old_fb);
4648-
drm_modeset_unlock(&crtc->mutex);
4648+
drm_modeset_unlock_crtc(crtc);
46494649

46504650
return ret;
46514651
}

‎drivers/gpu/drm/drm_modeset_lock.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,90 @@ void drm_modeset_unlock_all(struct drm_device *dev)
129129
}
130130
EXPORT_SYMBOL(drm_modeset_unlock_all);
131131

132+
/**
133+
* drm_modeset_lock_crtc - lock crtc with hidden acquire ctx
134+
* @crtc: drm crtc
135+
*
136+
* This function locks the given crtc using a hidden acquire context. This is
137+
* necessary so that drivers internally using the atomic interfaces can grab
138+
* further locks with the lock acquire context.
139+
*/
140+
void drm_modeset_lock_crtc(struct drm_crtc *crtc)
141+
{
142+
struct drm_modeset_acquire_ctx *ctx;
143+
int ret;
144+
145+
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
146+
if (WARN_ON(!ctx))
147+
return;
148+
149+
drm_modeset_acquire_init(ctx, 0);
150+
151+
retry:
152+
ret = drm_modeset_lock(&crtc->mutex, ctx);
153+
if (ret)
154+
goto fail;
155+
156+
WARN_ON(crtc->acquire_ctx);
157+
158+
/* now we hold the locks, so now that it is safe, stash the
159+
* ctx for drm_modeset_unlock_crtc():
160+
*/
161+
crtc->acquire_ctx = ctx;
162+
163+
return;
164+
165+
fail:
166+
if (ret == -EDEADLK) {
167+
drm_modeset_backoff(ctx);
168+
goto retry;
169+
}
170+
}
171+
EXPORT_SYMBOL(drm_modeset_lock_crtc);
172+
173+
/**
174+
* drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
175+
* crtc: drm crtc
176+
*
177+
* Legacy ioctl operations like cursor updates or page flips only have per-crtc
178+
* locking, and store the acquire ctx in the corresponding crtc. All other
179+
* legacy operations take all locks and use a global acquire context. This
180+
* function grabs the right one.
181+
*/
182+
struct drm_modeset_acquire_ctx *
183+
drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
184+
{
185+
if (crtc->acquire_ctx)
186+
return crtc->acquire_ctx;
187+
188+
WARN_ON(!crtc->dev->mode_config.acquire_ctx);
189+
190+
return crtc->dev->mode_config.acquire_ctx;
191+
}
192+
EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
193+
194+
/**
195+
* drm_modeset_unlock_crtc - drop crtc lock
196+
* @crtc: drm crtc
197+
*
198+
* This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
199+
* locks acquired through the hidden context.
200+
*/
201+
void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
202+
{
203+
struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
204+
205+
if (WARN_ON(!ctx))
206+
return;
207+
208+
crtc->acquire_ctx = NULL;
209+
drm_modeset_drop_locks(ctx);
210+
drm_modeset_acquire_fini(ctx);
211+
212+
kfree(ctx);
213+
}
214+
EXPORT_SYMBOL(drm_modeset_unlock_crtc);
215+
132216
/**
133217
* drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
134218
* @dev: device

‎include/drm/drm_crtc.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ struct drm_crtc {
371371
void *helper_private;
372372

373373
struct drm_object_properties properties;
374+
375+
/*
376+
* For legacy crtc ioctls so that atomic drivers can get at the locking
377+
* acquire context.
378+
*/
379+
struct drm_modeset_acquire_ctx *acquire_ctx;
374380
};
375381

376382

‎include/drm/drm_modeset_lock.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
120120
void drm_modeset_unlock(struct drm_modeset_lock *lock);
121121

122122
struct drm_device;
123+
struct drm_crtc;
123124

124125
void drm_modeset_lock_all(struct drm_device *dev);
125126
void drm_modeset_unlock_all(struct drm_device *dev);
127+
void drm_modeset_lock_crtc(struct drm_crtc *crtc);
128+
void drm_modeset_unlock_crtc(struct drm_crtc *crtc);
126129
void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
130+
struct drm_modeset_acquire_ctx *
131+
drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc);
127132

128133
int drm_modeset_lock_all_crtcs(struct drm_device *dev,
129134
struct drm_modeset_acquire_ctx *ctx);

0 commit comments

Comments
 (0)