Skip to content

Commit 9f2a795

Browse files
committed
drm/atomic-helper: nonblocking commit support
Design ideas: - split up the actual commit into different phases, and have completions for each of them. This will be useful for the future when we want to interleave phases much more aggressively, for e.g. queue depth > 1. For not it's just a minimal optimization compared to current common nonblocking implementation patterns from drivers, which all stall for the entire commit to complete, including vblank waits and cleanups. - Extract a separate atomic_commit_hw hook since that's the part most drivers will need to overwrite, hopefully allowing even more shared code. - Enforce EBUSY seamntics by attaching one of the completions to the flip_done vblank event. Side benefit of forcing atomic drivers using these helpers to implement event handlign at least semi-correct. I'm evil that way ;-) - Ridiculously modular, as usual. - The main tracking unit for a commit stays struct drm_atomic_state, and the ownership rules for that are unchanged. Ownership still gets transferred to the driver (and subsequently to the worker) on successful commits. What is added is a small, per-crtc, refcounted structure to track pending commits called struct drm_crtc_commit. No actual state is attached to that though, it's purely for ordering and waiting. - Dependencies are implicitly handled by assuming that any CRTC part of &drm_atomic_state is a dependency, and that the current commit must wait for any commits to complete on those CRTC. This way drivers can easily add more depencies using drm_atomic_get_crtc_state(), which is very natural since in most case a dependency exists iff there's some bit of state that needs to be cross checked. Removing depencies is not possible, drivers simply need to be careful to not include every CRTC in a commit if that's not necessary. Which is a good idea anyway, since that also avoids ww_mutex lock contention. - Queue depth > 1 sees some prep work in this patch by adding a stall paramater to drm_atomic_helper_swap_states(). To be able to push commits entirely free-standing and in a deeper queue through the back-end the driver must not access any obj->state pointers. This means we need to track the old state in drm_atomic_state (much easier with the consolidated arrays), and pass them all explicitly to driver backends (this will be serious amounts of churn). Once that's done stall can be set to false in swap_states. v2: Dont ask for flip_done signalling when the CRTC is off and stays off: Drivers don't handle events in that case. Instead complete right away. This way future commits don't need to have special-case logic, but can keep blocking for the flip_done completion. v3: Tons of fixes: - Stall for preceeding commit for real, not the current one by accident. - Add WARN_ON in case drivers don't fire the drm event. - Don't double-free drm events. v4: Make legacy cursor not stall. v5: Extend the helper hook to cover the entire commit tail. Some drivers need special code for cleanup and vblank waiting, this makes it a bit more useful. Inspired by the rockchip driver. v6: Add WARN_ON to catch drivers who forget to send out the drm event. v7: Fixup the stalls in swap_state for real!! v8: - Fixup trailing whitespace, spotted by Maarten. - Actually wait for flip_done in cleanup_done, like the comment says we should do. Thanks a lot for Tomeu for helping with debugging this on. v9: Now with awesome kerneldoc! v10: Split out drm_crtc_commit tracking infrastructure. v: - Add missing static (Gustavo). - Split out the sync functions, only do the actual nonblocking logic in this patch (Maarten). Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk> Tested-by: Tomeu Vizoso <tomeu.vizoso@collabora.com> Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Cc: Tomeu Vizoso <tomeu.vizoso@gmail.com> Cc: Daniel Stone <daniels@collabora.com> Tested-by: Liviu Dudau <Liviu.Dudau@arm.com> Reviewed-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com> Testcase: igt/kms_flip/* Testcase: igt/kms_cursor* Testcase: igt/kms*plane* Signed-off-by: Daniel Vetter <daniel.vetter@intel.com> Link: http://patchwork.freedesktop.org/patch/msgid/1465388359-8070-10-git-send-email-daniel.vetter@ffwll.ch
1 parent a095caa commit 9f2a795

4 files changed

Lines changed: 141 additions & 46 deletions

File tree

‎drivers/gpu/drm/drm_atomic_helper.c‎

Lines changed: 98 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,30 +1120,91 @@ drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
11201120
EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
11211121

11221122
/**
1123-
* drm_atomic_helper_commit - commit validated state object
1124-
* @dev: DRM device
1125-
* @state: the driver state object
1126-
* @nonblocking: whether nonblocking behavior is requested.
1123+
* drm_atomic_helper_commit_tail - commit atomic update to hardware
1124+
* @state: new modeset state to be committed
11271125
*
1128-
* This function commits a with drm_atomic_helper_check() pre-validated state
1129-
* object. This can still fail when e.g. the framebuffer reservation fails. For
1130-
* now this doesn't implement nonblocking commits.
1126+
* This is the default implemenation for the ->atomic_commit_tail() hook of the
1127+
* &drm_mode_config_helper_funcs vtable.
11311128
*
1132-
* Note that right now this function does not support nonblocking commits, hence
1133-
* driver writers must implement their own version for now. Also note that the
1134-
* default ordering of how the various stages are called is to match the legacy
1135-
* modeset helper library closest. One peculiarity of that is that it doesn't
1136-
* mesh well with runtime PM at all.
1129+
* Note that the default ordering of how the various stages are called is to
1130+
* match the legacy modeset helper library closest. One peculiarity of that is
1131+
* that it doesn't mesh well with runtime PM at all.
11371132
*
1138-
* For drivers supporting runtime PM the recommended sequence is
1133+
* For drivers supporting runtime PM the recommended sequence is instead ::
11391134
*
11401135
* drm_atomic_helper_commit_modeset_disables(dev, state);
11411136
*
11421137
* drm_atomic_helper_commit_modeset_enables(dev, state);
11431138
*
11441139
* drm_atomic_helper_commit_planes(dev, state, true);
11451140
*
1146-
* See the kerneldoc entries for these three functions for more details.
1141+
* for committing the atomic update to hardware. See the kerneldoc entries for
1142+
* these three functions for more details.
1143+
*/
1144+
void drm_atomic_helper_commit_tail(struct drm_atomic_state *state)
1145+
{
1146+
struct drm_device *dev = state->dev;
1147+
1148+
drm_atomic_helper_commit_modeset_disables(dev, state);
1149+
1150+
drm_atomic_helper_commit_planes(dev, state, false);
1151+
1152+
drm_atomic_helper_commit_modeset_enables(dev, state);
1153+
1154+
drm_atomic_helper_commit_hw_done(state);
1155+
1156+
drm_atomic_helper_wait_for_vblanks(dev, state);
1157+
1158+
drm_atomic_helper_cleanup_planes(dev, state);
1159+
}
1160+
EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1161+
1162+
static void commit_tail(struct drm_atomic_state *state)
1163+
{
1164+
struct drm_device *dev = state->dev;
1165+
struct drm_mode_config_helper_funcs *funcs;
1166+
1167+
funcs = dev->mode_config.helper_private;
1168+
1169+
drm_atomic_helper_wait_for_fences(dev, state);
1170+
1171+
drm_atomic_helper_wait_for_dependencies(state);
1172+
1173+
if (funcs && funcs->atomic_commit_tail)
1174+
funcs->atomic_commit_tail(state);
1175+
else
1176+
drm_atomic_helper_commit_tail(state);
1177+
1178+
drm_atomic_helper_commit_cleanup_done(state);
1179+
1180+
drm_atomic_state_free(state);
1181+
}
1182+
1183+
static void commit_work(struct work_struct *work)
1184+
{
1185+
struct drm_atomic_state *state = container_of(work,
1186+
struct drm_atomic_state,
1187+
commit_work);
1188+
commit_tail(state);
1189+
}
1190+
1191+
/**
1192+
* drm_atomic_helper_commit - commit validated state object
1193+
* @dev: DRM device
1194+
* @state: the driver state object
1195+
* @nonblock: whether nonblocking behavior is requested.
1196+
*
1197+
* This function commits a with drm_atomic_helper_check() pre-validated state
1198+
* object. This can still fail when e.g. the framebuffer reservation fails. This
1199+
* function implements nonblocking commits, using
1200+
* drm_atomic_helper_setup_commit() and related functions.
1201+
*
1202+
* Note that right now this function does not support nonblocking commits, hence
1203+
* driver writers must implement their own version for now.
1204+
*
1205+
* Committing the actual hardware state is done through the
1206+
* ->atomic_commit_tail() callback of the &drm_mode_config_helper_funcs vtable,
1207+
* or it's default implementation drm_atomic_helper_commit_tail().
11471208
*
11481209
* RETURNS:
11491210
* Zero for success or -errno.
@@ -1154,13 +1215,12 @@ int drm_atomic_helper_commit(struct drm_device *dev,
11541215
{
11551216
int ret;
11561217

1157-
if (nonblock)
1158-
return -EBUSY;
1159-
11601218
ret = drm_atomic_helper_setup_commit(state, nonblock);
11611219
if (ret)
11621220
return ret;
11631221

1222+
INIT_WORK(&state->commit_work, commit_work);
1223+
11641224
ret = drm_atomic_helper_prepare_planes(dev, state);
11651225
if (ret)
11661226
return ret;
@@ -1187,27 +1247,16 @@ int drm_atomic_helper_commit(struct drm_device *dev,
11871247
* update. Which is important since compositors need to figure out the
11881248
* composition of the next frame right after having submitted the
11891249
* current layout.
1250+
*
1251+
* NOTE: Commit work has multiple phases, first hardware commit, then
1252+
* cleanup. We want them to overlap, hence need system_unbound_wq to
1253+
* make sure work items don't artifically stall on each another.
11901254
*/
11911255

1192-
drm_atomic_helper_wait_for_fences(dev, state);
1193-
1194-
drm_atomic_helper_wait_for_dependencies(state);
1195-
1196-
drm_atomic_helper_commit_modeset_disables(dev, state);
1197-
1198-
drm_atomic_helper_commit_planes(dev, state, false);
1199-
1200-
drm_atomic_helper_commit_modeset_enables(dev, state);
1201-
1202-
drm_atomic_helper_commit_hw_done(state);
1203-
1204-
drm_atomic_helper_wait_for_vblanks(dev, state);
1205-
1206-
drm_atomic_helper_cleanup_planes(dev, state);
1207-
1208-
drm_atomic_helper_commit_cleanup_done(state);
1209-
1210-
drm_atomic_state_free(state);
1256+
if (nonblock)
1257+
queue_work(system_unbound_wq, &state->commit_work);
1258+
else
1259+
commit_tail(state);
12111260

12121261
return 0;
12131262
}
@@ -1216,12 +1265,7 @@ EXPORT_SYMBOL(drm_atomic_helper_commit);
12161265
/**
12171266
* DOC: implementing nonblocking commit
12181267
*
1219-
* For now the atomic helpers don't support nonblocking commit directly. If
1220-
* there is real need it could be added though, using the dma-buf fence
1221-
* infrastructure for generic synchronization with outstanding rendering.
1222-
*
1223-
* For now drivers have to implement nonblocking commit themselves, with the
1224-
* following sequence being the recommended one:
1268+
* Nonblocking atomic commits have to be implemented in the following sequence:
12251269
*
12261270
* 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
12271271
* which commit needs to call which can fail, so we want to run it first and
@@ -1233,10 +1277,14 @@ EXPORT_SYMBOL(drm_atomic_helper_commit);
12331277
* cancelled updates. Note that it is important to ensure that the framebuffer
12341278
* cleanup is still done when cancelling.
12351279
*
1236-
* For sufficient parallelism it is recommended to have a work item per crtc
1237-
* (for updates which don't touch global state) and a global one. Then we only
1238-
* need to synchronize with the crtc work items for changed crtcs and the global
1239-
* work item, which allows nice concurrent updates on disjoint sets of crtcs.
1280+
* Asynchronous workers need to have sufficient parallelism to be able to run
1281+
* different atomic commits on different CRTCs in parallel. The simplest way to
1282+
* achive this is by running them on the &system_unbound_wq work queue. Note
1283+
* that drivers are not required to split up atomic commits and run an
1284+
* individual commit in parallel - userspace is supposed to do that if it cares.
1285+
* But it might be beneficial to do that for modesets, since those necessarily
1286+
* must be done as one global operation, and enabling or disabling a CRTC can
1287+
* take a long time. But even that is not required.
12401288
*
12411289
* 3. The software state is updated synchronously with
12421290
* drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
@@ -1249,6 +1297,10 @@ EXPORT_SYMBOL(drm_atomic_helper_commit);
12491297
* commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
12501298
* then cleaning up the framebuffers after the old framebuffer is no longer
12511299
* being displayed.
1300+
*
1301+
* The above scheme is implemented in the atomic helper libraries in
1302+
* drm_atomic_helper_commit() using a bunch of helper functions. See
1303+
* drm_atomic_helper_setup_commit() for a starting point.
12521304
*/
12531305

12541306
static int stall_checks(struct drm_crtc *crtc, bool nonblock)

‎include/drm/drm_atomic_helper.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int drm_atomic_helper_check_planes(struct drm_device *dev,
3838
struct drm_atomic_state *state);
3939
int drm_atomic_helper_check(struct drm_device *dev,
4040
struct drm_atomic_state *state);
41+
void drm_atomic_helper_commit_tail(struct drm_atomic_state *state);
4142
int drm_atomic_helper_commit(struct drm_device *dev,
4243
struct drm_atomic_state *state,
4344
bool nonblock);

‎include/drm/drm_crtc.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,6 +2248,7 @@ struct drm_mode_config_funcs {
22482248
* @async_page_flip: does this device support async flips on the primary plane?
22492249
* @cursor_width: hint to userspace for max cursor width
22502250
* @cursor_height: hint to userspace for max cursor height
2251+
* @helper_private: mid-layer private data
22512252
*
22522253
* Core mode resource tracking structure. All CRTC, encoders, and connectors
22532254
* enumerated by the driver are added here, as are global properties. Some
@@ -2391,6 +2392,8 @@ struct drm_mode_config {
23912392

23922393
/* cursor size */
23932394
uint32_t cursor_width, cursor_height;
2395+
2396+
struct drm_mode_config_helper_funcs *helper_private;
23942397
};
23952398

23962399
/**

‎include/drm/drm_modeset_helper_vtables.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,4 +931,43 @@ static inline void drm_plane_helper_add(struct drm_plane *plane,
931931
plane->helper_private = funcs;
932932
}
933933

934+
/**
935+
* struct drm_mode_config_helper_funcs - global modeset helper operations
936+
*
937+
* These helper functions are used by the atomic helpers.
938+
*/
939+
struct drm_mode_config_helper_funcs {
940+
/**
941+
* @atomic_commit_tail:
942+
*
943+
* This hook is used by the default atomic_commit() hook implemented in
944+
* drm_atomic_helper_commit() together with the nonblocking commit
945+
* helpers (see drm_atomic_helper_setup_commit() for a starting point)
946+
* to implement blocking and nonblocking commits easily. It is not used
947+
* by the atomic helpers
948+
*
949+
* This hook should first commit the given atomic state to the hardware.
950+
* But drivers can add more waiting calls at the start of their
951+
* implementation, e.g. to wait for driver-internal request for implicit
952+
* syncing, before starting to commit the update to the hardware.
953+
*
954+
* After the atomic update is committed to the hardware this hook needs
955+
* to call drm_atomic_helper_commit_hw_done(). Then wait for the upate
956+
* to be executed by the hardware, for example using
957+
* drm_atomic_helper_wait_for_vblanks(), and then clean up the old
958+
* framebuffers using drm_atomic_helper_cleanup_planes().
959+
*
960+
* When disabling a CRTC this hook _must_ stall for the commit to
961+
* complete. Vblank waits don't work on disabled CRTC, hence the core
962+
* can't take care of this. And it also can't rely on the vblank event,
963+
* since that can be signalled already when the screen shows black,
964+
* which can happen much earlier than the last hardware access needed to
965+
* shut off the display pipeline completely.
966+
*
967+
* This hook is optional, the default implementation is
968+
* drm_atomic_helper_commit_tail().
969+
*/
970+
void (*atomic_commit_tail)(struct drm_atomic_state *state);
971+
};
972+
934973
#endif

0 commit comments

Comments
 (0)