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