Home | History | Annotate | Line # | Download | only in drm
drm_crtc_helper.c revision 1.1.1.4
      1 /*	$NetBSD: drm_crtc_helper.c,v 1.1.1.4 2021/12/18 20:11:00 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006-2008 Intel Corporation
      5  * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
      6  *
      7  * DRM core CRTC related functions
      8  *
      9  * Permission to use, copy, modify, distribute, and sell this software and its
     10  * documentation for any purpose is hereby granted without fee, provided that
     11  * the above copyright notice appear in all copies and that both that copyright
     12  * notice and this permission notice appear in supporting documentation, and
     13  * that the name of the copyright holders not be used in advertising or
     14  * publicity pertaining to distribution of the software without specific,
     15  * written prior permission.  The copyright holders make no representations
     16  * about the suitability of this software for any purpose.  It is provided "as
     17  * is" without express or implied warranty.
     18  *
     19  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     20  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     21  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     22  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     23  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     24  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     25  * OF THIS SOFTWARE.
     26  *
     27  * Authors:
     28  *      Keith Packard
     29  *	Eric Anholt <eric (at) anholt.net>
     30  *      Dave Airlie <airlied (at) linux.ie>
     31  *      Jesse Barnes <jesse.barnes (at) intel.com>
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: drm_crtc_helper.c,v 1.1.1.4 2021/12/18 20:11:00 riastradh Exp $");
     36 
     37 #include <linux/export.h>
     38 #include <linux/kernel.h>
     39 #include <linux/moduleparam.h>
     40 
     41 #include <drm/drm_atomic.h>
     42 #include <drm/drm_atomic_helper.h>
     43 #include <drm/drm_atomic_uapi.h>
     44 #include <drm/drm_bridge.h>
     45 #include <drm/drm_crtc.h>
     46 #include <drm/drm_crtc_helper.h>
     47 #include <drm/drm_drv.h>
     48 #include <drm/drm_edid.h>
     49 #include <drm/drm_encoder.h>
     50 #include <drm/drm_fb_helper.h>
     51 #include <drm/drm_fourcc.h>
     52 #include <drm/drm_plane_helper.h>
     53 #include <drm/drm_print.h>
     54 #include <drm/drm_vblank.h>
     55 
     56 #include "drm_crtc_helper_internal.h"
     57 
     58 /**
     59  * DOC: overview
     60  *
     61  * The CRTC modeset helper library provides a default set_config implementation
     62  * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
     63  * the same callbacks which drivers can use to e.g. restore the modeset
     64  * configuration on resume with drm_helper_resume_force_mode().
     65  *
     66  * Note that this helper library doesn't track the current power state of CRTCs
     67  * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
     68  * though the hardware is already in the desired state. This deficiency has been
     69  * fixed in the atomic helpers.
     70  *
     71  * The driver callbacks are mostly compatible with the atomic modeset helpers,
     72  * except for the handling of the primary plane: Atomic helpers require that the
     73  * primary plane is implemented as a real standalone plane and not directly tied
     74  * to the CRTC state. For easier transition this library provides functions to
     75  * implement the old semantics required by the CRTC helpers using the new plane
     76  * and atomic helper callbacks.
     77  *
     78  * Drivers are strongly urged to convert to the atomic helpers (by way of first
     79  * converting to the plane helpers). New drivers must not use these functions
     80  * but need to implement the atomic interface instead, potentially using the
     81  * atomic helpers for that.
     82  *
     83  * These legacy modeset helpers use the same function table structures as
     84  * all other modesetting helpers. See the documentation for struct
     85  * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
     86  * &drm_connector_helper_funcs.
     87  */
     88 
     89 /**
     90  * drm_helper_encoder_in_use - check if a given encoder is in use
     91  * @encoder: encoder to check
     92  *
     93  * Checks whether @encoder is with the current mode setting output configuration
     94  * in use by any connector. This doesn't mean that it is actually enabled since
     95  * the DPMS state is tracked separately.
     96  *
     97  * Returns:
     98  * True if @encoder is used, false otherwise.
     99  */
    100 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
    101 {
    102 	struct drm_connector *connector;
    103 	struct drm_connector_list_iter conn_iter;
    104 	struct drm_device *dev = encoder->dev;
    105 
    106 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    107 
    108 	/*
    109 	 * We can expect this mutex to be locked if we are not panicking.
    110 	 * Locking is currently fubar in the panic handler.
    111 	 */
    112 	if (!oops_in_progress) {
    113 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
    114 		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
    115 	}
    116 
    117 
    118 	drm_connector_list_iter_begin(dev, &conn_iter);
    119 	drm_for_each_connector_iter(connector, &conn_iter) {
    120 		if (connector->encoder == encoder) {
    121 			drm_connector_list_iter_end(&conn_iter);
    122 			return true;
    123 		}
    124 	}
    125 	drm_connector_list_iter_end(&conn_iter);
    126 	return false;
    127 }
    128 EXPORT_SYMBOL(drm_helper_encoder_in_use);
    129 
    130 /**
    131  * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
    132  * @crtc: CRTC to check
    133  *
    134  * Checks whether @crtc is with the current mode setting output configuration
    135  * in use by any connector. This doesn't mean that it is actually enabled since
    136  * the DPMS state is tracked separately.
    137  *
    138  * Returns:
    139  * True if @crtc is used, false otherwise.
    140  */
    141 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
    142 {
    143 	struct drm_encoder *encoder;
    144 	struct drm_device *dev = crtc->dev;
    145 
    146 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    147 
    148 	/*
    149 	 * We can expect this mutex to be locked if we are not panicking.
    150 	 * Locking is currently fubar in the panic handler.
    151 	 */
    152 	if (!oops_in_progress)
    153 		WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
    154 
    155 	drm_for_each_encoder(encoder, dev)
    156 		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
    157 			return true;
    158 	return false;
    159 }
    160 EXPORT_SYMBOL(drm_helper_crtc_in_use);
    161 
    162 static void
    163 drm_encoder_disable(struct drm_encoder *encoder)
    164 {
    165 	const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
    166 
    167 	if (!encoder_funcs)
    168 		return;
    169 
    170 	if (encoder_funcs->disable)
    171 		(*encoder_funcs->disable)(encoder);
    172 	else if (encoder_funcs->dpms)
    173 		(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
    174 }
    175 
    176 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
    177 {
    178 	struct drm_encoder *encoder;
    179 	struct drm_crtc *crtc;
    180 
    181 	drm_warn_on_modeset_not_all_locked(dev);
    182 
    183 	drm_for_each_encoder(encoder, dev) {
    184 		if (!drm_helper_encoder_in_use(encoder)) {
    185 			drm_encoder_disable(encoder);
    186 			/* disconnect encoder from any connector */
    187 			encoder->crtc = NULL;
    188 		}
    189 	}
    190 
    191 	drm_for_each_crtc(crtc, dev) {
    192 		const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
    193 		crtc->enabled = drm_helper_crtc_in_use(crtc);
    194 		if (!crtc->enabled) {
    195 			if (crtc_funcs->disable)
    196 				(*crtc_funcs->disable)(crtc);
    197 			else
    198 				(*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
    199 			crtc->primary->fb = NULL;
    200 		}
    201 	}
    202 }
    203 
    204 /**
    205  * drm_helper_disable_unused_functions - disable unused objects
    206  * @dev: DRM device
    207  *
    208  * This function walks through the entire mode setting configuration of @dev. It
    209  * will remove any CRTC links of unused encoders and encoder links of
    210  * disconnected connectors. Then it will disable all unused encoders and CRTCs
    211  * either by calling their disable callback if available or by calling their
    212  * dpms callback with DRM_MODE_DPMS_OFF.
    213  *
    214  * NOTE:
    215  *
    216  * This function is part of the legacy modeset helper library and will cause
    217  * major confusion with atomic drivers. This is because atomic helpers guarantee
    218  * to never call ->disable() hooks on a disabled function, or ->enable() hooks
    219  * on an enabled functions. drm_helper_disable_unused_functions() on the other
    220  * hand throws such guarantees into the wind and calls disable hooks
    221  * unconditionally on unused functions.
    222  */
    223 void drm_helper_disable_unused_functions(struct drm_device *dev)
    224 {
    225 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    226 
    227 	drm_modeset_lock_all(dev);
    228 	__drm_helper_disable_unused_functions(dev);
    229 	drm_modeset_unlock_all(dev);
    230 }
    231 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
    232 
    233 /*
    234  * Check the CRTC we're going to map each output to vs. its current
    235  * CRTC.  If they don't match, we have to disable the output and the CRTC
    236  * since the driver will have to re-route things.
    237  */
    238 static void
    239 drm_crtc_prepare_encoders(struct drm_device *dev)
    240 {
    241 	const struct drm_encoder_helper_funcs *encoder_funcs;
    242 	struct drm_encoder *encoder;
    243 
    244 	drm_for_each_encoder(encoder, dev) {
    245 		encoder_funcs = encoder->helper_private;
    246 		if (!encoder_funcs)
    247 			continue;
    248 
    249 		/* Disable unused encoders */
    250 		if (encoder->crtc == NULL)
    251 			drm_encoder_disable(encoder);
    252 		/* Disable encoders whose CRTC is about to change */
    253 		if (encoder_funcs->get_crtc &&
    254 		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
    255 			drm_encoder_disable(encoder);
    256 	}
    257 }
    258 
    259 /**
    260  * drm_crtc_helper_set_mode - internal helper to set a mode
    261  * @crtc: CRTC to program
    262  * @mode: mode to use
    263  * @x: horizontal offset into the surface
    264  * @y: vertical offset into the surface
    265  * @old_fb: old framebuffer, for cleanup
    266  *
    267  * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
    268  * to fixup or reject the mode prior to trying to set it. This is an internal
    269  * helper that drivers could e.g. use to update properties that require the
    270  * entire output pipe to be disabled and re-enabled in a new configuration. For
    271  * example for changing whether audio is enabled on a hdmi link or for changing
    272  * panel fitter or dither attributes. It is also called by the
    273  * drm_crtc_helper_set_config() helper function to drive the mode setting
    274  * sequence.
    275  *
    276  * Returns:
    277  * True if the mode was set successfully, false otherwise.
    278  */
    279 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
    280 			      struct drm_display_mode *mode,
    281 			      int x, int y,
    282 			      struct drm_framebuffer *old_fb)
    283 {
    284 	struct drm_device *dev = crtc->dev;
    285 	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
    286 	const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
    287 	const struct drm_encoder_helper_funcs *encoder_funcs;
    288 	int saved_x, saved_y;
    289 	bool saved_enabled;
    290 	struct drm_encoder *encoder;
    291 	bool ret = true;
    292 
    293 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    294 
    295 	drm_warn_on_modeset_not_all_locked(dev);
    296 
    297 	saved_enabled = crtc->enabled;
    298 	crtc->enabled = drm_helper_crtc_in_use(crtc);
    299 	if (!crtc->enabled)
    300 		return true;
    301 
    302 	adjusted_mode = drm_mode_duplicate(dev, mode);
    303 	if (!adjusted_mode) {
    304 		crtc->enabled = saved_enabled;
    305 		return false;
    306 	}
    307 
    308 	saved_mode = crtc->mode;
    309 	saved_hwmode = crtc->hwmode;
    310 	saved_x = crtc->x;
    311 	saved_y = crtc->y;
    312 
    313 	/* Update crtc values up front so the driver can rely on them for mode
    314 	 * setting.
    315 	 */
    316 	crtc->mode = *mode;
    317 	crtc->x = x;
    318 	crtc->y = y;
    319 
    320 	/* Pass our mode to the connectors and the CRTC to give them a chance to
    321 	 * adjust it according to limitations or connector properties, and also
    322 	 * a chance to reject the mode entirely.
    323 	 */
    324 	drm_for_each_encoder(encoder, dev) {
    325 
    326 		if (encoder->crtc != crtc)
    327 			continue;
    328 
    329 		encoder_funcs = encoder->helper_private;
    330 		if (!encoder_funcs)
    331 			continue;
    332 
    333 		encoder_funcs = encoder->helper_private;
    334 		if (encoder_funcs->mode_fixup) {
    335 			if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
    336 							      adjusted_mode))) {
    337 				DRM_DEBUG_KMS("Encoder fixup failed\n");
    338 				goto done;
    339 			}
    340 		}
    341 	}
    342 
    343 	if (crtc_funcs->mode_fixup) {
    344 		if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
    345 						adjusted_mode))) {
    346 			DRM_DEBUG_KMS("CRTC fixup failed\n");
    347 			goto done;
    348 		}
    349 	}
    350 	DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
    351 
    352 	crtc->hwmode = *adjusted_mode;
    353 
    354 	/* Prepare the encoders and CRTCs before setting the mode. */
    355 	drm_for_each_encoder(encoder, dev) {
    356 
    357 		if (encoder->crtc != crtc)
    358 			continue;
    359 
    360 		encoder_funcs = encoder->helper_private;
    361 		if (!encoder_funcs)
    362 			continue;
    363 
    364 		/* Disable the encoders as the first thing we do. */
    365 		if (encoder_funcs->prepare)
    366 			encoder_funcs->prepare(encoder);
    367 	}
    368 
    369 	drm_crtc_prepare_encoders(dev);
    370 
    371 	crtc_funcs->prepare(crtc);
    372 
    373 	/* Set up the DPLL and any encoders state that needs to adjust or depend
    374 	 * on the DPLL.
    375 	 */
    376 	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
    377 	if (!ret)
    378 	    goto done;
    379 
    380 	drm_for_each_encoder(encoder, dev) {
    381 
    382 		if (encoder->crtc != crtc)
    383 			continue;
    384 
    385 		encoder_funcs = encoder->helper_private;
    386 		if (!encoder_funcs)
    387 			continue;
    388 
    389 		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
    390 			encoder->base.id, encoder->name, mode->name);
    391 		if (encoder_funcs->mode_set)
    392 			encoder_funcs->mode_set(encoder, mode, adjusted_mode);
    393 	}
    394 
    395 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
    396 	crtc_funcs->commit(crtc);
    397 
    398 	drm_for_each_encoder(encoder, dev) {
    399 
    400 		if (encoder->crtc != crtc)
    401 			continue;
    402 
    403 		encoder_funcs = encoder->helper_private;
    404 		if (!encoder_funcs)
    405 			continue;
    406 
    407 		if (encoder_funcs->commit)
    408 			encoder_funcs->commit(encoder);
    409 	}
    410 
    411 	/* Calculate and store various constants which
    412 	 * are later needed by vblank and swap-completion
    413 	 * timestamping. They are derived from true hwmode.
    414 	 */
    415 	drm_calc_timestamping_constants(crtc, &crtc->hwmode);
    416 
    417 	/* FIXME: add subpixel order */
    418 done:
    419 	drm_mode_destroy(dev, adjusted_mode);
    420 	if (!ret) {
    421 		crtc->enabled = saved_enabled;
    422 		crtc->mode = saved_mode;
    423 		crtc->hwmode = saved_hwmode;
    424 		crtc->x = saved_x;
    425 		crtc->y = saved_y;
    426 	}
    427 
    428 	return ret;
    429 }
    430 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
    431 
    432 static void
    433 drm_crtc_helper_disable(struct drm_crtc *crtc)
    434 {
    435 	struct drm_device *dev = crtc->dev;
    436 	struct drm_connector *connector;
    437 	struct drm_encoder *encoder;
    438 
    439 	/* Decouple all encoders and their attached connectors from this crtc */
    440 	drm_for_each_encoder(encoder, dev) {
    441 		struct drm_connector_list_iter conn_iter;
    442 
    443 		if (encoder->crtc != crtc)
    444 			continue;
    445 
    446 		drm_connector_list_iter_begin(dev, &conn_iter);
    447 		drm_for_each_connector_iter(connector, &conn_iter) {
    448 			if (connector->encoder != encoder)
    449 				continue;
    450 
    451 			connector->encoder = NULL;
    452 
    453 			/*
    454 			 * drm_helper_disable_unused_functions() ought to be
    455 			 * doing this, but since we've decoupled the encoder
    456 			 * from the connector above, the required connection
    457 			 * between them is henceforth no longer available.
    458 			 */
    459 			connector->dpms = DRM_MODE_DPMS_OFF;
    460 
    461 			/* we keep a reference while the encoder is bound */
    462 			drm_connector_put(connector);
    463 		}
    464 		drm_connector_list_iter_end(&conn_iter);
    465 	}
    466 
    467 	__drm_helper_disable_unused_functions(dev);
    468 }
    469 
    470 /*
    471  * For connectors that support multiple encoders, either the
    472  * .atomic_best_encoder() or .best_encoder() operation must be implemented.
    473  */
    474 struct drm_encoder *
    475 drm_connector_get_single_encoder(struct drm_connector *connector)
    476 {
    477 	struct drm_encoder *encoder;
    478 
    479 	WARN_ON(hweight32(connector->possible_encoders) > 1);
    480 	drm_connector_for_each_possible_encoder(connector, encoder)
    481 		return encoder;
    482 
    483 	return NULL;
    484 }
    485 
    486 /**
    487  * drm_crtc_helper_set_config - set a new config from userspace
    488  * @set: mode set configuration
    489  * @ctx: lock acquire context, not used here
    490  *
    491  * The drm_crtc_helper_set_config() helper function implements the of
    492  * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
    493  * helpers.
    494  *
    495  * It first tries to locate the best encoder for each connector by calling the
    496  * connector @drm_connector_helper_funcs.best_encoder helper operation.
    497  *
    498  * After locating the appropriate encoders, the helper function will call the
    499  * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
    500  * or reject it completely in which case an error will be returned to the
    501  * application. If the new configuration after mode adjustment is identical to
    502  * the current configuration the helper function will return without performing
    503  * any other operation.
    504  *
    505  * If the adjusted mode is identical to the current mode but changes to the
    506  * frame buffer need to be applied, the drm_crtc_helper_set_config() function
    507  * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
    508  *
    509  * If the adjusted mode differs from the current mode, or if the
    510  * ->mode_set_base() helper operation is not provided, the helper function
    511  * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
    512  * and ->commit() CRTC and encoder helper operations, in that order.
    513  * Alternatively it can also use the dpms and disable helper operations. For
    514  * details see &struct drm_crtc_helper_funcs and struct
    515  * &drm_encoder_helper_funcs.
    516  *
    517  * This function is deprecated.  New drivers must implement atomic modeset
    518  * support, for which this function is unsuitable. Instead drivers should use
    519  * drm_atomic_helper_set_config().
    520  *
    521  * Returns:
    522  * Returns 0 on success, negative errno numbers on failure.
    523  */
    524 int drm_crtc_helper_set_config(struct drm_mode_set *set,
    525 			       struct drm_modeset_acquire_ctx *ctx)
    526 {
    527 	struct drm_device *dev;
    528 	struct drm_crtc **save_encoder_crtcs, *new_crtc;
    529 	struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
    530 	bool mode_changed = false; /* if true do a full mode set */
    531 	bool fb_changed = false; /* if true and !mode_changed just do a flip */
    532 	struct drm_connector *connector;
    533 	struct drm_connector_list_iter conn_iter;
    534 	int count = 0, ro, fail = 0;
    535 	const struct drm_crtc_helper_funcs *crtc_funcs;
    536 	struct drm_mode_set save_set;
    537 	int ret;
    538 	int i;
    539 
    540 	DRM_DEBUG_KMS("\n");
    541 
    542 	BUG_ON(!set);
    543 	BUG_ON(!set->crtc);
    544 	BUG_ON(!set->crtc->helper_private);
    545 
    546 	/* Enforce sane interface api - has been abused by the fb helper. */
    547 	BUG_ON(!set->mode && set->fb);
    548 	BUG_ON(set->fb && set->num_connectors == 0);
    549 
    550 	crtc_funcs = set->crtc->helper_private;
    551 
    552 	dev = set->crtc->dev;
    553 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    554 
    555 	if (!set->mode)
    556 		set->fb = NULL;
    557 
    558 	if (set->fb) {
    559 		DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
    560 			      set->crtc->base.id, set->crtc->name,
    561 			      set->fb->base.id,
    562 			      (int)set->num_connectors, set->x, set->y);
    563 	} else {
    564 		DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
    565 			      set->crtc->base.id, set->crtc->name);
    566 		drm_crtc_helper_disable(set->crtc);
    567 		return 0;
    568 	}
    569 
    570 	drm_warn_on_modeset_not_all_locked(dev);
    571 
    572 	/*
    573 	 * Allocate space for the backup of all (non-pointer) encoder and
    574 	 * connector data.
    575 	 */
    576 	save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
    577 				sizeof(struct drm_crtc *), GFP_KERNEL);
    578 	if (!save_encoder_crtcs)
    579 		return -ENOMEM;
    580 
    581 	save_connector_encoders = kcalloc(dev->mode_config.num_connector,
    582 				sizeof(struct drm_encoder *), GFP_KERNEL);
    583 	if (!save_connector_encoders) {
    584 		kfree(save_encoder_crtcs);
    585 		return -ENOMEM;
    586 	}
    587 
    588 	/*
    589 	 * Copy data. Note that driver private data is not affected.
    590 	 * Should anything bad happen only the expected state is
    591 	 * restored, not the drivers personal bookkeeping.
    592 	 */
    593 	count = 0;
    594 	drm_for_each_encoder(encoder, dev) {
    595 		save_encoder_crtcs[count++] = encoder->crtc;
    596 	}
    597 
    598 	count = 0;
    599 	drm_connector_list_iter_begin(dev, &conn_iter);
    600 	drm_for_each_connector_iter(connector, &conn_iter)
    601 		save_connector_encoders[count++] = connector->encoder;
    602 	drm_connector_list_iter_end(&conn_iter);
    603 
    604 	save_set.crtc = set->crtc;
    605 	save_set.mode = &set->crtc->mode;
    606 	save_set.x = set->crtc->x;
    607 	save_set.y = set->crtc->y;
    608 	save_set.fb = set->crtc->primary->fb;
    609 
    610 	/* We should be able to check here if the fb has the same properties
    611 	 * and then just flip_or_move it */
    612 	if (set->crtc->primary->fb != set->fb) {
    613 		/* If we have no fb then treat it as a full mode set */
    614 		if (set->crtc->primary->fb == NULL) {
    615 			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
    616 			mode_changed = true;
    617 		} else if (set->fb->format != set->crtc->primary->fb->format) {
    618 			mode_changed = true;
    619 		} else
    620 			fb_changed = true;
    621 	}
    622 
    623 	if (set->x != set->crtc->x || set->y != set->crtc->y)
    624 		fb_changed = true;
    625 
    626 	if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
    627 		DRM_DEBUG_KMS("modes are different, full mode set\n");
    628 		drm_mode_debug_printmodeline(&set->crtc->mode);
    629 		drm_mode_debug_printmodeline(set->mode);
    630 		mode_changed = true;
    631 	}
    632 
    633 	/* take a reference on all unbound connectors in set, reuse the
    634 	 * already taken reference for bound connectors
    635 	 */
    636 	for (ro = 0; ro < set->num_connectors; ro++) {
    637 		if (set->connectors[ro]->encoder)
    638 			continue;
    639 		drm_connector_get(set->connectors[ro]);
    640 	}
    641 
    642 	/* a) traverse passed in connector list and get encoders for them */
    643 	count = 0;
    644 	drm_connector_list_iter_begin(dev, &conn_iter);
    645 	drm_for_each_connector_iter(connector, &conn_iter) {
    646 		const struct drm_connector_helper_funcs *connector_funcs =
    647 			connector->helper_private;
    648 		new_encoder = connector->encoder;
    649 		for (ro = 0; ro < set->num_connectors; ro++) {
    650 			if (set->connectors[ro] == connector) {
    651 				if (connector_funcs->best_encoder)
    652 					new_encoder = connector_funcs->best_encoder(connector);
    653 				else
    654 					new_encoder = drm_connector_get_single_encoder(connector);
    655 
    656 				/* if we can't get an encoder for a connector
    657 				   we are setting now - then fail */
    658 				if (new_encoder == NULL)
    659 					/* don't break so fail path works correct */
    660 					fail = 1;
    661 
    662 				if (connector->dpms != DRM_MODE_DPMS_ON) {
    663 					DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
    664 					mode_changed = true;
    665 				}
    666 
    667 				break;
    668 			}
    669 		}
    670 
    671 		if (new_encoder != connector->encoder) {
    672 			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
    673 			mode_changed = true;
    674 			/* If the encoder is reused for another connector, then
    675 			 * the appropriate crtc will be set later.
    676 			 */
    677 			if (connector->encoder)
    678 				connector->encoder->crtc = NULL;
    679 			connector->encoder = new_encoder;
    680 		}
    681 	}
    682 	drm_connector_list_iter_end(&conn_iter);
    683 
    684 	if (fail) {
    685 		ret = -EINVAL;
    686 		goto fail;
    687 	}
    688 
    689 	count = 0;
    690 	drm_connector_list_iter_begin(dev, &conn_iter);
    691 	drm_for_each_connector_iter(connector, &conn_iter) {
    692 		if (!connector->encoder)
    693 			continue;
    694 
    695 		if (connector->encoder->crtc == set->crtc)
    696 			new_crtc = NULL;
    697 		else
    698 			new_crtc = connector->encoder->crtc;
    699 
    700 		for (ro = 0; ro < set->num_connectors; ro++) {
    701 			if (set->connectors[ro] == connector)
    702 				new_crtc = set->crtc;
    703 		}
    704 
    705 		/* Make sure the new CRTC will work with the encoder */
    706 		if (new_crtc &&
    707 		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
    708 			ret = -EINVAL;
    709 			drm_connector_list_iter_end(&conn_iter);
    710 			goto fail;
    711 		}
    712 		if (new_crtc != connector->encoder->crtc) {
    713 			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
    714 			mode_changed = true;
    715 			connector->encoder->crtc = new_crtc;
    716 		}
    717 		if (new_crtc) {
    718 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
    719 				      connector->base.id, connector->name,
    720 				      new_crtc->base.id, new_crtc->name);
    721 		} else {
    722 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
    723 				      connector->base.id, connector->name);
    724 		}
    725 	}
    726 	drm_connector_list_iter_end(&conn_iter);
    727 
    728 	/* mode_set_base is not a required function */
    729 	if (fb_changed && !crtc_funcs->mode_set_base)
    730 		mode_changed = true;
    731 
    732 	if (mode_changed) {
    733 		if (drm_helper_crtc_in_use(set->crtc)) {
    734 			DRM_DEBUG_KMS("attempting to set mode from"
    735 					" userspace\n");
    736 			drm_mode_debug_printmodeline(set->mode);
    737 			set->crtc->primary->fb = set->fb;
    738 			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
    739 						      set->x, set->y,
    740 						      save_set.fb)) {
    741 				DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
    742 					  set->crtc->base.id, set->crtc->name);
    743 				set->crtc->primary->fb = save_set.fb;
    744 				ret = -EINVAL;
    745 				goto fail;
    746 			}
    747 			DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
    748 			for (i = 0; i < set->num_connectors; i++) {
    749 				DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
    750 					      set->connectors[i]->name);
    751 				set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
    752 			}
    753 		}
    754 		__drm_helper_disable_unused_functions(dev);
    755 	} else if (fb_changed) {
    756 		set->crtc->x = set->x;
    757 		set->crtc->y = set->y;
    758 		set->crtc->primary->fb = set->fb;
    759 		ret = crtc_funcs->mode_set_base(set->crtc,
    760 						set->x, set->y, save_set.fb);
    761 		if (ret != 0) {
    762 			set->crtc->x = save_set.x;
    763 			set->crtc->y = save_set.y;
    764 			set->crtc->primary->fb = save_set.fb;
    765 			goto fail;
    766 		}
    767 	}
    768 
    769 	kfree(save_connector_encoders);
    770 	kfree(save_encoder_crtcs);
    771 	return 0;
    772 
    773 fail:
    774 	/* Restore all previous data. */
    775 	count = 0;
    776 	drm_for_each_encoder(encoder, dev) {
    777 		encoder->crtc = save_encoder_crtcs[count++];
    778 	}
    779 
    780 	count = 0;
    781 	drm_connector_list_iter_begin(dev, &conn_iter);
    782 	drm_for_each_connector_iter(connector, &conn_iter)
    783 		connector->encoder = save_connector_encoders[count++];
    784 	drm_connector_list_iter_end(&conn_iter);
    785 
    786 	/* after fail drop reference on all unbound connectors in set, let
    787 	 * bound connectors keep their reference
    788 	 */
    789 	for (ro = 0; ro < set->num_connectors; ro++) {
    790 		if (set->connectors[ro]->encoder)
    791 			continue;
    792 		drm_connector_put(set->connectors[ro]);
    793 	}
    794 
    795 	/* Try to restore the config */
    796 	if (mode_changed &&
    797 	    !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
    798 				      save_set.y, save_set.fb))
    799 		DRM_ERROR("failed to restore config after modeset failure\n");
    800 
    801 	kfree(save_connector_encoders);
    802 	kfree(save_encoder_crtcs);
    803 	return ret;
    804 }
    805 EXPORT_SYMBOL(drm_crtc_helper_set_config);
    806 
    807 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
    808 {
    809 	int dpms = DRM_MODE_DPMS_OFF;
    810 	struct drm_connector *connector;
    811 	struct drm_connector_list_iter conn_iter;
    812 	struct drm_device *dev = encoder->dev;
    813 
    814 	drm_connector_list_iter_begin(dev, &conn_iter);
    815 	drm_for_each_connector_iter(connector, &conn_iter)
    816 		if (connector->encoder == encoder)
    817 			if (connector->dpms < dpms)
    818 				dpms = connector->dpms;
    819 	drm_connector_list_iter_end(&conn_iter);
    820 
    821 	return dpms;
    822 }
    823 
    824 /* Helper which handles bridge ordering around encoder dpms */
    825 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
    826 {
    827 	const struct drm_encoder_helper_funcs *encoder_funcs;
    828 
    829 	encoder_funcs = encoder->helper_private;
    830 	if (!encoder_funcs)
    831 		return;
    832 
    833 	if (encoder_funcs->dpms)
    834 		encoder_funcs->dpms(encoder, mode);
    835 }
    836 
    837 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
    838 {
    839 	int dpms = DRM_MODE_DPMS_OFF;
    840 	struct drm_connector *connector;
    841 	struct drm_connector_list_iter conn_iter;
    842 	struct drm_device *dev = crtc->dev;
    843 
    844 	drm_connector_list_iter_begin(dev, &conn_iter);
    845 	drm_for_each_connector_iter(connector, &conn_iter)
    846 		if (connector->encoder && connector->encoder->crtc == crtc)
    847 			if (connector->dpms < dpms)
    848 				dpms = connector->dpms;
    849 	drm_connector_list_iter_end(&conn_iter);
    850 
    851 	return dpms;
    852 }
    853 
    854 /**
    855  * drm_helper_connector_dpms() - connector dpms helper implementation
    856  * @connector: affected connector
    857  * @mode: DPMS mode
    858  *
    859  * The drm_helper_connector_dpms() helper function implements the
    860  * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
    861  * helpers.
    862  *
    863  * This is the main helper function provided by the CRTC helper framework for
    864  * implementing the DPMS connector attribute. It computes the new desired DPMS
    865  * state for all encoders and CRTCs in the output mesh and calls the
    866  * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
    867  * provided by the driver.
    868  *
    869  * This function is deprecated.  New drivers must implement atomic modeset
    870  * support, where DPMS is handled in the DRM core.
    871  *
    872  * Returns:
    873  * Always returns 0.
    874  */
    875 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
    876 {
    877 	struct drm_encoder *encoder = connector->encoder;
    878 	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
    879 	int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
    880 
    881 	WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
    882 
    883 	if (mode == connector->dpms)
    884 		return 0;
    885 
    886 	old_dpms = connector->dpms;
    887 	connector->dpms = mode;
    888 
    889 	if (encoder)
    890 		encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
    891 
    892 	/* from off to on, do crtc then encoder */
    893 	if (mode < old_dpms) {
    894 		if (crtc) {
    895 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
    896 			if (crtc_funcs->dpms)
    897 				(*crtc_funcs->dpms) (crtc,
    898 						     drm_helper_choose_crtc_dpms(crtc));
    899 		}
    900 		if (encoder)
    901 			drm_helper_encoder_dpms(encoder, encoder_dpms);
    902 	}
    903 
    904 	/* from on to off, do encoder then crtc */
    905 	if (mode > old_dpms) {
    906 		if (encoder)
    907 			drm_helper_encoder_dpms(encoder, encoder_dpms);
    908 		if (crtc) {
    909 			const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
    910 			if (crtc_funcs->dpms)
    911 				(*crtc_funcs->dpms) (crtc,
    912 						     drm_helper_choose_crtc_dpms(crtc));
    913 		}
    914 	}
    915 
    916 	return 0;
    917 }
    918 EXPORT_SYMBOL(drm_helper_connector_dpms);
    919 
    920 /**
    921  * drm_helper_resume_force_mode - force-restore mode setting configuration
    922  * @dev: drm_device which should be restored
    923  *
    924  * Drivers which use the mode setting helpers can use this function to
    925  * force-restore the mode setting configuration e.g. on resume or when something
    926  * else might have trampled over the hw state (like some overzealous old BIOSen
    927  * tended to do).
    928  *
    929  * This helper doesn't provide a error return value since restoring the old
    930  * config should never fail due to resource allocation issues since the driver
    931  * has successfully set the restored configuration already. Hence this should
    932  * boil down to the equivalent of a few dpms on calls, which also don't provide
    933  * an error code.
    934  *
    935  * Drivers where simply restoring an old configuration again might fail (e.g.
    936  * due to slight differences in allocating shared resources when the
    937  * configuration is restored in a different order than when userspace set it up)
    938  * need to use their own restore logic.
    939  *
    940  * This function is deprecated. New drivers should implement atomic mode-
    941  * setting and use the atomic suspend/resume helpers.
    942  *
    943  * See also:
    944  * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
    945  */
    946 void drm_helper_resume_force_mode(struct drm_device *dev)
    947 {
    948 	struct drm_crtc *crtc;
    949 	struct drm_encoder *encoder;
    950 	const struct drm_crtc_helper_funcs *crtc_funcs;
    951 	int encoder_dpms;
    952 	bool ret;
    953 
    954 	WARN_ON(drm_drv_uses_atomic_modeset(dev));
    955 
    956 	drm_modeset_lock_all(dev);
    957 	drm_for_each_crtc(crtc, dev) {
    958 
    959 		if (!crtc->enabled)
    960 			continue;
    961 
    962 		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
    963 					       crtc->x, crtc->y, crtc->primary->fb);
    964 
    965 		/* Restoring the old config should never fail! */
    966 		if (ret == false)
    967 			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
    968 
    969 		/* Turn off outputs that were already powered off */
    970 		if (drm_helper_choose_crtc_dpms(crtc)) {
    971 			drm_for_each_encoder(encoder, dev) {
    972 
    973 				if(encoder->crtc != crtc)
    974 					continue;
    975 
    976 				encoder_dpms = drm_helper_choose_encoder_dpms(
    977 							encoder);
    978 
    979 				drm_helper_encoder_dpms(encoder, encoder_dpms);
    980 			}
    981 
    982 			crtc_funcs = crtc->helper_private;
    983 			if (crtc_funcs->dpms)
    984 				(*crtc_funcs->dpms) (crtc,
    985 						     drm_helper_choose_crtc_dpms(crtc));
    986 		}
    987 	}
    988 
    989 	/* disable the unused connectors while restoring the modesetting */
    990 	__drm_helper_disable_unused_functions(dev);
    991 	drm_modeset_unlock_all(dev);
    992 }
    993 EXPORT_SYMBOL(drm_helper_resume_force_mode);
    994 
    995 /**
    996  * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
    997  * @dev: DRM device whose CRTCs to turn off
    998  *
    999  * Drivers may want to call this on unload to ensure that all displays are
   1000  * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
   1001  *
   1002  * Note: This should only be used by non-atomic legacy drivers. For an atomic
   1003  * version look at drm_atomic_helper_shutdown().
   1004  *
   1005  * Returns:
   1006  * Zero on success, error code on failure.
   1007  */
   1008 int drm_helper_force_disable_all(struct drm_device *dev)
   1009 {
   1010 	struct drm_crtc *crtc;
   1011 	int ret = 0;
   1012 
   1013 	drm_modeset_lock_all(dev);
   1014 	drm_for_each_crtc(crtc, dev)
   1015 		if (crtc->enabled) {
   1016 			struct drm_mode_set set = {
   1017 				.crtc = crtc,
   1018 			};
   1019 
   1020 			ret = drm_mode_set_config_internal(&set);
   1021 			if (ret)
   1022 				goto out;
   1023 		}
   1024 out:
   1025 	drm_modeset_unlock_all(dev);
   1026 	return ret;
   1027 }
   1028 EXPORT_SYMBOL(drm_helper_force_disable_all);
   1029