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