/*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>32#include <drm/drm_colorop.h>3334/**35* struct drm_crtc_commit - track modeset commits on a CRTC36*37* This structure is used to track pending modeset changes and atomic commit on38* a per-CRTC basis. Since updating the list should never block, this structure39* is reference counted to allow waiters to safely wait on an event to complete,40* without holding any locks.41*42* It has 3 different events in total to allow a fine-grained synchronization43* between outstanding updates::44*45* atomic commit thread hardware46*47* write new state into hardware ----> ...48* signal hw_done49* switch to new state on next50* ... v/hblank51*52* wait for buffers to show up ...53*54* ... send completion irq55* irq handler signals flip_done56* cleanup old buffers57*58* signal cleanup_done59*60* wait for flip_done <----61* clean up atomic state62*63* The important bit to know is that &cleanup_done is the terminal event, but the64* ordering between &flip_done and &hw_done is entirely up to the specific driver65* and modeset state change.66*67* For an implementation of how to use this look at68* drm_atomic_helper_setup_commit() from the atomic helper library.69*70* See also drm_crtc_commit_wait().71*/72struct drm_crtc_commit {73/**74* @crtc:75*76* DRM CRTC for this commit.77*/78struct drm_crtc *crtc;7980/**81* @ref:82*83* Reference count for this structure. Needed to allow blocking on84* completions without the risk of the completion disappearing85* meanwhile.86*/87struct kref ref;8889/**90* @flip_done:91*92* Will be signaled when the hardware has flipped to the new set of93* buffers. Signals at the same time as when the drm event for this94* commit is sent to userspace, or when an out-fence is singalled. Note95* that for most hardware, in most cases this happens after @hw_done is96* signalled.97*98* Completion of this stage is signalled implicitly by calling99* drm_crtc_send_vblank_event() on &drm_crtc_state.event.100*/101struct completion flip_done;102103/**104* @hw_done:105*106* Will be signalled when all hw register changes for this commit have107* been written out. Especially when disabling a pipe this can be much108* later than @flip_done, since that can signal already when the109* screen goes black, whereas to fully shut down a pipe more register110* I/O is required.111*112* Note that this does not need to include separately reference-counted113* resources like backing storage buffer pinning, or runtime pm114* management.115*116* Drivers should call drm_atomic_helper_commit_hw_done() to signal117* completion of this stage.118*/119struct completion hw_done;120121/**122* @cleanup_done:123*124* Will be signalled after old buffers have been cleaned up by calling125* drm_atomic_helper_cleanup_planes(). Since this can only happen after126* a vblank wait completed it might be a bit later. This completion is127* useful to throttle updates and avoid hardware updates getting ahead128* of the buffer cleanup too much.129*130* Drivers should call drm_atomic_helper_commit_cleanup_done() to signal131* completion of this stage.132*/133struct completion cleanup_done;134135/**136* @commit_entry:137*138* Entry on the per-CRTC &drm_crtc.commit_list. Protected by139* $drm_crtc.commit_lock.140*/141struct list_head commit_entry;142143/**144* @event:145*146* &drm_pending_vblank_event pointer to clean up private events.147*/148struct drm_pending_vblank_event *event;149150/**151* @abort_completion:152*153* A flag that's set after drm_atomic_helper_setup_commit() takes a154* second reference for the completion of $drm_crtc_state.event. It's155* used by the free code to remove the second reference if commit fails.156*/157bool abort_completion;158};159160struct __drm_colorops_state {161struct drm_colorop *ptr;162struct drm_colorop_state *state, *old_state, *new_state;163};164165struct __drm_planes_state {166struct drm_plane *ptr;167168/**169* @state_to_destroy:170*171* Used to track the @drm_plane_state we will need to free when172* tearing down the associated &drm_atomic_state in173* $drm_mode_config_funcs.atomic_state_clear or174* drm_atomic_state_default_clear().175*176* Before a commit, and the call to177* drm_atomic_helper_swap_state() in particular, it points to178* the same state than @new_state. After a commit, it points to179* the same state than @old_state.180*/181struct drm_plane_state *state_to_destroy;182183struct drm_plane_state *old_state, *new_state;184};185186struct __drm_crtcs_state {187struct drm_crtc *ptr;188189/**190* @state_to_destroy:191*192* Used to track the @drm_crtc_state we will need to free when193* tearing down the associated &drm_atomic_state in194* $drm_mode_config_funcs.atomic_state_clear or195* drm_atomic_state_default_clear().196*197* Before a commit, and the call to198* drm_atomic_helper_swap_state() in particular, it points to199* the same state than @new_state. After a commit, it points to200* the same state than @old_state.201*/202struct drm_crtc_state *state_to_destroy;203204struct drm_crtc_state *old_state, *new_state;205206/**207* @commit:208*209* A reference to the CRTC commit object that is kept for use by210* drm_atomic_helper_wait_for_flip_done() after211* drm_atomic_helper_commit_hw_done() is called. This ensures that a212* concurrent commit won't free a commit object that is still in use.213*/214struct drm_crtc_commit *commit;215216s32 __user *out_fence_ptr;217u64 last_vblank_count;218};219220struct __drm_connnectors_state {221struct drm_connector *ptr;222223/**224* @state_to_destroy:225*226* Used to track the @drm_connector_state we will need to free227* when tearing down the associated &drm_atomic_state in228* $drm_mode_config_funcs.atomic_state_clear or229* drm_atomic_state_default_clear().230*231* Before a commit, and the call to232* drm_atomic_helper_swap_state() in particular, it points to233* the same state than @new_state. After a commit, it points to234* the same state than @old_state.235*/236struct drm_connector_state *state_to_destroy;237238struct drm_connector_state *old_state, *new_state;239240/**241* @out_fence_ptr:242*243* User-provided pointer which the kernel uses to return a sync_file244* file descriptor. Used by writeback connectors to signal completion of245* the writeback.246*/247s32 __user *out_fence_ptr;248};249250struct drm_private_obj;251struct drm_private_state;252253/**254* struct drm_private_state_funcs - atomic state functions for private objects255*256* These hooks are used by atomic helpers to create, swap and destroy states of257* private objects. The structure itself is used as a vtable to identify the258* associated private object type. Each private object type that needs to be259* added to the atomic states is expected to have an implementation of these260* hooks and pass a pointer to its drm_private_state_funcs struct to261* drm_atomic_get_private_obj_state().262*/263struct drm_private_state_funcs {264/**265* @atomic_duplicate_state:266*267* Duplicate the current state of the private object and return it. It268* is an error to call this before obj->state has been initialized.269*270* RETURNS:271*272* Duplicated atomic state or NULL when obj->state is not273* initialized or allocation failed.274*/275struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);276277/**278* @atomic_destroy_state:279*280* Frees the private object state created with @atomic_duplicate_state.281*/282void (*atomic_destroy_state)(struct drm_private_obj *obj,283struct drm_private_state *state);284285/**286* @atomic_print_state:287*288* If driver subclasses &struct drm_private_state, it should implement289* this optional hook for printing additional driver specific state.290*291* Do not call this directly, use drm_atomic_private_obj_print_state()292* instead.293*/294void (*atomic_print_state)(struct drm_printer *p,295const struct drm_private_state *state);296};297298/**299* struct drm_private_obj - base struct for driver private atomic object300*301* A driver private object is initialized by calling302* drm_atomic_private_obj_init() and cleaned up by calling303* drm_atomic_private_obj_fini().304*305* Currently only tracks the state update functions and the opaque driver306* private state itself, but in the future might also track which307* &drm_modeset_lock is required to duplicate and update this object's state.308*309* All private objects must be initialized before the DRM device they are310* attached to is registered to the DRM subsystem (call to drm_dev_register())311* and should stay around until this DRM device is unregistered (call to312* drm_dev_unregister()). In other words, private objects lifetime is tied313* to the DRM device lifetime. This implies that:314*315* 1/ all calls to drm_atomic_private_obj_init() must be done before calling316* drm_dev_register()317* 2/ all calls to drm_atomic_private_obj_fini() must be done after calling318* drm_dev_unregister()319*320* If that private object is used to store a state shared by multiple321* CRTCs, proper care must be taken to ensure that non-blocking commits are322* properly ordered to avoid a use-after-free issue.323*324* Indeed, assuming a sequence of two non-blocking &drm_atomic_commit on two325* different &drm_crtc using different &drm_plane and &drm_connector, so with no326* resources shared, there's no guarantee on which commit is going to happen327* first. However, the second &drm_atomic_commit will consider the first328* &drm_private_obj its old state, and will be in charge of freeing it whenever329* the second &drm_atomic_commit is done.330*331* If the first &drm_atomic_commit happens after it, it will consider its332* &drm_private_obj the new state and will be likely to access it, resulting in333* an access to a freed memory region. Drivers should store (and get a reference334* to) the &drm_crtc_commit structure in our private state in335* &drm_mode_config_helper_funcs.atomic_commit_setup, and then wait for that336* commit to complete as the first step of337* &drm_mode_config_helper_funcs.atomic_commit_tail, similar to338* drm_atomic_helper_wait_for_dependencies().339*/340struct drm_private_obj {341/**342* @head: List entry used to attach a private object to a &drm_device343* (queued to &drm_mode_config.privobj_list).344*/345struct list_head head;346347/**348* @lock: Modeset lock to protect the state object.349*/350struct drm_modeset_lock lock;351352/**353* @state: Current atomic state for this driver private object.354*/355struct drm_private_state *state;356357/**358* @funcs:359*360* Functions to manipulate the state of this driver private object, see361* &drm_private_state_funcs.362*/363const struct drm_private_state_funcs *funcs;364};365366/**367* drm_for_each_privobj() - private object iterator368*369* @privobj: pointer to the current private object. Updated after each370* iteration371* @dev: the DRM device we want get private objects from372*373* Allows one to iterate over all private objects attached to @dev374*/375#define drm_for_each_privobj(privobj, dev) \376list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)377378/**379* struct drm_private_state - base struct for driver private object state380*381* Currently only contains a backpointer to the overall atomic update,382* and the relevant private object but in the future also might hold383* synchronization information similar to e.g. &drm_crtc.commit.384*/385struct drm_private_state {386/**387* @state: backpointer to global drm_atomic_state388*/389struct drm_atomic_state *state;390391/**392* @obj: backpointer to the private object393*/394struct drm_private_obj *obj;395};396397struct __drm_private_objs_state {398struct drm_private_obj *ptr;399400/**401* @state_to_destroy:402*403* Used to track the @drm_private_state we will need to free404* when tearing down the associated &drm_atomic_state in405* $drm_mode_config_funcs.atomic_state_clear or406* drm_atomic_state_default_clear().407*408* Before a commit, and the call to409* drm_atomic_helper_swap_state() in particular, it points to410* the same state than @new_state. After a commit, it points to411* the same state than @old_state.412*/413struct drm_private_state *state_to_destroy;414415struct drm_private_state *old_state, *new_state;416};417418/**419* struct drm_atomic_state - Atomic commit structure420*421* This structure is the kernel counterpart of @drm_mode_atomic and represents422* an atomic commit that transitions from an old to a new display state. It423* contains all the objects affected by the atomic commit and both the new424* state structures and pointers to the old state structures for425* these.426*427* States are added to an atomic update by calling drm_atomic_get_crtc_state(),428* drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for429* private state structures, drm_atomic_get_private_obj_state().430*431* NOTE: struct drm_atomic_state first started as a single collection of432* entities state pointers (drm_plane_state, drm_crtc_state, etc.).433*434* At atomic_check time, you could get the state about to be committed435* from drm_atomic_state, and the one currently running from the436* entities state pointer (drm_crtc.state, for example). After the call437* to drm_atomic_helper_swap_state(), the entities state pointer would438* contain the state previously checked, and the drm_atomic_state439* structure the old state.440*441* Over time, and in order to avoid confusion, drm_atomic_state has442* grown to have both the old state (ie, the state we replace) and the443* new state (ie, the state we want to apply). Those names are stable444* during the commit process, which makes it easier to reason about.445*446* You can still find some traces of that evolution through some hooks447* or callbacks taking a drm_atomic_state parameter called names like448* "old_state". This doesn't necessarily mean that the previous449* drm_atomic_state is passed, but rather that this used to be the state450* collection we were replacing after drm_atomic_helper_swap_state(),451* but the variable name was never updated.452*453* Some atomic operations implementations followed a similar process. We454* first started to pass the entity state only. However, it was pretty455* cumbersome for drivers, and especially CRTCs, to retrieve the states456* of other components. Thus, we switched to passing the whole457* drm_atomic_state as a parameter to those operations. Similarly, the458* transition isn't complete yet, and one might still find atomic459* operations taking a drm_atomic_state pointer, or a component state460* pointer. The former is the preferred form.461*/462struct drm_atomic_state {463/**464* @ref:465*466* Count of all references to this update (will not be freed until zero).467*/468struct kref ref;469470/**471* @dev: Parent DRM Device.472*/473struct drm_device *dev;474475/**476* @allow_modeset:477*478* Allow full modeset. This is used by the ATOMIC IOCTL handler to479* implement the DRM_MODE_ATOMIC_ALLOW_MODESET flag. Drivers should480* generally not consult this flag, but instead look at the output of481* drm_atomic_crtc_needs_modeset(). The detailed rules are:482*483* - Drivers must not consult @allow_modeset in the atomic commit path.484* Use drm_atomic_crtc_needs_modeset() instead.485*486* - Drivers must consult @allow_modeset before adding unrelated struct487* drm_crtc_state to this commit by calling488* drm_atomic_get_crtc_state(). See also the warning in the489* documentation for that function.490*491* - Drivers must never change this flag, it is under the exclusive492* control of userspace.493*494* - Drivers may consult @allow_modeset in the atomic check path, if495* they have the choice between an optimal hardware configuration496* which requires a modeset, and a less optimal configuration which497* can be committed without a modeset. An example would be suboptimal498* scanout FIFO allocation resulting in increased idle power499* consumption. This allows userspace to avoid flickering and delays500* for the normal composition loop at reasonable cost.501*/502bool allow_modeset : 1;503/**504* @legacy_cursor_update:505*506* Hint to enforce legacy cursor IOCTL semantics.507*508* WARNING: This is thoroughly broken and pretty much impossible to509* implement correctly. Drivers must ignore this and should instead510* implement &drm_plane_helper_funcs.atomic_async_check and511* &drm_plane_helper_funcs.atomic_async_commit hooks. New users of this512* flag are not allowed.513*/514bool legacy_cursor_update : 1;515516/**517* @async_update: hint for asynchronous plane update518*/519bool async_update : 1;520521/**522* @duplicated:523*524* Indicates whether or not this atomic state was duplicated using525* drm_atomic_helper_duplicate_state(). Drivers and atomic helpers526* should use this to fixup normal inconsistencies in duplicated527* states.528*/529bool duplicated : 1;530531/**532* @checked:533*534* Indicates the state has been checked and thus must no longer535* be mutated. For internal use only, do not consult from drivers.536*/537bool checked : 1;538539/**540* @plane_color_pipeline:541*542* Indicates whether this atomic state originated with a client that543* set the DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE.544*545* Drivers and helper functions should use this to ignore legacy546* properties that are incompatible with the drm_plane COLOR_PIPELINE547* behavior, such as:548*549* - COLOR_RANGE550* - COLOR_ENCODING551*552* or any other driver-specific properties that might affect pixel553* values.554*/555bool plane_color_pipeline : 1;556557/**558* @colorops:559*560* Pointer to array of @drm_colorop and @drm_colorop_state part of this561* update.562*/563struct __drm_colorops_state *colorops;564565/**566* @planes:567*568* Pointer to array of @drm_plane and @drm_plane_state part of this569* update.570*/571struct __drm_planes_state *planes;572573/**574* @crtcs:575*576* Pointer to array of @drm_crtc and @drm_crtc_state part of this577* update.578*/579struct __drm_crtcs_state *crtcs;580581/**582* @num_connector: size of the @connectors array583*/584int num_connector;585586/**587* @connectors:588*589* Pointer to array of @drm_connector and @drm_connector_state part of590* this update.591*/592struct __drm_connnectors_state *connectors;593594/**595* @num_private_objs: size of the @private_objs array596*/597int num_private_objs;598599/**600* @private_objs:601*602* Pointer to array of @drm_private_obj and @drm_private_obj_state part603* of this update.604*/605struct __drm_private_objs_state *private_objs;606607/**608* @acquire_ctx: acquire context for this atomic modeset state update609*/610struct drm_modeset_acquire_ctx *acquire_ctx;611612/**613* @fake_commit:614*615* Used for signaling unbound planes/connectors.616* When a connector or plane is not bound to any CRTC, it's still important617* to preserve linearity to prevent the atomic states from being freed too early.618*619* This commit (if set) is not bound to any CRTC, but will be completed when620* drm_atomic_helper_commit_hw_done() is called.621*/622struct drm_crtc_commit *fake_commit;623624/**625* @commit_work:626*627* Work item which can be used by the driver or helpers to execute the628* commit without blocking.629*/630struct work_struct commit_work;631};632633void __drm_crtc_commit_free(struct kref *kref);634635/**636* drm_crtc_commit_get - acquire a reference to the CRTC commit637* @commit: CRTC commit638*639* Increases the reference of @commit.640*641* Returns:642* The pointer to @commit, with reference increased.643*/644static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)645{646kref_get(&commit->ref);647return commit;648}649650/**651* drm_crtc_commit_put - release a reference to the CRTC commmit652* @commit: CRTC commit653*654* This releases a reference to @commit which is freed after removing the655* final reference. No locking required and callable from any context.656*/657static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)658{659kref_put(&commit->ref, __drm_crtc_commit_free);660}661662int drm_crtc_commit_wait(struct drm_crtc_commit *commit);663664struct drm_atomic_state * __must_check665drm_atomic_state_alloc(struct drm_device *dev);666void drm_atomic_state_clear(struct drm_atomic_state *state);667668/**669* drm_atomic_state_get - acquire a reference to the atomic state670* @state: The atomic state671*672* Returns a new reference to the @state673*/674static inline struct drm_atomic_state *675drm_atomic_state_get(struct drm_atomic_state *state)676{677kref_get(&state->ref);678return state;679}680681void __drm_atomic_state_free(struct kref *ref);682683/**684* drm_atomic_state_put - release a reference to the atomic state685* @state: The atomic state686*687* This releases a reference to @state which is freed after removing the688* final reference. No locking required and callable from any context.689*/690static inline void drm_atomic_state_put(struct drm_atomic_state *state)691{692kref_put(&state->ref, __drm_atomic_state_free);693}694695int __must_check696drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);697void drm_atomic_state_default_clear(struct drm_atomic_state *state);698void drm_atomic_state_default_release(struct drm_atomic_state *state);699700struct drm_crtc_state * __must_check701drm_atomic_get_crtc_state(struct drm_atomic_state *state,702struct drm_crtc *crtc);703struct drm_plane_state * __must_check704drm_atomic_get_plane_state(struct drm_atomic_state *state,705struct drm_plane *plane);706struct drm_colorop_state *707drm_atomic_get_colorop_state(struct drm_atomic_state *state,708struct drm_colorop *colorop);709struct drm_connector_state * __must_check710drm_atomic_get_connector_state(struct drm_atomic_state *state,711struct drm_connector *connector);712713void drm_atomic_private_obj_init(struct drm_device *dev,714struct drm_private_obj *obj,715struct drm_private_state *state,716const struct drm_private_state_funcs *funcs);717void drm_atomic_private_obj_fini(struct drm_private_obj *obj);718719struct drm_private_state * __must_check720drm_atomic_get_private_obj_state(struct drm_atomic_state *state,721struct drm_private_obj *obj);722struct drm_private_state *723drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,724struct drm_private_obj *obj);725struct drm_private_state *726drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,727struct drm_private_obj *obj);728729struct drm_connector *730drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,731struct drm_encoder *encoder);732struct drm_connector *733drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,734struct drm_encoder *encoder);735struct drm_connector *736drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,737struct drm_modeset_acquire_ctx *ctx);738739struct drm_crtc *740drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,741struct drm_encoder *encoder);742struct drm_crtc *743drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,744struct drm_encoder *encoder);745746/**747* drm_atomic_get_old_crtc_state - get old CRTC state, if it exists748* @state: global atomic state object749* @crtc: CRTC to grab750*751* This function returns the old CRTC state for the given CRTC, or752* NULL if the CRTC is not part of the global atomic state.753*/754static inline struct drm_crtc_state *755drm_atomic_get_old_crtc_state(const struct drm_atomic_state *state,756struct drm_crtc *crtc)757{758return state->crtcs[drm_crtc_index(crtc)].old_state;759}760/**761* drm_atomic_get_new_crtc_state - get new CRTC state, if it exists762* @state: global atomic state object763* @crtc: CRTC to grab764*765* This function returns the new CRTC state for the given CRTC, or766* NULL if the CRTC is not part of the global atomic state.767*/768static inline struct drm_crtc_state *769drm_atomic_get_new_crtc_state(const struct drm_atomic_state *state,770struct drm_crtc *crtc)771{772return state->crtcs[drm_crtc_index(crtc)].new_state;773}774775/**776* drm_atomic_get_old_plane_state - get plane state, if it exists777* @state: global atomic state object778* @plane: plane to grab779*780* This function returns the old plane state for the given plane, or781* NULL if the plane is not part of the global atomic state.782*/783static inline struct drm_plane_state *784drm_atomic_get_old_plane_state(const struct drm_atomic_state *state,785struct drm_plane *plane)786{787return state->planes[drm_plane_index(plane)].old_state;788}789790/**791* drm_atomic_get_new_plane_state - get plane state, if it exists792* @state: global atomic state object793* @plane: plane to grab794*795* This function returns the new plane state for the given plane, or796* NULL if the plane is not part of the global atomic state.797*/798static inline struct drm_plane_state *799drm_atomic_get_new_plane_state(const struct drm_atomic_state *state,800struct drm_plane *plane)801{802return state->planes[drm_plane_index(plane)].new_state;803}804805/**806* drm_atomic_get_old_colorop_state - get colorop state, if it exists807* @state: global atomic state object808* @colorop: colorop to grab809*810* This function returns the old colorop state for the given colorop, or811* NULL if the colorop is not part of the global atomic state.812*/813static inline struct drm_colorop_state *814drm_atomic_get_old_colorop_state(struct drm_atomic_state *state,815struct drm_colorop *colorop)816{817return state->colorops[drm_colorop_index(colorop)].old_state;818}819820/**821* drm_atomic_get_new_colorop_state - get colorop state, if it exists822* @state: global atomic state object823* @colorop: colorop to grab824*825* This function returns the new colorop state for the given colorop, or826* NULL if the colorop is not part of the global atomic state.827*/828static inline struct drm_colorop_state *829drm_atomic_get_new_colorop_state(struct drm_atomic_state *state,830struct drm_colorop *colorop)831{832return state->colorops[drm_colorop_index(colorop)].new_state;833}834835/**836* drm_atomic_get_old_connector_state - get connector state, if it exists837* @state: global atomic state object838* @connector: connector to grab839*840* This function returns the old connector state for the given connector,841* or NULL if the connector is not part of the global atomic state.842*/843static inline struct drm_connector_state *844drm_atomic_get_old_connector_state(const struct drm_atomic_state *state,845struct drm_connector *connector)846{847int index = drm_connector_index(connector);848849if (index >= state->num_connector)850return NULL;851852return state->connectors[index].old_state;853}854855/**856* drm_atomic_get_new_connector_state - get connector state, if it exists857* @state: global atomic state object858* @connector: connector to grab859*860* This function returns the new connector state for the given connector,861* or NULL if the connector is not part of the global atomic state.862*/863static inline struct drm_connector_state *864drm_atomic_get_new_connector_state(const struct drm_atomic_state *state,865struct drm_connector *connector)866{867int index = drm_connector_index(connector);868869if (index >= state->num_connector)870return NULL;871872return state->connectors[index].new_state;873}874875/**876* __drm_atomic_get_current_plane_state - get current plane state877* @state: global atomic state object878* @plane: plane to grab879*880* This function returns the plane state for the given plane, either the881* new plane state from @state, or if the plane isn't part of the atomic882* state update, from @plane. This is useful in atomic check callbacks,883* when drivers need to peek at, but not change, state of other planes,884* since it avoids threading an error code back up the call chain.885*886* WARNING:887*888* Note that this function is in general unsafe since it doesn't check for the889* required locking for access state structures. Drivers must ensure that it is890* safe to access the returned state structure through other means. One common891* example is when planes are fixed to a single CRTC, and the driver knows that892* the CRTC lock is held already. In that case holding the CRTC lock gives a893* read-lock on all planes connected to that CRTC. But if planes can be894* reassigned things get more tricky. In that case it's better to use895* drm_atomic_get_plane_state and wire up full error handling.896*897* Returns:898*899* Read-only pointer to the current plane state.900*/901static inline const struct drm_plane_state *902__drm_atomic_get_current_plane_state(const struct drm_atomic_state *state,903struct drm_plane *plane)904{905struct drm_plane_state *plane_state;906907plane_state = drm_atomic_get_new_plane_state(state, plane);908if (plane_state)909return plane_state;910911/*912* If the plane isn't part of the state, fallback to the currently active one.913*/914return plane->state;915}916917int __must_check918drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,919struct drm_encoder *encoder);920int __must_check921drm_atomic_add_affected_connectors(struct drm_atomic_state *state,922struct drm_crtc *crtc);923int __must_check924drm_atomic_add_affected_planes(struct drm_atomic_state *state,925struct drm_crtc *crtc);926int __must_check927drm_atomic_add_affected_colorops(struct drm_atomic_state *state,928struct drm_plane *plane);929930int __must_check drm_atomic_check_only(struct drm_atomic_state *state);931int __must_check drm_atomic_commit(struct drm_atomic_state *state);932int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);933934void drm_state_dump(struct drm_device *dev, struct drm_printer *p);935936/**937* for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update938* @__state: &struct drm_atomic_state pointer939* @connector: &struct drm_connector iteration cursor940* @old_connector_state: &struct drm_connector_state iteration cursor for the941* old state942* @new_connector_state: &struct drm_connector_state iteration cursor for the943* new state944* @__i: int iteration cursor, for macro-internal use945*946* This iterates over all connectors in an atomic update, tracking both old and947* new state. This is useful in places where the state delta needs to be948* considered, for example in atomic check functions.949*/950#define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \951for ((__i) = 0; \952(__i) < (__state)->num_connector; \953(__i)++) \954for_each_if ((__state)->connectors[__i].ptr && \955((connector) = (__state)->connectors[__i].ptr, \956(void)(connector) /* Only to avoid unused-but-set-variable warning */, \957(old_connector_state) = (__state)->connectors[__i].old_state, \958(new_connector_state) = (__state)->connectors[__i].new_state, 1))959960/**961* for_each_old_connector_in_state - iterate over all connectors in an atomic update962* @__state: &struct drm_atomic_state pointer963* @connector: &struct drm_connector iteration cursor964* @old_connector_state: &struct drm_connector_state iteration cursor for the965* old state966* @__i: int iteration cursor, for macro-internal use967*968* This iterates over all connectors in an atomic update, tracking only the old969* state. This is useful in disable functions, where we need the old state the970* hardware is still in.971*/972#define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \973for ((__i) = 0; \974(__i) < (__state)->num_connector; \975(__i)++) \976for_each_if ((__state)->connectors[__i].ptr && \977((connector) = (__state)->connectors[__i].ptr, \978(void)(connector) /* Only to avoid unused-but-set-variable warning */, \979(old_connector_state) = (__state)->connectors[__i].old_state, 1))980981/**982* for_each_new_connector_in_state - iterate over all connectors in an atomic update983* @__state: &struct drm_atomic_state pointer984* @connector: &struct drm_connector iteration cursor985* @new_connector_state: &struct drm_connector_state iteration cursor for the986* new state987* @__i: int iteration cursor, for macro-internal use988*989* This iterates over all connectors in an atomic update, tracking only the new990* state. This is useful in enable functions, where we need the new state the991* hardware should be in when the atomic commit operation has completed.992*/993#define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \994for ((__i) = 0; \995(__i) < (__state)->num_connector; \996(__i)++) \997for_each_if ((__state)->connectors[__i].ptr && \998((connector) = (__state)->connectors[__i].ptr, \999(void)(connector) /* Only to avoid unused-but-set-variable warning */, \1000(new_connector_state) = (__state)->connectors[__i].new_state, \1001(void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))10021003/**1004* for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update1005* @__state: &struct drm_atomic_state pointer1006* @crtc: &struct drm_crtc iteration cursor1007* @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state1008* @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state1009* @__i: int iteration cursor, for macro-internal use1010*1011* This iterates over all CRTCs in an atomic update, tracking both old and1012* new state. This is useful in places where the state delta needs to be1013* considered, for example in atomic check functions.1014*/1015#define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \1016for ((__i) = 0; \1017(__i) < (__state)->dev->mode_config.num_crtc; \1018(__i)++) \1019for_each_if ((__state)->crtcs[__i].ptr && \1020((crtc) = (__state)->crtcs[__i].ptr, \1021(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \1022(old_crtc_state) = (__state)->crtcs[__i].old_state, \1023(void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \1024(new_crtc_state) = (__state)->crtcs[__i].new_state, \1025(void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))10261027/**1028* for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update1029* @__state: &struct drm_atomic_state pointer1030* @crtc: &struct drm_crtc iteration cursor1031* @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state1032* @__i: int iteration cursor, for macro-internal use1033*1034* This iterates over all CRTCs in an atomic update, tracking only the old1035* state. This is useful in disable functions, where we need the old state the1036* hardware is still in.1037*/1038#define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i) \1039for ((__i) = 0; \1040(__i) < (__state)->dev->mode_config.num_crtc; \1041(__i)++) \1042for_each_if ((__state)->crtcs[__i].ptr && \1043((crtc) = (__state)->crtcs[__i].ptr, \1044(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \1045(old_crtc_state) = (__state)->crtcs[__i].old_state, 1))10461047/**1048* for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update1049* @__state: &struct drm_atomic_state pointer1050* @crtc: &struct drm_crtc iteration cursor1051* @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state1052* @__i: int iteration cursor, for macro-internal use1053*1054* This iterates over all CRTCs in an atomic update, tracking only the new1055* state. This is useful in enable functions, where we need the new state the1056* hardware should be in when the atomic commit operation has completed.1057*/1058#define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i) \1059for ((__i) = 0; \1060(__i) < (__state)->dev->mode_config.num_crtc; \1061(__i)++) \1062for_each_if ((__state)->crtcs[__i].ptr && \1063((crtc) = (__state)->crtcs[__i].ptr, \1064(void)(crtc) /* Only to avoid unused-but-set-variable warning */, \1065(new_crtc_state) = (__state)->crtcs[__i].new_state, \1066(void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))10671068/**1069* for_each_oldnew_colorop_in_state - iterate over all colorops in an atomic update1070* @__state: &struct drm_atomic_state pointer1071* @colorop: &struct drm_colorop iteration cursor1072* @old_colorop_state: &struct drm_colorop_state iteration cursor for the old state1073* @new_colorop_state: &struct drm_colorop_state iteration cursor for the new state1074* @__i: int iteration cursor, for macro-internal use1075*1076* This iterates over all colorops in an atomic update, tracking both old and1077* new state. This is useful in places where the state delta needs to be1078* considered, for example in atomic check functions.1079*/1080#define for_each_oldnew_colorop_in_state(__state, colorop, old_colorop_state, \1081new_colorop_state, __i) \1082for ((__i) = 0; \1083(__i) < (__state)->dev->mode_config.num_colorop; \1084(__i)++) \1085for_each_if ((__state)->colorops[__i].ptr && \1086((colorop) = (__state)->colorops[__i].ptr, \1087(void)(colorop) /* Only to avoid unused-but-set-variable warning */, \1088(old_colorop_state) = (__state)->colorops[__i].old_state,\1089(new_colorop_state) = (__state)->colorops[__i].new_state, 1))10901091/**1092* for_each_new_colorop_in_state - iterate over all colorops in an atomic update1093* @__state: &struct drm_atomic_state pointer1094* @colorop: &struct drm_colorop iteration cursor1095* @new_colorop_state: &struct drm_colorop_state iteration cursor for the new state1096* @__i: int iteration cursor, for macro-internal use1097*1098* This iterates over all colorops in an atomic update, tracking new state. This is1099* useful in places where the state delta needs to be considered, for example in1100* atomic check functions.1101*/1102#define for_each_new_colorop_in_state(__state, colorop, new_colorop_state, __i) \1103for ((__i) = 0; \1104(__i) < (__state)->dev->mode_config.num_colorop; \1105(__i)++) \1106for_each_if ((__state)->colorops[__i].ptr && \1107((colorop) = (__state)->colorops[__i].ptr, \1108(void)(colorop) /* Only to avoid unused-but-set-variable warning */, \1109(new_colorop_state) = (__state)->colorops[__i].new_state, 1))11101111/**1112* for_each_oldnew_plane_in_state - iterate over all planes in an atomic update1113* @__state: &struct drm_atomic_state pointer1114* @plane: &struct drm_plane iteration cursor1115* @old_plane_state: &struct drm_plane_state iteration cursor for the old state1116* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1117* @__i: int iteration cursor, for macro-internal use1118*1119* This iterates over all planes in an atomic update, tracking both old and1120* new state. This is useful in places where the state delta needs to be1121* considered, for example in atomic check functions.1122*/1123#define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \1124for ((__i) = 0; \1125(__i) < (__state)->dev->mode_config.num_total_plane; \1126(__i)++) \1127for_each_if ((__state)->planes[__i].ptr && \1128((plane) = (__state)->planes[__i].ptr, \1129(void)(plane) /* Only to avoid unused-but-set-variable warning */, \1130(old_plane_state) = (__state)->planes[__i].old_state,\1131(new_plane_state) = (__state)->planes[__i].new_state, 1))11321133/**1134* for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic1135* update in reverse order1136* @__state: &struct drm_atomic_state pointer1137* @plane: &struct drm_plane iteration cursor1138* @old_plane_state: &struct drm_plane_state iteration cursor for the old state1139* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1140* @__i: int iteration cursor, for macro-internal use1141*1142* This iterates over all planes in an atomic update in reverse order,1143* tracking both old and new state. This is useful in places where the1144* state delta needs to be considered, for example in atomic check functions.1145*/1146#define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \1147for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \1148(__i) >= 0; \1149(__i)--) \1150for_each_if ((__state)->planes[__i].ptr && \1151((plane) = (__state)->planes[__i].ptr, \1152(old_plane_state) = (__state)->planes[__i].old_state,\1153(new_plane_state) = (__state)->planes[__i].new_state, 1))11541155/**1156* for_each_new_plane_in_state_reverse - other than only tracking new state,1157* it's the same as for_each_oldnew_plane_in_state_reverse1158* @__state: &struct drm_atomic_state pointer1159* @plane: &struct drm_plane iteration cursor1160* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1161* @__i: int iteration cursor, for macro-internal use1162*/1163#define for_each_new_plane_in_state_reverse(__state, plane, new_plane_state, __i) \1164for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \1165(__i) >= 0; \1166(__i)--) \1167for_each_if ((__state)->planes[__i].ptr && \1168((plane) = (__state)->planes[__i].ptr, \1169(new_plane_state) = (__state)->planes[__i].new_state, 1))11701171/**1172* for_each_old_plane_in_state - iterate over all planes in an atomic update1173* @__state: &struct drm_atomic_state pointer1174* @plane: &struct drm_plane iteration cursor1175* @old_plane_state: &struct drm_plane_state iteration cursor for the old state1176* @__i: int iteration cursor, for macro-internal use1177*1178* This iterates over all planes in an atomic update, tracking only the old1179* state. This is useful in disable functions, where we need the old state the1180* hardware is still in.1181*/1182#define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \1183for ((__i) = 0; \1184(__i) < (__state)->dev->mode_config.num_total_plane; \1185(__i)++) \1186for_each_if ((__state)->planes[__i].ptr && \1187((plane) = (__state)->planes[__i].ptr, \1188(old_plane_state) = (__state)->planes[__i].old_state, 1))1189/**1190* for_each_new_plane_in_state - iterate over all planes in an atomic update1191* @__state: &struct drm_atomic_state pointer1192* @plane: &struct drm_plane iteration cursor1193* @new_plane_state: &struct drm_plane_state iteration cursor for the new state1194* @__i: int iteration cursor, for macro-internal use1195*1196* This iterates over all planes in an atomic update, tracking only the new1197* state. This is useful in enable functions, where we need the new state the1198* hardware should be in when the atomic commit operation has completed.1199*/1200#define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \1201for ((__i) = 0; \1202(__i) < (__state)->dev->mode_config.num_total_plane; \1203(__i)++) \1204for_each_if ((__state)->planes[__i].ptr && \1205((plane) = (__state)->planes[__i].ptr, \1206(void)(plane) /* Only to avoid unused-but-set-variable warning */, \1207(new_plane_state) = (__state)->planes[__i].new_state, \1208(void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))12091210/**1211* for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update1212* @__state: &struct drm_atomic_state pointer1213* @obj: &struct drm_private_obj iteration cursor1214* @old_obj_state: &struct drm_private_state iteration cursor for the old state1215* @new_obj_state: &struct drm_private_state iteration cursor for the new state1216* @__i: int iteration cursor, for macro-internal use1217*1218* This iterates over all private objects in an atomic update, tracking both1219* old and new state. This is useful in places where the state delta needs1220* to be considered, for example in atomic check functions.1221*/1222#define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \1223for ((__i) = 0; \1224(__i) < (__state)->num_private_objs && \1225((obj) = (__state)->private_objs[__i].ptr, \1226(old_obj_state) = (__state)->private_objs[__i].old_state, \1227(new_obj_state) = (__state)->private_objs[__i].new_state, 1); \1228(__i)++)12291230/**1231* for_each_old_private_obj_in_state - iterate over all private objects in an atomic update1232* @__state: &struct drm_atomic_state pointer1233* @obj: &struct drm_private_obj iteration cursor1234* @old_obj_state: &struct drm_private_state iteration cursor for the old state1235* @__i: int iteration cursor, for macro-internal use1236*1237* This iterates over all private objects in an atomic update, tracking only1238* the old state. This is useful in disable functions, where we need the old1239* state the hardware is still in.1240*/1241#define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \1242for ((__i) = 0; \1243(__i) < (__state)->num_private_objs && \1244((obj) = (__state)->private_objs[__i].ptr, \1245(old_obj_state) = (__state)->private_objs[__i].old_state, 1); \1246(__i)++)12471248/**1249* for_each_new_private_obj_in_state - iterate over all private objects in an atomic update1250* @__state: &struct drm_atomic_state pointer1251* @obj: &struct drm_private_obj iteration cursor1252* @new_obj_state: &struct drm_private_state iteration cursor for the new state1253* @__i: int iteration cursor, for macro-internal use1254*1255* This iterates over all private objects in an atomic update, tracking only1256* the new state. This is useful in enable functions, where we need the new state the1257* hardware should be in when the atomic commit operation has completed.1258*/1259#define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \1260for ((__i) = 0; \1261(__i) < (__state)->num_private_objs && \1262((obj) = (__state)->private_objs[__i].ptr, \1263(void)(obj) /* Only to avoid unused-but-set-variable warning */, \1264(new_obj_state) = (__state)->private_objs[__i].new_state, 1); \1265(__i)++)12661267/**1268* drm_atomic_crtc_needs_modeset - compute combined modeset need1269* @state: &drm_crtc_state for the CRTC1270*1271* To give drivers flexibility &struct drm_crtc_state has 3 booleans to track1272* whether the state CRTC changed enough to need a full modeset cycle:1273* mode_changed, active_changed and connectors_changed. This helper simply1274* combines these three to compute the overall need for a modeset for @state.1275*1276* The atomic helper code sets these booleans, but drivers can and should1277* change them appropriately to accurately represent whether a modeset is1278* really needed. In general, drivers should avoid full modesets whenever1279* possible.1280*1281* For example if the CRTC mode has changed, and the hardware is able to enact1282* the requested mode change without going through a full modeset, the driver1283* should clear mode_changed in its &drm_mode_config_funcs.atomic_check1284* implementation.1285*/1286static inline bool1287drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)1288{1289return state->mode_changed || state->active_changed ||1290state->connectors_changed;1291}12921293/**1294* drm_atomic_crtc_effectively_active - compute whether CRTC is actually active1295* @state: &drm_crtc_state for the CRTC1296*1297* When in self refresh mode, the crtc_state->active value will be false, since1298* the CRTC is off. However in some cases we're interested in whether the CRTC1299* is active, or effectively active (ie: it's connected to an active display).1300* In these cases, use this function instead of just checking active.1301*/1302static inline bool1303drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)1304{1305return state->active || state->self_refresh_active;1306}13071308/**1309* struct drm_bus_cfg - bus configuration1310*1311* This structure stores the configuration of a physical bus between two1312* components in an output pipeline, usually between two bridges, an encoder1313* and a bridge, or a bridge and a connector.1314*1315* The bus configuration is stored in &drm_bridge_state separately for the1316* input and output buses, as seen from the point of view of each bridge. The1317* bus configuration of a bridge output is usually identical to the1318* configuration of the next bridge's input, but may differ if the signals are1319* modified between the two bridges, for instance by an inverter on the board.1320* The input and output configurations of a bridge may differ if the bridge1321* modifies the signals internally, for instance by performing format1322* conversion, or modifying signals polarities.1323*/1324struct drm_bus_cfg {1325/**1326* @format: format used on this bus (one of the MEDIA_BUS_FMT_* format)1327*1328* This field should not be directly modified by drivers1329* (drm_atomic_bridge_chain_select_bus_fmts() takes care of the bus1330* format negotiation).1331*/1332u32 format;13331334/**1335* @flags: DRM_BUS_* flags used on this bus1336*/1337u32 flags;1338};13391340/**1341* struct drm_bridge_state - Atomic bridge state object1342*/1343struct drm_bridge_state {1344/**1345* @base: inherit from &drm_private_state1346*/1347struct drm_private_state base;13481349/**1350* @bridge: the bridge this state refers to1351*/1352struct drm_bridge *bridge;13531354/**1355* @input_bus_cfg: input bus configuration1356*/1357struct drm_bus_cfg input_bus_cfg;13581359/**1360* @output_bus_cfg: output bus configuration1361*/1362struct drm_bus_cfg output_bus_cfg;1363};13641365static inline struct drm_bridge_state *1366drm_priv_to_bridge_state(struct drm_private_state *priv)1367{1368return container_of(priv, struct drm_bridge_state, base);1369}13701371struct drm_bridge_state *1372drm_atomic_get_bridge_state(struct drm_atomic_state *state,1373struct drm_bridge *bridge);1374struct drm_bridge_state *1375drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,1376struct drm_bridge *bridge);1377struct drm_bridge_state *1378drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,1379struct drm_bridge *bridge);13801381#endif /* DRM_ATOMIC_H_ */138213831384