xref: /linux/include/drm/drm_atomic.h (revision 9b5c7e8b452398ee29dd42b21df73fa71dfc648e)
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27 
28 #ifndef DRM_ATOMIC_H_
29 #define DRM_ATOMIC_H_
30 
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_util.h>
33 #include <drm/drm_colorop.h>
34 
35 /**
36  * struct drm_crtc_commit - track modeset commits on a CRTC
37  *
38  * This structure is used to track pending modeset changes and atomic commit on
39  * a per-CRTC basis. Since updating the list should never block, this structure
40  * is reference counted to allow waiters to safely wait on an event to complete,
41  * without holding any locks.
42  *
43  * It has 3 different events in total to allow a fine-grained synchronization
44  * between outstanding updates::
45  *
46  *	atomic commit thread			hardware
47  *
48  * 	write new state into hardware	---->	...
49  * 	signal hw_done
50  * 						switch to new state on next
51  * 	...					v/hblank
52  *
53  *	wait for buffers to show up		...
54  *
55  *	...					send completion irq
56  *						irq handler signals flip_done
57  *	cleanup old buffers
58  *
59  * 	signal cleanup_done
60  *
61  * 	wait for flip_done		<----
62  * 	clean up atomic state
63  *
64  * The important bit to know is that &cleanup_done is the terminal event, but the
65  * ordering between &flip_done and &hw_done is entirely up to the specific driver
66  * and modeset state change.
67  *
68  * For an implementation of how to use this look at
69  * drm_atomic_helper_setup_commit() from the atomic helper library.
70  *
71  * See also drm_crtc_commit_wait().
72  */
73 struct drm_crtc_commit {
74 	/**
75 	 * @crtc:
76 	 *
77 	 * DRM CRTC for this commit.
78 	 */
79 	struct drm_crtc *crtc;
80 
81 	/**
82 	 * @ref:
83 	 *
84 	 * Reference count for this structure. Needed to allow blocking on
85 	 * completions without the risk of the completion disappearing
86 	 * meanwhile.
87 	 */
88 	struct kref ref;
89 
90 	/**
91 	 * @flip_done:
92 	 *
93 	 * Will be signaled when the hardware has flipped to the new set of
94 	 * buffers. Signals at the same time as when the drm event for this
95 	 * commit is sent to userspace, or when an out-fence is singalled. Note
96 	 * that for most hardware, in most cases this happens after @hw_done is
97 	 * signalled.
98 	 *
99 	 * Completion of this stage is signalled implicitly by calling
100 	 * drm_crtc_send_vblank_event() on &drm_crtc_state.event.
101 	 */
102 	struct completion flip_done;
103 
104 	/**
105 	 * @hw_done:
106 	 *
107 	 * Will be signalled when all hw register changes for this commit have
108 	 * been written out. Especially when disabling a pipe this can be much
109 	 * later than @flip_done, since that can signal already when the
110 	 * screen goes black, whereas to fully shut down a pipe more register
111 	 * I/O is required.
112 	 *
113 	 * Note that this does not need to include separately reference-counted
114 	 * resources like backing storage buffer pinning, or runtime pm
115 	 * management.
116 	 *
117 	 * Drivers should call drm_atomic_helper_commit_hw_done() to signal
118 	 * completion of this stage.
119 	 */
120 	struct completion hw_done;
121 
122 	/**
123 	 * @cleanup_done:
124 	 *
125 	 * Will be signalled after old buffers have been cleaned up by calling
126 	 * drm_atomic_helper_cleanup_planes(). Since this can only happen after
127 	 * a vblank wait completed it might be a bit later. This completion is
128 	 * useful to throttle updates and avoid hardware updates getting ahead
129 	 * of the buffer cleanup too much.
130 	 *
131 	 * Drivers should call drm_atomic_helper_commit_cleanup_done() to signal
132 	 * completion of this stage.
133 	 */
134 	struct completion cleanup_done;
135 
136 	/**
137 	 * @commit_entry:
138 	 *
139 	 * Entry on the per-CRTC &drm_crtc.commit_list. Protected by
140 	 * $drm_crtc.commit_lock.
141 	 */
142 	struct list_head commit_entry;
143 
144 	/**
145 	 * @event:
146 	 *
147 	 * &drm_pending_vblank_event pointer to clean up private events.
148 	 */
149 	struct drm_pending_vblank_event *event;
150 
151 	/**
152 	 * @abort_completion:
153 	 *
154 	 * A flag that's set after drm_atomic_helper_setup_commit() takes a
155 	 * second reference for the completion of $drm_crtc_state.event. It's
156 	 * used by the free code to remove the second reference if commit fails.
157 	 */
158 	bool abort_completion;
159 };
160 
161 struct __drm_colorops_state {
162 	struct drm_colorop *ptr;
163 	struct drm_colorop_state *state, *old_state, *new_state;
164 };
165 
166 struct __drm_planes_state {
167 	struct drm_plane *ptr;
168 
169 	/**
170 	 * @state_to_destroy:
171 	 *
172 	 * Used to track the @drm_plane_state we will need to free when
173 	 * tearing down the associated &drm_atomic_state in
174 	 * $drm_mode_config_funcs.atomic_state_clear or
175 	 * drm_atomic_state_default_clear().
176 	 *
177 	 * Before a commit, and the call to
178 	 * drm_atomic_helper_swap_state() in particular, it points to
179 	 * the same state than @new_state. After a commit, it points to
180 	 * the same state than @old_state.
181 	 */
182 	struct drm_plane_state *state_to_destroy;
183 
184 	struct drm_plane_state *old_state, *new_state;
185 };
186 
187 struct __drm_crtcs_state {
188 	struct drm_crtc *ptr;
189 
190 	/**
191 	 * @state_to_destroy:
192 	 *
193 	 * Used to track the @drm_crtc_state we will need to free when
194 	 * tearing down the associated &drm_atomic_state in
195 	 * $drm_mode_config_funcs.atomic_state_clear or
196 	 * drm_atomic_state_default_clear().
197 	 *
198 	 * Before a commit, and the call to
199 	 * drm_atomic_helper_swap_state() in particular, it points to
200 	 * the same state than @new_state. After a commit, it points to
201 	 * the same state than @old_state.
202 	 */
203 	struct drm_crtc_state *state_to_destroy;
204 
205 	struct drm_crtc_state *old_state, *new_state;
206 
207 	/**
208 	 * @commit:
209 	 *
210 	 * A reference to the CRTC commit object that is kept for use by
211 	 * drm_atomic_helper_wait_for_flip_done() after
212 	 * drm_atomic_helper_commit_hw_done() is called. This ensures that a
213 	 * concurrent commit won't free a commit object that is still in use.
214 	 */
215 	struct drm_crtc_commit *commit;
216 
217 	s32 __user *out_fence_ptr;
218 	u64 last_vblank_count;
219 };
220 
221 struct __drm_connnectors_state {
222 	struct drm_connector *ptr;
223 
224 	/**
225 	 * @state_to_destroy:
226 	 *
227 	 * Used to track the @drm_connector_state we will need to free
228 	 * when tearing down the associated &drm_atomic_state in
229 	 * $drm_mode_config_funcs.atomic_state_clear or
230 	 * drm_atomic_state_default_clear().
231 	 *
232 	 * Before a commit, and the call to
233 	 * drm_atomic_helper_swap_state() in particular, it points to
234 	 * the same state than @new_state. After a commit, it points to
235 	 * the same state than @old_state.
236 	 */
237 	struct drm_connector_state *state_to_destroy;
238 
239 	struct drm_connector_state *old_state, *new_state;
240 
241 	/**
242 	 * @out_fence_ptr:
243 	 *
244 	 * User-provided pointer which the kernel uses to return a sync_file
245 	 * file descriptor. Used by writeback connectors to signal completion of
246 	 * the writeback.
247 	 */
248 	s32 __user *out_fence_ptr;
249 };
250 
251 struct drm_private_obj;
252 struct drm_private_state;
253 
254 /**
255  * struct drm_private_state_funcs - atomic state functions for private objects
256  *
257  * These hooks are used by atomic helpers to create, swap and destroy states of
258  * private objects. The structure itself is used as a vtable to identify the
259  * associated private object type. Each private object type that needs to be
260  * added to the atomic states is expected to have an implementation of these
261  * hooks and pass a pointer to its drm_private_state_funcs struct to
262  * drm_atomic_get_private_obj_state().
263  */
264 struct drm_private_state_funcs {
265 	/**
266 	 * @atomic_duplicate_state:
267 	 *
268 	 * Duplicate the current state of the private object and return it. It
269 	 * is an error to call this before obj->state has been initialized.
270 	 *
271 	 * RETURNS:
272 	 *
273 	 * Duplicated atomic state or NULL when obj->state is not
274 	 * initialized or allocation failed.
275 	 */
276 	struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);
277 
278 	/**
279 	 * @atomic_destroy_state:
280 	 *
281 	 * Frees the private object state created with @atomic_duplicate_state.
282 	 */
283 	void (*atomic_destroy_state)(struct drm_private_obj *obj,
284 				     struct drm_private_state *state);
285 
286 	/**
287 	 * @atomic_print_state:
288 	 *
289 	 * If driver subclasses &struct drm_private_state, it should implement
290 	 * this optional hook for printing additional driver specific state.
291 	 *
292 	 * Do not call this directly, use drm_atomic_private_obj_print_state()
293 	 * instead.
294 	 */
295 	void (*atomic_print_state)(struct drm_printer *p,
296 				   const struct drm_private_state *state);
297 };
298 
299 /**
300  * struct drm_private_obj - base struct for driver private atomic object
301  *
302  * A driver private object is initialized by calling
303  * drm_atomic_private_obj_init() and cleaned up by calling
304  * drm_atomic_private_obj_fini().
305  *
306  * Currently only tracks the state update functions and the opaque driver
307  * private state itself, but in the future might also track which
308  * &drm_modeset_lock is required to duplicate and update this object's state.
309  *
310  * All private objects must be initialized before the DRM device they are
311  * attached to is registered to the DRM subsystem (call to drm_dev_register())
312  * and should stay around until this DRM device is unregistered (call to
313  * drm_dev_unregister()). In other words, private objects lifetime is tied
314  * to the DRM device lifetime. This implies that:
315  *
316  * 1/ all calls to drm_atomic_private_obj_init() must be done before calling
317  *    drm_dev_register()
318  * 2/ all calls to drm_atomic_private_obj_fini() must be done after calling
319  *    drm_dev_unregister()
320  *
321  * If that private object is used to store a state shared by multiple
322  * CRTCs, proper care must be taken to ensure that non-blocking commits are
323  * properly ordered to avoid a use-after-free issue.
324  *
325  * Indeed, assuming a sequence of two non-blocking &drm_atomic_commit on two
326  * different &drm_crtc using different &drm_plane and &drm_connector, so with no
327  * resources shared, there's no guarantee on which commit is going to happen
328  * first. However, the second &drm_atomic_commit will consider the first
329  * &drm_private_obj its old state, and will be in charge of freeing it whenever
330  * the second &drm_atomic_commit is done.
331  *
332  * If the first &drm_atomic_commit happens after it, it will consider its
333  * &drm_private_obj the new state and will be likely to access it, resulting in
334  * an access to a freed memory region. Drivers should store (and get a reference
335  * to) the &drm_crtc_commit structure in our private state in
336  * &drm_mode_config_helper_funcs.atomic_commit_setup, and then wait for that
337  * commit to complete as the first step of
338  * &drm_mode_config_helper_funcs.atomic_commit_tail, similar to
339  * drm_atomic_helper_wait_for_dependencies().
340  */
341 struct drm_private_obj {
342 	/**
343 	 * @head: List entry used to attach a private object to a &drm_device
344 	 * (queued to &drm_mode_config.privobj_list).
345 	 */
346 	struct list_head head;
347 
348 	/**
349 	 * @lock: Modeset lock to protect the state object.
350 	 */
351 	struct drm_modeset_lock lock;
352 
353 	/**
354 	 * @state: Current atomic state for this driver private object.
355 	 */
356 	struct drm_private_state *state;
357 
358 	/**
359 	 * @funcs:
360 	 *
361 	 * Functions to manipulate the state of this driver private object, see
362 	 * &drm_private_state_funcs.
363 	 */
364 	const struct drm_private_state_funcs *funcs;
365 };
366 
367 /**
368  * drm_for_each_privobj() - private object iterator
369  *
370  * @privobj: pointer to the current private object. Updated after each
371  *	     iteration
372  * @dev: the DRM device we want get private objects from
373  *
374  * Allows one to iterate over all private objects attached to @dev
375  */
376 #define drm_for_each_privobj(privobj, dev) \
377 	list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)
378 
379 /**
380  * struct drm_private_state - base struct for driver private object state
381  *
382  * Currently only contains a backpointer to the overall atomic update,
383  * and the relevant private object but in the future also might hold
384  * synchronization information similar to e.g. &drm_crtc.commit.
385  */
386 struct drm_private_state {
387 	/**
388 	 * @state: backpointer to global drm_atomic_state
389 	 */
390 	struct drm_atomic_state *state;
391 
392 	/**
393 	 * @obj: backpointer to the private object
394 	 */
395 	struct drm_private_obj *obj;
396 };
397 
398 struct __drm_private_objs_state {
399 	struct drm_private_obj *ptr;
400 
401 	/**
402 	 * @state_to_destroy:
403 	 *
404 	 * Used to track the @drm_private_state we will need to free
405 	 * when tearing down the associated &drm_atomic_state in
406 	 * $drm_mode_config_funcs.atomic_state_clear or
407 	 * drm_atomic_state_default_clear().
408 	 *
409 	 * Before a commit, and the call to
410 	 * drm_atomic_helper_swap_state() in particular, it points to
411 	 * the same state than @new_state. After a commit, it points to
412 	 * the same state than @old_state.
413 	 */
414 	struct drm_private_state *state_to_destroy;
415 
416 	struct drm_private_state *old_state, *new_state;
417 };
418 
419 /**
420  * struct drm_atomic_state - Atomic commit structure
421  *
422  * This structure is the kernel counterpart of @drm_mode_atomic and represents
423  * an atomic commit that transitions from an old to a new display state. It
424  * contains all the objects affected by the atomic commit and both the new
425  * state structures and pointers to the old state structures for
426  * these.
427  *
428  * States are added to an atomic update by calling drm_atomic_get_crtc_state(),
429  * drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for
430  * private state structures, drm_atomic_get_private_obj_state().
431  *
432  * NOTE: struct drm_atomic_state first started as a single collection of
433  * entities state pointers (drm_plane_state, drm_crtc_state, etc.).
434  *
435  * At atomic_check time, you could get the state about to be committed
436  * from drm_atomic_state, and the one currently running from the
437  * entities state pointer (drm_crtc.state, for example). After the call
438  * to drm_atomic_helper_swap_state(), the entities state pointer would
439  * contain the state previously checked, and the drm_atomic_state
440  * structure the old state.
441  *
442  * Over time, and in order to avoid confusion, drm_atomic_state has
443  * grown to have both the old state (ie, the state we replace) and the
444  * new state (ie, the state we want to apply). Those names are stable
445  * during the commit process, which makes it easier to reason about.
446  *
447  * You can still find some traces of that evolution through some hooks
448  * or callbacks taking a drm_atomic_state parameter called names like
449  * "old_state". This doesn't necessarily mean that the previous
450  * drm_atomic_state is passed, but rather that this used to be the state
451  * collection we were replacing after drm_atomic_helper_swap_state(),
452  * but the variable name was never updated.
453  *
454  * Some atomic operations implementations followed a similar process. We
455  * first started to pass the entity state only. However, it was pretty
456  * cumbersome for drivers, and especially CRTCs, to retrieve the states
457  * of other components. Thus, we switched to passing the whole
458  * drm_atomic_state as a parameter to those operations. Similarly, the
459  * transition isn't complete yet, and one might still find atomic
460  * operations taking a drm_atomic_state pointer, or a component state
461  * pointer. The former is the preferred form.
462  */
463 struct drm_atomic_state {
464 	/**
465 	 * @ref:
466 	 *
467 	 * Count of all references to this update (will not be freed until zero).
468 	 */
469 	struct kref ref;
470 
471 	/**
472 	 * @dev: Parent DRM Device.
473 	 */
474 	struct drm_device *dev;
475 
476 	/**
477 	 * @allow_modeset:
478 	 *
479 	 * Allow full modeset. This is used by the ATOMIC IOCTL handler to
480 	 * implement the DRM_MODE_ATOMIC_ALLOW_MODESET flag. Drivers should
481 	 * generally not consult this flag, but instead look at the output of
482 	 * drm_atomic_crtc_needs_modeset(). The detailed rules are:
483 	 *
484 	 * - Drivers must not consult @allow_modeset in the atomic commit path.
485 	 *   Use drm_atomic_crtc_needs_modeset() instead.
486 	 *
487 	 * - Drivers must consult @allow_modeset before adding unrelated struct
488 	 *   drm_crtc_state to this commit by calling
489 	 *   drm_atomic_get_crtc_state(). See also the warning in the
490 	 *   documentation for that function.
491 	 *
492 	 * - Drivers must never change this flag, it is under the exclusive
493 	 *   control of userspace.
494 	 *
495 	 * - Drivers may consult @allow_modeset in the atomic check path, if
496 	 *   they have the choice between an optimal hardware configuration
497 	 *   which requires a modeset, and a less optimal configuration which
498 	 *   can be committed without a modeset. An example would be suboptimal
499 	 *   scanout FIFO allocation resulting in increased idle power
500 	 *   consumption. This allows userspace to avoid flickering and delays
501 	 *   for the normal composition loop at reasonable cost.
502 	 */
503 	bool allow_modeset : 1;
504 	/**
505 	 * @legacy_cursor_update:
506 	 *
507 	 * Hint to enforce legacy cursor IOCTL semantics.
508 	 *
509 	 * WARNING: This is thoroughly broken and pretty much impossible to
510 	 * implement correctly. Drivers must ignore this and should instead
511 	 * implement &drm_plane_helper_funcs.atomic_async_check and
512 	 * &drm_plane_helper_funcs.atomic_async_commit hooks. New users of this
513 	 * flag are not allowed.
514 	 */
515 	bool legacy_cursor_update : 1;
516 
517 	/**
518 	 * @async_update: hint for asynchronous plane update
519 	 */
520 	bool async_update : 1;
521 
522 	/**
523 	 * @duplicated:
524 	 *
525 	 * Indicates whether or not this atomic state was duplicated using
526 	 * drm_atomic_helper_duplicate_state(). Drivers and atomic helpers
527 	 * should use this to fixup normal  inconsistencies in duplicated
528 	 * states.
529 	 */
530 	bool duplicated : 1;
531 
532 	/**
533 	 * @checked:
534 	 *
535 	 * Indicates the state has been checked and thus must no longer
536 	 * be mutated. For internal use only, do not consult from drivers.
537 	 */
538 	bool checked : 1;
539 
540 	/**
541 	 * @colorops:
542 	 *
543 	 * Pointer to array of @drm_colorop and @drm_colorop_state part of this
544 	 * update.
545 	 */
546 	struct __drm_colorops_state *colorops;
547 
548 	/**
549 	 * @planes:
550 	 *
551 	 * Pointer to array of @drm_plane and @drm_plane_state part of this
552 	 * update.
553 	 */
554 	struct __drm_planes_state *planes;
555 
556 	/**
557 	 * @crtcs:
558 	 *
559 	 * Pointer to array of @drm_crtc and @drm_crtc_state part of this
560 	 * update.
561 	 */
562 	struct __drm_crtcs_state *crtcs;
563 
564 	/**
565 	 * @num_connector: size of the @connectors array
566 	 */
567 	int num_connector;
568 
569 	/**
570 	 * @connectors:
571 	 *
572 	 * Pointer to array of @drm_connector and @drm_connector_state part of
573 	 * this update.
574 	 */
575 	struct __drm_connnectors_state *connectors;
576 
577 	/**
578 	 * @num_private_objs: size of the @private_objs array
579 	 */
580 	int num_private_objs;
581 
582 	/**
583 	 * @private_objs:
584 	 *
585 	 * Pointer to array of @drm_private_obj and @drm_private_obj_state part
586 	 * of this update.
587 	 */
588 	struct __drm_private_objs_state *private_objs;
589 
590 	/**
591 	 * @acquire_ctx: acquire context for this atomic modeset state update
592 	 */
593 	struct drm_modeset_acquire_ctx *acquire_ctx;
594 
595 	/**
596 	 * @fake_commit:
597 	 *
598 	 * Used for signaling unbound planes/connectors.
599 	 * When a connector or plane is not bound to any CRTC, it's still important
600 	 * to preserve linearity to prevent the atomic states from being freed too early.
601 	 *
602 	 * This commit (if set) is not bound to any CRTC, but will be completed when
603 	 * drm_atomic_helper_commit_hw_done() is called.
604 	 */
605 	struct drm_crtc_commit *fake_commit;
606 
607 	/**
608 	 * @commit_work:
609 	 *
610 	 * Work item which can be used by the driver or helpers to execute the
611 	 * commit without blocking.
612 	 */
613 	struct work_struct commit_work;
614 };
615 
616 void __drm_crtc_commit_free(struct kref *kref);
617 
618 /**
619  * drm_crtc_commit_get - acquire a reference to the CRTC commit
620  * @commit: CRTC commit
621  *
622  * Increases the reference of @commit.
623  *
624  * Returns:
625  * The pointer to @commit, with reference increased.
626  */
627 static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
628 {
629 	kref_get(&commit->ref);
630 	return commit;
631 }
632 
633 /**
634  * drm_crtc_commit_put - release a reference to the CRTC commmit
635  * @commit: CRTC commit
636  *
637  * This releases a reference to @commit which is freed after removing the
638  * final reference. No locking required and callable from any context.
639  */
640 static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
641 {
642 	kref_put(&commit->ref, __drm_crtc_commit_free);
643 }
644 
645 int drm_crtc_commit_wait(struct drm_crtc_commit *commit);
646 
647 struct drm_atomic_state * __must_check
648 drm_atomic_state_alloc(struct drm_device *dev);
649 void drm_atomic_state_clear(struct drm_atomic_state *state);
650 
651 /**
652  * drm_atomic_state_get - acquire a reference to the atomic state
653  * @state: The atomic state
654  *
655  * Returns a new reference to the @state
656  */
657 static inline struct drm_atomic_state *
658 drm_atomic_state_get(struct drm_atomic_state *state)
659 {
660 	kref_get(&state->ref);
661 	return state;
662 }
663 
664 void __drm_atomic_state_free(struct kref *ref);
665 
666 /**
667  * drm_atomic_state_put - release a reference to the atomic state
668  * @state: The atomic state
669  *
670  * This releases a reference to @state which is freed after removing the
671  * final reference. No locking required and callable from any context.
672  */
673 static inline void drm_atomic_state_put(struct drm_atomic_state *state)
674 {
675 	kref_put(&state->ref, __drm_atomic_state_free);
676 }
677 
678 int  __must_check
679 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
680 void drm_atomic_state_default_clear(struct drm_atomic_state *state);
681 void drm_atomic_state_default_release(struct drm_atomic_state *state);
682 
683 struct drm_crtc_state * __must_check
684 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
685 			  struct drm_crtc *crtc);
686 struct drm_plane_state * __must_check
687 drm_atomic_get_plane_state(struct drm_atomic_state *state,
688 			   struct drm_plane *plane);
689 struct drm_colorop_state *
690 drm_atomic_get_colorop_state(struct drm_atomic_state *state,
691 			     struct drm_colorop *colorop);
692 struct drm_connector_state * __must_check
693 drm_atomic_get_connector_state(struct drm_atomic_state *state,
694 			       struct drm_connector *connector);
695 
696 void drm_atomic_private_obj_init(struct drm_device *dev,
697 				 struct drm_private_obj *obj,
698 				 struct drm_private_state *state,
699 				 const struct drm_private_state_funcs *funcs);
700 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
701 
702 struct drm_private_state * __must_check
703 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
704 				 struct drm_private_obj *obj);
705 struct drm_private_state *
706 drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,
707 				     struct drm_private_obj *obj);
708 struct drm_private_state *
709 drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,
710 				     struct drm_private_obj *obj);
711 
712 struct drm_connector *
713 drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,
714 					 struct drm_encoder *encoder);
715 struct drm_connector *
716 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,
717 					 struct drm_encoder *encoder);
718 struct drm_connector *
719 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
720 				     struct drm_modeset_acquire_ctx *ctx);
721 
722 struct drm_crtc *
723 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,
724 					 struct drm_encoder *encoder);
725 struct drm_crtc *
726 drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,
727 					 struct drm_encoder *encoder);
728 
729 /**
730  * drm_atomic_get_old_crtc_state - get old CRTC state, if it exists
731  * @state: global atomic state object
732  * @crtc: CRTC to grab
733  *
734  * This function returns the old CRTC state for the given CRTC, or
735  * NULL if the CRTC is not part of the global atomic state.
736  */
737 static inline struct drm_crtc_state *
738 drm_atomic_get_old_crtc_state(const struct drm_atomic_state *state,
739 			      struct drm_crtc *crtc)
740 {
741 	return state->crtcs[drm_crtc_index(crtc)].old_state;
742 }
743 /**
744  * drm_atomic_get_new_crtc_state - get new CRTC state, if it exists
745  * @state: global atomic state object
746  * @crtc: CRTC to grab
747  *
748  * This function returns the new CRTC state for the given CRTC, or
749  * NULL if the CRTC is not part of the global atomic state.
750  */
751 static inline struct drm_crtc_state *
752 drm_atomic_get_new_crtc_state(const struct drm_atomic_state *state,
753 			      struct drm_crtc *crtc)
754 {
755 	return state->crtcs[drm_crtc_index(crtc)].new_state;
756 }
757 
758 /**
759  * drm_atomic_get_old_plane_state - get plane state, if it exists
760  * @state: global atomic state object
761  * @plane: plane to grab
762  *
763  * This function returns the old plane state for the given plane, or
764  * NULL if the plane is not part of the global atomic state.
765  */
766 static inline struct drm_plane_state *
767 drm_atomic_get_old_plane_state(const struct drm_atomic_state *state,
768 			       struct drm_plane *plane)
769 {
770 	return state->planes[drm_plane_index(plane)].old_state;
771 }
772 
773 /**
774  * drm_atomic_get_new_plane_state - get plane state, if it exists
775  * @state: global atomic state object
776  * @plane: plane to grab
777  *
778  * This function returns the new plane state for the given plane, or
779  * NULL if the plane is not part of the global atomic state.
780  */
781 static inline struct drm_plane_state *
782 drm_atomic_get_new_plane_state(const struct drm_atomic_state *state,
783 			       struct drm_plane *plane)
784 {
785 	return state->planes[drm_plane_index(plane)].new_state;
786 }
787 
788 /**
789  * drm_atomic_get_old_colorop_state - get colorop state, if it exists
790  * @state: global atomic state object
791  * @colorop: colorop to grab
792  *
793  * This function returns the old colorop state for the given colorop, or
794  * NULL if the colorop is not part of the global atomic state.
795  */
796 static inline struct drm_colorop_state *
797 drm_atomic_get_old_colorop_state(struct drm_atomic_state *state,
798 				 struct drm_colorop *colorop)
799 {
800 	return state->colorops[drm_colorop_index(colorop)].old_state;
801 }
802 
803 /**
804  * drm_atomic_get_new_colorop_state - get colorop state, if it exists
805  * @state: global atomic state object
806  * @colorop: colorop to grab
807  *
808  * This function returns the new colorop state for the given colorop, or
809  * NULL if the colorop is not part of the global atomic state.
810  */
811 static inline struct drm_colorop_state *
812 drm_atomic_get_new_colorop_state(struct drm_atomic_state *state,
813 				 struct drm_colorop *colorop)
814 {
815 	return state->colorops[drm_colorop_index(colorop)].new_state;
816 }
817 
818 /**
819  * drm_atomic_get_old_connector_state - get connector state, if it exists
820  * @state: global atomic state object
821  * @connector: connector to grab
822  *
823  * This function returns the old connector state for the given connector,
824  * or NULL if the connector is not part of the global atomic state.
825  */
826 static inline struct drm_connector_state *
827 drm_atomic_get_old_connector_state(const struct drm_atomic_state *state,
828 				   struct drm_connector *connector)
829 {
830 	int index = drm_connector_index(connector);
831 
832 	if (index >= state->num_connector)
833 		return NULL;
834 
835 	return state->connectors[index].old_state;
836 }
837 
838 /**
839  * drm_atomic_get_new_connector_state - get connector state, if it exists
840  * @state: global atomic state object
841  * @connector: connector to grab
842  *
843  * This function returns the new connector state for the given connector,
844  * or NULL if the connector is not part of the global atomic state.
845  */
846 static inline struct drm_connector_state *
847 drm_atomic_get_new_connector_state(const struct drm_atomic_state *state,
848 				   struct drm_connector *connector)
849 {
850 	int index = drm_connector_index(connector);
851 
852 	if (index >= state->num_connector)
853 		return NULL;
854 
855 	return state->connectors[index].new_state;
856 }
857 
858 /**
859  * __drm_atomic_get_current_plane_state - get current plane state
860  * @state: global atomic state object
861  * @plane: plane to grab
862  *
863  * This function returns the plane state for the given plane, either the
864  * new plane state from @state, or if the plane isn't part of the atomic
865  * state update, from @plane. This is useful in atomic check callbacks,
866  * when drivers need to peek at, but not change, state of other planes,
867  * since it avoids threading an error code back up the call chain.
868  *
869  * WARNING:
870  *
871  * Note that this function is in general unsafe since it doesn't check for the
872  * required locking for access state structures. Drivers must ensure that it is
873  * safe to access the returned state structure through other means. One common
874  * example is when planes are fixed to a single CRTC, and the driver knows that
875  * the CRTC lock is held already. In that case holding the CRTC lock gives a
876  * read-lock on all planes connected to that CRTC. But if planes can be
877  * reassigned things get more tricky. In that case it's better to use
878  * drm_atomic_get_plane_state and wire up full error handling.
879  *
880  * Returns:
881  *
882  * Read-only pointer to the current plane state.
883  */
884 static inline const struct drm_plane_state *
885 __drm_atomic_get_current_plane_state(const struct drm_atomic_state *state,
886 				     struct drm_plane *plane)
887 {
888 	struct drm_plane_state *plane_state;
889 
890 	plane_state = drm_atomic_get_new_plane_state(state, plane);
891 	if (plane_state)
892 		return plane_state;
893 
894 	/*
895 	 * If the plane isn't part of the state, fallback to the currently active one.
896 	 */
897 	return plane->state;
898 }
899 
900 int __must_check
901 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
902 			       struct drm_encoder *encoder);
903 int __must_check
904 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
905 				   struct drm_crtc *crtc);
906 int __must_check
907 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
908 			       struct drm_crtc *crtc);
909 int __must_check
910 drm_atomic_add_affected_colorops(struct drm_atomic_state *state,
911 				 struct drm_plane *plane);
912 
913 int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
914 int __must_check drm_atomic_commit(struct drm_atomic_state *state);
915 int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
916 
917 void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
918 
919 /**
920  * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
921  * @__state: &struct drm_atomic_state pointer
922  * @connector: &struct drm_connector iteration cursor
923  * @old_connector_state: &struct drm_connector_state iteration cursor for the
924  * 	old state
925  * @new_connector_state: &struct drm_connector_state iteration cursor for the
926  * 	new state
927  * @__i: int iteration cursor, for macro-internal use
928  *
929  * This iterates over all connectors in an atomic update, tracking both old and
930  * new state. This is useful in places where the state delta needs to be
931  * considered, for example in atomic check functions.
932  */
933 #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
934 	for ((__i) = 0;								\
935 	     (__i) < (__state)->num_connector;					\
936 	     (__i)++)								\
937 		for_each_if ((__state)->connectors[__i].ptr &&			\
938 			     ((connector) = (__state)->connectors[__i].ptr,	\
939 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
940 			     (old_connector_state) = (__state)->connectors[__i].old_state,	\
941 			     (new_connector_state) = (__state)->connectors[__i].new_state, 1))
942 
943 /**
944  * for_each_old_connector_in_state - iterate over all connectors in an atomic update
945  * @__state: &struct drm_atomic_state pointer
946  * @connector: &struct drm_connector iteration cursor
947  * @old_connector_state: &struct drm_connector_state iteration cursor for the
948  * 	old state
949  * @__i: int iteration cursor, for macro-internal use
950  *
951  * This iterates over all connectors in an atomic update, tracking only the old
952  * state. This is useful in disable functions, where we need the old state the
953  * hardware is still in.
954  */
955 #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
956 	for ((__i) = 0;								\
957 	     (__i) < (__state)->num_connector;					\
958 	     (__i)++)								\
959 		for_each_if ((__state)->connectors[__i].ptr &&			\
960 			     ((connector) = (__state)->connectors[__i].ptr,	\
961 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
962 			     (old_connector_state) = (__state)->connectors[__i].old_state, 1))
963 
964 /**
965  * for_each_new_connector_in_state - iterate over all connectors in an atomic update
966  * @__state: &struct drm_atomic_state pointer
967  * @connector: &struct drm_connector iteration cursor
968  * @new_connector_state: &struct drm_connector_state iteration cursor for the
969  * 	new state
970  * @__i: int iteration cursor, for macro-internal use
971  *
972  * This iterates over all connectors in an atomic update, tracking only the new
973  * state. This is useful in enable functions, where we need the new state the
974  * hardware should be in when the atomic commit operation has completed.
975  */
976 #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
977 	for ((__i) = 0;								\
978 	     (__i) < (__state)->num_connector;					\
979 	     (__i)++)								\
980 		for_each_if ((__state)->connectors[__i].ptr &&			\
981 			     ((connector) = (__state)->connectors[__i].ptr,	\
982 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
983 			     (new_connector_state) = (__state)->connectors[__i].new_state, \
984 			     (void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))
985 
986 /**
987  * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
988  * @__state: &struct drm_atomic_state pointer
989  * @crtc: &struct drm_crtc iteration cursor
990  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
991  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
992  * @__i: int iteration cursor, for macro-internal use
993  *
994  * This iterates over all CRTCs in an atomic update, tracking both old and
995  * new state. This is useful in places where the state delta needs to be
996  * considered, for example in atomic check functions.
997  */
998 #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
999 	for ((__i) = 0;							\
1000 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1001 	     (__i)++)							\
1002 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1003 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1004 			      (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1005 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, \
1006 			     (void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \
1007 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, \
1008 			     (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
1009 
1010 /**
1011  * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
1012  * @__state: &struct drm_atomic_state pointer
1013  * @crtc: &struct drm_crtc iteration cursor
1014  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
1015  * @__i: int iteration cursor, for macro-internal use
1016  *
1017  * This iterates over all CRTCs in an atomic update, tracking only the old
1018  * state. This is useful in disable functions, where we need the old state the
1019  * hardware is still in.
1020  */
1021 #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i)	\
1022 	for ((__i) = 0;							\
1023 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1024 	     (__i)++)							\
1025 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1026 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1027 			     (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1028 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
1029 
1030 /**
1031  * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
1032  * @__state: &struct drm_atomic_state pointer
1033  * @crtc: &struct drm_crtc iteration cursor
1034  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
1035  * @__i: int iteration cursor, for macro-internal use
1036  *
1037  * This iterates over all CRTCs in an atomic update, tracking only the new
1038  * state. This is useful in enable functions, where we need the new state the
1039  * hardware should be in when the atomic commit operation has completed.
1040  */
1041 #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i)	\
1042 	for ((__i) = 0;							\
1043 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1044 	     (__i)++)							\
1045 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1046 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1047 			     (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1048 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, \
1049 			     (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
1050 
1051 /**
1052  * for_each_oldnew_colorop_in_state - iterate over all colorops in an atomic update
1053  * @__state: &struct drm_atomic_state pointer
1054  * @colorop: &struct drm_colorop iteration cursor
1055  * @old_colorop_state: &struct drm_colorop_state iteration cursor for the old state
1056  * @new_colorop_state: &struct drm_colorop_state iteration cursor for the new state
1057  * @__i: int iteration cursor, for macro-internal use
1058  *
1059  * This iterates over all colorops in an atomic update, tracking both old and
1060  * new state. This is useful in places where the state delta needs to be
1061  * considered, for example in atomic check functions.
1062  */
1063 #define for_each_oldnew_colorop_in_state(__state, colorop, old_colorop_state,	\
1064 					 new_colorop_state, __i)		\
1065 	for ((__i) = 0;								\
1066 	     (__i) < (__state)->dev->mode_config.num_colorop;			\
1067 	     (__i)++)								\
1068 		for_each_if ((__state)->colorops[__i].ptr &&			\
1069 			     ((colorop) = (__state)->colorops[__i].ptr,		\
1070 			      (void)(colorop) /* Only to avoid unused-but-set-variable warning */, \
1071 			      (old_colorop_state) = (__state)->colorops[__i].old_state,\
1072 			      (new_colorop_state) = (__state)->colorops[__i].new_state, 1))
1073 
1074 /**
1075  * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
1076  * @__state: &struct drm_atomic_state pointer
1077  * @plane: &struct drm_plane iteration cursor
1078  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1079  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1080  * @__i: int iteration cursor, for macro-internal use
1081  *
1082  * This iterates over all planes in an atomic update, tracking both old and
1083  * new state. This is useful in places where the state delta needs to be
1084  * considered, for example in atomic check functions.
1085  */
1086 #define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
1087 	for ((__i) = 0;							\
1088 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1089 	     (__i)++)							\
1090 		for_each_if ((__state)->planes[__i].ptr &&		\
1091 			     ((plane) = (__state)->planes[__i].ptr,	\
1092 			      (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
1093 			      (old_plane_state) = (__state)->planes[__i].old_state,\
1094 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1095 
1096 /**
1097  * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
1098  * update in reverse order
1099  * @__state: &struct drm_atomic_state pointer
1100  * @plane: &struct drm_plane iteration cursor
1101  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1102  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1103  * @__i: int iteration cursor, for macro-internal use
1104  *
1105  * This iterates over all planes in an atomic update in reverse order,
1106  * tracking both old and  new state. This is useful in places where the
1107  * state delta needs to be considered, for example in atomic check functions.
1108  */
1109 #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
1110 	for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1);	\
1111 	     (__i) >= 0;						\
1112 	     (__i)--)							\
1113 		for_each_if ((__state)->planes[__i].ptr &&		\
1114 			     ((plane) = (__state)->planes[__i].ptr,	\
1115 			      (old_plane_state) = (__state)->planes[__i].old_state,\
1116 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1117 
1118 /**
1119  * for_each_new_plane_in_state_reverse - other than only tracking new state,
1120  * it's the same as for_each_oldnew_plane_in_state_reverse
1121  * @__state: &struct drm_atomic_state pointer
1122  * @plane: &struct drm_plane iteration cursor
1123  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1124  * @__i: int iteration cursor, for macro-internal use
1125  */
1126 #define for_each_new_plane_in_state_reverse(__state, plane, new_plane_state, __i) \
1127 	for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1);	\
1128 	     (__i) >= 0;						\
1129 	     (__i)--)							\
1130 		for_each_if ((__state)->planes[__i].ptr &&		\
1131 			     ((plane) = (__state)->planes[__i].ptr,	\
1132 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1133 
1134 /**
1135  * for_each_old_plane_in_state - iterate over all planes in an atomic update
1136  * @__state: &struct drm_atomic_state pointer
1137  * @plane: &struct drm_plane iteration cursor
1138  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1139  * @__i: int iteration cursor, for macro-internal use
1140  *
1141  * This iterates over all planes in an atomic update, tracking only the old
1142  * state. This is useful in disable functions, where we need the old state the
1143  * hardware is still in.
1144  */
1145 #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
1146 	for ((__i) = 0;							\
1147 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1148 	     (__i)++)							\
1149 		for_each_if ((__state)->planes[__i].ptr &&		\
1150 			     ((plane) = (__state)->planes[__i].ptr,	\
1151 			      (old_plane_state) = (__state)->planes[__i].old_state, 1))
1152 /**
1153  * for_each_new_plane_in_state - iterate over all planes in an atomic update
1154  * @__state: &struct drm_atomic_state pointer
1155  * @plane: &struct drm_plane iteration cursor
1156  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1157  * @__i: int iteration cursor, for macro-internal use
1158  *
1159  * This iterates over all planes in an atomic update, tracking only the new
1160  * state. This is useful in enable functions, where we need the new state the
1161  * hardware should be in when the atomic commit operation has completed.
1162  */
1163 #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
1164 	for ((__i) = 0;							\
1165 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1166 	     (__i)++)							\
1167 		for_each_if ((__state)->planes[__i].ptr &&		\
1168 			     ((plane) = (__state)->planes[__i].ptr,	\
1169 			      (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
1170 			      (new_plane_state) = (__state)->planes[__i].new_state, \
1171 			      (void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))
1172 
1173 /**
1174  * for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update
1175  * @__state: &struct drm_atomic_state pointer
1176  * @obj: &struct drm_private_obj iteration cursor
1177  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
1178  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
1179  * @__i: int iteration cursor, for macro-internal use
1180  *
1181  * This iterates over all private objects in an atomic update, tracking both
1182  * old and new state. This is useful in places where the state delta needs
1183  * to be considered, for example in atomic check functions.
1184  */
1185 #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
1186 	for ((__i) = 0; \
1187 	     (__i) < (__state)->num_private_objs && \
1188 		     ((obj) = (__state)->private_objs[__i].ptr, \
1189 		      (old_obj_state) = (__state)->private_objs[__i].old_state,	\
1190 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
1191 	     (__i)++)
1192 
1193 /**
1194  * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
1195  * @__state: &struct drm_atomic_state pointer
1196  * @obj: &struct drm_private_obj iteration cursor
1197  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
1198  * @__i: int iteration cursor, for macro-internal use
1199  *
1200  * This iterates over all private objects in an atomic update, tracking only
1201  * the old state. This is useful in disable functions, where we need the old
1202  * state the hardware is still in.
1203  */
1204 #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
1205 	for ((__i) = 0; \
1206 	     (__i) < (__state)->num_private_objs && \
1207 		     ((obj) = (__state)->private_objs[__i].ptr, \
1208 		      (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
1209 	     (__i)++)
1210 
1211 /**
1212  * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
1213  * @__state: &struct drm_atomic_state pointer
1214  * @obj: &struct drm_private_obj iteration cursor
1215  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
1216  * @__i: int iteration cursor, for macro-internal use
1217  *
1218  * This iterates over all private objects in an atomic update, tracking only
1219  * the new state. This is useful in enable functions, where we need the new state the
1220  * hardware should be in when the atomic commit operation has completed.
1221  */
1222 #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
1223 	for ((__i) = 0; \
1224 	     (__i) < (__state)->num_private_objs && \
1225 		     ((obj) = (__state)->private_objs[__i].ptr, \
1226 		      (void)(obj) /* Only to avoid unused-but-set-variable warning */, \
1227 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
1228 	     (__i)++)
1229 
1230 /**
1231  * drm_atomic_crtc_needs_modeset - compute combined modeset need
1232  * @state: &drm_crtc_state for the CRTC
1233  *
1234  * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
1235  * whether the state CRTC changed enough to need a full modeset cycle:
1236  * mode_changed, active_changed and connectors_changed. This helper simply
1237  * combines these three to compute the overall need for a modeset for @state.
1238  *
1239  * The atomic helper code sets these booleans, but drivers can and should
1240  * change them appropriately to accurately represent whether a modeset is
1241  * really needed. In general, drivers should avoid full modesets whenever
1242  * possible.
1243  *
1244  * For example if the CRTC mode has changed, and the hardware is able to enact
1245  * the requested mode change without going through a full modeset, the driver
1246  * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
1247  * implementation.
1248  */
1249 static inline bool
1250 drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
1251 {
1252 	return state->mode_changed || state->active_changed ||
1253 	       state->connectors_changed;
1254 }
1255 
1256 /**
1257  * drm_atomic_crtc_effectively_active - compute whether CRTC is actually active
1258  * @state: &drm_crtc_state for the CRTC
1259  *
1260  * When in self refresh mode, the crtc_state->active value will be false, since
1261  * the CRTC is off. However in some cases we're interested in whether the CRTC
1262  * is active, or effectively active (ie: it's connected to an active display).
1263  * In these cases, use this function instead of just checking active.
1264  */
1265 static inline bool
1266 drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)
1267 {
1268 	return state->active || state->self_refresh_active;
1269 }
1270 
1271 /**
1272  * struct drm_bus_cfg - bus configuration
1273  *
1274  * This structure stores the configuration of a physical bus between two
1275  * components in an output pipeline, usually between two bridges, an encoder
1276  * and a bridge, or a bridge and a connector.
1277  *
1278  * The bus configuration is stored in &drm_bridge_state separately for the
1279  * input and output buses, as seen from the point of view of each bridge. The
1280  * bus configuration of a bridge output is usually identical to the
1281  * configuration of the next bridge's input, but may differ if the signals are
1282  * modified between the two bridges, for instance by an inverter on the board.
1283  * The input and output configurations of a bridge may differ if the bridge
1284  * modifies the signals internally, for instance by performing format
1285  * conversion, or modifying signals polarities.
1286  */
1287 struct drm_bus_cfg {
1288 	/**
1289 	 * @format: format used on this bus (one of the MEDIA_BUS_FMT_* format)
1290 	 *
1291 	 * This field should not be directly modified by drivers
1292 	 * (drm_atomic_bridge_chain_select_bus_fmts() takes care of the bus
1293 	 * format negotiation).
1294 	 */
1295 	u32 format;
1296 
1297 	/**
1298 	 * @flags: DRM_BUS_* flags used on this bus
1299 	 */
1300 	u32 flags;
1301 };
1302 
1303 /**
1304  * struct drm_bridge_state - Atomic bridge state object
1305  */
1306 struct drm_bridge_state {
1307 	/**
1308 	 * @base: inherit from &drm_private_state
1309 	 */
1310 	struct drm_private_state base;
1311 
1312 	/**
1313 	 * @bridge: the bridge this state refers to
1314 	 */
1315 	struct drm_bridge *bridge;
1316 
1317 	/**
1318 	 * @input_bus_cfg: input bus configuration
1319 	 */
1320 	struct drm_bus_cfg input_bus_cfg;
1321 
1322 	/**
1323 	 * @output_bus_cfg: output bus configuration
1324 	 */
1325 	struct drm_bus_cfg output_bus_cfg;
1326 };
1327 
1328 static inline struct drm_bridge_state *
1329 drm_priv_to_bridge_state(struct drm_private_state *priv)
1330 {
1331 	return container_of(priv, struct drm_bridge_state, base);
1332 }
1333 
1334 struct drm_bridge_state *
1335 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1336 			    struct drm_bridge *bridge);
1337 struct drm_bridge_state *
1338 drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,
1339 				struct drm_bridge *bridge);
1340 struct drm_bridge_state *
1341 drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,
1342 				struct drm_bridge *bridge);
1343 
1344 #endif /* DRM_ATOMIC_H_ */
1345