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