xref: /linux/drivers/gpu/drm/drm_atomic_helper.c (revision e2330f0719515e41090a4d9685b931888b7c01d6)
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 static void
36 drm_atomic_helper_plane_changed(struct drm_atomic_state *state,
37 				struct drm_plane_state *plane_state,
38 				struct drm_plane *plane)
39 {
40 	struct drm_crtc_state *crtc_state;
41 
42 	if (plane->state->crtc) {
43 		crtc_state = state->crtc_states[drm_crtc_index(plane->crtc)];
44 
45 		if (WARN_ON(!crtc_state))
46 			return;
47 
48 		crtc_state->planes_changed = true;
49 	}
50 
51 	if (plane_state->crtc) {
52 		crtc_state =
53 			state->crtc_states[drm_crtc_index(plane_state->crtc)];
54 
55 		if (WARN_ON(!crtc_state))
56 			return;
57 
58 		crtc_state->planes_changed = true;
59 	}
60 }
61 
62 static struct drm_crtc *
63 get_current_crtc_for_encoder(struct drm_device *dev,
64 			     struct drm_encoder *encoder)
65 {
66 	struct drm_mode_config *config = &dev->mode_config;
67 	struct drm_connector *connector;
68 
69 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
70 
71 	list_for_each_entry(connector, &config->connector_list, head) {
72 		if (connector->state->best_encoder != encoder)
73 			continue;
74 
75 		return connector->state->crtc;
76 	}
77 
78 	return NULL;
79 }
80 
81 static int
82 steal_encoder(struct drm_atomic_state *state,
83 	      struct drm_encoder *encoder,
84 	      struct drm_crtc *encoder_crtc)
85 {
86 	struct drm_mode_config *config = &state->dev->mode_config;
87 	struct drm_crtc_state *crtc_state;
88 	struct drm_connector *connector;
89 	struct drm_connector_state *connector_state;
90 	int ret;
91 
92 	/*
93 	 * We can only steal an encoder coming from a connector, which means we
94 	 * must already hold the connection_mutex.
95 	 */
96 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
97 
98 	DRM_DEBUG_KMS("[ENCODER:%d:%s] in use on [CRTC:%d], stealing it\n",
99 		      encoder->base.id, encoder->name,
100 		      encoder_crtc->base.id);
101 
102 	crtc_state = drm_atomic_get_crtc_state(state, encoder_crtc);
103 	if (IS_ERR(crtc_state))
104 		return PTR_ERR(crtc_state);
105 
106 	crtc_state->mode_changed = true;
107 
108 	list_for_each_entry(connector, &config->connector_list, head) {
109 		if (connector->state->best_encoder != encoder)
110 			continue;
111 
112 		DRM_DEBUG_KMS("Stealing encoder from [CONNECTOR:%d:%s]\n",
113 			      connector->base.id,
114 			      connector->name);
115 
116 		connector_state = drm_atomic_get_connector_state(state,
117 								 connector);
118 		if (IS_ERR(connector_state))
119 			return PTR_ERR(connector_state);
120 
121 		ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
122 		if (ret)
123 			return ret;
124 		connector_state->best_encoder = NULL;
125 	}
126 
127 	return 0;
128 }
129 
130 static int
131 update_connector_routing(struct drm_atomic_state *state, int conn_idx)
132 {
133 	struct drm_connector_helper_funcs *funcs;
134 	struct drm_encoder *new_encoder;
135 	struct drm_crtc *encoder_crtc;
136 	struct drm_connector *connector;
137 	struct drm_connector_state *connector_state;
138 	struct drm_crtc_state *crtc_state;
139 	int idx, ret;
140 
141 	connector = state->connectors[conn_idx];
142 	connector_state = state->connector_states[conn_idx];
143 
144 	if (!connector)
145 		return 0;
146 
147 	DRM_DEBUG_KMS("Updating routing for [CONNECTOR:%d:%s]\n",
148 			connector->base.id,
149 			connector->name);
150 
151 	if (connector->state->crtc != connector_state->crtc) {
152 		if (connector->state->crtc) {
153 			idx = drm_crtc_index(connector->state->crtc);
154 
155 			crtc_state = state->crtc_states[idx];
156 			crtc_state->mode_changed = true;
157 		}
158 
159 		if (connector_state->crtc) {
160 			idx = drm_crtc_index(connector_state->crtc);
161 
162 			crtc_state = state->crtc_states[idx];
163 			crtc_state->mode_changed = true;
164 		}
165 	}
166 
167 	if (!connector_state->crtc) {
168 		DRM_DEBUG_KMS("Disabling [CONNECTOR:%d:%s]\n",
169 				connector->base.id,
170 				connector->name);
171 
172 		connector_state->best_encoder = NULL;
173 
174 		return 0;
175 	}
176 
177 	funcs = connector->helper_private;
178 	new_encoder = funcs->best_encoder(connector);
179 
180 	if (!new_encoder) {
181 		DRM_DEBUG_KMS("No suitable encoder found for [CONNECTOR:%d:%s]\n",
182 			      connector->base.id,
183 			      connector->name);
184 		return -EINVAL;
185 	}
186 
187 	if (new_encoder == connector_state->best_encoder) {
188 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] keeps [ENCODER:%d:%s], now on [CRTC:%d]\n",
189 			      connector->base.id,
190 			      connector->name,
191 			      new_encoder->base.id,
192 			      new_encoder->name,
193 			      connector_state->crtc->base.id);
194 
195 		return 0;
196 	}
197 
198 	encoder_crtc = get_current_crtc_for_encoder(state->dev,
199 						    new_encoder);
200 
201 	if (encoder_crtc) {
202 		ret = steal_encoder(state, new_encoder, encoder_crtc);
203 		if (ret) {
204 			DRM_DEBUG_KMS("Encoder stealing failed for [CONNECTOR:%d:%s]\n",
205 				      connector->base.id,
206 				      connector->name);
207 			return ret;
208 		}
209 	}
210 
211 	connector_state->best_encoder = new_encoder;
212 	idx = drm_crtc_index(connector_state->crtc);
213 
214 	crtc_state = state->crtc_states[idx];
215 	crtc_state->mode_changed = true;
216 
217 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using [ENCODER:%d:%s] on [CRTC:%d]\n",
218 		      connector->base.id,
219 		      connector->name,
220 		      new_encoder->base.id,
221 		      new_encoder->name,
222 		      connector_state->crtc->base.id);
223 
224 	return 0;
225 }
226 
227 static int
228 mode_fixup(struct drm_atomic_state *state)
229 {
230 	int ncrtcs = state->dev->mode_config.num_crtc;
231 	int nconnectors = state->dev->mode_config.num_connector;
232 	struct drm_crtc_state *crtc_state;
233 	struct drm_connector_state *conn_state;
234 	int i;
235 	bool ret;
236 
237 	for (i = 0; i < ncrtcs; i++) {
238 		crtc_state = state->crtc_states[i];
239 
240 		if (!crtc_state || !crtc_state->mode_changed)
241 			continue;
242 
243 		drm_mode_copy(&crtc_state->adjusted_mode, &crtc_state->mode);
244 	}
245 
246 	for (i = 0; i < nconnectors; i++) {
247 		struct drm_encoder_helper_funcs *funcs;
248 		struct drm_encoder *encoder;
249 
250 		conn_state = state->connector_states[i];
251 
252 		if (!conn_state)
253 			continue;
254 
255 		WARN_ON(!!conn_state->best_encoder != !!conn_state->crtc);
256 
257 		if (!conn_state->crtc || !conn_state->best_encoder)
258 			continue;
259 
260 		crtc_state =
261 			state->crtc_states[drm_crtc_index(conn_state->crtc)];
262 
263 		/*
264 		 * Each encoder has at most one connector (since we always steal
265 		 * it away), so we won't call ->mode_fixup twice.
266 		 */
267 		encoder = conn_state->best_encoder;
268 		funcs = encoder->helper_private;
269 
270 		if (encoder->bridge && encoder->bridge->funcs->mode_fixup) {
271 			ret = encoder->bridge->funcs->mode_fixup(
272 					encoder->bridge, &crtc_state->mode,
273 					&crtc_state->adjusted_mode);
274 			if (!ret) {
275 				DRM_DEBUG_KMS("Bridge fixup failed\n");
276 				return -EINVAL;
277 			}
278 		}
279 
280 
281 		ret = funcs->mode_fixup(encoder, &crtc_state->mode,
282 					&crtc_state->adjusted_mode);
283 		if (!ret) {
284 			DRM_DEBUG_KMS("[ENCODER:%d:%s] fixup failed\n",
285 				      encoder->base.id, encoder->name);
286 			return -EINVAL;
287 		}
288 	}
289 
290 	for (i = 0; i < ncrtcs; i++) {
291 		struct drm_crtc_helper_funcs *funcs;
292 		struct drm_crtc *crtc;
293 
294 		crtc_state = state->crtc_states[i];
295 		crtc = state->crtcs[i];
296 
297 		if (!crtc_state || !crtc_state->mode_changed)
298 			continue;
299 
300 		funcs = crtc->helper_private;
301 		ret = funcs->mode_fixup(crtc, &crtc_state->mode,
302 					&crtc_state->adjusted_mode);
303 		if (!ret) {
304 			DRM_DEBUG_KMS("[CRTC:%d] fixup failed\n",
305 				      crtc->base.id);
306 			return -EINVAL;
307 		}
308 	}
309 
310 	return 0;
311 }
312 
313 static int
314 drm_atomic_helper_check_prepare(struct drm_device *dev,
315 				struct drm_atomic_state *state)
316 {
317 	int ncrtcs = dev->mode_config.num_crtc;
318 	int nconnectors = dev->mode_config.num_connector;
319 	struct drm_crtc *crtc;
320 	struct drm_crtc_state *crtc_state;
321 	int i, ret;
322 
323 	for (i = 0; i < ncrtcs; i++) {
324 		crtc = state->crtcs[i];
325 		crtc_state = state->crtc_states[i];
326 
327 		if (!crtc)
328 			continue;
329 
330 		if (!drm_mode_equal(&crtc->state->mode, &crtc_state->mode)) {
331 			DRM_DEBUG_KMS("[CRTC:%d] mode changed\n",
332 				      crtc->base.id);
333 			crtc_state->mode_changed = true;
334 		}
335 
336 		if (crtc->state->enable != crtc_state->enable) {
337 			DRM_DEBUG_KMS("[CRTC:%d] enable changed\n",
338 				      crtc->base.id);
339 			crtc_state->mode_changed = true;
340 		}
341 	}
342 
343 	for (i = 0; i < nconnectors; i++) {
344 		/*
345 		 * This only sets crtc->mode_changed for routing changes,
346 		 * drivers must set crtc->mode_changed themselves when connector
347 		 * properties need to be updated.
348 		 */
349 		ret = update_connector_routing(state, i);
350 		if (ret)
351 			return ret;
352 	}
353 
354 	/*
355 	 * After all the routing has been prepared we need to add in any
356 	 * connector which is itself unchanged, but who's crtc changes it's
357 	 * configuration. This must be done before calling mode_fixup in case a
358 	 * crtc only changed its mode but has the same set of connectors.
359 	 */
360 	for (i = 0; i < ncrtcs; i++) {
361 		int num_connectors;
362 
363 		crtc = state->crtcs[i];
364 		crtc_state = state->crtc_states[i];
365 
366 		if (!crtc || !crtc_state->mode_changed)
367 			continue;
368 
369 		DRM_DEBUG_KMS("[CRTC:%d] needs full modeset, enable: %c\n",
370 			      crtc->base.id,
371 			      crtc_state->enable ? 'y' : 'n');
372 
373 		ret = drm_atomic_add_affected_connectors(state, crtc);
374 		if (ret != 0)
375 			return ret;
376 
377 		num_connectors = drm_atomic_connectors_for_crtc(state,
378 								crtc);
379 
380 		if (crtc_state->enable != !!num_connectors) {
381 			DRM_DEBUG_KMS("[CRTC:%d] enabled/connectors mismatch\n",
382 				      crtc->base.id);
383 
384 			return -EINVAL;
385 		}
386 	}
387 
388 	return mode_fixup(state);
389 }
390 
391 /**
392  * drm_atomic_helper_check - validate state object
393  * @dev: DRM device
394  * @state: the driver state object
395  *
396  * Check the state object to see if the requested state is physically possible.
397  * Only crtcs and planes have check callbacks, so for any additional (global)
398  * checking that a driver needs it can simply wrap that around this function.
399  * Drivers without such needs can directly use this as their ->atomic_check()
400  * callback.
401  *
402  * RETURNS
403  * Zero for success or -errno
404  */
405 int drm_atomic_helper_check(struct drm_device *dev,
406 			    struct drm_atomic_state *state)
407 {
408 	int nplanes = dev->mode_config.num_total_plane;
409 	int ncrtcs = dev->mode_config.num_crtc;
410 	int i, ret = 0;
411 
412 	ret = drm_atomic_helper_check_prepare(dev, state);
413 	if (ret)
414 		return ret;
415 
416 	for (i = 0; i < nplanes; i++) {
417 		struct drm_plane_helper_funcs *funcs;
418 		struct drm_plane *plane = state->planes[i];
419 		struct drm_plane_state *plane_state = state->plane_states[i];
420 
421 		if (!plane)
422 			continue;
423 
424 		funcs = plane->helper_private;
425 
426 		drm_atomic_helper_plane_changed(state, plane_state, plane);
427 
428 		if (!funcs || !funcs->atomic_check)
429 			continue;
430 
431 		ret = funcs->atomic_check(plane, plane_state);
432 		if (ret) {
433 			DRM_DEBUG_KMS("[PLANE:%d] atomic check failed\n",
434 				      plane->base.id);
435 			return ret;
436 		}
437 	}
438 
439 	for (i = 0; i < ncrtcs; i++) {
440 		struct drm_crtc_helper_funcs *funcs;
441 		struct drm_crtc *crtc = state->crtcs[i];
442 
443 		if (!crtc)
444 			continue;
445 
446 		funcs = crtc->helper_private;
447 
448 		if (!funcs || !funcs->atomic_check)
449 			continue;
450 
451 		ret = funcs->atomic_check(crtc, state->crtc_states[i]);
452 		if (ret) {
453 			DRM_DEBUG_KMS("[CRTC:%d] atomic check failed\n",
454 				      crtc->base.id);
455 			return ret;
456 		}
457 	}
458 
459 	return ret;
460 }
461 EXPORT_SYMBOL(drm_atomic_helper_check);
462 
463 static void
464 disable_outputs(struct drm_device *dev, struct drm_atomic_state *old_state)
465 {
466 	int ncrtcs = old_state->dev->mode_config.num_crtc;
467 	int nconnectors = old_state->dev->mode_config.num_connector;
468 	int i;
469 
470 	for (i = 0; i < nconnectors; i++) {
471 		struct drm_connector_state *old_conn_state;
472 		struct drm_connector *connector;
473 		struct drm_encoder_helper_funcs *funcs;
474 		struct drm_encoder *encoder;
475 
476 		old_conn_state = old_state->connector_states[i];
477 		connector = old_state->connectors[i];
478 
479 		/* Shut down everything that's in the changeset and currently
480 		 * still on. So need to check the old, saved state. */
481 		if (!old_conn_state || !old_conn_state->crtc)
482 			continue;
483 
484 		encoder = connector->state->best_encoder;
485 
486 		if (!encoder)
487 			continue;
488 
489 		funcs = encoder->helper_private;
490 
491 		/*
492 		 * Each encoder has at most one connector (since we always steal
493 		 * it away), so we won't call call disable hooks twice.
494 		 */
495 		if (encoder->bridge)
496 			encoder->bridge->funcs->disable(encoder->bridge);
497 
498 		/* Right function depends upon target state. */
499 		if (connector->state->crtc)
500 			funcs->prepare(encoder);
501 		else if (funcs->disable)
502 			funcs->disable(encoder);
503 		else
504 			funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
505 
506 		if (encoder->bridge)
507 			encoder->bridge->funcs->post_disable(encoder->bridge);
508 	}
509 
510 	for (i = 0; i < ncrtcs; i++) {
511 		struct drm_crtc_helper_funcs *funcs;
512 		struct drm_crtc *crtc;
513 
514 		crtc = old_state->crtcs[i];
515 
516 		/* Shut down everything that needs a full modeset. */
517 		if (!crtc || !crtc->state->mode_changed)
518 			continue;
519 
520 		funcs = crtc->helper_private;
521 
522 		/* Right function depends upon target state. */
523 		if (crtc->state->enable)
524 			funcs->prepare(crtc);
525 		else if (funcs->disable)
526 			funcs->disable(crtc);
527 		else
528 			funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
529 	}
530 }
531 
532 static void
533 set_routing_links(struct drm_device *dev, struct drm_atomic_state *old_state)
534 {
535 	int nconnectors = dev->mode_config.num_connector;
536 	int ncrtcs = old_state->dev->mode_config.num_crtc;
537 	int i;
538 
539 	/* clear out existing links */
540 	for (i = 0; i < nconnectors; i++) {
541 		struct drm_connector *connector;
542 
543 		connector = old_state->connectors[i];
544 
545 		if (!connector || !connector->encoder)
546 			continue;
547 
548 		WARN_ON(!connector->encoder->crtc);
549 
550 		connector->encoder->crtc = NULL;
551 		connector->encoder = NULL;
552 	}
553 
554 	/* set new links */
555 	for (i = 0; i < nconnectors; i++) {
556 		struct drm_connector *connector;
557 
558 		connector = old_state->connectors[i];
559 
560 		if (!connector || !connector->state->crtc)
561 			continue;
562 
563 		if (WARN_ON(!connector->state->best_encoder))
564 			continue;
565 
566 		connector->encoder = connector->state->best_encoder;
567 		connector->encoder->crtc = connector->state->crtc;
568 	}
569 
570 	/* set legacy state in the crtc structure */
571 	for (i = 0; i < ncrtcs; i++) {
572 		struct drm_crtc *crtc;
573 
574 		crtc = old_state->crtcs[i];
575 
576 		if (!crtc)
577 			continue;
578 
579 		crtc->mode = crtc->state->mode;
580 		crtc->enabled = crtc->state->enable;
581 		crtc->x = crtc->primary->state->src_x >> 16;
582 		crtc->y = crtc->primary->state->src_y >> 16;
583 	}
584 }
585 
586 static void
587 crtc_set_mode(struct drm_device *dev, struct drm_atomic_state *old_state)
588 {
589 	int ncrtcs = old_state->dev->mode_config.num_crtc;
590 	int nconnectors = old_state->dev->mode_config.num_connector;
591 	int i;
592 
593 	for (i = 0; i < ncrtcs; i++) {
594 		struct drm_crtc_helper_funcs *funcs;
595 		struct drm_crtc *crtc;
596 
597 		crtc = old_state->crtcs[i];
598 
599 		if (!crtc || !crtc->state->mode_changed)
600 			continue;
601 
602 		funcs = crtc->helper_private;
603 
604 		if (crtc->state->enable)
605 			funcs->mode_set_nofb(crtc);
606 	}
607 
608 	for (i = 0; i < nconnectors; i++) {
609 		struct drm_connector *connector;
610 		struct drm_crtc_state *new_crtc_state;
611 		struct drm_encoder_helper_funcs *funcs;
612 		struct drm_encoder *encoder;
613 		struct drm_display_mode *mode, *adjusted_mode;
614 
615 		connector = old_state->connectors[i];
616 
617 		if (!connector || !connector->state->best_encoder)
618 			continue;
619 
620 		encoder = connector->state->best_encoder;
621 		funcs = encoder->helper_private;
622 		new_crtc_state = connector->state->crtc->state;
623 		mode = &new_crtc_state->mode;
624 		adjusted_mode = &new_crtc_state->adjusted_mode;
625 
626 		/*
627 		 * Each encoder has at most one connector (since we always steal
628 		 * it away), so we won't call call mode_set hooks twice.
629 		 */
630 		funcs->mode_set(encoder, mode, adjusted_mode);
631 
632 		if (encoder->bridge && encoder->bridge->funcs->mode_set)
633 			encoder->bridge->funcs->mode_set(encoder->bridge,
634 							 mode, adjusted_mode);
635 	}
636 }
637 
638 /**
639  * drm_atomic_helper_commit_pre_planes - modeset commit before plane updates
640  * @dev: DRM device
641  * @state: atomic state
642  *
643  * This function commits the modeset changes that need to be committed before
644  * updating planes. It shuts down all the outputs that need to be shut down and
645  * prepares them (if required) with the new mode.
646  */
647 void drm_atomic_helper_commit_pre_planes(struct drm_device *dev,
648 					 struct drm_atomic_state *state)
649 {
650 	disable_outputs(dev, state);
651 	set_routing_links(dev, state);
652 	crtc_set_mode(dev, state);
653 }
654 EXPORT_SYMBOL(drm_atomic_helper_commit_pre_planes);
655 
656 /**
657  * drm_atomic_helper_commit_post_planes - modeset commit after plane updates
658  * @dev: DRM device
659  * @old_state: atomic state object with old state structures
660  *
661  * This function commits the modeset changes that need to be committed after
662  * updating planes: It enables all the outputs with the new configuration which
663  * had to be turned off for the update.
664  */
665 void drm_atomic_helper_commit_post_planes(struct drm_device *dev,
666 					  struct drm_atomic_state *old_state)
667 {
668 	int ncrtcs = old_state->dev->mode_config.num_crtc;
669 	int nconnectors = old_state->dev->mode_config.num_connector;
670 	int i;
671 
672 	for (i = 0; i < ncrtcs; i++) {
673 		struct drm_crtc_helper_funcs *funcs;
674 		struct drm_crtc *crtc;
675 
676 		crtc = old_state->crtcs[i];
677 
678 		/* Need to filter out CRTCs where only planes change. */
679 		if (!crtc || !crtc->state->mode_changed)
680 			continue;
681 
682 		funcs = crtc->helper_private;
683 
684 		if (crtc->state->enable)
685 			funcs->commit(crtc);
686 	}
687 
688 	for (i = 0; i < nconnectors; i++) {
689 		struct drm_connector *connector;
690 		struct drm_encoder_helper_funcs *funcs;
691 		struct drm_encoder *encoder;
692 
693 		connector = old_state->connectors[i];
694 
695 		if (!connector || !connector->state->best_encoder)
696 			continue;
697 
698 		encoder = connector->state->best_encoder;
699 		funcs = encoder->helper_private;
700 
701 		/*
702 		 * Each encoder has at most one connector (since we always steal
703 		 * it away), so we won't call call enable hooks twice.
704 		 */
705 		if (encoder->bridge)
706 			encoder->bridge->funcs->pre_enable(encoder->bridge);
707 
708 		funcs->commit(encoder);
709 
710 		if (encoder->bridge)
711 			encoder->bridge->funcs->enable(encoder->bridge);
712 	}
713 }
714 EXPORT_SYMBOL(drm_atomic_helper_commit_post_planes);
715 
716 static void wait_for_fences(struct drm_device *dev,
717 			    struct drm_atomic_state *state)
718 {
719 	int nplanes = dev->mode_config.num_total_plane;
720 	int i;
721 
722 	for (i = 0; i < nplanes; i++) {
723 		struct drm_plane *plane = state->planes[i];
724 
725 		if (!plane || !plane->state->fence)
726 			continue;
727 
728 		WARN_ON(!plane->state->fb);
729 
730 		fence_wait(plane->state->fence, false);
731 		fence_put(plane->state->fence);
732 		plane->state->fence = NULL;
733 	}
734 }
735 
736 static void
737 wait_for_vblanks(struct drm_device *dev, struct drm_atomic_state *old_state)
738 {
739 	struct drm_crtc *crtc;
740 	struct drm_crtc_state *old_crtc_state;
741 	int ncrtcs = old_state->dev->mode_config.num_crtc;
742 	int i, ret;
743 
744 	for (i = 0; i < ncrtcs; i++) {
745 		crtc = old_state->crtcs[i];
746 		old_crtc_state = old_state->crtc_states[i];
747 
748 		if (!crtc)
749 			continue;
750 
751 		/* No one cares about the old state, so abuse it for tracking
752 		 * and store whether we hold a vblank reference (and should do a
753 		 * vblank wait) in the ->enable boolean. */
754 		old_crtc_state->enable = false;
755 
756 		if (!crtc->state->enable)
757 			continue;
758 
759 		ret = drm_crtc_vblank_get(crtc);
760 		if (ret != 0)
761 			continue;
762 
763 		old_crtc_state->enable = true;
764 		old_crtc_state->last_vblank_count = drm_vblank_count(dev, i);
765 	}
766 
767 	for (i = 0; i < ncrtcs; i++) {
768 		crtc = old_state->crtcs[i];
769 		old_crtc_state = old_state->crtc_states[i];
770 
771 		if (!crtc || !old_crtc_state->enable)
772 			continue;
773 
774 		ret = wait_event_timeout(dev->vblank[i].queue,
775 				old_crtc_state->last_vblank_count !=
776 					drm_vblank_count(dev, i),
777 				msecs_to_jiffies(50));
778 
779 		drm_crtc_vblank_put(crtc);
780 	}
781 }
782 
783 /**
784  * drm_atomic_helper_commit - commit validated state object
785  * @dev: DRM device
786  * @state: the driver state object
787  * @async: asynchronous commit
788  *
789  * This function commits a with drm_atomic_helper_check() pre-validated state
790  * object. This can still fail when e.g. the framebuffer reservation fails. For
791  * now this doesn't implement asynchronous commits.
792  *
793  * RETURNS
794  * Zero for success or -errno.
795  */
796 int drm_atomic_helper_commit(struct drm_device *dev,
797 			     struct drm_atomic_state *state,
798 			     bool async)
799 {
800 	int ret;
801 
802 	if (async)
803 		return -EBUSY;
804 
805 	ret = drm_atomic_helper_prepare_planes(dev, state);
806 	if (ret)
807 		return ret;
808 
809 	/*
810 	 * This is the point of no return - everything below never fails except
811 	 * when the hw goes bonghits. Which means we can commit the new state on
812 	 * the software side now.
813 	 */
814 
815 	drm_atomic_helper_swap_state(dev, state);
816 
817 	/*
818 	 * Everything below can be run asynchronously without the need to grab
819 	 * any modeset locks at all under one conditions: It must be guaranteed
820 	 * that the asynchronous work has either been cancelled (if the driver
821 	 * supports it, which at least requires that the framebuffers get
822 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
823 	 * before the new state gets committed on the software side with
824 	 * drm_atomic_helper_swap_state().
825 	 *
826 	 * This scheme allows new atomic state updates to be prepared and
827 	 * checked in parallel to the asynchronous completion of the previous
828 	 * update. Which is important since compositors need to figure out the
829 	 * composition of the next frame right after having submitted the
830 	 * current layout.
831 	 */
832 
833 	wait_for_fences(dev, state);
834 
835 	drm_atomic_helper_commit_pre_planes(dev, state);
836 
837 	drm_atomic_helper_commit_planes(dev, state);
838 
839 	drm_atomic_helper_commit_post_planes(dev, state);
840 
841 	wait_for_vblanks(dev, state);
842 
843 	drm_atomic_helper_cleanup_planes(dev, state);
844 
845 	drm_atomic_state_free(state);
846 
847 	return 0;
848 }
849 EXPORT_SYMBOL(drm_atomic_helper_commit);
850 
851 /**
852  * drm_atomic_helper_prepare_planes - prepare plane resources after commit
853  * @dev: DRM device
854  * @state: atomic state object with old state structures
855  *
856  * This function prepares plane state, specifically framebuffers, for the new
857  * configuration. If any failure is encountered this function will call
858  * ->cleanup_fb on any already successfully prepared framebuffer.
859  *
860  * Returns:
861  * 0 on success, negative error code on failure.
862  */
863 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
864 				     struct drm_atomic_state *state)
865 {
866 	int nplanes = dev->mode_config.num_total_plane;
867 	int ret, i;
868 
869 	for (i = 0; i < nplanes; i++) {
870 		struct drm_plane_helper_funcs *funcs;
871 		struct drm_plane *plane = state->planes[i];
872 		struct drm_framebuffer *fb;
873 
874 		if (!plane)
875 			continue;
876 
877 		funcs = plane->helper_private;
878 
879 		fb = state->plane_states[i]->fb;
880 
881 		if (fb && funcs->prepare_fb) {
882 			ret = funcs->prepare_fb(plane, fb);
883 			if (ret)
884 				goto fail;
885 		}
886 	}
887 
888 	return 0;
889 
890 fail:
891 	for (i--; i >= 0; i--) {
892 		struct drm_plane_helper_funcs *funcs;
893 		struct drm_plane *plane = state->planes[i];
894 		struct drm_framebuffer *fb;
895 
896 		if (!plane)
897 			continue;
898 
899 		funcs = plane->helper_private;
900 
901 		fb = state->plane_states[i]->fb;
902 
903 		if (fb && funcs->cleanup_fb)
904 			funcs->cleanup_fb(plane, fb);
905 
906 	}
907 
908 	return ret;
909 }
910 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
911 
912 /**
913  * drm_atomic_helper_commit_planes - commit plane state
914  * @dev: DRM device
915  * @state: atomic state
916  *
917  * This function commits the new plane state using the plane and atomic helper
918  * functions for planes and crtcs. It assumes that the atomic state has already
919  * been pushed into the relevant object state pointers, since this step can no
920  * longer fail.
921  *
922  * It still requires the global state object @state to know which planes and
923  * crtcs need to be updated though.
924  */
925 void drm_atomic_helper_commit_planes(struct drm_device *dev,
926 				     struct drm_atomic_state *state)
927 {
928 	int nplanes = dev->mode_config.num_total_plane;
929 	int ncrtcs = dev->mode_config.num_crtc;
930 	int i;
931 
932 	for (i = 0; i < ncrtcs; i++) {
933 		struct drm_crtc_helper_funcs *funcs;
934 		struct drm_crtc *crtc = state->crtcs[i];
935 
936 		if (!crtc)
937 			continue;
938 
939 		funcs = crtc->helper_private;
940 
941 		if (!funcs || !funcs->atomic_begin)
942 			continue;
943 
944 		funcs->atomic_begin(crtc);
945 	}
946 
947 	for (i = 0; i < nplanes; i++) {
948 		struct drm_plane_helper_funcs *funcs;
949 		struct drm_plane *plane = state->planes[i];
950 
951 		if (!plane)
952 			continue;
953 
954 		funcs = plane->helper_private;
955 
956 		if (!funcs || !funcs->atomic_update)
957 			continue;
958 
959 		funcs->atomic_update(plane);
960 	}
961 
962 	for (i = 0; i < ncrtcs; i++) {
963 		struct drm_crtc_helper_funcs *funcs;
964 		struct drm_crtc *crtc = state->crtcs[i];
965 
966 		if (!crtc)
967 			continue;
968 
969 		funcs = crtc->helper_private;
970 
971 		if (!funcs || !funcs->atomic_flush)
972 			continue;
973 
974 		funcs->atomic_flush(crtc);
975 	}
976 }
977 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
978 
979 /**
980  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
981  * @dev: DRM device
982  * @old_state: atomic state object with old state structures
983  *
984  * This function cleans up plane state, specifically framebuffers, from the old
985  * configuration. Hence the old configuration must be perserved in @old_state to
986  * be able to call this function.
987  *
988  * This function must also be called on the new state when the atomic update
989  * fails at any point after calling drm_atomic_helper_prepare_planes().
990  */
991 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
992 				      struct drm_atomic_state *old_state)
993 {
994 	int nplanes = dev->mode_config.num_total_plane;
995 	int i;
996 
997 	for (i = 0; i < nplanes; i++) {
998 		struct drm_plane_helper_funcs *funcs;
999 		struct drm_plane *plane = old_state->planes[i];
1000 		struct drm_framebuffer *old_fb;
1001 
1002 		if (!plane)
1003 			continue;
1004 
1005 		funcs = plane->helper_private;
1006 
1007 		old_fb = old_state->plane_states[i]->fb;
1008 
1009 		if (old_fb && funcs->cleanup_fb)
1010 			funcs->cleanup_fb(plane, old_fb);
1011 	}
1012 }
1013 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
1014 
1015 /**
1016  * drm_atomic_helper_swap_state - store atomic state into current sw state
1017  * @dev: DRM device
1018  * @state: atomic state
1019  *
1020  * This function stores the atomic state into the current state pointers in all
1021  * driver objects. It should be called after all failing steps have been done
1022  * and succeeded, but before the actual hardware state is committed.
1023  *
1024  * For cleanup and error recovery the current state for all changed objects will
1025  * be swaped into @state.
1026  *
1027  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
1028  *
1029  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
1030  *
1031  * 2. Do any other steps that might fail.
1032  *
1033  * 3. Put the staged state into the current state pointers with this function.
1034  *
1035  * 4. Actually commit the hardware state.
1036  *
1037  * 5. Call drm_atomic_helper_cleanup_planes with @state, which since step 3
1038  * contains the old state. Also do any other cleanup required with that state.
1039  */
1040 void drm_atomic_helper_swap_state(struct drm_device *dev,
1041 				  struct drm_atomic_state *state)
1042 {
1043 	int i;
1044 
1045 	for (i = 0; i < dev->mode_config.num_connector; i++) {
1046 		struct drm_connector *connector = state->connectors[i];
1047 
1048 		if (!connector)
1049 			continue;
1050 
1051 		connector->state->state = state;
1052 		swap(state->connector_states[i], connector->state);
1053 		connector->state->state = NULL;
1054 	}
1055 
1056 	for (i = 0; i < dev->mode_config.num_crtc; i++) {
1057 		struct drm_crtc *crtc = state->crtcs[i];
1058 
1059 		if (!crtc)
1060 			continue;
1061 
1062 		crtc->state->state = state;
1063 		swap(state->crtc_states[i], crtc->state);
1064 		crtc->state->state = NULL;
1065 	}
1066 
1067 	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
1068 		struct drm_plane *plane = state->planes[i];
1069 
1070 		if (!plane)
1071 			continue;
1072 
1073 		plane->state->state = state;
1074 		swap(state->plane_states[i], plane->state);
1075 		plane->state->state = NULL;
1076 	}
1077 }
1078 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
1079 
1080 /**
1081  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
1082  * @plane: plane object to update
1083  * @crtc: owning CRTC of owning plane
1084  * @fb: framebuffer to flip onto plane
1085  * @crtc_x: x offset of primary plane on crtc
1086  * @crtc_y: y offset of primary plane on crtc
1087  * @crtc_w: width of primary plane rectangle on crtc
1088  * @crtc_h: height of primary plane rectangle on crtc
1089  * @src_x: x offset of @fb for panning
1090  * @src_y: y offset of @fb for panning
1091  * @src_w: width of source rectangle in @fb
1092  * @src_h: height of source rectangle in @fb
1093  *
1094  * Provides a default plane update handler using the atomic driver interface.
1095  *
1096  * RETURNS:
1097  * Zero on success, error code on failure
1098  */
1099 int drm_atomic_helper_update_plane(struct drm_plane *plane,
1100 				   struct drm_crtc *crtc,
1101 				   struct drm_framebuffer *fb,
1102 				   int crtc_x, int crtc_y,
1103 				   unsigned int crtc_w, unsigned int crtc_h,
1104 				   uint32_t src_x, uint32_t src_y,
1105 				   uint32_t src_w, uint32_t src_h)
1106 {
1107 	struct drm_atomic_state *state;
1108 	struct drm_plane_state *plane_state;
1109 	int ret = 0;
1110 
1111 	state = drm_atomic_state_alloc(plane->dev);
1112 	if (!state)
1113 		return -ENOMEM;
1114 
1115 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1116 retry:
1117 	plane_state = drm_atomic_get_plane_state(state, plane);
1118 	if (IS_ERR(plane_state)) {
1119 		ret = PTR_ERR(plane_state);
1120 		goto fail;
1121 	}
1122 
1123 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
1124 	if (ret != 0)
1125 		goto fail;
1126 	plane_state->fb = fb;
1127 	plane_state->crtc_x = crtc_x;
1128 	plane_state->crtc_y = crtc_y;
1129 	plane_state->crtc_h = crtc_h;
1130 	plane_state->crtc_w = crtc_w;
1131 	plane_state->src_x = src_x;
1132 	plane_state->src_y = src_y;
1133 	plane_state->src_h = src_h;
1134 	plane_state->src_w = src_w;
1135 
1136 	ret = drm_atomic_commit(state);
1137 	if (ret != 0)
1138 		goto fail;
1139 
1140 	/* Driver takes ownership of state on successful commit. */
1141 	return 0;
1142 fail:
1143 	if (ret == -EDEADLK)
1144 		goto backoff;
1145 
1146 	drm_atomic_state_free(state);
1147 
1148 	return ret;
1149 backoff:
1150 	drm_atomic_legacy_backoff(state);
1151 	drm_atomic_state_clear(state);
1152 
1153 	/*
1154 	 * Someone might have exchanged the framebuffer while we dropped locks
1155 	 * in the backoff code. We need to fix up the fb refcount tracking the
1156 	 * core does for us.
1157 	 */
1158 	plane->old_fb = plane->fb;
1159 
1160 	goto retry;
1161 }
1162 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
1163 
1164 /**
1165  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
1166  * @plane: plane to disable
1167  *
1168  * Provides a default plane disable handler using the atomic driver interface.
1169  *
1170  * RETURNS:
1171  * Zero on success, error code on failure
1172  */
1173 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
1174 {
1175 	struct drm_atomic_state *state;
1176 	struct drm_plane_state *plane_state;
1177 	int ret = 0;
1178 
1179 	state = drm_atomic_state_alloc(plane->dev);
1180 	if (!state)
1181 		return -ENOMEM;
1182 
1183 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
1184 retry:
1185 	plane_state = drm_atomic_get_plane_state(state, plane);
1186 	if (IS_ERR(plane_state)) {
1187 		ret = PTR_ERR(plane_state);
1188 		goto fail;
1189 	}
1190 
1191 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
1192 	if (ret != 0)
1193 		goto fail;
1194 	plane_state->fb = NULL;
1195 	plane_state->crtc_x = 0;
1196 	plane_state->crtc_y = 0;
1197 	plane_state->crtc_h = 0;
1198 	plane_state->crtc_w = 0;
1199 	plane_state->src_x = 0;
1200 	plane_state->src_y = 0;
1201 	plane_state->src_h = 0;
1202 	plane_state->src_w = 0;
1203 
1204 	ret = drm_atomic_commit(state);
1205 	if (ret != 0)
1206 		goto fail;
1207 
1208 	/* Driver takes ownership of state on successful commit. */
1209 	return 0;
1210 fail:
1211 	if (ret == -EDEADLK)
1212 		goto backoff;
1213 
1214 	drm_atomic_state_free(state);
1215 
1216 	return ret;
1217 backoff:
1218 	drm_atomic_legacy_backoff(state);
1219 	drm_atomic_state_clear(state);
1220 
1221 	/*
1222 	 * Someone might have exchanged the framebuffer while we dropped locks
1223 	 * in the backoff code. We need to fix up the fb refcount tracking the
1224 	 * core does for us.
1225 	 */
1226 	plane->old_fb = plane->fb;
1227 
1228 	goto retry;
1229 }
1230 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
1231 
1232 static int update_output_state(struct drm_atomic_state *state,
1233 			       struct drm_mode_set *set)
1234 {
1235 	struct drm_device *dev = set->crtc->dev;
1236 	struct drm_connector_state *conn_state;
1237 	int nconnectors = state->dev->mode_config.num_connector;
1238 	int ncrtcs = state->dev->mode_config.num_crtc;
1239 	int ret, i, j;
1240 
1241 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
1242 			       state->acquire_ctx);
1243 	if (ret)
1244 		return ret;
1245 
1246 	/* First grab all affected connector/crtc states. */
1247 	for (i = 0; i < set->num_connectors; i++) {
1248 		conn_state = drm_atomic_get_connector_state(state,
1249 							    set->connectors[i]);
1250 		if (IS_ERR(conn_state))
1251 			return PTR_ERR(conn_state);
1252 	}
1253 
1254 	for (i = 0; i < ncrtcs; i++) {
1255 		struct drm_crtc *crtc = state->crtcs[i];
1256 
1257 		if (!crtc)
1258 			continue;
1259 
1260 		ret = drm_atomic_add_affected_connectors(state, crtc);
1261 		if (ret)
1262 			return ret;
1263 	}
1264 
1265 	/* Then recompute connector->crtc links and crtc enabling state. */
1266 	for (i = 0; i < nconnectors; i++) {
1267 		struct drm_connector *connector;
1268 
1269 		connector = state->connectors[i];
1270 		conn_state = state->connector_states[i];
1271 
1272 		if (!connector)
1273 			continue;
1274 
1275 		if (conn_state->crtc == set->crtc) {
1276 			ret = drm_atomic_set_crtc_for_connector(conn_state,
1277 								NULL);
1278 			if (ret)
1279 				return ret;
1280 		}
1281 
1282 		for (j = 0; j < set->num_connectors; j++) {
1283 			if (set->connectors[j] == connector) {
1284 				ret = drm_atomic_set_crtc_for_connector(conn_state,
1285 									set->crtc);
1286 				if (ret)
1287 					return ret;
1288 				break;
1289 			}
1290 		}
1291 	}
1292 
1293 	for (i = 0; i < ncrtcs; i++) {
1294 		struct drm_crtc *crtc = state->crtcs[i];
1295 		struct drm_crtc_state *crtc_state = state->crtc_states[i];
1296 
1297 		if (!crtc)
1298 			continue;
1299 
1300 		/* Don't update ->enable for the CRTC in the set_config request,
1301 		 * since a mismatch would indicate a bug in the upper layers.
1302 		 * The actual modeset code later on will catch any
1303 		 * inconsistencies here. */
1304 		if (crtc == set->crtc)
1305 			continue;
1306 
1307 		crtc_state->enable =
1308 			drm_atomic_connectors_for_crtc(state, crtc);
1309 	}
1310 
1311 	return 0;
1312 }
1313 
1314 /**
1315  * drm_atomic_helper_set_config - set a new config from userspace
1316  * @set: mode set configuration
1317  *
1318  * Provides a default crtc set_config handler using the atomic driver interface.
1319  *
1320  * Returns:
1321  * Returns 0 on success, negative errno numbers on failure.
1322  */
1323 int drm_atomic_helper_set_config(struct drm_mode_set *set)
1324 {
1325 	struct drm_atomic_state *state;
1326 	struct drm_crtc *crtc = set->crtc;
1327 	struct drm_crtc_state *crtc_state;
1328 	struct drm_plane_state *primary_state;
1329 	int ret = 0;
1330 
1331 	state = drm_atomic_state_alloc(crtc->dev);
1332 	if (!state)
1333 		return -ENOMEM;
1334 
1335 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
1336 retry:
1337 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1338 	if (IS_ERR(crtc_state)) {
1339 		ret = PTR_ERR(crtc_state);
1340 		goto fail;
1341 	}
1342 
1343 	if (!set->mode) {
1344 		WARN_ON(set->fb);
1345 		WARN_ON(set->num_connectors);
1346 
1347 		crtc_state->enable = false;
1348 		goto commit;
1349 	}
1350 
1351 	WARN_ON(!set->fb);
1352 	WARN_ON(!set->num_connectors);
1353 
1354 	crtc_state->enable = true;
1355 	drm_mode_copy(&crtc_state->mode, set->mode);
1356 
1357 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
1358 	if (IS_ERR(primary_state)) {
1359 		ret = PTR_ERR(primary_state);
1360 		goto fail;
1361 	}
1362 
1363 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
1364 	if (ret != 0)
1365 		goto fail;
1366 	primary_state->fb = set->fb;
1367 	primary_state->crtc_x = 0;
1368 	primary_state->crtc_y = 0;
1369 	primary_state->crtc_h = set->mode->vdisplay;
1370 	primary_state->crtc_w = set->mode->hdisplay;
1371 	primary_state->src_x = set->x << 16;
1372 	primary_state->src_y = set->y << 16;
1373 	primary_state->src_h = set->mode->vdisplay << 16;
1374 	primary_state->src_w = set->mode->hdisplay << 16;
1375 
1376 commit:
1377 	ret = update_output_state(state, set);
1378 	if (ret)
1379 		goto fail;
1380 
1381 	ret = drm_atomic_commit(state);
1382 	if (ret != 0)
1383 		goto fail;
1384 
1385 	/* Driver takes ownership of state on successful commit. */
1386 	return 0;
1387 fail:
1388 	if (ret == -EDEADLK)
1389 		goto backoff;
1390 
1391 	drm_atomic_state_free(state);
1392 
1393 	return ret;
1394 backoff:
1395 	drm_atomic_legacy_backoff(state);
1396 	drm_atomic_state_clear(state);
1397 
1398 	/*
1399 	 * Someone might have exchanged the framebuffer while we dropped locks
1400 	 * in the backoff code. We need to fix up the fb refcount tracking the
1401 	 * core does for us.
1402 	 */
1403 	crtc->primary->old_fb = crtc->primary->fb;
1404 
1405 	goto retry;
1406 }
1407 EXPORT_SYMBOL(drm_atomic_helper_set_config);
1408 
1409 /**
1410  * drm_atomic_helper_crtc_set_property - helper for crtc prorties
1411  * @crtc: DRM crtc
1412  * @property: DRM property
1413  * @val: value of property
1414  *
1415  * Provides a default plane disablle handler using the atomic driver interface.
1416  *
1417  * RETURNS:
1418  * Zero on success, error code on failure
1419  */
1420 int
1421 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
1422 				    struct drm_property *property,
1423 				    uint64_t val)
1424 {
1425 	struct drm_atomic_state *state;
1426 	struct drm_crtc_state *crtc_state;
1427 	int ret = 0;
1428 
1429 	state = drm_atomic_state_alloc(crtc->dev);
1430 	if (!state)
1431 		return -ENOMEM;
1432 
1433 	/* ->set_property is always called with all locks held. */
1434 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
1435 retry:
1436 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
1437 	if (IS_ERR(crtc_state)) {
1438 		ret = PTR_ERR(crtc_state);
1439 		goto fail;
1440 	}
1441 
1442 	ret = crtc->funcs->atomic_set_property(crtc, crtc_state,
1443 					       property, val);
1444 	if (ret)
1445 		goto fail;
1446 
1447 	ret = drm_atomic_commit(state);
1448 	if (ret != 0)
1449 		goto fail;
1450 
1451 	/* Driver takes ownership of state on successful commit. */
1452 	return 0;
1453 fail:
1454 	if (ret == -EDEADLK)
1455 		goto backoff;
1456 
1457 	drm_atomic_state_free(state);
1458 
1459 	return ret;
1460 backoff:
1461 	drm_atomic_legacy_backoff(state);
1462 	drm_atomic_state_clear(state);
1463 
1464 	goto retry;
1465 }
1466 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
1467 
1468 /**
1469  * drm_atomic_helper_plane_set_property - helper for plane prorties
1470  * @plane: DRM plane
1471  * @property: DRM property
1472  * @val: value of property
1473  *
1474  * Provides a default plane disable handler using the atomic driver interface.
1475  *
1476  * RETURNS:
1477  * Zero on success, error code on failure
1478  */
1479 int
1480 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
1481 				    struct drm_property *property,
1482 				    uint64_t val)
1483 {
1484 	struct drm_atomic_state *state;
1485 	struct drm_plane_state *plane_state;
1486 	int ret = 0;
1487 
1488 	state = drm_atomic_state_alloc(plane->dev);
1489 	if (!state)
1490 		return -ENOMEM;
1491 
1492 	/* ->set_property is always called with all locks held. */
1493 	state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
1494 retry:
1495 	plane_state = drm_atomic_get_plane_state(state, plane);
1496 	if (IS_ERR(plane_state)) {
1497 		ret = PTR_ERR(plane_state);
1498 		goto fail;
1499 	}
1500 
1501 	ret = plane->funcs->atomic_set_property(plane, plane_state,
1502 					       property, val);
1503 	if (ret)
1504 		goto fail;
1505 
1506 	ret = drm_atomic_commit(state);
1507 	if (ret != 0)
1508 		goto fail;
1509 
1510 	/* Driver takes ownership of state on successful commit. */
1511 	return 0;
1512 fail:
1513 	if (ret == -EDEADLK)
1514 		goto backoff;
1515 
1516 	drm_atomic_state_free(state);
1517 
1518 	return ret;
1519 backoff:
1520 	drm_atomic_legacy_backoff(state);
1521 	drm_atomic_state_clear(state);
1522 
1523 	goto retry;
1524 }
1525 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
1526 
1527 /**
1528  * drm_atomic_helper_connector_set_property - helper for connector prorties
1529  * @connector: DRM connector
1530  * @property: DRM property
1531  * @val: value of property
1532  *
1533  * Provides a default plane disablle handler using the atomic driver interface.
1534  *
1535  * RETURNS:
1536  * Zero on success, error code on failure
1537  */
1538 int
1539 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
1540 				    struct drm_property *property,
1541 				    uint64_t val)
1542 {
1543 	struct drm_atomic_state *state;
1544 	struct drm_connector_state *connector_state;
1545 	int ret = 0;
1546 
1547 	state = drm_atomic_state_alloc(connector->dev);
1548 	if (!state)
1549 		return -ENOMEM;
1550 
1551 	/* ->set_property is always called with all locks held. */
1552 	state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
1553 retry:
1554 	connector_state = drm_atomic_get_connector_state(state, connector);
1555 	if (IS_ERR(connector_state)) {
1556 		ret = PTR_ERR(connector_state);
1557 		goto fail;
1558 	}
1559 
1560 	ret = connector->funcs->atomic_set_property(connector, connector_state,
1561 					       property, val);
1562 	if (ret)
1563 		goto fail;
1564 
1565 	ret = drm_atomic_commit(state);
1566 	if (ret != 0)
1567 		goto fail;
1568 
1569 	/* Driver takes ownership of state on successful commit. */
1570 	return 0;
1571 fail:
1572 	if (ret == -EDEADLK)
1573 		goto backoff;
1574 
1575 	drm_atomic_state_free(state);
1576 
1577 	return ret;
1578 backoff:
1579 	drm_atomic_legacy_backoff(state);
1580 	drm_atomic_state_clear(state);
1581 
1582 	goto retry;
1583 }
1584 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
1585