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