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