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