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