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