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