xref: /dragonfly/sys/dev/drm/drm_atomic_helper.c (revision d2de761e)
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 #include <drm/drmP.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/dma-fence.h>
34 
35 #include "drm_crtc_internal.h"
36 
37 /**
38  * DOC: overview
39  *
40  * This helper library provides implementations of check and commit functions on
41  * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
42  * also provides convenience implementations for the atomic state handling
43  * callbacks for drivers which don't need to subclass the drm core structures to
44  * add their own additional internal state.
45  *
46  * This library also provides default implementations for the check callback in
47  * drm_atomic_helper_check() and for the commit callback with
48  * drm_atomic_helper_commit(). But the individual stages and callbacks are
49  * exposed to allow drivers to mix and match and e.g. use the plane helpers only
50  * together with a driver private modeset implementation.
51  *
52  * This library also provides implementations for all the legacy driver
53  * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
54  * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
55  * various functions to implement set_property callbacks. New drivers must not
56  * implement these functions themselves but must use the provided helpers.
57  *
58  * The atomic helper uses the same function table structures as all other
59  * modesetting helpers. See the documentation for &struct drm_crtc_helper_funcs,
60  * struct &drm_encoder_helper_funcs and &struct drm_connector_helper_funcs. It
61  * also shares the &struct drm_plane_helper_funcs function table with the plane
62  * helpers.
63  */
64 static void
65 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
66 				struct drm_plane_state *old_plane_state,
67 				struct drm_plane_state *plane_state,
68 				struct drm_plane *plane)
69 {
70 	struct drm_crtc_state *crtc_state;
71 
72 	if (old_plane_state->crtc) {
73 		crtc_state = drm_atomic_get_new_crtc_state(state,
74 							   old_plane_state->crtc);
75 
76 		if (WARN_ON(!crtc_state))
77 			return;
78 
79 		crtc_state->planes_changed = true;
80 	}
81 
82 	if (plane_state->crtc) {
83 		crtc_state = drm_atomic_get_new_crtc_state(state, plane_state->crtc);
84 
85 		if (WARN_ON(!crtc_state))
86 			return;
87 
88 		crtc_state->planes_changed = true;
89 	}
90 }
91 
92 static int handle_conflicting_encoders(struct drm_atomic_state *state,
93 				       bool disable_conflicting_encoders)
94 {
95 	struct drm_connector_state *new_conn_state;
96 	struct drm_connector *connector;
97 	struct drm_connector_list_iter conn_iter;
98 	struct drm_encoder *encoder;
99 	unsigned encoder_mask = 0;
100 	int i, ret = 0;
101 
102 	/*
103 	 * First loop, find all newly assigned encoders from the connectors
104 	 * part of the state. If the same encoder is assigned to multiple
105 	 * connectors bail out.
106 	 */
107 	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
108 		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
109 		struct drm_encoder *new_encoder;
110 
111 		if (!new_conn_state->crtc)
112 			continue;
113 
114 		if (funcs->atomic_best_encoder)
115 			new_encoder = funcs->atomic_best_encoder(connector, new_conn_state);
116 		else if (funcs->best_encoder)
117 			new_encoder = funcs->best_encoder(connector);
118 		else
119 			new_encoder = drm_atomic_helper_best_encoder(connector);
120 
121 		if (new_encoder) {
122 			if (encoder_mask & (1 << drm_encoder_index(new_encoder))) {
123 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] on [CONNECTOR:%d:%s] already assigned\n",
124 					new_encoder->base.id, new_encoder->name,
125 					connector->base.id, connector->name);
126 
127 				return -EINVAL;
128 			}
129 
130 			encoder_mask |= 1 << drm_encoder_index(new_encoder);
131 		}
132 	}
133 
134 	if (!encoder_mask)
135 		return 0;
136 
137 	/*
138 	 * Second loop, iterate over all connectors not part of the state.
139 	 *
140 	 * If a conflicting encoder is found and disable_conflicting_encoders
141 	 * is not set, an error is returned. Userspace can provide a solution
142 	 * through the atomic ioctl.
143 	 *
144 	 * If the flag is set conflicting connectors are removed from the crtc
145 	 * and the crtc is disabled if no encoder is left. This preserves
146 	 * compatibility with the legacy set_config behavior.
147 	 */
148 	drm_connector_list_iter_begin(state->dev, &conn_iter);
149 	drm_for_each_connector_iter(connector, &conn_iter) {
150 		struct drm_crtc_state *crtc_state;
151 
152 		if (drm_atomic_get_new_connector_state(state, connector))
153 			continue;
154 
155 		encoder = connector->state->best_encoder;
156 		if (!encoder || !(encoder_mask & (1 << drm_encoder_index(encoder))))
157 			continue;
158 
159 		if (!disable_conflicting_encoders) {
160 			DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s] by [CONNECTOR:%d:%s]\n",
161 					 encoder->base.id, encoder->name,
162 					 connector->state->crtc->base.id,
163 					 connector->state->crtc->name,
164 					 connector->base.id, connector->name);
165 			ret = -EINVAL;
166 			goto out;
167 		}
168 
169 		new_conn_state = drm_atomic_get_connector_state(state, connector);
170 		if (IS_ERR(new_conn_state)) {
171 			ret = PTR_ERR(new_conn_state);
172 			goto out;
173 		}
174 
175 		DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], disabling [CONNECTOR:%d:%s]\n",
176 				 encoder->base.id, encoder->name,
177 				 new_conn_state->crtc->base.id, new_conn_state->crtc->name,
178 				 connector->base.id, connector->name);
179 
180 		crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
181 
182 		ret = drm_atomic_set_crtc_for_connector(new_conn_state, NULL);
183 		if (ret)
184 			goto out;
185 
186 		if (!crtc_state->connector_mask) {
187 			ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
188 								NULL);
189 			if (ret < 0)
190 				goto out;
191 
192 			crtc_state->active = false;
193 		}
194 	}
195 out:
196 	drm_connector_list_iter_end(&conn_iter);
197 
198 	return ret;
199 }
200 
201 static void
202 set_best_encoder(struct drm_atomic_state *state,
203 		 struct drm_connector_state *conn_state,
204 		 struct drm_encoder *encoder)
205 {
206 	struct drm_crtc_state *crtc_state;
207 	struct drm_crtc *crtc;
208 
209 	if (conn_state->best_encoder) {
210 		/* Unset the encoder_mask in the old crtc state. */
211 		crtc = conn_state->connector->state->crtc;
212 
213 		/* A NULL crtc is an error here because we should have
214 		 *  duplicated a NULL best_encoder when crtc was NULL.
215 		 * As an exception restoring duplicated atomic state
216 		 * during resume is allowed, so don't warn when
217 		 * best_encoder is equal to encoder we intend to set.
218 		 */
219 		WARN_ON(!crtc && encoder != conn_state->best_encoder);
220 		if (crtc) {
221 			crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
222 
223 			crtc_state->encoder_mask &=
224 				~(1 << drm_encoder_index(conn_state->best_encoder));
225 		}
226 	}
227 
228 	if (encoder) {
229 		crtc = conn_state->crtc;
230 		WARN_ON(!crtc);
231 		if (crtc) {
232 			crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
233 
234 			crtc_state->encoder_mask |=
235 				1 << drm_encoder_index(encoder);
236 		}
237 	}
238 
239 	conn_state->best_encoder = encoder;
240 }
241 
242 static void
243 steal_encoder(struct drm_atomic_state *state,
244 	      struct drm_encoder *encoder)
245 {
246 	struct drm_crtc_state *crtc_state;
247 	struct drm_connector *connector;
248 	struct drm_connector_state *old_connector_state, *new_connector_state;
249 	int i;
250 
251 	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
252 		struct drm_crtc *encoder_crtc;
253 
254 		if (new_connector_state->best_encoder != encoder)
255 			continue;
256 
257 		encoder_crtc = old_connector_state->crtc;
258 
259 		DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
260 				 encoder->base.id, encoder->name,
261 				 encoder_crtc->base.id, encoder_crtc->name);
262 
263 		set_best_encoder(state, new_connector_state, NULL);
264 
265 		crtc_state = drm_atomic_get_new_crtc_state(state, encoder_crtc);
266 		crtc_state->connectors_changed = true;
267 
268 		return;
269 	}
270 }
271 
272 static int
273 update_connector_routing(struct drm_atomic_state *state,
274 			 struct drm_connector *connector,
275 			 struct drm_connector_state *old_connector_state,
276 			 struct drm_connector_state *new_connector_state)
277 {
278 	const struct drm_connector_helper_funcs *funcs;
279 	struct drm_encoder *new_encoder;
280 	struct drm_crtc_state *crtc_state;
281 
282 	DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
283 			 connector->base.id,
284 			 connector->name);
285 
286 	if (old_connector_state->crtc != new_connector_state->crtc) {
287 		if (old_connector_state->crtc) {
288 			crtc_state = drm_atomic_get_new_crtc_state(state, old_connector_state->crtc);
289 			crtc_state->connectors_changed = true;
290 		}
291 
292 		if (new_connector_state->crtc) {
293 			crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
294 			crtc_state->connectors_changed = true;
295 		}
296 	}
297 
298 	if (!new_connector_state->crtc) {
299 		DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
300 				connector->base.id,
301 				connector->name);
302 
303 		set_best_encoder(state, new_connector_state, NULL);
304 
305 		return 0;
306 	}
307 
308 	funcs = connector->helper_private;
309 
310 	if (funcs->atomic_best_encoder)
311 		new_encoder = funcs->atomic_best_encoder(connector,
312 							 new_connector_state);
313 	else if (funcs->best_encoder)
314 		new_encoder = funcs->best_encoder(connector);
315 	else
316 		new_encoder = drm_atomic_helper_best_encoder(connector);
317 
318 	if (!new_encoder) {
319 		DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
320 				 connector->base.id,
321 				 connector->name);
322 		return -EINVAL;
323 	}
324 
325 	if (!drm_encoder_crtc_ok(new_encoder, new_connector_state->crtc)) {
326 		DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d:%s]\n",
327 				 new_encoder->base.id,
328 				 new_encoder->name,
329 				 new_connector_state->crtc->base.id,
330 				 new_connector_state->crtc->name);
331 		return -EINVAL;
332 	}
333 
334 	if (new_encoder == new_connector_state->best_encoder) {
335 		set_best_encoder(state, new_connector_state, new_encoder);
336 
337 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
338 				 connector->base.id,
339 				 connector->name,
340 				 new_encoder->base.id,
341 				 new_encoder->name,
342 				 new_connector_state->crtc->base.id,
343 				 new_connector_state->crtc->name);
344 
345 		return 0;
346 	}
347 
348 	steal_encoder(state, new_encoder);
349 
350 	set_best_encoder(state, new_connector_state, new_encoder);
351 
352 	crtc_state = drm_atomic_get_new_crtc_state(state, new_connector_state->crtc);
353 	crtc_state->connectors_changed = true;
354 
355 	DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
356 			 connector->base.id,
357 			 connector->name,
358 			 new_encoder->base.id,
359 			 new_encoder->name,
360 			 new_connector_state->crtc->base.id,
361 			 new_connector_state->crtc->name);
362 
363 	return 0;
364 }
365 
366 static int
367 mode_fixup(struct drm_atomic_state *state)
368 {
369 	struct drm_crtc *crtc;
370 	struct drm_crtc_state *new_crtc_state;
371 	struct drm_connector *connector;
372 	struct drm_connector_state *new_conn_state;
373 	int i;
374 	int ret;
375 
376 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
377 		if (!new_crtc_state->mode_changed &&
378 		    !new_crtc_state->connectors_changed)
379 			continue;
380 
381 		drm_mode_copy(&new_crtc_state->adjusted_mode, &new_crtc_state->mode);
382 	}
383 
384 	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
385 		const struct drm_encoder_helper_funcs *funcs;
386 		struct drm_encoder *encoder;
387 
388 		WARN_ON(!!new_conn_state->best_encoder != !!new_conn_state->crtc);
389 
390 		if (!new_conn_state->crtc || !new_conn_state->best_encoder)
391 			continue;
392 
393 		new_crtc_state =
394 			drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
395 
396 		/*
397 		 * Each encoder has at most one connector (since we always steal
398 		 * it away), so we won't call ->mode_fixup twice.
399 		 */
400 		encoder = new_conn_state->best_encoder;
401 		funcs = encoder->helper_private;
402 
403 		ret = drm_bridge_mode_fixup(encoder->bridge, &new_crtc_state->mode,
404 				&new_crtc_state->adjusted_mode);
405 		if (!ret) {
406 			DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
407 			return -EINVAL;
408 		}
409 
410 		if (funcs && funcs->atomic_check) {
411 			ret = funcs->atomic_check(encoder, new_crtc_state,
412 						  new_conn_state);
413 			if (ret) {
414 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
415 						 encoder->base.id, encoder->name);
416 				return ret;
417 			}
418 		} else if (funcs && funcs->mode_fixup) {
419 			ret = funcs->mode_fixup(encoder, &new_crtc_state->mode,
420 						&new_crtc_state->adjusted_mode);
421 			if (!ret) {
422 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
423 						 encoder->base.id, encoder->name);
424 				return -EINVAL;
425 			}
426 		}
427 	}
428 
429 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
430 		const struct drm_crtc_helper_funcs *funcs;
431 
432 		if (!new_crtc_state->enable)
433 			continue;
434 
435 		if (!new_crtc_state->mode_changed &&
436 		    !new_crtc_state->connectors_changed)
437 			continue;
438 
439 		funcs = crtc->helper_private;
440 		if (!funcs->mode_fixup)
441 			continue;
442 
443 		ret = funcs->mode_fixup(crtc, &new_crtc_state->mode,
444 					&new_crtc_state->adjusted_mode);
445 		if (!ret) {
446 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
447 					 crtc->base.id, crtc->name);
448 			return -EINVAL;
449 		}
450 	}
451 
452 	return 0;
453 }
454 
455 /**
456  * drm_atomic_helper_check_modeset - validate state object for modeset changes
457  * @dev: DRM device
458  * @state: the driver state object
459  *
460  * Check the state object to see if the requested state is physically possible.
461  * This does all the crtc and connector related computations for an atomic
462  * update and adds any additional connectors needed for full modesets. It calls
463  * the various per-object callbacks in the follow order:
464  *
465  * 1. &drm_connector_helper_funcs.atomic_best_encoder for determining the new encoder.
466  * 2. &drm_connector_helper_funcs.atomic_check to validate the connector state.
467  * 3. If it's determined a modeset is needed then all connectors on the affected crtc
468  *    crtc are added and &drm_connector_helper_funcs.atomic_check is run on them.
469  * 4. &drm_bridge_funcs.mode_fixup is called on all encoder bridges.
470  * 5. &drm_encoder_helper_funcs.atomic_check is called to validate any encoder state.
471  *    This function is only called when the encoder will be part of a configured crtc,
472  *    it must not be used for implementing connector property validation.
473  *    If this function is NULL, &drm_atomic_encoder_helper_funcs.mode_fixup is called
474  *    instead.
475  * 6. &drm_crtc_helper_funcs.mode_fixup is called last, to fix up the mode with crtc constraints.
476  *
477  * &drm_crtc_state.mode_changed is set when the input mode is changed.
478  * &drm_crtc_state.connectors_changed is set when a connector is added or
479  * removed from the crtc.  &drm_crtc_state.active_changed is set when
480  * &drm_crtc_state.active changes, which is used for DPMS.
481  * See also: drm_atomic_crtc_needs_modeset()
482  *
483  * IMPORTANT:
484  *
485  * Drivers which set &drm_crtc_state.mode_changed (e.g. in their
486  * &drm_plane_helper_funcs.atomic_check hooks if a plane update can't be done
487  * without a full modeset) _must_ call this function afterwards after that
488  * change. It is permitted to call this function multiple times for the same
489  * update, e.g. when the &drm_crtc_helper_funcs.atomic_check functions depend
490  * upon the adjusted dotclock for fifo space allocation and watermark
491  * computation.
492  *
493  * RETURNS:
494  * Zero for success or -errno
495  */
496 int
497 drm_atomic_helper_check_modeset(struct drm_device *dev,
498 				struct drm_atomic_state *state)
499 {
500 	struct drm_crtc *crtc;
501 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
502 	struct drm_connector *connector;
503 	struct drm_connector_state *old_connector_state, *new_connector_state;
504 	int i, ret;
505 	unsigned connectors_mask = 0;
506 
507 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
508 		bool has_connectors =
509 			!!new_crtc_state->connector_mask;
510 
511 		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
512 
513 		if (!drm_mode_equal(&old_crtc_state->mode, &new_crtc_state->mode)) {
514 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
515 					 crtc->base.id, crtc->name);
516 			new_crtc_state->mode_changed = true;
517 		}
518 
519 		if (old_crtc_state->enable != new_crtc_state->enable) {
520 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
521 					 crtc->base.id, crtc->name);
522 
523 			/*
524 			 * For clarity this assignment is done here, but
525 			 * enable == 0 is only true when there are no
526 			 * connectors and a NULL mode.
527 			 *
528 			 * The other way around is true as well. enable != 0
529 			 * iff connectors are attached and a mode is set.
530 			 */
531 			new_crtc_state->mode_changed = true;
532 			new_crtc_state->connectors_changed = true;
533 		}
534 
535 		if (old_crtc_state->active != new_crtc_state->active) {
536 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
537 					 crtc->base.id, crtc->name);
538 			new_crtc_state->active_changed = true;
539 		}
540 
541 		if (new_crtc_state->enable != has_connectors) {
542 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
543 					 crtc->base.id, crtc->name);
544 
545 			return -EINVAL;
546 		}
547 	}
548 
549 	ret = handle_conflicting_encoders(state, false);
550 	if (ret)
551 		return ret;
552 
553 	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
554 		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
555 
556 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
557 
558 		/*
559 		 * This only sets crtc->connectors_changed for routing changes,
560 		 * drivers must set crtc->connectors_changed themselves when
561 		 * connector properties need to be updated.
562 		 */
563 		ret = update_connector_routing(state, connector,
564 					       old_connector_state,
565 					       new_connector_state);
566 		if (ret)
567 			return ret;
568 		if (old_connector_state->crtc) {
569 			new_crtc_state = drm_atomic_get_new_crtc_state(state,
570 								       old_connector_state->crtc);
571 			if (old_connector_state->link_status !=
572 			    new_connector_state->link_status)
573 				new_crtc_state->connectors_changed = true;
574 		}
575 
576 		if (funcs->atomic_check)
577 			ret = funcs->atomic_check(connector, new_connector_state);
578 		if (ret)
579 			return ret;
580 
581 		connectors_mask += BIT(i);
582 	}
583 
584 	/*
585 	 * After all the routing has been prepared we need to add in any
586 	 * connector which is itself unchanged, but who's crtc changes it's
587 	 * configuration. This must be done before calling mode_fixup in case a
588 	 * crtc only changed its mode but has the same set of connectors.
589 	 */
590 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
591 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
592 			continue;
593 
594 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
595 				 crtc->base.id, crtc->name,
596 				 new_crtc_state->enable ? 'y' : 'n',
597 				 new_crtc_state->active ? 'y' : 'n');
598 
599 		ret = drm_atomic_add_affected_connectors(state, crtc);
600 		if (ret != 0)
601 			return ret;
602 
603 		ret = drm_atomic_add_affected_planes(state, crtc);
604 		if (ret != 0)
605 			return ret;
606 	}
607 
608 	/*
609 	 * Iterate over all connectors again, to make sure atomic_check()
610 	 * has been called on them when a modeset is forced.
611 	 */
612 	for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
613 		const struct drm_connector_helper_funcs *funcs = connector->helper_private;
614 
615 		if (connectors_mask & BIT(i))
616 			continue;
617 
618 		if (funcs->atomic_check)
619 			ret = funcs->atomic_check(connector, new_connector_state);
620 		if (ret)
621 			return ret;
622 	}
623 
624 	return mode_fixup(state);
625 }
626 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
627 
628 /**
629  * drm_atomic_helper_check_planes - validate state object for planes changes
630  * @dev: DRM device
631  * @state: the driver state object
632  *
633  * Check the state object to see if the requested state is physically possible.
634  * This does all the plane update related checks using by calling into the
635  * &drm_crtc_helper_funcs.atomic_check and &drm_plane_helper_funcs.atomic_check
636  * hooks provided by the driver.
637  *
638  * It also sets &drm_crtc_state.planes_changed to indicate that a crtc has
639  * updated planes.
640  *
641  * RETURNS:
642  * Zero for success or -errno
643  */
644 int
645 drm_atomic_helper_check_planes(struct drm_device *dev,
646 			       struct drm_atomic_state *state)
647 {
648 	struct drm_crtc *crtc;
649 	struct drm_crtc_state *new_crtc_state;
650 	struct drm_plane *plane;
651 	struct drm_plane_state *new_plane_state, *old_plane_state;
652 	int i, ret = 0;
653 
654 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
655 		const struct drm_plane_helper_funcs *funcs;
656 
657 		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
658 
659 		funcs = plane->helper_private;
660 
661 		drm_atomic_helper_plane_changed(state, old_plane_state, new_plane_state, plane);
662 
663 		if (!funcs || !funcs->atomic_check)
664 			continue;
665 
666 		ret = funcs->atomic_check(plane, new_plane_state);
667 		if (ret) {
668 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
669 					 plane->base.id, plane->name);
670 			return ret;
671 		}
672 	}
673 
674 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
675 		const struct drm_crtc_helper_funcs *funcs;
676 
677 		funcs = crtc->helper_private;
678 
679 		if (!funcs || !funcs->atomic_check)
680 			continue;
681 
682 		ret = funcs->atomic_check(crtc, new_crtc_state);
683 		if (ret) {
684 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
685 					 crtc->base.id, crtc->name);
686 			return ret;
687 		}
688 	}
689 
690 	return ret;
691 }
692 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
693 
694 /**
695  * drm_atomic_helper_check - validate state object
696  * @dev: DRM device
697  * @state: the driver state object
698  *
699  * Check the state object to see if the requested state is physically possible.
700  * Only crtcs and planes have check callbacks, so for any additional (global)
701  * checking that a driver needs it can simply wrap that around this function.
702  * Drivers without such needs can directly use this as their
703  * &drm_mode_config_funcs.atomic_check callback.
704  *
705  * This just wraps the two parts of the state checking for planes and modeset
706  * state in the default order: First it calls drm_atomic_helper_check_modeset()
707  * and then drm_atomic_helper_check_planes(). The assumption is that the
708  * @drm_plane_helper_funcs.atomic_check and @drm_crtc_helper_funcs.atomic_check
709  * functions depend upon an updated adjusted_mode.clock to e.g. properly compute
710  * watermarks.
711  *
712  * RETURNS:
713  * Zero for success or -errno
714  */
715 int drm_atomic_helper_check(struct drm_device *dev,
716 			    struct drm_atomic_state *state)
717 {
718 	int ret;
719 
720 	ret = drm_atomic_helper_check_modeset(dev, state);
721 	if (ret)
722 		return ret;
723 
724 	ret = drm_atomic_helper_check_planes(dev, state);
725 	if (ret)
726 		return ret;
727 
728 	return ret;
729 }
730 EXPORT_SYMBOL(drm_atomic_helper_check);
731 
732 static void
733 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
734 {
735 	struct drm_connector *connector;
736 	struct drm_connector_state *old_conn_state, *new_conn_state;
737 	struct drm_crtc *crtc;
738 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
739 	int i;
740 
741 	for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
742 		const struct drm_encoder_helper_funcs *funcs;
743 		struct drm_encoder *encoder;
744 
745 		/* Shut down everything that's in the changeset and currently
746 		 * still on. So need to check the old, saved state. */
747 		if (!old_conn_state->crtc)
748 			continue;
749 
750 		old_crtc_state = drm_atomic_get_old_crtc_state(old_state, old_conn_state->crtc);
751 
752 		if (!old_crtc_state->active ||
753 		    !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
754 			continue;
755 
756 		encoder = old_conn_state->best_encoder;
757 
758 		/* We shouldn't get this far if we didn't previously have
759 		 * an encoder.. but WARN_ON() rather than explode.
760 		 */
761 		if (WARN_ON(!encoder))
762 			continue;
763 
764 		funcs = encoder->helper_private;
765 
766 		DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
767 				 encoder->base.id, encoder->name);
768 
769 		/*
770 		 * Each encoder has at most one connector (since we always steal
771 		 * it away), so we won't call disable hooks twice.
772 		 */
773 		drm_bridge_disable(encoder->bridge);
774 
775 		/* Right function depends upon target state. */
776 		if (funcs) {
777 			if (new_conn_state->crtc && funcs->prepare)
778 				funcs->prepare(encoder);
779 			else if (funcs->disable)
780 				funcs->disable(encoder);
781 			else if (funcs->dpms)
782 				funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
783 		}
784 
785 		drm_bridge_post_disable(encoder->bridge);
786 	}
787 
788 	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
789 		const struct drm_crtc_helper_funcs *funcs;
790 
791 		/* Shut down everything that needs a full modeset. */
792 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
793 			continue;
794 
795 		if (!old_crtc_state->active)
796 			continue;
797 
798 		funcs = crtc->helper_private;
799 
800 		DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
801 				 crtc->base.id, crtc->name);
802 
803 
804 		/* Right function depends upon target state. */
805 		if (new_crtc_state->enable && funcs->prepare)
806 			funcs->prepare(crtc);
807 		else if (funcs->atomic_disable)
808 			funcs->atomic_disable(crtc, old_crtc_state);
809 		else if (funcs->disable)
810 			funcs->disable(crtc);
811 		else
812 			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
813 	}
814 }
815 
816 /**
817  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
818  * @dev: DRM device
819  * @old_state: atomic state object with old state structures
820  *
821  * This function updates all the various legacy modeset state pointers in
822  * connectors, encoders and crtcs. It also updates the timestamping constants
823  * used for precise vblank timestamps by calling
824  * drm_calc_timestamping_constants().
825  *
826  * Drivers can use this for building their own atomic commit if they don't have
827  * a pure helper-based modeset implementation.
828  */
829 void
830 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
831 					      struct drm_atomic_state *old_state)
832 {
833 	struct drm_connector *connector;
834 	struct drm_connector_state *old_conn_state, *new_conn_state;
835 	struct drm_crtc *crtc;
836 	struct drm_crtc_state *new_crtc_state;
837 	int i;
838 
839 	/* clear out existing links and update dpms */
840 	for_each_oldnew_connector_in_state(old_state, connector, old_conn_state, new_conn_state, i) {
841 		if (connector->encoder) {
842 			WARN_ON(!connector->encoder->crtc);
843 
844 			connector->encoder->crtc = NULL;
845 			connector->encoder = NULL;
846 		}
847 
848 		crtc = new_conn_state->crtc;
849 		if ((!crtc && old_conn_state->crtc) ||
850 		    (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
851 			struct drm_property *dpms_prop =
852 				dev->mode_config.dpms_property;
853 			int mode = DRM_MODE_DPMS_OFF;
854 
855 			if (crtc && crtc->state->active)
856 				mode = DRM_MODE_DPMS_ON;
857 
858 			connector->dpms = mode;
859 			drm_object_property_set_value(&connector->base,
860 						      dpms_prop, mode);
861 		}
862 	}
863 
864 	/* set new links */
865 	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
866 		if (!new_conn_state->crtc)
867 			continue;
868 
869 		if (WARN_ON(!new_conn_state->best_encoder))
870 			continue;
871 
872 		connector->encoder = new_conn_state->best_encoder;
873 		connector->encoder->crtc = new_conn_state->crtc;
874 	}
875 
876 	/* set legacy state in the crtc structure */
877 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
878 		struct drm_plane *primary = crtc->primary;
879 		struct drm_plane_state *new_plane_state;
880 
881 		crtc->mode = new_crtc_state->mode;
882 		crtc->enabled = new_crtc_state->enable;
883 
884 		new_plane_state =
885 			drm_atomic_get_new_plane_state(old_state, primary);
886 
887 		if (new_plane_state && new_plane_state->crtc == crtc) {
888 			crtc->x = new_plane_state->src_x >> 16;
889 			crtc->y = new_plane_state->src_y >> 16;
890 		}
891 
892 		if (new_crtc_state->enable)
893 			drm_calc_timestamping_constants(crtc,
894 							&new_crtc_state->adjusted_mode);
895 	}
896 }
897 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
898 
899 static void
900 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
901 {
902 	struct drm_crtc *crtc;
903 	struct drm_crtc_state *new_crtc_state;
904 	struct drm_connector *connector;
905 	struct drm_connector_state *new_conn_state;
906 	int i;
907 
908 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
909 		const struct drm_crtc_helper_funcs *funcs;
910 
911 		if (!new_crtc_state->mode_changed)
912 			continue;
913 
914 		funcs = crtc->helper_private;
915 
916 		if (new_crtc_state->enable && funcs->mode_set_nofb) {
917 			DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
918 					 crtc->base.id, crtc->name);
919 
920 			funcs->mode_set_nofb(crtc);
921 		}
922 	}
923 
924 	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
925 		const struct drm_encoder_helper_funcs *funcs;
926 		struct drm_encoder *encoder;
927 		struct drm_display_mode *mode, *adjusted_mode;
928 
929 		if (!new_conn_state->best_encoder)
930 			continue;
931 
932 		encoder = new_conn_state->best_encoder;
933 		funcs = encoder->helper_private;
934 		new_crtc_state = new_conn_state->crtc->state;
935 		mode = &new_crtc_state->mode;
936 		adjusted_mode = &new_crtc_state->adjusted_mode;
937 
938 		if (!new_crtc_state->mode_changed)
939 			continue;
940 
941 		DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
942 				 encoder->base.id, encoder->name);
943 
944 		/*
945 		 * Each encoder has at most one connector (since we always steal
946 		 * it away), so we won't call mode_set hooks twice.
947 		 */
948 		if (funcs && funcs->atomic_mode_set) {
949 			funcs->atomic_mode_set(encoder, new_crtc_state,
950 					       new_conn_state);
951 		} else if (funcs && funcs->mode_set) {
952 			funcs->mode_set(encoder, mode, adjusted_mode);
953 		}
954 
955 		drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
956 	}
957 }
958 
959 /**
960  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
961  * @dev: DRM device
962  * @old_state: atomic state object with old state structures
963  *
964  * This function shuts down all the outputs that need to be shut down and
965  * prepares them (if required) with the new mode.
966  *
967  * For compatibility with legacy crtc helpers this should be called before
968  * drm_atomic_helper_commit_planes(), which is what the default commit function
969  * does. But drivers with different needs can group the modeset commits together
970  * and do the plane commits at the end. This is useful for drivers doing runtime
971  * PM since planes updates then only happen when the CRTC is actually enabled.
972  */
973 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
974 					       struct drm_atomic_state *old_state)
975 {
976 	disable_outputs(dev, old_state);
977 
978 	drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
979 
980 	crtc_set_mode(dev, old_state);
981 }
982 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
983 
984 /**
985  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
986  * @dev: DRM device
987  * @old_state: atomic state object with old state structures
988  *
989  * This function enables all the outputs with the new configuration which had to
990  * be turned off for the update.
991  *
992  * For compatibility with legacy crtc helpers this should be called after
993  * drm_atomic_helper_commit_planes(), which is what the default commit function
994  * does. But drivers with different needs can group the modeset commits together
995  * and do the plane commits at the end. This is useful for drivers doing runtime
996  * PM since planes updates then only happen when the CRTC is actually enabled.
997  */
998 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
999 					      struct drm_atomic_state *old_state)
1000 {
1001 	struct drm_crtc *crtc;
1002 	struct drm_crtc_state *new_crtc_state;
1003 	struct drm_connector *connector;
1004 	struct drm_connector_state *new_conn_state;
1005 	int i;
1006 
1007 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1008 		const struct drm_crtc_helper_funcs *funcs;
1009 
1010 		/* Need to filter out CRTCs where only planes change. */
1011 		if (!drm_atomic_crtc_needs_modeset(new_crtc_state))
1012 			continue;
1013 
1014 		if (!new_crtc_state->active)
1015 			continue;
1016 
1017 		funcs = crtc->helper_private;
1018 
1019 		if (new_crtc_state->enable) {
1020 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
1021 					 crtc->base.id, crtc->name);
1022 
1023 			if (funcs->enable)
1024 				funcs->enable(crtc);
1025 			else
1026 				funcs->commit(crtc);
1027 		}
1028 	}
1029 
1030 	for_each_new_connector_in_state(old_state, connector, new_conn_state, i) {
1031 		const struct drm_encoder_helper_funcs *funcs;
1032 		struct drm_encoder *encoder;
1033 
1034 		if (!new_conn_state->best_encoder)
1035 			continue;
1036 
1037 		if (!new_conn_state->crtc->state->active ||
1038 		    !drm_atomic_crtc_needs_modeset(new_conn_state->crtc->state))
1039 			continue;
1040 
1041 		encoder = new_conn_state->best_encoder;
1042 		funcs = encoder->helper_private;
1043 
1044 		DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
1045 				 encoder->base.id, encoder->name);
1046 
1047 		/*
1048 		 * Each encoder has at most one connector (since we always steal
1049 		 * it away), so we won't call enable hooks twice.
1050 		 */
1051 		drm_bridge_pre_enable(encoder->bridge);
1052 
1053 		if (funcs) {
1054 			if (funcs->enable)
1055 				funcs->enable(encoder);
1056 			else if (funcs->commit)
1057 				funcs->commit(encoder);
1058 		}
1059 
1060 		drm_bridge_enable(encoder->bridge);
1061 	}
1062 }
1063 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
1064 
1065 /**
1066  * drm_atomic_helper_wait_for_fences - wait for fences stashed in plane state
1067  * @dev: DRM device
1068  * @state: atomic state object with old state structures
1069  * @pre_swap: If true, do an interruptible wait, and @state is the new state.
1070  * 	Otherwise @state is the old state.
1071  *
1072  * For implicit sync, driver should fish the exclusive fence out from the
1073  * incoming fb's and stash it in the drm_plane_state.  This is called after
1074  * drm_atomic_helper_swap_state() so it uses the current plane state (and
1075  * just uses the atomic state to find the changed planes)
1076  *
1077  * Note that @pre_swap is needed since the point where we block for fences moves
1078  * around depending upon whether an atomic commit is blocking or
1079  * non-blocking. For async commit all waiting needs to happen after
1080  * drm_atomic_helper_swap_state() is called, but for synchronous commits we want
1081  * to wait **before** we do anything that can't be easily rolled back. That is
1082  * before we call drm_atomic_helper_swap_state().
1083  *
1084  * Returns zero if success or < 0 if dma_fence_wait() fails.
1085  */
1086 int drm_atomic_helper_wait_for_fences(struct drm_device *dev,
1087 				      struct drm_atomic_state *state,
1088 				      bool pre_swap)
1089 {
1090 	struct drm_plane *plane;
1091 	struct drm_plane_state *new_plane_state;
1092 	int i, ret;
1093 
1094 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1095 		if (!new_plane_state->fence)
1096 			continue;
1097 
1098 		WARN_ON(!new_plane_state->fb);
1099 
1100 		/*
1101 		 * If waiting for fences pre-swap (ie: nonblock), userspace can
1102 		 * still interrupt the operation. Instead of blocking until the
1103 		 * timer expires, make the wait interruptible.
1104 		 */
1105 		ret = dma_fence_wait(new_plane_state->fence, pre_swap);
1106 		if (ret)
1107 			return ret;
1108 
1109 		dma_fence_put(new_plane_state->fence);
1110 		new_plane_state->fence = NULL;
1111 	}
1112 
1113 	return 0;
1114 }
1115 EXPORT_SYMBOL(drm_atomic_helper_wait_for_fences);
1116 
1117 /**
1118  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
1119  * @dev: DRM device
1120  * @old_state: atomic state object with old state structures
1121  *
1122  * Helper to, after atomic commit, wait for vblanks on all effected
1123  * crtcs (ie. before cleaning up old framebuffers using
1124  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
1125  * framebuffers have actually changed to optimize for the legacy cursor and
1126  * plane update use-case.
1127  */
1128 void
1129 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
1130 		struct drm_atomic_state *old_state)
1131 {
1132 	struct drm_crtc *crtc;
1133 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1134 	int i, ret;
1135 	unsigned crtc_mask = 0;
1136 
1137 	 /*
1138 	  * Legacy cursor ioctls are completely unsynced, and userspace
1139 	  * relies on that (by doing tons of cursor updates).
1140 	  */
1141 	if (old_state->legacy_cursor_update)
1142 		return;
1143 
1144 	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1145 		if (!new_crtc_state->active || !new_crtc_state->planes_changed)
1146 			continue;
1147 
1148 		ret = drm_crtc_vblank_get(crtc);
1149 		if (ret != 0)
1150 			continue;
1151 
1152 		crtc_mask |= drm_crtc_mask(crtc);
1153 		old_state->crtcs[i].last_vblank_count = drm_crtc_vblank_count(crtc);
1154 	}
1155 
1156 	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1157 		if (!(crtc_mask & drm_crtc_mask(crtc)))
1158 			continue;
1159 
1160 		ret = wait_event_timeout(dev->vblank[i].queue,
1161 				old_state->crtcs[i].last_vblank_count !=
1162 					drm_crtc_vblank_count(crtc),
1163 				msecs_to_jiffies(50));
1164 
1165 		WARN(!ret, "[CRTC:%d:%s] vblank wait timed out\n",
1166 		     crtc->base.id, crtc->name);
1167 
1168 		drm_crtc_vblank_put(crtc);
1169 	}
1170 }
1171 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1172 
1173 /**
1174  * drm_atomic_helper_commit_tail - commit atomic update to hardware
1175  * @old_state: atomic state object with old state structures
1176  *
1177  * This is the default implementation for the
1178  * &drm_mode_config_helper_funcs.atomic_commit_tail hook.
1179  *
1180  * Note that the default ordering of how the various stages are called is to
1181  * match the legacy modeset helper library closest. One peculiarity of that is
1182  * that it doesn't mesh well with runtime PM at all.
1183  *
1184  * For drivers supporting runtime PM the recommended sequence is instead ::
1185  *
1186  *     drm_atomic_helper_commit_modeset_disables(dev, old_state);
1187  *
1188  *     drm_atomic_helper_commit_modeset_enables(dev, old_state);
1189  *
1190  *     drm_atomic_helper_commit_planes(dev, old_state,
1191  *                                     DRM_PLANE_COMMIT_ACTIVE_ONLY);
1192  *
1193  * for committing the atomic update to hardware.  See the kerneldoc entries for
1194  * these three functions for more details.
1195  */
1196 void drm_atomic_helper_commit_tail(struct drm_atomic_state *old_state)
1197 {
1198 	struct drm_device *dev = old_state->dev;
1199 
1200 	drm_atomic_helper_commit_modeset_disables(dev, old_state);
1201 
1202 	drm_atomic_helper_commit_planes(dev, old_state, 0);
1203 
1204 	drm_atomic_helper_commit_modeset_enables(dev, old_state);
1205 
1206 	drm_atomic_helper_commit_hw_done(old_state);
1207 
1208 	drm_atomic_helper_wait_for_vblanks(dev, old_state);
1209 
1210 	drm_atomic_helper_cleanup_planes(dev, old_state);
1211 }
1212 EXPORT_SYMBOL(drm_atomic_helper_commit_tail);
1213 
1214 static void commit_tail(struct drm_atomic_state *old_state)
1215 {
1216 	struct drm_device *dev = old_state->dev;
1217 	const struct drm_mode_config_helper_funcs *funcs;
1218 
1219 	funcs = dev->mode_config.helper_private;
1220 
1221 	drm_atomic_helper_wait_for_fences(dev, old_state, false);
1222 
1223 	drm_atomic_helper_wait_for_dependencies(old_state);
1224 
1225 	if (funcs && funcs->atomic_commit_tail)
1226 		funcs->atomic_commit_tail(old_state);
1227 	else
1228 		drm_atomic_helper_commit_tail(old_state);
1229 
1230 	drm_atomic_helper_commit_cleanup_done(old_state);
1231 
1232 	drm_atomic_state_put(old_state);
1233 }
1234 
1235 static void commit_work(struct work_struct *work)
1236 {
1237 	struct drm_atomic_state *state = container_of(work,
1238 						      struct drm_atomic_state,
1239 						      commit_work);
1240 	commit_tail(state);
1241 }
1242 
1243 /**
1244  * drm_atomic_helper_commit - commit validated state object
1245  * @dev: DRM device
1246  * @state: the driver state object
1247  * @nonblock: whether nonblocking behavior is requested.
1248  *
1249  * This function commits a with drm_atomic_helper_check() pre-validated state
1250  * object. This can still fail when e.g. the framebuffer reservation fails. This
1251  * function implements nonblocking commits, using
1252  * drm_atomic_helper_setup_commit() and related functions.
1253  *
1254  * Committing the actual hardware state is done through the
1255  * &drm_mode_config_helper_funcs.atomic_commit_tail callback, or it's default
1256  * implementation drm_atomic_helper_commit_tail().
1257  *
1258  * RETURNS:
1259  * Zero for success or -errno.
1260  */
1261 int drm_atomic_helper_commit(struct drm_device *dev,
1262 			     struct drm_atomic_state *state,
1263 			     bool nonblock)
1264 {
1265 	int ret;
1266 
1267 	ret = drm_atomic_helper_setup_commit(state, nonblock);
1268 	if (ret)
1269 		return ret;
1270 
1271 	INIT_WORK(&state->commit_work, commit_work);
1272 
1273 	ret = drm_atomic_helper_prepare_planes(dev, state);
1274 	if (ret)
1275 		return ret;
1276 
1277 	if (!nonblock) {
1278 		ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1279 		if (ret) {
1280 			drm_atomic_helper_cleanup_planes(dev, state);
1281 			return ret;
1282 		}
1283 	}
1284 
1285 	/*
1286 	 * This is the point of no return - everything below never fails except
1287 	 * when the hw goes bonghits. Which means we can commit the new state on
1288 	 * the software side now.
1289 	 */
1290 
1291 	drm_atomic_helper_swap_state(state, true);
1292 
1293 	/*
1294 	 * Everything below can be run asynchronously without the need to grab
1295 	 * any modeset locks at all under one condition: It must be guaranteed
1296 	 * that the asynchronous work has either been cancelled (if the driver
1297 	 * supports it, which at least requires that the framebuffers get
1298 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1299 	 * before the new state gets committed on the software side with
1300 	 * drm_atomic_helper_swap_state().
1301 	 *
1302 	 * This scheme allows new atomic state updates to be prepared and
1303 	 * checked in parallel to the asynchronous completion of the previous
1304 	 * update. Which is important since compositors need to figure out the
1305 	 * composition of the next frame right after having submitted the
1306 	 * current layout.
1307 	 *
1308 	 * NOTE: Commit work has multiple phases, first hardware commit, then
1309 	 * cleanup. We want them to overlap, hence need system_unbound_wq to
1310 	 * make sure work items don't artifically stall on each another.
1311 	 */
1312 
1313 	drm_atomic_state_get(state);
1314 	if (nonblock)
1315 		queue_work(system_unbound_wq, &state->commit_work);
1316 	else
1317 		commit_tail(state);
1318 
1319 	return 0;
1320 }
1321 EXPORT_SYMBOL(drm_atomic_helper_commit);
1322 
1323 /**
1324  * DOC: implementing nonblocking commit
1325  *
1326  * Nonblocking atomic commits have to be implemented in the following sequence:
1327  *
1328  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1329  * which commit needs to call which can fail, so we want to run it first and
1330  * synchronously.
1331  *
1332  * 2. Synchronize with any outstanding nonblocking commit worker threads which
1333  * might be affected the new state update. This can be done by either cancelling
1334  * or flushing the work items, depending upon whether the driver can deal with
1335  * cancelled updates. Note that it is important to ensure that the framebuffer
1336  * cleanup is still done when cancelling.
1337  *
1338  * Asynchronous workers need to have sufficient parallelism to be able to run
1339  * different atomic commits on different CRTCs in parallel. The simplest way to
1340  * achive this is by running them on the &system_unbound_wq work queue. Note
1341  * that drivers are not required to split up atomic commits and run an
1342  * individual commit in parallel - userspace is supposed to do that if it cares.
1343  * But it might be beneficial to do that for modesets, since those necessarily
1344  * must be done as one global operation, and enabling or disabling a CRTC can
1345  * take a long time. But even that is not required.
1346  *
1347  * 3. The software state is updated synchronously with
1348  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1349  * locks means concurrent callers never see inconsistent state. And doing this
1350  * while it's guaranteed that no relevant nonblocking worker runs means that
1351  * nonblocking workers do not need grab any locks. Actually they must not grab
1352  * locks, for otherwise the work flushing will deadlock.
1353  *
1354  * 4. Schedule a work item to do all subsequent steps, using the split-out
1355  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1356  * then cleaning up the framebuffers after the old framebuffer is no longer
1357  * being displayed.
1358  *
1359  * The above scheme is implemented in the atomic helper libraries in
1360  * drm_atomic_helper_commit() using a bunch of helper functions. See
1361  * drm_atomic_helper_setup_commit() for a starting point.
1362  */
1363 
1364 static int stall_checks(struct drm_crtc *crtc, bool nonblock)
1365 {
1366 	struct drm_crtc_commit *commit, *stall_commit = NULL;
1367 	bool completed = true;
1368 	int i;
1369 	long ret = 0;
1370 
1371 	lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
1372 	i = 0;
1373 	list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1374 		if (i == 0) {
1375 			completed = try_wait_for_completion(&commit->flip_done);
1376 			/* Userspace is not allowed to get ahead of the previous
1377 			 * commit with nonblocking ones. */
1378 			if (!completed && nonblock) {
1379 				lockmgr(&crtc->commit_lock, LK_RELEASE);
1380 				return -EBUSY;
1381 			}
1382 		} else if (i == 1) {
1383 			stall_commit = commit;
1384 			drm_crtc_commit_get(stall_commit);
1385 			break;
1386 		}
1387 
1388 		i++;
1389 	}
1390 	lockmgr(&crtc->commit_lock, LK_RELEASE);
1391 
1392 	if (!stall_commit)
1393 		return 0;
1394 
1395 	/* We don't want to let commits get ahead of cleanup work too much,
1396 	 * stalling on 2nd previous commit means triple-buffer won't ever stall.
1397 	 */
1398 	ret = wait_for_completion_interruptible_timeout(&stall_commit->cleanup_done,
1399 							10*HZ);
1400 	if (ret == 0)
1401 		DRM_ERROR("[CRTC:%d:%s] cleanup_done timed out\n",
1402 			  crtc->base.id, crtc->name);
1403 
1404 	drm_crtc_commit_put(stall_commit);
1405 
1406 	return ret < 0 ? ret : 0;
1407 }
1408 
1409 static void release_crtc_commit(struct completion *completion)
1410 {
1411 	struct drm_crtc_commit *commit = container_of(completion,
1412 						      typeof(*commit),
1413 						      flip_done);
1414 
1415 	drm_crtc_commit_put(commit);
1416 }
1417 
1418 /**
1419  * drm_atomic_helper_setup_commit - setup possibly nonblocking commit
1420  * @state: new modeset state to be committed
1421  * @nonblock: whether nonblocking behavior is requested.
1422  *
1423  * This function prepares @state to be used by the atomic helper's support for
1424  * nonblocking commits. Drivers using the nonblocking commit infrastructure
1425  * should always call this function from their
1426  * &drm_mode_config_funcs.atomic_commit hook.
1427  *
1428  * To be able to use this support drivers need to use a few more helper
1429  * functions. drm_atomic_helper_wait_for_dependencies() must be called before
1430  * actually committing the hardware state, and for nonblocking commits this call
1431  * must be placed in the async worker. See also drm_atomic_helper_swap_state()
1432  * and it's stall parameter, for when a driver's commit hooks look at the
1433  * &drm_crtc.state, &drm_plane.state or &drm_connector.state pointer directly.
1434  *
1435  * Completion of the hardware commit step must be signalled using
1436  * drm_atomic_helper_commit_hw_done(). After this step the driver is not allowed
1437  * to read or change any permanent software or hardware modeset state. The only
1438  * exception is state protected by other means than &drm_modeset_lock locks.
1439  * Only the free standing @state with pointers to the old state structures can
1440  * be inspected, e.g. to clean up old buffers using
1441  * drm_atomic_helper_cleanup_planes().
1442  *
1443  * At the very end, before cleaning up @state drivers must call
1444  * drm_atomic_helper_commit_cleanup_done().
1445  *
1446  * This is all implemented by in drm_atomic_helper_commit(), giving drivers a
1447  * complete and esay-to-use default implementation of the atomic_commit() hook.
1448  *
1449  * The tracking of asynchronously executed and still pending commits is done
1450  * using the core structure &drm_crtc_commit.
1451  *
1452  * By default there's no need to clean up resources allocated by this function
1453  * explicitly: drm_atomic_state_default_clear() will take care of that
1454  * automatically.
1455  *
1456  * Returns:
1457  *
1458  * 0 on success. -EBUSY when userspace schedules nonblocking commits too fast,
1459  * -ENOMEM on allocation failures and -EINTR when a signal is pending.
1460  */
1461 int drm_atomic_helper_setup_commit(struct drm_atomic_state *state,
1462 				   bool nonblock)
1463 {
1464 	struct drm_crtc *crtc;
1465 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1466 	struct drm_crtc_commit *commit;
1467 	int i, ret;
1468 
1469 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1470 		commit = kzalloc(sizeof(*commit), GFP_KERNEL);
1471 		if (!commit)
1472 			return -ENOMEM;
1473 
1474 		init_completion(&commit->flip_done);
1475 		init_completion(&commit->hw_done);
1476 		init_completion(&commit->cleanup_done);
1477 		INIT_LIST_HEAD(&commit->commit_entry);
1478 		kref_init(&commit->ref);
1479 		commit->crtc = crtc;
1480 
1481 		state->crtcs[i].commit = commit;
1482 
1483 		ret = stall_checks(crtc, nonblock);
1484 		if (ret)
1485 			return ret;
1486 
1487 		/* Drivers only send out events when at least either current or
1488 		 * new CRTC state is active. Complete right away if everything
1489 		 * stays off. */
1490 		if (!old_crtc_state->active && !new_crtc_state->active) {
1491 			complete_all(&commit->flip_done);
1492 			continue;
1493 		}
1494 
1495 		/* Legacy cursor updates are fully unsynced. */
1496 		if (state->legacy_cursor_update) {
1497 			complete_all(&commit->flip_done);
1498 			continue;
1499 		}
1500 
1501 		if (!new_crtc_state->event) {
1502 			commit->event = kzalloc(sizeof(*commit->event),
1503 						GFP_KERNEL);
1504 			if (!commit->event)
1505 				return -ENOMEM;
1506 
1507 			new_crtc_state->event = commit->event;
1508 		}
1509 
1510 		new_crtc_state->event->base.completion = &commit->flip_done;
1511 		new_crtc_state->event->base.completion_release = release_crtc_commit;
1512 		drm_crtc_commit_get(commit);
1513 	}
1514 
1515 	return 0;
1516 }
1517 EXPORT_SYMBOL(drm_atomic_helper_setup_commit);
1518 
1519 
1520 static struct drm_crtc_commit *preceeding_commit(struct drm_crtc *crtc)
1521 {
1522 	struct drm_crtc_commit *commit;
1523 	int i = 0;
1524 
1525 	list_for_each_entry(commit, &crtc->commit_list, commit_entry) {
1526 		/* skip the first entry, that's the current commit */
1527 		if (i == 1)
1528 			return commit;
1529 		i++;
1530 	}
1531 
1532 	return NULL;
1533 }
1534 
1535 /**
1536  * drm_atomic_helper_wait_for_dependencies - wait for required preceeding commits
1537  * @old_state: atomic state object with old state structures
1538  *
1539  * This function waits for all preceeding commits that touch the same CRTC as
1540  * @old_state to both be committed to the hardware (as signalled by
1541  * drm_atomic_helper_commit_hw_done) and executed by the hardware (as signalled
1542  * by calling drm_crtc_vblank_send_event() on the &drm_crtc_state.event).
1543  *
1544  * This is part of the atomic helper support for nonblocking commits, see
1545  * drm_atomic_helper_setup_commit() for an overview.
1546  */
1547 void drm_atomic_helper_wait_for_dependencies(struct drm_atomic_state *old_state)
1548 {
1549 	struct drm_crtc *crtc;
1550 	struct drm_crtc_state *new_crtc_state;
1551 	struct drm_crtc_commit *commit;
1552 	int i;
1553 	long ret;
1554 
1555 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1556 		lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
1557 		commit = preceeding_commit(crtc);
1558 		if (commit)
1559 			drm_crtc_commit_get(commit);
1560 		lockmgr(&crtc->commit_lock, LK_RELEASE);
1561 
1562 		if (!commit)
1563 			continue;
1564 
1565 		ret = wait_for_completion_timeout(&commit->hw_done,
1566 						  10*HZ);
1567 		if (ret == 0)
1568 			DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
1569 				  crtc->base.id, crtc->name);
1570 
1571 		/* Currently no support for overwriting flips, hence
1572 		 * stall for previous one to execute completely. */
1573 		ret = wait_for_completion_timeout(&commit->flip_done,
1574 						  10*HZ);
1575 		if (ret == 0)
1576 			DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1577 				  crtc->base.id, crtc->name);
1578 
1579 		drm_crtc_commit_put(commit);
1580 	}
1581 }
1582 EXPORT_SYMBOL(drm_atomic_helper_wait_for_dependencies);
1583 
1584 /**
1585  * drm_atomic_helper_commit_hw_done - setup possible nonblocking commit
1586  * @old_state: atomic state object with old state structures
1587  *
1588  * This function is used to signal completion of the hardware commit step. After
1589  * this step the driver is not allowed to read or change any permanent software
1590  * or hardware modeset state. The only exception is state protected by other
1591  * means than &drm_modeset_lock locks.
1592  *
1593  * Drivers should try to postpone any expensive or delayed cleanup work after
1594  * this function is called.
1595  *
1596  * This is part of the atomic helper support for nonblocking commits, see
1597  * drm_atomic_helper_setup_commit() for an overview.
1598  */
1599 void drm_atomic_helper_commit_hw_done(struct drm_atomic_state *old_state)
1600 {
1601 	struct drm_crtc *crtc;
1602 	struct drm_crtc_state *new_crtc_state;
1603 	struct drm_crtc_commit *commit;
1604 	int i;
1605 
1606 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1607 		commit = old_state->crtcs[i].commit;
1608 		if (!commit)
1609 			continue;
1610 
1611 		/* backend must have consumed any event by now */
1612 		WARN_ON(new_crtc_state->event);
1613 		lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
1614 		complete_all(&commit->hw_done);
1615 		lockmgr(&crtc->commit_lock, LK_RELEASE);
1616 	}
1617 }
1618 EXPORT_SYMBOL(drm_atomic_helper_commit_hw_done);
1619 
1620 /**
1621  * drm_atomic_helper_commit_cleanup_done - signal completion of commit
1622  * @old_state: atomic state object with old state structures
1623  *
1624  * This signals completion of the atomic update @old_state, including any
1625  * cleanup work. If used, it must be called right before calling
1626  * drm_atomic_state_put().
1627  *
1628  * This is part of the atomic helper support for nonblocking commits, see
1629  * drm_atomic_helper_setup_commit() for an overview.
1630  */
1631 void drm_atomic_helper_commit_cleanup_done(struct drm_atomic_state *old_state)
1632 {
1633 	struct drm_crtc *crtc;
1634 	struct drm_crtc_state *new_crtc_state;
1635 	struct drm_crtc_commit *commit;
1636 	int i;
1637 	long ret;
1638 
1639 	for_each_new_crtc_in_state(old_state, crtc, new_crtc_state, i) {
1640 		commit = old_state->crtcs[i].commit;
1641 		if (WARN_ON(!commit))
1642 			continue;
1643 
1644 		lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
1645 		complete_all(&commit->cleanup_done);
1646 		WARN_ON(!try_wait_for_completion(&commit->hw_done));
1647 
1648 		/* commit_list borrows our reference, need to remove before we
1649 		 * clean up our drm_atomic_state. But only after it actually
1650 		 * completed, otherwise subsequent commits won't stall properly. */
1651 		if (try_wait_for_completion(&commit->flip_done))
1652 			goto del_commit;
1653 
1654 		lockmgr(&crtc->commit_lock, LK_RELEASE);
1655 
1656 		/* We must wait for the vblank event to signal our completion
1657 		 * before releasing our reference, since the vblank work does
1658 		 * not hold a reference of its own. */
1659 		ret = wait_for_completion_timeout(&commit->flip_done,
1660 						  10*HZ);
1661 		if (ret == 0)
1662 			DRM_ERROR("[CRTC:%d:%s] flip_done timed out\n",
1663 				  crtc->base.id, crtc->name);
1664 
1665 		lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
1666 del_commit:
1667 		list_del(&commit->commit_entry);
1668 		lockmgr(&crtc->commit_lock, LK_RELEASE);
1669 	}
1670 }
1671 EXPORT_SYMBOL(drm_atomic_helper_commit_cleanup_done);
1672 
1673 /**
1674  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1675  * @dev: DRM device
1676  * @state: atomic state object with new state structures
1677  *
1678  * This function prepares plane state, specifically framebuffers, for the new
1679  * configuration, by calling &drm_plane_helper_funcs.prepare_fb. If any failure
1680  * is encountered this function will call &drm_plane_helper_funcs.cleanup_fb on
1681  * any already successfully prepared framebuffer.
1682  *
1683  * Returns:
1684  * 0 on success, negative error code on failure.
1685  */
1686 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1687 				     struct drm_atomic_state *state)
1688 {
1689 	struct drm_plane *plane;
1690 	struct drm_plane_state *new_plane_state;
1691 	int ret, i, j;
1692 
1693 	for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1694 		const struct drm_plane_helper_funcs *funcs;
1695 
1696 		funcs = plane->helper_private;
1697 
1698 		if (funcs->prepare_fb) {
1699 			ret = funcs->prepare_fb(plane, new_plane_state);
1700 			if (ret)
1701 				goto fail;
1702 		}
1703 	}
1704 
1705 	return 0;
1706 
1707 fail:
1708 	for_each_new_plane_in_state(state, plane, new_plane_state, j) {
1709 		const struct drm_plane_helper_funcs *funcs;
1710 
1711 		if (j >= i)
1712 			continue;
1713 
1714 		funcs = plane->helper_private;
1715 
1716 		if (funcs->cleanup_fb)
1717 			funcs->cleanup_fb(plane, new_plane_state);
1718 	}
1719 
1720 	return ret;
1721 }
1722 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1723 
1724 static bool plane_crtc_active(const struct drm_plane_state *state)
1725 {
1726 	return state->crtc && state->crtc->state->active;
1727 }
1728 
1729 /**
1730  * drm_atomic_helper_commit_planes - commit plane state
1731  * @dev: DRM device
1732  * @old_state: atomic state object with old state structures
1733  * @flags: flags for committing plane state
1734  *
1735  * This function commits the new plane state using the plane and atomic helper
1736  * functions for planes and crtcs. It assumes that the atomic state has already
1737  * been pushed into the relevant object state pointers, since this step can no
1738  * longer fail.
1739  *
1740  * It still requires the global state object @old_state to know which planes and
1741  * crtcs need to be updated though.
1742  *
1743  * Note that this function does all plane updates across all CRTCs in one step.
1744  * If the hardware can't support this approach look at
1745  * drm_atomic_helper_commit_planes_on_crtc() instead.
1746  *
1747  * Plane parameters can be updated by applications while the associated CRTC is
1748  * disabled. The DRM/KMS core will store the parameters in the plane state,
1749  * which will be available to the driver when the CRTC is turned on. As a result
1750  * most drivers don't need to be immediately notified of plane updates for a
1751  * disabled CRTC.
1752  *
1753  * Unless otherwise needed, drivers are advised to set the ACTIVE_ONLY flag in
1754  * @flags in order not to receive plane update notifications related to a
1755  * disabled CRTC. This avoids the need to manually ignore plane updates in
1756  * driver code when the driver and/or hardware can't or just don't need to deal
1757  * with updates on disabled CRTCs, for example when supporting runtime PM.
1758  *
1759  * Drivers may set the NO_DISABLE_AFTER_MODESET flag in @flags if the relevant
1760  * display controllers require to disable a CRTC's planes when the CRTC is
1761  * disabled. This function would skip the &drm_plane_helper_funcs.atomic_disable
1762  * call for a plane if the CRTC of the old plane state needs a modesetting
1763  * operation. Of course, the drivers need to disable the planes in their CRTC
1764  * disable callbacks since no one else would do that.
1765  *
1766  * The drm_atomic_helper_commit() default implementation doesn't set the
1767  * ACTIVE_ONLY flag to most closely match the behaviour of the legacy helpers.
1768  * This should not be copied blindly by drivers.
1769  */
1770 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1771 				     struct drm_atomic_state *old_state,
1772 				     uint32_t flags)
1773 {
1774 	struct drm_crtc *crtc;
1775 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1776 	struct drm_plane *plane;
1777 	struct drm_plane_state *old_plane_state, *new_plane_state;
1778 	int i;
1779 	bool active_only = flags & DRM_PLANE_COMMIT_ACTIVE_ONLY;
1780 	bool no_disable = flags & DRM_PLANE_COMMIT_NO_DISABLE_AFTER_MODESET;
1781 
1782 	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1783 		const struct drm_crtc_helper_funcs *funcs;
1784 
1785 		funcs = crtc->helper_private;
1786 
1787 		if (!funcs || !funcs->atomic_begin)
1788 			continue;
1789 
1790 		if (active_only && !new_crtc_state->active)
1791 			continue;
1792 
1793 		funcs->atomic_begin(crtc, old_crtc_state);
1794 	}
1795 
1796 	for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1797 		const struct drm_plane_helper_funcs *funcs;
1798 		bool disabling;
1799 
1800 		funcs = plane->helper_private;
1801 
1802 		if (!funcs)
1803 			continue;
1804 
1805 		disabling = drm_atomic_plane_disabling(old_plane_state,
1806 						       new_plane_state);
1807 
1808 		if (active_only) {
1809 			/*
1810 			 * Skip planes related to inactive CRTCs. If the plane
1811 			 * is enabled use the state of the current CRTC. If the
1812 			 * plane is being disabled use the state of the old
1813 			 * CRTC to avoid skipping planes being disabled on an
1814 			 * active CRTC.
1815 			 */
1816 			if (!disabling && !plane_crtc_active(new_plane_state))
1817 				continue;
1818 			if (disabling && !plane_crtc_active(old_plane_state))
1819 				continue;
1820 		}
1821 
1822 		/*
1823 		 * Special-case disabling the plane if drivers support it.
1824 		 */
1825 		if (disabling && funcs->atomic_disable) {
1826 			struct drm_crtc_state *crtc_state;
1827 
1828 			crtc_state = old_plane_state->crtc->state;
1829 
1830 			if (drm_atomic_crtc_needs_modeset(crtc_state) &&
1831 			    no_disable)
1832 				continue;
1833 
1834 			funcs->atomic_disable(plane, old_plane_state);
1835 		} else if (new_plane_state->crtc || disabling) {
1836 			funcs->atomic_update(plane, old_plane_state);
1837 		}
1838 	}
1839 
1840 	for_each_oldnew_crtc_in_state(old_state, crtc, old_crtc_state, new_crtc_state, i) {
1841 		const struct drm_crtc_helper_funcs *funcs;
1842 
1843 		funcs = crtc->helper_private;
1844 
1845 		if (!funcs || !funcs->atomic_flush)
1846 			continue;
1847 
1848 		if (active_only && !new_crtc_state->active)
1849 			continue;
1850 
1851 		funcs->atomic_flush(crtc, old_crtc_state);
1852 	}
1853 }
1854 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1855 
1856 /**
1857  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1858  * @old_crtc_state: atomic state object with the old crtc state
1859  *
1860  * This function commits the new plane state using the plane and atomic helper
1861  * functions for planes on the specific crtc. It assumes that the atomic state
1862  * has already been pushed into the relevant object state pointers, since this
1863  * step can no longer fail.
1864  *
1865  * This function is useful when plane updates should be done crtc-by-crtc
1866  * instead of one global step like drm_atomic_helper_commit_planes() does.
1867  *
1868  * This function can only be savely used when planes are not allowed to move
1869  * between different CRTCs because this function doesn't handle inter-CRTC
1870  * depencies. Callers need to ensure that either no such depencies exist,
1871  * resolve them through ordering of commit calls or through some other means.
1872  */
1873 void
1874 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1875 {
1876 	const struct drm_crtc_helper_funcs *crtc_funcs;
1877 	struct drm_crtc *crtc = old_crtc_state->crtc;
1878 	struct drm_atomic_state *old_state = old_crtc_state->state;
1879 	struct drm_plane *plane;
1880 	unsigned plane_mask;
1881 
1882 	plane_mask = old_crtc_state->plane_mask;
1883 	plane_mask |= crtc->state->plane_mask;
1884 
1885 	crtc_funcs = crtc->helper_private;
1886 	if (crtc_funcs && crtc_funcs->atomic_begin)
1887 		crtc_funcs->atomic_begin(crtc, old_crtc_state);
1888 
1889 	drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1890 		struct drm_plane_state *old_plane_state =
1891 			drm_atomic_get_old_plane_state(old_state, plane);
1892 		const struct drm_plane_helper_funcs *plane_funcs;
1893 
1894 		plane_funcs = plane->helper_private;
1895 
1896 		if (!old_plane_state || !plane_funcs)
1897 			continue;
1898 
1899 		WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1900 
1901 		if (drm_atomic_plane_disabling(old_plane_state, plane->state) &&
1902 		    plane_funcs->atomic_disable)
1903 			plane_funcs->atomic_disable(plane, old_plane_state);
1904 		else if (plane->state->crtc ||
1905 			 drm_atomic_plane_disabling(old_plane_state, plane->state))
1906 			plane_funcs->atomic_update(plane, old_plane_state);
1907 	}
1908 
1909 	if (crtc_funcs && crtc_funcs->atomic_flush)
1910 		crtc_funcs->atomic_flush(crtc, old_crtc_state);
1911 }
1912 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1913 
1914 /**
1915  * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1916  * @old_crtc_state: atomic state object with the old CRTC state
1917  * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1918  *
1919  * Disables all planes associated with the given CRTC. This can be
1920  * used for instance in the CRTC helper atomic_disable callback to disable
1921  * all planes.
1922  *
1923  * If the atomic-parameter is set the function calls the CRTC's
1924  * atomic_begin hook before and atomic_flush hook after disabling the
1925  * planes.
1926  *
1927  * It is a bug to call this function without having implemented the
1928  * &drm_plane_helper_funcs.atomic_disable plane hook.
1929  */
1930 void
1931 drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc_state *old_crtc_state,
1932 					 bool atomic)
1933 {
1934 	struct drm_crtc *crtc = old_crtc_state->crtc;
1935 	const struct drm_crtc_helper_funcs *crtc_funcs =
1936 		crtc->helper_private;
1937 	struct drm_plane *plane;
1938 
1939 	if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1940 		crtc_funcs->atomic_begin(crtc, NULL);
1941 
1942 	drm_atomic_crtc_state_for_each_plane(plane, old_crtc_state) {
1943 		const struct drm_plane_helper_funcs *plane_funcs =
1944 			plane->helper_private;
1945 
1946 		if (!plane_funcs)
1947 			continue;
1948 
1949 		WARN_ON(!plane_funcs->atomic_disable);
1950 		if (plane_funcs->atomic_disable)
1951 			plane_funcs->atomic_disable(plane, NULL);
1952 	}
1953 
1954 	if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1955 		crtc_funcs->atomic_flush(crtc, NULL);
1956 }
1957 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1958 
1959 /**
1960  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1961  * @dev: DRM device
1962  * @old_state: atomic state object with old state structures
1963  *
1964  * This function cleans up plane state, specifically framebuffers, from the old
1965  * configuration. Hence the old configuration must be perserved in @old_state to
1966  * be able to call this function.
1967  *
1968  * This function must also be called on the new state when the atomic update
1969  * fails at any point after calling drm_atomic_helper_prepare_planes().
1970  */
1971 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1972 				      struct drm_atomic_state *old_state)
1973 {
1974 	struct drm_plane *plane;
1975 	struct drm_plane_state *old_plane_state, *new_plane_state;
1976 	int i;
1977 
1978 	for_each_oldnew_plane_in_state(old_state, plane, old_plane_state, new_plane_state, i) {
1979 		const struct drm_plane_helper_funcs *funcs;
1980 		struct drm_plane_state *plane_state;
1981 
1982 		/*
1983 		 * This might be called before swapping when commit is aborted,
1984 		 * in which case we have to cleanup the new state.
1985 		 */
1986 		if (old_plane_state == plane->state)
1987 			plane_state = new_plane_state;
1988 		else
1989 			plane_state = old_plane_state;
1990 
1991 		funcs = plane->helper_private;
1992 
1993 		if (funcs->cleanup_fb)
1994 			funcs->cleanup_fb(plane, plane_state);
1995 	}
1996 }
1997 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1998 
1999 /**
2000  * drm_atomic_helper_swap_state - store atomic state into current sw state
2001  * @state: atomic state
2002  * @stall: stall for proceeding commits
2003  *
2004  * This function stores the atomic state into the current state pointers in all
2005  * driver objects. It should be called after all failing steps have been done
2006  * and succeeded, but before the actual hardware state is committed.
2007  *
2008  * For cleanup and error recovery the current state for all changed objects will
2009  * be swaped into @state.
2010  *
2011  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
2012  *
2013  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
2014  *
2015  * 2. Do any other steps that might fail.
2016  *
2017  * 3. Put the staged state into the current state pointers with this function.
2018  *
2019  * 4. Actually commit the hardware state.
2020  *
2021  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
2022  * contains the old state. Also do any other cleanup required with that state.
2023  *
2024  * @stall must be set when nonblocking commits for this driver directly access
2025  * the &drm_plane.state, &drm_crtc.state or &drm_connector.state pointer. With
2026  * the current atomic helpers this is almost always the case, since the helpers
2027  * don't pass the right state structures to the callbacks.
2028  */
2029 void drm_atomic_helper_swap_state(struct drm_atomic_state *state,
2030 				  bool stall)
2031 {
2032 	int i;
2033 	long ret;
2034 	struct drm_connector *connector;
2035 	struct drm_connector_state *old_conn_state, *new_conn_state;
2036 	struct drm_crtc *crtc;
2037 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
2038 	struct drm_plane *plane;
2039 	struct drm_plane_state *old_plane_state, *new_plane_state;
2040 	struct drm_crtc_commit *commit;
2041 
2042 	if (stall) {
2043 		for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2044 			lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
2045 			commit = list_first_entry_or_null(&crtc->commit_list,
2046 					struct drm_crtc_commit, commit_entry);
2047 			if (commit)
2048 				drm_crtc_commit_get(commit);
2049 			lockmgr(&crtc->commit_lock, LK_RELEASE);
2050 
2051 			if (!commit)
2052 				continue;
2053 
2054 			ret = wait_for_completion_timeout(&commit->hw_done,
2055 							  10*HZ);
2056 			if (ret == 0)
2057 				DRM_ERROR("[CRTC:%d:%s] hw_done timed out\n",
2058 					  crtc->base.id, crtc->name);
2059 			drm_crtc_commit_put(commit);
2060 		}
2061 	}
2062 
2063 	for_each_oldnew_connector_in_state(state, connector, old_conn_state, new_conn_state, i) {
2064 		WARN_ON(connector->state != old_conn_state);
2065 
2066 		old_conn_state->state = state;
2067 		new_conn_state->state = NULL;
2068 
2069 		state->connectors[i].state = old_conn_state;
2070 		connector->state = new_conn_state;
2071 	}
2072 
2073 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
2074 		WARN_ON(crtc->state != old_crtc_state);
2075 
2076 		old_crtc_state->state = state;
2077 		new_crtc_state->state = NULL;
2078 
2079 		state->crtcs[i].state = old_crtc_state;
2080 		crtc->state = new_crtc_state;
2081 
2082 		if (state->crtcs[i].commit) {
2083 			lockmgr(&crtc->commit_lock, LK_EXCLUSIVE);
2084 			list_add(&state->crtcs[i].commit->commit_entry,
2085 				 &crtc->commit_list);
2086 			lockmgr(&crtc->commit_lock, LK_RELEASE);
2087 
2088 			state->crtcs[i].commit->event = NULL;
2089 		}
2090 	}
2091 
2092 	for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
2093 		WARN_ON(plane->state != old_plane_state);
2094 
2095 		old_plane_state->state = state;
2096 		new_plane_state->state = NULL;
2097 
2098 		state->planes[i].state = old_plane_state;
2099 		plane->state = new_plane_state;
2100 	}
2101 }
2102 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
2103 
2104 /**
2105  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
2106  * @plane: plane object to update
2107  * @crtc: owning CRTC of owning plane
2108  * @fb: framebuffer to flip onto plane
2109  * @crtc_x: x offset of primary plane on crtc
2110  * @crtc_y: y offset of primary plane on crtc
2111  * @crtc_w: width of primary plane rectangle on crtc
2112  * @crtc_h: height of primary plane rectangle on crtc
2113  * @src_x: x offset of @fb for panning
2114  * @src_y: y offset of @fb for panning
2115  * @src_w: width of source rectangle in @fb
2116  * @src_h: height of source rectangle in @fb
2117  * @ctx: lock acquire context
2118  *
2119  * Provides a default plane update handler using the atomic driver interface.
2120  *
2121  * RETURNS:
2122  * Zero on success, error code on failure
2123  */
2124 int drm_atomic_helper_update_plane(struct drm_plane *plane,
2125 				   struct drm_crtc *crtc,
2126 				   struct drm_framebuffer *fb,
2127 				   int crtc_x, int crtc_y,
2128 				   unsigned int crtc_w, unsigned int crtc_h,
2129 				   uint32_t src_x, uint32_t src_y,
2130 				   uint32_t src_w, uint32_t src_h,
2131 				   struct drm_modeset_acquire_ctx *ctx)
2132 {
2133 	struct drm_atomic_state *state;
2134 	struct drm_plane_state *plane_state;
2135 	int ret = 0;
2136 
2137 	state = drm_atomic_state_alloc(plane->dev);
2138 	if (!state)
2139 		return -ENOMEM;
2140 
2141 	state->acquire_ctx = ctx;
2142 	plane_state = drm_atomic_get_plane_state(state, plane);
2143 	if (IS_ERR(plane_state)) {
2144 		ret = PTR_ERR(plane_state);
2145 		goto fail;
2146 	}
2147 
2148 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2149 	if (ret != 0)
2150 		goto fail;
2151 	drm_atomic_set_fb_for_plane(plane_state, fb);
2152 	plane_state->crtc_x = crtc_x;
2153 	plane_state->crtc_y = crtc_y;
2154 	plane_state->crtc_w = crtc_w;
2155 	plane_state->crtc_h = crtc_h;
2156 	plane_state->src_x = src_x;
2157 	plane_state->src_y = src_y;
2158 	plane_state->src_w = src_w;
2159 	plane_state->src_h = src_h;
2160 
2161 	if (plane == crtc->cursor)
2162 		state->legacy_cursor_update = true;
2163 
2164 	ret = drm_atomic_commit(state);
2165 fail:
2166 	drm_atomic_state_put(state);
2167 	return ret;
2168 }
2169 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
2170 
2171 /**
2172  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
2173  * @plane: plane to disable
2174  * @ctx: lock acquire context
2175  *
2176  * Provides a default plane disable handler using the atomic driver interface.
2177  *
2178  * RETURNS:
2179  * Zero on success, error code on failure
2180  */
2181 int drm_atomic_helper_disable_plane(struct drm_plane *plane,
2182 				    struct drm_modeset_acquire_ctx *ctx)
2183 {
2184 	struct drm_atomic_state *state;
2185 	struct drm_plane_state *plane_state;
2186 	int ret = 0;
2187 
2188 	state = drm_atomic_state_alloc(plane->dev);
2189 	if (!state)
2190 		return -ENOMEM;
2191 
2192 	state->acquire_ctx = ctx;
2193 	plane_state = drm_atomic_get_plane_state(state, plane);
2194 	if (IS_ERR(plane_state)) {
2195 		ret = PTR_ERR(plane_state);
2196 		goto fail;
2197 	}
2198 
2199 	if (plane_state->crtc && (plane == plane->crtc->cursor))
2200 		plane_state->state->legacy_cursor_update = true;
2201 
2202 	ret = __drm_atomic_helper_disable_plane(plane, plane_state);
2203 	if (ret != 0)
2204 		goto fail;
2205 
2206 	ret = drm_atomic_commit(state);
2207 fail:
2208 	drm_atomic_state_put(state);
2209 	return ret;
2210 }
2211 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
2212 
2213 /* just used from fb-helper and atomic-helper: */
2214 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
2215 		struct drm_plane_state *plane_state)
2216 {
2217 	int ret;
2218 
2219 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2220 	if (ret != 0)
2221 		return ret;
2222 
2223 	drm_atomic_set_fb_for_plane(plane_state, NULL);
2224 	plane_state->crtc_x = 0;
2225 	plane_state->crtc_y = 0;
2226 	plane_state->crtc_w = 0;
2227 	plane_state->crtc_h = 0;
2228 	plane_state->src_x = 0;
2229 	plane_state->src_y = 0;
2230 	plane_state->src_w = 0;
2231 	plane_state->src_h = 0;
2232 
2233 	return 0;
2234 }
2235 
2236 static int update_output_state(struct drm_atomic_state *state,
2237 			       struct drm_mode_set *set)
2238 {
2239 	struct drm_device *dev = set->crtc->dev;
2240 	struct drm_crtc *crtc;
2241 	struct drm_crtc_state *new_crtc_state;
2242 	struct drm_connector *connector;
2243 	struct drm_connector_state *new_conn_state;
2244 	int ret, i;
2245 
2246 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
2247 			       state->acquire_ctx);
2248 	if (ret)
2249 		return ret;
2250 
2251 	/* First disable all connectors on the target crtc. */
2252 	ret = drm_atomic_add_affected_connectors(state, set->crtc);
2253 	if (ret)
2254 		return ret;
2255 
2256 	for_each_new_connector_in_state(state, connector, new_conn_state, i) {
2257 		if (new_conn_state->crtc == set->crtc) {
2258 			ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2259 								NULL);
2260 			if (ret)
2261 				return ret;
2262 
2263 			/* Make sure legacy setCrtc always re-trains */
2264 			new_conn_state->link_status = DRM_LINK_STATUS_GOOD;
2265 		}
2266 	}
2267 
2268 	/* Then set all connectors from set->connectors on the target crtc */
2269 	for (i = 0; i < set->num_connectors; i++) {
2270 		new_conn_state = drm_atomic_get_connector_state(state,
2271 							    set->connectors[i]);
2272 		if (IS_ERR(new_conn_state))
2273 			return PTR_ERR(new_conn_state);
2274 
2275 		ret = drm_atomic_set_crtc_for_connector(new_conn_state,
2276 							set->crtc);
2277 		if (ret)
2278 			return ret;
2279 	}
2280 
2281 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
2282 		/* Don't update ->enable for the CRTC in the set_config request,
2283 		 * since a mismatch would indicate a bug in the upper layers.
2284 		 * The actual modeset code later on will catch any
2285 		 * inconsistencies here. */
2286 		if (crtc == set->crtc)
2287 			continue;
2288 
2289 		if (!new_crtc_state->connector_mask) {
2290 			ret = drm_atomic_set_mode_prop_for_crtc(new_crtc_state,
2291 								NULL);
2292 			if (ret < 0)
2293 				return ret;
2294 
2295 			new_crtc_state->active = false;
2296 		}
2297 	}
2298 
2299 	return 0;
2300 }
2301 
2302 /**
2303  * drm_atomic_helper_set_config - set a new config from userspace
2304  * @set: mode set configuration
2305  * @ctx: lock acquisition context
2306  *
2307  * Provides a default crtc set_config handler using the atomic driver interface.
2308  *
2309  * NOTE: For backwards compatibility with old userspace this automatically
2310  * resets the "link-status" property to GOOD, to force any link
2311  * re-training. The SETCRTC ioctl does not define whether an update does
2312  * need a full modeset or just a plane update, hence we're allowed to do
2313  * that. See also drm_mode_connector_set_link_status_property().
2314  *
2315  * Returns:
2316  * Returns 0 on success, negative errno numbers on failure.
2317  */
2318 int drm_atomic_helper_set_config(struct drm_mode_set *set,
2319 				 struct drm_modeset_acquire_ctx *ctx)
2320 {
2321 	struct drm_atomic_state *state;
2322 	struct drm_crtc *crtc = set->crtc;
2323 	int ret = 0;
2324 
2325 	state = drm_atomic_state_alloc(crtc->dev);
2326 	if (!state)
2327 		return -ENOMEM;
2328 
2329 	state->acquire_ctx = ctx;
2330 	ret = __drm_atomic_helper_set_config(set, state);
2331 	if (ret != 0)
2332 		goto fail;
2333 
2334 	ret = handle_conflicting_encoders(state, true);
2335 	if (ret)
2336 		return ret;
2337 
2338 	ret = drm_atomic_commit(state);
2339 
2340 fail:
2341 	drm_atomic_state_put(state);
2342 	return ret;
2343 }
2344 EXPORT_SYMBOL(drm_atomic_helper_set_config);
2345 
2346 /* just used from fb-helper and atomic-helper: */
2347 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
2348 		struct drm_atomic_state *state)
2349 {
2350 	struct drm_crtc_state *crtc_state;
2351 	struct drm_plane_state *primary_state;
2352 	struct drm_crtc *crtc = set->crtc;
2353 	int hdisplay, vdisplay;
2354 	int ret;
2355 
2356 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2357 	if (IS_ERR(crtc_state))
2358 		return PTR_ERR(crtc_state);
2359 
2360 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
2361 	if (IS_ERR(primary_state))
2362 		return PTR_ERR(primary_state);
2363 
2364 	if (!set->mode) {
2365 		WARN_ON(set->fb);
2366 		WARN_ON(set->num_connectors);
2367 
2368 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
2369 		if (ret != 0)
2370 			return ret;
2371 
2372 		crtc_state->active = false;
2373 
2374 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
2375 		if (ret != 0)
2376 			return ret;
2377 
2378 		drm_atomic_set_fb_for_plane(primary_state, NULL);
2379 
2380 		goto commit;
2381 	}
2382 
2383 	WARN_ON(!set->fb);
2384 	WARN_ON(!set->num_connectors);
2385 
2386 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
2387 	if (ret != 0)
2388 		return ret;
2389 
2390 	crtc_state->active = true;
2391 
2392 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
2393 	if (ret != 0)
2394 		return ret;
2395 
2396 	drm_mode_get_hv_timing(set->mode, &hdisplay, &vdisplay);
2397 
2398 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
2399 	primary_state->crtc_x = 0;
2400 	primary_state->crtc_y = 0;
2401 	primary_state->crtc_w = hdisplay;
2402 	primary_state->crtc_h = vdisplay;
2403 	primary_state->src_x = set->x << 16;
2404 	primary_state->src_y = set->y << 16;
2405 	if (drm_rotation_90_or_270(primary_state->rotation)) {
2406 		primary_state->src_w = vdisplay << 16;
2407 		primary_state->src_h = hdisplay << 16;
2408 	} else {
2409 		primary_state->src_w = hdisplay << 16;
2410 		primary_state->src_h = vdisplay << 16;
2411 	}
2412 
2413 commit:
2414 	ret = update_output_state(state, set);
2415 	if (ret)
2416 		return ret;
2417 
2418 	return 0;
2419 }
2420 
2421 /**
2422  * drm_atomic_helper_disable_all - disable all currently active outputs
2423  * @dev: DRM device
2424  * @ctx: lock acquisition context
2425  *
2426  * Loops through all connectors, finding those that aren't turned off and then
2427  * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
2428  * that they are connected to.
2429  *
2430  * This is used for example in suspend/resume to disable all currently active
2431  * functions when suspending.
2432  *
2433  * Note that if callers haven't already acquired all modeset locks this might
2434  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2435  *
2436  * Returns:
2437  * 0 on success or a negative error code on failure.
2438  *
2439  * See also:
2440  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2441  */
2442 int drm_atomic_helper_disable_all(struct drm_device *dev,
2443 				  struct drm_modeset_acquire_ctx *ctx)
2444 {
2445 	struct drm_atomic_state *state;
2446 	struct drm_connector_state *conn_state;
2447 	struct drm_connector *conn;
2448 	struct drm_plane_state *plane_state;
2449 	struct drm_plane *plane;
2450 	struct drm_crtc_state *crtc_state;
2451 	struct drm_crtc *crtc;
2452 	int ret, i;
2453 
2454 	state = drm_atomic_state_alloc(dev);
2455 	if (!state)
2456 		return -ENOMEM;
2457 
2458 	state->acquire_ctx = ctx;
2459 
2460 	drm_for_each_crtc(crtc, dev) {
2461 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
2462 		if (IS_ERR(crtc_state)) {
2463 			ret = PTR_ERR(crtc_state);
2464 			goto free;
2465 		}
2466 
2467 		crtc_state->active = false;
2468 
2469 		ret = drm_atomic_set_mode_prop_for_crtc(crtc_state, NULL);
2470 		if (ret < 0)
2471 			goto free;
2472 
2473 		ret = drm_atomic_add_affected_planes(state, crtc);
2474 		if (ret < 0)
2475 			goto free;
2476 
2477 		ret = drm_atomic_add_affected_connectors(state, crtc);
2478 		if (ret < 0)
2479 			goto free;
2480 	}
2481 
2482 	for_each_connector_in_state(state, conn, conn_state, i) {
2483 		ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
2484 		if (ret < 0)
2485 			goto free;
2486 	}
2487 
2488 	for_each_plane_in_state(state, plane, plane_state, i) {
2489 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
2490 		if (ret < 0)
2491 			goto free;
2492 
2493 		drm_atomic_set_fb_for_plane(plane_state, NULL);
2494 	}
2495 
2496 	ret = drm_atomic_commit(state);
2497 free:
2498 	drm_atomic_state_put(state);
2499 	return ret;
2500 }
2501 
2502 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
2503 
2504 /**
2505  * drm_atomic_helper_suspend - subsystem-level suspend helper
2506  * @dev: DRM device
2507  *
2508  * Duplicates the current atomic state, disables all active outputs and then
2509  * returns a pointer to the original atomic state to the caller. Drivers can
2510  * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
2511  * restore the output configuration that was active at the time the system
2512  * entered suspend.
2513  *
2514  * Note that it is potentially unsafe to use this. The atomic state object
2515  * returned by this function is assumed to be persistent. Drivers must ensure
2516  * that this holds true. Before calling this function, drivers must make sure
2517  * to suspend fbdev emulation so that nothing can be using the device.
2518  *
2519  * Returns:
2520  * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
2521  * encoded error code on failure. Drivers should store the returned atomic
2522  * state object and pass it to the drm_atomic_helper_resume() helper upon
2523  * resume.
2524  *
2525  * See also:
2526  * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
2527  * drm_atomic_helper_resume(), drm_atomic_helper_commit_duplicated_state()
2528  */
2529 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
2530 {
2531 	struct drm_modeset_acquire_ctx ctx;
2532 	struct drm_atomic_state *state;
2533 	int err;
2534 
2535 	drm_modeset_acquire_init(&ctx, 0);
2536 
2537 retry:
2538 	err = drm_modeset_lock_all_ctx(dev, &ctx);
2539 	if (err < 0) {
2540 		state = ERR_PTR(err);
2541 		goto unlock;
2542 	}
2543 
2544 	state = drm_atomic_helper_duplicate_state(dev, &ctx);
2545 	if (IS_ERR(state))
2546 		goto unlock;
2547 
2548 	err = drm_atomic_helper_disable_all(dev, &ctx);
2549 	if (err < 0) {
2550 		drm_atomic_state_put(state);
2551 		state = ERR_PTR(err);
2552 		goto unlock;
2553 	}
2554 
2555 unlock:
2556 	if (PTR_ERR(state) == -EDEADLK) {
2557 		drm_modeset_backoff(&ctx);
2558 		goto retry;
2559 	}
2560 
2561 	drm_modeset_drop_locks(&ctx);
2562 	drm_modeset_acquire_fini(&ctx);
2563 	return state;
2564 }
2565 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2566 
2567 /**
2568  * drm_atomic_helper_commit_duplicated_state - commit duplicated state
2569  * @state: duplicated atomic state to commit
2570  * @ctx: pointer to acquire_ctx to use for commit.
2571  *
2572  * The state returned by drm_atomic_helper_duplicate_state() and
2573  * drm_atomic_helper_suspend() is partially invalid, and needs to
2574  * be fixed up before commit.
2575  *
2576  * Returns:
2577  * 0 on success or a negative error code on failure.
2578  *
2579  * See also:
2580  * drm_atomic_helper_suspend()
2581  */
2582 int drm_atomic_helper_commit_duplicated_state(struct drm_atomic_state *state,
2583 					      struct drm_modeset_acquire_ctx *ctx)
2584 {
2585 	int i;
2586 	struct drm_plane *plane;
2587 	struct drm_plane_state *new_plane_state;
2588 	struct drm_connector *connector;
2589 	struct drm_connector_state *new_conn_state;
2590 	struct drm_crtc *crtc;
2591 	struct drm_crtc_state *new_crtc_state;
2592 
2593 	state->acquire_ctx = ctx;
2594 
2595 	for_each_new_plane_in_state(state, plane, new_plane_state, i)
2596 		state->planes[i].old_state = plane->state;
2597 
2598 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
2599 		state->crtcs[i].old_state = crtc->state;
2600 
2601 	for_each_new_connector_in_state(state, connector, new_conn_state, i)
2602 		state->connectors[i].old_state = connector->state;
2603 
2604 	return drm_atomic_commit(state);
2605 }
2606 EXPORT_SYMBOL(drm_atomic_helper_commit_duplicated_state);
2607 
2608 /**
2609  * drm_atomic_helper_resume - subsystem-level resume helper
2610  * @dev: DRM device
2611  * @state: atomic state to resume to
2612  *
2613  * Calls drm_mode_config_reset() to synchronize hardware and software states,
2614  * grabs all modeset locks and commits the atomic state object. This can be
2615  * used in conjunction with the drm_atomic_helper_suspend() helper to
2616  * implement suspend/resume for drivers that support atomic mode-setting.
2617  *
2618  * Returns:
2619  * 0 on success or a negative error code on failure.
2620  *
2621  * See also:
2622  * drm_atomic_helper_suspend()
2623  */
2624 int drm_atomic_helper_resume(struct drm_device *dev,
2625 			     struct drm_atomic_state *state)
2626 {
2627 	struct drm_modeset_acquire_ctx ctx;
2628 	int err;
2629 
2630 	drm_mode_config_reset(dev);
2631 
2632 	drm_modeset_acquire_init(&ctx, 0);
2633 	while (1) {
2634 		err = drm_modeset_lock_all_ctx(dev, &ctx);
2635 		if (err)
2636 			goto out;
2637 
2638 		err = drm_atomic_helper_commit_duplicated_state(state, &ctx);
2639 out:
2640 		if (err != -EDEADLK)
2641 			break;
2642 
2643 		drm_modeset_backoff(&ctx);
2644 	}
2645 
2646 	drm_modeset_drop_locks(&ctx);
2647 	drm_modeset_acquire_fini(&ctx);
2648 
2649 	return err;
2650 }
2651 EXPORT_SYMBOL(drm_atomic_helper_resume);
2652 
2653 /**
2654  * drm_atomic_helper_crtc_set_property - helper for crtc properties
2655  * @crtc: DRM crtc
2656  * @property: DRM property
2657  * @val: value of property
2658  *
2659  * Provides a default crtc set_property handler using the atomic driver
2660  * interface.
2661  *
2662  * RETURNS:
2663  * Zero on success, error code on failure
2664  */
2665 int
2666 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2667 				    struct drm_property *property,
2668 				    uint64_t val)
2669 {
2670 	struct drm_atomic_state *state;
2671 	struct drm_crtc_state *crtc_state;
2672 	int ret = 0;
2673 
2674 	state = drm_atomic_state_alloc(crtc->dev);
2675 	if (!state)
2676 		return -ENOMEM;
2677 
2678 	/* ->set_property is always called with all locks held. */
2679 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2680 retry:
2681 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2682 	if (IS_ERR(crtc_state)) {
2683 		ret = PTR_ERR(crtc_state);
2684 		goto fail;
2685 	}
2686 
2687 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2688 			property, val);
2689 	if (ret)
2690 		goto fail;
2691 
2692 	ret = drm_atomic_commit(state);
2693 fail:
2694 	if (ret == -EDEADLK)
2695 		goto backoff;
2696 
2697 	drm_atomic_state_put(state);
2698 	return ret;
2699 
2700 backoff:
2701 	drm_atomic_state_clear(state);
2702 	drm_atomic_legacy_backoff(state);
2703 
2704 	goto retry;
2705 }
2706 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2707 
2708 /**
2709  * drm_atomic_helper_plane_set_property - helper for plane properties
2710  * @plane: DRM plane
2711  * @property: DRM property
2712  * @val: value of property
2713  *
2714  * Provides a default plane set_property handler using the atomic driver
2715  * interface.
2716  *
2717  * RETURNS:
2718  * Zero on success, error code on failure
2719  */
2720 int
2721 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2722 				    struct drm_property *property,
2723 				    uint64_t val)
2724 {
2725 	struct drm_atomic_state *state;
2726 	struct drm_plane_state *plane_state;
2727 	int ret = 0;
2728 
2729 	state = drm_atomic_state_alloc(plane->dev);
2730 	if (!state)
2731 		return -ENOMEM;
2732 
2733 	/* ->set_property is always called with all locks held. */
2734 	state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2735 retry:
2736 	plane_state = drm_atomic_get_plane_state(state, plane);
2737 	if (IS_ERR(plane_state)) {
2738 		ret = PTR_ERR(plane_state);
2739 		goto fail;
2740 	}
2741 
2742 	ret = drm_atomic_plane_set_property(plane, plane_state,
2743 			property, val);
2744 	if (ret)
2745 		goto fail;
2746 
2747 	ret = drm_atomic_commit(state);
2748 fail:
2749 	if (ret == -EDEADLK)
2750 		goto backoff;
2751 
2752 	drm_atomic_state_put(state);
2753 	return ret;
2754 
2755 backoff:
2756 	drm_atomic_state_clear(state);
2757 	drm_atomic_legacy_backoff(state);
2758 
2759 	goto retry;
2760 }
2761 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2762 
2763 /**
2764  * drm_atomic_helper_connector_set_property - helper for connector properties
2765  * @connector: DRM connector
2766  * @property: DRM property
2767  * @val: value of property
2768  *
2769  * Provides a default connector set_property handler using the atomic driver
2770  * interface.
2771  *
2772  * RETURNS:
2773  * Zero on success, error code on failure
2774  */
2775 int
2776 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2777 				    struct drm_property *property,
2778 				    uint64_t val)
2779 {
2780 	struct drm_atomic_state *state;
2781 	struct drm_connector_state *connector_state;
2782 	int ret = 0;
2783 
2784 	state = drm_atomic_state_alloc(connector->dev);
2785 	if (!state)
2786 		return -ENOMEM;
2787 
2788 	/* ->set_property is always called with all locks held. */
2789 	state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2790 retry:
2791 	connector_state = drm_atomic_get_connector_state(state, connector);
2792 	if (IS_ERR(connector_state)) {
2793 		ret = PTR_ERR(connector_state);
2794 		goto fail;
2795 	}
2796 
2797 	ret = drm_atomic_connector_set_property(connector, connector_state,
2798 			property, val);
2799 	if (ret)
2800 		goto fail;
2801 
2802 	ret = drm_atomic_commit(state);
2803 fail:
2804 	if (ret == -EDEADLK)
2805 		goto backoff;
2806 
2807 	drm_atomic_state_put(state);
2808 	return ret;
2809 
2810 backoff:
2811 	drm_atomic_state_clear(state);
2812 	drm_atomic_legacy_backoff(state);
2813 
2814 	goto retry;
2815 }
2816 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2817 
2818 static int page_flip_common(
2819 				struct drm_atomic_state *state,
2820 				struct drm_crtc *crtc,
2821 				struct drm_framebuffer *fb,
2822 				struct drm_pending_vblank_event *event,
2823 				uint32_t flags)
2824 {
2825 	struct drm_plane *plane = crtc->primary;
2826 	struct drm_plane_state *plane_state;
2827 	struct drm_crtc_state *crtc_state;
2828 	int ret = 0;
2829 
2830 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2831 	if (IS_ERR(crtc_state))
2832 		return PTR_ERR(crtc_state);
2833 
2834 	crtc_state->event = event;
2835 	crtc_state->pageflip_flags = flags;
2836 
2837 	plane_state = drm_atomic_get_plane_state(state, plane);
2838 	if (IS_ERR(plane_state))
2839 		return PTR_ERR(plane_state);
2840 
2841 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2842 	if (ret != 0)
2843 		return ret;
2844 	drm_atomic_set_fb_for_plane(plane_state, fb);
2845 
2846 	/* Make sure we don't accidentally do a full modeset. */
2847 	state->allow_modeset = false;
2848 	if (!crtc_state->active) {
2849 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled, rejecting legacy flip\n",
2850 				 crtc->base.id, crtc->name);
2851 		return -EINVAL;
2852 	}
2853 
2854 	return ret;
2855 }
2856 
2857 /**
2858  * drm_atomic_helper_page_flip - execute a legacy page flip
2859  * @crtc: DRM crtc
2860  * @fb: DRM framebuffer
2861  * @event: optional DRM event to signal upon completion
2862  * @flags: flip flags for non-vblank sync'ed updates
2863  * @ctx: lock acquisition context
2864  *
2865  * Provides a default &drm_crtc_funcs.page_flip implementation
2866  * using the atomic driver interface.
2867  *
2868  * Returns:
2869  * Returns 0 on success, negative errno numbers on failure.
2870  *
2871  * See also:
2872  * drm_atomic_helper_page_flip_target()
2873  */
2874 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2875 				struct drm_framebuffer *fb,
2876 				struct drm_pending_vblank_event *event,
2877 				uint32_t flags,
2878 				struct drm_modeset_acquire_ctx *ctx)
2879 {
2880 	struct drm_plane *plane = crtc->primary;
2881 	struct drm_atomic_state *state;
2882 	int ret = 0;
2883 
2884 	state = drm_atomic_state_alloc(plane->dev);
2885 	if (!state)
2886 		return -ENOMEM;
2887 
2888 	state->acquire_ctx = ctx;
2889 
2890 	ret = page_flip_common(state, crtc, fb, event, flags);
2891 	if (ret != 0)
2892 		goto fail;
2893 
2894 	ret = drm_atomic_nonblocking_commit(state);
2895 fail:
2896 	drm_atomic_state_put(state);
2897 	return ret;
2898 }
2899 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2900 
2901 /**
2902  * drm_atomic_helper_page_flip_target - do page flip on target vblank period.
2903  * @crtc: DRM crtc
2904  * @fb: DRM framebuffer
2905  * @event: optional DRM event to signal upon completion
2906  * @flags: flip flags for non-vblank sync'ed updates
2907  * @target: specifying the target vblank period when the flip to take effect
2908  * @ctx: lock acquisition context
2909  *
2910  * Provides a default &drm_crtc_funcs.page_flip_target implementation.
2911  * Similar to drm_atomic_helper_page_flip() with extra parameter to specify
2912  * target vblank period to flip.
2913  *
2914  * Returns:
2915  * Returns 0 on success, negative errno numbers on failure.
2916  */
2917 int drm_atomic_helper_page_flip_target(
2918 				struct drm_crtc *crtc,
2919 				struct drm_framebuffer *fb,
2920 				struct drm_pending_vblank_event *event,
2921 				uint32_t flags,
2922 				uint32_t target,
2923 				struct drm_modeset_acquire_ctx *ctx)
2924 {
2925 	struct drm_plane *plane = crtc->primary;
2926 	struct drm_atomic_state *state;
2927 	struct drm_crtc_state *crtc_state;
2928 	int ret = 0;
2929 
2930 	state = drm_atomic_state_alloc(plane->dev);
2931 	if (!state)
2932 		return -ENOMEM;
2933 
2934 	state->acquire_ctx = ctx;
2935 
2936 	ret = page_flip_common(state, crtc, fb, event, flags);
2937 	if (ret != 0)
2938 		goto fail;
2939 
2940 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
2941 	if (WARN_ON(!crtc_state)) {
2942 		ret = -EINVAL;
2943 		goto fail;
2944 	}
2945 	crtc_state->target_vblank = target;
2946 
2947 	ret = drm_atomic_nonblocking_commit(state);
2948 fail:
2949 	drm_atomic_state_put(state);
2950 	return ret;
2951 }
2952 EXPORT_SYMBOL(drm_atomic_helper_page_flip_target);
2953 
2954 /**
2955  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2956  * @connector: affected connector
2957  * @mode: DPMS mode
2958  *
2959  * This is the main helper function provided by the atomic helper framework for
2960  * implementing the legacy DPMS connector interface. It computes the new desired
2961  * &drm_crtc_state.active state for the corresponding CRTC (if the connector is
2962  * enabled) and updates it.
2963  *
2964  * Returns:
2965  * Returns 0 on success, negative errno numbers on failure.
2966  */
2967 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2968 				     int mode)
2969 {
2970 	struct drm_mode_config *config = &connector->dev->mode_config;
2971 	struct drm_atomic_state *state;
2972 	struct drm_crtc_state *crtc_state;
2973 	struct drm_crtc *crtc;
2974 	struct drm_connector *tmp_connector;
2975 	struct drm_connector_list_iter conn_iter;
2976 	int ret;
2977 	bool active = false;
2978 	int old_mode = connector->dpms;
2979 
2980 	if (mode != DRM_MODE_DPMS_ON)
2981 		mode = DRM_MODE_DPMS_OFF;
2982 
2983 	connector->dpms = mode;
2984 	crtc = connector->state->crtc;
2985 
2986 	if (!crtc)
2987 		return 0;
2988 
2989 	state = drm_atomic_state_alloc(connector->dev);
2990 	if (!state)
2991 		return -ENOMEM;
2992 
2993 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2994 retry:
2995 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2996 	if (IS_ERR(crtc_state)) {
2997 		ret = PTR_ERR(crtc_state);
2998 		goto fail;
2999 	}
3000 
3001 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
3002 
3003 	drm_connector_list_iter_begin(connector->dev, &conn_iter);
3004 	drm_for_each_connector_iter(tmp_connector, &conn_iter) {
3005 		if (tmp_connector->state->crtc != crtc)
3006 			continue;
3007 
3008 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
3009 			active = true;
3010 			break;
3011 		}
3012 	}
3013 	drm_connector_list_iter_end(&conn_iter);
3014 	crtc_state->active = active;
3015 
3016 	ret = drm_atomic_commit(state);
3017 fail:
3018 	if (ret == -EDEADLK)
3019 		goto backoff;
3020 	if (ret != 0)
3021 		connector->dpms = old_mode;
3022 	drm_atomic_state_put(state);
3023 	return ret;
3024 
3025 backoff:
3026 	drm_atomic_state_clear(state);
3027 	drm_atomic_legacy_backoff(state);
3028 
3029 	goto retry;
3030 }
3031 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
3032 
3033 /**
3034  * drm_atomic_helper_best_encoder - Helper for
3035  * 	&drm_connector_helper_funcs.best_encoder callback
3036  * @connector: Connector control structure
3037  *
3038  * This is a &drm_connector_helper_funcs.best_encoder callback helper for
3039  * connectors that support exactly 1 encoder, statically determined at driver
3040  * init time.
3041  */
3042 struct drm_encoder *
3043 drm_atomic_helper_best_encoder(struct drm_connector *connector)
3044 {
3045 	WARN_ON(connector->encoder_ids[1]);
3046 	return drm_encoder_find(connector->dev, connector->encoder_ids[0]);
3047 }
3048 EXPORT_SYMBOL(drm_atomic_helper_best_encoder);
3049 
3050 /**
3051  * DOC: atomic state reset and initialization
3052  *
3053  * Both the drm core and the atomic helpers assume that there is always the full
3054  * and correct atomic software state for all connectors, CRTCs and planes
3055  * available. Which is a bit a problem on driver load and also after system
3056  * suspend. One way to solve this is to have a hardware state read-out
3057  * infrastructure which reconstructs the full software state (e.g. the i915
3058  * driver).
3059  *
3060  * The simpler solution is to just reset the software state to everything off,
3061  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
3062  * the atomic helpers provide default reset implementations for all hooks.
3063  *
3064  * On the upside the precise state tracking of atomic simplifies system suspend
3065  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
3066  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
3067  * For other drivers the building blocks are split out, see the documentation
3068  * for these functions.
3069  */
3070 
3071 /**
3072  * drm_atomic_helper_crtc_reset - default &drm_crtc_funcs.reset hook for CRTCs
3073  * @crtc: drm CRTC
3074  *
3075  * Resets the atomic state for @crtc by freeing the state pointer (which might
3076  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3077  */
3078 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
3079 {
3080 	if (crtc->state)
3081 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
3082 
3083 	kfree(crtc->state);
3084 	crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
3085 
3086 	if (crtc->state)
3087 		crtc->state->crtc = crtc;
3088 }
3089 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
3090 
3091 /**
3092  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
3093  * @crtc: CRTC object
3094  * @state: atomic CRTC state
3095  *
3096  * Copies atomic state from a CRTC's current state and resets inferred values.
3097  * This is useful for drivers that subclass the CRTC state.
3098  */
3099 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
3100 					      struct drm_crtc_state *state)
3101 {
3102 	memcpy(state, crtc->state, sizeof(*state));
3103 
3104 	if (state->mode_blob)
3105 		drm_property_blob_get(state->mode_blob);
3106 	if (state->degamma_lut)
3107 		drm_property_blob_get(state->degamma_lut);
3108 	if (state->ctm)
3109 		drm_property_blob_get(state->ctm);
3110 	if (state->gamma_lut)
3111 		drm_property_blob_get(state->gamma_lut);
3112 	state->mode_changed = false;
3113 	state->active_changed = false;
3114 	state->planes_changed = false;
3115 	state->connectors_changed = false;
3116 	state->color_mgmt_changed = false;
3117 	state->zpos_changed = false;
3118 	state->event = NULL;
3119 	state->pageflip_flags = 0;
3120 }
3121 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
3122 
3123 /**
3124  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
3125  * @crtc: drm CRTC
3126  *
3127  * Default CRTC state duplicate hook for drivers which don't have their own
3128  * subclassed CRTC state structure.
3129  */
3130 struct drm_crtc_state *
3131 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
3132 {
3133 	struct drm_crtc_state *state;
3134 
3135 	if (WARN_ON(!crtc->state))
3136 		return NULL;
3137 
3138 	state = kmalloc(sizeof(*state), M_DRM, GFP_KERNEL);
3139 	if (state)
3140 		__drm_atomic_helper_crtc_duplicate_state(crtc, state);
3141 
3142 	return state;
3143 }
3144 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
3145 
3146 /**
3147  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
3148  * @state: CRTC state object to release
3149  *
3150  * Releases all resources stored in the CRTC state without actually freeing
3151  * the memory of the CRTC state. This is useful for drivers that subclass the
3152  * CRTC state.
3153  */
3154 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc_state *state)
3155 {
3156 	drm_property_blob_put(state->mode_blob);
3157 	drm_property_blob_put(state->degamma_lut);
3158 	drm_property_blob_put(state->ctm);
3159 	drm_property_blob_put(state->gamma_lut);
3160 }
3161 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
3162 
3163 /**
3164  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
3165  * @crtc: drm CRTC
3166  * @state: CRTC state object to release
3167  *
3168  * Default CRTC state destroy hook for drivers which don't have their own
3169  * subclassed CRTC state structure.
3170  */
3171 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
3172 					  struct drm_crtc_state *state)
3173 {
3174 	__drm_atomic_helper_crtc_destroy_state(state);
3175 	kfree(state);
3176 }
3177 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
3178 
3179 /**
3180  * drm_atomic_helper_plane_reset - default &drm_plane_funcs.reset hook for planes
3181  * @plane: drm plane
3182  *
3183  * Resets the atomic state for @plane by freeing the state pointer (which might
3184  * be NULL, e.g. at driver load time) and allocating a new empty state object.
3185  */
3186 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
3187 {
3188 	if (plane->state)
3189 		__drm_atomic_helper_plane_destroy_state(plane->state);
3190 
3191 	kfree(plane->state);
3192 	plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
3193 
3194 	if (plane->state) {
3195 		plane->state->plane = plane;
3196 		plane->state->rotation = DRM_ROTATE_0;
3197 	}
3198 }
3199 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
3200 
3201 /**
3202  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
3203  * @plane: plane object
3204  * @state: atomic plane state
3205  *
3206  * Copies atomic state from a plane's current state. This is useful for
3207  * drivers that subclass the plane state.
3208  */
3209 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
3210 					       struct drm_plane_state *state)
3211 {
3212 	memcpy(state, plane->state, sizeof(*state));
3213 
3214 	if (state->fb)
3215 		drm_framebuffer_get(state->fb);
3216 
3217 	state->fence = NULL;
3218 }
3219 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
3220 
3221 /**
3222  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
3223  * @plane: drm plane
3224  *
3225  * Default plane state duplicate hook for drivers which don't have their own
3226  * subclassed plane state structure.
3227  */
3228 struct drm_plane_state *
3229 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
3230 {
3231 	struct drm_plane_state *state;
3232 
3233 	if (WARN_ON(!plane->state))
3234 		return NULL;
3235 
3236 	state = kmalloc(sizeof(*state), M_DRM, GFP_KERNEL);
3237 	if (state)
3238 		__drm_atomic_helper_plane_duplicate_state(plane, state);
3239 
3240 	return state;
3241 }
3242 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
3243 
3244 /**
3245  * __drm_atomic_helper_plane_destroy_state - release plane state
3246  * @state: plane state object to release
3247  *
3248  * Releases all resources stored in the plane state without actually freeing
3249  * the memory of the plane state. This is useful for drivers that subclass the
3250  * plane state.
3251  */
3252 void __drm_atomic_helper_plane_destroy_state(struct drm_plane_state *state)
3253 {
3254 	if (state->fb)
3255 		drm_framebuffer_put(state->fb);
3256 
3257 	if (state->fence)
3258 		dma_fence_put(state->fence);
3259 }
3260 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
3261 
3262 /**
3263  * drm_atomic_helper_plane_destroy_state - default state destroy hook
3264  * @plane: drm plane
3265  * @state: plane state object to release
3266  *
3267  * Default plane state destroy hook for drivers which don't have their own
3268  * subclassed plane state structure.
3269  */
3270 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
3271 					   struct drm_plane_state *state)
3272 {
3273 	__drm_atomic_helper_plane_destroy_state(state);
3274 	kfree(state);
3275 }
3276 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
3277 
3278 /**
3279  * __drm_atomic_helper_connector_reset - reset state on connector
3280  * @connector: drm connector
3281  * @conn_state: connector state to assign
3282  *
3283  * Initializes the newly allocated @conn_state and assigns it to
3284  * the &drm_conector->state pointer of @connector, usually required when
3285  * initializing the drivers or when called from the &drm_connector_funcs.reset
3286  * hook.
3287  *
3288  * This is useful for drivers that subclass the connector state.
3289  */
3290 void
3291 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
3292 				    struct drm_connector_state *conn_state)
3293 {
3294 	if (conn_state)
3295 		conn_state->connector = connector;
3296 
3297 	connector->state = conn_state;
3298 }
3299 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
3300 
3301 /**
3302  * drm_atomic_helper_connector_reset - default &drm_connector_funcs.reset hook for connectors
3303  * @connector: drm connector
3304  *
3305  * Resets the atomic state for @connector by freeing the state pointer (which
3306  * might be NULL, e.g. at driver load time) and allocating a new empty state
3307  * object.
3308  */
3309 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
3310 {
3311 	struct drm_connector_state *conn_state =
3312 		kzalloc(sizeof(*conn_state), GFP_KERNEL);
3313 
3314 	if (connector->state)
3315 		__drm_atomic_helper_connector_destroy_state(connector->state);
3316 
3317 	kfree(connector->state);
3318 	__drm_atomic_helper_connector_reset(connector, conn_state);
3319 }
3320 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
3321 
3322 /**
3323  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
3324  * @connector: connector object
3325  * @state: atomic connector state
3326  *
3327  * Copies atomic state from a connector's current state. This is useful for
3328  * drivers that subclass the connector state.
3329  */
3330 void
3331 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
3332 					    struct drm_connector_state *state)
3333 {
3334 	memcpy(state, connector->state, sizeof(*state));
3335 	if (state->crtc)
3336 		drm_connector_get(connector);
3337 }
3338 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
3339 
3340 /**
3341  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
3342  * @connector: drm connector
3343  *
3344  * Default connector state duplicate hook for drivers which don't have their own
3345  * subclassed connector state structure.
3346  */
3347 struct drm_connector_state *
3348 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
3349 {
3350 	struct drm_connector_state *state;
3351 
3352 	if (WARN_ON(!connector->state))
3353 		return NULL;
3354 
3355 	state = kmalloc(sizeof(*state), M_DRM, GFP_KERNEL);
3356 	if (state)
3357 		__drm_atomic_helper_connector_duplicate_state(connector, state);
3358 
3359 	return state;
3360 }
3361 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
3362 
3363 /**
3364  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
3365  * @dev: DRM device
3366  * @ctx: lock acquisition context
3367  *
3368  * Makes a copy of the current atomic state by looping over all objects and
3369  * duplicating their respective states. This is used for example by suspend/
3370  * resume support code to save the state prior to suspend such that it can
3371  * be restored upon resume.
3372  *
3373  * Note that this treats atomic state as persistent between save and restore.
3374  * Drivers must make sure that this is possible and won't result in confusion
3375  * or erroneous behaviour.
3376  *
3377  * Note that if callers haven't already acquired all modeset locks this might
3378  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
3379  *
3380  * Returns:
3381  * A pointer to the copy of the atomic state object on success or an
3382  * ERR_PTR()-encoded error code on failure.
3383  *
3384  * See also:
3385  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
3386  */
3387 struct drm_atomic_state *
3388 drm_atomic_helper_duplicate_state(struct drm_device *dev,
3389 				  struct drm_modeset_acquire_ctx *ctx)
3390 {
3391 	struct drm_atomic_state *state;
3392 	struct drm_connector *conn;
3393 	struct drm_connector_list_iter conn_iter;
3394 	struct drm_plane *plane;
3395 	struct drm_crtc *crtc;
3396 	int err = 0;
3397 
3398 	state = drm_atomic_state_alloc(dev);
3399 	if (!state)
3400 		return ERR_PTR(-ENOMEM);
3401 
3402 	state->acquire_ctx = ctx;
3403 
3404 	drm_for_each_crtc(crtc, dev) {
3405 		struct drm_crtc_state *crtc_state;
3406 
3407 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
3408 		if (IS_ERR(crtc_state)) {
3409 			err = PTR_ERR(crtc_state);
3410 			goto free;
3411 		}
3412 	}
3413 
3414 	drm_for_each_plane(plane, dev) {
3415 		struct drm_plane_state *plane_state;
3416 
3417 		plane_state = drm_atomic_get_plane_state(state, plane);
3418 		if (IS_ERR(plane_state)) {
3419 			err = PTR_ERR(plane_state);
3420 			goto free;
3421 		}
3422 	}
3423 
3424 	drm_connector_list_iter_begin(dev, &conn_iter);
3425 	drm_for_each_connector_iter(conn, &conn_iter) {
3426 		struct drm_connector_state *conn_state;
3427 
3428 		conn_state = drm_atomic_get_connector_state(state, conn);
3429 		if (IS_ERR(conn_state)) {
3430 			err = PTR_ERR(conn_state);
3431 			drm_connector_list_iter_end(&conn_iter);
3432 			goto free;
3433 		}
3434 	}
3435 	drm_connector_list_iter_end(&conn_iter);
3436 
3437 	/* clear the acquire context so that it isn't accidentally reused */
3438 	state->acquire_ctx = NULL;
3439 
3440 free:
3441 	if (err < 0) {
3442 		drm_atomic_state_put(state);
3443 		state = ERR_PTR(err);
3444 	}
3445 
3446 	return state;
3447 }
3448 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
3449 
3450 /**
3451  * __drm_atomic_helper_connector_destroy_state - release connector state
3452  * @state: connector state object to release
3453  *
3454  * Releases all resources stored in the connector state without actually
3455  * freeing the memory of the connector state. This is useful for drivers that
3456  * subclass the connector state.
3457  */
3458 void
3459 __drm_atomic_helper_connector_destroy_state(struct drm_connector_state *state)
3460 {
3461 	if (state->crtc)
3462 		drm_connector_put(state->connector);
3463 }
3464 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
3465 
3466 /**
3467  * drm_atomic_helper_connector_destroy_state - default state destroy hook
3468  * @connector: drm connector
3469  * @state: connector state object to release
3470  *
3471  * Default connector state destroy hook for drivers which don't have their own
3472  * subclassed connector state structure.
3473  */
3474 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
3475 					  struct drm_connector_state *state)
3476 {
3477 	__drm_atomic_helper_connector_destroy_state(state);
3478 	kfree(state);
3479 }
3480 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
3481 
3482 /**
3483  * drm_atomic_helper_legacy_gamma_set - set the legacy gamma correction table
3484  * @crtc: CRTC object
3485  * @red: red correction table
3486  * @green: green correction table
3487  * @blue: green correction table
3488  * @size: size of the tables
3489  * @ctx: lock acquire context
3490  *
3491  * Implements support for legacy gamma correction table for drivers
3492  * that support color management through the DEGAMMA_LUT/GAMMA_LUT
3493  * properties.
3494  */
3495 int drm_atomic_helper_legacy_gamma_set(struct drm_crtc *crtc,
3496 				       u16 *red, u16 *green, u16 *blue,
3497 				       uint32_t size,
3498 				       struct drm_modeset_acquire_ctx *ctx)
3499 {
3500 	struct drm_device *dev = crtc->dev;
3501 	struct drm_mode_config *config = &dev->mode_config;
3502 	struct drm_atomic_state *state;
3503 	struct drm_crtc_state *crtc_state;
3504 	struct drm_property_blob *blob = NULL;
3505 	struct drm_color_lut *blob_data;
3506 	int i, ret = 0;
3507 
3508 	state = drm_atomic_state_alloc(crtc->dev);
3509 	if (!state)
3510 		return -ENOMEM;
3511 
3512 	blob = drm_property_create_blob(dev,
3513 					sizeof(struct drm_color_lut) * size,
3514 					NULL);
3515 	if (IS_ERR(blob)) {
3516 		ret = PTR_ERR(blob);
3517 		blob = NULL;
3518 		goto fail;
3519 	}
3520 
3521 	/* Prepare GAMMA_LUT with the legacy values. */
3522 	blob_data = (struct drm_color_lut *) blob->data;
3523 	for (i = 0; i < size; i++) {
3524 		blob_data[i].red = red[i];
3525 		blob_data[i].green = green[i];
3526 		blob_data[i].blue = blue[i];
3527 	}
3528 
3529 	state->acquire_ctx = ctx;
3530 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
3531 	if (IS_ERR(crtc_state)) {
3532 		ret = PTR_ERR(crtc_state);
3533 		goto fail;
3534 	}
3535 
3536 	/* Reset DEGAMMA_LUT and CTM properties. */
3537 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3538 			config->degamma_lut_property, 0);
3539 	if (ret)
3540 		goto fail;
3541 
3542 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3543 			config->ctm_property, 0);
3544 	if (ret)
3545 		goto fail;
3546 
3547 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
3548 			config->gamma_lut_property, blob->base.id);
3549 	if (ret)
3550 		goto fail;
3551 
3552 	ret = drm_atomic_commit(state);
3553 
3554 fail:
3555 	drm_atomic_state_put(state);
3556 	drm_property_blob_put(blob);
3557 	return ret;
3558 }
3559 EXPORT_SYMBOL(drm_atomic_helper_legacy_gamma_set);
3560