Home | History | Annotate | Line # | Download | only in drm
drm_atomic_helper.c revision 1.2
      1 /*	$NetBSD: drm_atomic_helper.c,v 1.2 2018/08/27 04:58:19 riastradh 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 (at) gmail.com>
     27  * Daniel Vetter <daniel.vetter (at) ffwll.ch>
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: drm_atomic_helper.c,v 1.2 2018/08/27 04:58:19 riastradh 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;
    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;
    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;
    838 	struct drm_connector *connector;
    839 	struct drm_connector_state *old_conn_state;
    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 		ret = wait_event_timeout(dev->vblank[i].queue,
    986 				old_crtc_state->last_vblank_count !=
    987 					drm_crtc_vblank_count(crtc),
    988 				msecs_to_jiffies(50));
    989 
    990 		drm_crtc_vblank_put(crtc);
    991 	}
    992 }
    993 EXPORT_SYMBOL(drm_atomic_helper_wait_for_vblanks);
    994 
    995 /**
    996  * drm_atomic_helper_commit - commit validated state object
    997  * @dev: DRM device
    998  * @state: the driver state object
    999  * @async: asynchronous commit
   1000  *
   1001  * This function commits a with drm_atomic_helper_check() pre-validated state
   1002  * object. This can still fail when e.g. the framebuffer reservation fails. For
   1003  * now this doesn't implement asynchronous commits.
   1004  *
   1005  * Note that right now this function does not support async commits, and hence
   1006  * driver writers must implement their own version for now. Also note that the
   1007  * default ordering of how the various stages are called is to match the legacy
   1008  * modeset helper library closest. One peculiarity of that is that it doesn't
   1009  * mesh well with runtime PM at all.
   1010  *
   1011  * For drivers supporting runtime PM the recommended sequence is
   1012  *
   1013  *     drm_atomic_helper_commit_modeset_disables(dev, state);
   1014  *
   1015  *     drm_atomic_helper_commit_modeset_enables(dev, state);
   1016  *
   1017  *     drm_atomic_helper_commit_planes(dev, state, true);
   1018  *
   1019  * See the kerneldoc entries for these three functions for more details.
   1020  *
   1021  * RETURNS
   1022  * Zero for success or -errno.
   1023  */
   1024 int drm_atomic_helper_commit(struct drm_device *dev,
   1025 			     struct drm_atomic_state *state,
   1026 			     bool async)
   1027 {
   1028 	int ret;
   1029 
   1030 	if (async)
   1031 		return -EBUSY;
   1032 
   1033 	ret = drm_atomic_helper_prepare_planes(dev, state);
   1034 	if (ret)
   1035 		return ret;
   1036 
   1037 	/*
   1038 	 * This is the point of no return - everything below never fails except
   1039 	 * when the hw goes bonghits. Which means we can commit the new state on
   1040 	 * the software side now.
   1041 	 */
   1042 
   1043 	drm_atomic_helper_swap_state(dev, state);
   1044 
   1045 	/*
   1046 	 * Everything below can be run asynchronously without the need to grab
   1047 	 * any modeset locks at all under one condition: It must be guaranteed
   1048 	 * that the asynchronous work has either been cancelled (if the driver
   1049 	 * supports it, which at least requires that the framebuffers get
   1050 	 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
   1051 	 * before the new state gets committed on the software side with
   1052 	 * drm_atomic_helper_swap_state().
   1053 	 *
   1054 	 * This scheme allows new atomic state updates to be prepared and
   1055 	 * checked in parallel to the asynchronous completion of the previous
   1056 	 * update. Which is important since compositors need to figure out the
   1057 	 * composition of the next frame right after having submitted the
   1058 	 * current layout.
   1059 	 */
   1060 
   1061 	wait_for_fences(dev, state);
   1062 
   1063 	drm_atomic_helper_commit_modeset_disables(dev, state);
   1064 
   1065 	drm_atomic_helper_commit_planes(dev, state, false);
   1066 
   1067 	drm_atomic_helper_commit_modeset_enables(dev, state);
   1068 
   1069 	drm_atomic_helper_wait_for_vblanks(dev, state);
   1070 
   1071 	drm_atomic_helper_cleanup_planes(dev, state);
   1072 
   1073 	drm_atomic_state_free(state);
   1074 
   1075 	return 0;
   1076 }
   1077 EXPORT_SYMBOL(drm_atomic_helper_commit);
   1078 
   1079 /**
   1080  * DOC: implementing async commit
   1081  *
   1082  * For now the atomic helpers don't support async commit directly. If there is
   1083  * real need it could be added though, using the dma-buf fence infrastructure
   1084  * for generic synchronization with outstanding rendering.
   1085  *
   1086  * For now drivers have to implement async commit themselves, with the following
   1087  * sequence being the recommended one:
   1088  *
   1089  * 1. Run drm_atomic_helper_prepare_planes() first. This is the only function
   1090  * which commit needs to call which can fail, so we want to run it first and
   1091  * synchronously.
   1092  *
   1093  * 2. Synchronize with any outstanding asynchronous commit worker threads which
   1094  * might be affected the new state update. This can be done by either cancelling
   1095  * or flushing the work items, depending upon whether the driver can deal with
   1096  * cancelled updates. Note that it is important to ensure that the framebuffer
   1097  * cleanup is still done when cancelling.
   1098  *
   1099  * For sufficient parallelism it is recommended to have a work item per crtc
   1100  * (for updates which don't touch global state) and a global one. Then we only
   1101  * need to synchronize with the crtc work items for changed crtcs and the global
   1102  * work item, which allows nice concurrent updates on disjoint sets of crtcs.
   1103  *
   1104  * 3. The software state is updated synchronously with
   1105  * drm_atomic_helper_swap_state(). Doing this under the protection of all modeset
   1106  * locks means concurrent callers never see inconsistent state. And doing this
   1107  * while it's guaranteed that no relevant async worker runs means that async
   1108  * workers do not need grab any locks. Actually they must not grab locks, for
   1109  * otherwise the work flushing will deadlock.
   1110  *
   1111  * 4. Schedule a work item to do all subsequent steps, using the split-out
   1112  * commit helpers: a) pre-plane commit b) plane commit c) post-plane commit and
   1113  * then cleaning up the framebuffers after the old framebuffer is no longer
   1114  * being displayed.
   1115  */
   1116 
   1117 /**
   1118  * drm_atomic_helper_prepare_planes - prepare plane resources before commit
   1119  * @dev: DRM device
   1120  * @state: atomic state object with new state structures
   1121  *
   1122  * This function prepares plane state, specifically framebuffers, for the new
   1123  * configuration. If any failure is encountered this function will call
   1124  * ->cleanup_fb on any already successfully prepared framebuffer.
   1125  *
   1126  * Returns:
   1127  * 0 on success, negative error code on failure.
   1128  */
   1129 int drm_atomic_helper_prepare_planes(struct drm_device *dev,
   1130 				     struct drm_atomic_state *state)
   1131 {
   1132 	int nplanes = dev->mode_config.num_total_plane;
   1133 	int ret, i;
   1134 
   1135 	for (i = 0; i < nplanes; i++) {
   1136 		const struct drm_plane_helper_funcs *funcs;
   1137 		struct drm_plane *plane = state->planes[i];
   1138 		struct drm_plane_state *plane_state = state->plane_states[i];
   1139 
   1140 		if (!plane)
   1141 			continue;
   1142 
   1143 		funcs = plane->helper_private;
   1144 
   1145 		if (funcs->prepare_fb) {
   1146 			ret = funcs->prepare_fb(plane, plane_state);
   1147 			if (ret)
   1148 				goto fail;
   1149 		}
   1150 	}
   1151 
   1152 	return 0;
   1153 
   1154 fail:
   1155 	for (i--; i >= 0; i--) {
   1156 		const struct drm_plane_helper_funcs *funcs;
   1157 		struct drm_plane *plane = state->planes[i];
   1158 		struct drm_plane_state *plane_state = state->plane_states[i];
   1159 
   1160 		if (!plane)
   1161 			continue;
   1162 
   1163 		funcs = plane->helper_private;
   1164 
   1165 		if (funcs->cleanup_fb)
   1166 			funcs->cleanup_fb(plane, plane_state);
   1167 
   1168 	}
   1169 
   1170 	return ret;
   1171 }
   1172 EXPORT_SYMBOL(drm_atomic_helper_prepare_planes);
   1173 
   1174 bool plane_crtc_active(struct drm_plane_state *state)
   1175 {
   1176 	return state->crtc && state->crtc->state->active;
   1177 }
   1178 
   1179 /**
   1180  * drm_atomic_helper_commit_planes - commit plane state
   1181  * @dev: DRM device
   1182  * @old_state: atomic state object with old state structures
   1183  * @active_only: Only commit on active CRTC if set
   1184  *
   1185  * This function commits the new plane state using the plane and atomic helper
   1186  * functions for planes and crtcs. It assumes that the atomic state has already
   1187  * been pushed into the relevant object state pointers, since this step can no
   1188  * longer fail.
   1189  *
   1190  * It still requires the global state object @old_state to know which planes and
   1191  * crtcs need to be updated though.
   1192  *
   1193  * Note that this function does all plane updates across all CRTCs in one step.
   1194  * If the hardware can't support this approach look at
   1195  * drm_atomic_helper_commit_planes_on_crtc() instead.
   1196  *
   1197  * Plane parameters can be updated by applications while the associated CRTC is
   1198  * disabled. The DRM/KMS core will store the parameters in the plane state,
   1199  * which will be available to the driver when the CRTC is turned on. As a result
   1200  * most drivers don't need to be immediately notified of plane updates for a
   1201  * disabled CRTC.
   1202  *
   1203  * Unless otherwise needed, drivers are advised to set the @active_only
   1204  * parameters to true in order not to receive plane update notifications related
   1205  * to a disabled CRTC. This avoids the need to manually ignore plane updates in
   1206  * driver code when the driver and/or hardware can't or just don't need to deal
   1207  * with updates on disabled CRTCs, for example when supporting runtime PM.
   1208  *
   1209  * The drm_atomic_helper_commit() default implementation only sets @active_only
   1210  * to false to most closely match the behaviour of the legacy helpers. This should
   1211  * not be copied blindly by drivers.
   1212  */
   1213 void drm_atomic_helper_commit_planes(struct drm_device *dev,
   1214 				     struct drm_atomic_state *old_state,
   1215 				     bool active_only)
   1216 {
   1217 	struct drm_crtc *crtc;
   1218 	struct drm_crtc_state *old_crtc_state;
   1219 	struct drm_plane *plane;
   1220 	struct drm_plane_state *old_plane_state;
   1221 	int i;
   1222 
   1223 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
   1224 		const struct drm_crtc_helper_funcs *funcs;
   1225 
   1226 		funcs = crtc->helper_private;
   1227 
   1228 		if (!funcs || !funcs->atomic_begin)
   1229 			continue;
   1230 
   1231 		if (active_only && !crtc->state->active)
   1232 			continue;
   1233 
   1234 		funcs->atomic_begin(crtc, old_crtc_state);
   1235 	}
   1236 
   1237 	for_each_plane_in_state(old_state, plane, old_plane_state, i) {
   1238 		const struct drm_plane_helper_funcs *funcs;
   1239 		bool disabling;
   1240 
   1241 		funcs = plane->helper_private;
   1242 
   1243 		if (!funcs)
   1244 			continue;
   1245 
   1246 		disabling = drm_atomic_plane_disabling(plane, old_plane_state);
   1247 
   1248 		if (active_only) {
   1249 			/*
   1250 			 * Skip planes related to inactive CRTCs. If the plane
   1251 			 * is enabled use the state of the current CRTC. If the
   1252 			 * plane is being disabled use the state of the old
   1253 			 * CRTC to avoid skipping planes being disabled on an
   1254 			 * active CRTC.
   1255 			 */
   1256 			if (!disabling && !plane_crtc_active(plane->state))
   1257 				continue;
   1258 			if (disabling && !plane_crtc_active(old_plane_state))
   1259 				continue;
   1260 		}
   1261 
   1262 		/*
   1263 		 * Special-case disabling the plane if drivers support it.
   1264 		 */
   1265 		if (disabling && funcs->atomic_disable)
   1266 			funcs->atomic_disable(plane, old_plane_state);
   1267 		else if (plane->state->crtc || disabling)
   1268 			funcs->atomic_update(plane, old_plane_state);
   1269 	}
   1270 
   1271 	for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
   1272 		const struct drm_crtc_helper_funcs *funcs;
   1273 
   1274 		funcs = crtc->helper_private;
   1275 
   1276 		if (!funcs || !funcs->atomic_flush)
   1277 			continue;
   1278 
   1279 		if (active_only && !crtc->state->active)
   1280 			continue;
   1281 
   1282 		funcs->atomic_flush(crtc, old_crtc_state);
   1283 	}
   1284 }
   1285 EXPORT_SYMBOL(drm_atomic_helper_commit_planes);
   1286 
   1287 /**
   1288  * drm_atomic_helper_commit_planes_on_crtc - commit plane state for a crtc
   1289  * @old_crtc_state: atomic state object with the old crtc state
   1290  *
   1291  * This function commits the new plane state using the plane and atomic helper
   1292  * functions for planes on the specific crtc. It assumes that the atomic state
   1293  * has already been pushed into the relevant object state pointers, since this
   1294  * step can no longer fail.
   1295  *
   1296  * This function is useful when plane updates should be done crtc-by-crtc
   1297  * instead of one global step like drm_atomic_helper_commit_planes() does.
   1298  *
   1299  * This function can only be savely used when planes are not allowed to move
   1300  * between different CRTCs because this function doesn't handle inter-CRTC
   1301  * depencies. Callers need to ensure that either no such depencies exist,
   1302  * resolve them through ordering of commit calls or through some other means.
   1303  */
   1304 void
   1305 drm_atomic_helper_commit_planes_on_crtc(struct drm_crtc_state *old_crtc_state)
   1306 {
   1307 	const struct drm_crtc_helper_funcs *crtc_funcs;
   1308 	struct drm_crtc *crtc = old_crtc_state->crtc;
   1309 	struct drm_atomic_state *old_state = old_crtc_state->state;
   1310 	struct drm_plane *plane;
   1311 	unsigned plane_mask;
   1312 
   1313 	plane_mask = old_crtc_state->plane_mask;
   1314 	plane_mask |= crtc->state->plane_mask;
   1315 
   1316 	crtc_funcs = crtc->helper_private;
   1317 	if (crtc_funcs && crtc_funcs->atomic_begin)
   1318 		crtc_funcs->atomic_begin(crtc, old_crtc_state);
   1319 
   1320 	drm_for_each_plane_mask(plane, crtc->dev, plane_mask) {
   1321 		struct drm_plane_state *old_plane_state =
   1322 			drm_atomic_get_existing_plane_state(old_state, plane);
   1323 		const struct drm_plane_helper_funcs *plane_funcs;
   1324 
   1325 		plane_funcs = plane->helper_private;
   1326 
   1327 		if (!old_plane_state || !plane_funcs)
   1328 			continue;
   1329 
   1330 		WARN_ON(plane->state->crtc && plane->state->crtc != crtc);
   1331 
   1332 		if (drm_atomic_plane_disabling(plane, old_plane_state) &&
   1333 		    plane_funcs->atomic_disable)
   1334 			plane_funcs->atomic_disable(plane, old_plane_state);
   1335 		else if (plane->state->crtc ||
   1336 			 drm_atomic_plane_disabling(plane, old_plane_state))
   1337 			plane_funcs->atomic_update(plane, old_plane_state);
   1338 	}
   1339 
   1340 	if (crtc_funcs && crtc_funcs->atomic_flush)
   1341 		crtc_funcs->atomic_flush(crtc, old_crtc_state);
   1342 }
   1343 EXPORT_SYMBOL(drm_atomic_helper_commit_planes_on_crtc);
   1344 
   1345 /**
   1346  * drm_atomic_helper_cleanup_planes - cleanup plane resources after commit
   1347  * @dev: DRM device
   1348  * @old_state: atomic state object with old state structures
   1349  *
   1350  * This function cleans up plane state, specifically framebuffers, from the old
   1351  * configuration. Hence the old configuration must be perserved in @old_state to
   1352  * be able to call this function.
   1353  *
   1354  * This function must also be called on the new state when the atomic update
   1355  * fails at any point after calling drm_atomic_helper_prepare_planes().
   1356  */
   1357 void drm_atomic_helper_cleanup_planes(struct drm_device *dev,
   1358 				      struct drm_atomic_state *old_state)
   1359 {
   1360 	struct drm_plane *plane;
   1361 	struct drm_plane_state *plane_state;
   1362 	int i;
   1363 
   1364 	for_each_plane_in_state(old_state, plane, plane_state, i) {
   1365 		const struct drm_plane_helper_funcs *funcs;
   1366 
   1367 		funcs = plane->helper_private;
   1368 
   1369 		if (funcs->cleanup_fb)
   1370 			funcs->cleanup_fb(plane, plane_state);
   1371 	}
   1372 }
   1373 EXPORT_SYMBOL(drm_atomic_helper_cleanup_planes);
   1374 
   1375 /**
   1376  * drm_atomic_helper_swap_state - store atomic state into current sw state
   1377  * @dev: DRM device
   1378  * @state: atomic state
   1379  *
   1380  * This function stores the atomic state into the current state pointers in all
   1381  * driver objects. It should be called after all failing steps have been done
   1382  * and succeeded, but before the actual hardware state is committed.
   1383  *
   1384  * For cleanup and error recovery the current state for all changed objects will
   1385  * be swaped into @state.
   1386  *
   1387  * With that sequence it fits perfectly into the plane prepare/cleanup sequence:
   1388  *
   1389  * 1. Call drm_atomic_helper_prepare_planes() with the staged atomic state.
   1390  *
   1391  * 2. Do any other steps that might fail.
   1392  *
   1393  * 3. Put the staged state into the current state pointers with this function.
   1394  *
   1395  * 4. Actually commit the hardware state.
   1396  *
   1397  * 5. Call drm_atomic_helper_cleanup_planes() with @state, which since step 3
   1398  * contains the old state. Also do any other cleanup required with that state.
   1399  */
   1400 void drm_atomic_helper_swap_state(struct drm_device *dev,
   1401 				  struct drm_atomic_state *state)
   1402 {
   1403 	int i;
   1404 
   1405 	for (i = 0; i < dev->mode_config.num_connector; i++) {
   1406 		struct drm_connector *connector = state->connectors[i];
   1407 
   1408 		if (!connector)
   1409 			continue;
   1410 
   1411 		connector->state->state = state;
   1412 		swap(state->connector_states[i], connector->state);
   1413 		connector->state->state = NULL;
   1414 	}
   1415 
   1416 	for (i = 0; i < dev->mode_config.num_crtc; i++) {
   1417 		struct drm_crtc *crtc = state->crtcs[i];
   1418 
   1419 		if (!crtc)
   1420 			continue;
   1421 
   1422 		crtc->state->state = state;
   1423 		swap(state->crtc_states[i], crtc->state);
   1424 		crtc->state->state = NULL;
   1425 	}
   1426 
   1427 	for (i = 0; i < dev->mode_config.num_total_plane; i++) {
   1428 		struct drm_plane *plane = state->planes[i];
   1429 
   1430 		if (!plane)
   1431 			continue;
   1432 
   1433 		plane->state->state = state;
   1434 		swap(state->plane_states[i], plane->state);
   1435 		plane->state->state = NULL;
   1436 	}
   1437 }
   1438 EXPORT_SYMBOL(drm_atomic_helper_swap_state);
   1439 
   1440 /**
   1441  * drm_atomic_helper_update_plane - Helper for primary plane update using atomic
   1442  * @plane: plane object to update
   1443  * @crtc: owning CRTC of owning plane
   1444  * @fb: framebuffer to flip onto plane
   1445  * @crtc_x: x offset of primary plane on crtc
   1446  * @crtc_y: y offset of primary plane on crtc
   1447  * @crtc_w: width of primary plane rectangle on crtc
   1448  * @crtc_h: height of primary plane rectangle on crtc
   1449  * @src_x: x offset of @fb for panning
   1450  * @src_y: y offset of @fb for panning
   1451  * @src_w: width of source rectangle in @fb
   1452  * @src_h: height of source rectangle in @fb
   1453  *
   1454  * Provides a default plane update handler using the atomic driver interface.
   1455  *
   1456  * RETURNS:
   1457  * Zero on success, error code on failure
   1458  */
   1459 int drm_atomic_helper_update_plane(struct drm_plane *plane,
   1460 				   struct drm_crtc *crtc,
   1461 				   struct drm_framebuffer *fb,
   1462 				   int crtc_x, int crtc_y,
   1463 				   unsigned int crtc_w, unsigned int crtc_h,
   1464 				   uint32_t src_x, uint32_t src_y,
   1465 				   uint32_t src_w, uint32_t src_h)
   1466 {
   1467 	struct drm_atomic_state *state;
   1468 	struct drm_plane_state *plane_state;
   1469 	int ret = 0;
   1470 
   1471 	state = drm_atomic_state_alloc(plane->dev);
   1472 	if (!state)
   1473 		return -ENOMEM;
   1474 
   1475 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
   1476 retry:
   1477 	plane_state = drm_atomic_get_plane_state(state, plane);
   1478 	if (IS_ERR(plane_state)) {
   1479 		ret = PTR_ERR(plane_state);
   1480 		goto fail;
   1481 	}
   1482 
   1483 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
   1484 	if (ret != 0)
   1485 		goto fail;
   1486 	drm_atomic_set_fb_for_plane(plane_state, fb);
   1487 	plane_state->crtc_x = crtc_x;
   1488 	plane_state->crtc_y = crtc_y;
   1489 	plane_state->crtc_h = crtc_h;
   1490 	plane_state->crtc_w = crtc_w;
   1491 	plane_state->src_x = src_x;
   1492 	plane_state->src_y = src_y;
   1493 	plane_state->src_h = src_h;
   1494 	plane_state->src_w = src_w;
   1495 
   1496 	if (plane == crtc->cursor)
   1497 		state->legacy_cursor_update = true;
   1498 
   1499 	ret = drm_atomic_commit(state);
   1500 	if (ret != 0)
   1501 		goto fail;
   1502 
   1503 	/* Driver takes ownership of state on successful commit. */
   1504 	return 0;
   1505 fail:
   1506 	if (ret == -EDEADLK)
   1507 		goto backoff;
   1508 
   1509 	drm_atomic_state_free(state);
   1510 
   1511 	return ret;
   1512 backoff:
   1513 	drm_atomic_state_clear(state);
   1514 	drm_atomic_legacy_backoff(state);
   1515 
   1516 	/*
   1517 	 * Someone might have exchanged the framebuffer while we dropped locks
   1518 	 * in the backoff code. We need to fix up the fb refcount tracking the
   1519 	 * core does for us.
   1520 	 */
   1521 	plane->old_fb = plane->fb;
   1522 
   1523 	goto retry;
   1524 }
   1525 EXPORT_SYMBOL(drm_atomic_helper_update_plane);
   1526 
   1527 /**
   1528  * drm_atomic_helper_disable_plane - Helper for primary plane disable using * atomic
   1529  * @plane: plane to disable
   1530  *
   1531  * Provides a default plane disable handler using the atomic driver interface.
   1532  *
   1533  * RETURNS:
   1534  * Zero on success, error code on failure
   1535  */
   1536 int drm_atomic_helper_disable_plane(struct drm_plane *plane)
   1537 {
   1538 	struct drm_atomic_state *state;
   1539 	struct drm_plane_state *plane_state;
   1540 	int ret = 0;
   1541 
   1542 	/*
   1543 	 * FIXME: Without plane->crtc set we can't get at the implicit legacy
   1544 	 * acquire context. The real fix will be to wire the acquire ctx through
   1545 	 * everywhere we need it, but meanwhile prevent chaos by just skipping
   1546 	 * this noop. The critical case is the cursor ioctls which a) only grab
   1547 	 * crtc/cursor-plane locks (so we need the crtc to get at the right
   1548 	 * acquire context) and b) can try to disable the plane multiple times.
   1549 	 */
   1550 	if (!plane->crtc)
   1551 		return 0;
   1552 
   1553 	state = drm_atomic_state_alloc(plane->dev);
   1554 	if (!state)
   1555 		return -ENOMEM;
   1556 
   1557 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(plane->crtc);
   1558 retry:
   1559 	plane_state = drm_atomic_get_plane_state(state, plane);
   1560 	if (IS_ERR(plane_state)) {
   1561 		ret = PTR_ERR(plane_state);
   1562 		goto fail;
   1563 	}
   1564 
   1565 	if (plane_state->crtc && (plane == plane->crtc->cursor))
   1566 		plane_state->state->legacy_cursor_update = true;
   1567 
   1568 	ret = __drm_atomic_helper_disable_plane(plane, plane_state);
   1569 	if (ret != 0)
   1570 		goto fail;
   1571 
   1572 	ret = drm_atomic_commit(state);
   1573 	if (ret != 0)
   1574 		goto fail;
   1575 
   1576 	/* Driver takes ownership of state on successful commit. */
   1577 	return 0;
   1578 fail:
   1579 	if (ret == -EDEADLK)
   1580 		goto backoff;
   1581 
   1582 	drm_atomic_state_free(state);
   1583 
   1584 	return ret;
   1585 backoff:
   1586 	drm_atomic_state_clear(state);
   1587 	drm_atomic_legacy_backoff(state);
   1588 
   1589 	/*
   1590 	 * Someone might have exchanged the framebuffer while we dropped locks
   1591 	 * in the backoff code. We need to fix up the fb refcount tracking the
   1592 	 * core does for us.
   1593 	 */
   1594 	plane->old_fb = plane->fb;
   1595 
   1596 	goto retry;
   1597 }
   1598 EXPORT_SYMBOL(drm_atomic_helper_disable_plane);
   1599 
   1600 /* just used from fb-helper and atomic-helper: */
   1601 int __drm_atomic_helper_disable_plane(struct drm_plane *plane,
   1602 		struct drm_plane_state *plane_state)
   1603 {
   1604 	int ret;
   1605 
   1606 	ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
   1607 	if (ret != 0)
   1608 		return ret;
   1609 
   1610 	drm_atomic_set_fb_for_plane(plane_state, NULL);
   1611 	plane_state->crtc_x = 0;
   1612 	plane_state->crtc_y = 0;
   1613 	plane_state->crtc_h = 0;
   1614 	plane_state->crtc_w = 0;
   1615 	plane_state->src_x = 0;
   1616 	plane_state->src_y = 0;
   1617 	plane_state->src_h = 0;
   1618 	plane_state->src_w = 0;
   1619 
   1620 	return 0;
   1621 }
   1622 
   1623 static int update_output_state(struct drm_atomic_state *state,
   1624 			       struct drm_mode_set *set)
   1625 {
   1626 	struct drm_device *dev = set->crtc->dev;
   1627 	struct drm_crtc *crtc;
   1628 	struct drm_crtc_state *crtc_state;
   1629 	struct drm_connector *connector;
   1630 	struct drm_connector_state *conn_state;
   1631 	int ret, i, j;
   1632 
   1633 	ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
   1634 			       state->acquire_ctx);
   1635 	if (ret)
   1636 		return ret;
   1637 
   1638 	/* First grab all affected connector/crtc states. */
   1639 	for (i = 0; i < set->num_connectors; i++) {
   1640 		conn_state = drm_atomic_get_connector_state(state,
   1641 							    set->connectors[i]);
   1642 		if (IS_ERR(conn_state))
   1643 			return PTR_ERR(conn_state);
   1644 	}
   1645 
   1646 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
   1647 		ret = drm_atomic_add_affected_connectors(state, crtc);
   1648 		if (ret)
   1649 			return ret;
   1650 	}
   1651 
   1652 	/* Then recompute connector->crtc links and crtc enabling state. */
   1653 	for_each_connector_in_state(state, connector, conn_state, i) {
   1654 		if (conn_state->crtc == set->crtc) {
   1655 			ret = drm_atomic_set_crtc_for_connector(conn_state,
   1656 								NULL);
   1657 			if (ret)
   1658 				return ret;
   1659 		}
   1660 
   1661 		for (j = 0; j < set->num_connectors; j++) {
   1662 			if (set->connectors[j] == connector) {
   1663 				ret = drm_atomic_set_crtc_for_connector(conn_state,
   1664 									set->crtc);
   1665 				if (ret)
   1666 					return ret;
   1667 				break;
   1668 			}
   1669 		}
   1670 	}
   1671 
   1672 	for_each_crtc_in_state(state, crtc, crtc_state, i) {
   1673 		/* Don't update ->enable for the CRTC in the set_config request,
   1674 		 * since a mismatch would indicate a bug in the upper layers.
   1675 		 * The actual modeset code later on will catch any
   1676 		 * inconsistencies here. */
   1677 		if (crtc == set->crtc)
   1678 			continue;
   1679 
   1680 		if (!drm_atomic_connectors_for_crtc(state, crtc)) {
   1681 			ret = drm_atomic_set_mode_prop_for_crtc(crtc_state,
   1682 								NULL);
   1683 			if (ret < 0)
   1684 				return ret;
   1685 
   1686 			crtc_state->active = false;
   1687 		}
   1688 	}
   1689 
   1690 	return 0;
   1691 }
   1692 
   1693 /**
   1694  * drm_atomic_helper_set_config - set a new config from userspace
   1695  * @set: mode set configuration
   1696  *
   1697  * Provides a default crtc set_config handler using the atomic driver interface.
   1698  *
   1699  * Returns:
   1700  * Returns 0 on success, negative errno numbers on failure.
   1701  */
   1702 int drm_atomic_helper_set_config(struct drm_mode_set *set)
   1703 {
   1704 	struct drm_atomic_state *state;
   1705 	struct drm_crtc *crtc = set->crtc;
   1706 	int ret = 0;
   1707 
   1708 	state = drm_atomic_state_alloc(crtc->dev);
   1709 	if (!state)
   1710 		return -ENOMEM;
   1711 
   1712 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
   1713 retry:
   1714 	ret = __drm_atomic_helper_set_config(set, state);
   1715 	if (ret != 0)
   1716 		goto fail;
   1717 
   1718 	ret = drm_atomic_commit(state);
   1719 	if (ret != 0)
   1720 		goto fail;
   1721 
   1722 	/* Driver takes ownership of state on successful commit. */
   1723 	return 0;
   1724 fail:
   1725 	if (ret == -EDEADLK)
   1726 		goto backoff;
   1727 
   1728 	drm_atomic_state_free(state);
   1729 
   1730 	return ret;
   1731 backoff:
   1732 	drm_atomic_state_clear(state);
   1733 	drm_atomic_legacy_backoff(state);
   1734 
   1735 	/*
   1736 	 * Someone might have exchanged the framebuffer while we dropped locks
   1737 	 * in the backoff code. We need to fix up the fb refcount tracking the
   1738 	 * core does for us.
   1739 	 */
   1740 	crtc->primary->old_fb = crtc->primary->fb;
   1741 
   1742 	goto retry;
   1743 }
   1744 EXPORT_SYMBOL(drm_atomic_helper_set_config);
   1745 
   1746 /* just used from fb-helper and atomic-helper: */
   1747 int __drm_atomic_helper_set_config(struct drm_mode_set *set,
   1748 		struct drm_atomic_state *state)
   1749 {
   1750 	struct drm_crtc_state *crtc_state;
   1751 	struct drm_plane_state *primary_state;
   1752 	struct drm_crtc *crtc = set->crtc;
   1753 	int hdisplay, vdisplay;
   1754 	int ret;
   1755 
   1756 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
   1757 	if (IS_ERR(crtc_state))
   1758 		return PTR_ERR(crtc_state);
   1759 
   1760 	primary_state = drm_atomic_get_plane_state(state, crtc->primary);
   1761 	if (IS_ERR(primary_state))
   1762 		return PTR_ERR(primary_state);
   1763 
   1764 	if (!set->mode) {
   1765 		WARN_ON(set->fb);
   1766 		WARN_ON(set->num_connectors);
   1767 
   1768 		ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
   1769 		if (ret != 0)
   1770 			return ret;
   1771 
   1772 		crtc_state->active = false;
   1773 
   1774 		ret = drm_atomic_set_crtc_for_plane(primary_state, NULL);
   1775 		if (ret != 0)
   1776 			return ret;
   1777 
   1778 		drm_atomic_set_fb_for_plane(primary_state, NULL);
   1779 
   1780 		goto commit;
   1781 	}
   1782 
   1783 	WARN_ON(!set->fb);
   1784 	WARN_ON(!set->num_connectors);
   1785 
   1786 	ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
   1787 	if (ret != 0)
   1788 		return ret;
   1789 
   1790 	crtc_state->active = true;
   1791 
   1792 	ret = drm_atomic_set_crtc_for_plane(primary_state, crtc);
   1793 	if (ret != 0)
   1794 		return ret;
   1795 
   1796 	drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
   1797 
   1798 	drm_atomic_set_fb_for_plane(primary_state, set->fb);
   1799 	primary_state->crtc_x = 0;
   1800 	primary_state->crtc_y = 0;
   1801 	primary_state->crtc_h = vdisplay;
   1802 	primary_state->crtc_w = hdisplay;
   1803 	primary_state->src_x = set->x << 16;
   1804 	primary_state->src_y = set->y << 16;
   1805 	if (primary_state->rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
   1806 		primary_state->src_h = hdisplay << 16;
   1807 		primary_state->src_w = vdisplay << 16;
   1808 	} else {
   1809 		primary_state->src_h = vdisplay << 16;
   1810 		primary_state->src_w = hdisplay << 16;
   1811 	}
   1812 
   1813 commit:
   1814 	ret = update_output_state(state, set);
   1815 	if (ret)
   1816 		return ret;
   1817 
   1818 	return 0;
   1819 }
   1820 
   1821 /**
   1822  * drm_atomic_helper_crtc_set_property - helper for crtc properties
   1823  * @crtc: DRM crtc
   1824  * @property: DRM property
   1825  * @val: value of property
   1826  *
   1827  * Provides a default crtc set_property handler using the atomic driver
   1828  * interface.
   1829  *
   1830  * RETURNS:
   1831  * Zero on success, error code on failure
   1832  */
   1833 int
   1834 drm_atomic_helper_crtc_set_property(struct drm_crtc *crtc,
   1835 				    struct drm_property *property,
   1836 				    uint64_t val)
   1837 {
   1838 	struct drm_atomic_state *state;
   1839 	struct drm_crtc_state *crtc_state;
   1840 	int ret = 0;
   1841 
   1842 	state = drm_atomic_state_alloc(crtc->dev);
   1843 	if (!state)
   1844 		return -ENOMEM;
   1845 
   1846 	/* ->set_property is always called with all locks held. */
   1847 	state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
   1848 retry:
   1849 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
   1850 	if (IS_ERR(crtc_state)) {
   1851 		ret = PTR_ERR(crtc_state);
   1852 		goto fail;
   1853 	}
   1854 
   1855 	ret = drm_atomic_crtc_set_property(crtc, crtc_state,
   1856 			property, val);
   1857 	if (ret)
   1858 		goto fail;
   1859 
   1860 	ret = drm_atomic_commit(state);
   1861 	if (ret != 0)
   1862 		goto fail;
   1863 
   1864 	/* Driver takes ownership of state on successful commit. */
   1865 	return 0;
   1866 fail:
   1867 	if (ret == -EDEADLK)
   1868 		goto backoff;
   1869 
   1870 	drm_atomic_state_free(state);
   1871 
   1872 	return ret;
   1873 backoff:
   1874 	drm_atomic_state_clear(state);
   1875 	drm_atomic_legacy_backoff(state);
   1876 
   1877 	goto retry;
   1878 }
   1879 EXPORT_SYMBOL(drm_atomic_helper_crtc_set_property);
   1880 
   1881 /**
   1882  * drm_atomic_helper_plane_set_property - helper for plane properties
   1883  * @plane: DRM plane
   1884  * @property: DRM property
   1885  * @val: value of property
   1886  *
   1887  * Provides a default plane set_property handler using the atomic driver
   1888  * interface.
   1889  *
   1890  * RETURNS:
   1891  * Zero on success, error code on failure
   1892  */
   1893 int
   1894 drm_atomic_helper_plane_set_property(struct drm_plane *plane,
   1895 				    struct drm_property *property,
   1896 				    uint64_t val)
   1897 {
   1898 	struct drm_atomic_state *state;
   1899 	struct drm_plane_state *plane_state;
   1900 	int ret = 0;
   1901 
   1902 	state = drm_atomic_state_alloc(plane->dev);
   1903 	if (!state)
   1904 		return -ENOMEM;
   1905 
   1906 	/* ->set_property is always called with all locks held. */
   1907 	state->acquire_ctx = plane->dev->mode_config.acquire_ctx;
   1908 retry:
   1909 	plane_state = drm_atomic_get_plane_state(state, plane);
   1910 	if (IS_ERR(plane_state)) {
   1911 		ret = PTR_ERR(plane_state);
   1912 		goto fail;
   1913 	}
   1914 
   1915 	ret = drm_atomic_plane_set_property(plane, plane_state,
   1916 			property, val);
   1917 	if (ret)
   1918 		goto fail;
   1919 
   1920 	ret = drm_atomic_commit(state);
   1921 	if (ret != 0)
   1922 		goto fail;
   1923 
   1924 	/* Driver takes ownership of state on successful commit. */
   1925 	return 0;
   1926 fail:
   1927 	if (ret == -EDEADLK)
   1928 		goto backoff;
   1929 
   1930 	drm_atomic_state_free(state);
   1931 
   1932 	return ret;
   1933 backoff:
   1934 	drm_atomic_state_clear(state);
   1935 	drm_atomic_legacy_backoff(state);
   1936 
   1937 	goto retry;
   1938 }
   1939 EXPORT_SYMBOL(drm_atomic_helper_plane_set_property);
   1940 
   1941 /**
   1942  * drm_atomic_helper_connector_set_property - helper for connector properties
   1943  * @connector: DRM connector
   1944  * @property: DRM property
   1945  * @val: value of property
   1946  *
   1947  * Provides a default connector set_property handler using the atomic driver
   1948  * interface.
   1949  *
   1950  * RETURNS:
   1951  * Zero on success, error code on failure
   1952  */
   1953 int
   1954 drm_atomic_helper_connector_set_property(struct drm_connector *connector,
   1955 				    struct drm_property *property,
   1956 				    uint64_t val)
   1957 {
   1958 	struct drm_atomic_state *state;
   1959 	struct drm_connector_state *connector_state;
   1960 	int ret = 0;
   1961 
   1962 	state = drm_atomic_state_alloc(connector->dev);
   1963 	if (!state)
   1964 		return -ENOMEM;
   1965 
   1966 	/* ->set_property is always called with all locks held. */
   1967 	state->acquire_ctx = connector->dev->mode_config.acquire_ctx;
   1968 retry:
   1969 	connector_state = drm_atomic_get_connector_state(state, connector);
   1970 	if (IS_ERR(connector_state)) {
   1971 		ret = PTR_ERR(connector_state);
   1972 		goto fail;
   1973 	}
   1974 
   1975 	ret = drm_atomic_connector_set_property(connector, connector_state,
   1976 			property, val);
   1977 	if (ret)
   1978 		goto fail;
   1979 
   1980 	ret = drm_atomic_commit(state);
   1981 	if (ret != 0)
   1982 		goto fail;
   1983 
   1984 	/* Driver takes ownership of state on successful commit. */
   1985 	return 0;
   1986 fail:
   1987 	if (ret == -EDEADLK)
   1988 		goto backoff;
   1989 
   1990 	drm_atomic_state_free(state);
   1991 
   1992 	return ret;
   1993 backoff:
   1994 	drm_atomic_state_clear(state);
   1995 	drm_atomic_legacy_backoff(state);
   1996 
   1997 	goto retry;
   1998 }
   1999 EXPORT_SYMBOL(drm_atomic_helper_connector_set_property);
   2000 
   2001 /**
   2002  * drm_atomic_helper_page_flip - execute a legacy page flip
   2003  * @crtc: DRM crtc
   2004  * @fb: DRM framebuffer
   2005  * @event: optional DRM event to signal upon completion
   2006  * @flags: flip flags for non-vblank sync'ed updates
   2007  *
   2008  * Provides a default page flip implementation using the atomic driver interface.
   2009  *
   2010  * Note that for now so called async page flips (i.e. updates which are not
   2011  * synchronized to vblank) are not supported, since the atomic interfaces have
   2012  * no provisions for this yet.
   2013  *
   2014  * Returns:
   2015  * Returns 0 on success, negative errno numbers on failure.
   2016  */
   2017 int drm_atomic_helper_page_flip(struct drm_crtc *crtc,
   2018 				struct drm_framebuffer *fb,
   2019 				struct drm_pending_vblank_event *event,
   2020 				uint32_t flags)
   2021 {
   2022 	struct drm_plane *plane = crtc->primary;
   2023 	struct drm_atomic_state *state;
   2024 	struct drm_plane_state *plane_state;
   2025 	struct drm_crtc_state *crtc_state;
   2026 	int ret = 0;
   2027 
   2028 	if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
   2029 		return -EINVAL;
   2030 
   2031 	state = drm_atomic_state_alloc(plane->dev);
   2032 	if (!state)
   2033 		return -ENOMEM;
   2034 
   2035 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
   2036 retry:
   2037 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
   2038 	if (IS_ERR(crtc_state)) {
   2039 		ret = PTR_ERR(crtc_state);
   2040 		goto fail;
   2041 	}
   2042 	crtc_state->event = event;
   2043 
   2044 	plane_state = drm_atomic_get_plane_state(state, plane);
   2045 	if (IS_ERR(plane_state)) {
   2046 		ret = PTR_ERR(plane_state);
   2047 		goto fail;
   2048 	}
   2049 
   2050 	ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
   2051 	if (ret != 0)
   2052 		goto fail;
   2053 	drm_atomic_set_fb_for_plane(plane_state, fb);
   2054 
   2055 	ret = drm_atomic_async_commit(state);
   2056 	if (ret != 0)
   2057 		goto fail;
   2058 
   2059 	/* Driver takes ownership of state on successful async commit. */
   2060 	return 0;
   2061 fail:
   2062 	if (ret == -EDEADLK)
   2063 		goto backoff;
   2064 
   2065 	drm_atomic_state_free(state);
   2066 
   2067 	return ret;
   2068 backoff:
   2069 	drm_atomic_state_clear(state);
   2070 	drm_atomic_legacy_backoff(state);
   2071 
   2072 	/*
   2073 	 * Someone might have exchanged the framebuffer while we dropped locks
   2074 	 * in the backoff code. We need to fix up the fb refcount tracking the
   2075 	 * core does for us.
   2076 	 */
   2077 	plane->old_fb = plane->fb;
   2078 
   2079 	goto retry;
   2080 }
   2081 EXPORT_SYMBOL(drm_atomic_helper_page_flip);
   2082 
   2083 /**
   2084  * drm_atomic_helper_connector_dpms() - connector dpms helper implementation
   2085  * @connector: affected connector
   2086  * @mode: DPMS mode
   2087  *
   2088  * This is the main helper function provided by the atomic helper framework for
   2089  * implementing the legacy DPMS connector interface. It computes the new desired
   2090  * ->active state for the corresponding CRTC (if the connector is enabled) and
   2091  *  updates it.
   2092  *
   2093  * Returns:
   2094  * Returns 0 on success, negative errno numbers on failure.
   2095  */
   2096 int drm_atomic_helper_connector_dpms(struct drm_connector *connector,
   2097 				     int mode)
   2098 {
   2099 	struct drm_mode_config *config = &connector->dev->mode_config;
   2100 	struct drm_atomic_state *state;
   2101 	struct drm_crtc_state *crtc_state;
   2102 	struct drm_crtc *crtc;
   2103 	struct drm_connector *tmp_connector;
   2104 	int ret;
   2105 	bool active = false;
   2106 	int old_mode = connector->dpms;
   2107 
   2108 	if (mode != DRM_MODE_DPMS_ON)
   2109 		mode = DRM_MODE_DPMS_OFF;
   2110 
   2111 	connector->dpms = mode;
   2112 	crtc = connector->state->crtc;
   2113 
   2114 	if (!crtc)
   2115 		return 0;
   2116 
   2117 	state = drm_atomic_state_alloc(connector->dev);
   2118 	if (!state)
   2119 		return -ENOMEM;
   2120 
   2121 	state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
   2122 retry:
   2123 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
   2124 	if (IS_ERR(crtc_state)) {
   2125 		ret = PTR_ERR(crtc_state);
   2126 		goto fail;
   2127 	}
   2128 
   2129 	WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
   2130 
   2131 	drm_for_each_connector(tmp_connector, connector->dev) {
   2132 		if (tmp_connector->state->crtc != crtc)
   2133 			continue;
   2134 
   2135 		if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
   2136 			active = true;
   2137 			break;
   2138 		}
   2139 	}
   2140 	crtc_state->active = active;
   2141 
   2142 	ret = drm_atomic_commit(state);
   2143 	if (ret != 0)
   2144 		goto fail;
   2145 
   2146 	/* Driver takes ownership of state on successful commit. */
   2147 	return 0;
   2148 fail:
   2149 	if (ret == -EDEADLK)
   2150 		goto backoff;
   2151 
   2152 	connector->dpms = old_mode;
   2153 	drm_atomic_state_free(state);
   2154 
   2155 	return ret;
   2156 backoff:
   2157 	drm_atomic_state_clear(state);
   2158 	drm_atomic_legacy_backoff(state);
   2159 
   2160 	goto retry;
   2161 }
   2162 EXPORT_SYMBOL(drm_atomic_helper_connector_dpms);
   2163 
   2164 /**
   2165  * DOC: atomic state reset and initialization
   2166  *
   2167  * Both the drm core and the atomic helpers assume that there is always the full
   2168  * and correct atomic software state for all connectors, CRTCs and planes
   2169  * available. Which is a bit a problem on driver load and also after system
   2170  * suspend. One way to solve this is to have a hardware state read-out
   2171  * infrastructure which reconstructs the full software state (e.g. the i915
   2172  * driver).
   2173  *
   2174  * The simpler solution is to just reset the software state to everything off,
   2175  * which is easiest to do by calling drm_mode_config_reset(). To facilitate this
   2176  * the atomic helpers provide default reset implementations for all hooks.
   2177  */
   2178 
   2179 /**
   2180  * drm_atomic_helper_crtc_reset - default ->reset hook for CRTCs
   2181  * @crtc: drm CRTC
   2182  *
   2183  * Resets the atomic state for @crtc by freeing the state pointer (which might
   2184  * be NULL, e.g. at driver load time) and allocating a new empty state object.
   2185  */
   2186 void drm_atomic_helper_crtc_reset(struct drm_crtc *crtc)
   2187 {
   2188 	if (crtc->state && crtc->state->mode_blob)
   2189 		drm_property_unreference_blob(crtc->state->mode_blob);
   2190 	kfree(crtc->state);
   2191 	crtc->state = kzalloc(sizeof(*crtc->state), GFP_KERNEL);
   2192 
   2193 	if (crtc->state)
   2194 		crtc->state->crtc = crtc;
   2195 }
   2196 EXPORT_SYMBOL(drm_atomic_helper_crtc_reset);
   2197 
   2198 /**
   2199  * __drm_atomic_helper_crtc_duplicate_state - copy atomic CRTC state
   2200  * @crtc: CRTC object
   2201  * @state: atomic CRTC state
   2202  *
   2203  * Copies atomic state from a CRTC's current state and resets inferred values.
   2204  * This is useful for drivers that subclass the CRTC state.
   2205  */
   2206 void __drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc,
   2207 					      struct drm_crtc_state *state)
   2208 {
   2209 	memcpy(state, crtc->state, sizeof(*state));
   2210 
   2211 	if (state->mode_blob)
   2212 		drm_property_reference_blob(state->mode_blob);
   2213 	state->mode_changed = false;
   2214 	state->active_changed = false;
   2215 	state->planes_changed = false;
   2216 	state->connectors_changed = false;
   2217 	state->event = NULL;
   2218 }
   2219 EXPORT_SYMBOL(__drm_atomic_helper_crtc_duplicate_state);
   2220 
   2221 /**
   2222  * drm_atomic_helper_crtc_duplicate_state - default state duplicate hook
   2223  * @crtc: drm CRTC
   2224  *
   2225  * Default CRTC state duplicate hook for drivers which don't have their own
   2226  * subclassed CRTC state structure.
   2227  */
   2228 struct drm_crtc_state *
   2229 drm_atomic_helper_crtc_duplicate_state(struct drm_crtc *crtc)
   2230 {
   2231 	struct drm_crtc_state *state;
   2232 
   2233 	if (WARN_ON(!crtc->state))
   2234 		return NULL;
   2235 
   2236 	state = kmalloc(sizeof(*state), GFP_KERNEL);
   2237 	if (state)
   2238 		__drm_atomic_helper_crtc_duplicate_state(crtc, state);
   2239 
   2240 	return state;
   2241 }
   2242 EXPORT_SYMBOL(drm_atomic_helper_crtc_duplicate_state);
   2243 
   2244 /**
   2245  * __drm_atomic_helper_crtc_destroy_state - release CRTC state
   2246  * @crtc: CRTC object
   2247  * @state: CRTC state object to release
   2248  *
   2249  * Releases all resources stored in the CRTC state without actually freeing
   2250  * the memory of the CRTC state. This is useful for drivers that subclass the
   2251  * CRTC state.
   2252  */
   2253 void __drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
   2254 					    struct drm_crtc_state *state)
   2255 {
   2256 	if (state->mode_blob)
   2257 		drm_property_unreference_blob(state->mode_blob);
   2258 }
   2259 EXPORT_SYMBOL(__drm_atomic_helper_crtc_destroy_state);
   2260 
   2261 /**
   2262  * drm_atomic_helper_crtc_destroy_state - default state destroy hook
   2263  * @crtc: drm CRTC
   2264  * @state: CRTC state object to release
   2265  *
   2266  * Default CRTC state destroy hook for drivers which don't have their own
   2267  * subclassed CRTC state structure.
   2268  */
   2269 void drm_atomic_helper_crtc_destroy_state(struct drm_crtc *crtc,
   2270 					  struct drm_crtc_state *state)
   2271 {
   2272 	__drm_atomic_helper_crtc_destroy_state(crtc, state);
   2273 	kfree(state);
   2274 }
   2275 EXPORT_SYMBOL(drm_atomic_helper_crtc_destroy_state);
   2276 
   2277 /**
   2278  * drm_atomic_helper_plane_reset - default ->reset hook for planes
   2279  * @plane: drm plane
   2280  *
   2281  * Resets the atomic state for @plane by freeing the state pointer (which might
   2282  * be NULL, e.g. at driver load time) and allocating a new empty state object.
   2283  */
   2284 void drm_atomic_helper_plane_reset(struct drm_plane *plane)
   2285 {
   2286 	if (plane->state && plane->state->fb)
   2287 		drm_framebuffer_unreference(plane->state->fb);
   2288 
   2289 	kfree(plane->state);
   2290 	plane->state = kzalloc(sizeof(*plane->state), GFP_KERNEL);
   2291 
   2292 	if (plane->state)
   2293 		plane->state->plane = plane;
   2294 }
   2295 EXPORT_SYMBOL(drm_atomic_helper_plane_reset);
   2296 
   2297 /**
   2298  * __drm_atomic_helper_plane_duplicate_state - copy atomic plane state
   2299  * @plane: plane object
   2300  * @state: atomic plane state
   2301  *
   2302  * Copies atomic state from a plane's current state. This is useful for
   2303  * drivers that subclass the plane state.
   2304  */
   2305 void __drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane,
   2306 					       struct drm_plane_state *state)
   2307 {
   2308 	memcpy(state, plane->state, sizeof(*state));
   2309 
   2310 	if (state->fb)
   2311 		drm_framebuffer_reference(state->fb);
   2312 }
   2313 EXPORT_SYMBOL(__drm_atomic_helper_plane_duplicate_state);
   2314 
   2315 /**
   2316  * drm_atomic_helper_plane_duplicate_state - default state duplicate hook
   2317  * @plane: drm plane
   2318  *
   2319  * Default plane state duplicate hook for drivers which don't have their own
   2320  * subclassed plane state structure.
   2321  */
   2322 struct drm_plane_state *
   2323 drm_atomic_helper_plane_duplicate_state(struct drm_plane *plane)
   2324 {
   2325 	struct drm_plane_state *state;
   2326 
   2327 	if (WARN_ON(!plane->state))
   2328 		return NULL;
   2329 
   2330 	state = kmalloc(sizeof(*state), GFP_KERNEL);
   2331 	if (state)
   2332 		__drm_atomic_helper_plane_duplicate_state(plane, state);
   2333 
   2334 	return state;
   2335 }
   2336 EXPORT_SYMBOL(drm_atomic_helper_plane_duplicate_state);
   2337 
   2338 /**
   2339  * __drm_atomic_helper_plane_destroy_state - release plane state
   2340  * @plane: plane object
   2341  * @state: plane state object to release
   2342  *
   2343  * Releases all resources stored in the plane state without actually freeing
   2344  * the memory of the plane state. This is useful for drivers that subclass the
   2345  * plane state.
   2346  */
   2347 void __drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
   2348 					     struct drm_plane_state *state)
   2349 {
   2350 	if (state->fb)
   2351 		drm_framebuffer_unreference(state->fb);
   2352 }
   2353 EXPORT_SYMBOL(__drm_atomic_helper_plane_destroy_state);
   2354 
   2355 /**
   2356  * drm_atomic_helper_plane_destroy_state - default state destroy hook
   2357  * @plane: drm plane
   2358  * @state: plane state object to release
   2359  *
   2360  * Default plane state destroy hook for drivers which don't have their own
   2361  * subclassed plane state structure.
   2362  */
   2363 void drm_atomic_helper_plane_destroy_state(struct drm_plane *plane,
   2364 					   struct drm_plane_state *state)
   2365 {
   2366 	__drm_atomic_helper_plane_destroy_state(plane, state);
   2367 	kfree(state);
   2368 }
   2369 EXPORT_SYMBOL(drm_atomic_helper_plane_destroy_state);
   2370 
   2371 /**
   2372  * drm_atomic_helper_connector_reset - default ->reset hook for connectors
   2373  * @connector: drm connector
   2374  *
   2375  * Resets the atomic state for @connector by freeing the state pointer (which
   2376  * might be NULL, e.g. at driver load time) and allocating a new empty state
   2377  * object.
   2378  */
   2379 void drm_atomic_helper_connector_reset(struct drm_connector *connector)
   2380 {
   2381 	kfree(connector->state);
   2382 	connector->state = kzalloc(sizeof(*connector->state), GFP_KERNEL);
   2383 
   2384 	if (connector->state)
   2385 		connector->state->connector = connector;
   2386 }
   2387 EXPORT_SYMBOL(drm_atomic_helper_connector_reset);
   2388 
   2389 /**
   2390  * __drm_atomic_helper_connector_duplicate_state - copy atomic connector state
   2391  * @connector: connector object
   2392  * @state: atomic connector state
   2393  *
   2394  * Copies atomic state from a connector's current state. This is useful for
   2395  * drivers that subclass the connector state.
   2396  */
   2397 void
   2398 __drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector,
   2399 					    struct drm_connector_state *state)
   2400 {
   2401 	memcpy(state, connector->state, sizeof(*state));
   2402 }
   2403 EXPORT_SYMBOL(__drm_atomic_helper_connector_duplicate_state);
   2404 
   2405 /**
   2406  * drm_atomic_helper_connector_duplicate_state - default state duplicate hook
   2407  * @connector: drm connector
   2408  *
   2409  * Default connector state duplicate hook for drivers which don't have their own
   2410  * subclassed connector state structure.
   2411  */
   2412 struct drm_connector_state *
   2413 drm_atomic_helper_connector_duplicate_state(struct drm_connector *connector)
   2414 {
   2415 	struct drm_connector_state *state;
   2416 
   2417 	if (WARN_ON(!connector->state))
   2418 		return NULL;
   2419 
   2420 	state = kmalloc(sizeof(*state), GFP_KERNEL);
   2421 	if (state)
   2422 		__drm_atomic_helper_connector_duplicate_state(connector, state);
   2423 
   2424 	return state;
   2425 }
   2426 EXPORT_SYMBOL(drm_atomic_helper_connector_duplicate_state);
   2427 
   2428 /**
   2429  * drm_atomic_helper_duplicate_state - duplicate an atomic state object
   2430  * @dev: DRM device
   2431  * @ctx: lock acquisition context
   2432  *
   2433  * Makes a copy of the current atomic state by looping over all objects and
   2434  * duplicating their respective states.
   2435  *
   2436  * Note that this treats atomic state as persistent between save and restore.
   2437  * Drivers must make sure that this is possible and won't result in confusion
   2438  * or erroneous behaviour.
   2439  *
   2440  * Note that if callers haven't already acquired all modeset locks this might
   2441  * return -EDEADLK, which must be handled by calling drm_modeset_backoff().
   2442  *
   2443  * Returns:
   2444  * A pointer to the copy of the atomic state object on success or an
   2445  * ERR_PTR()-encoded error code on failure.
   2446  */
   2447 struct drm_atomic_state *
   2448 drm_atomic_helper_duplicate_state(struct drm_device *dev,
   2449 				  struct drm_modeset_acquire_ctx *ctx)
   2450 {
   2451 	struct drm_atomic_state *state;
   2452 	struct drm_connector *conn;
   2453 	struct drm_plane *plane;
   2454 	struct drm_crtc *crtc;
   2455 	int err = 0;
   2456 
   2457 	state = drm_atomic_state_alloc(dev);
   2458 	if (!state)
   2459 		return ERR_PTR(-ENOMEM);
   2460 
   2461 	state->acquire_ctx = ctx;
   2462 
   2463 	drm_for_each_crtc(crtc, dev) {
   2464 		struct drm_crtc_state *crtc_state;
   2465 
   2466 		crtc_state = drm_atomic_get_crtc_state(state, crtc);
   2467 		if (IS_ERR(crtc_state)) {
   2468 			err = PTR_ERR(crtc_state);
   2469 			goto free;
   2470 		}
   2471 	}
   2472 
   2473 	drm_for_each_plane(plane, dev) {
   2474 		struct drm_plane_state *plane_state;
   2475 
   2476 		plane_state = drm_atomic_get_plane_state(state, plane);
   2477 		if (IS_ERR(plane_state)) {
   2478 			err = PTR_ERR(plane_state);
   2479 			goto free;
   2480 		}
   2481 	}
   2482 
   2483 	drm_for_each_connector(conn, dev) {
   2484 		struct drm_connector_state *conn_state;
   2485 
   2486 		conn_state = drm_atomic_get_connector_state(state, conn);
   2487 		if (IS_ERR(conn_state)) {
   2488 			err = PTR_ERR(conn_state);
   2489 			goto free;
   2490 		}
   2491 	}
   2492 
   2493 	/* clear the acquire context so that it isn't accidentally reused */
   2494 	state->acquire_ctx = NULL;
   2495 
   2496 free:
   2497 	if (err < 0) {
   2498 		drm_atomic_state_free(state);
   2499 		state = ERR_PTR(err);
   2500 	}
   2501 
   2502 	return state;
   2503 }
   2504 EXPORT_SYMBOL(drm_atomic_helper_duplicate_state);
   2505 
   2506 /**
   2507  * __drm_atomic_helper_connector_destroy_state - release connector state
   2508  * @connector: connector object
   2509  * @state: connector state object to release
   2510  *
   2511  * Releases all resources stored in the connector state without actually
   2512  * freeing the memory of the connector state. This is useful for drivers that
   2513  * subclass the connector state.
   2514  */
   2515 void
   2516 __drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
   2517 					    struct drm_connector_state *state)
   2518 {
   2519 	/*
   2520 	 * This is currently a placeholder so that drivers that subclass the
   2521 	 * state will automatically do the right thing if code is ever added
   2522 	 * to this function.
   2523 	 */
   2524 }
   2525 EXPORT_SYMBOL(__drm_atomic_helper_connector_destroy_state);
   2526 
   2527 /**
   2528  * drm_atomic_helper_connector_destroy_state - default state destroy hook
   2529  * @connector: drm connector
   2530  * @state: connector state object to release
   2531  *
   2532  * Default connector state destroy hook for drivers which don't have their own
   2533  * subclassed connector state structure.
   2534  */
   2535 void drm_atomic_helper_connector_destroy_state(struct drm_connector *connector,
   2536 					  struct drm_connector_state *state)
   2537 {
   2538 	__drm_atomic_helper_connector_destroy_state(connector, state);
   2539 	kfree(state);
   2540 }
   2541 EXPORT_SYMBOL(drm_atomic_helper_connector_destroy_state);
   2542