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 
33 /**
34  * struct drm_crtc_commit - track modeset commits on a CRTC
35  *
36  * This structure is used to track pending modeset changes and atomic commit on
37  * a per-CRTC basis. Since updating the list should never block this structure
38  * is reference counted to allow waiters to safely wait on an event to complete,
39  * without holding any locks.
40  *
41  * It has 3 different events in total to allow a fine-grained synchronization
42  * between outstanding updates::
43  *
44  *	atomic commit thread			hardware
45  *
46  * 	write new state into hardware	---->	...
47  * 	signal hw_done
48  * 						switch to new state on next
49  * 	...					v/hblank
50  *
51  *	wait for buffers to show up		...
52  *
53  *	...					send completion irq
54  *						irq handler signals flip_done
55  *	cleanup old buffers
56  *
57  * 	signal cleanup_done
58  *
59  * 	wait for flip_done		<----
60  * 	clean up atomic state
61  *
62  * The important bit to know is that cleanup_done is the terminal event, but the
63  * ordering between flip_done and hw_done is entirely up to the specific driver
64  * and modeset state change.
65  *
66  * For an implementation of how to use this look at
67  * drm_atomic_helper_setup_commit() from the atomic helper library.
68  */
69 struct drm_crtc_commit {
70 	/**
71 	 * @crtc:
72 	 *
73 	 * DRM CRTC for this commit.
74 	 */
75 	struct drm_crtc *crtc;
76 
77 	/**
78 	 * @ref:
79 	 *
80 	 * Reference count for this structure. Needed to allow blocking on
81 	 * completions without the risk of the completion disappearing
82 	 * meanwhile.
83 	 */
84 	struct kref ref;
85 
86 	/**
87 	 * @flip_done:
88 	 *
89 	 * Will be signaled when the hardware has flipped to the new set of
90 	 * buffers. Signals at the same time as when the drm event for this
91 	 * commit is sent to userspace, or when an out-fence is singalled. Note
92 	 * that for most hardware, in most cases this happens after @hw_done is
93 	 * signalled.
94 	 */
95 	struct completion flip_done;
96 
97 	/**
98 	 * @hw_done:
99 	 *
100 	 * Will be signalled when all hw register changes for this commit have
101 	 * been written out. Especially when disabling a pipe this can be much
102 	 * later than than @flip_done, since that can signal already when the
103 	 * screen goes black, whereas to fully shut down a pipe more register
104 	 * I/O is required.
105 	 *
106 	 * Note that this does not need to include separately reference-counted
107 	 * resources like backing storage buffer pinning, or runtime pm
108 	 * management.
109 	 */
110 	struct completion hw_done;
111 
112 	/**
113 	 * @cleanup_done:
114 	 *
115 	 * Will be signalled after old buffers have been cleaned up by calling
116 	 * drm_atomic_helper_cleanup_planes(). Since this can only happen after
117 	 * a vblank wait completed it might be a bit later. This completion is
118 	 * useful to throttle updates and avoid hardware updates getting ahead
119 	 * of the buffer cleanup too much.
120 	 */
121 	struct completion cleanup_done;
122 
123 	/**
124 	 * @commit_entry:
125 	 *
126 	 * Entry on the per-CRTC &drm_crtc.commit_list. Protected by
127 	 * $drm_crtc.commit_lock.
128 	 */
129 	struct list_head commit_entry;
130 
131 	/**
132 	 * @event:
133 	 *
134 	 * &drm_pending_vblank_event pointer to clean up private events.
135 	 */
136 	struct drm_pending_vblank_event *event;
137 
138 	/**
139 	 * @abort_completion:
140 	 *
141 	 * A flag that's set after drm_atomic_helper_setup_commit takes a second
142 	 * reference for the completion of $drm_crtc_state.event. It's used by
143 	 * the free code to remove the second reference if commit fails.
144 	 */
145 	bool abort_completion;
146 };
147 
148 struct __drm_planes_state {
149 	struct drm_plane *ptr;
150 	struct drm_plane_state *state, *old_state, *new_state;
151 };
152 
153 struct __drm_crtcs_state {
154 	struct drm_crtc *ptr;
155 	struct drm_crtc_state *state, *old_state, *new_state;
156 
157 	/**
158 	 * @commit:
159 	 *
160 	 * A reference to the CRTC commit object that is kept for use by
161 	 * drm_atomic_helper_wait_for_flip_done() after
162 	 * drm_atomic_helper_commit_hw_done() is called. This ensures that a
163 	 * concurrent commit won't free a commit object that is still in use.
164 	 */
165 	struct drm_crtc_commit *commit;
166 
167 	s32 __user *out_fence_ptr;
168 	u64 last_vblank_count;
169 };
170 
171 struct __drm_connnectors_state {
172 	struct drm_connector *ptr;
173 	struct drm_connector_state *state, *old_state, *new_state;
174 	/**
175 	 * @out_fence_ptr:
176 	 *
177 	 * User-provided pointer which the kernel uses to return a sync_file
178 	 * file descriptor. Used by writeback connectors to signal completion of
179 	 * the writeback.
180 	 */
181 	s32 __user *out_fence_ptr;
182 };
183 
184 struct drm_private_obj;
185 struct drm_private_state;
186 
187 /**
188  * struct drm_private_state_funcs - atomic state functions for private objects
189  *
190  * These hooks are used by atomic helpers to create, swap and destroy states of
191  * private objects. The structure itself is used as a vtable to identify the
192  * associated private object type. Each private object type that needs to be
193  * added to the atomic states is expected to have an implementation of these
194  * hooks and pass a pointer to it's drm_private_state_funcs struct to
195  * drm_atomic_get_private_obj_state().
196  */
197 struct drm_private_state_funcs {
198 	/**
199 	 * @atomic_duplicate_state:
200 	 *
201 	 * Duplicate the current state of the private object and return it. It
202 	 * is an error to call this before obj->state has been initialized.
203 	 *
204 	 * RETURNS:
205 	 *
206 	 * Duplicated atomic state or NULL when obj->state is not
207 	 * initialized or allocation failed.
208 	 */
209 	struct drm_private_state *(*atomic_duplicate_state)(struct drm_private_obj *obj);
210 
211 	/**
212 	 * @atomic_destroy_state:
213 	 *
214 	 * Frees the private object state created with @atomic_duplicate_state.
215 	 */
216 	void (*atomic_destroy_state)(struct drm_private_obj *obj,
217 				     struct drm_private_state *state);
218 };
219 
220 /**
221  * struct drm_private_obj - base struct for driver private atomic object
222  *
223  * A driver private object is initialized by calling
224  * drm_atomic_private_obj_init() and cleaned up by calling
225  * drm_atomic_private_obj_fini().
226  *
227  * Currently only tracks the state update functions and the opaque driver
228  * private state itself, but in the future might also track which
229  * &drm_modeset_lock is required to duplicate and update this object's state.
230  */
231 struct drm_private_obj {
232 	/**
233 	 * @state: Current atomic state for this driver private object.
234 	 */
235 	struct drm_private_state *state;
236 
237 	/**
238 	 * @funcs:
239 	 *
240 	 * Functions to manipulate the state of this driver private object, see
241 	 * &drm_private_state_funcs.
242 	 */
243 	const struct drm_private_state_funcs *funcs;
244 };
245 
246 /**
247  * struct drm_private_state - base struct for driver private object state
248  * @state: backpointer to global drm_atomic_state
249  *
250  * Currently only contains a backpointer to the overall atomic update, but in
251  * the future also might hold synchronization information similar to e.g.
252  * &drm_crtc.commit.
253  */
254 struct drm_private_state {
255 	struct drm_atomic_state *state;
256 };
257 
258 struct __drm_private_objs_state {
259 	struct drm_private_obj *ptr;
260 	struct drm_private_state *state, *old_state, *new_state;
261 };
262 
263 /**
264  * struct drm_atomic_state - the global state object for atomic updates
265  * @ref: count of all references to this state (will not be freed until zero)
266  * @dev: parent DRM device
267  * @allow_modeset: allow full modeset
268  * @legacy_cursor_update: hint to enforce legacy cursor IOCTL semantics
269  * @async_update: hint for asynchronous plane update
270  * @planes: pointer to array of structures with per-plane data
271  * @crtcs: pointer to array of CRTC pointers
272  * @num_connector: size of the @connectors and @connector_states arrays
273  * @connectors: pointer to array of structures with per-connector data
274  * @num_private_objs: size of the @private_objs array
275  * @private_objs: pointer to array of private object pointers
276  * @acquire_ctx: acquire context for this atomic modeset state update
277  *
278  * States are added to an atomic update by calling drm_atomic_get_crtc_state(),
279  * drm_atomic_get_plane_state(), drm_atomic_get_connector_state(), or for
280  * private state structures, drm_atomic_get_private_obj_state().
281  */
282 struct drm_atomic_state {
283 	struct kref ref;
284 
285 	struct drm_device *dev;
286 	bool allow_modeset : 1;
287 	bool legacy_cursor_update : 1;
288 	bool async_update : 1;
289 	struct __drm_planes_state *planes;
290 	struct __drm_crtcs_state *crtcs;
291 	int num_connector;
292 	struct __drm_connnectors_state *connectors;
293 	int num_private_objs;
294 	struct __drm_private_objs_state *private_objs;
295 
296 	struct drm_modeset_acquire_ctx *acquire_ctx;
297 
298 	/**
299 	 * @fake_commit:
300 	 *
301 	 * Used for signaling unbound planes/connectors.
302 	 * When a connector or plane is not bound to any CRTC, it's still important
303 	 * to preserve linearity to prevent the atomic states from being freed to early.
304 	 *
305 	 * This commit (if set) is not bound to any crtc, but will be completed when
306 	 * drm_atomic_helper_commit_hw_done() is called.
307 	 */
308 	struct drm_crtc_commit *fake_commit;
309 
310 	/**
311 	 * @commit_work:
312 	 *
313 	 * Work item which can be used by the driver or helpers to execute the
314 	 * commit without blocking.
315 	 */
316 	struct work_struct commit_work;
317 };
318 
319 void __drm_crtc_commit_free(struct kref *kref);
320 
321 /**
322  * drm_crtc_commit_get - acquire a reference to the CRTC commit
323  * @commit: CRTC commit
324  *
325  * Increases the reference of @commit.
326  *
327  * Returns:
328  * The pointer to @commit, with reference increased.
329  */
drm_crtc_commit_get(struct drm_crtc_commit * commit)330 static inline struct drm_crtc_commit *drm_crtc_commit_get(struct drm_crtc_commit *commit)
331 {
332 	kref_get(&commit->ref);
333 	return commit;
334 }
335 
336 /**
337  * drm_crtc_commit_put - release a reference to the CRTC commmit
338  * @commit: CRTC commit
339  *
340  * This releases a reference to @commit which is freed after removing the
341  * final reference. No locking required and callable from any context.
342  */
drm_crtc_commit_put(struct drm_crtc_commit * commit)343 static inline void drm_crtc_commit_put(struct drm_crtc_commit *commit)
344 {
345 	kref_put(&commit->ref, __drm_crtc_commit_free);
346 }
347 
348 struct drm_atomic_state * __must_check
349 drm_atomic_state_alloc(struct drm_device *dev);
350 void drm_atomic_state_clear(struct drm_atomic_state *state);
351 
352 /**
353  * drm_atomic_state_get - acquire a reference to the atomic state
354  * @state: The atomic state
355  *
356  * Returns a new reference to the @state
357  */
358 static inline struct drm_atomic_state *
drm_atomic_state_get(struct drm_atomic_state * state)359 drm_atomic_state_get(struct drm_atomic_state *state)
360 {
361 	kref_get(&state->ref);
362 	return state;
363 }
364 
365 void __drm_atomic_state_free(struct kref *ref);
366 
367 /**
368  * drm_atomic_state_put - release a reference to the atomic state
369  * @state: The atomic state
370  *
371  * This releases a reference to @state which is freed after removing the
372  * final reference. No locking required and callable from any context.
373  */
drm_atomic_state_put(struct drm_atomic_state * state)374 static inline void drm_atomic_state_put(struct drm_atomic_state *state)
375 {
376 	kref_put(&state->ref, __drm_atomic_state_free);
377 }
378 
379 int  __must_check
380 drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state);
381 void drm_atomic_state_default_clear(struct drm_atomic_state *state);
382 void drm_atomic_state_default_release(struct drm_atomic_state *state);
383 
384 struct drm_crtc_state * __must_check
385 drm_atomic_get_crtc_state(struct drm_atomic_state *state,
386 			  struct drm_crtc *crtc);
387 int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
388 		struct drm_crtc_state *state, struct drm_property *property,
389 		uint64_t val);
390 struct drm_plane_state * __must_check
391 drm_atomic_get_plane_state(struct drm_atomic_state *state,
392 			   struct drm_plane *plane);
393 struct drm_connector_state * __must_check
394 drm_atomic_get_connector_state(struct drm_atomic_state *state,
395 			       struct drm_connector *connector);
396 
397 void drm_atomic_private_obj_init(struct drm_private_obj *obj,
398 				 struct drm_private_state *state,
399 				 const struct drm_private_state_funcs *funcs);
400 void drm_atomic_private_obj_fini(struct drm_private_obj *obj);
401 
402 struct drm_private_state * __must_check
403 drm_atomic_get_private_obj_state(struct drm_atomic_state *state,
404 				 struct drm_private_obj *obj);
405 
406 /**
407  * drm_atomic_get_existing_crtc_state - get crtc state, if it exists
408  * @state: global atomic state object
409  * @crtc: crtc to grab
410  *
411  * This function returns the crtc state for the given crtc, or NULL
412  * if the crtc is not part of the global atomic state.
413  *
414  * This function is deprecated, @drm_atomic_get_old_crtc_state or
415  * @drm_atomic_get_new_crtc_state should be used instead.
416  */
417 static inline struct drm_crtc_state *
drm_atomic_get_existing_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)418 drm_atomic_get_existing_crtc_state(struct drm_atomic_state *state,
419 				   struct drm_crtc *crtc)
420 {
421 	return state->crtcs[drm_crtc_index(crtc)].state;
422 }
423 
424 /**
425  * drm_atomic_get_old_crtc_state - get old crtc state, if it exists
426  * @state: global atomic state object
427  * @crtc: crtc to grab
428  *
429  * This function returns the old crtc state for the given crtc, or
430  * NULL if the crtc is not part of the global atomic state.
431  */
432 static inline struct drm_crtc_state *
drm_atomic_get_old_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)433 drm_atomic_get_old_crtc_state(struct drm_atomic_state *state,
434 			      struct drm_crtc *crtc)
435 {
436 	return state->crtcs[drm_crtc_index(crtc)].old_state;
437 }
438 /**
439  * drm_atomic_get_new_crtc_state - get new crtc state, if it exists
440  * @state: global atomic state object
441  * @crtc: crtc to grab
442  *
443  * This function returns the new crtc state for the given crtc, or
444  * NULL if the crtc is not part of the global atomic state.
445  */
446 static inline struct drm_crtc_state *
drm_atomic_get_new_crtc_state(struct drm_atomic_state * state,struct drm_crtc * crtc)447 drm_atomic_get_new_crtc_state(struct drm_atomic_state *state,
448 			      struct drm_crtc *crtc)
449 {
450 	return state->crtcs[drm_crtc_index(crtc)].new_state;
451 }
452 
453 /**
454  * drm_atomic_get_existing_plane_state - get plane state, if it exists
455  * @state: global atomic state object
456  * @plane: plane to grab
457  *
458  * This function returns the plane state for the given plane, or NULL
459  * if the plane is not part of the global atomic state.
460  *
461  * This function is deprecated, @drm_atomic_get_old_plane_state or
462  * @drm_atomic_get_new_plane_state should be used instead.
463  */
464 static inline struct drm_plane_state *
drm_atomic_get_existing_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)465 drm_atomic_get_existing_plane_state(struct drm_atomic_state *state,
466 				    struct drm_plane *plane)
467 {
468 	return state->planes[drm_plane_index(plane)].state;
469 }
470 
471 /**
472  * drm_atomic_get_old_plane_state - get plane state, if it exists
473  * @state: global atomic state object
474  * @plane: plane to grab
475  *
476  * This function returns the old plane state for the given plane, or
477  * NULL if the plane is not part of the global atomic state.
478  */
479 static inline struct drm_plane_state *
drm_atomic_get_old_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)480 drm_atomic_get_old_plane_state(struct drm_atomic_state *state,
481 			       struct drm_plane *plane)
482 {
483 	return state->planes[drm_plane_index(plane)].old_state;
484 }
485 
486 /**
487  * drm_atomic_get_new_plane_state - get plane state, if it exists
488  * @state: global atomic state object
489  * @plane: plane to grab
490  *
491  * This function returns the new plane state for the given plane, or
492  * NULL if the plane is not part of the global atomic state.
493  */
494 static inline struct drm_plane_state *
drm_atomic_get_new_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)495 drm_atomic_get_new_plane_state(struct drm_atomic_state *state,
496 			       struct drm_plane *plane)
497 {
498 	return state->planes[drm_plane_index(plane)].new_state;
499 }
500 
501 /**
502  * drm_atomic_get_existing_connector_state - get connector state, if it exists
503  * @state: global atomic state object
504  * @connector: connector to grab
505  *
506  * This function returns the connector state for the given connector,
507  * or NULL if the connector is not part of the global atomic state.
508  *
509  * This function is deprecated, @drm_atomic_get_old_connector_state or
510  * @drm_atomic_get_new_connector_state should be used instead.
511  */
512 static inline struct drm_connector_state *
drm_atomic_get_existing_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)513 drm_atomic_get_existing_connector_state(struct drm_atomic_state *state,
514 					struct drm_connector *connector)
515 {
516 	int index = drm_connector_index(connector);
517 
518 	if (index >= state->num_connector)
519 		return NULL;
520 
521 	return state->connectors[index].state;
522 }
523 
524 /**
525  * drm_atomic_get_old_connector_state - get connector state, if it exists
526  * @state: global atomic state object
527  * @connector: connector to grab
528  *
529  * This function returns the old connector state for the given connector,
530  * or NULL if the connector is not part of the global atomic state.
531  */
532 static inline struct drm_connector_state *
drm_atomic_get_old_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)533 drm_atomic_get_old_connector_state(struct drm_atomic_state *state,
534 				   struct drm_connector *connector)
535 {
536 	int index = drm_connector_index(connector);
537 
538 	if (index >= state->num_connector)
539 		return NULL;
540 
541 	return state->connectors[index].old_state;
542 }
543 
544 /**
545  * drm_atomic_get_new_connector_state - get connector state, if it exists
546  * @state: global atomic state object
547  * @connector: connector to grab
548  *
549  * This function returns the new connector state for the given connector,
550  * or NULL if the connector is not part of the global atomic state.
551  */
552 static inline struct drm_connector_state *
drm_atomic_get_new_connector_state(struct drm_atomic_state * state,struct drm_connector * connector)553 drm_atomic_get_new_connector_state(struct drm_atomic_state *state,
554 				   struct drm_connector *connector)
555 {
556 	int index = drm_connector_index(connector);
557 
558 	if (index >= state->num_connector)
559 		return NULL;
560 
561 	return state->connectors[index].new_state;
562 }
563 
564 /**
565  * __drm_atomic_get_current_plane_state - get current plane state
566  * @state: global atomic state object
567  * @plane: plane to grab
568  *
569  * This function returns the plane state for the given plane, either from
570  * @state, or if the plane isn't part of the atomic state update, from @plane.
571  * This is useful in atomic check callbacks, when drivers need to peek at, but
572  * not change, state of other planes, since it avoids threading an error code
573  * back up the call chain.
574  *
575  * WARNING:
576  *
577  * Note that this function is in general unsafe since it doesn't check for the
578  * required locking for access state structures. Drivers must ensure that it is
579  * safe to access the returned state structure through other means. One common
580  * example is when planes are fixed to a single CRTC, and the driver knows that
581  * the CRTC lock is held already. In that case holding the CRTC lock gives a
582  * read-lock on all planes connected to that CRTC. But if planes can be
583  * reassigned things get more tricky. In that case it's better to use
584  * drm_atomic_get_plane_state and wire up full error handling.
585  *
586  * Returns:
587  *
588  * Read-only pointer to the current plane state.
589  */
590 static inline const struct drm_plane_state *
__drm_atomic_get_current_plane_state(struct drm_atomic_state * state,struct drm_plane * plane)591 __drm_atomic_get_current_plane_state(struct drm_atomic_state *state,
592 				     struct drm_plane *plane)
593 {
594 	if (state->planes[drm_plane_index(plane)].state)
595 		return state->planes[drm_plane_index(plane)].state;
596 
597 	return plane->state;
598 }
599 
600 int __must_check
601 drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
602 			     const struct drm_display_mode *mode);
603 int __must_check
604 drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
605 				  struct drm_property_blob *blob);
606 int __must_check
607 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
608 			      struct drm_crtc *crtc);
609 void drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
610 				 struct drm_framebuffer *fb);
611 void drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
612 				    struct dma_fence *fence);
613 int __must_check
614 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
615 				  struct drm_crtc *crtc);
616 int drm_atomic_set_writeback_fb_for_connector(
617 		struct drm_connector_state *conn_state,
618 		struct drm_framebuffer *fb);
619 int __must_check
620 drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
621 				   struct drm_crtc *crtc);
622 int __must_check
623 drm_atomic_add_affected_planes(struct drm_atomic_state *state,
624 			       struct drm_crtc *crtc);
625 
626 int __must_check drm_atomic_check_only(struct drm_atomic_state *state);
627 int __must_check drm_atomic_commit(struct drm_atomic_state *state);
628 int __must_check drm_atomic_nonblocking_commit(struct drm_atomic_state *state);
629 
630 void drm_state_dump(struct drm_device *dev, struct drm_printer *p);
631 
632 /**
633  * for_each_oldnew_connector_in_state - iterate over all connectors in an atomic update
634  * @__state: &struct drm_atomic_state pointer
635  * @connector: &struct drm_connector iteration cursor
636  * @old_connector_state: &struct drm_connector_state iteration cursor for the
637  * 	old state
638  * @new_connector_state: &struct drm_connector_state iteration cursor for the
639  * 	new state
640  * @__i: int iteration cursor, for macro-internal use
641  *
642  * This iterates over all connectors in an atomic update, tracking both old and
643  * new state. This is useful in places where the state delta needs to be
644  * considered, for example in atomic check functions.
645  */
646 #define for_each_oldnew_connector_in_state(__state, connector, old_connector_state, new_connector_state, __i) \
647 	for ((__i) = 0;								\
648 	     (__i) < (__state)->num_connector;					\
649 	     (__i)++)								\
650 		for_each_if ((__state)->connectors[__i].ptr &&			\
651 			     ((connector) = (__state)->connectors[__i].ptr,	\
652 			     (old_connector_state) = (__state)->connectors[__i].old_state,	\
653 			     (new_connector_state) = (__state)->connectors[__i].new_state, 1))
654 
655 /**
656  * for_each_old_connector_in_state - iterate over all connectors in an atomic update
657  * @__state: &struct drm_atomic_state pointer
658  * @connector: &struct drm_connector iteration cursor
659  * @old_connector_state: &struct drm_connector_state iteration cursor for the
660  * 	old state
661  * @__i: int iteration cursor, for macro-internal use
662  *
663  * This iterates over all connectors in an atomic update, tracking only the old
664  * state. This is useful in disable functions, where we need the old state the
665  * hardware is still in.
666  */
667 #define for_each_old_connector_in_state(__state, connector, old_connector_state, __i) \
668 	for ((__i) = 0;								\
669 	     (__i) < (__state)->num_connector;					\
670 	     (__i)++)								\
671 		for_each_if ((__state)->connectors[__i].ptr &&			\
672 			     ((connector) = (__state)->connectors[__i].ptr,	\
673 			     (old_connector_state) = (__state)->connectors[__i].old_state, 1))
674 
675 /**
676  * for_each_new_connector_in_state - iterate over all connectors in an atomic update
677  * @__state: &struct drm_atomic_state pointer
678  * @connector: &struct drm_connector iteration cursor
679  * @new_connector_state: &struct drm_connector_state iteration cursor for the
680  * 	new state
681  * @__i: int iteration cursor, for macro-internal use
682  *
683  * This iterates over all connectors in an atomic update, tracking only the new
684  * state. This is useful in enable functions, where we need the new state the
685  * hardware should be in when the atomic commit operation has completed.
686  */
687 #define for_each_new_connector_in_state(__state, connector, new_connector_state, __i) \
688 	for ((__i) = 0;								\
689 	     (__i) < (__state)->num_connector;					\
690 	     (__i)++)								\
691 		for_each_if ((__state)->connectors[__i].ptr &&			\
692 			     ((connector) = (__state)->connectors[__i].ptr,	\
693 			     (new_connector_state) = (__state)->connectors[__i].new_state, 1))
694 
695 /**
696  * for_each_oldnew_crtc_in_state - iterate over all CRTCs in an atomic update
697  * @__state: &struct drm_atomic_state pointer
698  * @crtc: &struct drm_crtc iteration cursor
699  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
700  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
701  * @__i: int iteration cursor, for macro-internal use
702  *
703  * This iterates over all CRTCs in an atomic update, tracking both old and
704  * new state. This is useful in places where the state delta needs to be
705  * considered, for example in atomic check functions.
706  */
707 #define for_each_oldnew_crtc_in_state(__state, crtc, old_crtc_state, new_crtc_state, __i) \
708 	for ((__i) = 0;							\
709 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
710 	     (__i)++)							\
711 		for_each_if ((__state)->crtcs[__i].ptr &&		\
712 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
713 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, \
714 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
715 
716 /**
717  * for_each_old_crtc_in_state - iterate over all CRTCs in an atomic update
718  * @__state: &struct drm_atomic_state pointer
719  * @crtc: &struct drm_crtc iteration cursor
720  * @old_crtc_state: &struct drm_crtc_state iteration cursor for the old state
721  * @__i: int iteration cursor, for macro-internal use
722  *
723  * This iterates over all CRTCs in an atomic update, tracking only the old
724  * state. This is useful in disable functions, where we need the old state the
725  * hardware is still in.
726  */
727 #define for_each_old_crtc_in_state(__state, crtc, old_crtc_state, __i)	\
728 	for ((__i) = 0;							\
729 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
730 	     (__i)++)							\
731 		for_each_if ((__state)->crtcs[__i].ptr &&		\
732 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
733 			     (old_crtc_state) = (__state)->crtcs[__i].old_state, 1))
734 
735 /**
736  * for_each_new_crtc_in_state - iterate over all CRTCs in an atomic update
737  * @__state: &struct drm_atomic_state pointer
738  * @crtc: &struct drm_crtc iteration cursor
739  * @new_crtc_state: &struct drm_crtc_state iteration cursor for the new state
740  * @__i: int iteration cursor, for macro-internal use
741  *
742  * This iterates over all CRTCs in an atomic update, tracking only the new
743  * state. This is useful in enable functions, where we need the new state the
744  * hardware should be in when the atomic commit operation has completed.
745  */
746 #define for_each_new_crtc_in_state(__state, crtc, new_crtc_state, __i)	\
747 	for ((__i) = 0;							\
748 	     (__i) < (__state)->dev->mode_config.num_crtc;		\
749 	     (__i)++)							\
750 		for_each_if ((__state)->crtcs[__i].ptr &&		\
751 			     ((crtc) = (__state)->crtcs[__i].ptr,	\
752 			     (new_crtc_state) = (__state)->crtcs[__i].new_state, 1))
753 
754 /**
755  * for_each_oldnew_plane_in_state - iterate over all planes in an atomic update
756  * @__state: &struct drm_atomic_state pointer
757  * @plane: &struct drm_plane iteration cursor
758  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
759  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
760  * @__i: int iteration cursor, for macro-internal use
761  *
762  * This iterates over all planes 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_plane_in_state(__state, plane, old_plane_state, new_plane_state, __i) \
767 	for ((__i) = 0;							\
768 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
769 	     (__i)++)							\
770 		for_each_if ((__state)->planes[__i].ptr &&		\
771 			     ((plane) = (__state)->planes[__i].ptr,	\
772 			      (old_plane_state) = (__state)->planes[__i].old_state,\
773 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
774 
775 /**
776  * for_each_oldnew_plane_in_state_reverse - iterate over all planes in an atomic
777  * update in reverse order
778  * @__state: &struct drm_atomic_state pointer
779  * @plane: &struct drm_plane iteration cursor
780  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
781  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
782  * @__i: int iteration cursor, for macro-internal use
783  *
784  * This iterates over all planes in an atomic update in reverse order,
785  * tracking both old and  new state. This is useful in places where the
786  * state delta needs to be considered, for example in atomic check functions.
787  */
788 #define for_each_oldnew_plane_in_state_reverse(__state, plane, old_plane_state, new_plane_state, __i) \
789 	for ((__i) = ((__state)->dev->mode_config.num_total_plane - 1);	\
790 	     (__i) >= 0;						\
791 	     (__i)--)							\
792 		for_each_if ((__state)->planes[__i].ptr &&		\
793 			     ((plane) = (__state)->planes[__i].ptr,	\
794 			      (old_plane_state) = (__state)->planes[__i].old_state,\
795 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
796 
797 /**
798  * for_each_old_plane_in_state - iterate over all planes in an atomic update
799  * @__state: &struct drm_atomic_state pointer
800  * @plane: &struct drm_plane iteration cursor
801  * @old_plane_state: &struct drm_plane_state iteration cursor for the old state
802  * @__i: int iteration cursor, for macro-internal use
803  *
804  * This iterates over all planes in an atomic update, tracking only the old
805  * state. This is useful in disable functions, where we need the old state the
806  * hardware is still in.
807  */
808 #define for_each_old_plane_in_state(__state, plane, old_plane_state, __i) \
809 	for ((__i) = 0;							\
810 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
811 	     (__i)++)							\
812 		for_each_if ((__state)->planes[__i].ptr &&		\
813 			     ((plane) = (__state)->planes[__i].ptr,	\
814 			      (old_plane_state) = (__state)->planes[__i].old_state, 1))
815 /**
816  * for_each_new_plane_in_state - iterate over all planes in an atomic update
817  * @__state: &struct drm_atomic_state pointer
818  * @plane: &struct drm_plane iteration cursor
819  * @new_plane_state: &struct drm_plane_state iteration cursor for the new state
820  * @__i: int iteration cursor, for macro-internal use
821  *
822  * This iterates over all planes in an atomic update, tracking only the new
823  * state. This is useful in enable functions, where we need the new state the
824  * hardware should be in when the atomic commit operation has completed.
825  */
826 #define for_each_new_plane_in_state(__state, plane, new_plane_state, __i) \
827 	for ((__i) = 0;							\
828 	     (__i) < (__state)->dev->mode_config.num_total_plane;	\
829 	     (__i)++)							\
830 		for_each_if ((__state)->planes[__i].ptr &&		\
831 			     ((plane) = (__state)->planes[__i].ptr,	\
832 			      (new_plane_state) = (__state)->planes[__i].new_state, 1))
833 
834 /**
835  * for_each_oldnew_private_obj_in_state - iterate over all private objects in an atomic update
836  * @__state: &struct drm_atomic_state pointer
837  * @obj: &struct drm_private_obj iteration cursor
838  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
839  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
840  * @__i: int iteration cursor, for macro-internal use
841  *
842  * This iterates over all private objects in an atomic update, tracking both
843  * old and new state. This is useful in places where the state delta needs
844  * to be considered, for example in atomic check functions.
845  */
846 #define for_each_oldnew_private_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
847 	for ((__i) = 0; \
848 	     (__i) < (__state)->num_private_objs && \
849 		     ((obj) = (__state)->private_objs[__i].ptr, \
850 		      (old_obj_state) = (__state)->private_objs[__i].old_state,	\
851 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
852 	     (__i)++)
853 
854 /**
855  * for_each_old_private_obj_in_state - iterate over all private objects in an atomic update
856  * @__state: &struct drm_atomic_state pointer
857  * @obj: &struct drm_private_obj iteration cursor
858  * @old_obj_state: &struct drm_private_state iteration cursor for the old state
859  * @__i: int iteration cursor, for macro-internal use
860  *
861  * This iterates over all private objects in an atomic update, tracking only
862  * the old state. This is useful in disable functions, where we need the old
863  * state the hardware is still in.
864  */
865 #define for_each_old_private_obj_in_state(__state, obj, old_obj_state, __i) \
866 	for ((__i) = 0; \
867 	     (__i) < (__state)->num_private_objs && \
868 		     ((obj) = (__state)->private_objs[__i].ptr, \
869 		      (old_obj_state) = (__state)->private_objs[__i].old_state, 1); \
870 	     (__i)++)
871 
872 /**
873  * for_each_new_private_obj_in_state - iterate over all private objects in an atomic update
874  * @__state: &struct drm_atomic_state pointer
875  * @obj: &struct drm_private_obj iteration cursor
876  * @new_obj_state: &struct drm_private_state iteration cursor for the new state
877  * @__i: int iteration cursor, for macro-internal use
878  *
879  * This iterates over all private objects in an atomic update, tracking only
880  * the new state. This is useful in enable functions, where we need the new state the
881  * hardware should be in when the atomic commit operation has completed.
882  */
883 #define for_each_new_private_obj_in_state(__state, obj, new_obj_state, __i) \
884 	for ((__i) = 0; \
885 	     (__i) < (__state)->num_private_objs && \
886 		     ((obj) = (__state)->private_objs[__i].ptr, \
887 		      (new_obj_state) = (__state)->private_objs[__i].new_state, 1); \
888 	     (__i)++)
889 
890 /**
891  * drm_atomic_crtc_needs_modeset - compute combined modeset need
892  * @state: &drm_crtc_state for the CRTC
893  *
894  * To give drivers flexibility &struct drm_crtc_state has 3 booleans to track
895  * whether the state CRTC changed enough to need a full modeset cycle:
896  * mode_changed, active_changed and connectors_changed. This helper simply
897  * combines these three to compute the overall need for a modeset for @state.
898  *
899  * The atomic helper code sets these booleans, but drivers can and should
900  * change them appropriately to accurately represent whether a modeset is
901  * really needed. In general, drivers should avoid full modesets whenever
902  * possible.
903  *
904  * For example if the CRTC mode has changed, and the hardware is able to enact
905  * the requested mode change without going through a full modeset, the driver
906  * should clear mode_changed in its &drm_mode_config_funcs.atomic_check
907  * implementation.
908  */
909 static inline bool
drm_atomic_crtc_needs_modeset(const struct drm_crtc_state * state)910 drm_atomic_crtc_needs_modeset(const struct drm_crtc_state *state)
911 {
912 	return state->mode_changed || state->active_changed ||
913 	       state->connectors_changed;
914 }
915 
916 #endif /* DRM_ATOMIC_H_ */
917