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