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