xref: /linux/include/drm/drm_atomic.h (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
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 	 * @plane_color_pipeline:
542 	 *
543 	 * Indicates whether this atomic state originated with a client that
544 	 * set the DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE.
545 	 *
546 	 * Drivers and helper functions should use this to ignore legacy
547 	 * properties that are incompatible with the drm_plane COLOR_PIPELINE
548 	 * behavior, such as:
549 	 *
550 	 *  - COLOR_RANGE
551 	 *  - COLOR_ENCODING
552 	 *
553 	 * or any other driver-specific properties that might affect pixel
554 	 * values.
555 	 */
556 	bool plane_color_pipeline : 1;
557 
558 	/**
559 	 * @colorops:
560 	 *
561 	 * Pointer to array of @drm_colorop and @drm_colorop_state part of this
562 	 * update.
563 	 */
564 	struct __drm_colorops_state *colorops;
565 
566 	/**
567 	 * @planes:
568 	 *
569 	 * Pointer to array of @drm_plane and @drm_plane_state part of this
570 	 * update.
571 	 */
572 	struct __drm_planes_state *planes;
573 
574 	/**
575 	 * @crtcs:
576 	 *
577 	 * Pointer to array of @drm_crtc and @drm_crtc_state part of this
578 	 * update.
579 	 */
580 	struct __drm_crtcs_state *crtcs;
581 
582 	/**
583 	 * @num_connector: size of the @connectors array
584 	 */
585 	int num_connector;
586 
587 	/**
588 	 * @connectors:
589 	 *
590 	 * Pointer to array of @drm_connector and @drm_connector_state part of
591 	 * this update.
592 	 */
593 	struct __drm_connnectors_state *connectors;
594 
595 	/**
596 	 * @num_private_objs: size of the @private_objs array
597 	 */
598 	int num_private_objs;
599 
600 	/**
601 	 * @private_objs:
602 	 *
603 	 * Pointer to array of @drm_private_obj and @drm_private_obj_state part
604 	 * of this update.
605 	 */
606 	struct __drm_private_objs_state *private_objs;
607 
608 	/**
609 	 * @acquire_ctx: acquire context for this atomic modeset state update
610 	 */
611 	struct drm_modeset_acquire_ctx *acquire_ctx;
612 
613 	/**
614 	 * @fake_commit:
615 	 *
616 	 * Used for signaling unbound planes/connectors.
617 	 * When a connector or plane is not bound to any CRTC, it's still important
618 	 * to preserve linearity to prevent the atomic states from being freed too early.
619 	 *
620 	 * This commit (if set) is not bound to any CRTC, but will be completed when
621 	 * drm_atomic_helper_commit_hw_done() is called.
622 	 */
623 	struct drm_crtc_commit *fake_commit;
624 
625 	/**
626 	 * @commit_work:
627 	 *
628 	 * Work item which can be used by the driver or helpers to execute the
629 	 * commit without blocking.
630 	 */
631 	struct work_struct commit_work;
632 };
633 
634 void __drm_crtc_commit_free(struct kref *kref);
635 
636 /**
637  * drm_crtc_commit_get - acquire a reference to the CRTC commit
638  * @commit: CRTC commit
639  *
640  * Increases the reference of @commit.
641  *
642  * Returns:
643  * The pointer to @commit, with reference increased.
644  */
drm_crtc_commit_get(struct drm_crtc_commit * commit)645 static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
646 {
647 	kref_get(&commit->ref);
648 	return commit;
649 }
650 
651 /**
652  * drm_crtc_commit_put - release a reference to the CRTC commmit
653  * @commit: CRTC commit
654  *
655  * This releases a reference to @commit which is freed after removing the
656  * final reference. No locking required and callable from any context.
657  */
drm_crtc_commit_put(struct drm_crtc_commit * commit)658 static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
659 {
660 	kref_put(&commit->ref, __drm_crtc_commit_free);
661 }
662 
663 int drm_crtc_commit_wait(struct drm_crtc_commit *commit);
664 
665 struct drm_atomic_state * __must_check
666 drm_atomic_state_alloc(struct drm_device *dev);
667 void drm_atomic_state_clear(struct drm_atomic_state *state);
668 
669 /**
670  * drm_atomic_state_get - acquire a reference to the atomic state
671  * @state: The atomic state
672  *
673  * Returns a new reference to the @state
674  */
675 static inline struct drm_atomic_state *
drm_atomic_state_get(struct drm_atomic_state * state)676 drm_atomic_state_get(struct drm_atomic_state *state)
677 {
678 	kref_get(&state->ref);
679 	return state;
680 }
681 
682 void __drm_atomic_state_free(struct kref *ref);
683 
684 /**
685  * drm_atomic_state_put - release a reference to the atomic state
686  * @state: The atomic state
687  *
688  * This releases a reference to @state which is freed after removing the
689  * final reference. No locking required and callable from any context.
690  */
drm_atomic_state_put(struct drm_atomic_state * state)691 static inline void drm_atomic_state_put(struct drm_atomic_state *state)
692 {
693 	kref_put(&state->ref, __drm_atomic_state_free);
694 }
695 
696 int  __must_check
697 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
698 void drm_atomic_state_default_clear(struct drm_atomic_state *state);
699 void drm_atomic_state_default_release(struct drm_atomic_state *state);
700 
701 struct drm_crtc_state * __must_check
702 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
703 			  struct drm_crtc *crtc);
704 struct drm_plane_state * __must_check
705 drm_atomic_get_plane_state(struct drm_atomic_state *state,
706 			   struct drm_plane *plane);
707 struct drm_colorop_state *
708 drm_atomic_get_colorop_state(struct drm_atomic_state *state,
709 			     struct drm_colorop *colorop);
710 struct drm_connector_state * __must_check
711 drm_atomic_get_connector_state(struct drm_atomic_state *state,
712 			       struct drm_connector *connector);
713 
714 void drm_atomic_private_obj_init(struct drm_device *dev,
715 				 struct drm_private_obj *obj,
716 				 struct drm_private_state *state,
717 				 const struct drm_private_state_funcs *funcs);
718 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
719 
720 struct drm_private_state * __must_check
721 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
722 				 struct drm_private_obj *obj);
723 struct drm_private_state *
724 drm_atomic_get_old_private_obj_state(const struct drm_atomic_state *state,
725 				     struct drm_private_obj *obj);
726 struct drm_private_state *
727 drm_atomic_get_new_private_obj_state(const struct drm_atomic_state *state,
728 				     struct drm_private_obj *obj);
729 
730 struct drm_connector *
731 drm_atomic_get_old_connector_for_encoder(const struct drm_atomic_state *state,
732 					 struct drm_encoder *encoder);
733 struct drm_connector *
734 drm_atomic_get_new_connector_for_encoder(const struct drm_atomic_state *state,
735 					 struct drm_encoder *encoder);
736 struct drm_connector *
737 drm_atomic_get_connector_for_encoder(const struct drm_encoder *encoder,
738 				     struct drm_modeset_acquire_ctx *ctx);
739 
740 struct drm_crtc *
741 drm_atomic_get_old_crtc_for_encoder(struct drm_atomic_state *state,
742 					 struct drm_encoder *encoder);
743 struct drm_crtc *
744 drm_atomic_get_new_crtc_for_encoder(struct drm_atomic_state *state,
745 					 struct drm_encoder *encoder);
746 
747 /**
748  * drm_atomic_get_old_crtc_state - get old CRTC state, if it exists
749  * @state: global atomic state object
750  * @crtc: CRTC to grab
751  *
752  * This function returns the old CRTC state for the given CRTC, or
753  * NULL if the CRTC is not part of the global atomic state.
754  */
755 static inline struct drm_crtc_state *
drm_atomic_get_old_crtc_state(const struct drm_atomic_state * state,struct drm_crtc * crtc)756 drm_atomic_get_old_crtc_state(const struct drm_atomic_state *state,
757 			      struct drm_crtc *crtc)
758 {
759 	return state->crtcs[drm_crtc_index(crtc)].old_state;
760 }
761 /**
762  * drm_atomic_get_new_crtc_state - get new CRTC state, if it exists
763  * @state: global atomic state object
764  * @crtc: CRTC to grab
765  *
766  * This function returns the new CRTC state for the given CRTC, or
767  * NULL if the CRTC is not part of the global atomic state.
768  */
769 static inline struct drm_crtc_state *
drm_atomic_get_new_crtc_state(const struct drm_atomic_state * state,struct drm_crtc * crtc)770 drm_atomic_get_new_crtc_state(const struct drm_atomic_state *state,
771 			      struct drm_crtc *crtc)
772 {
773 	return state->crtcs[drm_crtc_index(crtc)].new_state;
774 }
775 
776 /**
777  * drm_atomic_get_old_plane_state - get plane state, if it exists
778  * @state: global atomic state object
779  * @plane: plane to grab
780  *
781  * This function returns the old plane state for the given plane, or
782  * NULL if the plane is not part of the global atomic state.
783  */
784 static inline struct drm_plane_state *
drm_atomic_get_old_plane_state(const struct drm_atomic_state * state,struct drm_plane * plane)785 drm_atomic_get_old_plane_state(const struct drm_atomic_state *state,
786 			       struct drm_plane *plane)
787 {
788 	return state->planes[drm_plane_index(plane)].old_state;
789 }
790 
791 /**
792  * drm_atomic_get_new_plane_state - get plane state, if it exists
793  * @state: global atomic state object
794  * @plane: plane to grab
795  *
796  * This function returns the new plane state for the given plane, or
797  * NULL if the plane is not part of the global atomic state.
798  */
799 static inline struct drm_plane_state *
drm_atomic_get_new_plane_state(const struct drm_atomic_state * state,struct drm_plane * plane)800 drm_atomic_get_new_plane_state(const struct drm_atomic_state *state,
801 			       struct drm_plane *plane)
802 {
803 	return state->planes[drm_plane_index(plane)].new_state;
804 }
805 
806 /**
807  * drm_atomic_get_old_colorop_state - get colorop state, if it exists
808  * @state: global atomic state object
809  * @colorop: colorop to grab
810  *
811  * This function returns the old colorop state for the given colorop, or
812  * NULL if the colorop is not part of the global atomic state.
813  */
814 static inline struct drm_colorop_state *
drm_atomic_get_old_colorop_state(struct drm_atomic_state * state,struct drm_colorop * colorop)815 drm_atomic_get_old_colorop_state(struct drm_atomic_state *state,
816 				 struct drm_colorop *colorop)
817 {
818 	return state->colorops[drm_colorop_index(colorop)].old_state;
819 }
820 
821 /**
822  * drm_atomic_get_new_colorop_state - get colorop state, if it exists
823  * @state: global atomic state object
824  * @colorop: colorop to grab
825  *
826  * This function returns the new colorop state for the given colorop, or
827  * NULL if the colorop is not part of the global atomic state.
828  */
829 static inline struct drm_colorop_state *
drm_atomic_get_new_colorop_state(struct drm_atomic_state * state,struct drm_colorop * colorop)830 drm_atomic_get_new_colorop_state(struct drm_atomic_state *state,
831 				 struct drm_colorop *colorop)
832 {
833 	return state->colorops[drm_colorop_index(colorop)].new_state;
834 }
835 
836 /**
837  * drm_atomic_get_old_connector_state - get connector state, if it exists
838  * @state: global atomic state object
839  * @connector: connector to grab
840  *
841  * This function returns the old connector state for the given connector,
842  * or NULL if the connector is not part of the global atomic state.
843  */
844 static inline struct drm_connector_state *
drm_atomic_get_old_connector_state(const struct drm_atomic_state * state,struct drm_connector * connector)845 drm_atomic_get_old_connector_state(const struct drm_atomic_state *state,
846 				   struct drm_connector *connector)
847 {
848 	int index = drm_connector_index(connector);
849 
850 	if (index >= state->num_connector)
851 		return NULL;
852 
853 	return state->connectors[index].old_state;
854 }
855 
856 /**
857  * drm_atomic_get_new_connector_state - get connector state, if it exists
858  * @state: global atomic state object
859  * @connector: connector to grab
860  *
861  * This function returns the new connector state for the given connector,
862  * or NULL if the connector is not part of the global atomic state.
863  */
864 static inline struct drm_connector_state *
drm_atomic_get_new_connector_state(const struct drm_atomic_state * state,struct drm_connector * connector)865 drm_atomic_get_new_connector_state(const struct drm_atomic_state *state,
866 				   struct drm_connector *connector)
867 {
868 	int index = drm_connector_index(connector);
869 
870 	if (index >= state->num_connector)
871 		return NULL;
872 
873 	return state->connectors[index].new_state;
874 }
875 
876 /**
877  * __drm_atomic_get_current_plane_state - get current plane state
878  * @state: global atomic state object
879  * @plane: plane to grab
880  *
881  * This function returns the plane state for the given plane, either the
882  * new plane state from @state, or if the plane isn't part of the atomic
883  * state update, from @plane. This is useful in atomic check callbacks,
884  * when drivers need to peek at, but not change, state of other planes,
885  * since it avoids threading an error code back up the call chain.
886  *
887  * WARNING:
888  *
889  * Note that this function is in general unsafe since it doesn't check for the
890  * required locking for access state structures. Drivers must ensure that it is
891  * safe to access the returned state structure through other means. One common
892  * example is when planes are fixed to a single CRTC, and the driver knows that
893  * the CRTC lock is held already. In that case holding the CRTC lock gives a
894  * read-lock on all planes connected to that CRTC. But if planes can be
895  * reassigned things get more tricky. In that case it's better to use
896  * drm_atomic_get_plane_state and wire up full error handling.
897  *
898  * Returns:
899  *
900  * Read-only pointer to the current plane state.
901  */
902 static inline const struct drm_plane_state *
__drm_atomic_get_current_plane_state(const struct drm_atomic_state * state,struct drm_plane * plane)903 __drm_atomic_get_current_plane_state(const struct drm_atomic_state *state,
904 				     struct drm_plane *plane)
905 {
906 	struct drm_plane_state *plane_state;
907 
908 	plane_state = drm_atomic_get_new_plane_state(state, plane);
909 	if (plane_state)
910 		return plane_state;
911 
912 	/*
913 	 * If the plane isn't part of the state, fallback to the currently active one.
914 	 */
915 	return plane->state;
916 }
917 
918 int __must_check
919 drm_atomic_add_encoder_bridges(struct drm_atomic_state *state,
920 			       struct drm_encoder *encoder);
921 int __must_check
922 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
923 				   struct drm_crtc *crtc);
924 int __must_check
925 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
926 			       struct drm_crtc *crtc);
927 int __must_check
928 drm_atomic_add_affected_colorops(struct drm_atomic_state *state,
929 				 struct drm_plane *plane);
930 
931 int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
932 int __must_check drm_atomic_commit(struct drm_atomic_state *state);
933 int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
934 
935 void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
936 
937 /**
938  * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
939  * @__state: &struct drm_atomic_state pointer
940  * @connector: &struct drm_connector iteration cursor
941  * @old_connector_state: &struct drm_connector_state iteration cursor for the
942  * 	old state
943  * @new_connector_state: &struct drm_connector_state iteration cursor for the
944  * 	new state
945  * @__i: int iteration cursor, for macro-internal use
946  *
947  * This iterates over all connectors in an atomic update, tracking both old and
948  * new state. This is useful in places where the state delta needs to be
949  * considered, for example in atomic check functions.
950  */
951 #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
952 	for ((__i) = 0;								\
953 	     (__i) < (__state)->num_connector;					\
954 	     (__i)++)								\
955 		for_each_if ((__state)->connectors[__i].ptr &&			\
956 			     ((connector) = (__state)->connectors[__i].ptr,	\
957 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
958 			     (old_connector_state) = (__state)->connectors[__i].old_state,	\
959 			     (new_connector_state) = (__state)->connectors[__i].new_state, 1))
960 
961 /**
962  * for_each_old_connector_in_state - iterate over all connectors in an atomic update
963  * @__state: &struct drm_atomic_state pointer
964  * @connector: &struct drm_connector iteration cursor
965  * @old_connector_state: &struct drm_connector_state iteration cursor for the
966  * 	old state
967  * @__i: int iteration cursor, for macro-internal use
968  *
969  * This iterates over all connectors in an atomic update, tracking only the old
970  * state. This is useful in disable functions, where we need the old state the
971  * hardware is still in.
972  */
973 #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
974 	for ((__i) = 0;								\
975 	     (__i) < (__state)->num_connector;					\
976 	     (__i)++)								\
977 		for_each_if ((__state)->connectors[__i].ptr &&			\
978 			     ((connector) = (__state)->connectors[__i].ptr,	\
979 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
980 			     (old_connector_state) = (__state)->connectors[__i].old_state, 1))
981 
982 /**
983  * for_each_new_connector_in_state - iterate over all connectors in an atomic update
984  * @__state: &struct drm_atomic_state pointer
985  * @connector: &struct drm_connector iteration cursor
986  * @new_connector_state: &struct drm_connector_state iteration cursor for the
987  * 	new state
988  * @__i: int iteration cursor, for macro-internal use
989  *
990  * This iterates over all connectors in an atomic update, tracking only the new
991  * state. This is useful in enable functions, where we need the new state the
992  * hardware should be in when the atomic commit operation has completed.
993  */
994 #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
995 	for ((__i) = 0;								\
996 	     (__i) < (__state)->num_connector;					\
997 	     (__i)++)								\
998 		for_each_if ((__state)->connectors[__i].ptr &&			\
999 			     ((connector) = (__state)->connectors[__i].ptr,	\
1000 			     (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
1001 			     (new_connector_state) = (__state)->connectors[__i].new_state, \
1002 			     (void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))
1003 
1004 /**
1005  * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
1006  * @__state: &struct drm_atomic_state pointer
1007  * @crtc: &struct drm_crtc iteration cursor
1008  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
1009  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
1010  * @__i: int iteration cursor, for macro-internal use
1011  *
1012  * This iterates over all CRTCs in an atomic update, tracking both old and
1013  * new state. This is useful in places where the state delta needs to be
1014  * considered, for example in atomic check functions.
1015  */
1016 #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
1017 	for ((__i) = 0;							\
1018 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1019 	     (__i)++)							\
1020 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1021 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1022 			      (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1023 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, \
1024 			     (void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \
1025 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, \
1026 			     (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
1027 
1028 /**
1029  * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
1030  * @__state: &struct drm_atomic_state pointer
1031  * @crtc: &struct drm_crtc iteration cursor
1032  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
1033  * @__i: int iteration cursor, for macro-internal use
1034  *
1035  * This iterates over all CRTCs in an atomic update, tracking only the old
1036  * state. This is useful in disable functions, where we need the old state the
1037  * hardware is still in.
1038  */
1039 #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i)	\
1040 	for ((__i) = 0;							\
1041 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1042 	     (__i)++)							\
1043 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1044 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1045 			     (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1046 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
1047 
1048 /**
1049  * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
1050  * @__state: &struct drm_atomic_state pointer
1051  * @crtc: &struct drm_crtc iteration cursor
1052  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
1053  * @__i: int iteration cursor, for macro-internal use
1054  *
1055  * This iterates over all CRTCs in an atomic update, tracking only the new
1056  * state. This is useful in enable functions, where we need the new state the
1057  * hardware should be in when the atomic commit operation has completed.
1058  */
1059 #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i)	\
1060 	for ((__i) = 0;							\
1061 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
1062 	     (__i)++)							\
1063 		for_each_if ((__state)->crtcs[__i].ptr &&		\
1064 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
1065 			     (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
1066 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, \
1067 			     (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
1068 
1069 /**
1070  * for_each_oldnew_colorop_in_state - iterate over all colorops in an atomic update
1071  * @__state: &struct drm_atomic_state pointer
1072  * @colorop: &struct drm_colorop iteration cursor
1073  * @old_colorop_state: &struct drm_colorop_state iteration cursor for the old state
1074  * @new_colorop_state: &struct drm_colorop_state iteration cursor for the new state
1075  * @__i: int iteration cursor, for macro-internal use
1076  *
1077  * This iterates over all colorops in an atomic update, tracking both old and
1078  * new state. This is useful in places where the state delta needs to be
1079  * considered, for example in atomic check functions.
1080  */
1081 #define for_each_oldnew_colorop_in_state(__state, colorop, old_colorop_state,	\
1082 					 new_colorop_state, __i)		\
1083 	for ((__i) = 0;								\
1084 	     (__i) < (__state)->dev->mode_config.num_colorop;			\
1085 	     (__i)++)								\
1086 		for_each_if ((__state)->colorops[__i].ptr &&			\
1087 			     ((colorop) = (__state)->colorops[__i].ptr,		\
1088 			      (void)(colorop) /* Only to avoid unused-but-set-variable warning */, \
1089 			      (old_colorop_state) = (__state)->colorops[__i].old_state,\
1090 			      (new_colorop_state) = (__state)->colorops[__i].new_state, 1))
1091 
1092 /**
1093  * for_each_new_colorop_in_state - iterate over all colorops in an atomic update
1094  * @__state: &struct drm_atomic_state pointer
1095  * @colorop: &struct drm_colorop iteration cursor
1096  * @new_colorop_state: &struct drm_colorop_state iteration cursor for the new state
1097  * @__i: int iteration cursor, for macro-internal use
1098  *
1099  * This iterates over all colorops in an atomic update, tracking new state. This is
1100  * useful in places where the state delta needs to be considered, for example in
1101  * atomic check functions.
1102  */
1103 #define for_each_new_colorop_in_state(__state, colorop, new_colorop_state, __i) \
1104 	for ((__i) = 0;							\
1105 	     (__i) < (__state)->dev->mode_config.num_colorop;		\
1106 	     (__i)++)							\
1107 		for_each_if ((__state)->colorops[__i].ptr &&		\
1108 			     ((colorop) = (__state)->colorops[__i].ptr,	\
1109 			      (void)(colorop) /* Only to avoid unused-but-set-variable warning */, \
1110 			      (new_colorop_state) = (__state)->colorops[__i].new_state, 1))
1111 
1112 /**
1113  * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
1114  * @__state: &struct drm_atomic_state pointer
1115  * @plane: &struct drm_plane iteration cursor
1116  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1117  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1118  * @__i: int iteration cursor, for macro-internal use
1119  *
1120  * This iterates over all planes in an atomic update, tracking both old and
1121  * new state. This is useful in places where the state delta needs to be
1122  * considered, for example in atomic check functions.
1123  */
1124 #define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
1125 	for ((__i) = 0;							\
1126 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1127 	     (__i)++)							\
1128 		for_each_if ((__state)->planes[__i].ptr &&		\
1129 			     ((plane) = (__state)->planes[__i].ptr,	\
1130 			      (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
1131 			      (old_plane_state) = (__state)->planes[__i].old_state,\
1132 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1133 
1134 /**
1135  * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
1136  * update in reverse order
1137  * @__state: &struct drm_atomic_state pointer
1138  * @plane: &struct drm_plane iteration cursor
1139  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1140  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1141  * @__i: int iteration cursor, for macro-internal use
1142  *
1143  * This iterates over all planes in an atomic update in reverse order,
1144  * tracking both old and  new state. This is useful in places where the
1145  * state delta needs to be considered, for example in atomic check functions.
1146  */
1147 #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
1148 	for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1);	\
1149 	     (__i) >= 0;						\
1150 	     (__i)--)							\
1151 		for_each_if ((__state)->planes[__i].ptr &&		\
1152 			     ((plane) = (__state)->planes[__i].ptr,	\
1153 			      (old_plane_state) = (__state)->planes[__i].old_state,\
1154 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1155 
1156 /**
1157  * for_each_new_plane_in_state_reverse - other than only tracking new state,
1158  * it's the same as for_each_oldnew_plane_in_state_reverse
1159  * @__state: &struct drm_atomic_state pointer
1160  * @plane: &struct drm_plane iteration cursor
1161  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1162  * @__i: int iteration cursor, for macro-internal use
1163  */
1164 #define for_each_new_plane_in_state_reverse(__state, plane, new_plane_state, __i) \
1165 	for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1);	\
1166 	     (__i) >= 0;						\
1167 	     (__i)--)							\
1168 		for_each_if ((__state)->planes[__i].ptr &&		\
1169 			     ((plane) = (__state)->planes[__i].ptr,	\
1170 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
1171 
1172 /**
1173  * for_each_old_plane_in_state - iterate over all planes in an atomic update
1174  * @__state: &struct drm_atomic_state pointer
1175  * @plane: &struct drm_plane iteration cursor
1176  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
1177  * @__i: int iteration cursor, for macro-internal use
1178  *
1179  * This iterates over all planes in an atomic update, tracking only the old
1180  * state. This is useful in disable functions, where we need the old state the
1181  * hardware is still in.
1182  */
1183 #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
1184 	for ((__i) = 0;							\
1185 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1186 	     (__i)++)							\
1187 		for_each_if ((__state)->planes[__i].ptr &&		\
1188 			     ((plane) = (__state)->planes[__i].ptr,	\
1189 			      (old_plane_state) = (__state)->planes[__i].old_state, 1))
1190 /**
1191  * for_each_new_plane_in_state - iterate over all planes in an atomic update
1192  * @__state: &struct drm_atomic_state pointer
1193  * @plane: &struct drm_plane iteration cursor
1194  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
1195  * @__i: int iteration cursor, for macro-internal use
1196  *
1197  * This iterates over all planes in an atomic update, tracking only the new
1198  * state. This is useful in enable functions, where we need the new state the
1199  * hardware should be in when the atomic commit operation has completed.
1200  */
1201 #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
1202 	for ((__i) = 0;							\
1203 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
1204 	     (__i)++)							\
1205 		for_each_if ((__state)->planes[__i].ptr &&		\
1206 			     ((plane) = (__state)->planes[__i].ptr,	\
1207 			      (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
1208 			      (new_plane_state) = (__state)->planes[__i].new_state, \
1209 			      (void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))
1210 
1211 /**
1212  * for_each_oldnew_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  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
1216  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
1217  * @__i: int iteration cursor, for macro-internal use
1218  *
1219  * This iterates over all private objects in an atomic update, tracking both
1220  * old and new state. This is useful in places where the state delta needs
1221  * to be considered, for example in atomic check functions.
1222  */
1223 #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
1224 	for ((__i) = 0; \
1225 	     (__i) < (__state)->num_private_objs && \
1226 		     ((obj) = (__state)->private_objs[__i].ptr, \
1227 		      (old_obj_state) = (__state)->private_objs[__i].old_state,	\
1228 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
1229 	     (__i)++)
1230 
1231 /**
1232  * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
1233  * @__state: &struct drm_atomic_state pointer
1234  * @obj: &struct drm_private_obj iteration cursor
1235  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
1236  * @__i: int iteration cursor, for macro-internal use
1237  *
1238  * This iterates over all private objects in an atomic update, tracking only
1239  * the old state. This is useful in disable functions, where we need the old
1240  * state the hardware is still in.
1241  */
1242 #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
1243 	for ((__i) = 0; \
1244 	     (__i) < (__state)->num_private_objs && \
1245 		     ((obj) = (__state)->private_objs[__i].ptr, \
1246 		      (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
1247 	     (__i)++)
1248 
1249 /**
1250  * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
1251  * @__state: &struct drm_atomic_state pointer
1252  * @obj: &struct drm_private_obj iteration cursor
1253  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
1254  * @__i: int iteration cursor, for macro-internal use
1255  *
1256  * This iterates over all private objects in an atomic update, tracking only
1257  * the new state. This is useful in enable functions, where we need the new state the
1258  * hardware should be in when the atomic commit operation has completed.
1259  */
1260 #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
1261 	for ((__i) = 0; \
1262 	     (__i) < (__state)->num_private_objs && \
1263 		     ((obj) = (__state)->private_objs[__i].ptr, \
1264 		      (void)(obj) /* Only to avoid unused-but-set-variable warning */, \
1265 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
1266 	     (__i)++)
1267 
1268 /**
1269  * drm_atomic_crtc_needs_modeset - compute combined modeset need
1270  * @state: &drm_crtc_state for the CRTC
1271  *
1272  * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
1273  * whether the state CRTC changed enough to need a full modeset cycle:
1274  * mode_changed, active_changed and connectors_changed. This helper simply
1275  * combines these three to compute the overall need for a modeset for @state.
1276  *
1277  * The atomic helper code sets these booleans, but drivers can and should
1278  * change them appropriately to accurately represent whether a modeset is
1279  * really needed. In general, drivers should avoid full modesets whenever
1280  * possible.
1281  *
1282  * For example if the CRTC mode has changed, and the hardware is able to enact
1283  * the requested mode change without going through a full modeset, the driver
1284  * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
1285  * implementation.
1286  */
1287 static inline bool
drm_atomic_crtc_needs_modeset(const struct drm_crtc_state * state)1288 drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
1289 {
1290 	return state->mode_changed || state->active_changed ||
1291 	       state->connectors_changed;
1292 }
1293 
1294 /**
1295  * drm_atomic_crtc_effectively_active - compute whether CRTC is actually active
1296  * @state: &drm_crtc_state for the CRTC
1297  *
1298  * When in self refresh mode, the crtc_state->active value will be false, since
1299  * the CRTC is off. However in some cases we're interested in whether the CRTC
1300  * is active, or effectively active (ie: it's connected to an active display).
1301  * In these cases, use this function instead of just checking active.
1302  */
1303 static inline bool
drm_atomic_crtc_effectively_active(const struct drm_crtc_state * state)1304 drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)
1305 {
1306 	return state->active || state->self_refresh_active;
1307 }
1308 
1309 /**
1310  * struct drm_bus_cfg - bus configuration
1311  *
1312  * This structure stores the configuration of a physical bus between two
1313  * components in an output pipeline, usually between two bridges, an encoder
1314  * and a bridge, or a bridge and a connector.
1315  *
1316  * The bus configuration is stored in &drm_bridge_state separately for the
1317  * input and output buses, as seen from the point of view of each bridge. The
1318  * bus configuration of a bridge output is usually identical to the
1319  * configuration of the next bridge's input, but may differ if the signals are
1320  * modified between the two bridges, for instance by an inverter on the board.
1321  * The input and output configurations of a bridge may differ if the bridge
1322  * modifies the signals internally, for instance by performing format
1323  * conversion, or modifying signals polarities.
1324  */
1325 struct drm_bus_cfg {
1326 	/**
1327 	 * @format: format used on this bus (one of the MEDIA_BUS_FMT_* format)
1328 	 *
1329 	 * This field should not be directly modified by drivers
1330 	 * (drm_atomic_bridge_chain_select_bus_fmts() takes care of the bus
1331 	 * format negotiation).
1332 	 */
1333 	u32 format;
1334 
1335 	/**
1336 	 * @flags: DRM_BUS_* flags used on this bus
1337 	 */
1338 	u32 flags;
1339 };
1340 
1341 /**
1342  * struct drm_bridge_state - Atomic bridge state object
1343  */
1344 struct drm_bridge_state {
1345 	/**
1346 	 * @base: inherit from &drm_private_state
1347 	 */
1348 	struct drm_private_state base;
1349 
1350 	/**
1351 	 * @bridge: the bridge this state refers to
1352 	 */
1353 	struct drm_bridge *bridge;
1354 
1355 	/**
1356 	 * @input_bus_cfg: input bus configuration
1357 	 */
1358 	struct drm_bus_cfg input_bus_cfg;
1359 
1360 	/**
1361 	 * @output_bus_cfg: output bus configuration
1362 	 */
1363 	struct drm_bus_cfg output_bus_cfg;
1364 };
1365 
1366 static inline struct drm_bridge_state *
drm_priv_to_bridge_state(struct drm_private_state * priv)1367 drm_priv_to_bridge_state(struct drm_private_state *priv)
1368 {
1369 	return container_of(priv, struct drm_bridge_state, base);
1370 }
1371 
1372 struct drm_bridge_state *
1373 drm_atomic_get_bridge_state(struct drm_atomic_state *state,
1374 			    struct drm_bridge *bridge);
1375 struct drm_bridge_state *
1376 drm_atomic_get_old_bridge_state(const struct drm_atomic_state *state,
1377 				struct drm_bridge *bridge);
1378 struct drm_bridge_state *
1379 drm_atomic_get_new_bridge_state(const struct drm_atomic_state *state,
1380 				struct drm_bridge *bridge);
1381 
1382 #endif /* DRM_ATOMIC_H_ */
1383