xref: /linux/drivers/gpu/drm/drm_atomic_helper.c (revision a8fe58cec351c25e09c393bf46117c0c47b5a17c)
1 /*
2  * Copyright (C) 2014 Red Hat
3  * Copyright (C) 2014 Intel Corp.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  * Rob Clark <robdclark@gmail.com>
25  * Daniel Vetter <daniel.vetter@ffwll.ch>
26  */
27 
28 #include <drm/drmP.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <linux/fence.h>
34 
35 /**
36  * DOC: overview
37  *
38  * This helper library provides implementations of check and commit functions on
39  * top of the CRTC modeset helper callbacks and the plane helper callbacks. It
40  * also provides convenience implementations for the atomic state handling
41  * callbacks for drivers which don't need to subclass the drm core structures to
42  * add their own additional internal state.
43  *
44  * This library also provides default implementations for the check callback in
45  * drm_atomic_helper_check() and for the commit callback with
46  * drm_atomic_helper_commit(). But the individual stages and callbacks are
47  * exposed to allow drivers to mix and match and e.g. use the plane helpers only
48  * together with a driver private modeset implementation.
49  *
50  * This library also provides implementations for all the legacy driver
51  * interfaces on top of the atomic interface. See drm_atomic_helper_set_config(),
52  * drm_atomic_helper_disable_plane(), drm_atomic_helper_disable_plane() and the
53  * various functions to implement set_property callbacks. New drivers must not
54  * implement these functions themselves but must use the provided helpers.
55  *
56  * The atomic helper uses the same function table structures as all other
57  * modesetting helpers. See the documentation for struct &drm_crtc_helper_funcs,
58  * struct &drm_encoder_helper_funcs and struct &drm_connector_helper_funcs. It
59  * also shares the struct &drm_plane_helper_funcs function table with the plane
60  * helpers.
61  */
62 static void
63 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
64 				struct drm_plane_state *plane_state,
65 				struct drm_plane *plane)
66 {
67 	struct drm_crtc_state *crtc_state;
68 
69 	if (plane->state->crtc) {
70 		crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
71 
72 		if (WARN_ON(!crtc_state))
73 			return;
74 
75 		crtc_state->planes_changed = true;
76 	}
77 
78 	if (plane_state->crtc) {
79 		crtc_state =
80 			state->crtc_states[drm_crtc_index(plane_state->crtc)];
81 
82 		if (WARN_ON(!crtc_state))
83 			return;
84 
85 		crtc_state->planes_changed = true;
86 	}
87 }
88 
89 static bool
90 check_pending_encoder_assignment(struct drm_atomic_state *state,
91 				 struct drm_encoder *new_encoder)
92 {
93 	struct drm_connector *connector;
94 	struct drm_connector_state *conn_state;
95 	int i;
96 
97 	for_each_connector_in_state(state, connector, conn_state, i) {
98 		if (conn_state->best_encoder != new_encoder)
99 			continue;
100 
101 		/* encoder already assigned and we're trying to re-steal it! */
102 		if (connector->state->best_encoder != conn_state->best_encoder)
103 			return false;
104 	}
105 
106 	return true;
107 }
108 
109 static struct drm_crtc *
110 get_current_crtc_for_encoder(struct drm_device *dev,
111 			     struct drm_encoder *encoder)
112 {
113 	struct drm_mode_config *config = &dev->mode_config;
114 	struct drm_connector *connector;
115 
116 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
117 
118 	drm_for_each_connector(connector, dev) {
119 		if (connector->state->best_encoder != encoder)
120 			continue;
121 
122 		return connector->state->crtc;
123 	}
124 
125 	return NULL;
126 }
127 
128 static int
129 steal_encoder(struct drm_atomic_state *state,
130 	      struct drm_encoder *encoder,
131 	      struct drm_crtc *encoder_crtc)
132 {
133 	struct drm_mode_config *config = &state->dev->mode_config;
134 	struct drm_crtc_state *crtc_state;
135 	struct drm_connector *connector;
136 	struct drm_connector_state *connector_state;
137 	int ret;
138 
139 	/*
140 	 * We can only steal an encoder coming from a connector, which means we
141 	 * must already hold the connection_mutex.
142 	 */
143 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
144 
145 	DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d:%s], stealing it\n",
146 			 encoder->base.id, encoder->name,
147 			 encoder_crtc->base.id, encoder_crtc->name);
148 
149 	crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
150 	if (IS_ERR(crtc_state))
151 		return PTR_ERR(crtc_state);
152 
153 	crtc_state->connectors_changed = true;
154 
155 	list_for_each_entry(connector, &config->connector_list, head) {
156 		if (connector->state->best_encoder != encoder)
157 			continue;
158 
159 		DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
160 				 connector->base.id,
161 				 connector->name);
162 
163 		connector_state = drm_atomic_get_connector_state(state,
164 								 connector);
165 		if (IS_ERR(connector_state))
166 			return PTR_ERR(connector_state);
167 
168 		ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
169 		if (ret)
170 			return ret;
171 		connector_state->best_encoder = NULL;
172 	}
173 
174 	return 0;
175 }
176 
177 static int
178 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
179 {
180 	const struct drm_connector_helper_funcs *funcs;
181 	struct drm_encoder *new_encoder;
182 	struct drm_crtc *encoder_crtc;
183 	struct drm_connector *connector;
184 	struct drm_connector_state *connector_state;
185 	struct drm_crtc_state *crtc_state;
186 	int idx, ret;
187 
188 	connector = state->connectors[conn_idx];
189 	connector_state = state->connector_states[conn_idx];
190 
191 	if (!connector)
192 		return 0;
193 
194 	DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
195 			 connector->base.id,
196 			 connector->name);
197 
198 	if (connector->state->crtc != connector_state->crtc) {
199 		if (connector->state->crtc) {
200 			idx = drm_crtc_index(connector->state->crtc);
201 
202 			crtc_state = state->crtc_states[idx];
203 			crtc_state->connectors_changed = true;
204 		}
205 
206 		if (connector_state->crtc) {
207 			idx = drm_crtc_index(connector_state->crtc);
208 
209 			crtc_state = state->crtc_states[idx];
210 			crtc_state->connectors_changed = true;
211 		}
212 	}
213 
214 	if (!connector_state->crtc) {
215 		DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
216 				connector->base.id,
217 				connector->name);
218 
219 		connector_state->best_encoder = NULL;
220 
221 		return 0;
222 	}
223 
224 	funcs = connector->helper_private;
225 
226 	if (funcs->atomic_best_encoder)
227 		new_encoder = funcs->atomic_best_encoder(connector,
228 							 connector_state);
229 	else
230 		new_encoder = funcs->best_encoder(connector);
231 
232 	if (!new_encoder) {
233 		DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
234 				 connector->base.id,
235 				 connector->name);
236 		return -EINVAL;
237 	}
238 
239 	if (!drm_encoder_crtc_ok(new_encoder, connector_state->crtc)) {
240 		DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] incompatible with [CRTC:%d]\n",
241 				 new_encoder->base.id,
242 				 new_encoder->name,
243 				 connector_state->crtc->base.id);
244 		return -EINVAL;
245 	}
246 
247 	if (new_encoder == connector_state->best_encoder) {
248 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d:%s]\n",
249 				 connector->base.id,
250 				 connector->name,
251 				 new_encoder->base.id,
252 				 new_encoder->name,
253 				 connector_state->crtc->base.id,
254 				 connector_state->crtc->name);
255 
256 		return 0;
257 	}
258 
259 	if (!check_pending_encoder_assignment(state, new_encoder)) {
260 		DRM_DEBUG_ATOMIC("Encoder for [CONNECTOR:%d:%s] already assigned\n",
261 				 connector->base.id,
262 				 connector->name);
263 		return -EINVAL;
264 	}
265 
266 	encoder_crtc = get_current_crtc_for_encoder(state->dev,
267 						    new_encoder);
268 
269 	if (encoder_crtc) {
270 		ret = steal_encoder(state, new_encoder, encoder_crtc);
271 		if (ret) {
272 			DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
273 					 connector->base.id,
274 					 connector->name);
275 			return ret;
276 		}
277 	}
278 
279 	if (WARN_ON(!connector_state->crtc))
280 		return -EINVAL;
281 
282 	connector_state->best_encoder = new_encoder;
283 	idx = drm_crtc_index(connector_state->crtc);
284 
285 	crtc_state = state->crtc_states[idx];
286 	crtc_state->connectors_changed = true;
287 
288 	DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d:%s]\n",
289 			 connector->base.id,
290 			 connector->name,
291 			 new_encoder->base.id,
292 			 new_encoder->name,
293 			 connector_state->crtc->base.id,
294 			 connector_state->crtc->name);
295 
296 	return 0;
297 }
298 
299 static int
300 mode_fixup(struct drm_atomic_state *state)
301 {
302 	struct drm_crtc *crtc;
303 	struct drm_crtc_state *crtc_state;
304 	struct drm_connector *connector;
305 	struct drm_connector_state *conn_state;
306 	int i;
307 	bool ret;
308 
309 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
310 		if (!crtc_state->mode_changed &&
311 		    !crtc_state->connectors_changed)
312 			continue;
313 
314 		drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
315 	}
316 
317 	for_each_connector_in_state(state, connector, conn_state, i) {
318 		const struct drm_encoder_helper_funcs *funcs;
319 		struct drm_encoder *encoder;
320 
321 		WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
322 
323 		if (!conn_state->crtc || !conn_state->best_encoder)
324 			continue;
325 
326 		crtc_state =
327 			state->crtc_states[drm_crtc_index(conn_state->crtc)];
328 
329 		/*
330 		 * Each encoder has at most one connector (since we always steal
331 		 * it away), so we won't call ->mode_fixup twice.
332 		 */
333 		encoder = conn_state->best_encoder;
334 		funcs = encoder->helper_private;
335 		if (!funcs)
336 			continue;
337 
338 		ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
339 				&crtc_state->adjusted_mode);
340 		if (!ret) {
341 			DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
342 			return -EINVAL;
343 		}
344 
345 		if (funcs->atomic_check) {
346 			ret = funcs->atomic_check(encoder, crtc_state,
347 						  conn_state);
348 			if (ret) {
349 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
350 						 encoder->base.id, encoder->name);
351 				return ret;
352 			}
353 		} else if (funcs->mode_fixup) {
354 			ret = funcs->mode_fixup(encoder, &crtc_state->mode,
355 						&crtc_state->adjusted_mode);
356 			if (!ret) {
357 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
358 						 encoder->base.id, encoder->name);
359 				return -EINVAL;
360 			}
361 		}
362 	}
363 
364 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
365 		const struct drm_crtc_helper_funcs *funcs;
366 
367 		if (!crtc_state->mode_changed &&
368 		    !crtc_state->connectors_changed)
369 			continue;
370 
371 		funcs = crtc->helper_private;
372 		if (!funcs->mode_fixup)
373 			continue;
374 
375 		ret = funcs->mode_fixup(crtc, &crtc_state->mode,
376 					&crtc_state->adjusted_mode);
377 		if (!ret) {
378 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] fixup failed\n",
379 					 crtc->base.id, crtc->name);
380 			return -EINVAL;
381 		}
382 	}
383 
384 	return 0;
385 }
386 
387 /**
388  * drm_atomic_helper_check_modeset - validate state object for modeset changes
389  * @dev: DRM device
390  * @state: the driver state object
391  *
392  * Check the state object to see if the requested state is physically possible.
393  * This does all the crtc and connector related computations for an atomic
394  * update and adds any additional connectors needed for full modesets and calls
395  * down into ->mode_fixup functions of the driver backend.
396  *
397  * crtc_state->mode_changed is set when the input mode is changed.
398  * crtc_state->connectors_changed is set when a connector is added or
399  * removed from the crtc.
400  * crtc_state->active_changed is set when crtc_state->active changes,
401  * which is used for dpms.
402  *
403  * IMPORTANT:
404  *
405  * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
406  * plane update can't be done without a full modeset) _must_ call this function
407  * afterwards after that change. It is permitted to call this function multiple
408  * times for the same update, e.g. when the ->atomic_check functions depend upon
409  * the adjusted dotclock for fifo space allocation and watermark computation.
410  *
411  * RETURNS
412  * Zero for success or -errno
413  */
414 int
415 drm_atomic_helper_check_modeset(struct drm_device *dev,
416 				struct drm_atomic_state *state)
417 {
418 	struct drm_crtc *crtc;
419 	struct drm_crtc_state *crtc_state;
420 	struct drm_connector *connector;
421 	struct drm_connector_state *connector_state;
422 	int i, ret;
423 
424 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
425 		if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
426 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] mode changed\n",
427 					 crtc->base.id, crtc->name);
428 			crtc_state->mode_changed = true;
429 		}
430 
431 		if (crtc->state->enable != crtc_state->enable) {
432 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enable changed\n",
433 					 crtc->base.id, crtc->name);
434 
435 			/*
436 			 * For clarity this assignment is done here, but
437 			 * enable == 0 is only true when there are no
438 			 * connectors and a NULL mode.
439 			 *
440 			 * The other way around is true as well. enable != 0
441 			 * iff connectors are attached and a mode is set.
442 			 */
443 			crtc_state->mode_changed = true;
444 			crtc_state->connectors_changed = true;
445 		}
446 	}
447 
448 	for_each_connector_in_state(state, connector, connector_state, i) {
449 		/*
450 		 * This only sets crtc->mode_changed for routing changes,
451 		 * drivers must set crtc->mode_changed themselves when connector
452 		 * properties need to be updated.
453 		 */
454 		ret = update_connector_routing(state, i);
455 		if (ret)
456 			return ret;
457 	}
458 
459 	/*
460 	 * After all the routing has been prepared we need to add in any
461 	 * connector which is itself unchanged, but who's crtc changes it's
462 	 * configuration. This must be done before calling mode_fixup in case a
463 	 * crtc only changed its mode but has the same set of connectors.
464 	 */
465 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
466 		bool has_connectors =
467 			!!crtc_state->connector_mask;
468 
469 		/*
470 		 * We must set ->active_changed after walking connectors for
471 		 * otherwise an update that only changes active would result in
472 		 * a full modeset because update_connector_routing force that.
473 		 */
474 		if (crtc->state->active != crtc_state->active) {
475 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active changed\n",
476 					 crtc->base.id, crtc->name);
477 			crtc_state->active_changed = true;
478 		}
479 
480 		if (!drm_atomic_crtc_needs_modeset(crtc_state))
481 			continue;
482 
483 		DRM_DEBUG_ATOMIC("[CRTC:%d:%s] needs all connectors, enable: %c, active: %c\n",
484 				 crtc->base.id, crtc->name,
485 				 crtc_state->enable ? 'y' : 'n',
486 				 crtc_state->active ? 'y' : 'n');
487 
488 		ret = drm_atomic_add_affected_connectors(state, crtc);
489 		if (ret != 0)
490 			return ret;
491 
492 		ret = drm_atomic_add_affected_planes(state, crtc);
493 		if (ret != 0)
494 			return ret;
495 
496 		if (crtc_state->enable != has_connectors) {
497 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled/connectors mismatch\n",
498 					 crtc->base.id, crtc->name);
499 
500 			return -EINVAL;
501 		}
502 	}
503 
504 	return mode_fixup(state);
505 }
506 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
507 
508 /**
509  * drm_atomic_helper_check_planes - validate state object for planes changes
510  * @dev: DRM device
511  * @state: the driver state object
512  *
513  * Check the state object to see if the requested state is physically possible.
514  * This does all the plane update related checks using by calling into the
515  * ->atomic_check hooks provided by the driver.
516  *
517  * It also sets crtc_state->planes_changed to indicate that a crtc has
518  * updated planes.
519  *
520  * RETURNS
521  * Zero for success or -errno
522  */
523 int
524 drm_atomic_helper_check_planes(struct drm_device *dev,
525 			       struct drm_atomic_state *state)
526 {
527 	struct drm_crtc *crtc;
528 	struct drm_crtc_state *crtc_state;
529 	struct drm_plane *plane;
530 	struct drm_plane_state *plane_state;
531 	int i, ret = 0;
532 
533 	for_each_plane_in_state(state, plane, plane_state, i) {
534 		const struct drm_plane_helper_funcs *funcs;
535 
536 		funcs = plane->helper_private;
537 
538 		drm_atomic_helper_plane_changed(state, plane_state, plane);
539 
540 		if (!funcs || !funcs->atomic_check)
541 			continue;
542 
543 		ret = funcs->atomic_check(plane, plane_state);
544 		if (ret) {
545 			DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic driver check failed\n",
546 					 plane->base.id, plane->name);
547 			return ret;
548 		}
549 	}
550 
551 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
552 		const struct drm_crtc_helper_funcs *funcs;
553 
554 		funcs = crtc->helper_private;
555 
556 		if (!funcs || !funcs->atomic_check)
557 			continue;
558 
559 		ret = funcs->atomic_check(crtc, state->crtc_states[i]);
560 		if (ret) {
561 			DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic driver check failed\n",
562 					 crtc->base.id, crtc->name);
563 			return ret;
564 		}
565 	}
566 
567 	return ret;
568 }
569 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
570 
571 /**
572  * drm_atomic_helper_check - validate state object
573  * @dev: DRM device
574  * @state: the driver state object
575  *
576  * Check the state object to see if the requested state is physically possible.
577  * Only crtcs and planes have check callbacks, so for any additional (global)
578  * checking that a driver needs it can simply wrap that around this function.
579  * Drivers without such needs can directly use this as their ->atomic_check()
580  * callback.
581  *
582  * This just wraps the two parts of the state checking for planes and modeset
583  * state in the default order: First it calls drm_atomic_helper_check_modeset()
584  * and then drm_atomic_helper_check_planes(). The assumption is that the
585  * ->atomic_check functions depend upon an updated adjusted_mode.clock to
586  * e.g. properly compute watermarks.
587  *
588  * RETURNS
589  * Zero for success or -errno
590  */
591 int drm_atomic_helper_check(struct drm_device *dev,
592 			    struct drm_atomic_state *state)
593 {
594 	int ret;
595 
596 	ret = drm_atomic_helper_check_modeset(dev, state);
597 	if (ret)
598 		return ret;
599 
600 	ret = drm_atomic_helper_check_planes(dev, state);
601 	if (ret)
602 		return ret;
603 
604 	return ret;
605 }
606 EXPORT_SYMBOL(drm_atomic_helper_check);
607 
608 static void
609 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
610 {
611 	struct drm_connector *connector;
612 	struct drm_connector_state *old_conn_state;
613 	struct drm_crtc *crtc;
614 	struct drm_crtc_state *old_crtc_state;
615 	int i;
616 
617 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
618 		const struct drm_encoder_helper_funcs *funcs;
619 		struct drm_encoder *encoder;
620 
621 		/* Shut down everything that's in the changeset and currently
622 		 * still on. So need to check the old, saved state. */
623 		if (!old_conn_state->crtc)
624 			continue;
625 
626 		old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
627 
628 		if (!old_crtc_state->active ||
629 		    !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
630 			continue;
631 
632 		encoder = old_conn_state->best_encoder;
633 
634 		/* We shouldn't get this far if we didn't previously have
635 		 * an encoder.. but WARN_ON() rather than explode.
636 		 */
637 		if (WARN_ON(!encoder))
638 			continue;
639 
640 		funcs = encoder->helper_private;
641 
642 		DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
643 				 encoder->base.id, encoder->name);
644 
645 		/*
646 		 * Each encoder has at most one connector (since we always steal
647 		 * it away), so we won't call disable hooks twice.
648 		 */
649 		drm_bridge_disable(encoder->bridge);
650 
651 		/* Right function depends upon target state. */
652 		if (connector->state->crtc && funcs->prepare)
653 			funcs->prepare(encoder);
654 		else if (funcs->disable)
655 			funcs->disable(encoder);
656 		else
657 			funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
658 
659 		drm_bridge_post_disable(encoder->bridge);
660 	}
661 
662 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
663 		const struct drm_crtc_helper_funcs *funcs;
664 
665 		/* Shut down everything that needs a full modeset. */
666 		if (!drm_atomic_crtc_needs_modeset(crtc->state))
667 			continue;
668 
669 		if (!old_crtc_state->active)
670 			continue;
671 
672 		funcs = crtc->helper_private;
673 
674 		DRM_DEBUG_ATOMIC("disabling [CRTC:%d:%s]\n",
675 				 crtc->base.id, crtc->name);
676 
677 
678 		/* Right function depends upon target state. */
679 		if (crtc->state->enable && funcs->prepare)
680 			funcs->prepare(crtc);
681 		else if (funcs->disable)
682 			funcs->disable(crtc);
683 		else
684 			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
685 	}
686 }
687 
688 /**
689  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
690  * @dev: DRM device
691  * @old_state: atomic state object with old state structures
692  *
693  * This function updates all the various legacy modeset state pointers in
694  * connectors, encoders and crtcs. It also updates the timestamping constants
695  * used for precise vblank timestamps by calling
696  * drm_calc_timestamping_constants().
697  *
698  * Drivers can use this for building their own atomic commit if they don't have
699  * a pure helper-based modeset implementation.
700  */
701 void
702 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
703 					      struct drm_atomic_state *old_state)
704 {
705 	struct drm_connector *connector;
706 	struct drm_connector_state *old_conn_state;
707 	struct drm_crtc *crtc;
708 	struct drm_crtc_state *old_crtc_state;
709 	int i;
710 
711 	/* clear out existing links and update dpms */
712 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
713 		if (connector->encoder) {
714 			WARN_ON(!connector->encoder->crtc);
715 
716 			connector->encoder->crtc = NULL;
717 			connector->encoder = NULL;
718 		}
719 
720 		crtc = connector->state->crtc;
721 		if ((!crtc && old_conn_state->crtc) ||
722 		    (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
723 			struct drm_property *dpms_prop =
724 				dev->mode_config.dpms_property;
725 			int mode = DRM_MODE_DPMS_OFF;
726 
727 			if (crtc && crtc->state->active)
728 				mode = DRM_MODE_DPMS_ON;
729 
730 			connector->dpms = mode;
731 			drm_object_property_set_value(&connector->base,
732 						      dpms_prop, mode);
733 		}
734 	}
735 
736 	/* set new links */
737 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
738 		if (!connector->state->crtc)
739 			continue;
740 
741 		if (WARN_ON(!connector->state->best_encoder))
742 			continue;
743 
744 		connector->encoder = connector->state->best_encoder;
745 		connector->encoder->crtc = connector->state->crtc;
746 	}
747 
748 	/* set legacy state in the crtc structure */
749 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
750 		struct drm_plane *primary = crtc->primary;
751 
752 		crtc->mode = crtc->state->mode;
753 		crtc->enabled = crtc->state->enable;
754 
755 		if (drm_atomic_get_existing_plane_state(old_state, primary) &&
756 		    primary->state->crtc == crtc) {
757 			crtc->x = primary->state->src_x >> 16;
758 			crtc->y = primary->state->src_y >> 16;
759 		}
760 
761 		if (crtc->state->enable)
762 			drm_calc_timestamping_constants(crtc,
763 							&crtc->state->adjusted_mode);
764 	}
765 }
766 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
767 
768 static void
769 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
770 {
771 	struct drm_crtc *crtc;
772 	struct drm_crtc_state *old_crtc_state;
773 	struct drm_connector *connector;
774 	struct drm_connector_state *old_conn_state;
775 	int i;
776 
777 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
778 		const struct drm_crtc_helper_funcs *funcs;
779 
780 		if (!crtc->state->mode_changed)
781 			continue;
782 
783 		funcs = crtc->helper_private;
784 
785 		if (crtc->state->enable && funcs->mode_set_nofb) {
786 			DRM_DEBUG_ATOMIC("modeset on [CRTC:%d:%s]\n",
787 					 crtc->base.id, crtc->name);
788 
789 			funcs->mode_set_nofb(crtc);
790 		}
791 	}
792 
793 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
794 		const struct drm_encoder_helper_funcs *funcs;
795 		struct drm_crtc_state *new_crtc_state;
796 		struct drm_encoder *encoder;
797 		struct drm_display_mode *mode, *adjusted_mode;
798 
799 		if (!connector->state->best_encoder)
800 			continue;
801 
802 		encoder = connector->state->best_encoder;
803 		funcs = encoder->helper_private;
804 		new_crtc_state = connector->state->crtc->state;
805 		mode = &new_crtc_state->mode;
806 		adjusted_mode = &new_crtc_state->adjusted_mode;
807 
808 		if (!new_crtc_state->mode_changed)
809 			continue;
810 
811 		DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
812 				 encoder->base.id, encoder->name);
813 
814 		/*
815 		 * Each encoder has at most one connector (since we always steal
816 		 * it away), so we won't call mode_set hooks twice.
817 		 */
818 		if (funcs->mode_set)
819 			funcs->mode_set(encoder, mode, adjusted_mode);
820 
821 		drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
822 	}
823 }
824 
825 /**
826  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
827  * @dev: DRM device
828  * @old_state: atomic state object with old state structures
829  *
830  * This function shuts down all the outputs that need to be shut down and
831  * prepares them (if required) with the new mode.
832  *
833  * For compatibility with legacy crtc helpers this should be called before
834  * drm_atomic_helper_commit_planes(), which is what the default commit function
835  * does. But drivers with different needs can group the modeset commits together
836  * and do the plane commits at the end. This is useful for drivers doing runtime
837  * PM since planes updates then only happen when the CRTC is actually enabled.
838  */
839 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
840 					       struct drm_atomic_state *old_state)
841 {
842 	disable_outputs(dev, old_state);
843 
844 	drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
845 
846 	crtc_set_mode(dev, old_state);
847 }
848 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
849 
850 /**
851  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
852  * @dev: DRM device
853  * @old_state: atomic state object with old state structures
854  *
855  * This function enables all the outputs with the new configuration which had to
856  * be turned off for the update.
857  *
858  * For compatibility with legacy crtc helpers this should be called after
859  * drm_atomic_helper_commit_planes(), which is what the default commit function
860  * does. But drivers with different needs can group the modeset commits together
861  * and do the plane commits at the end. This is useful for drivers doing runtime
862  * PM since planes updates then only happen when the CRTC is actually enabled.
863  */
864 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
865 					      struct drm_atomic_state *old_state)
866 {
867 	struct drm_crtc *crtc;
868 	struct drm_crtc_state *old_crtc_state;
869 	struct drm_connector *connector;
870 	struct drm_connector_state *old_conn_state;
871 	int i;
872 
873 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
874 		const struct drm_crtc_helper_funcs *funcs;
875 
876 		/* Need to filter out CRTCs where only planes change. */
877 		if (!drm_atomic_crtc_needs_modeset(crtc->state))
878 			continue;
879 
880 		if (!crtc->state->active)
881 			continue;
882 
883 		funcs = crtc->helper_private;
884 
885 		if (crtc->state->enable) {
886 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d:%s]\n",
887 					 crtc->base.id, crtc->name);
888 
889 			if (funcs->enable)
890 				funcs->enable(crtc);
891 			else
892 				funcs->commit(crtc);
893 		}
894 	}
895 
896 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
897 		const struct drm_encoder_helper_funcs *funcs;
898 		struct drm_encoder *encoder;
899 
900 		if (!connector->state->best_encoder)
901 			continue;
902 
903 		if (!connector->state->crtc->state->active ||
904 		    !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
905 			continue;
906 
907 		encoder = connector->state->best_encoder;
908 		funcs = encoder->helper_private;
909 
910 		DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
911 				 encoder->base.id, encoder->name);
912 
913 		/*
914 		 * Each encoder has at most one connector (since we always steal
915 		 * it away), so we won't call enable hooks twice.
916 		 */
917 		drm_bridge_pre_enable(encoder->bridge);
918 
919 		if (funcs->enable)
920 			funcs->enable(encoder);
921 		else
922 			funcs->commit(encoder);
923 
924 		drm_bridge_enable(encoder->bridge);
925 	}
926 }
927 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
928 
929 static void wait_for_fences(struct drm_device *dev,
930 			    struct drm_atomic_state *state)
931 {
932 	struct drm_plane *plane;
933 	struct drm_plane_state *plane_state;
934 	int i;
935 
936 	for_each_plane_in_state(state, plane, plane_state, i) {
937 		if (!plane->state->fence)
938 			continue;
939 
940 		WARN_ON(!plane->state->fb);
941 
942 		fence_wait(plane->state->fence, false);
943 		fence_put(plane->state->fence);
944 		plane->state->fence = NULL;
945 	}
946 }
947 
948 /**
949  * drm_atomic_helper_framebuffer_changed - check if framebuffer has changed
950  * @dev: DRM device
951  * @old_state: atomic state object with old state structures
952  * @crtc: DRM crtc
953  *
954  * Checks whether the framebuffer used for this CRTC changes as a result of
955  * the atomic update.  This is useful for drivers which cannot use
956  * drm_atomic_helper_wait_for_vblanks() and need to reimplement its
957  * functionality.
958  *
959  * Returns:
960  * true if the framebuffer changed.
961  */
962 bool drm_atomic_helper_framebuffer_changed(struct drm_device *dev,
963 					   struct drm_atomic_state *old_state,
964 					   struct drm_crtc *crtc)
965 {
966 	struct drm_plane *plane;
967 	struct drm_plane_state *old_plane_state;
968 	int i;
969 
970 	for_each_plane_in_state(old_state, plane, old_plane_state, i) {
971 		if (plane->state->crtc != crtc &&
972 		    old_plane_state->crtc != crtc)
973 			continue;
974 
975 		if (plane->state->fb != old_plane_state->fb)
976 			return true;
977 	}
978 
979 	return false;
980 }
981 EXPORT_SYMBOL(drm_atomic_helper_framebuffer_changed);
982 
983 /**
984  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
985  * @dev: DRM device
986  * @old_state: atomic state object with old state structures
987  *
988  * Helper to, after atomic commit, wait for vblanks on all effected
989  * crtcs (ie. before cleaning up old framebuffers using
990  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
991  * framebuffers have actually changed to optimize for the legacy cursor and
992  * plane update use-case.
993  */
994 void
995 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
996 		struct drm_atomic_state *old_state)
997 {
998 	struct drm_crtc *crtc;
999 	struct drm_crtc_state *old_crtc_state;
1000 	int i, ret;
1001 
1002 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1003 		/* No one cares about the old state, so abuse it for tracking
1004 		 * and store whether we hold a vblank reference (and should do a
1005 		 * vblank wait) in the ->enable boolean. */
1006 		old_crtc_state->enable = false;
1007 
1008 		if (!crtc->state->enable)
1009 			continue;
1010 
1011 		/* Legacy cursor ioctls are completely unsynced, and userspace
1012 		 * relies on that (by doing tons of cursor updates). */
1013 		if (old_state->legacy_cursor_update)
1014 			continue;
1015 
1016 		if (!drm_atomic_helper_framebuffer_changed(dev,
1017 				old_state, crtc))
1018 			continue;
1019 
1020 		ret = drm_crtc_vblank_get(crtc);
1021 		if (ret != 0)
1022 			continue;
1023 
1024 		old_crtc_state->enable = true;
1025 		old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
1026 	}
1027 
1028 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1029 		if (!old_crtc_state->enable)
1030 			continue;
1031 
1032 		ret = wait_event_timeout(dev->vblank[i].queue,
1033 				old_crtc_state->last_vblank_count !=
1034 					drm_crtc_vblank_count(crtc),
1035 				msecs_to_jiffies(50));
1036 
1037 		drm_crtc_vblank_put(crtc);
1038 	}
1039 }
1040 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
1041 
1042 /**
1043  * drm_atomic_helper_commit - commit validated state object
1044  * @dev: DRM device
1045  * @state: the driver state object
1046  * @async: asynchronous commit
1047  *
1048  * This function commits a with drm_atomic_helper_check() pre-validated state
1049  * object. This can still fail when e.g. the framebuffer reservation fails. For
1050  * now this doesn't implement asynchronous commits.
1051  *
1052  * Note that right now this function does not support async commits, and hence
1053  * driver writers must implement their own version for now. Also note that the
1054  * default ordering of how the various stages are called is to match the legacy
1055  * modeset helper library closest. One peculiarity of that is that it doesn't
1056  * mesh well with runtime PM at all.
1057  *
1058  * For drivers supporting runtime PM the recommended sequence is
1059  *
1060  *     drm_atomic_helper_commit_modeset_disables(dev, state);
1061  *
1062  *     drm_atomic_helper_commit_modeset_enables(dev, state);
1063  *
1064  *     drm_atomic_helper_commit_planes(dev, state, true);
1065  *
1066  * See the kerneldoc entries for these three functions for more details.
1067  *
1068  * RETURNS
1069  * Zero for success or -errno.
1070  */
1071 int drm_atomic_helper_commit(struct drm_device *dev,
1072 			     struct drm_atomic_state *state,
1073 			     bool async)
1074 {
1075 	int ret;
1076 
1077 	if (async)
1078 		return -EBUSY;
1079 
1080 	ret = drm_atomic_helper_prepare_planes(dev, state);
1081 	if (ret)
1082 		return ret;
1083 
1084 	/*
1085 	 * This is the point of no return - everything below never fails except
1086 	 * when the hw goes bonghits. Which means we can commit the new state on
1087 	 * the software side now.
1088 	 */
1089 
1090 	drm_atomic_helper_swap_state(dev, state);
1091 
1092 	/*
1093 	 * Everything below can be run asynchronously without the need to grab
1094 	 * any modeset locks at all under one condition: It must be guaranteed
1095 	 * that the asynchronous work has either been cancelled (if the driver
1096 	 * supports it, which at least requires that the framebuffers get
1097 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1098 	 * before the new state gets committed on the software side with
1099 	 * drm_atomic_helper_swap_state().
1100 	 *
1101 	 * This scheme allows new atomic state updates to be prepared and
1102 	 * checked in parallel to the asynchronous completion of the previous
1103 	 * update. Which is important since compositors need to figure out the
1104 	 * composition of the next frame right after having submitted the
1105 	 * current layout.
1106 	 */
1107 
1108 	wait_for_fences(dev, state);
1109 
1110 	drm_atomic_helper_commit_modeset_disables(dev, state);
1111 
1112 	drm_atomic_helper_commit_planes(dev, state, false);
1113 
1114 	drm_atomic_helper_commit_modeset_enables(dev, state);
1115 
1116 	drm_atomic_helper_wait_for_vblanks(dev, state);
1117 
1118 	drm_atomic_helper_cleanup_planes(dev, state);
1119 
1120 	drm_atomic_state_free(state);
1121 
1122 	return 0;
1123 }
1124 EXPORT_SYMBOL(drm_atomic_helper_commit);
1125 
1126 /**
1127  * DOC: implementing async commit
1128  *
1129  * For now the atomic helpers don't support async commit directly. If there is
1130  * real need it could be added though, using the dma-buf fence infrastructure
1131  * for generic synchronization with outstanding rendering.
1132  *
1133  * For now drivers have to implement async commit themselves, with the following
1134  * sequence being the recommended one:
1135  *
1136  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1137  * which commit needs to call which can fail, so we want to run it first and
1138  * synchronously.
1139  *
1140  * 2. Synchronize with any outstanding asynchronous commit worker threads which
1141  * might be affected the new state update. This can be done by either cancelling
1142  * or flushing the work items, depending upon whether the driver can deal with
1143  * cancelled updates. Note that it is important to ensure that the framebuffer
1144  * cleanup is still done when cancelling.
1145  *
1146  * For sufficient parallelism it is recommended to have a work item per crtc
1147  * (for updates which don't touch global state) and a global one. Then we only
1148  * need to synchronize with the crtc work items for changed crtcs and the global
1149  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1150  *
1151  * 3. The software state is updated synchronously with
1152  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
1153  * locks means concurrent callers never see inconsistent state. And doing this
1154  * while it's guaranteed that no relevant async worker runs means that async
1155  * workers do not need grab any locks. Actually they must not grab locks, for
1156  * otherwise the work flushing will deadlock.
1157  *
1158  * 4. Schedule a work item to do all subsequent steps, using the split-out
1159  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1160  * then cleaning up the framebuffers after the old framebuffer is no longer
1161  * being displayed.
1162  */
1163 
1164 /**
1165  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1166  * @dev: DRM device
1167  * @state: atomic state object with new state structures
1168  *
1169  * This function prepares plane state, specifically framebuffers, for the new
1170  * configuration. If any failure is encountered this function will call
1171  * ->cleanup_fb on any already successfully prepared framebuffer.
1172  *
1173  * Returns:
1174  * 0 on success, negative error code on failure.
1175  */
1176 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1177 				     struct drm_atomic_state *state)
1178 {
1179 	int nplanes = dev->mode_config.num_total_plane;
1180 	int ret, i;
1181 
1182 	for (i = 0; i < nplanes; i++) {
1183 		const struct drm_plane_helper_funcs *funcs;
1184 		struct drm_plane *plane = state->planes[i];
1185 		struct drm_plane_state *plane_state = state->plane_states[i];
1186 
1187 		if (!plane)
1188 			continue;
1189 
1190 		funcs = plane->helper_private;
1191 
1192 		if (funcs->prepare_fb) {
1193 			ret = funcs->prepare_fb(plane, plane_state);
1194 			if (ret)
1195 				goto fail;
1196 		}
1197 	}
1198 
1199 	return 0;
1200 
1201 fail:
1202 	for (i--; i >= 0; i--) {
1203 		const struct drm_plane_helper_funcs *funcs;
1204 		struct drm_plane *plane = state->planes[i];
1205 		struct drm_plane_state *plane_state = state->plane_states[i];
1206 
1207 		if (!plane)
1208 			continue;
1209 
1210 		funcs = plane->helper_private;
1211 
1212 		if (funcs->cleanup_fb)
1213 			funcs->cleanup_fb(plane, plane_state);
1214 
1215 	}
1216 
1217 	return ret;
1218 }
1219 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1220 
1221 bool plane_crtc_active(struct drm_plane_state *state)
1222 {
1223 	return state->crtc && state->crtc->state->active;
1224 }
1225 
1226 /**
1227  * drm_atomic_helper_commit_planes - commit plane state
1228  * @dev: DRM device
1229  * @old_state: atomic state object with old state structures
1230  * @active_only: Only commit on active CRTC if set
1231  *
1232  * This function commits the new plane state using the plane and atomic helper
1233  * functions for planes and crtcs. It assumes that the atomic state has already
1234  * been pushed into the relevant object state pointers, since this step can no
1235  * longer fail.
1236  *
1237  * It still requires the global state object @old_state to know which planes and
1238  * crtcs need to be updated though.
1239  *
1240  * Note that this function does all plane updates across all CRTCs in one step.
1241  * If the hardware can't support this approach look at
1242  * drm_atomic_helper_commit_planes_on_crtc() instead.
1243  *
1244  * Plane parameters can be updated by applications while the associated CRTC is
1245  * disabled. The DRM/KMS core will store the parameters in the plane state,
1246  * which will be available to the driver when the CRTC is turned on. As a result
1247  * most drivers don't need to be immediately notified of plane updates for a
1248  * disabled CRTC.
1249  *
1250  * Unless otherwise needed, drivers are advised to set the @active_only
1251  * parameters to true in order not to receive plane update notifications related
1252  * to a disabled CRTC. This avoids the need to manually ignore plane updates in
1253  * driver code when the driver and/or hardware can't or just don't need to deal
1254  * with updates on disabled CRTCs, for example when supporting runtime PM.
1255  *
1256  * The drm_atomic_helper_commit() default implementation only sets @active_only
1257  * to false to most closely match the behaviour of the legacy helpers. This should
1258  * not be copied blindly by drivers.
1259  */
1260 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1261 				     struct drm_atomic_state *old_state,
1262 				     bool active_only)
1263 {
1264 	struct drm_crtc *crtc;
1265 	struct drm_crtc_state *old_crtc_state;
1266 	struct drm_plane *plane;
1267 	struct drm_plane_state *old_plane_state;
1268 	int i;
1269 
1270 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1271 		const struct drm_crtc_helper_funcs *funcs;
1272 
1273 		funcs = crtc->helper_private;
1274 
1275 		if (!funcs || !funcs->atomic_begin)
1276 			continue;
1277 
1278 		if (active_only && !crtc->state->active)
1279 			continue;
1280 
1281 		funcs->atomic_begin(crtc, old_crtc_state);
1282 	}
1283 
1284 	for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1285 		const struct drm_plane_helper_funcs *funcs;
1286 		bool disabling;
1287 
1288 		funcs = plane->helper_private;
1289 
1290 		if (!funcs)
1291 			continue;
1292 
1293 		disabling = drm_atomic_plane_disabling(plane, old_plane_state);
1294 
1295 		if (active_only) {
1296 			/*
1297 			 * Skip planes related to inactive CRTCs. If the plane
1298 			 * is enabled use the state of the current CRTC. If the
1299 			 * plane is being disabled use the state of the old
1300 			 * CRTC to avoid skipping planes being disabled on an
1301 			 * active CRTC.
1302 			 */
1303 			if (!disabling && !plane_crtc_active(plane->state))
1304 				continue;
1305 			if (disabling && !plane_crtc_active(old_plane_state))
1306 				continue;
1307 		}
1308 
1309 		/*
1310 		 * Special-case disabling the plane if drivers support it.
1311 		 */
1312 		if (disabling && funcs->atomic_disable)
1313 			funcs->atomic_disable(plane, old_plane_state);
1314 		else if (plane->state->crtc || disabling)
1315 			funcs->atomic_update(plane, old_plane_state);
1316 	}
1317 
1318 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1319 		const struct drm_crtc_helper_funcs *funcs;
1320 
1321 		funcs = crtc->helper_private;
1322 
1323 		if (!funcs || !funcs->atomic_flush)
1324 			continue;
1325 
1326 		if (active_only && !crtc->state->active)
1327 			continue;
1328 
1329 		funcs->atomic_flush(crtc, old_crtc_state);
1330 	}
1331 }
1332 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1333 
1334 /**
1335  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1336  * @old_crtc_state: atomic state object with the old crtc state
1337  *
1338  * This function commits the new plane state using the plane and atomic helper
1339  * functions for planes on the specific crtc. It assumes that the atomic state
1340  * has already been pushed into the relevant object state pointers, since this
1341  * step can no longer fail.
1342  *
1343  * This function is useful when plane updates should be done crtc-by-crtc
1344  * instead of one global step like drm_atomic_helper_commit_planes() does.
1345  *
1346  * This function can only be savely used when planes are not allowed to move
1347  * between different CRTCs because this function doesn't handle inter-CRTC
1348  * depencies. Callers need to ensure that either no such depencies exist,
1349  * resolve them through ordering of commit calls or through some other means.
1350  */
1351 void
1352 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1353 {
1354 	const struct drm_crtc_helper_funcs *crtc_funcs;
1355 	struct drm_crtc *crtc = old_crtc_state->crtc;
1356 	struct drm_atomic_state *old_state = old_crtc_state->state;
1357 	struct drm_plane *plane;
1358 	unsigned plane_mask;
1359 
1360 	plane_mask = old_crtc_state->plane_mask;
1361 	plane_mask |= crtc->state->plane_mask;
1362 
1363 	crtc_funcs = crtc->helper_private;
1364 	if (crtc_funcs && crtc_funcs->atomic_begin)
1365 		crtc_funcs->atomic_begin(crtc, old_crtc_state);
1366 
1367 	drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1368 		struct drm_plane_state *old_plane_state =
1369 			drm_atomic_get_existing_plane_state(old_state, plane);
1370 		const struct drm_plane_helper_funcs *plane_funcs;
1371 
1372 		plane_funcs = plane->helper_private;
1373 
1374 		if (!old_plane_state || !plane_funcs)
1375 			continue;
1376 
1377 		WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1378 
1379 		if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1380 		    plane_funcs->atomic_disable)
1381 			plane_funcs->atomic_disable(plane, old_plane_state);
1382 		else if (plane->state->crtc ||
1383 			 drm_atomic_plane_disabling(plane, old_plane_state))
1384 			plane_funcs->atomic_update(plane, old_plane_state);
1385 	}
1386 
1387 	if (crtc_funcs && crtc_funcs->atomic_flush)
1388 		crtc_funcs->atomic_flush(crtc, old_crtc_state);
1389 }
1390 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1391 
1392 /**
1393  * drm_atomic_helper_disable_planes_on_crtc - helper to disable CRTC's planes
1394  * @crtc: CRTC
1395  * @atomic: if set, synchronize with CRTC's atomic_begin/flush hooks
1396  *
1397  * Disables all planes associated with the given CRTC. This can be
1398  * used for instance in the CRTC helper disable callback to disable
1399  * all planes before shutting down the display pipeline.
1400  *
1401  * If the atomic-parameter is set the function calls the CRTC's
1402  * atomic_begin hook before and atomic_flush hook after disabling the
1403  * planes.
1404  *
1405  * It is a bug to call this function without having implemented the
1406  * ->atomic_disable() plane hook.
1407  */
1408 void drm_atomic_helper_disable_planes_on_crtc(struct drm_crtc *crtc,
1409 					      bool atomic)
1410 {
1411 	const struct drm_crtc_helper_funcs *crtc_funcs =
1412 		crtc->helper_private;
1413 	struct drm_plane *plane;
1414 
1415 	if (atomic && crtc_funcs && crtc_funcs->atomic_begin)
1416 		crtc_funcs->atomic_begin(crtc, NULL);
1417 
1418 	drm_for_each_plane(plane, crtc->dev) {
1419 		const struct drm_plane_helper_funcs *plane_funcs =
1420 			plane->helper_private;
1421 
1422 		if (plane->state->crtc != crtc || !plane_funcs)
1423 			continue;
1424 
1425 		WARN_ON(!plane_funcs->atomic_disable);
1426 		if (plane_funcs->atomic_disable)
1427 			plane_funcs->atomic_disable(plane, NULL);
1428 	}
1429 
1430 	if (atomic && crtc_funcs && crtc_funcs->atomic_flush)
1431 		crtc_funcs->atomic_flush(crtc, NULL);
1432 }
1433 EXPORT_SYMBOL(drm_atomic_helper_disable_planes_on_crtc);
1434 
1435 /**
1436  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1437  * @dev: DRM device
1438  * @old_state: atomic state object with old state structures
1439  *
1440  * This function cleans up plane state, specifically framebuffers, from the old
1441  * configuration. Hence the old configuration must be perserved in @old_state to
1442  * be able to call this function.
1443  *
1444  * This function must also be called on the new state when the atomic update
1445  * fails at any point after calling drm_atomic_helper_prepare_planes().
1446  */
1447 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1448 				      struct drm_atomic_state *old_state)
1449 {
1450 	struct drm_plane *plane;
1451 	struct drm_plane_state *plane_state;
1452 	int i;
1453 
1454 	for_each_plane_in_state(old_state, plane, plane_state, i) {
1455 		const struct drm_plane_helper_funcs *funcs;
1456 
1457 		funcs = plane->helper_private;
1458 
1459 		if (funcs->cleanup_fb)
1460 			funcs->cleanup_fb(plane, plane_state);
1461 	}
1462 }
1463 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1464 
1465 /**
1466  * drm_atomic_helper_swap_state - store atomic state into current sw state
1467  * @dev: DRM device
1468  * @state: atomic state
1469  *
1470  * This function stores the atomic state into the current state pointers in all
1471  * driver objects. It should be called after all failing steps have been done
1472  * and succeeded, but before the actual hardware state is committed.
1473  *
1474  * For cleanup and error recovery the current state for all changed objects will
1475  * be swaped into @state.
1476  *
1477  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1478  *
1479  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1480  *
1481  * 2. Do any other steps that might fail.
1482  *
1483  * 3. Put the staged state into the current state pointers with this function.
1484  *
1485  * 4. Actually commit the hardware state.
1486  *
1487  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
1488  * contains the old state. Also do any other cleanup required with that state.
1489  */
1490 void drm_atomic_helper_swap_state(struct drm_device *dev,
1491 				  struct drm_atomic_state *state)
1492 {
1493 	int i;
1494 
1495 	for (i = 0; i < dev->mode_config.num_connector; i++) {
1496 		struct drm_connector *connector = state->connectors[i];
1497 
1498 		if (!connector)
1499 			continue;
1500 
1501 		connector->state->state = state;
1502 		swap(state->connector_states[i], connector->state);
1503 		connector->state->state = NULL;
1504 	}
1505 
1506 	for (i = 0; i < dev->mode_config.num_crtc; i++) {
1507 		struct drm_crtc *crtc = state->crtcs[i];
1508 
1509 		if (!crtc)
1510 			continue;
1511 
1512 		crtc->state->state = state;
1513 		swap(state->crtc_states[i], crtc->state);
1514 		crtc->state->state = NULL;
1515 	}
1516 
1517 	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1518 		struct drm_plane *plane = state->planes[i];
1519 
1520 		if (!plane)
1521 			continue;
1522 
1523 		plane->state->state = state;
1524 		swap(state->plane_states[i], plane->state);
1525 		plane->state->state = NULL;
1526 	}
1527 }
1528 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1529 
1530 /**
1531  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1532  * @plane: plane object to update
1533  * @crtc: owning CRTC of owning plane
1534  * @fb: framebuffer to flip onto plane
1535  * @crtc_x: x offset of primary plane on crtc
1536  * @crtc_y: y offset of primary plane on crtc
1537  * @crtc_w: width of primary plane rectangle on crtc
1538  * @crtc_h: height of primary plane rectangle on crtc
1539  * @src_x: x offset of @fb for panning
1540  * @src_y: y offset of @fb for panning
1541  * @src_w: width of source rectangle in @fb
1542  * @src_h: height of source rectangle in @fb
1543  *
1544  * Provides a default plane update handler using the atomic driver interface.
1545  *
1546  * RETURNS:
1547  * Zero on success, error code on failure
1548  */
1549 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1550 				   struct drm_crtc *crtc,
1551 				   struct drm_framebuffer *fb,
1552 				   int crtc_x, int crtc_y,
1553 				   unsigned int crtc_w, unsigned int crtc_h,
1554 				   uint32_t src_x, uint32_t src_y,
1555 				   uint32_t src_w, uint32_t src_h)
1556 {
1557 	struct drm_atomic_state *state;
1558 	struct drm_plane_state *plane_state;
1559 	int ret = 0;
1560 
1561 	state = drm_atomic_state_alloc(plane->dev);
1562 	if (!state)
1563 		return -ENOMEM;
1564 
1565 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1566 retry:
1567 	plane_state = drm_atomic_get_plane_state(state, plane);
1568 	if (IS_ERR(plane_state)) {
1569 		ret = PTR_ERR(plane_state);
1570 		goto fail;
1571 	}
1572 
1573 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1574 	if (ret != 0)
1575 		goto fail;
1576 	drm_atomic_set_fb_for_plane(plane_state, fb);
1577 	plane_state->crtc_x = crtc_x;
1578 	plane_state->crtc_y = crtc_y;
1579 	plane_state->crtc_w = crtc_w;
1580 	plane_state->crtc_h = crtc_h;
1581 	plane_state->src_x = src_x;
1582 	plane_state->src_y = src_y;
1583 	plane_state->src_w = src_w;
1584 	plane_state->src_h = src_h;
1585 
1586 	if (plane == crtc->cursor)
1587 		state->legacy_cursor_update = true;
1588 
1589 	ret = drm_atomic_commit(state);
1590 	if (ret != 0)
1591 		goto fail;
1592 
1593 	/* Driver takes ownership of state on successful commit. */
1594 	return 0;
1595 fail:
1596 	if (ret == -EDEADLK)
1597 		goto backoff;
1598 
1599 	drm_atomic_state_free(state);
1600 
1601 	return ret;
1602 backoff:
1603 	drm_atomic_state_clear(state);
1604 	drm_atomic_legacy_backoff(state);
1605 
1606 	/*
1607 	 * Someone might have exchanged the framebuffer while we dropped locks
1608 	 * in the backoff code. We need to fix up the fb refcount tracking the
1609 	 * core does for us.
1610 	 */
1611 	plane->old_fb = plane->fb;
1612 
1613 	goto retry;
1614 }
1615 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1616 
1617 /**
1618  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1619  * @plane: plane to disable
1620  *
1621  * Provides a default plane disable handler using the atomic driver interface.
1622  *
1623  * RETURNS:
1624  * Zero on success, error code on failure
1625  */
1626 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1627 {
1628 	struct drm_atomic_state *state;
1629 	struct drm_plane_state *plane_state;
1630 	int ret = 0;
1631 
1632 	/*
1633 	 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1634 	 * acquire context. The real fix will be to wire the acquire ctx through
1635 	 * everywhere we need it, but meanwhile prevent chaos by just skipping
1636 	 * this noop. The critical case is the cursor ioctls which a) only grab
1637 	 * crtc/cursor-plane locks (so we need the crtc to get at the right
1638 	 * acquire context) and b) can try to disable the plane multiple times.
1639 	 */
1640 	if (!plane->crtc)
1641 		return 0;
1642 
1643 	state = drm_atomic_state_alloc(plane->dev);
1644 	if (!state)
1645 		return -ENOMEM;
1646 
1647 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1648 retry:
1649 	plane_state = drm_atomic_get_plane_state(state, plane);
1650 	if (IS_ERR(plane_state)) {
1651 		ret = PTR_ERR(plane_state);
1652 		goto fail;
1653 	}
1654 
1655 	if (plane_state->crtc && (plane == plane->crtc->cursor))
1656 		plane_state->state->legacy_cursor_update = true;
1657 
1658 	ret = __drm_atomic_helper_disable_plane(plane, plane_state);
1659 	if (ret != 0)
1660 		goto fail;
1661 
1662 	ret = drm_atomic_commit(state);
1663 	if (ret != 0)
1664 		goto fail;
1665 
1666 	/* Driver takes ownership of state on successful commit. */
1667 	return 0;
1668 fail:
1669 	if (ret == -EDEADLK)
1670 		goto backoff;
1671 
1672 	drm_atomic_state_free(state);
1673 
1674 	return ret;
1675 backoff:
1676 	drm_atomic_state_clear(state);
1677 	drm_atomic_legacy_backoff(state);
1678 
1679 	/*
1680 	 * Someone might have exchanged the framebuffer while we dropped locks
1681 	 * in the backoff code. We need to fix up the fb refcount tracking the
1682 	 * core does for us.
1683 	 */
1684 	plane->old_fb = plane->fb;
1685 
1686 	goto retry;
1687 }
1688 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1689 
1690 /* just used from fb-helper and atomic-helper: */
1691 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
1692 		struct drm_plane_state *plane_state)
1693 {
1694 	int ret;
1695 
1696 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1697 	if (ret != 0)
1698 		return ret;
1699 
1700 	drm_atomic_set_fb_for_plane(plane_state, NULL);
1701 	plane_state->crtc_x = 0;
1702 	plane_state->crtc_y = 0;
1703 	plane_state->crtc_w = 0;
1704 	plane_state->crtc_h = 0;
1705 	plane_state->src_x = 0;
1706 	plane_state->src_y = 0;
1707 	plane_state->src_w = 0;
1708 	plane_state->src_h = 0;
1709 
1710 	return 0;
1711 }
1712 
1713 static int update_output_state(struct drm_atomic_state *state,
1714 			       struct drm_mode_set *set)
1715 {
1716 	struct drm_device *dev = set->crtc->dev;
1717 	struct drm_crtc *crtc;
1718 	struct drm_crtc_state *crtc_state;
1719 	struct drm_connector *connector;
1720 	struct drm_connector_state *conn_state;
1721 	int ret, i, j;
1722 
1723 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1724 			       state->acquire_ctx);
1725 	if (ret)
1726 		return ret;
1727 
1728 	/* First grab all affected connector/crtc states. */
1729 	for (i = 0; i < set->num_connectors; i++) {
1730 		conn_state = drm_atomic_get_connector_state(state,
1731 							    set->connectors[i]);
1732 		if (IS_ERR(conn_state))
1733 			return PTR_ERR(conn_state);
1734 	}
1735 
1736 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1737 		ret = drm_atomic_add_affected_connectors(state, crtc);
1738 		if (ret)
1739 			return ret;
1740 	}
1741 
1742 	/* Then recompute connector->crtc links and crtc enabling state. */
1743 	for_each_connector_in_state(state, connector, conn_state, i) {
1744 		if (conn_state->crtc == set->crtc) {
1745 			ret = drm_atomic_set_crtc_for_connector(conn_state,
1746 								NULL);
1747 			if (ret)
1748 				return ret;
1749 		}
1750 
1751 		for (j = 0; j < set->num_connectors; j++) {
1752 			if (set->connectors[j] == connector) {
1753 				ret = drm_atomic_set_crtc_for_connector(conn_state,
1754 									set->crtc);
1755 				if (ret)
1756 					return ret;
1757 				break;
1758 			}
1759 		}
1760 	}
1761 
1762 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1763 		/* Don't update ->enable for the CRTC in the set_config request,
1764 		 * since a mismatch would indicate a bug in the upper layers.
1765 		 * The actual modeset code later on will catch any
1766 		 * inconsistencies here. */
1767 		if (crtc == set->crtc)
1768 			continue;
1769 
1770 		if (!crtc_state->connector_mask) {
1771 			ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1772 								NULL);
1773 			if (ret < 0)
1774 				return ret;
1775 
1776 			crtc_state->active = false;
1777 		}
1778 	}
1779 
1780 	return 0;
1781 }
1782 
1783 /**
1784  * drm_atomic_helper_set_config - set a new config from userspace
1785  * @set: mode set configuration
1786  *
1787  * Provides a default crtc set_config handler using the atomic driver interface.
1788  *
1789  * Returns:
1790  * Returns 0 on success, negative errno numbers on failure.
1791  */
1792 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1793 {
1794 	struct drm_atomic_state *state;
1795 	struct drm_crtc *crtc = set->crtc;
1796 	int ret = 0;
1797 
1798 	state = drm_atomic_state_alloc(crtc->dev);
1799 	if (!state)
1800 		return -ENOMEM;
1801 
1802 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1803 retry:
1804 	ret = __drm_atomic_helper_set_config(set, state);
1805 	if (ret != 0)
1806 		goto fail;
1807 
1808 	ret = drm_atomic_commit(state);
1809 	if (ret != 0)
1810 		goto fail;
1811 
1812 	/* Driver takes ownership of state on successful commit. */
1813 	return 0;
1814 fail:
1815 	if (ret == -EDEADLK)
1816 		goto backoff;
1817 
1818 	drm_atomic_state_free(state);
1819 
1820 	return ret;
1821 backoff:
1822 	drm_atomic_state_clear(state);
1823 	drm_atomic_legacy_backoff(state);
1824 
1825 	/*
1826 	 * Someone might have exchanged the framebuffer while we dropped locks
1827 	 * in the backoff code. We need to fix up the fb refcount tracking the
1828 	 * core does for us.
1829 	 */
1830 	crtc->primary->old_fb = crtc->primary->fb;
1831 
1832 	goto retry;
1833 }
1834 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1835 
1836 /* just used from fb-helper and atomic-helper: */
1837 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
1838 		struct drm_atomic_state *state)
1839 {
1840 	struct drm_crtc_state *crtc_state;
1841 	struct drm_plane_state *primary_state;
1842 	struct drm_crtc *crtc = set->crtc;
1843 	int hdisplay, vdisplay;
1844 	int ret;
1845 
1846 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1847 	if (IS_ERR(crtc_state))
1848 		return PTR_ERR(crtc_state);
1849 
1850 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1851 	if (IS_ERR(primary_state))
1852 		return PTR_ERR(primary_state);
1853 
1854 	if (!set->mode) {
1855 		WARN_ON(set->fb);
1856 		WARN_ON(set->num_connectors);
1857 
1858 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1859 		if (ret != 0)
1860 			return ret;
1861 
1862 		crtc_state->active = false;
1863 
1864 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1865 		if (ret != 0)
1866 			return ret;
1867 
1868 		drm_atomic_set_fb_for_plane(primary_state, NULL);
1869 
1870 		goto commit;
1871 	}
1872 
1873 	WARN_ON(!set->fb);
1874 	WARN_ON(!set->num_connectors);
1875 
1876 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1877 	if (ret != 0)
1878 		return ret;
1879 
1880 	crtc_state->active = true;
1881 
1882 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1883 	if (ret != 0)
1884 		return ret;
1885 
1886 	drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
1887 
1888 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
1889 	primary_state->crtc_x = 0;
1890 	primary_state->crtc_y = 0;
1891 	primary_state->crtc_w = hdisplay;
1892 	primary_state->crtc_h = vdisplay;
1893 	primary_state->src_x = set->x << 16;
1894 	primary_state->src_y = set->y << 16;
1895 	if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
1896 		primary_state->src_w = vdisplay << 16;
1897 		primary_state->src_h = hdisplay << 16;
1898 	} else {
1899 		primary_state->src_w = hdisplay << 16;
1900 		primary_state->src_h = vdisplay << 16;
1901 	}
1902 
1903 commit:
1904 	ret = update_output_state(state, set);
1905 	if (ret)
1906 		return ret;
1907 
1908 	return 0;
1909 }
1910 
1911 /**
1912  * drm_atomic_helper_disable_all - disable all currently active outputs
1913  * @dev: DRM device
1914  * @ctx: lock acquisition context
1915  *
1916  * Loops through all connectors, finding those that aren't turned off and then
1917  * turns them off by setting their DPMS mode to OFF and deactivating the CRTC
1918  * that they are connected to.
1919  *
1920  * This is used for example in suspend/resume to disable all currently active
1921  * functions when suspending.
1922  *
1923  * Note that if callers haven't already acquired all modeset locks this might
1924  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
1925  *
1926  * Returns:
1927  * 0 on success or a negative error code on failure.
1928  *
1929  * See also:
1930  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
1931  */
1932 int drm_atomic_helper_disable_all(struct drm_device *dev,
1933 				  struct drm_modeset_acquire_ctx *ctx)
1934 {
1935 	struct drm_atomic_state *state;
1936 	struct drm_connector *conn;
1937 	int err;
1938 
1939 	state = drm_atomic_state_alloc(dev);
1940 	if (!state)
1941 		return -ENOMEM;
1942 
1943 	state->acquire_ctx = ctx;
1944 
1945 	drm_for_each_connector(conn, dev) {
1946 		struct drm_crtc *crtc = conn->state->crtc;
1947 		struct drm_crtc_state *crtc_state;
1948 
1949 		if (!crtc || conn->dpms != DRM_MODE_DPMS_ON)
1950 			continue;
1951 
1952 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1953 		if (IS_ERR(crtc_state)) {
1954 			err = PTR_ERR(crtc_state);
1955 			goto free;
1956 		}
1957 
1958 		crtc_state->active = false;
1959 	}
1960 
1961 	err = drm_atomic_commit(state);
1962 
1963 free:
1964 	if (err < 0)
1965 		drm_atomic_state_free(state);
1966 
1967 	return err;
1968 }
1969 EXPORT_SYMBOL(drm_atomic_helper_disable_all);
1970 
1971 /**
1972  * drm_atomic_helper_suspend - subsystem-level suspend helper
1973  * @dev: DRM device
1974  *
1975  * Duplicates the current atomic state, disables all active outputs and then
1976  * returns a pointer to the original atomic state to the caller. Drivers can
1977  * pass this pointer to the drm_atomic_helper_resume() helper upon resume to
1978  * restore the output configuration that was active at the time the system
1979  * entered suspend.
1980  *
1981  * Note that it is potentially unsafe to use this. The atomic state object
1982  * returned by this function is assumed to be persistent. Drivers must ensure
1983  * that this holds true. Before calling this function, drivers must make sure
1984  * to suspend fbdev emulation so that nothing can be using the device.
1985  *
1986  * Returns:
1987  * A pointer to a copy of the state before suspend on success or an ERR_PTR()-
1988  * encoded error code on failure. Drivers should store the returned atomic
1989  * state object and pass it to the drm_atomic_helper_resume() helper upon
1990  * resume.
1991  *
1992  * See also:
1993  * drm_atomic_helper_duplicate_state(), drm_atomic_helper_disable_all(),
1994  * drm_atomic_helper_resume()
1995  */
1996 struct drm_atomic_state *drm_atomic_helper_suspend(struct drm_device *dev)
1997 {
1998 	struct drm_modeset_acquire_ctx ctx;
1999 	struct drm_atomic_state *state;
2000 	int err;
2001 
2002 	drm_modeset_acquire_init(&ctx, 0);
2003 
2004 retry:
2005 	err = drm_modeset_lock_all_ctx(dev, &ctx);
2006 	if (err < 0) {
2007 		state = ERR_PTR(err);
2008 		goto unlock;
2009 	}
2010 
2011 	state = drm_atomic_helper_duplicate_state(dev, &ctx);
2012 	if (IS_ERR(state))
2013 		goto unlock;
2014 
2015 	err = drm_atomic_helper_disable_all(dev, &ctx);
2016 	if (err < 0) {
2017 		drm_atomic_state_free(state);
2018 		state = ERR_PTR(err);
2019 		goto unlock;
2020 	}
2021 
2022 unlock:
2023 	if (PTR_ERR(state) == -EDEADLK) {
2024 		drm_modeset_backoff(&ctx);
2025 		goto retry;
2026 	}
2027 
2028 	drm_modeset_drop_locks(&ctx);
2029 	drm_modeset_acquire_fini(&ctx);
2030 	return state;
2031 }
2032 EXPORT_SYMBOL(drm_atomic_helper_suspend);
2033 
2034 /**
2035  * drm_atomic_helper_resume - subsystem-level resume helper
2036  * @dev: DRM device
2037  * @state: atomic state to resume to
2038  *
2039  * Calls drm_mode_config_reset() to synchronize hardware and software states,
2040  * grabs all modeset locks and commits the atomic state object. This can be
2041  * used in conjunction with the drm_atomic_helper_suspend() helper to
2042  * implement suspend/resume for drivers that support atomic mode-setting.
2043  *
2044  * Returns:
2045  * 0 on success or a negative error code on failure.
2046  *
2047  * See also:
2048  * drm_atomic_helper_suspend()
2049  */
2050 int drm_atomic_helper_resume(struct drm_device *dev,
2051 			     struct drm_atomic_state *state)
2052 {
2053 	struct drm_mode_config *config = &dev->mode_config;
2054 	int err;
2055 
2056 	drm_mode_config_reset(dev);
2057 	drm_modeset_lock_all(dev);
2058 	state->acquire_ctx = config->acquire_ctx;
2059 	err = drm_atomic_commit(state);
2060 	drm_modeset_unlock_all(dev);
2061 
2062 	return err;
2063 }
2064 EXPORT_SYMBOL(drm_atomic_helper_resume);
2065 
2066 /**
2067  * drm_atomic_helper_crtc_set_property - helper for crtc properties
2068  * @crtc: DRM crtc
2069  * @property: DRM property
2070  * @val: value of property
2071  *
2072  * Provides a default crtc set_property handler using the atomic driver
2073  * interface.
2074  *
2075  * RETURNS:
2076  * Zero on success, error code on failure
2077  */
2078 int
2079 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
2080 				    struct drm_property *property,
2081 				    uint64_t val)
2082 {
2083 	struct drm_atomic_state *state;
2084 	struct drm_crtc_state *crtc_state;
2085 	int ret = 0;
2086 
2087 	state = drm_atomic_state_alloc(crtc->dev);
2088 	if (!state)
2089 		return -ENOMEM;
2090 
2091 	/* ->set_property is always called with all locks held. */
2092 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
2093 retry:
2094 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2095 	if (IS_ERR(crtc_state)) {
2096 		ret = PTR_ERR(crtc_state);
2097 		goto fail;
2098 	}
2099 
2100 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
2101 			property, val);
2102 	if (ret)
2103 		goto fail;
2104 
2105 	ret = drm_atomic_commit(state);
2106 	if (ret != 0)
2107 		goto fail;
2108 
2109 	/* Driver takes ownership of state on successful commit. */
2110 	return 0;
2111 fail:
2112 	if (ret == -EDEADLK)
2113 		goto backoff;
2114 
2115 	drm_atomic_state_free(state);
2116 
2117 	return ret;
2118 backoff:
2119 	drm_atomic_state_clear(state);
2120 	drm_atomic_legacy_backoff(state);
2121 
2122 	goto retry;
2123 }
2124 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
2125 
2126 /**
2127  * drm_atomic_helper_plane_set_property - helper for plane properties
2128  * @plane: DRM plane
2129  * @property: DRM property
2130  * @val: value of property
2131  *
2132  * Provides a default plane set_property handler using the atomic driver
2133  * interface.
2134  *
2135  * RETURNS:
2136  * Zero on success, error code on failure
2137  */
2138 int
2139 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
2140 				    struct drm_property *property,
2141 				    uint64_t val)
2142 {
2143 	struct drm_atomic_state *state;
2144 	struct drm_plane_state *plane_state;
2145 	int ret = 0;
2146 
2147 	state = drm_atomic_state_alloc(plane->dev);
2148 	if (!state)
2149 		return -ENOMEM;
2150 
2151 	/* ->set_property is always called with all locks held. */
2152 	state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
2153 retry:
2154 	plane_state = drm_atomic_get_plane_state(state, plane);
2155 	if (IS_ERR(plane_state)) {
2156 		ret = PTR_ERR(plane_state);
2157 		goto fail;
2158 	}
2159 
2160 	ret = drm_atomic_plane_set_property(plane, plane_state,
2161 			property, val);
2162 	if (ret)
2163 		goto fail;
2164 
2165 	ret = drm_atomic_commit(state);
2166 	if (ret != 0)
2167 		goto fail;
2168 
2169 	/* Driver takes ownership of state on successful commit. */
2170 	return 0;
2171 fail:
2172 	if (ret == -EDEADLK)
2173 		goto backoff;
2174 
2175 	drm_atomic_state_free(state);
2176 
2177 	return ret;
2178 backoff:
2179 	drm_atomic_state_clear(state);
2180 	drm_atomic_legacy_backoff(state);
2181 
2182 	goto retry;
2183 }
2184 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
2185 
2186 /**
2187  * drm_atomic_helper_connector_set_property - helper for connector properties
2188  * @connector: DRM connector
2189  * @property: DRM property
2190  * @val: value of property
2191  *
2192  * Provides a default connector set_property handler using the atomic driver
2193  * interface.
2194  *
2195  * RETURNS:
2196  * Zero on success, error code on failure
2197  */
2198 int
2199 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
2200 				    struct drm_property *property,
2201 				    uint64_t val)
2202 {
2203 	struct drm_atomic_state *state;
2204 	struct drm_connector_state *connector_state;
2205 	int ret = 0;
2206 
2207 	state = drm_atomic_state_alloc(connector->dev);
2208 	if (!state)
2209 		return -ENOMEM;
2210 
2211 	/* ->set_property is always called with all locks held. */
2212 	state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
2213 retry:
2214 	connector_state = drm_atomic_get_connector_state(state, connector);
2215 	if (IS_ERR(connector_state)) {
2216 		ret = PTR_ERR(connector_state);
2217 		goto fail;
2218 	}
2219 
2220 	ret = drm_atomic_connector_set_property(connector, connector_state,
2221 			property, val);
2222 	if (ret)
2223 		goto fail;
2224 
2225 	ret = drm_atomic_commit(state);
2226 	if (ret != 0)
2227 		goto fail;
2228 
2229 	/* Driver takes ownership of state on successful commit. */
2230 	return 0;
2231 fail:
2232 	if (ret == -EDEADLK)
2233 		goto backoff;
2234 
2235 	drm_atomic_state_free(state);
2236 
2237 	return ret;
2238 backoff:
2239 	drm_atomic_state_clear(state);
2240 	drm_atomic_legacy_backoff(state);
2241 
2242 	goto retry;
2243 }
2244 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
2245 
2246 /**
2247  * drm_atomic_helper_page_flip - execute a legacy page flip
2248  * @crtc: DRM crtc
2249  * @fb: DRM framebuffer
2250  * @event: optional DRM event to signal upon completion
2251  * @flags: flip flags for non-vblank sync'ed updates
2252  *
2253  * Provides a default page flip implementation using the atomic driver interface.
2254  *
2255  * Note that for now so called async page flips (i.e. updates which are not
2256  * synchronized to vblank) are not supported, since the atomic interfaces have
2257  * no provisions for this yet.
2258  *
2259  * Returns:
2260  * Returns 0 on success, negative errno numbers on failure.
2261  */
2262 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
2263 				struct drm_framebuffer *fb,
2264 				struct drm_pending_vblank_event *event,
2265 				uint32_t flags)
2266 {
2267 	struct drm_plane *plane = crtc->primary;
2268 	struct drm_atomic_state *state;
2269 	struct drm_plane_state *plane_state;
2270 	struct drm_crtc_state *crtc_state;
2271 	int ret = 0;
2272 
2273 	if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
2274 		return -EINVAL;
2275 
2276 	state = drm_atomic_state_alloc(plane->dev);
2277 	if (!state)
2278 		return -ENOMEM;
2279 
2280 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2281 retry:
2282 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2283 	if (IS_ERR(crtc_state)) {
2284 		ret = PTR_ERR(crtc_state);
2285 		goto fail;
2286 	}
2287 	crtc_state->event = event;
2288 
2289 	plane_state = drm_atomic_get_plane_state(state, plane);
2290 	if (IS_ERR(plane_state)) {
2291 		ret = PTR_ERR(plane_state);
2292 		goto fail;
2293 	}
2294 
2295 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
2296 	if (ret != 0)
2297 		goto fail;
2298 	drm_atomic_set_fb_for_plane(plane_state, fb);
2299 
2300 	/* Make sure we don't accidentally do a full modeset. */
2301 	state->allow_modeset = false;
2302 	if (!crtc_state->active) {
2303 		DRM_DEBUG_ATOMIC("[CRTC:%d] disabled, rejecting legacy flip\n",
2304 				 crtc->base.id);
2305 		ret = -EINVAL;
2306 		goto fail;
2307 	}
2308 
2309 	ret = drm_atomic_async_commit(state);
2310 	if (ret != 0)
2311 		goto fail;
2312 
2313 	/* Driver takes ownership of state on successful async commit. */
2314 	return 0;
2315 fail:
2316 	if (ret == -EDEADLK)
2317 		goto backoff;
2318 
2319 	drm_atomic_state_free(state);
2320 
2321 	return ret;
2322 backoff:
2323 	drm_atomic_state_clear(state);
2324 	drm_atomic_legacy_backoff(state);
2325 
2326 	/*
2327 	 * Someone might have exchanged the framebuffer while we dropped locks
2328 	 * in the backoff code. We need to fix up the fb refcount tracking the
2329 	 * core does for us.
2330 	 */
2331 	plane->old_fb = plane->fb;
2332 
2333 	goto retry;
2334 }
2335 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
2336 
2337 /**
2338  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
2339  * @connector: affected connector
2340  * @mode: DPMS mode
2341  *
2342  * This is the main helper function provided by the atomic helper framework for
2343  * implementing the legacy DPMS connector interface. It computes the new desired
2344  * ->active state for the corresponding CRTC (if the connector is enabled) and
2345  *  updates it.
2346  *
2347  * Returns:
2348  * Returns 0 on success, negative errno numbers on failure.
2349  */
2350 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2351 				     int mode)
2352 {
2353 	struct drm_mode_config *config = &connector->dev->mode_config;
2354 	struct drm_atomic_state *state;
2355 	struct drm_crtc_state *crtc_state;
2356 	struct drm_crtc *crtc;
2357 	struct drm_connector *tmp_connector;
2358 	int ret;
2359 	bool active = false;
2360 	int old_mode = connector->dpms;
2361 
2362 	if (mode != DRM_MODE_DPMS_ON)
2363 		mode = DRM_MODE_DPMS_OFF;
2364 
2365 	connector->dpms = mode;
2366 	crtc = connector->state->crtc;
2367 
2368 	if (!crtc)
2369 		return 0;
2370 
2371 	state = drm_atomic_state_alloc(connector->dev);
2372 	if (!state)
2373 		return -ENOMEM;
2374 
2375 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2376 retry:
2377 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2378 	if (IS_ERR(crtc_state)) {
2379 		ret = PTR_ERR(crtc_state);
2380 		goto fail;
2381 	}
2382 
2383 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2384 
2385 	drm_for_each_connector(tmp_connector, connector->dev) {
2386 		if (tmp_connector->state->crtc != crtc)
2387 			continue;
2388 
2389 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2390 			active = true;
2391 			break;
2392 		}
2393 	}
2394 	crtc_state->active = active;
2395 
2396 	ret = drm_atomic_commit(state);
2397 	if (ret != 0)
2398 		goto fail;
2399 
2400 	/* Driver takes ownership of state on successful commit. */
2401 	return 0;
2402 fail:
2403 	if (ret == -EDEADLK)
2404 		goto backoff;
2405 
2406 	connector->dpms = old_mode;
2407 	drm_atomic_state_free(state);
2408 
2409 	return ret;
2410 backoff:
2411 	drm_atomic_state_clear(state);
2412 	drm_atomic_legacy_backoff(state);
2413 
2414 	goto retry;
2415 }
2416 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2417 
2418 /**
2419  * DOC: atomic state reset and initialization
2420  *
2421  * Both the drm core and the atomic helpers assume that there is always the full
2422  * and correct atomic software state for all connectors, CRTCs and planes
2423  * available. Which is a bit a problem on driver load and also after system
2424  * suspend. One way to solve this is to have a hardware state read-out
2425  * infrastructure which reconstructs the full software state (e.g. the i915
2426  * driver).
2427  *
2428  * The simpler solution is to just reset the software state to everything off,
2429  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2430  * the atomic helpers provide default reset implementations for all hooks.
2431  *
2432  * On the upside the precise state tracking of atomic simplifies system suspend
2433  * and resume a lot. For drivers using drm_mode_config_reset() a complete recipe
2434  * is implemented in drm_atomic_helper_suspend() and drm_atomic_helper_resume().
2435  * For other drivers the building blocks are split out, see the documentation
2436  * for these functions.
2437  */
2438 
2439 /**
2440  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2441  * @crtc: drm CRTC
2442  *
2443  * Resets the atomic state for @crtc by freeing the state pointer (which might
2444  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2445  */
2446 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2447 {
2448 	if (crtc->state)
2449 		drm_property_unreference_blob(crtc->state->mode_blob);
2450 	kfree(crtc->state);
2451 	crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2452 
2453 	if (crtc->state)
2454 		crtc->state->crtc = crtc;
2455 }
2456 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2457 
2458 /**
2459  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2460  * @crtc: CRTC object
2461  * @state: atomic CRTC state
2462  *
2463  * Copies atomic state from a CRTC's current state and resets inferred values.
2464  * This is useful for drivers that subclass the CRTC state.
2465  */
2466 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2467 					      struct drm_crtc_state *state)
2468 {
2469 	memcpy(state, crtc->state, sizeof(*state));
2470 
2471 	if (state->mode_blob)
2472 		drm_property_reference_blob(state->mode_blob);
2473 	state->mode_changed = false;
2474 	state->active_changed = false;
2475 	state->planes_changed = false;
2476 	state->connectors_changed = false;
2477 	state->event = NULL;
2478 }
2479 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2480 
2481 /**
2482  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2483  * @crtc: drm CRTC
2484  *
2485  * Default CRTC state duplicate hook for drivers which don't have their own
2486  * subclassed CRTC state structure.
2487  */
2488 struct drm_crtc_state *
2489 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2490 {
2491 	struct drm_crtc_state *state;
2492 
2493 	if (WARN_ON(!crtc->state))
2494 		return NULL;
2495 
2496 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2497 	if (state)
2498 		__drm_atomic_helper_crtc_duplicate_state(crtc, state);
2499 
2500 	return state;
2501 }
2502 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2503 
2504 /**
2505  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2506  * @crtc: CRTC object
2507  * @state: CRTC state object to release
2508  *
2509  * Releases all resources stored in the CRTC state without actually freeing
2510  * the memory of the CRTC state. This is useful for drivers that subclass the
2511  * CRTC state.
2512  */
2513 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2514 					    struct drm_crtc_state *state)
2515 {
2516 	drm_property_unreference_blob(state->mode_blob);
2517 }
2518 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2519 
2520 /**
2521  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2522  * @crtc: drm CRTC
2523  * @state: CRTC state object to release
2524  *
2525  * Default CRTC state destroy hook for drivers which don't have their own
2526  * subclassed CRTC state structure.
2527  */
2528 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2529 					  struct drm_crtc_state *state)
2530 {
2531 	__drm_atomic_helper_crtc_destroy_state(crtc, state);
2532 	kfree(state);
2533 }
2534 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2535 
2536 /**
2537  * drm_atomic_helper_plane_reset - default ->reset hook for planes
2538  * @plane: drm plane
2539  *
2540  * Resets the atomic state for @plane by freeing the state pointer (which might
2541  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2542  */
2543 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2544 {
2545 	if (plane->state && plane->state->fb)
2546 		drm_framebuffer_unreference(plane->state->fb);
2547 
2548 	kfree(plane->state);
2549 	plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2550 
2551 	if (plane->state) {
2552 		plane->state->plane = plane;
2553 		plane->state->rotation = BIT(DRM_ROTATE_0);
2554 	}
2555 }
2556 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2557 
2558 /**
2559  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2560  * @plane: plane object
2561  * @state: atomic plane state
2562  *
2563  * Copies atomic state from a plane's current state. This is useful for
2564  * drivers that subclass the plane state.
2565  */
2566 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2567 					       struct drm_plane_state *state)
2568 {
2569 	memcpy(state, plane->state, sizeof(*state));
2570 
2571 	if (state->fb)
2572 		drm_framebuffer_reference(state->fb);
2573 }
2574 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2575 
2576 /**
2577  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2578  * @plane: drm plane
2579  *
2580  * Default plane state duplicate hook for drivers which don't have their own
2581  * subclassed plane state structure.
2582  */
2583 struct drm_plane_state *
2584 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2585 {
2586 	struct drm_plane_state *state;
2587 
2588 	if (WARN_ON(!plane->state))
2589 		return NULL;
2590 
2591 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2592 	if (state)
2593 		__drm_atomic_helper_plane_duplicate_state(plane, state);
2594 
2595 	return state;
2596 }
2597 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2598 
2599 /**
2600  * __drm_atomic_helper_plane_destroy_state - release plane state
2601  * @plane: plane object
2602  * @state: plane state object to release
2603  *
2604  * Releases all resources stored in the plane state without actually freeing
2605  * the memory of the plane state. This is useful for drivers that subclass the
2606  * plane state.
2607  */
2608 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2609 					     struct drm_plane_state *state)
2610 {
2611 	if (state->fb)
2612 		drm_framebuffer_unreference(state->fb);
2613 }
2614 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2615 
2616 /**
2617  * drm_atomic_helper_plane_destroy_state - default state destroy hook
2618  * @plane: drm plane
2619  * @state: plane state object to release
2620  *
2621  * Default plane state destroy hook for drivers which don't have their own
2622  * subclassed plane state structure.
2623  */
2624 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2625 					   struct drm_plane_state *state)
2626 {
2627 	__drm_atomic_helper_plane_destroy_state(plane, state);
2628 	kfree(state);
2629 }
2630 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2631 
2632 /**
2633  * __drm_atomic_helper_connector_reset - reset state on connector
2634  * @connector: drm connector
2635  * @conn_state: connector state to assign
2636  *
2637  * Initializes the newly allocated @conn_state and assigns it to
2638  * #connector ->state, usually required when initializing the drivers
2639  * or when called from the ->reset hook.
2640  *
2641  * This is useful for drivers that subclass the connector state.
2642  */
2643 void
2644 __drm_atomic_helper_connector_reset(struct drm_connector *connector,
2645 				    struct drm_connector_state *conn_state)
2646 {
2647 	if (conn_state)
2648 		conn_state->connector = connector;
2649 
2650 	connector->state = conn_state;
2651 }
2652 EXPORT_SYMBOL(__drm_atomic_helper_connector_reset);
2653 
2654 /**
2655  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2656  * @connector: drm connector
2657  *
2658  * Resets the atomic state for @connector by freeing the state pointer (which
2659  * might be NULL, e.g. at driver load time) and allocating a new empty state
2660  * object.
2661  */
2662 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2663 {
2664 	struct drm_connector_state *conn_state =
2665 		kzalloc(sizeof(*conn_state), GFP_KERNEL);
2666 
2667 	kfree(connector->state);
2668 	__drm_atomic_helper_connector_reset(connector, conn_state);
2669 }
2670 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2671 
2672 /**
2673  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2674  * @connector: connector object
2675  * @state: atomic connector state
2676  *
2677  * Copies atomic state from a connector's current state. This is useful for
2678  * drivers that subclass the connector state.
2679  */
2680 void
2681 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2682 					    struct drm_connector_state *state)
2683 {
2684 	memcpy(state, connector->state, sizeof(*state));
2685 }
2686 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2687 
2688 /**
2689  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2690  * @connector: drm connector
2691  *
2692  * Default connector state duplicate hook for drivers which don't have their own
2693  * subclassed connector state structure.
2694  */
2695 struct drm_connector_state *
2696 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2697 {
2698 	struct drm_connector_state *state;
2699 
2700 	if (WARN_ON(!connector->state))
2701 		return NULL;
2702 
2703 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2704 	if (state)
2705 		__drm_atomic_helper_connector_duplicate_state(connector, state);
2706 
2707 	return state;
2708 }
2709 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2710 
2711 /**
2712  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
2713  * @dev: DRM device
2714  * @ctx: lock acquisition context
2715  *
2716  * Makes a copy of the current atomic state by looping over all objects and
2717  * duplicating their respective states. This is used for example by suspend/
2718  * resume support code to save the state prior to suspend such that it can
2719  * be restored upon resume.
2720  *
2721  * Note that this treats atomic state as persistent between save and restore.
2722  * Drivers must make sure that this is possible and won't result in confusion
2723  * or erroneous behaviour.
2724  *
2725  * Note that if callers haven't already acquired all modeset locks this might
2726  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
2727  *
2728  * Returns:
2729  * A pointer to the copy of the atomic state object on success or an
2730  * ERR_PTR()-encoded error code on failure.
2731  *
2732  * See also:
2733  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
2734  */
2735 struct drm_atomic_state *
2736 drm_atomic_helper_duplicate_state(struct drm_device *dev,
2737 				  struct drm_modeset_acquire_ctx *ctx)
2738 {
2739 	struct drm_atomic_state *state;
2740 	struct drm_connector *conn;
2741 	struct drm_plane *plane;
2742 	struct drm_crtc *crtc;
2743 	int err = 0;
2744 
2745 	state = drm_atomic_state_alloc(dev);
2746 	if (!state)
2747 		return ERR_PTR(-ENOMEM);
2748 
2749 	state->acquire_ctx = ctx;
2750 
2751 	drm_for_each_crtc(crtc, dev) {
2752 		struct drm_crtc_state *crtc_state;
2753 
2754 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
2755 		if (IS_ERR(crtc_state)) {
2756 			err = PTR_ERR(crtc_state);
2757 			goto free;
2758 		}
2759 	}
2760 
2761 	drm_for_each_plane(plane, dev) {
2762 		struct drm_plane_state *plane_state;
2763 
2764 		plane_state = drm_atomic_get_plane_state(state, plane);
2765 		if (IS_ERR(plane_state)) {
2766 			err = PTR_ERR(plane_state);
2767 			goto free;
2768 		}
2769 	}
2770 
2771 	drm_for_each_connector(conn, dev) {
2772 		struct drm_connector_state *conn_state;
2773 
2774 		conn_state = drm_atomic_get_connector_state(state, conn);
2775 		if (IS_ERR(conn_state)) {
2776 			err = PTR_ERR(conn_state);
2777 			goto free;
2778 		}
2779 	}
2780 
2781 	/* clear the acquire context so that it isn't accidentally reused */
2782 	state->acquire_ctx = NULL;
2783 
2784 free:
2785 	if (err < 0) {
2786 		drm_atomic_state_free(state);
2787 		state = ERR_PTR(err);
2788 	}
2789 
2790 	return state;
2791 }
2792 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
2793 
2794 /**
2795  * __drm_atomic_helper_connector_destroy_state - release connector state
2796  * @connector: connector object
2797  * @state: connector state object to release
2798  *
2799  * Releases all resources stored in the connector state without actually
2800  * freeing the memory of the connector state. This is useful for drivers that
2801  * subclass the connector state.
2802  */
2803 void
2804 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2805 					    struct drm_connector_state *state)
2806 {
2807 	/*
2808 	 * This is currently a placeholder so that drivers that subclass the
2809 	 * state will automatically do the right thing if code is ever added
2810 	 * to this function.
2811 	 */
2812 }
2813 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2814 
2815 /**
2816  * drm_atomic_helper_connector_destroy_state - default state destroy hook
2817  * @connector: drm connector
2818  * @state: connector state object to release
2819  *
2820  * Default connector state destroy hook for drivers which don't have their own
2821  * subclassed connector state structure.
2822  */
2823 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2824 					  struct drm_connector_state *state)
2825 {
2826 	__drm_atomic_helper_connector_destroy_state(connector, state);
2827 	kfree(state);
2828 }
2829 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
2830