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