/*1* Copyright (C) 2014 Red Hat2* Copyright (C) 2014 Intel Corp.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,19* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR20* OTHER DEALINGS IN THE SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24* Daniel Vetter <[email protected]>25*/2627#ifndef DRM_ATOMIC_H_28#define DRM_ATOMIC_H_2930#include <drm/drm_crtc.h>31#include <drm/drm_util.h>3233/**34* struct drm_crtc_commit - track modeset commits on a CRTC35*36* This structure is used to track pending modeset changes and atomic commit on37* a per-CRTC basis. Since updating the list should never block, this structure38* is reference counted to allow waiters to safely wait on an event to complete,39* without holding any locks.40*41* It has 3 different events in total to allow a fine-grained synchronization42* between outstanding updates::43*44* atomic commit thread hardware45*46* write new state into hardware ----> ...47* signal hw_done48* switch to new state on next49* ... v/hblank50*51* wait for buffers to show up ...52*53* ... send completion irq54* irq handler signals flip_done55* cleanup old buffers56*57* signal cleanup_done58*59* wait for flip_done <----60* clean up atomic state61*62* The important bit to know is that &cleanup_done is the terminal event, but the63* ordering between &flip_done and &hw_done is entirely up to the specific driver64* and modeset state change.65*66* For an implementation of how to use this look at67* drm_atomic_helper_setup_commit() from the atomic helper library.68*69* See also drm_crtc_commit_wait().70*/71struct drm_crtc_commit {72/**73* @crtc:74*75* DRM CRTC for this commit.76*/77struct drm_crtc *crtc;7879/**80* @ref:81*82* Reference count for this structure. Needed to allow blocking on83* completions without the risk of the completion disappearing84* meanwhile.85*/86struct kref ref;8788/**89* @flip_done:90*91* Will be signaled when the hardware has flipped to the new set of92* buffers. Signals at the same time as when the drm event for this93* commit is sent to userspace, or when an out-fence is singalled. Note94* that for most hardware, in most cases this happens after @hw_done is95* signalled.96*97* Completion of this stage is signalled implicitly by calling98* drm_crtc_send_vblank_event() on &drm_crtc_state.event.99*/100struct completion flip_done;101102/**103* @hw_done:104*105* Will be signalled when all hw register changes for this commit have106* been written out. Especially when disabling a pipe this can be much107* later than @flip_done, since that can signal already when the108* screen goes black, whereas to fully shut down a pipe more register109* I/O is required.110*111* Note that this does not need to include separately reference-counted112* resources like backing storage buffer pinning, or runtime pm113* management.114*115* Drivers should call drm_atomic_helper_commit_hw_done() to signal116* completion of this stage.117*/118struct completion hw_done;119120/**121* @cleanup_done:122*123* Will be signalled after old buffers have been cleaned up by calling124* drm_atomic_helper_cleanup_planes(). Since this can only happen after125* a vblank wait completed it might be a bit later. This completion is126* useful to throttle updates and avoid hardware updates getting ahead127* of the buffer cleanup too much.128*129* Drivers should call drm_atomic_helper_commit_cleanup_done() to signal130* completion of this stage.131*/132struct completion cleanup_done;133134/**135* @commit_entry:136*137* Entry on the per-CRTC &drm_crtc.commit_list. Protected by138* $drm_crtc.commit_lock.139*/140struct list_head commit_entry;141142/**143* @event:144*145* &drm_pending_vblank_event pointer to clean up private events.146*/147struct drm_pending_vblank_event *event;148149/**150* @abort_completion:151*152* A flag that's set after drm_atomic_helper_setup_commit() takes a153* second reference for the completion of $drm_crtc_state.event. It's154* used by the free code to remove the second reference if commit fails.155*/156bool abort_completion;157};158159struct __drm_planes_state {160struct drm_plane *ptr;161struct drm_plane_state *state, *old_state, *new_state;162};163164struct __drm_crtcs_state {165struct drm_crtc *ptr;166struct drm_crtc_state *state, *old_state, *new_state;167168/**169* @commit:170*171* A reference to the CRTC commit object that is kept for use by172* drm_atomic_helper_wait_for_flip_done() after173* drm_atomic_helper_commit_hw_done() is called. This ensures that a174* concurrent commit won't free a commit object that is still in use.175*/176struct drm_crtc_commit *commit;177178s32 __user *out_fence_ptr;179u64 last_vblank_count;180};181182struct __drm_connnectors_state {183struct drm_connector *ptr;184struct drm_connector_state *state, *old_state, *new_state;185/**186* @out_fence_ptr:187*188* User-provided pointer which the kernel uses to return a sync_file189* file descriptor. Used by writeback connectors to signal completion of190* the writeback.191*/192s32 __user *out_fence_ptr;193};194195struct drm_private_obj;196struct drm_private_state;197198/**199* struct drm_private_state_funcs - atomic state functions for private objects200*201* These hooks are used by atomic helpers to create, swap and destroy states of202* private objects. The structure itself is used as a vtable to identify the203* associated private object type. Each private object type that needs to be204* added to the atomic states is expected to have an implementation of these205* hooks and pass a pointer to its drm_private_state_funcs struct to206* drm_atomic_get_private_obj_state().207*/208struct drm_private_state_funcs {209/**210* @atomic_duplicate_state:211*212* Duplicate the current state of the private object and return it. It213* is an error to call this before obj->state has been initialized.214*215* RETURNS:216*217* Duplicated atomic state or NULL when obj->state is not218* initialized or allocation failed.219*/220struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);221222/**223* @atomic_destroy_state:224*225* Frees the private object state created with @atomic_duplicate_state.226*/227void (*atomic_destroy_state)(struct drm_private_obj *obj,228struct drm_private_state *state);229230/**231* @atomic_print_state:232*233* If driver subclasses &struct drm_private_state, it should implement234* this optional hook for printing additional driver specific state.235*236* Do not call this directly, use drm_atomic_private_obj_print_state()237* instead.238*/239void (*atomic_print_state)(struct drm_printer *p,240const struct drm_private_state *state);241};242243/**244* struct drm_private_obj - base struct for driver private atomic object245*246* A driver private object is initialized by calling247* drm_atomic_private_obj_init() and cleaned up by calling248* drm_atomic_private_obj_fini().249*250* Currently only tracks the state update functions and the opaque driver251* private state itself, but in the future might also track which252* &drm_modeset_lock is required to duplicate and update this object's state.253*254* All private objects must be initialized before the DRM device they are255* attached to is registered to the DRM subsystem (call to drm_dev_register())256* and should stay around until this DRM device is unregistered (call to257* drm_dev_unregister()). In other words, private objects lifetime is tied258* to the DRM device lifetime. This implies that:259*260* 1/ all calls to drm_atomic_private_obj_init() must be done before calling261* drm_dev_register()262* 2/ all calls to drm_atomic_private_obj_fini() must be done after calling263* drm_dev_unregister()264*265* If that private object is used to store a state shared by multiple266* CRTCs, proper care must be taken to ensure that non-blocking commits are267* properly ordered to avoid a use-after-free issue.268*269* Indeed, assuming a sequence of two non-blocking &drm_atomic_commit on two270* different &drm_crtc using different &drm_plane and &drm_connector, so with no271* resources shared, there's no guarantee on which commit is going to happen272* first. However, the second &drm_atomic_commit will consider the first273* &drm_private_obj its old state, and will be in charge of freeing it whenever274* the second &drm_atomic_commit is done.275*276* If the first &drm_atomic_commit happens after it, it will consider its277* &drm_private_obj the new state and will be likely to access it, resulting in278* an access to a freed memory region. Drivers should store (and get a reference279* to) the &drm_crtc_commit structure in our private state in280* &drm_mode_config_helper_funcs.atomic_commit_setup, and then wait for that281* commit to complete as the first step of282* &drm_mode_config_helper_funcs.atomic_commit_tail, similar to283* drm_atomic_helper_wait_for_dependencies().284*/285struct drm_private_obj {286/**287* @head: List entry used to attach a private object to a &drm_device288* (queued to &drm_mode_config.privobj_list).289*/290struct list_head head;291292/**293* @lock: Modeset lock to protect the state object.294*/295struct drm_modeset_lock lock;296297/**298* @state: Current atomic state for this driver private object.299*/300struct drm_private_state *state;301302/**303* @funcs:304*305* Functions to manipulate the state of this driver private object, see306* &drm_private_state_funcs.307*/308const struct drm_private_state_funcs *funcs;309};310311/**312* drm_for_each_privobj() - private object iterator313*314* @privobj: pointer to the current private object. Updated after each315* iteration316* @dev: the DRM device we want get private objects from317*318* Allows one to iterate over all private objects attached to @dev319*/320#define drm_for_each_privobj(privobj, dev) \321list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)322323/**324* struct drm_private_state - base struct for driver private object state325*326* Currently only contains a backpointer to the overall atomic update,327* and the relevant private object but in the future also might hold328* synchronization information similar to e.g. &drm_crtc.commit.329*/330struct drm_private_state {331/**332* @state: backpointer to global drm_atomic_state333*/334struct drm_atomic_state *state;335336/**337* @obj: backpointer to the private object338*/339struct drm_private_obj *obj;340};341342struct __drm_private_objs_state {343struct drm_private_obj *ptr;344struct drm_private_state *state, *old_state, *new_state;345};346347/**348* struct drm_atomic_state - Atomic commit structure349*350* This structure is the kernel counterpart of @drm_mode_atomic and represents351* an atomic commit that transitions from an old to a new display state. It352* contains all the objects affected by the atomic commit and both the new353* state structures and pointers to the old state structures for354* these.355*356* States are added to an atomic update by calling drm_atomic_get_crtc_state(),357* drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for358* private state structures, drm_atomic_get_private_obj_state().359*360* NOTE: struct drm_atomic_state first started as a single collection of361* entities state pointers (drm_plane_state, drm_crtc_state, etc.).362*363* At atomic_check time, you could get the state about to be committed364* from drm_atomic_state, and the one currently running from the365* entities state pointer (drm_crtc.state, for example). After the call366* to drm_atomic_helper_swap_state(), the entities state pointer would367* contain the state previously checked, and the drm_atomic_state368* structure the old state.369*370* Over time, and in order to avoid confusion, drm_atomic_state has371* grown to have both the old state (ie, the state we replace) and the372* new state (ie, the state we want to apply). Those names are stable373* during the commit process, which makes it easier to reason about.374*375* You can still find some traces of that evolution through some hooks376* or callbacks taking a drm_atomic_state parameter called names like377* "old_state". This doesn't necessarily mean that the previous378* drm_atomic_state is passed, but rather that this used to be the state379* collection we were replacing after drm_atomic_helper_swap_state(),380* but the variable name was never updated.381*382* Some atomic operations implementations followed a similar process. We383* first started to pass the entity state only. However, it was pretty384* cumbersome for drivers, and especially CRTCs, to retrieve the states385* of other components. Thus, we switched to passing the whole386* drm_atomic_state as a parameter to those operations. Similarly, the387* transition isn't complete yet, and one might still find atomic388* operations taking a drm_atomic_state pointer, or a component state389* pointer. The former is the preferred form.390*/391struct drm_atomic_state {392/**393* @ref:394*395* Count of all references to this update (will not be freed until zero).396*/397struct kref ref;398399/**400* @dev: Parent DRM Device.401*/402struct drm_device *dev;403404/**405* @allow_modeset:406*407* Allow full modeset. This is used by the ATOMIC IOCTL handler to408* implement the DRM_MODE_ATOMIC_ALLOW_MODESET flag. Drivers should409* generally not consult this flag, but instead look at the output of410* drm_atomic_crtc_needs_modeset(). The detailed rules are:411*412* - Drivers must not consult @allow_modeset in the atomic commit path.413* Use drm_atomic_crtc_needs_modeset() instead.414*415* - Drivers must consult @allow_modeset before adding unrelated struct416* drm_crtc_state to this commit by calling417* drm_atomic_get_crtc_state(). See also the warning in the418* documentation for that function.419*420* - Drivers must never change this flag, it is under the exclusive421* control of userspace.422*423* - Drivers may consult @allow_modeset in the atomic check path, if424* they have the choice between an optimal hardware configuration425* which requires a modeset, and a less optimal configuration which426* can be committed without a modeset. An example would be suboptimal427* scanout FIFO allocation resulting in increased idle power428* consumption. This allows userspace to avoid flickering and delays429* for the normal composition loop at reasonable cost.430*/431bool allow_modeset : 1;432/**433* @legacy_cursor_update:434*435* Hint to enforce legacy cursor IOCTL semantics.436*437* WARNING: This is thoroughly broken and pretty much impossible to438* implement correctly. Drivers must ignore this and should instead439* implement &drm_plane_helper_funcs.atomic_async_check and440* &drm_plane_helper_funcs.atomic_async_commit hooks. New users of this441* flag are not allowed.442*/443bool legacy_cursor_update : 1;444445/**446* @async_update: hint for asynchronous plane update447*/448bool async_update : 1;449450/**451* @duplicated:452*453* Indicates whether or not this atomic state was duplicated using454* drm_atomic_helper_duplicate_state(). Drivers and atomic helpers455* should use this to fixup normal inconsistencies in duplicated456* states.457*/458bool duplicated : 1;459460/**461* @planes:462*463* Pointer to array of @drm_plane and @drm_plane_state part of this464* update.465*/466struct __drm_planes_state *planes;467468/**469* @crtcs:470*471* Pointer to array of @drm_crtc and @drm_crtc_state part of this472* update.473*/474struct __drm_crtcs_state *crtcs;475476/**477* @num_connector: size of the @connectors array478*/479int num_connector;480481/**482* @connectors:483*484* Pointer to array of @drm_connector and @drm_connector_state part of485* this update.486*/487struct __drm_connnectors_state *connectors;488489/**490* @num_private_objs: size of the @private_objs array491*/492int num_private_objs;493494/**495* @private_objs:496*497* Pointer to array of @drm_private_obj and @drm_private_obj_state part498* of this update.499*/500struct __drm_private_objs_state *private_objs;501502/**503* @acquire_ctx: acquire context for this atomic modeset state update504*/505struct drm_modeset_acquire_ctx *acquire_ctx;506507/**508* @fake_commit:509*510* Used for signaling unbound planes/connectors.511* When a connector or plane is not bound to any CRTC, it's still important512* to preserve linearity to prevent the atomic states from being freed too early.513*514* This commit (if set) is not bound to any CRTC, but will be completed when515* drm_atomic_helper_commit_hw_done() is called.516*/517struct drm_crtc_commit *fake_commit;518519/**520* @commit_work:521*522* Work item which can be used by the driver or helpers to execute the523* commit without blocking.524*/525struct work_struct commit_work;526};527528void __drm_crtc_commit_free(struct kref *kref);529530/**531* drm_crtc_commit_get - acquire a reference to the CRTC commit532* @commit: CRTC commit533*534* Increases the reference of @commit.535*536* Returns:537* The pointer to @commit, with reference increased.538*/539static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)540{541kref_get(&commit->ref);542return commit;543}544545/**546* drm_crtc_commit_put - release a reference to the CRTC commmit547* @commit: CRTC commit548*549* This releases a reference to @commit which is freed after removing the550* final reference. No locking required and callable from any context.551*/552static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)553{554kref_put(&commit->ref, __drm_crtc_commit_free);555}556557int drm_crtc_commit_wait(struct drm_crtc_commit *commit);558559struct drm_atomic_state * __must_check560drm_atomic_state_alloc(struct drm_device *dev);561void drm_atomic_state_clear(struct drm_atomic_state *state);562563/**564* drm_atomic_state_get - acquire a reference to the atomic state565* @state: The atomic state566*567* Returns a new reference to the @state568*/569static inline struct drm_atomic_state *570drm_atomic_state_get(struct drm_atomic_state *state)571{572kref_get(&state->ref);573return state;574}575576void __drm_atomic_state_free(struct kref *ref);577578/**579* drm_atomic_state_put - release a reference to the atomic state580* @state: The atomic state581*582* This releases a reference to @state which is freed after removing the583* final reference. No locking required and callable from any context.584*/585static inline void drm_atomic_state_put(struct drm_atomic_state *state)586{587kref_put(&state->ref, __drm_atomic_state_free);588}589590int __must_check591drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);592void drm_atomic_state_default_clear(struct drm_atomic_state *state);593void drm_atomic_state_default_release(struct drm_atomic_state *state);594595struct drm_crtc_state * __must_check596drm_atomic_get_crtc_state(struct drm_atomic_state *state,597struct drm_crtc *crtc);598struct drm_plane_state * __must_check599drm_atomic_get_plane_state(struct drm_atomic_state *state,600struct drm_plane *plane);601struct drm_connector_state * __must_check602drm_atomic_get_connector_state(struct drm_atomic_state *state,603struct drm_connector *connector);604605void drm_atomic_private_obj_init(struct drm_device *dev,606struct drm_private_obj *obj,607struct drm_private_state *state,608const struct drm_private_state_funcs *funcs);609void drm_atomic_private_obj_fini(struct drm_private_obj *obj);610611struct drm_private_state * __must_check612drm_atomic_get_private_obj_state(struct drm_atomic_state *state,613struct drm_private_obj *obj);614struct drm_private_state *615drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,616struct drm_private_obj *obj);617struct drm_private_state *618drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,619struct drm_private_obj *obj);620621struct drm_connector *622drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,623struct drm_encoder *encoder);624struct drm_connector *625drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,626struct drm_encoder *encoder);627struct drm_connector *628drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,629struct drm_modeset_acquire_ctx *ctx);630631struct drm_crtc *632drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,633struct drm_encoder *encoder);634struct drm_crtc *635drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,636struct drm_encoder *encoder);637638/**639* drm_atomic_get_existing_crtc_state - get CRTC state, if it exists640* @state: global atomic state object641* @crtc: CRTC to grab642*643* This function returns the CRTC state for the given CRTC, or NULL644* if the CRTC is not part of the global atomic state.645*646* This function is deprecated, @drm_atomic_get_old_crtc_state or647* @drm_atomic_get_new_crtc_state should be used instead.648*/649static inline struct drm_crtc_state *650drm_atomic_get_existing_crtc_state(const struct drm_atomic_state *state,651struct drm_crtc *crtc)652{653return state->crtcs[drm_crtc_index(crtc)].state;654}655656/**657* drm_atomic_get_old_crtc_state - get old CRTC state, if it exists658* @state: global atomic state object659* @crtc: CRTC to grab660*661* This function returns the old CRTC state for the given CRTC, or662* NULL if the CRTC is not part of the global atomic state.663*/664static inline struct drm_crtc_state *665drm_atomic_get_old_crtc_state(const struct drm_atomic_state *state,666struct drm_crtc *crtc)667{668return state->crtcs[drm_crtc_index(crtc)].old_state;669}670/**671* drm_atomic_get_new_crtc_state - get new CRTC state, if it exists672* @state: global atomic state object673* @crtc: CRTC to grab674*675* This function returns the new CRTC state for the given CRTC, or676* NULL if the CRTC is not part of the global atomic state.677*/678static inline struct drm_crtc_state *679drm_atomic_get_new_crtc_state(const struct drm_atomic_state *state,680struct drm_crtc *crtc)681{682return state->crtcs[drm_crtc_index(crtc)].new_state;683}684685/**686* drm_atomic_get_existing_plane_state - get plane state, if it exists687* @state: global atomic state object688* @plane: plane to grab689*690* This function returns the plane state for the given plane, or NULL691* if the plane is not part of the global atomic state.692*693* This function is deprecated, @drm_atomic_get_old_plane_state or694* @drm_atomic_get_new_plane_state should be used instead.695*/696static inline struct drm_plane_state *697drm_atomic_get_existing_plane_state(const struct drm_atomic_state *state,698struct drm_plane *plane)699{700return state->planes[drm_plane_index(plane)].state;701}702703/**704* drm_atomic_get_old_plane_state - get plane state, if it exists705* @state: global atomic state object706* @plane: plane to grab707*708* This function returns the old plane state for the given plane, or709* NULL if the plane is not part of the global atomic state.710*/711static inline struct drm_plane_state *712drm_atomic_get_old_plane_state(const struct drm_atomic_state *state,713struct drm_plane *plane)714{715return state->planes[drm_plane_index(plane)].old_state;716}717718/**719* drm_atomic_get_new_plane_state - get plane state, if it exists720* @state: global atomic state object721* @plane: plane to grab722*723* This function returns the new plane state for the given plane, or724* NULL if the plane is not part of the global atomic state.725*/726static inline struct drm_plane_state *727drm_atomic_get_new_plane_state(const struct drm_atomic_state *state,728struct drm_plane *plane)729{730return state->planes[drm_plane_index(plane)].new_state;731}732733/**734* drm_atomic_get_existing_connector_state - get connector state, if it exists735* @state: global atomic state object736* @connector: connector to grab737*738* This function returns the connector state for the given connector,739* or NULL if the connector is not part of the global atomic state.740*741* This function is deprecated, @drm_atomic_get_old_connector_state or742* @drm_atomic_get_new_connector_state should be used instead.743*/744static inline struct drm_connector_state *745drm_atomic_get_existing_connector_state(const struct drm_atomic_state *state,746struct drm_connector *connector)747{748int index = drm_connector_index(connector);749750if (index >= state->num_connector)751return NULL;752753return state->connectors[index].state;754}755756/**757* drm_atomic_get_old_connector_state - get connector state, if it exists758* @state: global atomic state object759* @connector: connector to grab760*761* This function returns the old connector state for the given connector,762* or NULL if the connector is not part of the global atomic state.763*/764static inline struct drm_connector_state *765drm_atomic_get_old_connector_state(const struct drm_atomic_state *state,766struct drm_connector *connector)767{768int index = drm_connector_index(connector);769770if (index >= state->num_connector)771return NULL;772773return state->connectors[index].old_state;774}775776/**777* drm_atomic_get_new_connector_state - get connector state, if it exists778* @state: global atomic state object779* @connector: connector to grab780*781* This function returns the new connector state for the given connector,782* or NULL if the connector is not part of the global atomic state.783*/784static inline struct drm_connector_state *785drm_atomic_get_new_connector_state(const struct drm_atomic_state *state,786struct drm_connector *connector)787{788int index = drm_connector_index(connector);789790if (index >= state->num_connector)791return NULL;792793return state->connectors[index].new_state;794}795796/**797* __drm_atomic_get_current_plane_state - get current plane state798* @state: global atomic state object799* @plane: plane to grab800*801* This function returns the plane state for the given plane, either from802* @state, or if the plane isn't part of the atomic state update, from @plane.803* This is useful in atomic check callbacks, when drivers need to peek at, but804* not change, state of other planes, since it avoids threading an error code805* back up the call chain.806*807* WARNING:808*809* Note that this function is in general unsafe since it doesn't check for the810* required locking for access state structures. Drivers must ensure that it is811* safe to access the returned state structure through other means. One common812* example is when planes are fixed to a single CRTC, and the driver knows that813* the CRTC lock is held already. In that case holding the CRTC lock gives a814* read-lock on all planes connected to that CRTC. But if planes can be815* reassigned things get more tricky. In that case it's better to use816* drm_atomic_get_plane_state and wire up full error handling.817*818* Returns:819*820* Read-only pointer to the current plane state.821*/822static inline const struct drm_plane_state *823__drm_atomic_get_current_plane_state(const struct drm_atomic_state *state,824struct drm_plane *plane)825{826if (state->planes[drm_plane_index(plane)].state)827return state->planes[drm_plane_index(plane)].state;828829return plane->state;830}831832int __must_check833drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,834struct drm_encoder *encoder);835int __must_check836drm_atomic_add_affected_connectors(struct drm_atomic_state *state,837struct drm_crtc *crtc);838int __must_check839drm_atomic_add_affected_planes(struct drm_atomic_state *state,840struct drm_crtc *crtc);841842int __must_check drm_atomic_check_only(struct drm_atomic_state *state);843int __must_check drm_atomic_commit(struct drm_atomic_state *state);844int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);845846void drm_state_dump(struct drm_device *dev, struct drm_printer *p);847848/**849* for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update850* @__state: &struct drm_atomic_state pointer851* @connector: &struct drm_connector iteration cursor852* @old_connector_state: &struct drm_connector_state iteration cursor for the853* old state854* @new_connector_state: &struct drm_connector_state iteration cursor for the855* new state856* @__i: int iteration cursor, for macro-internal use857*858* This iterates over all connectors in an atomic update, tracking both old and859* new state. This is useful in places where the state delta needs to be860* considered, for example in atomic check functions.861*/862#define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \863for ((__i) = 0; \864(__i) < (__state)->num_connector; \865(__i)++) \866for_each_if ((__state)->connectors[__i].ptr && \867((connector) = (__state)->connectors[__i].ptr, \868(void)(connector) /* Only to avoid unused-but-set-variable warning */, \869(old_connector_state) = (__state)->connectors[__i].old_state, \870(new_connector_state) = (__state)->connectors[__i].new_state, 1))871872/**873* for_each_old_connector_in_state - iterate over all connectors in an atomic update874* @__state: &struct drm_atomic_state pointer875* @connector: &struct drm_connector iteration cursor876* @old_connector_state: &struct drm_connector_state iteration cursor for the877* old state878* @__i: int iteration cursor, for macro-internal use879*880* This iterates over all connectors in an atomic update, tracking only the old881* state. This is useful in disable functions, where we need the old state the882* hardware is still in.883*/884#define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \885for ((__i) = 0; \886(__i) < (__state)->num_connector; \887(__i)++) \888for_each_if ((__state)->connectors[__i].ptr && \889((connector) = (__state)->connectors[__i].ptr, \890(void)(connector) /* Only to avoid unused-but-set-variable warning */, \891(old_connector_state) = (__state)->connectors[__i].old_state, 1))892893/**894* for_each_new_connector_in_state - iterate over all connectors in an atomic update895* @__state: &struct drm_atomic_state pointer896* @connector: &struct drm_connector iteration cursor897* @new_connector_state: &struct drm_connector_state iteration cursor for the898* new state899* @__i: int iteration cursor, for macro-internal use900*901* This iterates over all connectors in an atomic update, tracking only the new902* state. This is useful in enable functions, where we need the new state the903* hardware should be in when the atomic commit operation has completed.904*/905#define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \906for ((__i) = 0; \907(__i) < (__state)->num_connector; \908(__i)++) \909for_each_if ((__state)->connectors[__i].ptr && \910((connector) = (__state)->connectors[__i].ptr, \911(void)(connector) /* Only to avoid unused-but-set-variable warning */, \912(new_connector_state) = (__state)->connectors[__i].new_state, \913(void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))914915/**916* for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update917* @__state: &struct drm_atomic_state pointer918* @crtc: &struct drm_crtc iteration cursor919* @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state920* @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state921* @__i: int iteration cursor, for macro-internal use922*923* This iterates over all CRTCs in an atomic update, tracking both old and924* new state. This is useful in places where the state delta needs to be925* considered, for example in atomic check functions.926*/927#define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \928for ((__i) = 0; \929(__i) < (__state)->dev->mode_config.num_crtc; \930(__i)++) \931for_each_if ((__state)->crtcs[__i].ptr && \932((crtc) = (__state)->crtcs[__i].ptr, \933(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \934(old_crtc_state) = (__state)->crtcs[__i].old_state, \935(void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \936(new_crtc_state) = (__state)->crtcs[__i].new_state, \937(void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))938939/**940* for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update941* @__state: &struct drm_atomic_state pointer942* @crtc: &struct drm_crtc iteration cursor943* @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state944* @__i: int iteration cursor, for macro-internal use945*946* This iterates over all CRTCs in an atomic update, tracking only the old947* state. This is useful in disable functions, where we need the old state the948* hardware is still in.949*/950#define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i) \951for ((__i) = 0; \952(__i) < (__state)->dev->mode_config.num_crtc; \953(__i)++) \954for_each_if ((__state)->crtcs[__i].ptr && \955((crtc) = (__state)->crtcs[__i].ptr, \956(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \957(old_crtc_state) = (__state)->crtcs[__i].old_state, 1))958959/**960* for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update961* @__state: &struct drm_atomic_state pointer962* @crtc: &struct drm_crtc iteration cursor963* @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state964* @__i: int iteration cursor, for macro-internal use965*966* This iterates over all CRTCs in an atomic update, tracking only the new967* state. This is useful in enable functions, where we need the new state the968* hardware should be in when the atomic commit operation has completed.969*/970#define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i) \971for ((__i) = 0; \972(__i) < (__state)->dev->mode_config.num_crtc; \973(__i)++) \974for_each_if ((__state)->crtcs[__i].ptr && \975((crtc) = (__state)->crtcs[__i].ptr, \976(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \977(new_crtc_state) = (__state)->crtcs[__i].new_state, \978(void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))979980/**981* for_each_oldnew_plane_in_state - iterate over all planes in an atomic update982* @__state: &struct drm_atomic_state pointer983* @plane: &struct drm_plane iteration cursor984* @old_plane_state: &struct drm_plane_state iteration cursor for the old state985* @new_plane_state: &struct drm_plane_state iteration cursor for the new state986* @__i: int iteration cursor, for macro-internal use987*988* This iterates over all planes in an atomic update, tracking both old and989* new state. This is useful in places where the state delta needs to be990* considered, for example in atomic check functions.991*/992#define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \993for ((__i) = 0; \994(__i) < (__state)->dev->mode_config.num_total_plane; \995(__i)++) \996for_each_if ((__state)->planes[__i].ptr && \997((plane) = (__state)->planes[__i].ptr, \998(void)(plane) /* Only to avoid unused-but-set-variable warning */, \999(old_plane_state) = (__state)->planes[__i].old_state,\1000(new_plane_state) = (__state)->planes[__i].new_state, 1))10011002/**1003* for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic1004* update in reverse order1005* @__state: &struct drm_atomic_state pointer1006* @plane: &struct drm_plane iteration cursor1007* @old_plane_state: &struct drm_plane_state iteration cursor for the old state1008* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1009* @__i: int iteration cursor, for macro-internal use1010*1011* This iterates over all planes in an atomic update in reverse order,1012* tracking both old and new state. This is useful in places where the1013* state delta needs to be considered, for example in atomic check functions.1014*/1015#define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \1016for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \1017(__i) >= 0; \1018(__i)--) \1019for_each_if ((__state)->planes[__i].ptr && \1020((plane) = (__state)->planes[__i].ptr, \1021(old_plane_state) = (__state)->planes[__i].old_state,\1022(new_plane_state) = (__state)->planes[__i].new_state, 1))10231024/**1025* for_each_new_plane_in_state_reverse - other than only tracking new state,1026* it's the same as for_each_oldnew_plane_in_state_reverse1027* @__state: &struct drm_atomic_state pointer1028* @plane: &struct drm_plane iteration cursor1029* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1030* @__i: int iteration cursor, for macro-internal use1031*/1032#define for_each_new_plane_in_state_reverse(__state, plane, new_plane_state, __i) \1033for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \1034(__i) >= 0; \1035(__i)--) \1036for_each_if ((__state)->planes[__i].ptr && \1037((plane) = (__state)->planes[__i].ptr, \1038(new_plane_state) = (__state)->planes[__i].new_state, 1))10391040/**1041* for_each_old_plane_in_state - iterate over all planes in an atomic update1042* @__state: &struct drm_atomic_state pointer1043* @plane: &struct drm_plane iteration cursor1044* @old_plane_state: &struct drm_plane_state iteration cursor for the old state1045* @__i: int iteration cursor, for macro-internal use1046*1047* This iterates over all planes in an atomic update, tracking only the old1048* state. This is useful in disable functions, where we need the old state the1049* hardware is still in.1050*/1051#define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \1052for ((__i) = 0; \1053(__i) < (__state)->dev->mode_config.num_total_plane; \1054(__i)++) \1055for_each_if ((__state)->planes[__i].ptr && \1056((plane) = (__state)->planes[__i].ptr, \1057(old_plane_state) = (__state)->planes[__i].old_state, 1))1058/**1059* for_each_new_plane_in_state - iterate over all planes in an atomic update1060* @__state: &struct drm_atomic_state pointer1061* @plane: &struct drm_plane iteration cursor1062* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1063* @__i: int iteration cursor, for macro-internal use1064*1065* This iterates over all planes in an atomic update, tracking only the new1066* state. This is useful in enable functions, where we need the new state the1067* hardware should be in when the atomic commit operation has completed.1068*/1069#define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \1070for ((__i) = 0; \1071(__i) < (__state)->dev->mode_config.num_total_plane; \1072(__i)++) \1073for_each_if ((__state)->planes[__i].ptr && \1074((plane) = (__state)->planes[__i].ptr, \1075(void)(plane) /* Only to avoid unused-but-set-variable warning */, \1076(new_plane_state) = (__state)->planes[__i].new_state, \1077(void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))10781079/**1080* for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update1081* @__state: &struct drm_atomic_state pointer1082* @obj: &struct drm_private_obj iteration cursor1083* @old_obj_state: &struct drm_private_state iteration cursor for the old state1084* @new_obj_state: &struct drm_private_state iteration cursor for the new state1085* @__i: int iteration cursor, for macro-internal use1086*1087* This iterates over all private objects in an atomic update, tracking both1088* old and new state. This is useful in places where the state delta needs1089* to be considered, for example in atomic check functions.1090*/1091#define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \1092for ((__i) = 0; \1093(__i) < (__state)->num_private_objs && \1094((obj) = (__state)->private_objs[__i].ptr, \1095(old_obj_state) = (__state)->private_objs[__i].old_state, \1096(new_obj_state) = (__state)->private_objs[__i].new_state, 1); \1097(__i)++)10981099/**1100* for_each_old_private_obj_in_state - iterate over all private objects in an atomic update1101* @__state: &struct drm_atomic_state pointer1102* @obj: &struct drm_private_obj iteration cursor1103* @old_obj_state: &struct drm_private_state iteration cursor for the old state1104* @__i: int iteration cursor, for macro-internal use1105*1106* This iterates over all private objects in an atomic update, tracking only1107* the old state. This is useful in disable functions, where we need the old1108* state the hardware is still in.1109*/1110#define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \1111for ((__i) = 0; \1112(__i) < (__state)->num_private_objs && \1113((obj) = (__state)->private_objs[__i].ptr, \1114(old_obj_state) = (__state)->private_objs[__i].old_state, 1); \1115(__i)++)11161117/**1118* for_each_new_private_obj_in_state - iterate over all private objects in an atomic update1119* @__state: &struct drm_atomic_state pointer1120* @obj: &struct drm_private_obj iteration cursor1121* @new_obj_state: &struct drm_private_state iteration cursor for the new state1122* @__i: int iteration cursor, for macro-internal use1123*1124* This iterates over all private objects in an atomic update, tracking only1125* the new state. This is useful in enable functions, where we need the new state the1126* hardware should be in when the atomic commit operation has completed.1127*/1128#define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \1129for ((__i) = 0; \1130(__i) < (__state)->num_private_objs && \1131((obj) = (__state)->private_objs[__i].ptr, \1132(void)(obj) /* Only to avoid unused-but-set-variable warning */, \1133(new_obj_state) = (__state)->private_objs[__i].new_state, 1); \1134(__i)++)11351136/**1137* drm_atomic_crtc_needs_modeset - compute combined modeset need1138* @state: &drm_crtc_state for the CRTC1139*1140* To give drivers flexibility &struct drm_crtc_state has 3 booleans to track1141* whether the state CRTC changed enough to need a full modeset cycle:1142* mode_changed, active_changed and connectors_changed. This helper simply1143* combines these three to compute the overall need for a modeset for @state.1144*1145* The atomic helper code sets these booleans, but drivers can and should1146* change them appropriately to accurately represent whether a modeset is1147* really needed. In general, drivers should avoid full modesets whenever1148* possible.1149*1150* For example if the CRTC mode has changed, and the hardware is able to enact1151* the requested mode change without going through a full modeset, the driver1152* should clear mode_changed in its &drm_mode_config_funcs.atomic_check1153* implementation.1154*/1155static inline bool1156drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)1157{1158return state->mode_changed || state->active_changed ||1159state->connectors_changed;1160}11611162/**1163* drm_atomic_crtc_effectively_active - compute whether CRTC is actually active1164* @state: &drm_crtc_state for the CRTC1165*1166* When in self refresh mode, the crtc_state->active value will be false, since1167* the CRTC is off. However in some cases we're interested in whether the CRTC1168* is active, or effectively active (ie: it's connected to an active display).1169* In these cases, use this function instead of just checking active.1170*/1171static inline bool1172drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)1173{1174return state->active || state->self_refresh_active;1175}11761177/**1178* struct drm_bus_cfg - bus configuration1179*1180* This structure stores the configuration of a physical bus between two1181* components in an output pipeline, usually between two bridges, an encoder1182* and a bridge, or a bridge and a connector.1183*1184* The bus configuration is stored in &drm_bridge_state separately for the1185* input and output buses, as seen from the point of view of each bridge. The1186* bus configuration of a bridge output is usually identical to the1187* configuration of the next bridge's input, but may differ if the signals are1188* modified between the two bridges, for instance by an inverter on the board.1189* The input and output configurations of a bridge may differ if the bridge1190* modifies the signals internally, for instance by performing format1191* conversion, or modifying signals polarities.1192*/1193struct drm_bus_cfg {1194/**1195* @format: format used on this bus (one of the MEDIA_BUS_FMT_* format)1196*1197* This field should not be directly modified by drivers1198* (drm_atomic_bridge_chain_select_bus_fmts() takes care of the bus1199* format negotiation).1200*/1201u32 format;12021203/**1204* @flags: DRM_BUS_* flags used on this bus1205*/1206u32 flags;1207};12081209/**1210* struct drm_bridge_state - Atomic bridge state object1211*/1212struct drm_bridge_state {1213/**1214* @base: inherit from &drm_private_state1215*/1216struct drm_private_state base;12171218/**1219* @bridge: the bridge this state refers to1220*/1221struct drm_bridge *bridge;12221223/**1224* @input_bus_cfg: input bus configuration1225*/1226struct drm_bus_cfg input_bus_cfg;12271228/**1229* @output_bus_cfg: output bus configuration1230*/1231struct drm_bus_cfg output_bus_cfg;1232};12331234static inline struct drm_bridge_state *1235drm_priv_to_bridge_state(struct drm_private_state *priv)1236{1237return container_of(priv, struct drm_bridge_state, base);1238}12391240struct drm_bridge_state *1241drm_atomic_get_bridge_state(struct drm_atomic_state *state,1242struct drm_bridge *bridge);1243struct drm_bridge_state *1244drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,1245struct drm_bridge *bridge);1246struct drm_bridge_state *1247drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,1248struct drm_bridge *bridge);12491250#endif /* DRM_ATOMIC_H_ */125112521253