Commit 47b5ac7daa46 ("drm/atomic: Add new atomic_create_state callback
to drm_private_obj") introduced a new pattern for allocating drm object
states.Instead of relying on the reset() callback, it created a new atomic_create_state hook. This is helpful because reset is a bit overloaded: it's used to create the initial software tate, reset it, but also reset the hardware. It can also be used either at probe time, to create the initial state and possibly reset the hardware to an expected default, but also during suspend/resume. Both these cases come with different expectations too: during the initialization, we want to initialize all states, but during suspend/resume, drm_private_states for example are expected to be kept around. And reset() isn't fallible, which makes it harder to handle initialization errors properly. And this is only really relevant for some drivers, since all the helpers for reset only create a new state, and don't touch the hardware at all. It was thus decided to create a new hook that would allocate and initialize a pristine state without any side effect: atomic_create_state to untangle a bit some of it, and to separate the initialization with the actual reset one might need during a suspend/resume. Let's continue the transition to the new pattern with crtcs. Signed-off-by: Maxime Ripard <[email protected]> --- drivers/gpu/drm/drm_atomic_state_helper.c | 47 +++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_mode_config.c | 21 +++++++++++++- include/drm/drm_atomic_state_helper.h | 4 +++ include/drm/drm_crtc.h | 13 +++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_atomic_state_helper.c b/drivers/gpu/drm/drm_atomic_state_helper.c index b87cc3cd5b0021699ac57a33739d172a5706e2e1..f1fc2edc6e6e35fbac5ef82437f3cb7485afc412 100644 --- a/drivers/gpu/drm/drm_atomic_state_helper.c +++ b/drivers/gpu/drm/drm_atomic_state_helper.c @@ -103,10 +103,32 @@ __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc, crtc->state = crtc_state; } EXPORT_SYMBOL(__drm_atomic_helper_crtc_reset); +/** + * __drm_atomic_helper_crtc_create_state - initializes crtc state + * @crtc: crtc object + * @state: new state to initialize + * + * Initializes the newly allocated @state, usually required when + * initializing the drivers. + * + * @state is assumed to be zeroed. + * + * This is useful for drivers that subclass @drm_crtc_state. + */ +void __drm_atomic_helper_crtc_create_state(struct drm_crtc *crtc, + struct drm_crtc_state *state) +{ + __drm_atomic_helper_crtc_state_init(state, crtc); + + if (drm_dev_has_vblank(crtc->dev)) + drm_crtc_vblank_reset(crtc); +} +EXPORT_SYMBOL(__drm_atomic_helper_crtc_create_state); + /** * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs * @crtc: drm CRTC * * Resets the atomic state for @crtc by freeing the state pointer (which might @@ -122,10 +144,35 @@ void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc) __drm_atomic_helper_crtc_reset(crtc, crtc_state); } EXPORT_SYMBOL(drm_atomic_helper_crtc_reset); +/** + * drm_atomic_helper_crtc_create_state - default &drm_crtc_funcs.atomic_create_state hook for crtcs + * @crtc: crtc object + * + * Initializes a pristine @drm_crtc_state. + * + * This is useful for drivers that don't subclass @drm_crtc_state. + * + * RETURNS: + * Pointer to new crtc state, or ERR_PTR on failure. + */ +struct drm_crtc_state *drm_atomic_helper_crtc_create_state(struct drm_crtc *crtc) +{ + struct drm_crtc_state *state; + + state = kzalloc_obj(*state); + if (!state) + return ERR_PTR(-ENOMEM); + + __drm_atomic_helper_crtc_create_state(crtc, state); + + return state; +} +EXPORT_SYMBOL(drm_atomic_helper_crtc_create_state); + /** * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state * @crtc: CRTC object * @state: atomic CRTC state * diff --git a/drivers/gpu/drm/drm_mode_config.c b/drivers/gpu/drm/drm_mode_config.c index 09b8292195ba5eb5d96735aee5506407bd32ade3..f5092d03b168bb114a199b1dbcaeb770983c9749 100644 --- a/drivers/gpu/drm/drm_mode_config.c +++ b/drivers/gpu/drm/drm_mode_config.c @@ -196,10 +196,26 @@ static int drm_mode_config_plane_create_state(struct drm_plane *plane) plane->state = plane_state; return 0; } +static int drm_mode_config_crtc_create_state(struct drm_crtc *crtc) +{ + struct drm_crtc_state *crtc_state; + + if (!crtc->funcs->atomic_create_state) + return 0; + + crtc_state = crtc->funcs->atomic_create_state(crtc); + if (IS_ERR(crtc_state)) + return PTR_ERR(crtc_state); + + crtc->state = crtc_state; + + return 0; +} + /** * drm_mode_config_reset - call ->reset callbacks * @dev: drm device * * This functions calls all the crtc's, encoder's and connector's ->reset @@ -227,13 +243,16 @@ void drm_mode_config_reset(struct drm_device *dev) plane->funcs->reset(plane); else if (plane->funcs->atomic_create_state) drm_mode_config_plane_create_state(plane); } - drm_for_each_crtc(crtc, dev) + drm_for_each_crtc(crtc, dev) { if (crtc->funcs->reset) crtc->funcs->reset(crtc); + else if (crtc->funcs->atomic_create_state) + drm_mode_config_crtc_create_state(crtc); + } drm_for_each_encoder(encoder, dev) if (encoder->funcs && encoder->funcs->reset) encoder->funcs->reset(encoder); diff --git a/include/drm/drm_atomic_state_helper.h b/include/drm/drm_atomic_state_helper.h index 161cd1f6b8266e882eb1fd128b403cba350be3d2..bc83094dfb78f0f3699ca7956a8a666ca09b2d1a 100644 --- a/include/drm/drm_atomic_state_helper.h +++ b/include/drm/drm_atomic_state_helper.h @@ -43,10 +43,14 @@ struct drm_device; void __drm_atomic_helper_crtc_state_init(struct drm_crtc_state *state, struct drm_crtc *crtc); void __drm_atomic_helper_crtc_reset(struct drm_crtc *crtc, struct drm_crtc_state *state); void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc); +void __drm_atomic_helper_crtc_create_state(struct drm_crtc *crtc, + struct drm_crtc_state *state); +struct drm_crtc_state * +drm_atomic_helper_crtc_create_state(struct drm_crtc *crtc); void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc, struct drm_crtc_state *state); struct drm_crtc_state * drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc); void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state); diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h index 312fc1e745d226adec787cd5f1b7c923fe629670..9e410191683c4cb978d759e3b734afdd3bb8aa76 100644 --- a/include/drm/drm_crtc.h +++ b/include/drm/drm_crtc.h @@ -636,10 +636,23 @@ struct drm_crtc_funcs { * 0 on success or a negative error code on failure. */ int (*set_property)(struct drm_crtc *crtc, struct drm_property *property, uint64_t val); + /** + * @atomic_create_state: + * + * Allocates a pristine, initialized, state for the crtc object + * and returns it. + * + * RETURNS: + * + * A new, pristine, crtc state instance or an error pointer + * on failure. + */ + struct drm_crtc_state *(*atomic_create_state)(struct drm_crtc *crtc); + /** * @atomic_duplicate_state: * * Duplicate the current atomic state for this CRTC and return it. * The core and helpers guarantee that any atomic state duplicated with -- 2.53.0
