blob: 747661f63fbb4f65d35a5f9d1521ff01c5e56ed9 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 *
5 * DRM core CRTC related functions
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting documentation, and
11 * that the name of the copyright holders not be used in advertising or
12 * publicity pertaining to distribution of the software without specific,
13 * written prior permission. The copyright holders make no representations
14 * about the suitability of this software for any purpose. It is provided "as
15 * is" without express or implied warranty.
16 *
17 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 *
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
30 */
31
Sergei Antonovba6f5822014-05-12 00:30:48 +020032#include <linux/kernel.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040033#include <linux/export.h>
Paul Gortmaker0603ba12011-08-31 11:29:09 -040034#include <linux/moduleparam.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040035
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
Daniel Vetter321ebf02014-11-04 22:57:27 +010037#include <drm/drm_atomic.h>
Daniel Vetter72fdb402018-09-05 15:57:11 +020038#include <drm/drm_atomic_uapi.h>
David Howells760285e2012-10-02 18:01:07 +010039#include <drm/drm_crtc.h>
Laurent Pinchart93382032016-11-28 20:51:09 +020040#include <drm/drm_encoder.h>
David Howells760285e2012-10-02 18:01:07 +010041#include <drm/drm_fourcc.h>
42#include <drm/drm_crtc_helper.h>
43#include <drm/drm_fb_helper.h>
Daniel Vetter2f324b42014-10-29 11:13:47 +010044#include <drm/drm_plane_helper.h>
Daniel Vetter321ebf02014-11-04 22:57:27 +010045#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010046#include <drm/drm_edid.h>
Dave Airlief453ba02008-11-07 14:05:41 -080047
Daniel Vetter3150c7d2014-11-06 20:53:29 +010048/**
49 * DOC: overview
50 *
51 * The CRTC modeset helper library provides a default set_config implementation
52 * in drm_crtc_helper_set_config(). Plus a few other convenience functions using
53 * the same callbacks which drivers can use to e.g. restore the modeset
54 * configuration on resume with drm_helper_resume_force_mode().
55 *
Daniel Vetter2be94972015-12-04 09:45:46 +010056 * Note that this helper library doesn't track the current power state of CRTCs
Daniel Vetter6806cdf2017-01-25 07:26:43 +010057 * and encoders. It can call callbacks like &drm_encoder_helper_funcs.dpms even
58 * though the hardware is already in the desired state. This deficiency has been
59 * fixed in the atomic helpers.
Daniel Vetter2be94972015-12-04 09:45:46 +010060 *
Daniel Vetter3150c7d2014-11-06 20:53:29 +010061 * The driver callbacks are mostly compatible with the atomic modeset helpers,
62 * except for the handling of the primary plane: Atomic helpers require that the
63 * primary plane is implemented as a real standalone plane and not directly tied
64 * to the CRTC state. For easier transition this library provides functions to
65 * implement the old semantics required by the CRTC helpers using the new plane
66 * and atomic helper callbacks.
67 *
68 * Drivers are strongly urged to convert to the atomic helpers (by way of first
69 * converting to the plane helpers). New drivers must not use these functions
70 * but need to implement the atomic interface instead, potentially using the
71 * atomic helpers for that.
Daniel Vetter092d01d2015-12-04 09:45:44 +010072 *
73 * These legacy modeset helpers use the same function table structures as
74 * all other modesetting helpers. See the documentation for struct
Daniel Vetterea0dd852016-12-29 21:48:26 +010075 * &drm_crtc_helper_funcs, &struct drm_encoder_helper_funcs and struct
Daniel Vetter092d01d2015-12-04 09:45:44 +010076 * &drm_connector_helper_funcs.
Daniel Vetter3150c7d2014-11-06 20:53:29 +010077 */
Daniel Vetter92b6f892013-10-08 17:44:47 +020078
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +010079/**
Keith Packardc9fb15f2009-05-30 20:42:28 -070080 * drm_helper_encoder_in_use - check if a given encoder is in use
81 * @encoder: encoder to check
82 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +010083 * Checks whether @encoder is with the current mode setting output configuration
84 * in use by any connector. This doesn't mean that it is actually enabled since
85 * the DPMS state is tracked separately.
Keith Packardc9fb15f2009-05-30 20:42:28 -070086 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +010087 * Returns:
88 * True if @encoder is used, false otherwise.
Keith Packardc9fb15f2009-05-30 20:42:28 -070089 */
90bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
91{
92 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +010093 struct drm_connector_list_iter conn_iter;
Keith Packardc9fb15f2009-05-30 20:42:28 -070094 struct drm_device *dev = encoder->dev;
Daniel Vetter62ff94a2014-01-23 22:18:47 +010095
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +010096 WARN_ON(drm_drv_uses_atomic_modeset(dev));
97
Sergei Antonovba6f5822014-05-12 00:30:48 +020098 /*
99 * We can expect this mutex to be locked if we are not panicking.
100 * Locking is currently fubar in the panic handler.
101 */
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000102 if (!oops_in_progress) {
Sergei Antonovba6f5822014-05-12 00:30:48 +0200103 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000104 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
105 }
Sergei Antonovba6f5822014-05-12 00:30:48 +0200106
Daniel Vetterc36a3252016-12-15 16:58:43 +0100107
Thierry Redingb982dab2017-02-28 15:46:43 +0100108 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100109 drm_for_each_connector_iter(connector, &conn_iter) {
110 if (connector->encoder == encoder) {
Thierry Redingb982dab2017-02-28 15:46:43 +0100111 drm_connector_list_iter_end(&conn_iter);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700112 return true;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100113 }
114 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100115 drm_connector_list_iter_end(&conn_iter);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700116 return false;
117}
118EXPORT_SYMBOL(drm_helper_encoder_in_use);
119
120/**
Dave Airlief453ba02008-11-07 14:05:41 -0800121 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
122 * @crtc: CRTC to check
123 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100124 * Checks whether @crtc is with the current mode setting output configuration
125 * in use by any connector. This doesn't mean that it is actually enabled since
126 * the DPMS state is tracked separately.
Dave Airlief453ba02008-11-07 14:05:41 -0800127 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100128 * Returns:
129 * True if @crtc is used, false otherwise.
Dave Airlief453ba02008-11-07 14:05:41 -0800130 */
131bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
132{
133 struct drm_encoder *encoder;
134 struct drm_device *dev = crtc->dev;
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100135
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100136 WARN_ON(drm_drv_uses_atomic_modeset(dev));
137
Sergei Antonovba6f5822014-05-12 00:30:48 +0200138 /*
139 * We can expect this mutex to be locked if we are not panicking.
140 * Locking is currently fubar in the panic handler.
141 */
142 if (!oops_in_progress)
143 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
144
Daniel Vettere4f62542015-07-09 23:44:35 +0200145 drm_for_each_encoder(encoder, dev)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700146 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
Dave Airlief453ba02008-11-07 14:05:41 -0800147 return true;
148 return false;
149}
150EXPORT_SYMBOL(drm_helper_crtc_in_use);
151
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000152static void
153drm_encoder_disable(struct drm_encoder *encoder)
154{
Jani Nikulabe26a662015-03-11 11:51:06 +0200155 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000156
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200157 if (!encoder_funcs)
158 return;
159
Archit Taneja862e6862015-05-21 11:03:16 +0530160 drm_bridge_disable(encoder->bridge);
Sean Paul3b336ec2013-08-14 16:47:37 -0400161
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000162 if (encoder_funcs->disable)
163 (*encoder_funcs->disable)(encoder);
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200164 else if (encoder_funcs->dpms)
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000165 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
Sean Paul3b336ec2013-08-14 16:47:37 -0400166
Archit Taneja862e6862015-05-21 11:03:16 +0530167 drm_bridge_post_disable(encoder->bridge);
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000168}
169
Daniel Vetterb182cc52014-03-20 14:26:34 +0100170static void __drm_helper_disable_unused_functions(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800171{
172 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -0800173 struct drm_crtc *crtc;
174
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100175 drm_warn_on_modeset_not_all_locked(dev);
176
Daniel Vetter6295d602015-07-09 23:44:25 +0200177 drm_for_each_encoder(encoder, dev) {
Dave Airlie929710212010-12-21 12:47:56 +1000178 if (!drm_helper_encoder_in_use(encoder)) {
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000179 drm_encoder_disable(encoder);
Rob Clarkb5e6c1d2014-05-24 14:30:10 -0400180 /* disconnect encoder from any connector */
Dave Airlie9c552dd2009-09-02 14:00:11 +1000181 encoder->crtc = NULL;
Dave Airliea3a05442009-08-31 15:16:30 +1000182 }
Dave Airlief453ba02008-11-07 14:05:41 -0800183 }
184
Daniel Vetter6295d602015-07-09 23:44:25 +0200185 drm_for_each_crtc(crtc, dev) {
Jani Nikulabe26a662015-03-11 11:51:06 +0200186 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800187 crtc->enabled = drm_helper_crtc_in_use(crtc);
188 if (!crtc->enabled) {
Alex Deucher5c8d7172010-06-11 17:04:35 -0400189 if (crtc_funcs->disable)
190 (*crtc_funcs->disable)(crtc);
191 else
192 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
Matt Roperf4510a22014-04-01 15:22:40 -0700193 crtc->primary->fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800194 }
195 }
196}
Daniel Vetterb182cc52014-03-20 14:26:34 +0100197
198/**
199 * drm_helper_disable_unused_functions - disable unused objects
200 * @dev: DRM device
201 *
202 * This function walks through the entire mode setting configuration of @dev. It
Daniel Vetter2be94972015-12-04 09:45:46 +0100203 * will remove any CRTC links of unused encoders and encoder links of
204 * disconnected connectors. Then it will disable all unused encoders and CRTCs
Daniel Vetterb182cc52014-03-20 14:26:34 +0100205 * either by calling their disable callback if available or by calling their
206 * dpms callback with DRM_MODE_DPMS_OFF.
Daniel Vetter09859d22016-01-13 15:31:16 +0100207 *
208 * NOTE:
209 *
210 * This function is part of the legacy modeset helper library and will cause
211 * major confusion with atomic drivers. This is because atomic helpers guarantee
212 * to never call ->disable() hooks on a disabled function, or ->enable() hooks
213 * on an enabled functions. drm_helper_disable_unused_functions() on the other
214 * hand throws such guarantees into the wind and calls disable hooks
215 * unconditionally on unused functions.
Daniel Vetterb182cc52014-03-20 14:26:34 +0100216 */
217void drm_helper_disable_unused_functions(struct drm_device *dev)
218{
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100219 WARN_ON(drm_drv_uses_atomic_modeset(dev));
Daniel Vetter6605ca02016-06-08 14:19:18 +0200220
Daniel Vetterb182cc52014-03-20 14:26:34 +0100221 drm_modeset_lock_all(dev);
222 __drm_helper_disable_unused_functions(dev);
223 drm_modeset_unlock_all(dev);
224}
Dave Airlief453ba02008-11-07 14:05:41 -0800225EXPORT_SYMBOL(drm_helper_disable_unused_functions);
226
Jesse Barnes7bec7562009-02-23 16:09:34 -0800227/*
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 */
232static void
233drm_crtc_prepare_encoders(struct drm_device *dev)
234{
Jani Nikulabe26a662015-03-11 11:51:06 +0200235 const struct drm_encoder_helper_funcs *encoder_funcs;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800236 struct drm_encoder *encoder;
237
Daniel Vetter6295d602015-07-09 23:44:25 +0200238 drm_for_each_encoder(encoder, dev) {
Jesse Barnes7bec7562009-02-23 16:09:34 -0800239 encoder_funcs = encoder->helper_private;
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200240 if (!encoder_funcs)
241 continue;
242
Jesse Barnes7bec7562009-02-23 16:09:34 -0800243 /* Disable unused encoders */
244 if (encoder->crtc == NULL)
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000245 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800246 /* Disable encoders whose CRTC is about to change */
247 if (encoder_funcs->get_crtc &&
248 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
Ben Skeggs86a1b9d12010-07-01 16:49:57 +1000249 drm_encoder_disable(encoder);
Jesse Barnes7bec7562009-02-23 16:09:34 -0800250 }
251}
252
Dave Airlief453ba02008-11-07 14:05:41 -0800253/**
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100254 * drm_crtc_helper_set_mode - internal helper to set a mode
Dave Airlief453ba02008-11-07 14:05:41 -0800255 * @crtc: CRTC to program
256 * @mode: mode to use
Alex Deucher4c9287c2012-11-09 17:26:32 +0000257 * @x: horizontal offset into the surface
258 * @y: vertical offset into the surface
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100259 * @old_fb: old framebuffer, for cleanup
Dave Airlief453ba02008-11-07 14:05:41 -0800260 *
Dave Airlief453ba02008-11-07 14:05:41 -0800261 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100262 * to fixup or reject the mode prior to trying to set it. This is an internal
263 * helper that drivers could e.g. use to update properties that require the
264 * entire output pipe to be disabled and re-enabled in a new configuration. For
265 * example for changing whether audio is enabled on a hdmi link or for changing
266 * panel fitter or dither attributes. It is also called by the
267 * drm_crtc_helper_set_config() helper function to drive the mode setting
268 * sequence.
Dave Airlief453ba02008-11-07 14:05:41 -0800269 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100270 * Returns:
271 * True if the mode was set successfully, false otherwise.
Dave Airlief453ba02008-11-07 14:05:41 -0800272 */
273bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
274 struct drm_display_mode *mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500275 int x, int y,
276 struct drm_framebuffer *old_fb)
Dave Airlief453ba02008-11-07 14:05:41 -0800277{
278 struct drm_device *dev = crtc->dev;
Daniel Stone5a275282015-03-19 04:33:03 +0000279 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
Jani Nikulabe26a662015-03-11 11:51:06 +0200280 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
281 const struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800282 int saved_x, saved_y;
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400283 bool saved_enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800284 struct drm_encoder *encoder;
285 bool ret = true;
286
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100287 WARN_ON(drm_drv_uses_atomic_modeset(dev));
288
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100289 drm_warn_on_modeset_not_all_locked(dev);
290
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400291 saved_enabled = crtc->enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800292 crtc->enabled = drm_helper_crtc_in_use(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -0800293 if (!crtc->enabled)
294 return true;
295
Chris Wilson021a8452011-01-28 11:31:56 +0000296 adjusted_mode = drm_mode_duplicate(dev, mode);
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400297 if (!adjusted_mode) {
298 crtc->enabled = saved_enabled;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200299 return false;
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400300 }
Chris Wilson021a8452011-01-28 11:31:56 +0000301
Dave Airlief453ba02008-11-07 14:05:41 -0800302 saved_mode = crtc->mode;
Daniel Stone5a275282015-03-19 04:33:03 +0000303 saved_hwmode = crtc->hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800304 saved_x = crtc->x;
305 saved_y = crtc->y;
306
307 /* Update crtc values up front so the driver can rely on them for mode
308 * setting.
309 */
310 crtc->mode = *mode;
311 crtc->x = x;
312 crtc->y = y;
313
Dave Airlief453ba02008-11-07 14:05:41 -0800314 /* Pass our mode to the connectors and the CRTC to give them a chance to
315 * adjust it according to limitations or connector properties, and also
316 * a chance to reject the mode entirely.
317 */
Daniel Vetter6295d602015-07-09 23:44:25 +0200318 drm_for_each_encoder(encoder, dev) {
Dave Airlief453ba02008-11-07 14:05:41 -0800319
320 if (encoder->crtc != crtc)
321 continue;
Sean Paul3b336ec2013-08-14 16:47:37 -0400322
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200323 encoder_funcs = encoder->helper_private;
324 if (!encoder_funcs)
325 continue;
326
Archit Taneja862e6862015-05-21 11:03:16 +0530327 ret = drm_bridge_mode_fixup(encoder->bridge,
328 mode, adjusted_mode);
329 if (!ret) {
330 DRM_DEBUG_KMS("Bridge fixup failed\n");
331 goto done;
Sean Paul3b336ec2013-08-14 16:47:37 -0400332 }
333
Dave Airlief453ba02008-11-07 14:05:41 -0800334 encoder_funcs = encoder->helper_private;
Carlos Palminha3c5b2672016-02-10 12:15:22 +0000335 if (encoder_funcs->mode_fixup) {
336 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
337 adjusted_mode))) {
338 DRM_DEBUG_KMS("Encoder fixup failed\n");
339 goto done;
340 }
Dave Airlief453ba02008-11-07 14:05:41 -0800341 }
342 }
343
Carlos Palminha49f718c2016-02-16 14:10:03 +0000344 if (crtc_funcs->mode_fixup) {
345 if (!(ret = crtc_funcs->mode_fixup(crtc, mode,
346 adjusted_mode))) {
347 DRM_DEBUG_KMS("CRTC fixup failed\n");
348 goto done;
349 }
Dave Airlief453ba02008-11-07 14:05:41 -0800350 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200351 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -0800352
Daniel Stone5a275282015-03-19 04:33:03 +0000353 crtc->hwmode = *adjusted_mode;
354
Dave Airlief453ba02008-11-07 14:05:41 -0800355 /* Prepare the encoders and CRTCs before setting the mode. */
Daniel Vetter6295d602015-07-09 23:44:25 +0200356 drm_for_each_encoder(encoder, dev) {
Dave Airlief453ba02008-11-07 14:05:41 -0800357
358 if (encoder->crtc != crtc)
359 continue;
Sean Paul3b336ec2013-08-14 16:47:37 -0400360
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200361 encoder_funcs = encoder->helper_private;
362 if (!encoder_funcs)
363 continue;
364
Archit Taneja862e6862015-05-21 11:03:16 +0530365 drm_bridge_disable(encoder->bridge);
Sean Paul3b336ec2013-08-14 16:47:37 -0400366
Dave Airlief453ba02008-11-07 14:05:41 -0800367 /* Disable the encoders as the first thing we do. */
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200368 if (encoder_funcs->prepare)
369 encoder_funcs->prepare(encoder);
Sean Paul3b336ec2013-08-14 16:47:37 -0400370
Archit Taneja862e6862015-05-21 11:03:16 +0530371 drm_bridge_post_disable(encoder->bridge);
Dave Airlief453ba02008-11-07 14:05:41 -0800372 }
373
Jesse Barnes7bec7562009-02-23 16:09:34 -0800374 drm_crtc_prepare_encoders(dev);
375
Dave Airlief453ba02008-11-07 14:05:41 -0800376 crtc_funcs->prepare(crtc);
377
378 /* Set up the DPLL and any encoders state that needs to adjust or depend
379 * on the DPLL.
380 */
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000381 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
382 if (!ret)
383 goto done;
Dave Airlief453ba02008-11-07 14:05:41 -0800384
Daniel Vetter6295d602015-07-09 23:44:25 +0200385 drm_for_each_encoder(encoder, dev) {
Dave Airlief453ba02008-11-07 14:05:41 -0800386
387 if (encoder->crtc != crtc)
388 continue;
389
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200390 encoder_funcs = encoder->helper_private;
391 if (!encoder_funcs)
392 continue;
393
Shayenne Moura0e691bc2019-01-11 12:45:48 -0200394 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%s]\n",
395 encoder->base.id, encoder->name, mode->name);
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200396 if (encoder_funcs->mode_set)
397 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
Sean Paul3b336ec2013-08-14 16:47:37 -0400398
Archit Taneja862e6862015-05-21 11:03:16 +0530399 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800400 }
401
402 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
403 crtc_funcs->commit(crtc);
404
Daniel Vetter6295d602015-07-09 23:44:25 +0200405 drm_for_each_encoder(encoder, dev) {
Dave Airlief453ba02008-11-07 14:05:41 -0800406
407 if (encoder->crtc != crtc)
408 continue;
409
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200410 encoder_funcs = encoder->helper_private;
411 if (!encoder_funcs)
412 continue;
413
Archit Taneja862e6862015-05-21 11:03:16 +0530414 drm_bridge_pre_enable(encoder->bridge);
Sean Paul3b336ec2013-08-14 16:47:37 -0400415
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200416 if (encoder_funcs->commit)
417 encoder_funcs->commit(encoder);
Dave Airlief453ba02008-11-07 14:05:41 -0800418
Archit Taneja862e6862015-05-21 11:03:16 +0530419 drm_bridge_enable(encoder->bridge);
Dave Airlief453ba02008-11-07 14:05:41 -0800420 }
421
Mario Kleiner27641c32010-10-23 04:20:23 +0200422 /* Calculate and store various constants which
423 * are later needed by vblank and swap-completion
424 * timestamping. They are derived from true hwmode.
425 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300426 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
Mario Kleiner27641c32010-10-23 04:20:23 +0200427
Dave Airlief453ba02008-11-07 14:05:41 -0800428 /* FIXME: add subpixel order */
429done:
Chris Wilson021a8452011-01-28 11:31:56 +0000430 drm_mode_destroy(dev, adjusted_mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800431 if (!ret) {
Ilija Hadzic7e99acd2013-10-29 11:09:44 -0400432 crtc->enabled = saved_enabled;
Dave Airlief453ba02008-11-07 14:05:41 -0800433 crtc->mode = saved_mode;
Daniel Stone5a275282015-03-19 04:33:03 +0000434 crtc->hwmode = saved_hwmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800435 crtc->x = saved_x;
436 crtc->y = saved_y;
437 }
438
439 return ret;
440}
441EXPORT_SYMBOL(drm_crtc_helper_set_mode);
442
Thierry Redinga74591d2014-04-29 11:44:39 +0200443static void
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000444drm_crtc_helper_disable(struct drm_crtc *crtc)
445{
446 struct drm_device *dev = crtc->dev;
447 struct drm_connector *connector;
448 struct drm_encoder *encoder;
449
450 /* Decouple all encoders and their attached connectors from this crtc */
Daniel Vetter6295d602015-07-09 23:44:25 +0200451 drm_for_each_encoder(encoder, dev) {
Daniel Vetterc36a3252016-12-15 16:58:43 +0100452 struct drm_connector_list_iter conn_iter;
453
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000454 if (encoder->crtc != crtc)
455 continue;
456
Thierry Redingb982dab2017-02-28 15:46:43 +0100457 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100458 drm_for_each_connector_iter(connector, &conn_iter) {
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000459 if (connector->encoder != encoder)
460 continue;
461
462 connector->encoder = NULL;
Thierry Redinga6ad6232013-10-02 15:50:06 +0200463
464 /*
465 * drm_helper_disable_unused_functions() ought to be
466 * doing this, but since we've decoupled the encoder
467 * from the connector above, the required connection
468 * between them is henceforth no longer available.
469 */
470 connector->dpms = DRM_MODE_DPMS_OFF;
Dave Airlie0955c122016-04-27 11:27:54 +1000471
472 /* we keep a reference while the encoder is bound */
Thierry Redingad093602017-02-28 15:46:39 +0100473 drm_connector_put(connector);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000474 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100475 drm_connector_list_iter_end(&conn_iter);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000476 }
477
Daniel Vetterb182cc52014-03-20 14:26:34 +0100478 __drm_helper_disable_unused_functions(dev);
Chris Wilson6eebd6b2011-11-28 21:10:05 +0000479}
480
Dave Airlief453ba02008-11-07 14:05:41 -0800481/**
482 * drm_crtc_helper_set_config - set a new config from userspace
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100483 * @set: mode set configuration
Daniel Vettera4eff9a2017-03-22 22:50:57 +0100484 * @ctx: lock acquire context, not used here
Dave Airlief453ba02008-11-07 14:05:41 -0800485 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100486 * The drm_crtc_helper_set_config() helper function implements the of
487 * &drm_crtc_funcs.set_config callback for drivers using the legacy CRTC
488 * helpers.
Daniel Vetter2be94972015-12-04 09:45:46 +0100489 *
490 * It first tries to locate the best encoder for each connector by calling the
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100491 * connector @drm_connector_helper_funcs.best_encoder helper operation.
Daniel Vetter2be94972015-12-04 09:45:46 +0100492 *
493 * After locating the appropriate encoders, the helper function will call the
494 * mode_fixup encoder and CRTC helper operations to adjust the requested mode,
495 * or reject it completely in which case an error will be returned to the
496 * application. If the new configuration after mode adjustment is identical to
497 * the current configuration the helper function will return without performing
498 * any other operation.
499 *
500 * If the adjusted mode is identical to the current mode but changes to the
501 * frame buffer need to be applied, the drm_crtc_helper_set_config() function
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100502 * will call the CRTC &drm_crtc_helper_funcs.mode_set_base helper operation.
Daniel Vetter2be94972015-12-04 09:45:46 +0100503 *
504 * If the adjusted mode differs from the current mode, or if the
505 * ->mode_set_base() helper operation is not provided, the helper function
506 * performs a full mode set sequence by calling the ->prepare(), ->mode_set()
507 * and ->commit() CRTC and encoder helper operations, in that order.
508 * Alternatively it can also use the dpms and disable helper operations. For
Daniel Vetterea0dd852016-12-29 21:48:26 +0100509 * details see &struct drm_crtc_helper_funcs and struct
Daniel Vetter2be94972015-12-04 09:45:46 +0100510 * &drm_encoder_helper_funcs.
511 *
512 * This function is deprecated. New drivers must implement atomic modeset
513 * support, for which this function is unsuitable. Instead drivers should use
514 * drm_atomic_helper_set_config().
Dave Airlief453ba02008-11-07 14:05:41 -0800515 *
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100516 * Returns:
517 * Returns 0 on success, negative errno numbers on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800518 */
Daniel Vettera4eff9a2017-03-22 22:50:57 +0100519int drm_crtc_helper_set_config(struct drm_mode_set *set,
520 struct drm_modeset_acquire_ctx *ctx)
Dave Airlief453ba02008-11-07 14:05:41 -0800521{
522 struct drm_device *dev;
Philipp Zabel93f55972016-06-02 19:27:52 +0200523 struct drm_crtc **save_encoder_crtcs, *new_crtc;
524 struct drm_encoder **save_connector_encoders, *new_encoder, *encoder;
Jakob Bornecrantz4cb72b12009-08-03 13:43:59 +0100525 bool mode_changed = false; /* if true do a full mode set */
526 bool fb_changed = false; /* if true and !mode_changed just do a flip */
Philipp Zabel93f55972016-06-02 19:27:52 +0200527 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100528 struct drm_connector_list_iter conn_iter;
Dave Airlief453ba02008-11-07 14:05:41 -0800529 int count = 0, ro, fail = 0;
Jani Nikulabe26a662015-03-11 11:51:06 +0200530 const struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800531 struct drm_mode_set save_set;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200532 int ret;
Keith Packardbf9dc102010-11-26 10:45:58 -0800533 int i;
Dave Airlief453ba02008-11-07 14:05:41 -0800534
Zhao Yakui58367ed2009-07-20 13:48:07 +0800535 DRM_DEBUG_KMS("\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800536
Daniel Vettere58de882013-06-15 00:13:11 +0200537 BUG_ON(!set);
538 BUG_ON(!set->crtc);
539 BUG_ON(!set->crtc->helper_private);
Dave Airlief453ba02008-11-07 14:05:41 -0800540
Daniel Vettere58de882013-06-15 00:13:11 +0200541 /* Enforce sane interface api - has been abused by the fb helper. */
542 BUG_ON(!set->mode && set->fb);
543 BUG_ON(set->fb && set->num_connectors == 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800544
545 crtc_funcs = set->crtc->helper_private;
546
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100547 dev = set->crtc->dev;
548 WARN_ON(drm_drv_uses_atomic_modeset(dev));
549
Chris Wilsonede3ff52011-01-31 11:16:33 +0000550 if (!set->mode)
551 set->fb = NULL;
552
Jerome Glisse94401062010-07-15 15:43:25 -0400553 if (set->fb) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200554 DRM_DEBUG_KMS("[CRTC:%d:%s] [FB:%d] #connectors=%d (x y) (%i %i)\n",
555 set->crtc->base.id, set->crtc->name,
556 set->fb->base.id,
557 (int)set->num_connectors, set->x, set->y);
Jerome Glisse94401062010-07-15 15:43:25 -0400558 } else {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200559 DRM_DEBUG_KMS("[CRTC:%d:%s] [NOFB]\n",
560 set->crtc->base.id, set->crtc->name);
Thierry Redinga74591d2014-04-29 11:44:39 +0200561 drm_crtc_helper_disable(set->crtc);
562 return 0;
Jerome Glisse94401062010-07-15 15:43:25 -0400563 }
Dave Airlief453ba02008-11-07 14:05:41 -0800564
Daniel Vetter62ff94a2014-01-23 22:18:47 +0100565 drm_warn_on_modeset_not_all_locked(dev);
566
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400567 /*
568 * Allocate space for the backup of all (non-pointer) encoder and
569 * connector data.
570 */
Harsha Sharma4b947b1c2017-10-13 13:07:47 +0530571 save_encoder_crtcs = kcalloc(dev->mode_config.num_encoder,
Philipp Zabel93f55972016-06-02 19:27:52 +0200572 sizeof(struct drm_crtc *), GFP_KERNEL);
573 if (!save_encoder_crtcs)
Dave Airlief453ba02008-11-07 14:05:41 -0800574 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800575
Harsha Sharma4b947b1c2017-10-13 13:07:47 +0530576 save_connector_encoders = kcalloc(dev->mode_config.num_connector,
Philipp Zabel93f55972016-06-02 19:27:52 +0200577 sizeof(struct drm_encoder *), GFP_KERNEL);
578 if (!save_connector_encoders) {
579 kfree(save_encoder_crtcs);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200580 return -ENOMEM;
581 }
582
Ilija Hadzic02ee4e92013-10-29 11:09:46 -0400583 /*
584 * Copy data. Note that driver private data is not affected.
Maarten Maathuise67aae72009-08-27 10:18:29 +0200585 * Should anything bad happen only the expected state is
586 * restored, not the drivers personal bookkeeping.
587 */
588 count = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200589 drm_for_each_encoder(encoder, dev) {
Philipp Zabel93f55972016-06-02 19:27:52 +0200590 save_encoder_crtcs[count++] = encoder->crtc;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200591 }
592
593 count = 0;
Thierry Redingb982dab2017-02-28 15:46:43 +0100594 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100595 drm_for_each_connector_iter(connector, &conn_iter)
Philipp Zabel93f55972016-06-02 19:27:52 +0200596 save_connector_encoders[count++] = connector->encoder;
Thierry Redingb982dab2017-02-28 15:46:43 +0100597 drm_connector_list_iter_end(&conn_iter);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200598
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800599 save_set.crtc = set->crtc;
600 save_set.mode = &set->crtc->mode;
601 save_set.x = set->crtc->x;
602 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -0700603 save_set.fb = set->crtc->primary->fb;
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800604
Dave Airlief453ba02008-11-07 14:05:41 -0800605 /* We should be able to check here if the fb has the same properties
606 * and then just flip_or_move it */
Matt Roperf4510a22014-04-01 15:22:40 -0700607 if (set->crtc->primary->fb != set->fb) {
Jesse Barnes712531b2009-01-09 13:56:14 -0800608 /* If we have no fb then treat it as a full mode set */
Matt Roperf4510a22014-04-01 15:22:40 -0700609 if (set->crtc->primary->fb == NULL) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800610 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800611 mode_changed = true;
Ville Syrjälädbd4d572016-11-18 21:53:10 +0200612 } else if (set->fb->format != set->crtc->primary->fb->format) {
Laurent Pinchartce83adf2013-04-22 01:38:47 +0200613 mode_changed = true;
Dave Airlie8dff4742010-02-11 14:28:58 +1000614 } else
Jesse Barnes712531b2009-01-09 13:56:14 -0800615 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800616 }
617
618 if (set->x != set->crtc->x || set->y != set->crtc->y)
Jesse Barnes712531b2009-01-09 13:56:14 -0800619 fb_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800620
Liu Ying2deafc72016-01-14 14:00:10 +0800621 if (!drm_mode_equal(set->mode, &set->crtc->mode)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800622 DRM_DEBUG_KMS("modes are different, full mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800623 drm_mode_debug_printmodeline(&set->crtc->mode);
624 drm_mode_debug_printmodeline(set->mode);
Jesse Barnes712531b2009-01-09 13:56:14 -0800625 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800626 }
627
Philipp Zabelfffc5f52016-06-02 19:27:51 +0200628 /* take a reference on all unbound connectors in set, reuse the
629 * already taken reference for bound connectors
630 */
Dave Airlie0955c122016-04-27 11:27:54 +1000631 for (ro = 0; ro < set->num_connectors; ro++) {
Philipp Zabelfffc5f52016-06-02 19:27:51 +0200632 if (set->connectors[ro]->encoder)
633 continue;
Thierry Redingad093602017-02-28 15:46:39 +0100634 drm_connector_get(set->connectors[ro]);
Dave Airlie0955c122016-04-27 11:27:54 +1000635 }
636
Dave Airlief453ba02008-11-07 14:05:41 -0800637 /* a) traverse passed in connector list and get encoders for them */
638 count = 0;
Thierry Redingb982dab2017-02-28 15:46:43 +0100639 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100640 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikulabe26a662015-03-11 11:51:06 +0200641 const struct drm_connector_helper_funcs *connector_funcs =
Dave Airlief453ba02008-11-07 14:05:41 -0800642 connector->helper_private;
Dave Airlief453ba02008-11-07 14:05:41 -0800643 new_encoder = connector->encoder;
644 for (ro = 0; ro < set->num_connectors; ro++) {
645 if (set->connectors[ro] == connector) {
646 new_encoder = connector_funcs->best_encoder(connector);
647 /* if we can't get an encoder for a connector
648 we are setting now - then fail */
649 if (new_encoder == NULL)
650 /* don't break so fail path works correct */
651 fail = 1;
Daniel Vetter25f397a2013-07-19 18:57:11 +0200652
653 if (connector->dpms != DRM_MODE_DPMS_ON) {
654 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
655 mode_changed = true;
656 }
Daniel Vetter177cf922014-04-01 22:14:59 +0200657
658 break;
Dave Airlief453ba02008-11-07 14:05:41 -0800659 }
660 }
661
662 if (new_encoder != connector->encoder) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800663 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800664 mode_changed = true;
Maarten Maathuisff846ab2009-08-19 00:56:45 +0200665 /* If the encoder is reused for another connector, then
666 * the appropriate crtc will be set later.
667 */
Maarten Maathuisff6fdbe2009-09-01 03:39:04 +0200668 if (connector->encoder)
669 connector->encoder->crtc = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800670 connector->encoder = new_encoder;
671 }
672 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100673 drm_connector_list_iter_end(&conn_iter);
Dave Airlief453ba02008-11-07 14:05:41 -0800674
675 if (fail) {
676 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200677 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800678 }
679
680 count = 0;
Thierry Redingb982dab2017-02-28 15:46:43 +0100681 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100682 drm_for_each_connector_iter(connector, &conn_iter) {
Dave Airlief453ba02008-11-07 14:05:41 -0800683 if (!connector->encoder)
684 continue;
685
Dave Airlief453ba02008-11-07 14:05:41 -0800686 if (connector->encoder->crtc == set->crtc)
687 new_crtc = NULL;
688 else
689 new_crtc = connector->encoder->crtc;
690
691 for (ro = 0; ro < set->num_connectors; ro++) {
692 if (set->connectors[ro] == connector)
693 new_crtc = set->crtc;
694 }
Jesse Barnes7bec7562009-02-23 16:09:34 -0800695
696 /* Make sure the new CRTC will work with the encoder */
697 if (new_crtc &&
698 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
699 ret = -EINVAL;
Thierry Redingb982dab2017-02-28 15:46:43 +0100700 drm_connector_list_iter_end(&conn_iter);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200701 goto fail;
Jesse Barnes7bec7562009-02-23 16:09:34 -0800702 }
Dave Airlief453ba02008-11-07 14:05:41 -0800703 if (new_crtc != connector->encoder->crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800704 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Jesse Barnes712531b2009-01-09 13:56:14 -0800705 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800706 connector->encoder->crtc = new_crtc;
707 }
Jerome Glisse94401062010-07-15 15:43:25 -0400708 if (new_crtc) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200709 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d:%s]\n",
710 connector->base.id, connector->name,
711 new_crtc->base.id, new_crtc->name);
Jerome Glisse94401062010-07-15 15:43:25 -0400712 } else {
713 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200714 connector->base.id, connector->name);
Jerome Glisse94401062010-07-15 15:43:25 -0400715 }
Dave Airlief453ba02008-11-07 14:05:41 -0800716 }
Thierry Redingb982dab2017-02-28 15:46:43 +0100717 drm_connector_list_iter_end(&conn_iter);
Dave Airlief453ba02008-11-07 14:05:41 -0800718
719 /* mode_set_base is not a required function */
Jesse Barnes712531b2009-01-09 13:56:14 -0800720 if (fb_changed && !crtc_funcs->mode_set_base)
721 mode_changed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800722
Jesse Barnes712531b2009-01-09 13:56:14 -0800723 if (mode_changed) {
Ilija Hadzic48b1f5d2013-10-29 11:09:45 -0400724 if (drm_helper_crtc_in_use(set->crtc)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +0800725 DRM_DEBUG_KMS("attempting to set mode from"
726 " userspace\n");
Dave Airlief453ba02008-11-07 14:05:41 -0800727 drm_mode_debug_printmodeline(set->mode);
Matt Roperf4510a22014-04-01 15:22:40 -0700728 set->crtc->primary->fb = set->fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800729 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500730 set->x, set->y,
Ilija Hadzicbec2eac2013-10-29 11:09:42 -0400731 save_set.fb)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200732 DRM_ERROR("failed to set mode on [CRTC:%d:%s]\n",
733 set->crtc->base.id, set->crtc->name);
Matt Roperf4510a22014-04-01 15:22:40 -0700734 set->crtc->primary->fb = save_set.fb;
Dave Airlief453ba02008-11-07 14:05:41 -0800735 ret = -EINVAL;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200736 goto fail;
Dave Airlief453ba02008-11-07 14:05:41 -0800737 }
Daniel Vetter25f397a2013-07-19 18:57:11 +0200738 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
739 for (i = 0; i < set->num_connectors; i++) {
740 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
Jani Nikula25933822014-06-03 14:56:20 +0300741 set->connectors[i]->name);
Daniel Vetter25f397a2013-07-19 18:57:11 +0200742 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
743 }
Dave Airlief453ba02008-11-07 14:05:41 -0800744 }
Daniel Vetterb182cc52014-03-20 14:26:34 +0100745 __drm_helper_disable_unused_functions(dev);
Jesse Barnes712531b2009-01-09 13:56:14 -0800746 } else if (fb_changed) {
Ben Skeggs9b1596a2009-09-18 10:43:52 +1000747 set->crtc->x = set->x;
748 set->crtc->y = set->y;
Matt Roperf4510a22014-04-01 15:22:40 -0700749 set->crtc->primary->fb = set->fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +0000750 ret = crtc_funcs->mode_set_base(set->crtc,
Ilija Hadzicbec2eac2013-10-29 11:09:42 -0400751 set->x, set->y, save_set.fb);
Chris Wilson0ba41e42011-01-08 15:10:41 +0000752 if (ret != 0) {
Ilija Hadzicfbce4062013-10-29 11:09:43 -0400753 set->crtc->x = save_set.x;
754 set->crtc->y = save_set.y;
Matt Roperf4510a22014-04-01 15:22:40 -0700755 set->crtc->primary->fb = save_set.fb;
Maarten Maathuise67aae72009-08-27 10:18:29 +0200756 goto fail;
Chris Wilson0ba41e42011-01-08 15:10:41 +0000757 }
Dave Airlief453ba02008-11-07 14:05:41 -0800758 }
759
Philipp Zabel93f55972016-06-02 19:27:52 +0200760 kfree(save_connector_encoders);
761 kfree(save_encoder_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800762 return 0;
763
Maarten Maathuise67aae72009-08-27 10:18:29 +0200764fail:
765 /* Restore all previous data. */
Dave Airlief453ba02008-11-07 14:05:41 -0800766 count = 0;
Daniel Vetter6295d602015-07-09 23:44:25 +0200767 drm_for_each_encoder(encoder, dev) {
Philipp Zabel93f55972016-06-02 19:27:52 +0200768 encoder->crtc = save_encoder_crtcs[count++];
Chris Wilsone62fb642009-02-11 16:39:21 +0000769 }
Maarten Maathuise67aae72009-08-27 10:18:29 +0200770
Dave Airlief453ba02008-11-07 14:05:41 -0800771 count = 0;
Thierry Redingb982dab2017-02-28 15:46:43 +0100772 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100773 drm_for_each_connector_iter(connector, &conn_iter)
Philipp Zabel93f55972016-06-02 19:27:52 +0200774 connector->encoder = save_connector_encoders[count++];
Thierry Redingb982dab2017-02-28 15:46:43 +0100775 drm_connector_list_iter_end(&conn_iter);
Maarten Maathuise67aae72009-08-27 10:18:29 +0200776
Philipp Zabelfffc5f52016-06-02 19:27:51 +0200777 /* after fail drop reference on all unbound connectors in set, let
778 * bound connectors keep their reference
779 */
Dave Airlie0955c122016-04-27 11:27:54 +1000780 for (ro = 0; ro < set->num_connectors; ro++) {
Philipp Zabelfffc5f52016-06-02 19:27:51 +0200781 if (set->connectors[ro]->encoder)
782 continue;
Thierry Redingad093602017-02-28 15:46:39 +0100783 drm_connector_put(set->connectors[ro]);
Dave Airlie0955c122016-04-27 11:27:54 +1000784 }
785
Jesse Barnesc5006cfe2011-11-07 10:39:57 -0800786 /* Try to restore the config */
787 if (mode_changed &&
788 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
789 save_set.y, save_set.fb))
790 DRM_ERROR("failed to restore config after modeset failure\n");
791
Philipp Zabel93f55972016-06-02 19:27:52 +0200792 kfree(save_connector_encoders);
793 kfree(save_encoder_crtcs);
Dave Airlief453ba02008-11-07 14:05:41 -0800794 return ret;
795}
796EXPORT_SYMBOL(drm_crtc_helper_set_config);
797
Keith Packardc9fb15f2009-05-30 20:42:28 -0700798static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
799{
800 int dpms = DRM_MODE_DPMS_OFF;
801 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100802 struct drm_connector_list_iter conn_iter;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700803 struct drm_device *dev = encoder->dev;
804
Thierry Redingb982dab2017-02-28 15:46:43 +0100805 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100806 drm_for_each_connector_iter(connector, &conn_iter)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700807 if (connector->encoder == encoder)
808 if (connector->dpms < dpms)
809 dpms = connector->dpms;
Thierry Redingb982dab2017-02-28 15:46:43 +0100810 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100811
Keith Packardc9fb15f2009-05-30 20:42:28 -0700812 return dpms;
813}
814
Sean Paul3b336ec2013-08-14 16:47:37 -0400815/* Helper which handles bridge ordering around encoder dpms */
816static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
817{
818 struct drm_bridge *bridge = encoder->bridge;
Jani Nikulabe26a662015-03-11 11:51:06 +0200819 const struct drm_encoder_helper_funcs *encoder_funcs;
Sean Paul3b336ec2013-08-14 16:47:37 -0400820
Noralf Trønnes75229ec2016-05-05 15:24:32 +0200821 encoder_funcs = encoder->helper_private;
822 if (!encoder_funcs)
823 return;
824
Archit Taneja862e6862015-05-21 11:03:16 +0530825 if (mode == DRM_MODE_DPMS_ON)
826 drm_bridge_pre_enable(bridge);
827 else
828 drm_bridge_disable(bridge);
Sean Paul3b336ec2013-08-14 16:47:37 -0400829
Sean Paul3b336ec2013-08-14 16:47:37 -0400830 if (encoder_funcs->dpms)
831 encoder_funcs->dpms(encoder, mode);
832
Archit Taneja862e6862015-05-21 11:03:16 +0530833 if (mode == DRM_MODE_DPMS_ON)
834 drm_bridge_enable(bridge);
835 else
836 drm_bridge_post_disable(bridge);
Sean Paul3b336ec2013-08-14 16:47:37 -0400837}
838
Keith Packardc9fb15f2009-05-30 20:42:28 -0700839static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
840{
841 int dpms = DRM_MODE_DPMS_OFF;
842 struct drm_connector *connector;
Daniel Vetterc36a3252016-12-15 16:58:43 +0100843 struct drm_connector_list_iter conn_iter;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700844 struct drm_device *dev = crtc->dev;
845
Thierry Redingb982dab2017-02-28 15:46:43 +0100846 drm_connector_list_iter_begin(dev, &conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100847 drm_for_each_connector_iter(connector, &conn_iter)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700848 if (connector->encoder && connector->encoder->crtc == crtc)
849 if (connector->dpms < dpms)
850 dpms = connector->dpms;
Thierry Redingb982dab2017-02-28 15:46:43 +0100851 drm_connector_list_iter_end(&conn_iter);
Daniel Vetterc36a3252016-12-15 16:58:43 +0100852
Keith Packardc9fb15f2009-05-30 20:42:28 -0700853 return dpms;
854}
855
856/**
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100857 * drm_helper_connector_dpms() - connector dpms helper implementation
858 * @connector: affected connector
859 * @mode: DPMS mode
Keith Packardc9fb15f2009-05-30 20:42:28 -0700860 *
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100861 * The drm_helper_connector_dpms() helper function implements the
862 * &drm_connector_funcs.dpms callback for drivers using the legacy CRTC
863 * helpers.
Daniel Vetter2be94972015-12-04 09:45:46 +0100864 *
865 * This is the main helper function provided by the CRTC helper framework for
Daniel Vetter0d4ed4c2012-11-01 14:45:16 +0100866 * implementing the DPMS connector attribute. It computes the new desired DPMS
Daniel Vetter6806cdf2017-01-25 07:26:43 +0100867 * state for all encoders and CRTCs in the output mesh and calls the
868 * &drm_crtc_helper_funcs.dpms and &drm_encoder_helper_funcs.dpms callbacks
869 * provided by the driver.
Daniel Vetter2be94972015-12-04 09:45:46 +0100870 *
871 * This function is deprecated. New drivers must implement atomic modeset
Daniel Vetter144a7992017-07-25 14:02:04 +0200872 * support, where DPMS is handled in the DRM core.
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +0200873 *
874 * Returns:
875 * Always returns 0.
Keith Packardc9fb15f2009-05-30 20:42:28 -0700876 */
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +0200877int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
Keith Packardc9fb15f2009-05-30 20:42:28 -0700878{
879 struct drm_encoder *encoder = connector->encoder;
880 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
Sean Paul3b336ec2013-08-14 16:47:37 -0400881 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700882
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100883 WARN_ON(drm_drv_uses_atomic_modeset(connector->dev));
884
Keith Packardc9fb15f2009-05-30 20:42:28 -0700885 if (mode == connector->dpms)
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +0200886 return 0;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700887
888 old_dpms = connector->dpms;
889 connector->dpms = mode;
890
Sean Paul3b336ec2013-08-14 16:47:37 -0400891 if (encoder)
892 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
893
Keith Packardc9fb15f2009-05-30 20:42:28 -0700894 /* from off to on, do crtc then encoder */
895 if (mode < old_dpms) {
896 if (crtc) {
Jani Nikulabe26a662015-03-11 11:51:06 +0200897 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700898 if (crtc_funcs->dpms)
899 (*crtc_funcs->dpms) (crtc,
900 drm_helper_choose_crtc_dpms(crtc));
901 }
Sean Paul3b336ec2013-08-14 16:47:37 -0400902 if (encoder)
903 drm_helper_encoder_dpms(encoder, encoder_dpms);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700904 }
905
906 /* from on to off, do encoder then crtc */
907 if (mode > old_dpms) {
Sean Paul3b336ec2013-08-14 16:47:37 -0400908 if (encoder)
909 drm_helper_encoder_dpms(encoder, encoder_dpms);
Keith Packardc9fb15f2009-05-30 20:42:28 -0700910 if (crtc) {
Jani Nikulabe26a662015-03-11 11:51:06 +0200911 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700912 if (crtc_funcs->dpms)
913 (*crtc_funcs->dpms) (crtc,
914 drm_helper_choose_crtc_dpms(crtc));
915 }
916 }
917
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +0200918 return 0;
Keith Packardc9fb15f2009-05-30 20:42:28 -0700919}
920EXPORT_SYMBOL(drm_helper_connector_dpms);
921
Daniel Vetter3fcc42e2014-01-23 22:58:26 +0100922/**
Daniel Vetteraa4cd912014-01-22 16:42:02 +0100923 * drm_helper_resume_force_mode - force-restore mode setting configuration
924 * @dev: drm_device which should be restored
925 *
926 * Drivers which use the mode setting helpers can use this function to
927 * force-restore the mode setting configuration e.g. on resume or when something
928 * else might have trampled over the hw state (like some overzealous old BIOSen
929 * tended to do).
Daniel Vetter00d762c2014-01-23 22:28:30 +0100930 *
931 * This helper doesn't provide a error return value since restoring the old
932 * config should never fail due to resource allocation issues since the driver
933 * has successfully set the restored configuration already. Hence this should
934 * boil down to the equivalent of a few dpms on calls, which also don't provide
935 * an error code.
936 *
937 * Drivers where simply restoring an old configuration again might fail (e.g.
938 * due to slight differences in allocating shared resources when the
939 * configuration is restored in a different order than when userspace set it up)
940 * need to use their own restore logic.
Thierry Reding14942762015-12-02 17:50:04 +0100941 *
942 * This function is deprecated. New drivers should implement atomic mode-
943 * setting and use the atomic suspend/resume helpers.
944 *
945 * See also:
946 * drm_atomic_helper_suspend(), drm_atomic_helper_resume()
Daniel Vetteraa4cd912014-01-22 16:42:02 +0100947 */
Daniel Vetter00d762c2014-01-23 22:28:30 +0100948void drm_helper_resume_force_mode(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -0800949{
950 struct drm_crtc *crtc;
David John89347bb2009-12-31 12:00:46 +0530951 struct drm_encoder *encoder;
Jani Nikulabe26a662015-03-11 11:51:06 +0200952 const struct drm_crtc_helper_funcs *crtc_funcs;
Daniel Vetter00d762c2014-01-23 22:28:30 +0100953 int encoder_dpms;
954 bool ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800955
Daniel Vetter2b5ab0e2019-01-10 11:30:45 +0100956 WARN_ON(drm_drv_uses_atomic_modeset(dev));
957
Dave Airlie3ea87852014-03-21 10:45:40 +1000958 drm_modeset_lock_all(dev);
Daniel Vetter6295d602015-07-09 23:44:25 +0200959 drm_for_each_crtc(crtc, dev) {
Dave Airlief453ba02008-11-07 14:05:41 -0800960
961 if (!crtc->enabled)
962 continue;
963
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -0500964 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
Matt Roperf4510a22014-04-01 15:22:40 -0700965 crtc->x, crtc->y, crtc->primary->fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800966
Daniel Vetter00d762c2014-01-23 22:28:30 +0100967 /* Restoring the old config should never fail! */
Dave Airlief453ba02008-11-07 14:05:41 -0800968 if (ret == false)
969 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
David John89347bb2009-12-31 12:00:46 +0530970
971 /* Turn off outputs that were already powered off */
972 if (drm_helper_choose_crtc_dpms(crtc)) {
Daniel Vetter6295d602015-07-09 23:44:25 +0200973 drm_for_each_encoder(encoder, dev) {
David John89347bb2009-12-31 12:00:46 +0530974
975 if(encoder->crtc != crtc)
976 continue;
977
Sean Paul3b336ec2013-08-14 16:47:37 -0400978 encoder_dpms = drm_helper_choose_encoder_dpms(
979 encoder);
980
981 drm_helper_encoder_dpms(encoder, encoder_dpms);
David John89347bb2009-12-31 12:00:46 +0530982 }
Chris Wilson817e6312010-08-06 15:03:31 +0100983
984 crtc_funcs = crtc->helper_private;
985 if (crtc_funcs->dpms)
986 (*crtc_funcs->dpms) (crtc,
987 drm_helper_choose_crtc_dpms(crtc));
David John89347bb2009-12-31 12:00:46 +0530988 }
Dave Airlief453ba02008-11-07 14:05:41 -0800989 }
Daniel Vetter00d762c2014-01-23 22:28:30 +0100990
Zhao Yakuiaf4fcb52009-07-08 14:13:13 +0800991 /* disable the unused connectors while restoring the modesetting */
Daniel Vetterb182cc52014-03-20 14:26:34 +0100992 __drm_helper_disable_unused_functions(dev);
Dave Airlie3ea87852014-03-21 10:45:40 +1000993 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800994}
995EXPORT_SYMBOL(drm_helper_resume_force_mode);
Daniel Vetterc2d88e02018-12-17 20:43:00 +0100996
997/**
998 * drm_helper_force_disable_all - Forcibly turn off all enabled CRTCs
999 * @dev: DRM device whose CRTCs to turn off
1000 *
1001 * Drivers may want to call this on unload to ensure that all displays are
1002 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
1003 *
1004 * Note: This should only be used by non-atomic legacy drivers. For an atomic
1005 * version look at drm_atomic_helper_shutdown().
1006 *
1007 * Returns:
1008 * Zero on success, error code on failure.
1009 */
1010int drm_helper_force_disable_all(struct drm_device *dev)
1011{
1012 struct drm_crtc *crtc;
1013 int ret = 0;
1014
1015 drm_modeset_lock_all(dev);
1016 drm_for_each_crtc(crtc, dev)
1017 if (crtc->enabled) {
1018 struct drm_mode_set set = {
1019 .crtc = crtc,
1020 };
1021
1022 ret = drm_mode_set_config_internal(&set);
1023 if (ret)
1024 goto out;
1025 }
1026out:
1027 drm_modeset_unlock_all(dev);
1028 return ret;
1029}
1030EXPORT_SYMBOL(drm_helper_force_disable_all);