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