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