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 expose
47  * 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 static void
57 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
58 				struct drm_plane_state *plane_state,
59 				struct drm_plane *plane)
60 {
61 	struct drm_crtc_state *crtc_state;
62 
63 	if (plane->state->crtc) {
64 		crtc_state = state->crtc_states[drm_crtc_index(plane->state->crtc)];
65 
66 		if (WARN_ON(!crtc_state))
67 			return;
68 
69 		crtc_state->planes_changed = true;
70 	}
71 
72 	if (plane_state->crtc) {
73 		crtc_state =
74 			state->crtc_states[drm_crtc_index(plane_state->crtc)];
75 
76 		if (WARN_ON(!crtc_state))
77 			return;
78 
79 		crtc_state->planes_changed = true;
80 	}
81 }
82 
83 static struct drm_crtc *
84 get_current_crtc_for_encoder(struct drm_device *dev,
85 			     struct drm_encoder *encoder)
86 {
87 	struct drm_mode_config *config = &dev->mode_config;
88 	struct drm_connector *connector;
89 
90 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
91 
92 	drm_for_each_connector(connector, dev) {
93 		if (connector->state->best_encoder != encoder)
94 			continue;
95 
96 		return connector->state->crtc;
97 	}
98 
99 	return NULL;
100 }
101 
102 static int
103 steal_encoder(struct drm_atomic_state *state,
104 	      struct drm_encoder *encoder,
105 	      struct drm_crtc *encoder_crtc)
106 {
107 	struct drm_mode_config *config = &state->dev->mode_config;
108 	struct drm_crtc_state *crtc_state;
109 	struct drm_connector *connector;
110 	struct drm_connector_state *connector_state;
111 	int ret;
112 
113 	/*
114 	 * We can only steal an encoder coming from a connector, which means we
115 	 * must already hold the connection_mutex.
116 	 */
117 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
118 
119 	DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
120 			 encoder->base.id, encoder->name,
121 			 encoder_crtc->base.id);
122 
123 	crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
124 	if (IS_ERR(crtc_state))
125 		return PTR_ERR(crtc_state);
126 
127 	crtc_state->connectors_changed = true;
128 
129 	list_for_each_entry(connector, &config->connector_list, head) {
130 		if (connector->state->best_encoder != encoder)
131 			continue;
132 
133 		DRM_DEBUG_ATOMIC("Stealing encoder from [CONNECTOR:%d:%s]\n",
134 				 connector->base.id,
135 				 connector->name);
136 
137 		connector_state = drm_atomic_get_connector_state(state,
138 								 connector);
139 		if (IS_ERR(connector_state))
140 			return PTR_ERR(connector_state);
141 
142 		ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
143 		if (ret)
144 			return ret;
145 		connector_state->best_encoder = NULL;
146 	}
147 
148 	return 0;
149 }
150 
151 static int
152 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
153 {
154 	const struct drm_connector_helper_funcs *funcs;
155 	struct drm_encoder *new_encoder;
156 	struct drm_crtc *encoder_crtc;
157 	struct drm_connector *connector;
158 	struct drm_connector_state *connector_state;
159 	struct drm_crtc_state *crtc_state;
160 	int idx, ret;
161 
162 	connector = state->connectors[conn_idx];
163 	connector_state = state->connector_states[conn_idx];
164 
165 	if (!connector)
166 		return 0;
167 
168 	DRM_DEBUG_ATOMIC("Updating routing for [CONNECTOR:%d:%s]\n",
169 			 connector->base.id,
170 			 connector->name);
171 
172 	if (connector->state->crtc != connector_state->crtc) {
173 		if (connector->state->crtc) {
174 			idx = drm_crtc_index(connector->state->crtc);
175 
176 			crtc_state = state->crtc_states[idx];
177 			crtc_state->connectors_changed = true;
178 		}
179 
180 		if (connector_state->crtc) {
181 			idx = drm_crtc_index(connector_state->crtc);
182 
183 			crtc_state = state->crtc_states[idx];
184 			crtc_state->connectors_changed = true;
185 		}
186 	}
187 
188 	if (!connector_state->crtc) {
189 		DRM_DEBUG_ATOMIC("Disabling [CONNECTOR:%d:%s]\n",
190 				connector->base.id,
191 				connector->name);
192 
193 		connector_state->best_encoder = NULL;
194 
195 		return 0;
196 	}
197 
198 	funcs = connector->helper_private;
199 
200 	if (funcs->atomic_best_encoder)
201 		new_encoder = funcs->atomic_best_encoder(connector,
202 							 connector_state);
203 	else
204 		new_encoder = funcs->best_encoder(connector);
205 
206 	if (!new_encoder) {
207 		DRM_DEBUG_ATOMIC("No suitable encoder found for [CONNECTOR:%d:%s]\n",
208 				 connector->base.id,
209 				 connector->name);
210 		return -EINVAL;
211 	}
212 
213 	if (new_encoder == connector_state->best_encoder) {
214 		DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
215 				 connector->base.id,
216 				 connector->name,
217 				 new_encoder->base.id,
218 				 new_encoder->name,
219 				 connector_state->crtc->base.id);
220 
221 		return 0;
222 	}
223 
224 	encoder_crtc = get_current_crtc_for_encoder(state->dev,
225 						    new_encoder);
226 
227 	if (encoder_crtc) {
228 		ret = steal_encoder(state, new_encoder, encoder_crtc);
229 		if (ret) {
230 			DRM_DEBUG_ATOMIC("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
231 					 connector->base.id,
232 					 connector->name);
233 			return ret;
234 		}
235 	}
236 
237 	if (WARN_ON(!connector_state->crtc))
238 		return -EINVAL;
239 
240 	connector_state->best_encoder = new_encoder;
241 	idx = drm_crtc_index(connector_state->crtc);
242 
243 	crtc_state = state->crtc_states[idx];
244 	crtc_state->connectors_changed = true;
245 
246 	DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
247 			 connector->base.id,
248 			 connector->name,
249 			 new_encoder->base.id,
250 			 new_encoder->name,
251 			 connector_state->crtc->base.id);
252 
253 	return 0;
254 }
255 
256 static int
257 mode_fixup(struct drm_atomic_state *state)
258 {
259 	struct drm_crtc *crtc;
260 	struct drm_crtc_state *crtc_state;
261 	struct drm_connector *connector;
262 	struct drm_connector_state *conn_state;
263 	int i;
264 	bool ret;
265 
266 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
267 		if (!crtc_state->mode_changed &&
268 		    !crtc_state->connectors_changed)
269 			continue;
270 
271 		drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
272 	}
273 
274 	for_each_connector_in_state(state, connector, conn_state, i) {
275 		const struct drm_encoder_helper_funcs *funcs;
276 		struct drm_encoder *encoder;
277 
278 		WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
279 
280 		if (!conn_state->crtc || !conn_state->best_encoder)
281 			continue;
282 
283 		crtc_state =
284 			state->crtc_states[drm_crtc_index(conn_state->crtc)];
285 
286 		/*
287 		 * Each encoder has at most one connector (since we always steal
288 		 * it away), so we won't call ->mode_fixup twice.
289 		 */
290 		encoder = conn_state->best_encoder;
291 		funcs = encoder->helper_private;
292 		if (!funcs)
293 			continue;
294 
295 		ret = drm_bridge_mode_fixup(encoder->bridge, &crtc_state->mode,
296 				&crtc_state->adjusted_mode);
297 		if (!ret) {
298 			DRM_DEBUG_ATOMIC("Bridge fixup failed\n");
299 			return -EINVAL;
300 		}
301 
302 		if (funcs->atomic_check) {
303 			ret = funcs->atomic_check(encoder, crtc_state,
304 						  conn_state);
305 			if (ret) {
306 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] check failed\n",
307 						 encoder->base.id, encoder->name);
308 				return ret;
309 			}
310 		} else if (funcs->mode_fixup) {
311 			ret = funcs->mode_fixup(encoder, &crtc_state->mode,
312 						&crtc_state->adjusted_mode);
313 			if (!ret) {
314 				DRM_DEBUG_ATOMIC("[ENCODER:%d:%s] fixup failed\n",
315 						 encoder->base.id, encoder->name);
316 				return -EINVAL;
317 			}
318 		}
319 	}
320 
321 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
322 		const struct drm_crtc_helper_funcs *funcs;
323 
324 		if (!crtc_state->mode_changed &&
325 		    !crtc_state->connectors_changed)
326 			continue;
327 
328 		funcs = crtc->helper_private;
329 		if (!funcs->mode_fixup)
330 			continue;
331 
332 		ret = funcs->mode_fixup(crtc, &crtc_state->mode,
333 					&crtc_state->adjusted_mode);
334 		if (!ret) {
335 			DRM_DEBUG_ATOMIC("[CRTC:%d] fixup failed\n",
336 					 crtc->base.id);
337 			return -EINVAL;
338 		}
339 	}
340 
341 	return 0;
342 }
343 
344 /**
345  * drm_atomic_helper_check_modeset - validate state object for modeset changes
346  * @dev: DRM device
347  * @state: the driver state object
348  *
349  * Check the state object to see if the requested state is physically possible.
350  * This does all the crtc and connector related computations for an atomic
351  * update and adds any additional connectors needed for full modesets and calls
352  * down into ->mode_fixup functions of the driver backend.
353  *
354  * crtc_state->mode_changed is set when the input mode is changed.
355  * crtc_state->connectors_changed is set when a connector is added or
356  * removed from the crtc.
357  * crtc_state->active_changed is set when crtc_state->active changes,
358  * which is used for dpms.
359  *
360  * IMPORTANT:
361  *
362  * Drivers which update ->mode_changed (e.g. in their ->atomic_check hooks if a
363  * plane update can't be done without a full modeset) _must_ call this function
364  * afterwards after that change. It is permitted to call this function multiple
365  * times for the same update, e.g. when the ->atomic_check functions depend upon
366  * the adjusted dotclock for fifo space allocation and watermark computation.
367  *
368  * RETURNS
369  * Zero for success or -errno
370  */
371 int
372 drm_atomic_helper_check_modeset(struct drm_device *dev,
373 				struct drm_atomic_state *state)
374 {
375 	struct drm_crtc *crtc;
376 	struct drm_crtc_state *crtc_state;
377 	struct drm_connector *connector;
378 	struct drm_connector_state *connector_state;
379 	int i, ret;
380 
381 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
382 		if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
383 			DRM_DEBUG_ATOMIC("[CRTC:%d] mode changed\n",
384 					 crtc->base.id);
385 			crtc_state->mode_changed = true;
386 		}
387 
388 		if (crtc->state->enable != crtc_state->enable) {
389 			DRM_DEBUG_ATOMIC("[CRTC:%d] enable changed\n",
390 					 crtc->base.id);
391 
392 			/*
393 			 * For clarity this assignment is done here, but
394 			 * enable == 0 is only true when there are no
395 			 * connectors and a NULL mode.
396 			 *
397 			 * The other way around is true as well. enable != 0
398 			 * iff connectors are attached and a mode is set.
399 			 */
400 			crtc_state->mode_changed = true;
401 			crtc_state->connectors_changed = true;
402 		}
403 	}
404 
405 	for_each_connector_in_state(state, connector, connector_state, i) {
406 		/*
407 		 * This only sets crtc->mode_changed for routing changes,
408 		 * drivers must set crtc->mode_changed themselves when connector
409 		 * properties need to be updated.
410 		 */
411 		ret = update_connector_routing(state, i);
412 		if (ret)
413 			return ret;
414 	}
415 
416 	/*
417 	 * After all the routing has been prepared we need to add in any
418 	 * connector which is itself unchanged, but who's crtc changes it's
419 	 * configuration. This must be done before calling mode_fixup in case a
420 	 * crtc only changed its mode but has the same set of connectors.
421 	 */
422 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
423 		int num_connectors;
424 
425 		/*
426 		 * We must set ->active_changed after walking connectors for
427 		 * otherwise an update that only changes active would result in
428 		 * a full modeset because update_connector_routing force that.
429 		 */
430 		if (crtc->state->active != crtc_state->active) {
431 			DRM_DEBUG_ATOMIC("[CRTC:%d] active changed\n",
432 					 crtc->base.id);
433 			crtc_state->active_changed = true;
434 		}
435 
436 		if (!drm_atomic_crtc_needs_modeset(crtc_state))
437 			continue;
438 
439 		DRM_DEBUG_ATOMIC("[CRTC:%d] needs all connectors, enable: %c, active: %c\n",
440 				 crtc->base.id,
441 				 crtc_state->enable ? 'y' : 'n',
442 			      crtc_state->active ? 'y' : 'n');
443 
444 		ret = drm_atomic_add_affected_connectors(state, crtc);
445 		if (ret != 0)
446 			return ret;
447 
448 		ret = drm_atomic_add_affected_planes(state, crtc);
449 		if (ret != 0)
450 			return ret;
451 
452 		num_connectors = drm_atomic_connectors_for_crtc(state,
453 								crtc);
454 
455 		if (crtc_state->enable != !!num_connectors) {
456 			DRM_DEBUG_ATOMIC("[CRTC:%d] enabled/connectors mismatch\n",
457 					 crtc->base.id);
458 
459 			return -EINVAL;
460 		}
461 	}
462 
463 	return mode_fixup(state);
464 }
465 EXPORT_SYMBOL(drm_atomic_helper_check_modeset);
466 
467 /**
468  * drm_atomic_helper_check_planes - validate state object for planes changes
469  * @dev: DRM device
470  * @state: the driver state object
471  *
472  * Check the state object to see if the requested state is physically possible.
473  * This does all the plane update related checks using by calling into the
474  * ->atomic_check hooks provided by the driver.
475  *
476  * It also sets crtc_state->planes_changed to indicate that a crtc has
477  * updated planes.
478  *
479  * RETURNS
480  * Zero for success or -errno
481  */
482 int
483 drm_atomic_helper_check_planes(struct drm_device *dev,
484 			       struct drm_atomic_state *state)
485 {
486 	struct drm_crtc *crtc;
487 	struct drm_crtc_state *crtc_state;
488 	struct drm_plane *plane;
489 	struct drm_plane_state *plane_state;
490 	int i, ret = 0;
491 
492 	for_each_plane_in_state(state, plane, plane_state, i) {
493 		const struct drm_plane_helper_funcs *funcs;
494 
495 		funcs = plane->helper_private;
496 
497 		drm_atomic_helper_plane_changed(state, plane_state, plane);
498 
499 		if (!funcs || !funcs->atomic_check)
500 			continue;
501 
502 		ret = funcs->atomic_check(plane, plane_state);
503 		if (ret) {
504 			DRM_DEBUG_ATOMIC("[PLANE:%d] atomic driver check failed\n",
505 					 plane->base.id);
506 			return ret;
507 		}
508 	}
509 
510 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
511 		const struct drm_crtc_helper_funcs *funcs;
512 
513 		funcs = crtc->helper_private;
514 
515 		if (!funcs || !funcs->atomic_check)
516 			continue;
517 
518 		ret = funcs->atomic_check(crtc, state->crtc_states[i]);
519 		if (ret) {
520 			DRM_DEBUG_ATOMIC("[CRTC:%d] atomic driver check failed\n",
521 					 crtc->base.id);
522 			return ret;
523 		}
524 	}
525 
526 	return ret;
527 }
528 EXPORT_SYMBOL(drm_atomic_helper_check_planes);
529 
530 /**
531  * drm_atomic_helper_check - validate state object
532  * @dev: DRM device
533  * @state: the driver state object
534  *
535  * Check the state object to see if the requested state is physically possible.
536  * Only crtcs and planes have check callbacks, so for any additional (global)
537  * checking that a driver needs it can simply wrap that around this function.
538  * Drivers without such needs can directly use this as their ->atomic_check()
539  * callback.
540  *
541  * This just wraps the two parts of the state checking for planes and modeset
542  * state in the default order: First it calls drm_atomic_helper_check_modeset()
543  * and then drm_atomic_helper_check_planes(). The assumption is that the
544  * ->atomic_check functions depend upon an updated adjusted_mode.clock to
545  * e.g. properly compute watermarks.
546  *
547  * RETURNS
548  * Zero for success or -errno
549  */
550 int drm_atomic_helper_check(struct drm_device *dev,
551 			    struct drm_atomic_state *state)
552 {
553 	int ret;
554 
555 	ret = drm_atomic_helper_check_modeset(dev, state);
556 	if (ret)
557 		return ret;
558 
559 	ret = drm_atomic_helper_check_planes(dev, state);
560 	if (ret)
561 		return ret;
562 
563 	return ret;
564 }
565 EXPORT_SYMBOL(drm_atomic_helper_check);
566 
567 static void
568 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
569 {
570 	struct drm_connector *connector;
571 	struct drm_connector_state *old_conn_state;
572 	struct drm_crtc *crtc;
573 	struct drm_crtc_state *old_crtc_state;
574 	int i;
575 
576 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
577 		const struct drm_encoder_helper_funcs *funcs;
578 		struct drm_encoder *encoder;
579 		struct drm_crtc_state *old_crtc_state;
580 
581 		/* Shut down everything that's in the changeset and currently
582 		 * still on. So need to check the old, saved state. */
583 		if (!old_conn_state->crtc)
584 			continue;
585 
586 		old_crtc_state = old_state->crtc_states[drm_crtc_index(old_conn_state->crtc)];
587 
588 		if (!old_crtc_state->active ||
589 		    !drm_atomic_crtc_needs_modeset(old_conn_state->crtc->state))
590 			continue;
591 
592 		encoder = old_conn_state->best_encoder;
593 
594 		/* We shouldn't get this far if we didn't previously have
595 		 * an encoder.. but WARN_ON() rather than explode.
596 		 */
597 		if (WARN_ON(!encoder))
598 			continue;
599 
600 		funcs = encoder->helper_private;
601 
602 		DRM_DEBUG_ATOMIC("disabling [ENCODER:%d:%s]\n",
603 				 encoder->base.id, encoder->name);
604 
605 		/*
606 		 * Each encoder has at most one connector (since we always steal
607 		 * it away), so we won't call disable hooks twice.
608 		 */
609 		drm_bridge_disable(encoder->bridge);
610 
611 		/* Right function depends upon target state. */
612 		if (connector->state->crtc && funcs->prepare)
613 			funcs->prepare(encoder);
614 		else if (funcs->disable)
615 			funcs->disable(encoder);
616 		else
617 			funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
618 
619 		drm_bridge_post_disable(encoder->bridge);
620 	}
621 
622 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
623 		const struct drm_crtc_helper_funcs *funcs;
624 
625 		/* Shut down everything that needs a full modeset. */
626 		if (!drm_atomic_crtc_needs_modeset(crtc->state))
627 			continue;
628 
629 		if (!old_crtc_state->active)
630 			continue;
631 
632 		funcs = crtc->helper_private;
633 
634 		DRM_DEBUG_ATOMIC("disabling [CRTC:%d]\n",
635 				 crtc->base.id);
636 
637 
638 		/* Right function depends upon target state. */
639 		if (crtc->state->enable && funcs->prepare)
640 			funcs->prepare(crtc);
641 		else if (funcs->disable)
642 			funcs->disable(crtc);
643 		else
644 			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
645 	}
646 }
647 
648 /**
649  * drm_atomic_helper_update_legacy_modeset_state - update legacy modeset state
650  * @dev: DRM device
651  * @old_state: atomic state object with old state structures
652  *
653  * This function updates all the various legacy modeset state pointers in
654  * connectors, encoders and crtcs. It also updates the timestamping constants
655  * used for precise vblank timestamps by calling
656  * drm_calc_timestamping_constants().
657  *
658  * Drivers can use this for building their own atomic commit if they don't have
659  * a pure helper-based modeset implementation.
660  */
661 void
662 drm_atomic_helper_update_legacy_modeset_state(struct drm_device *dev,
663 					      struct drm_atomic_state *old_state)
664 {
665 	struct drm_connector *connector;
666 	struct drm_connector_state *old_conn_state;
667 	struct drm_crtc *crtc;
668 	struct drm_crtc_state *old_crtc_state;
669 	int i;
670 
671 	/* clear out existing links and update dpms */
672 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
673 		if (connector->encoder) {
674 			WARN_ON(!connector->encoder->crtc);
675 
676 			connector->encoder->crtc = NULL;
677 			connector->encoder = NULL;
678 		}
679 
680 		crtc = connector->state->crtc;
681 		if ((!crtc && old_conn_state->crtc) ||
682 		    (crtc && drm_atomic_crtc_needs_modeset(crtc->state))) {
683 			struct drm_property *dpms_prop =
684 				dev->mode_config.dpms_property;
685 			int mode = DRM_MODE_DPMS_OFF;
686 
687 			if (crtc && crtc->state->active)
688 				mode = DRM_MODE_DPMS_ON;
689 
690 			connector->dpms = mode;
691 			drm_object_property_set_value(&connector->base,
692 						      dpms_prop, mode);
693 		}
694 	}
695 
696 	/* set new links */
697 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
698 		if (!connector->state->crtc)
699 			continue;
700 
701 		if (WARN_ON(!connector->state->best_encoder))
702 			continue;
703 
704 		connector->encoder = connector->state->best_encoder;
705 		connector->encoder->crtc = connector->state->crtc;
706 	}
707 
708 	/* set legacy state in the crtc structure */
709 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
710 		struct drm_plane *primary = crtc->primary;
711 
712 		crtc->mode = crtc->state->mode;
713 		crtc->enabled = crtc->state->enable;
714 
715 		if (drm_atomic_get_existing_plane_state(old_state, primary) &&
716 		    primary->state->crtc == crtc) {
717 			crtc->x = primary->state->src_x >> 16;
718 			crtc->y = primary->state->src_y >> 16;
719 		}
720 
721 		if (crtc->state->enable)
722 			drm_calc_timestamping_constants(crtc,
723 							&crtc->state->adjusted_mode);
724 	}
725 }
726 EXPORT_SYMBOL(drm_atomic_helper_update_legacy_modeset_state);
727 
728 static void
729 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
730 {
731 	struct drm_crtc *crtc;
732 	struct drm_crtc_state *old_crtc_state;
733 	struct drm_connector *connector;
734 	struct drm_connector_state *old_conn_state;
735 	int i;
736 
737 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
738 		const struct drm_crtc_helper_funcs *funcs;
739 
740 		if (!crtc->state->mode_changed)
741 			continue;
742 
743 		funcs = crtc->helper_private;
744 
745 		if (crtc->state->enable && funcs->mode_set_nofb) {
746 			DRM_DEBUG_ATOMIC("modeset on [CRTC:%d]\n",
747 					 crtc->base.id);
748 
749 			funcs->mode_set_nofb(crtc);
750 		}
751 	}
752 
753 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
754 		const struct drm_encoder_helper_funcs *funcs;
755 		struct drm_crtc_state *new_crtc_state;
756 		struct drm_encoder *encoder;
757 		struct drm_display_mode *mode, *adjusted_mode;
758 
759 		if (!connector->state->best_encoder)
760 			continue;
761 
762 		encoder = connector->state->best_encoder;
763 		funcs = encoder->helper_private;
764 		new_crtc_state = connector->state->crtc->state;
765 		mode = &new_crtc_state->mode;
766 		adjusted_mode = &new_crtc_state->adjusted_mode;
767 
768 		if (!new_crtc_state->mode_changed)
769 			continue;
770 
771 		DRM_DEBUG_ATOMIC("modeset on [ENCODER:%d:%s]\n",
772 				 encoder->base.id, encoder->name);
773 
774 		/*
775 		 * Each encoder has at most one connector (since we always steal
776 		 * it away), so we won't call mode_set hooks twice.
777 		 */
778 		if (funcs->mode_set)
779 			funcs->mode_set(encoder, mode, adjusted_mode);
780 
781 		drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
782 	}
783 }
784 
785 /**
786  * drm_atomic_helper_commit_modeset_disables - modeset commit to disable outputs
787  * @dev: DRM device
788  * @old_state: atomic state object with old state structures
789  *
790  * This function shuts down all the outputs that need to be shut down and
791  * prepares them (if required) with the new mode.
792  *
793  * For compatibility with legacy crtc helpers this should be called before
794  * drm_atomic_helper_commit_planes(), which is what the default commit function
795  * does. But drivers with different needs can group the modeset commits together
796  * and do the plane commits at the end. This is useful for drivers doing runtime
797  * PM since planes updates then only happen when the CRTC is actually enabled.
798  */
799 void drm_atomic_helper_commit_modeset_disables(struct drm_device *dev,
800 					       struct drm_atomic_state *old_state)
801 {
802 	disable_outputs(dev, old_state);
803 
804 	drm_atomic_helper_update_legacy_modeset_state(dev, old_state);
805 
806 	crtc_set_mode(dev, old_state);
807 }
808 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_disables);
809 
810 /**
811  * drm_atomic_helper_commit_modeset_enables - modeset commit to enable outputs
812  * @dev: DRM device
813  * @old_state: atomic state object with old state structures
814  *
815  * This function enables all the outputs with the new configuration which had to
816  * be turned off for the update.
817  *
818  * For compatibility with legacy crtc helpers this should be called after
819  * drm_atomic_helper_commit_planes(), which is what the default commit function
820  * does. But drivers with different needs can group the modeset commits together
821  * and do the plane commits at the end. This is useful for drivers doing runtime
822  * PM since planes updates then only happen when the CRTC is actually enabled.
823  */
824 void drm_atomic_helper_commit_modeset_enables(struct drm_device *dev,
825 					      struct drm_atomic_state *old_state)
826 {
827 	struct drm_crtc *crtc;
828 	struct drm_crtc_state *old_crtc_state;
829 	struct drm_connector *connector;
830 	struct drm_connector_state *old_conn_state;
831 	int i;
832 
833 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
834 		const struct drm_crtc_helper_funcs *funcs;
835 
836 		/* Need to filter out CRTCs where only planes change. */
837 		if (!drm_atomic_crtc_needs_modeset(crtc->state))
838 			continue;
839 
840 		if (!crtc->state->active)
841 			continue;
842 
843 		funcs = crtc->helper_private;
844 
845 		if (crtc->state->enable) {
846 			DRM_DEBUG_ATOMIC("enabling [CRTC:%d]\n",
847 					 crtc->base.id);
848 
849 			if (funcs->enable)
850 				funcs->enable(crtc);
851 			else
852 				funcs->commit(crtc);
853 		}
854 	}
855 
856 	for_each_connector_in_state(old_state, connector, old_conn_state, i) {
857 		const struct drm_encoder_helper_funcs *funcs;
858 		struct drm_encoder *encoder;
859 
860 		if (!connector->state->best_encoder)
861 			continue;
862 
863 		if (!connector->state->crtc->state->active ||
864 		    !drm_atomic_crtc_needs_modeset(connector->state->crtc->state))
865 			continue;
866 
867 		encoder = connector->state->best_encoder;
868 		funcs = encoder->helper_private;
869 
870 		DRM_DEBUG_ATOMIC("enabling [ENCODER:%d:%s]\n",
871 				 encoder->base.id, encoder->name);
872 
873 		/*
874 		 * Each encoder has at most one connector (since we always steal
875 		 * it away), so we won't call enable hooks twice.
876 		 */
877 		drm_bridge_pre_enable(encoder->bridge);
878 
879 		if (funcs->enable)
880 			funcs->enable(encoder);
881 		else
882 			funcs->commit(encoder);
883 
884 		drm_bridge_enable(encoder->bridge);
885 	}
886 }
887 EXPORT_SYMBOL(drm_atomic_helper_commit_modeset_enables);
888 
889 static void wait_for_fences(struct drm_device *dev,
890 			    struct drm_atomic_state *state)
891 {
892 	struct drm_plane *plane;
893 	struct drm_plane_state *plane_state;
894 	int i;
895 
896 	for_each_plane_in_state(state, plane, plane_state, i) {
897 		if (!plane->state->fence)
898 			continue;
899 
900 		WARN_ON(!plane->state->fb);
901 
902 		fence_wait(plane->state->fence, false);
903 		fence_put(plane->state->fence);
904 		plane->state->fence = NULL;
905 	}
906 }
907 
908 static bool framebuffer_changed(struct drm_device *dev,
909 				struct drm_atomic_state *old_state,
910 				struct drm_crtc *crtc)
911 {
912 	struct drm_plane *plane;
913 	struct drm_plane_state *old_plane_state;
914 	int i;
915 
916 	for_each_plane_in_state(old_state, plane, old_plane_state, i) {
917 		if (plane->state->crtc != crtc &&
918 		    old_plane_state->crtc != crtc)
919 			continue;
920 
921 		if (plane->state->fb != old_plane_state->fb)
922 			return true;
923 	}
924 
925 	return false;
926 }
927 
928 /**
929  * drm_atomic_helper_wait_for_vblanks - wait for vblank on crtcs
930  * @dev: DRM device
931  * @old_state: atomic state object with old state structures
932  *
933  * Helper to, after atomic commit, wait for vblanks on all effected
934  * crtcs (ie. before cleaning up old framebuffers using
935  * drm_atomic_helper_cleanup_planes()). It will only wait on crtcs where the
936  * framebuffers have actually changed to optimize for the legacy cursor and
937  * plane update use-case.
938  */
939 void
940 drm_atomic_helper_wait_for_vblanks(struct drm_device *dev,
941 		struct drm_atomic_state *old_state)
942 {
943 	struct drm_crtc *crtc;
944 	struct drm_crtc_state *old_crtc_state;
945 	int i, ret;
946 
947 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
948 		/* No one cares about the old state, so abuse it for tracking
949 		 * and store whether we hold a vblank reference (and should do a
950 		 * vblank wait) in the ->enable boolean. */
951 		old_crtc_state->enable = false;
952 
953 		if (!crtc->state->enable)
954 			continue;
955 
956 		/* Legacy cursor ioctls are completely unsynced, and userspace
957 		 * relies on that (by doing tons of cursor updates). */
958 		if (old_state->legacy_cursor_update)
959 			continue;
960 
961 		if (!framebuffer_changed(dev, old_state, crtc))
962 			continue;
963 
964 		ret = drm_crtc_vblank_get(crtc);
965 		if (ret != 0)
966 			continue;
967 
968 		old_crtc_state->enable = true;
969 		old_crtc_state->last_vblank_count = drm_crtc_vblank_count(crtc);
970 	}
971 
972 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
973 		if (!old_crtc_state->enable)
974 			continue;
975 
976 		ret = wait_event_timeout(dev->vblank[i].queue,
977 				old_crtc_state->last_vblank_count !=
978 					drm_crtc_vblank_count(crtc),
979 				msecs_to_jiffies(50));
980 
981 		drm_crtc_vblank_put(crtc);
982 	}
983 }
984 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
985 
986 /**
987  * drm_atomic_helper_commit - commit validated state object
988  * @dev: DRM device
989  * @state: the driver state object
990  * @async: asynchronous commit
991  *
992  * This function commits a with drm_atomic_helper_check() pre-validated state
993  * object. This can still fail when e.g. the framebuffer reservation fails. For
994  * now this doesn't implement asynchronous commits.
995  *
996  * RETURNS
997  * Zero for success or -errno.
998  */
999 int drm_atomic_helper_commit(struct drm_device *dev,
1000 			     struct drm_atomic_state *state,
1001 			     bool async)
1002 {
1003 	int ret;
1004 
1005 	if (async)
1006 		return -EBUSY;
1007 
1008 	ret = drm_atomic_helper_prepare_planes(dev, state);
1009 	if (ret)
1010 		return ret;
1011 
1012 	/*
1013 	 * This is the point of no return - everything below never fails except
1014 	 * when the hw goes bonghits. Which means we can commit the new state on
1015 	 * the software side now.
1016 	 */
1017 
1018 	drm_atomic_helper_swap_state(dev, state);
1019 
1020 	/*
1021 	 * Everything below can be run asynchronously without the need to grab
1022 	 * any modeset locks at all under one condition: It must be guaranteed
1023 	 * that the asynchronous work has either been cancelled (if the driver
1024 	 * supports it, which at least requires that the framebuffers get
1025 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
1026 	 * before the new state gets committed on the software side with
1027 	 * drm_atomic_helper_swap_state().
1028 	 *
1029 	 * This scheme allows new atomic state updates to be prepared and
1030 	 * checked in parallel to the asynchronous completion of the previous
1031 	 * update. Which is important since compositors need to figure out the
1032 	 * composition of the next frame right after having submitted the
1033 	 * current layout.
1034 	 */
1035 
1036 	wait_for_fences(dev, state);
1037 
1038 	drm_atomic_helper_commit_modeset_disables(dev, state);
1039 
1040 	drm_atomic_helper_commit_planes(dev, state);
1041 
1042 	drm_atomic_helper_commit_modeset_enables(dev, state);
1043 
1044 	drm_atomic_helper_wait_for_vblanks(dev, state);
1045 
1046 	drm_atomic_helper_cleanup_planes(dev, state);
1047 
1048 	drm_atomic_state_free(state);
1049 
1050 	return 0;
1051 }
1052 EXPORT_SYMBOL(drm_atomic_helper_commit);
1053 
1054 /**
1055  * DOC: implementing async commit
1056  *
1057  * For now the atomic helpers don't support async commit directly. If there is
1058  * real need it could be added though, using the dma-buf fence infrastructure
1059  * for generic synchronization with outstanding rendering.
1060  *
1061  * For now drivers have to implement async commit themselves, with the following
1062  * sequence being the recommended one:
1063  *
1064  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
1065  * which commit needs to call which can fail, so we want to run it first and
1066  * synchronously.
1067  *
1068  * 2. Synchronize with any outstanding asynchronous commit worker threads which
1069  * might be affected the new state update. This can be done by either cancelling
1070  * or flushing the work items, depending upon whether the driver can deal with
1071  * cancelled updates. Note that it is important to ensure that the framebuffer
1072  * cleanup is still done when cancelling.
1073  *
1074  * For sufficient parallelism it is recommended to have a work item per crtc
1075  * (for updates which don't touch global state) and a global one. Then we only
1076  * need to synchronize with the crtc work items for changed crtcs and the global
1077  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
1078  *
1079  * 3. The software state is updated synchronously with
1080  * drm_atomic_helper_swap_state. Doing this under the protection of all modeset
1081  * locks means concurrent callers never see inconsistent state. And doing this
1082  * while it's guaranteed that no relevant async worker runs means that async
1083  * workers do not need grab any locks. Actually they must not grab locks, for
1084  * otherwise the work flushing will deadlock.
1085  *
1086  * 4. Schedule a work item to do all subsequent steps, using the split-out
1087  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
1088  * then cleaning up the framebuffers after the old framebuffer is no longer
1089  * being displayed.
1090  */
1091 
1092 /**
1093  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
1094  * @dev: DRM device
1095  * @state: atomic state object with new state structures
1096  *
1097  * This function prepares plane state, specifically framebuffers, for the new
1098  * configuration. If any failure is encountered this function will call
1099  * ->cleanup_fb on any already successfully prepared framebuffer.
1100  *
1101  * Returns:
1102  * 0 on success, negative error code on failure.
1103  */
1104 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
1105 				     struct drm_atomic_state *state)
1106 {
1107 	int nplanes = dev->mode_config.num_total_plane;
1108 	int ret, i;
1109 
1110 	for (i = 0; i < nplanes; i++) {
1111 		const struct drm_plane_helper_funcs *funcs;
1112 		struct drm_plane *plane = state->planes[i];
1113 		struct drm_plane_state *plane_state = state->plane_states[i];
1114 		struct drm_framebuffer *fb;
1115 
1116 		if (!plane)
1117 			continue;
1118 
1119 		funcs = plane->helper_private;
1120 
1121 		fb = plane_state->fb;
1122 
1123 		if (fb && funcs->prepare_fb) {
1124 			ret = funcs->prepare_fb(plane, fb, plane_state);
1125 			if (ret)
1126 				goto fail;
1127 		}
1128 	}
1129 
1130 	return 0;
1131 
1132 fail:
1133 	for (i--; i >= 0; i--) {
1134 		const struct drm_plane_helper_funcs *funcs;
1135 		struct drm_plane *plane = state->planes[i];
1136 		struct drm_plane_state *plane_state = state->plane_states[i];
1137 		struct drm_framebuffer *fb;
1138 
1139 		if (!plane)
1140 			continue;
1141 
1142 		funcs = plane->helper_private;
1143 
1144 		fb = state->plane_states[i]->fb;
1145 
1146 		if (fb && funcs->cleanup_fb)
1147 			funcs->cleanup_fb(plane, fb, plane_state);
1148 
1149 	}
1150 
1151 	return ret;
1152 }
1153 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
1154 
1155 /**
1156  * drm_atomic_helper_commit_planes - commit plane state
1157  * @dev: DRM device
1158  * @old_state: atomic state object with old state structures
1159  *
1160  * This function commits the new plane state using the plane and atomic helper
1161  * functions for planes and crtcs. It assumes that the atomic state has already
1162  * been pushed into the relevant object state pointers, since this step can no
1163  * longer fail.
1164  *
1165  * It still requires the global state object @old_state to know which planes and
1166  * crtcs need to be updated though.
1167  *
1168  * Note that this function does all plane updates across all CRTCs in one step.
1169  * If the hardware can't support this approach look at
1170  * drm_atomic_helper_commit_planes_on_crtc() instead.
1171  */
1172 void drm_atomic_helper_commit_planes(struct drm_device *dev,
1173 				     struct drm_atomic_state *old_state)
1174 {
1175 	struct drm_crtc *crtc;
1176 	struct drm_crtc_state *old_crtc_state;
1177 	struct drm_plane *plane;
1178 	struct drm_plane_state *old_plane_state;
1179 	int i;
1180 
1181 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1182 		const struct drm_crtc_helper_funcs *funcs;
1183 
1184 		funcs = crtc->helper_private;
1185 
1186 		if (!funcs || !funcs->atomic_begin)
1187 			continue;
1188 
1189 		funcs->atomic_begin(crtc, old_crtc_state);
1190 	}
1191 
1192 	for_each_plane_in_state(old_state, plane, old_plane_state, i) {
1193 		const struct drm_plane_helper_funcs *funcs;
1194 
1195 		funcs = plane->helper_private;
1196 
1197 		if (!funcs)
1198 			continue;
1199 
1200 		/*
1201 		 * Special-case disabling the plane if drivers support it.
1202 		 */
1203 		if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1204 		    funcs->atomic_disable)
1205 			funcs->atomic_disable(plane, old_plane_state);
1206 		else if (plane->state->crtc ||
1207 			 drm_atomic_plane_disabling(plane, old_plane_state))
1208 			funcs->atomic_update(plane, old_plane_state);
1209 	}
1210 
1211 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1212 		const struct drm_crtc_helper_funcs *funcs;
1213 
1214 		funcs = crtc->helper_private;
1215 
1216 		if (!funcs || !funcs->atomic_flush)
1217 			continue;
1218 
1219 		funcs->atomic_flush(crtc, old_crtc_state);
1220 	}
1221 }
1222 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
1223 
1224 /**
1225  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
1226  * @old_crtc_state: atomic state object with the old crtc state
1227  *
1228  * This function commits the new plane state using the plane and atomic helper
1229  * functions for planes on the specific crtc. It assumes that the atomic state
1230  * has already been pushed into the relevant object state pointers, since this
1231  * step can no longer fail.
1232  *
1233  * This function is useful when plane updates should be done crtc-by-crtc
1234  * instead of one global step like drm_atomic_helper_commit_planes() does.
1235  *
1236  * This function can only be savely used when planes are not allowed to move
1237  * between different CRTCs because this function doesn't handle inter-CRTC
1238  * depencies. Callers need to ensure that either no such depencies exist,
1239  * resolve them through ordering of commit calls or through some other means.
1240  */
1241 void
1242 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
1243 {
1244 	const struct drm_crtc_helper_funcs *crtc_funcs;
1245 	struct drm_crtc *crtc = old_crtc_state->crtc;
1246 	struct drm_atomic_state *old_state = old_crtc_state->state;
1247 	struct drm_plane *plane;
1248 	unsigned plane_mask;
1249 
1250 	plane_mask = old_crtc_state->plane_mask;
1251 	plane_mask |= crtc->state->plane_mask;
1252 
1253 	crtc_funcs = crtc->helper_private;
1254 	if (crtc_funcs && crtc_funcs->atomic_begin)
1255 		crtc_funcs->atomic_begin(crtc, old_crtc_state);
1256 
1257 	drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
1258 		struct drm_plane_state *old_plane_state =
1259 			drm_atomic_get_existing_plane_state(old_state, plane);
1260 		const struct drm_plane_helper_funcs *plane_funcs;
1261 
1262 		plane_funcs = plane->helper_private;
1263 
1264 		if (!old_plane_state || !plane_funcs)
1265 			continue;
1266 
1267 		WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
1268 
1269 		if (drm_atomic_plane_disabling(plane, old_plane_state) &&
1270 		    plane_funcs->atomic_disable)
1271 			plane_funcs->atomic_disable(plane, old_plane_state);
1272 		else if (plane->state->crtc ||
1273 			 drm_atomic_plane_disabling(plane, old_plane_state))
1274 			plane_funcs->atomic_update(plane, old_plane_state);
1275 	}
1276 
1277 	if (crtc_funcs && crtc_funcs->atomic_flush)
1278 		crtc_funcs->atomic_flush(crtc, old_crtc_state);
1279 }
1280 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
1281 
1282 /**
1283  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
1284  * @dev: DRM device
1285  * @old_state: atomic state object with old state structures
1286  *
1287  * This function cleans up plane state, specifically framebuffers, from the old
1288  * configuration. Hence the old configuration must be perserved in @old_state to
1289  * be able to call this function.
1290  *
1291  * This function must also be called on the new state when the atomic update
1292  * fails at any point after calling drm_atomic_helper_prepare_planes().
1293  */
1294 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
1295 				      struct drm_atomic_state *old_state)
1296 {
1297 	struct drm_plane *plane;
1298 	struct drm_plane_state *plane_state;
1299 	int i;
1300 
1301 	for_each_plane_in_state(old_state, plane, plane_state, i) {
1302 		const struct drm_plane_helper_funcs *funcs;
1303 		struct drm_framebuffer *old_fb;
1304 
1305 		funcs = plane->helper_private;
1306 
1307 		old_fb = plane_state->fb;
1308 
1309 		if (old_fb && funcs->cleanup_fb)
1310 			funcs->cleanup_fb(plane, old_fb, plane_state);
1311 	}
1312 }
1313 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1314 
1315 /**
1316  * drm_atomic_helper_swap_state - store atomic state into current sw state
1317  * @dev: DRM device
1318  * @state: atomic state
1319  *
1320  * This function stores the atomic state into the current state pointers in all
1321  * driver objects. It should be called after all failing steps have been done
1322  * and succeeded, but before the actual hardware state is committed.
1323  *
1324  * For cleanup and error recovery the current state for all changed objects will
1325  * be swaped into @state.
1326  *
1327  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1328  *
1329  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1330  *
1331  * 2. Do any other steps that might fail.
1332  *
1333  * 3. Put the staged state into the current state pointers with this function.
1334  *
1335  * 4. Actually commit the hardware state.
1336  *
1337  * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1338  * contains the old state. Also do any other cleanup required with that state.
1339  */
1340 void drm_atomic_helper_swap_state(struct drm_device *dev,
1341 				  struct drm_atomic_state *state)
1342 {
1343 	int i;
1344 
1345 	for (i = 0; i < dev->mode_config.num_connector; i++) {
1346 		struct drm_connector *connector = state->connectors[i];
1347 
1348 		if (!connector)
1349 			continue;
1350 
1351 		connector->state->state = state;
1352 		swap(state->connector_states[i], connector->state);
1353 		connector->state->state = NULL;
1354 	}
1355 
1356 	for (i = 0; i < dev->mode_config.num_crtc; i++) {
1357 		struct drm_crtc *crtc = state->crtcs[i];
1358 
1359 		if (!crtc)
1360 			continue;
1361 
1362 		crtc->state->state = state;
1363 		swap(state->crtc_states[i], crtc->state);
1364 		crtc->state->state = NULL;
1365 	}
1366 
1367 	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1368 		struct drm_plane *plane = state->planes[i];
1369 
1370 		if (!plane)
1371 			continue;
1372 
1373 		plane->state->state = state;
1374 		swap(state->plane_states[i], plane->state);
1375 		plane->state->state = NULL;
1376 	}
1377 }
1378 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1379 
1380 /**
1381  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1382  * @plane: plane object to update
1383  * @crtc: owning CRTC of owning plane
1384  * @fb: framebuffer to flip onto plane
1385  * @crtc_x: x offset of primary plane on crtc
1386  * @crtc_y: y offset of primary plane on crtc
1387  * @crtc_w: width of primary plane rectangle on crtc
1388  * @crtc_h: height of primary plane rectangle on crtc
1389  * @src_x: x offset of @fb for panning
1390  * @src_y: y offset of @fb for panning
1391  * @src_w: width of source rectangle in @fb
1392  * @src_h: height of source rectangle in @fb
1393  *
1394  * Provides a default plane update handler using the atomic driver interface.
1395  *
1396  * RETURNS:
1397  * Zero on success, error code on failure
1398  */
1399 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1400 				   struct drm_crtc *crtc,
1401 				   struct drm_framebuffer *fb,
1402 				   int crtc_x, int crtc_y,
1403 				   unsigned int crtc_w, unsigned int crtc_h,
1404 				   uint32_t src_x, uint32_t src_y,
1405 				   uint32_t src_w, uint32_t src_h)
1406 {
1407 	struct drm_atomic_state *state;
1408 	struct drm_plane_state *plane_state;
1409 	int ret = 0;
1410 
1411 	state = drm_atomic_state_alloc(plane->dev);
1412 	if (!state)
1413 		return -ENOMEM;
1414 
1415 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1416 retry:
1417 	plane_state = drm_atomic_get_plane_state(state, plane);
1418 	if (IS_ERR(plane_state)) {
1419 		ret = PTR_ERR(plane_state);
1420 		goto fail;
1421 	}
1422 
1423 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1424 	if (ret != 0)
1425 		goto fail;
1426 	drm_atomic_set_fb_for_plane(plane_state, fb);
1427 	plane_state->crtc_x = crtc_x;
1428 	plane_state->crtc_y = crtc_y;
1429 	plane_state->crtc_h = crtc_h;
1430 	plane_state->crtc_w = crtc_w;
1431 	plane_state->src_x = src_x;
1432 	plane_state->src_y = src_y;
1433 	plane_state->src_h = src_h;
1434 	plane_state->src_w = src_w;
1435 
1436 	if (plane == crtc->cursor)
1437 		state->legacy_cursor_update = true;
1438 
1439 	ret = drm_atomic_commit(state);
1440 	if (ret != 0)
1441 		goto fail;
1442 
1443 	/* Driver takes ownership of state on successful commit. */
1444 	return 0;
1445 fail:
1446 	if (ret == -EDEADLK)
1447 		goto backoff;
1448 
1449 	drm_atomic_state_free(state);
1450 
1451 	return ret;
1452 backoff:
1453 	drm_atomic_state_clear(state);
1454 	drm_atomic_legacy_backoff(state);
1455 
1456 	/*
1457 	 * Someone might have exchanged the framebuffer while we dropped locks
1458 	 * in the backoff code. We need to fix up the fb refcount tracking the
1459 	 * core does for us.
1460 	 */
1461 	plane->old_fb = plane->fb;
1462 
1463 	goto retry;
1464 }
1465 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1466 
1467 /**
1468  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1469  * @plane: plane to disable
1470  *
1471  * Provides a default plane disable handler using the atomic driver interface.
1472  *
1473  * RETURNS:
1474  * Zero on success, error code on failure
1475  */
1476 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1477 {
1478 	struct drm_atomic_state *state;
1479 	struct drm_plane_state *plane_state;
1480 	int ret = 0;
1481 
1482 	/*
1483 	 * FIXME: Without plane->crtc set we can't get at the implicit legacy
1484 	 * acquire context. The real fix will be to wire the acquire ctx through
1485 	 * everywhere we need it, but meanwhile prevent chaos by just skipping
1486 	 * this noop. The critical case is the cursor ioctls which a) only grab
1487 	 * crtc/cursor-plane locks (so we need the crtc to get at the right
1488 	 * acquire context) and b) can try to disable the plane multiple times.
1489 	 */
1490 	if (!plane->crtc)
1491 		return 0;
1492 
1493 	state = drm_atomic_state_alloc(plane->dev);
1494 	if (!state)
1495 		return -ENOMEM;
1496 
1497 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1498 retry:
1499 	plane_state = drm_atomic_get_plane_state(state, plane);
1500 	if (IS_ERR(plane_state)) {
1501 		ret = PTR_ERR(plane_state);
1502 		goto fail;
1503 	}
1504 
1505 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1506 	if (ret != 0)
1507 		goto fail;
1508 	drm_atomic_set_fb_for_plane(plane_state, NULL);
1509 	plane_state->crtc_x = 0;
1510 	plane_state->crtc_y = 0;
1511 	plane_state->crtc_h = 0;
1512 	plane_state->crtc_w = 0;
1513 	plane_state->src_x = 0;
1514 	plane_state->src_y = 0;
1515 	plane_state->src_h = 0;
1516 	plane_state->src_w = 0;
1517 
1518 	if (plane == plane->crtc->cursor)
1519 		state->legacy_cursor_update = true;
1520 
1521 	ret = drm_atomic_commit(state);
1522 	if (ret != 0)
1523 		goto fail;
1524 
1525 	/* Driver takes ownership of state on successful commit. */
1526 	return 0;
1527 fail:
1528 	if (ret == -EDEADLK)
1529 		goto backoff;
1530 
1531 	drm_atomic_state_free(state);
1532 
1533 	return ret;
1534 backoff:
1535 	drm_atomic_state_clear(state);
1536 	drm_atomic_legacy_backoff(state);
1537 
1538 	/*
1539 	 * Someone might have exchanged the framebuffer while we dropped locks
1540 	 * in the backoff code. We need to fix up the fb refcount tracking the
1541 	 * core does for us.
1542 	 */
1543 	plane->old_fb = plane->fb;
1544 
1545 	goto retry;
1546 }
1547 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1548 
1549 static int update_output_state(struct drm_atomic_state *state,
1550 			       struct drm_mode_set *set)
1551 {
1552 	struct drm_device *dev = set->crtc->dev;
1553 	struct drm_crtc *crtc;
1554 	struct drm_crtc_state *crtc_state;
1555 	struct drm_connector *connector;
1556 	struct drm_connector_state *conn_state;
1557 	int ret, i, j;
1558 
1559 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1560 			       state->acquire_ctx);
1561 	if (ret)
1562 		return ret;
1563 
1564 	/* First grab all affected connector/crtc states. */
1565 	for (i = 0; i < set->num_connectors; i++) {
1566 		conn_state = drm_atomic_get_connector_state(state,
1567 							    set->connectors[i]);
1568 		if (IS_ERR(conn_state))
1569 			return PTR_ERR(conn_state);
1570 	}
1571 
1572 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1573 		ret = drm_atomic_add_affected_connectors(state, crtc);
1574 		if (ret)
1575 			return ret;
1576 	}
1577 
1578 	/* Then recompute connector->crtc links and crtc enabling state. */
1579 	for_each_connector_in_state(state, connector, conn_state, i) {
1580 		if (conn_state->crtc == set->crtc) {
1581 			ret = drm_atomic_set_crtc_for_connector(conn_state,
1582 								NULL);
1583 			if (ret)
1584 				return ret;
1585 		}
1586 
1587 		for (j = 0; j < set->num_connectors; j++) {
1588 			if (set->connectors[j] == connector) {
1589 				ret = drm_atomic_set_crtc_for_connector(conn_state,
1590 									set->crtc);
1591 				if (ret)
1592 					return ret;
1593 				break;
1594 			}
1595 		}
1596 	}
1597 
1598 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1599 		/* Don't update ->enable for the CRTC in the set_config request,
1600 		 * since a mismatch would indicate a bug in the upper layers.
1601 		 * The actual modeset code later on will catch any
1602 		 * inconsistencies here. */
1603 		if (crtc == set->crtc)
1604 			continue;
1605 
1606 		if (!drm_atomic_connectors_for_crtc(state, crtc)) {
1607 			ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
1608 								NULL);
1609 			if (ret < 0)
1610 				return ret;
1611 
1612 			crtc_state->active = false;
1613 		}
1614 	}
1615 
1616 	return 0;
1617 }
1618 
1619 /**
1620  * drm_atomic_helper_set_config - set a new config from userspace
1621  * @set: mode set configuration
1622  *
1623  * Provides a default crtc set_config handler using the atomic driver interface.
1624  *
1625  * Returns:
1626  * Returns 0 on success, negative errno numbers on failure.
1627  */
1628 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1629 {
1630 	struct drm_atomic_state *state;
1631 	struct drm_crtc *crtc = set->crtc;
1632 	struct drm_crtc_state *crtc_state;
1633 	struct drm_plane_state *primary_state;
1634 	int ret = 0;
1635 
1636 	state = drm_atomic_state_alloc(crtc->dev);
1637 	if (!state)
1638 		return -ENOMEM;
1639 
1640 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1641 retry:
1642 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1643 	if (IS_ERR(crtc_state)) {
1644 		ret = PTR_ERR(crtc_state);
1645 		goto fail;
1646 	}
1647 
1648 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1649 	if (IS_ERR(primary_state)) {
1650 		ret = PTR_ERR(primary_state);
1651 		goto fail;
1652 	}
1653 
1654 	if (!set->mode) {
1655 		WARN_ON(set->fb);
1656 		WARN_ON(set->num_connectors);
1657 
1658 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
1659 		if (ret != 0)
1660 			goto fail;
1661 
1662 		crtc_state->active = false;
1663 
1664 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
1665 		if (ret != 0)
1666 			goto fail;
1667 
1668 		drm_atomic_set_fb_for_plane(primary_state, NULL);
1669 
1670 		goto commit;
1671 	}
1672 
1673 	WARN_ON(!set->fb);
1674 	WARN_ON(!set->num_connectors);
1675 
1676 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
1677 	if (ret != 0)
1678 		goto fail;
1679 
1680 	crtc_state->active = true;
1681 
1682 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1683 	if (ret != 0)
1684 		goto fail;
1685 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
1686 	primary_state->crtc_x = 0;
1687 	primary_state->crtc_y = 0;
1688 	primary_state->crtc_h = set->mode->vdisplay;
1689 	primary_state->crtc_w = set->mode->hdisplay;
1690 	primary_state->src_x = set->x << 16;
1691 	primary_state->src_y = set->y << 16;
1692 	primary_state->src_h = set->mode->vdisplay << 16;
1693 	primary_state->src_w = set->mode->hdisplay << 16;
1694 
1695 commit:
1696 	ret = update_output_state(state, set);
1697 	if (ret)
1698 		goto fail;
1699 
1700 	ret = drm_atomic_commit(state);
1701 	if (ret != 0)
1702 		goto fail;
1703 
1704 	/* Driver takes ownership of state on successful commit. */
1705 	return 0;
1706 fail:
1707 	if (ret == -EDEADLK)
1708 		goto backoff;
1709 
1710 	drm_atomic_state_free(state);
1711 
1712 	return ret;
1713 backoff:
1714 	drm_atomic_state_clear(state);
1715 	drm_atomic_legacy_backoff(state);
1716 
1717 	/*
1718 	 * Someone might have exchanged the framebuffer while we dropped locks
1719 	 * in the backoff code. We need to fix up the fb refcount tracking the
1720 	 * core does for us.
1721 	 */
1722 	crtc->primary->old_fb = crtc->primary->fb;
1723 
1724 	goto retry;
1725 }
1726 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1727 
1728 /**
1729  * drm_atomic_helper_crtc_set_property - helper for crtc properties
1730  * @crtc: DRM crtc
1731  * @property: DRM property
1732  * @val: value of property
1733  *
1734  * Provides a default crtc set_property handler using the atomic driver
1735  * interface.
1736  *
1737  * RETURNS:
1738  * Zero on success, error code on failure
1739  */
1740 int
1741 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1742 				    struct drm_property *property,
1743 				    uint64_t val)
1744 {
1745 	struct drm_atomic_state *state;
1746 	struct drm_crtc_state *crtc_state;
1747 	int ret = 0;
1748 
1749 	state = drm_atomic_state_alloc(crtc->dev);
1750 	if (!state)
1751 		return -ENOMEM;
1752 
1753 	/* ->set_property is always called with all locks held. */
1754 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1755 retry:
1756 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1757 	if (IS_ERR(crtc_state)) {
1758 		ret = PTR_ERR(crtc_state);
1759 		goto fail;
1760 	}
1761 
1762 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
1763 			property, val);
1764 	if (ret)
1765 		goto fail;
1766 
1767 	ret = drm_atomic_commit(state);
1768 	if (ret != 0)
1769 		goto fail;
1770 
1771 	/* Driver takes ownership of state on successful commit. */
1772 	return 0;
1773 fail:
1774 	if (ret == -EDEADLK)
1775 		goto backoff;
1776 
1777 	drm_atomic_state_free(state);
1778 
1779 	return ret;
1780 backoff:
1781 	drm_atomic_state_clear(state);
1782 	drm_atomic_legacy_backoff(state);
1783 
1784 	goto retry;
1785 }
1786 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1787 
1788 /**
1789  * drm_atomic_helper_plane_set_property - helper for plane properties
1790  * @plane: DRM plane
1791  * @property: DRM property
1792  * @val: value of property
1793  *
1794  * Provides a default plane set_property handler using the atomic driver
1795  * interface.
1796  *
1797  * RETURNS:
1798  * Zero on success, error code on failure
1799  */
1800 int
1801 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1802 				    struct drm_property *property,
1803 				    uint64_t val)
1804 {
1805 	struct drm_atomic_state *state;
1806 	struct drm_plane_state *plane_state;
1807 	int ret = 0;
1808 
1809 	state = drm_atomic_state_alloc(plane->dev);
1810 	if (!state)
1811 		return -ENOMEM;
1812 
1813 	/* ->set_property is always called with all locks held. */
1814 	state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1815 retry:
1816 	plane_state = drm_atomic_get_plane_state(state, plane);
1817 	if (IS_ERR(plane_state)) {
1818 		ret = PTR_ERR(plane_state);
1819 		goto fail;
1820 	}
1821 
1822 	ret = drm_atomic_plane_set_property(plane, plane_state,
1823 			property, val);
1824 	if (ret)
1825 		goto fail;
1826 
1827 	ret = drm_atomic_commit(state);
1828 	if (ret != 0)
1829 		goto fail;
1830 
1831 	/* Driver takes ownership of state on successful commit. */
1832 	return 0;
1833 fail:
1834 	if (ret == -EDEADLK)
1835 		goto backoff;
1836 
1837 	drm_atomic_state_free(state);
1838 
1839 	return ret;
1840 backoff:
1841 	drm_atomic_state_clear(state);
1842 	drm_atomic_legacy_backoff(state);
1843 
1844 	goto retry;
1845 }
1846 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1847 
1848 /**
1849  * drm_atomic_helper_connector_set_property - helper for connector properties
1850  * @connector: DRM connector
1851  * @property: DRM property
1852  * @val: value of property
1853  *
1854  * Provides a default connector set_property handler using the atomic driver
1855  * interface.
1856  *
1857  * RETURNS:
1858  * Zero on success, error code on failure
1859  */
1860 int
1861 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1862 				    struct drm_property *property,
1863 				    uint64_t val)
1864 {
1865 	struct drm_atomic_state *state;
1866 	struct drm_connector_state *connector_state;
1867 	int ret = 0;
1868 
1869 	state = drm_atomic_state_alloc(connector->dev);
1870 	if (!state)
1871 		return -ENOMEM;
1872 
1873 	/* ->set_property is always called with all locks held. */
1874 	state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1875 retry:
1876 	connector_state = drm_atomic_get_connector_state(state, connector);
1877 	if (IS_ERR(connector_state)) {
1878 		ret = PTR_ERR(connector_state);
1879 		goto fail;
1880 	}
1881 
1882 	ret = drm_atomic_connector_set_property(connector, connector_state,
1883 			property, val);
1884 	if (ret)
1885 		goto fail;
1886 
1887 	ret = drm_atomic_commit(state);
1888 	if (ret != 0)
1889 		goto fail;
1890 
1891 	/* Driver takes ownership of state on successful commit. */
1892 	return 0;
1893 fail:
1894 	if (ret == -EDEADLK)
1895 		goto backoff;
1896 
1897 	drm_atomic_state_free(state);
1898 
1899 	return ret;
1900 backoff:
1901 	drm_atomic_state_clear(state);
1902 	drm_atomic_legacy_backoff(state);
1903 
1904 	goto retry;
1905 }
1906 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1907 
1908 /**
1909  * drm_atomic_helper_page_flip - execute a legacy page flip
1910  * @crtc: DRM crtc
1911  * @fb: DRM framebuffer
1912  * @event: optional DRM event to signal upon completion
1913  * @flags: flip flags for non-vblank sync'ed updates
1914  *
1915  * Provides a default page flip implementation using the atomic driver interface.
1916  *
1917  * Note that for now so called async page flips (i.e. updates which are not
1918  * synchronized to vblank) are not supported, since the atomic interfaces have
1919  * no provisions for this yet.
1920  *
1921  * Returns:
1922  * Returns 0 on success, negative errno numbers on failure.
1923  */
1924 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
1925 				struct drm_framebuffer *fb,
1926 				struct drm_pending_vblank_event *event,
1927 				uint32_t flags)
1928 {
1929 	struct drm_plane *plane = crtc->primary;
1930 	struct drm_atomic_state *state;
1931 	struct drm_plane_state *plane_state;
1932 	struct drm_crtc_state *crtc_state;
1933 	int ret = 0;
1934 
1935 	if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
1936 		return -EINVAL;
1937 
1938 	state = drm_atomic_state_alloc(plane->dev);
1939 	if (!state)
1940 		return -ENOMEM;
1941 
1942 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1943 retry:
1944 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1945 	if (IS_ERR(crtc_state)) {
1946 		ret = PTR_ERR(crtc_state);
1947 		goto fail;
1948 	}
1949 	crtc_state->event = event;
1950 
1951 	plane_state = drm_atomic_get_plane_state(state, plane);
1952 	if (IS_ERR(plane_state)) {
1953 		ret = PTR_ERR(plane_state);
1954 		goto fail;
1955 	}
1956 
1957 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1958 	if (ret != 0)
1959 		goto fail;
1960 	drm_atomic_set_fb_for_plane(plane_state, fb);
1961 
1962 	ret = drm_atomic_async_commit(state);
1963 	if (ret != 0)
1964 		goto fail;
1965 
1966 	/* Driver takes ownership of state on successful async commit. */
1967 	return 0;
1968 fail:
1969 	if (ret == -EDEADLK)
1970 		goto backoff;
1971 
1972 	drm_atomic_state_free(state);
1973 
1974 	return ret;
1975 backoff:
1976 	drm_atomic_state_clear(state);
1977 	drm_atomic_legacy_backoff(state);
1978 
1979 	/*
1980 	 * Someone might have exchanged the framebuffer while we dropped locks
1981 	 * in the backoff code. We need to fix up the fb refcount tracking the
1982 	 * core does for us.
1983 	 */
1984 	plane->old_fb = plane->fb;
1985 
1986 	goto retry;
1987 }
1988 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
1989 
1990 /**
1991  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
1992  * @connector: affected connector
1993  * @mode: DPMS mode
1994  *
1995  * This is the main helper function provided by the atomic helper framework for
1996  * implementing the legacy DPMS connector interface. It computes the new desired
1997  * ->active state for the corresponding CRTC (if the connector is enabled) and
1998  *  updates it.
1999  *
2000  * Returns:
2001  * Returns 0 on success, negative errno numbers on failure.
2002  */
2003 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
2004 				     int mode)
2005 {
2006 	struct drm_mode_config *config = &connector->dev->mode_config;
2007 	struct drm_atomic_state *state;
2008 	struct drm_crtc_state *crtc_state;
2009 	struct drm_crtc *crtc;
2010 	struct drm_connector *tmp_connector;
2011 	int ret;
2012 	bool active = false;
2013 	int old_mode = connector->dpms;
2014 
2015 	if (mode != DRM_MODE_DPMS_ON)
2016 		mode = DRM_MODE_DPMS_OFF;
2017 
2018 	connector->dpms = mode;
2019 	crtc = connector->state->crtc;
2020 
2021 	if (!crtc)
2022 		return 0;
2023 
2024 	state = drm_atomic_state_alloc(connector->dev);
2025 	if (!state)
2026 		return -ENOMEM;
2027 
2028 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
2029 retry:
2030 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
2031 	if (IS_ERR(crtc_state)) {
2032 		ret = PTR_ERR(crtc_state);
2033 		goto fail;
2034 	}
2035 
2036 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
2037 
2038 	drm_for_each_connector(tmp_connector, connector->dev) {
2039 		if (tmp_connector->state->crtc != crtc)
2040 			continue;
2041 
2042 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
2043 			active = true;
2044 			break;
2045 		}
2046 	}
2047 	crtc_state->active = active;
2048 
2049 	ret = drm_atomic_commit(state);
2050 	if (ret != 0)
2051 		goto fail;
2052 
2053 	/* Driver takes ownership of state on successful commit. */
2054 	return 0;
2055 fail:
2056 	if (ret == -EDEADLK)
2057 		goto backoff;
2058 
2059 	connector->dpms = old_mode;
2060 	drm_atomic_state_free(state);
2061 
2062 	return ret;
2063 backoff:
2064 	drm_atomic_state_clear(state);
2065 	drm_atomic_legacy_backoff(state);
2066 
2067 	goto retry;
2068 }
2069 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
2070 
2071 /**
2072  * DOC: atomic state reset and initialization
2073  *
2074  * Both the drm core and the atomic helpers assume that there is always the full
2075  * and correct atomic software state for all connectors, CRTCs and planes
2076  * available. Which is a bit a problem on driver load and also after system
2077  * suspend. One way to solve this is to have a hardware state read-out
2078  * infrastructure which reconstructs the full software state (e.g. the i915
2079  * driver).
2080  *
2081  * The simpler solution is to just reset the software state to everything off,
2082  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
2083  * the atomic helpers provide default reset implementations for all hooks.
2084  */
2085 
2086 /**
2087  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
2088  * @crtc: drm CRTC
2089  *
2090  * Resets the atomic state for @crtc by freeing the state pointer (which might
2091  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2092  */
2093 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
2094 {
2095 	if (crtc->state && crtc->state->mode_blob)
2096 		drm_property_unreference_blob(crtc->state->mode_blob);
2097 	kfree(crtc->state);
2098 	crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
2099 
2100 	if (crtc->state)
2101 		crtc->state->crtc = crtc;
2102 }
2103 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
2104 
2105 /**
2106  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
2107  * @crtc: CRTC object
2108  * @state: atomic CRTC state
2109  *
2110  * Copies atomic state from a CRTC's current state and resets inferred values.
2111  * This is useful for drivers that subclass the CRTC state.
2112  */
2113 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
2114 					      struct drm_crtc_state *state)
2115 {
2116 	memcpy(state, crtc->state, sizeof(*state));
2117 
2118 	if (state->mode_blob)
2119 		drm_property_reference_blob(state->mode_blob);
2120 	state->mode_changed = false;
2121 	state->active_changed = false;
2122 	state->planes_changed = false;
2123 	state->connectors_changed = false;
2124 	state->event = NULL;
2125 }
2126 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
2127 
2128 /**
2129  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
2130  * @crtc: drm CRTC
2131  *
2132  * Default CRTC state duplicate hook for drivers which don't have their own
2133  * subclassed CRTC state structure.
2134  */
2135 struct drm_crtc_state *
2136 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
2137 {
2138 	struct drm_crtc_state *state;
2139 
2140 	if (WARN_ON(!crtc->state))
2141 		return NULL;
2142 
2143 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2144 	if (state)
2145 		__drm_atomic_helper_crtc_duplicate_state(crtc, state);
2146 
2147 	return state;
2148 }
2149 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
2150 
2151 /**
2152  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
2153  * @crtc: CRTC object
2154  * @state: CRTC state object to release
2155  *
2156  * Releases all resources stored in the CRTC state without actually freeing
2157  * the memory of the CRTC state. This is useful for drivers that subclass the
2158  * CRTC state.
2159  */
2160 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2161 					    struct drm_crtc_state *state)
2162 {
2163 	if (state->mode_blob)
2164 		drm_property_unreference_blob(state->mode_blob);
2165 }
2166 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
2167 
2168 /**
2169  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
2170  * @crtc: drm CRTC
2171  * @state: CRTC state object to release
2172  *
2173  * Default CRTC state destroy hook for drivers which don't have their own
2174  * subclassed CRTC state structure.
2175  */
2176 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
2177 					  struct drm_crtc_state *state)
2178 {
2179 	__drm_atomic_helper_crtc_destroy_state(crtc, state);
2180 	kfree(state);
2181 }
2182 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
2183 
2184 /**
2185  * drm_atomic_helper_plane_reset - default ->reset hook for planes
2186  * @plane: drm plane
2187  *
2188  * Resets the atomic state for @plane by freeing the state pointer (which might
2189  * be NULL, e.g. at driver load time) and allocating a new empty state object.
2190  */
2191 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
2192 {
2193 	if (plane->state && plane->state->fb)
2194 		drm_framebuffer_unreference(plane->state->fb);
2195 
2196 	kfree(plane->state);
2197 	plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
2198 
2199 	if (plane->state)
2200 		plane->state->plane = plane;
2201 }
2202 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
2203 
2204 /**
2205  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
2206  * @plane: plane object
2207  * @state: atomic plane state
2208  *
2209  * Copies atomic state from a plane's current state. This is useful for
2210  * drivers that subclass the plane state.
2211  */
2212 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
2213 					       struct drm_plane_state *state)
2214 {
2215 	memcpy(state, plane->state, sizeof(*state));
2216 
2217 	if (state->fb)
2218 		drm_framebuffer_reference(state->fb);
2219 }
2220 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
2221 
2222 /**
2223  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
2224  * @plane: drm plane
2225  *
2226  * Default plane state duplicate hook for drivers which don't have their own
2227  * subclassed plane state structure.
2228  */
2229 struct drm_plane_state *
2230 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
2231 {
2232 	struct drm_plane_state *state;
2233 
2234 	if (WARN_ON(!plane->state))
2235 		return NULL;
2236 
2237 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2238 	if (state)
2239 		__drm_atomic_helper_plane_duplicate_state(plane, state);
2240 
2241 	return state;
2242 }
2243 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
2244 
2245 /**
2246  * __drm_atomic_helper_plane_destroy_state - release plane state
2247  * @plane: plane object
2248  * @state: plane state object to release
2249  *
2250  * Releases all resources stored in the plane state without actually freeing
2251  * the memory of the plane state. This is useful for drivers that subclass the
2252  * plane state.
2253  */
2254 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2255 					     struct drm_plane_state *state)
2256 {
2257 	if (state->fb)
2258 		drm_framebuffer_unreference(state->fb);
2259 }
2260 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
2261 
2262 /**
2263  * drm_atomic_helper_plane_destroy_state - default state destroy hook
2264  * @plane: drm plane
2265  * @state: plane state object to release
2266  *
2267  * Default plane state destroy hook for drivers which don't have their own
2268  * subclassed plane state structure.
2269  */
2270 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
2271 					   struct drm_plane_state *state)
2272 {
2273 	__drm_atomic_helper_plane_destroy_state(plane, state);
2274 	kfree(state);
2275 }
2276 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
2277 
2278 /**
2279  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
2280  * @connector: drm connector
2281  *
2282  * Resets the atomic state for @connector by freeing the state pointer (which
2283  * might be NULL, e.g. at driver load time) and allocating a new empty state
2284  * object.
2285  */
2286 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
2287 {
2288 	kfree(connector->state);
2289 	connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
2290 
2291 	if (connector->state)
2292 		connector->state->connector = connector;
2293 }
2294 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
2295 
2296 /**
2297  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
2298  * @connector: connector object
2299  * @state: atomic connector state
2300  *
2301  * Copies atomic state from a connector's current state. This is useful for
2302  * drivers that subclass the connector state.
2303  */
2304 void
2305 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
2306 					    struct drm_connector_state *state)
2307 {
2308 	memcpy(state, connector->state, sizeof(*state));
2309 }
2310 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
2311 
2312 /**
2313  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
2314  * @connector: drm connector
2315  *
2316  * Default connector state duplicate hook for drivers which don't have their own
2317  * subclassed connector state structure.
2318  */
2319 struct drm_connector_state *
2320 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
2321 {
2322 	struct drm_connector_state *state;
2323 
2324 	if (WARN_ON(!connector->state))
2325 		return NULL;
2326 
2327 	state = kmalloc(sizeof(*state), GFP_KERNEL);
2328 	if (state)
2329 		__drm_atomic_helper_connector_duplicate_state(connector, state);
2330 
2331 	return state;
2332 }
2333 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
2334 
2335 /**
2336  * __drm_atomic_helper_connector_destroy_state - release connector state
2337  * @connector: connector object
2338  * @state: connector state object to release
2339  *
2340  * Releases all resources stored in the connector state without actually
2341  * freeing the memory of the connector state. This is useful for drivers that
2342  * subclass the connector state.
2343  */
2344 void
2345 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2346 					    struct drm_connector_state *state)
2347 {
2348 	/*
2349 	 * This is currently a placeholder so that drivers that subclass the
2350 	 * state will automatically do the right thing if code is ever added
2351 	 * to this function.
2352 	 */
2353 }
2354 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
2355 
2356 /**
2357  * drm_atomic_helper_connector_destroy_state - default state destroy hook
2358  * @connector: drm connector
2359  * @state: connector state object to release
2360  *
2361  * Default connector state destroy hook for drivers which don't have their own
2362  * subclassed connector state structure.
2363  */
2364 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
2365 					  struct drm_connector_state *state)
2366 {
2367 	__drm_atomic_helper_connector_destroy_state(connector, state);
2368 	kfree(state);
2369 }
2370 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
2371