drm_atomic.h revision 1.3 1 /* $NetBSD: drm_atomic.h,v 1.3 2021/12/18 23:45:45 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2014 Red Hat
5 * Copyright (C) 2014 Intel Corp.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robdclark (at) gmail.com>
27 * Daniel Vetter <daniel.vetter (at) ffwll.ch>
28 */
29
30 #ifndef DRM_ATOMIC_H_
31 #define DRM_ATOMIC_H_
32
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_util.h>
35
36 /**
37 * struct drm_crtc_commit - track modeset commits on a CRTC
38 *
39 * This structure is used to track pending modeset changes and atomic commit on
40 * a per-CRTC basis. Since updating the list should never block, this structure
41 * is reference counted to allow waiters to safely wait on an event to complete,
42 * without holding any locks.
43 *
44 * It has 3 different events in total to allow a fine-grained synchronization
45 * between outstanding updates::
46 *
47 * atomic commit thread hardware
48 *
49 * write new state into hardware ----> ...
50 * signal hw_done
51 * switch to new state on next
52 * ... v/hblank
53 *
54 * wait for buffers to show up ...
55 *
56 * ... send completion irq
57 * irq handler signals flip_done
58 * cleanup old buffers
59 *
60 * signal cleanup_done
61 *
62 * wait for flip_done <----
63 * clean up atomic state
64 *
65 * The important bit to know is that &cleanup_done is the terminal event, but the
66 * ordering between &flip_done and &hw_done is entirely up to the specific driver
67 * and modeset state change.
68 *
69 * For an implementation of how to use this look at
70 * drm_atomic_helper_setup_commit() from the atomic helper library.
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 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_planes_state {
161 struct drm_plane *ptr;
162 struct drm_plane_state *state, *old_state, *new_state;
163 };
164
165 struct __drm_crtcs_state {
166 struct drm_crtc *ptr;
167 struct drm_crtc_state *state, *old_state, *new_state;
168
169 /**
170 * @commit:
171 *
172 * A reference to the CRTC commit object that is kept for use by
173 * drm_atomic_helper_wait_for_flip_done() after
174 * drm_atomic_helper_commit_hw_done() is called. This ensures that a
175 * concurrent commit won't free a commit object that is still in use.
176 */
177 struct drm_crtc_commit *commit;
178
179 s32 __user *out_fence_ptr;
180 u64 last_vblank_count;
181 };
182
183 struct __drm_connnectors_state {
184 struct drm_connector *ptr;
185 struct drm_connector_state *state, *old_state, *new_state;
186 /**
187 * @out_fence_ptr:
188 *
189 * User-provided pointer which the kernel uses to return a sync_file
190 * file descriptor. Used by writeback connectors to signal completion of
191 * the writeback.
192 */
193 s32 __user *out_fence_ptr;
194 };
195
196 struct drm_private_obj;
197 struct drm_private_state;
198
199 /**
200 * struct drm_private_state_funcs - atomic state functions for private objects
201 *
202 * These hooks are used by atomic helpers to create, swap and destroy states of
203 * private objects. The structure itself is used as a vtable to identify the
204 * associated private object type. Each private object type that needs to be
205 * added to the atomic states is expected to have an implementation of these
206 * hooks and pass a pointer to its drm_private_state_funcs struct to
207 * drm_atomic_get_private_obj_state().
208 */
209 struct drm_private_state_funcs {
210 /**
211 * @atomic_duplicate_state:
212 *
213 * Duplicate the current state of the private object and return it. It
214 * is an error to call this before obj->state has been initialized.
215 *
216 * RETURNS:
217 *
218 * Duplicated atomic state or NULL when obj->state is not
219 * initialized or allocation failed.
220 */
221 struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);
222
223 /**
224 * @atomic_destroy_state:
225 *
226 * Frees the private object state created with @atomic_duplicate_state.
227 */
228 void (*atomic_destroy_state)(struct drm_private_obj *obj,
229 struct drm_private_state *state);
230 };
231
232 /**
233 * struct drm_private_obj - base struct for driver private atomic object
234 *
235 * A driver private object is initialized by calling
236 * drm_atomic_private_obj_init() and cleaned up by calling
237 * drm_atomic_private_obj_fini().
238 *
239 * Currently only tracks the state update functions and the opaque driver
240 * private state itself, but in the future might also track which
241 * &drm_modeset_lock is required to duplicate and update this object's state.
242 *
243 * All private objects must be initialized before the DRM device they are
244 * attached to is registered to the DRM subsystem (call to drm_dev_register())
245 * and should stay around until this DRM device is unregistered (call to
246 * drm_dev_unregister()). In other words, private objects lifetime is tied
247 * to the DRM device lifetime. This implies that:
248 *
249 * 1/ all calls to drm_atomic_private_obj_init() must be done before calling
250 * drm_dev_register()
251 * 2/ all calls to drm_atomic_private_obj_fini() must be done after calling
252 * drm_dev_unregister()
253 */
254 struct drm_private_obj {
255 /**
256 * @head: List entry used to attach a private object to a &drm_device
257 * (queued to &drm_mode_config.privobj_list).
258 */
259 struct list_head head;
260
261 /**
262 * @lock: Modeset lock to protect the state object.
263 */
264 struct drm_modeset_lock lock;
265
266 /**
267 * @state: Current atomic state for this driver private object.
268 */
269 struct drm_private_state *state;
270
271 /**
272 * @funcs:
273 *
274 * Functions to manipulate the state of this driver private object, see
275 * &drm_private_state_funcs.
276 */
277 const struct drm_private_state_funcs *funcs;
278 };
279
280 /**
281 * drm_for_each_privobj() - private object iterator
282 *
283 * @privobj: pointer to the current private object. Updated after each
284 * iteration
285 * @dev: the DRM device we want get private objects from
286 *
287 * Allows one to iterate over all private objects attached to @dev
288 */
289 #define drm_for_each_privobj(privobj, dev) \
290 list_for_each_entry(privobj, &(dev)->mode_config.privobj_list, head)
291
292 /**
293 * struct drm_private_state - base struct for driver private object state
294 * @state: backpointer to global drm_atomic_state
295 *
296 * Currently only contains a backpointer to the overall atomic update, but in
297 * the future also might hold synchronization information similar to e.g.
298 * &drm_crtc.commit.
299 */
300 struct drm_private_state {
301 struct drm_atomic_state *state;
302 };
303
304 struct __drm_private_objs_state {
305 struct drm_private_obj *ptr;
306 struct drm_private_state *state, *old_state, *new_state;
307 };
308
309 /**
310 * struct drm_atomic_state - the global state object for atomic updates
311 * @ref: count of all references to this state (will not be freed until zero)
312 * @dev: parent DRM device
313 * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
314 * @async_update: hint for asynchronous plane update
315 * @planes: pointer to array of structures with per-plane data
316 * @crtcs: pointer to array of CRTC pointers
317 * @num_connector: size of the @connectors and @connector_states arrays
318 * @connectors: pointer to array of structures with per-connector data
319 * @num_private_objs: size of the @private_objs array
320 * @private_objs: pointer to array of private object pointers
321 * @acquire_ctx: acquire context for this atomic modeset state update
322 *
323 * States are added to an atomic update by calling drm_atomic_get_crtc_state(),
324 * drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for
325 * private state structures, drm_atomic_get_private_obj_state().
326 */
327 struct drm_atomic_state {
328 struct kref ref;
329
330 struct drm_device *dev;
331
332 /**
333 * @allow_modeset:
334 *
335 * Allow full modeset. This is used by the ATOMIC IOCTL handler to
336 * implement the DRM_MODE_ATOMIC_ALLOW_MODESET flag. Drivers should
337 * never consult this flag, instead looking at the output of
338 * drm_atomic_crtc_needs_modeset().
339 */
340 bool allow_modeset : 1;
341 bool legacy_cursor_update : 1;
342 bool async_update : 1;
343 /**
344 * @duplicated:
345 *
346 * Indicates whether or not this atomic state was duplicated using
347 * drm_atomic_helper_duplicate_state(). Drivers and atomic helpers
348 * should use this to fixup normal inconsistencies in duplicated
349 * states.
350 */
351 bool duplicated : 1;
352 struct __drm_planes_state *planes;
353 struct __drm_crtcs_state *crtcs;
354 int num_connector;
355 struct __drm_connnectors_state *connectors;
356 int num_private_objs;
357 struct __drm_private_objs_state *private_objs;
358
359 struct drm_modeset_acquire_ctx *acquire_ctx;
360
361 /**
362 * @fake_commit:
363 *
364 * Used for signaling unbound planes/connectors.
365 * When a connector or plane is not bound to any CRTC, it's still important
366 * to preserve linearity to prevent the atomic states from being freed to early.
367 *
368 * This commit (if set) is not bound to any CRTC, but will be completed when
369 * drm_atomic_helper_commit_hw_done() is called.
370 */
371 struct drm_crtc_commit *fake_commit;
372
373 /**
374 * @commit_work:
375 *
376 * Work item which can be used by the driver or helpers to execute the
377 * commit without blocking.
378 */
379 struct work_struct commit_work;
380 };
381
382 void __drm_crtc_commit_free(struct kref *kref);
383
384 /**
385 * drm_crtc_commit_get - acquire a reference to the CRTC commit
386 * @commit: CRTC commit
387 *
388 * Increases the reference of @commit.
389 *
390 * Returns:
391 * The pointer to @commit, with reference increased.
392 */
393 static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
394 {
395 kref_get(&commit->ref);
396 return commit;
397 }
398
399 /**
400 * drm_crtc_commit_put - release a reference to the CRTC commmit
401 * @commit: CRTC commit
402 *
403 * This releases a reference to @commit which is freed after removing the
404 * final reference. No locking required and callable from any context.
405 */
406 static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
407 {
408 kref_put(&commit->ref, __drm_crtc_commit_free);
409 }
410
411 struct drm_atomic_state * __must_check
412 drm_atomic_state_alloc(struct drm_device *dev);
413 void drm_atomic_state_clear(struct drm_atomic_state *state);
414
415 /**
416 * drm_atomic_state_get - acquire a reference to the atomic state
417 * @state: The atomic state
418 *
419 * Returns a new reference to the @state
420 */
421 static inline struct drm_atomic_state *
422 drm_atomic_state_get(struct drm_atomic_state *state)
423 {
424 kref_get(&state->ref);
425 return state;
426 }
427
428 void __drm_atomic_state_free(struct kref *ref);
429
430 /**
431 * drm_atomic_state_put - release a reference to the atomic state
432 * @state: The atomic state
433 *
434 * This releases a reference to @state which is freed after removing the
435 * final reference. No locking required and callable from any context.
436 */
437 static inline void drm_atomic_state_put(struct drm_atomic_state *state)
438 {
439 kref_put(&state->ref, __drm_atomic_state_free);
440 }
441
442 int __must_check
443 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
444 void drm_atomic_state_default_clear(struct drm_atomic_state *state);
445 void drm_atomic_state_default_release(struct drm_atomic_state *state);
446
447 struct drm_crtc_state * __must_check
448 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
449 struct drm_crtc *crtc);
450 struct drm_plane_state * __must_check
451 drm_atomic_get_plane_state(struct drm_atomic_state *state,
452 struct drm_plane *plane);
453 struct drm_connector_state * __must_check
454 drm_atomic_get_connector_state(struct drm_atomic_state *state,
455 struct drm_connector *connector);
456
457 void drm_atomic_private_obj_init(struct drm_device *dev,
458 struct drm_private_obj *obj,
459 struct drm_private_state *state,
460 const struct drm_private_state_funcs *funcs);
461 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
462
463 struct drm_private_state * __must_check
464 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
465 struct drm_private_obj *obj);
466 struct drm_private_state *
467 drm_atomic_get_old_private_obj_state(struct drm_atomic_state *state,
468 struct drm_private_obj *obj);
469 struct drm_private_state *
470 drm_atomic_get_new_private_obj_state(struct drm_atomic_state *state,
471 struct drm_private_obj *obj);
472
473 struct drm_connector *
474 drm_atomic_get_old_connector_for_encoder(struct drm_atomic_state *state,
475 struct drm_encoder *encoder);
476 struct drm_connector *
477 drm_atomic_get_new_connector_for_encoder(struct drm_atomic_state *state,
478 struct drm_encoder *encoder);
479
480 /**
481 * drm_atomic_get_existing_crtc_state - get CRTC state, if it exists
482 * @state: global atomic state object
483 * @crtc: CRTC to grab
484 *
485 * This function returns the CRTC state for the given CRTC, or NULL
486 * if the CRTC is not part of the global atomic state.
487 *
488 * This function is deprecated, @drm_atomic_get_old_crtc_state or
489 * @drm_atomic_get_new_crtc_state should be used instead.
490 */
491 static inline struct drm_crtc_state *
492 drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
493 struct drm_crtc *crtc)
494 {
495 return state->crtcs[drm_crtc_index(crtc)].state;
496 }
497
498 /**
499 * drm_atomic_get_old_crtc_state - get old CRTC state, if it exists
500 * @state: global atomic state object
501 * @crtc: CRTC to grab
502 *
503 * This function returns the old CRTC state for the given CRTC, or
504 * NULL if the CRTC is not part of the global atomic state.
505 */
506 static inline struct drm_crtc_state *
507 drm_atomic_get_old_crtc_state(struct drm_atomic_state *state,
508 struct drm_crtc *crtc)
509 {
510 return state->crtcs[drm_crtc_index(crtc)].old_state;
511 }
512 /**
513 * drm_atomic_get_new_crtc_state - get new CRTC state, if it exists
514 * @state: global atomic state object
515 * @crtc: CRTC to grab
516 *
517 * This function returns the new CRTC state for the given CRTC, or
518 * NULL if the CRTC is not part of the global atomic state.
519 */
520 static inline struct drm_crtc_state *
521 drm_atomic_get_new_crtc_state(struct drm_atomic_state *state,
522 struct drm_crtc *crtc)
523 {
524 return state->crtcs[drm_crtc_index(crtc)].new_state;
525 }
526
527 /**
528 * drm_atomic_get_existing_plane_state - get plane state, if it exists
529 * @state: global atomic state object
530 * @plane: plane to grab
531 *
532 * This function returns the plane state for the given plane, or NULL
533 * if the plane is not part of the global atomic state.
534 *
535 * This function is deprecated, @drm_atomic_get_old_plane_state or
536 * @drm_atomic_get_new_plane_state should be used instead.
537 */
538 static inline struct drm_plane_state *
539 drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
540 struct drm_plane *plane)
541 {
542 return state->planes[drm_plane_index(plane)].state;
543 }
544
545 /**
546 * drm_atomic_get_old_plane_state - get plane state, if it exists
547 * @state: global atomic state object
548 * @plane: plane to grab
549 *
550 * This function returns the old plane state for the given plane, or
551 * NULL if the plane is not part of the global atomic state.
552 */
553 static inline struct drm_plane_state *
554 drm_atomic_get_old_plane_state(struct drm_atomic_state *state,
555 struct drm_plane *plane)
556 {
557 return state->planes[drm_plane_index(plane)].old_state;
558 }
559
560 /**
561 * drm_atomic_get_new_plane_state - get plane state, if it exists
562 * @state: global atomic state object
563 * @plane: plane to grab
564 *
565 * This function returns the new plane state for the given plane, or
566 * NULL if the plane is not part of the global atomic state.
567 */
568 static inline struct drm_plane_state *
569 drm_atomic_get_new_plane_state(struct drm_atomic_state *state,
570 struct drm_plane *plane)
571 {
572 return state->planes[drm_plane_index(plane)].new_state;
573 }
574
575 /**
576 * drm_atomic_get_existing_connector_state - get connector state, if it exists
577 * @state: global atomic state object
578 * @connector: connector to grab
579 *
580 * This function returns the connector state for the given connector,
581 * or NULL if the connector is not part of the global atomic state.
582 *
583 * This function is deprecated, @drm_atomic_get_old_connector_state or
584 * @drm_atomic_get_new_connector_state should be used instead.
585 */
586 static inline struct drm_connector_state *
587 drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
588 struct drm_connector *connector)
589 {
590 int index = drm_connector_index(connector);
591
592 if (index >= state->num_connector)
593 return NULL;
594
595 return state->connectors[index].state;
596 }
597
598 /**
599 * drm_atomic_get_old_connector_state - get connector state, if it exists
600 * @state: global atomic state object
601 * @connector: connector to grab
602 *
603 * This function returns the old connector state for the given connector,
604 * or NULL if the connector is not part of the global atomic state.
605 */
606 static inline struct drm_connector_state *
607 drm_atomic_get_old_connector_state(struct drm_atomic_state *state,
608 struct drm_connector *connector)
609 {
610 int index = drm_connector_index(connector);
611
612 if (index >= state->num_connector)
613 return NULL;
614
615 return state->connectors[index].old_state;
616 }
617
618 /**
619 * drm_atomic_get_new_connector_state - get connector state, if it exists
620 * @state: global atomic state object
621 * @connector: connector to grab
622 *
623 * This function returns the new connector state for the given connector,
624 * or NULL if the connector is not part of the global atomic state.
625 */
626 static inline struct drm_connector_state *
627 drm_atomic_get_new_connector_state(struct drm_atomic_state *state,
628 struct drm_connector *connector)
629 {
630 int index = drm_connector_index(connector);
631
632 if (index >= state->num_connector)
633 return NULL;
634
635 return state->connectors[index].new_state;
636 }
637
638 /**
639 * __drm_atomic_get_current_plane_state - get current plane state
640 * @state: global atomic state object
641 * @plane: plane to grab
642 *
643 * This function returns the plane state for the given plane, either from
644 * @state, or if the plane isn't part of the atomic state update, from @plane.
645 * This is useful in atomic check callbacks, when drivers need to peek at, but
646 * not change, state of other planes, since it avoids threading an error code
647 * back up the call chain.
648 *
649 * WARNING:
650 *
651 * Note that this function is in general unsafe since it doesn't check for the
652 * required locking for access state structures. Drivers must ensure that it is
653 * safe to access the returned state structure through other means. One common
654 * example is when planes are fixed to a single CRTC, and the driver knows that
655 * the CRTC lock is held already. In that case holding the CRTC lock gives a
656 * read-lock on all planes connected to that CRTC. But if planes can be
657 * reassigned things get more tricky. In that case it's better to use
658 * drm_atomic_get_plane_state and wire up full error handling.
659 *
660 * Returns:
661 *
662 * Read-only pointer to the current plane state.
663 */
664 static inline const struct drm_plane_state *
665 __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
666 struct drm_plane *plane)
667 {
668 if (state->planes[drm_plane_index(plane)].state)
669 return state->planes[drm_plane_index(plane)].state;
670
671 return plane->state;
672 }
673
674 int __must_check
675 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
676 struct drm_crtc *crtc);
677 int __must_check
678 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
679 struct drm_crtc *crtc);
680
681 int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
682 int __must_check drm_atomic_commit(struct drm_atomic_state *state);
683 int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
684
685 void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
686
687 /**
688 * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
689 * @__state: &struct drm_atomic_state pointer
690 * @connector: &struct drm_connector iteration cursor
691 * @old_connector_state: &struct drm_connector_state iteration cursor for the
692 * old state
693 * @new_connector_state: &struct drm_connector_state iteration cursor for the
694 * new state
695 * @__i: int iteration cursor, for macro-internal use
696 *
697 * This iterates over all connectors in an atomic update, tracking both old and
698 * new state. This is useful in places where the state delta needs to be
699 * considered, for example in atomic check functions.
700 */
701 #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
702 for ((__i) = 0; \
703 (__i) < (__state)->num_connector; \
704 (__i)++) \
705 for_each_if ((__state)->connectors[__i].ptr && \
706 ((connector) = (__state)->connectors[__i].ptr, \
707 (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
708 (old_connector_state) = (__state)->connectors[__i].old_state, \
709 (new_connector_state) = (__state)->connectors[__i].new_state, 1))
710
711 /**
712 * for_each_old_connector_in_state - iterate over all connectors in an atomic update
713 * @__state: &struct drm_atomic_state pointer
714 * @connector: &struct drm_connector iteration cursor
715 * @old_connector_state: &struct drm_connector_state iteration cursor for the
716 * old state
717 * @__i: int iteration cursor, for macro-internal use
718 *
719 * This iterates over all connectors in an atomic update, tracking only the old
720 * state. This is useful in disable functions, where we need the old state the
721 * hardware is still in.
722 */
723 #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
724 for ((__i) = 0; \
725 (__i) < (__state)->num_connector; \
726 (__i)++) \
727 for_each_if ((__state)->connectors[__i].ptr && \
728 ((connector) = (__state)->connectors[__i].ptr, \
729 (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
730 (old_connector_state) = (__state)->connectors[__i].old_state, 1))
731
732 /**
733 * for_each_new_connector_in_state - iterate over all connectors in an atomic update
734 * @__state: &struct drm_atomic_state pointer
735 * @connector: &struct drm_connector iteration cursor
736 * @new_connector_state: &struct drm_connector_state iteration cursor for the
737 * new state
738 * @__i: int iteration cursor, for macro-internal use
739 *
740 * This iterates over all connectors in an atomic update, tracking only the new
741 * state. This is useful in enable functions, where we need the new state the
742 * hardware should be in when the atomic commit operation has completed.
743 */
744 #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
745 for ((__i) = 0; \
746 (__i) < (__state)->num_connector; \
747 (__i)++) \
748 for_each_if ((__state)->connectors[__i].ptr && \
749 ((connector) = (__state)->connectors[__i].ptr, \
750 (void)(connector) /* Only to avoid unused-but-set-variable warning */, \
751 (new_connector_state) = (__state)->connectors[__i].new_state, \
752 (void)(new_connector_state) /* Only to avoid unused-but-set-variable warning */, 1))
753
754 /**
755 * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
756 * @__state: &struct drm_atomic_state pointer
757 * @crtc: &struct drm_crtc iteration cursor
758 * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
759 * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
760 * @__i: int iteration cursor, for macro-internal use
761 *
762 * This iterates over all CRTCs in an atomic update, tracking both old and
763 * new state. This is useful in places where the state delta needs to be
764 * considered, for example in atomic check functions.
765 */
766 #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
767 for ((__i) = 0; \
768 (__i) < (__state)->dev->mode_config.num_crtc; \
769 (__i)++) \
770 for_each_if ((__state)->crtcs[__i].ptr && \
771 ((crtc) = (__state)->crtcs[__i].ptr, \
772 (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
773 (old_crtc_state) = (__state)->crtcs[__i].old_state, \
774 (void)(old_crtc_state) /* Only to avoid unused-but-set-variable warning */, \
775 (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
776
777 /**
778 * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
779 * @__state: &struct drm_atomic_state pointer
780 * @crtc: &struct drm_crtc iteration cursor
781 * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
782 * @__i: int iteration cursor, for macro-internal use
783 *
784 * This iterates over all CRTCs in an atomic update, tracking only the old
785 * state. This is useful in disable functions, where we need the old state the
786 * hardware is still in.
787 */
788 #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i) \
789 for ((__i) = 0; \
790 (__i) < (__state)->dev->mode_config.num_crtc; \
791 (__i)++) \
792 for_each_if ((__state)->crtcs[__i].ptr && \
793 ((crtc) = (__state)->crtcs[__i].ptr, \
794 (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
795
796 /**
797 * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
798 * @__state: &struct drm_atomic_state pointer
799 * @crtc: &struct drm_crtc iteration cursor
800 * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
801 * @__i: int iteration cursor, for macro-internal use
802 *
803 * This iterates over all CRTCs in an atomic update, tracking only the new
804 * state. This is useful in enable functions, where we need the new state the
805 * hardware should be in when the atomic commit operation has completed.
806 */
807 #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i) \
808 for ((__i) = 0; \
809 (__i) < (__state)->dev->mode_config.num_crtc; \
810 (__i)++) \
811 for_each_if ((__state)->crtcs[__i].ptr && \
812 ((crtc) = (__state)->crtcs[__i].ptr, \
813 (void)(crtc) /* Only to avoid unused-but-set-variable warning */, \
814 (new_crtc_state) = (__state)->crtcs[__i].new_state, \
815 (void)(new_crtc_state) /* Only to avoid unused-but-set-variable warning */, 1))
816
817 /**
818 * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
819 * @__state: &struct drm_atomic_state pointer
820 * @plane: &struct drm_plane iteration cursor
821 * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
822 * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
823 * @__i: int iteration cursor, for macro-internal use
824 *
825 * This iterates over all planes in an atomic update, tracking both old and
826 * new state. This is useful in places where the state delta needs to be
827 * considered, for example in atomic check functions.
828 */
829 #define for_each_oldnew_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
830 for ((__i) = 0; \
831 (__i) < (__state)->dev->mode_config.num_total_plane; \
832 (__i)++) \
833 for_each_if ((__state)->planes[__i].ptr && \
834 ((plane) = (__state)->planes[__i].ptr, \
835 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
836 (old_plane_state) = (__state)->planes[__i].old_state,\
837 (new_plane_state) = (__state)->planes[__i].new_state, 1))
838
839 /**
840 * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
841 * update in reverse order
842 * @__state: &struct drm_atomic_state pointer
843 * @plane: &struct drm_plane iteration cursor
844 * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
845 * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
846 * @__i: int iteration cursor, for macro-internal use
847 *
848 * This iterates over all planes in an atomic update in reverse order,
849 * tracking both old and new state. This is useful in places where the
850 * state delta needs to be considered, for example in atomic check functions.
851 */
852 #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
853 for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1); \
854 (__i) >= 0; \
855 (__i)--) \
856 for_each_if ((__state)->planes[__i].ptr && \
857 ((plane) = (__state)->planes[__i].ptr, \
858 (old_plane_state) = (__state)->planes[__i].old_state,\
859 (new_plane_state) = (__state)->planes[__i].new_state, 1))
860
861 /**
862 * for_each_old_plane_in_state - iterate over all planes in an atomic update
863 * @__state: &struct drm_atomic_state pointer
864 * @plane: &struct drm_plane iteration cursor
865 * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
866 * @__i: int iteration cursor, for macro-internal use
867 *
868 * This iterates over all planes in an atomic update, tracking only the old
869 * state. This is useful in disable functions, where we need the old state the
870 * hardware is still in.
871 */
872 #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
873 for ((__i) = 0; \
874 (__i) < (__state)->dev->mode_config.num_total_plane; \
875 (__i)++) \
876 for_each_if ((__state)->planes[__i].ptr && \
877 ((plane) = (__state)->planes[__i].ptr, \
878 (old_plane_state) = (__state)->planes[__i].old_state, 1))
879 /**
880 * for_each_new_plane_in_state - iterate over all planes in an atomic update
881 * @__state: &struct drm_atomic_state pointer
882 * @plane: &struct drm_plane iteration cursor
883 * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
884 * @__i: int iteration cursor, for macro-internal use
885 *
886 * This iterates over all planes in an atomic update, tracking only the new
887 * state. This is useful in enable functions, where we need the new state the
888 * hardware should be in when the atomic commit operation has completed.
889 */
890 #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
891 for ((__i) = 0; \
892 (__i) < (__state)->dev->mode_config.num_total_plane; \
893 (__i)++) \
894 for_each_if ((__state)->planes[__i].ptr && \
895 ((plane) = (__state)->planes[__i].ptr, \
896 (void)(plane) /* Only to avoid unused-but-set-variable warning */, \
897 (new_plane_state) = (__state)->planes[__i].new_state, \
898 (void)(new_plane_state) /* Only to avoid unused-but-set-variable warning */, 1))
899
900 /**
901 * for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update
902 * @__state: &struct drm_atomic_state pointer
903 * @obj: &struct drm_private_obj iteration cursor
904 * @old_obj_state: &struct drm_private_state iteration cursor for the old state
905 * @new_obj_state: &struct drm_private_state iteration cursor for the new state
906 * @__i: int iteration cursor, for macro-internal use
907 *
908 * This iterates over all private objects in an atomic update, tracking both
909 * old and new state. This is useful in places where the state delta needs
910 * to be considered, for example in atomic check functions.
911 */
912 #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
913 for ((__i) = 0; \
914 (__i) < (__state)->num_private_objs && \
915 ((obj) = (__state)->private_objs[__i].ptr, \
916 (old_obj_state) = (__state)->private_objs[__i].old_state, \
917 (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
918 (__i)++)
919
920 /**
921 * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
922 * @__state: &struct drm_atomic_state pointer
923 * @obj: &struct drm_private_obj iteration cursor
924 * @old_obj_state: &struct drm_private_state iteration cursor for the old state
925 * @__i: int iteration cursor, for macro-internal use
926 *
927 * This iterates over all private objects in an atomic update, tracking only
928 * the old state. This is useful in disable functions, where we need the old
929 * state the hardware is still in.
930 */
931 #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
932 for ((__i) = 0; \
933 (__i) < (__state)->num_private_objs && \
934 ((obj) = (__state)->private_objs[__i].ptr, \
935 (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
936 (__i)++)
937
938 /**
939 * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
940 * @__state: &struct drm_atomic_state pointer
941 * @obj: &struct drm_private_obj iteration cursor
942 * @new_obj_state: &struct drm_private_state iteration cursor for the new state
943 * @__i: int iteration cursor, for macro-internal use
944 *
945 * This iterates over all private objects in an atomic update, tracking only
946 * the new state. This is useful in enable functions, where we need the new state the
947 * hardware should be in when the atomic commit operation has completed.
948 */
949 #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
950 for ((__i) = 0; \
951 (__i) < (__state)->num_private_objs && \
952 ((obj) = (__state)->private_objs[__i].ptr, \
953 (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
954 (__i)++)
955
956 /**
957 * drm_atomic_crtc_needs_modeset - compute combined modeset need
958 * @state: &drm_crtc_state for the CRTC
959 *
960 * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
961 * whether the state CRTC changed enough to need a full modeset cycle:
962 * mode_changed, active_changed and connectors_changed. This helper simply
963 * combines these three to compute the overall need for a modeset for @state.
964 *
965 * The atomic helper code sets these booleans, but drivers can and should
966 * change them appropriately to accurately represent whether a modeset is
967 * really needed. In general, drivers should avoid full modesets whenever
968 * possible.
969 *
970 * For example if the CRTC mode has changed, and the hardware is able to enact
971 * the requested mode change without going through a full modeset, the driver
972 * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
973 * implementation.
974 */
975 static inline bool
976 drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
977 {
978 return state->mode_changed || state->active_changed ||
979 state->connectors_changed;
980 }
981
982 /**
983 * drm_atomic_crtc_effectively_active - compute whether CRTC is actually active
984 * @state: &drm_crtc_state for the CRTC
985 *
986 * When in self refresh mode, the crtc_state->active value will be false, since
987 * the CRTC is off. However in some cases we're interested in whether the CRTC
988 * is active, or effectively active (ie: it's connected to an active display).
989 * In these cases, use this function instead of just checking active.
990 */
991 static inline bool
992 drm_atomic_crtc_effectively_active(const struct drm_crtc_state *state)
993 {
994 return state->active || state->self_refresh_active;
995 }
996
997 #endif /* DRM_ATOMIC_H_ */
998