kernel - cleanup vfs_cache debugging
[dragonfly.git] / sys / dev / drm / drm_crtc_helper.c
blob78bf85b62ccdc5126599a14e11c79f5c6a52784f
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 * DRM core CRTC related functions
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.
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.
25 * Authors:
26 * Keith Packard
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/kernel.h>
33 #include <linux/export.h>
34 #include <linux/moduleparam.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_crtc.h>
39 #include <uapi_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>
46 /**
47 * DOC: overview
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().
54 * The driver callbacks are mostly compatible with the atomic modeset helpers,
55 * except for the handling of the primary plane: Atomic helpers require that the
56 * primary plane is implemented as a real standalone plane and not directly tied
57 * to the CRTC state. For easier transition this library provides functions to
58 * implement the old semantics required by the CRTC helpers using the new plane
59 * and atomic helper callbacks.
61 * Drivers are strongly urged to convert to the atomic helpers (by way of first
62 * converting to the plane helpers). New drivers must not use these functions
63 * but need to implement the atomic interface instead, potentially using the
64 * atomic helpers for that.
66 MODULE_AUTHOR("David Airlie, Jesse Barnes");
67 MODULE_DESCRIPTION("DRM KMS helper");
69 /**
70 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
71 * connector list
72 * @dev: drm device to operate on
74 * Some userspace presumes that the first connected connector is the main
75 * display, where it's supposed to display e.g. the login screen. For
76 * laptops, this should be the main panel. Use this function to sort all
77 * (eDP/LVDS) panels to the front of the connector list, instead of
78 * painstakingly trying to initialize them in the right order.
80 void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
82 struct drm_connector *connector, *tmp;
83 struct list_head panel_list;
85 INIT_LIST_HEAD(&panel_list);
87 list_for_each_entry_safe(connector, tmp,
88 &dev->mode_config.connector_list, head) {
89 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
90 connector->connector_type == DRM_MODE_CONNECTOR_eDP)
91 list_move_tail(&connector->head, &panel_list);
94 list_splice(&panel_list, &dev->mode_config.connector_list);
96 EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
98 /**
99 * drm_helper_encoder_in_use - check if a given encoder is in use
100 * @encoder: encoder to check
102 * Checks whether @encoder is with the current mode setting output configuration
103 * in use by any connector. This doesn't mean that it is actually enabled since
104 * the DPMS state is tracked separately.
106 * Returns:
107 * True if @encoder is used, false otherwise.
109 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
111 struct drm_connector *connector;
112 struct drm_device *dev = encoder->dev;
115 * We can expect this mutex to be locked if we are not panicking.
116 * Locking is currently fubar in the panic handler.
118 #if 0
119 if (!oops_in_progress) {
120 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
121 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
123 #endif
125 drm_for_each_connector(connector, dev)
126 if (connector->encoder == encoder)
127 return true;
128 return false;
130 EXPORT_SYMBOL(drm_helper_encoder_in_use);
133 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
134 * @crtc: CRTC to check
136 * Checks whether @crtc is with the current mode setting output configuration
137 * in use by any connector. This doesn't mean that it is actually enabled since
138 * the DPMS state is tracked separately.
140 * Returns:
141 * True if @crtc is used, false otherwise.
143 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
145 struct drm_encoder *encoder;
146 struct drm_device *dev = crtc->dev;
149 * We can expect this mutex to be locked if we are not panicking.
150 * Locking is currently fubar in the panic handler.
152 #if 0
153 if (!oops_in_progress)
154 #endif
155 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
157 drm_for_each_encoder(encoder, dev)
158 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
159 return true;
160 return false;
162 EXPORT_SYMBOL(drm_helper_crtc_in_use);
164 static void
165 drm_encoder_disable(struct drm_encoder *encoder)
167 const struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
169 drm_bridge_disable(encoder->bridge);
171 if (encoder_funcs->disable)
172 (*encoder_funcs->disable)(encoder);
173 else
174 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
176 drm_bridge_post_disable(encoder->bridge);
179 static void __drm_helper_disable_unused_functions(struct drm_device *dev)
181 struct drm_encoder *encoder;
182 struct drm_crtc *crtc;
184 drm_warn_on_modeset_not_all_locked(dev);
186 drm_for_each_encoder(encoder, dev) {
187 if (!drm_helper_encoder_in_use(encoder)) {
188 drm_encoder_disable(encoder);
189 /* disconnect encoder from any connector */
190 encoder->crtc = NULL;
194 drm_for_each_crtc(crtc, dev) {
195 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
196 crtc->enabled = drm_helper_crtc_in_use(crtc);
197 if (!crtc->enabled) {
198 if (crtc_funcs->disable)
199 (*crtc_funcs->disable)(crtc);
200 else
201 (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
202 crtc->primary->fb = NULL;
208 * drm_helper_disable_unused_functions - disable unused objects
209 * @dev: DRM device
211 * This function walks through the entire mode setting configuration of @dev. It
212 * will remove any crtc links of unused encoders and encoder links of
213 * disconnected connectors. Then it will disable all unused encoders and crtcs
214 * either by calling their disable callback if available or by calling their
215 * dpms callback with DRM_MODE_DPMS_OFF.
217 void drm_helper_disable_unused_functions(struct drm_device *dev)
219 drm_modeset_lock_all(dev);
220 __drm_helper_disable_unused_functions(dev);
221 drm_modeset_unlock_all(dev);
223 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
226 * Check the CRTC we're going to map each output to vs. its current
227 * CRTC. If they don't match, we have to disable the output and the CRTC
228 * since the driver will have to re-route things.
230 static void
231 drm_crtc_prepare_encoders(struct drm_device *dev)
233 const struct drm_encoder_helper_funcs *encoder_funcs;
234 struct drm_encoder *encoder;
236 drm_for_each_encoder(encoder, dev) {
237 encoder_funcs = encoder->helper_private;
238 /* Disable unused encoders */
239 if (encoder->crtc == NULL)
240 drm_encoder_disable(encoder);
241 /* Disable encoders whose CRTC is about to change */
242 if (encoder_funcs->get_crtc &&
243 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
244 drm_encoder_disable(encoder);
249 * drm_crtc_helper_set_mode - internal helper to set a mode
250 * @crtc: CRTC to program
251 * @mode: mode to use
252 * @x: horizontal offset into the surface
253 * @y: vertical offset into the surface
254 * @old_fb: old framebuffer, for cleanup
256 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
257 * to fixup or reject the mode prior to trying to set it. This is an internal
258 * helper that drivers could e.g. use to update properties that require the
259 * entire output pipe to be disabled and re-enabled in a new configuration. For
260 * example for changing whether audio is enabled on a hdmi link or for changing
261 * panel fitter or dither attributes. It is also called by the
262 * drm_crtc_helper_set_config() helper function to drive the mode setting
263 * sequence.
265 * Returns:
266 * True if the mode was set successfully, false otherwise.
268 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
269 struct drm_display_mode *mode,
270 int x, int y,
271 struct drm_framebuffer *old_fb)
273 struct drm_device *dev = crtc->dev;
274 struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
275 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
276 const struct drm_encoder_helper_funcs *encoder_funcs;
277 int saved_x, saved_y;
278 bool saved_enabled;
279 struct drm_encoder *encoder;
280 bool ret = true;
282 drm_warn_on_modeset_not_all_locked(dev);
284 saved_enabled = crtc->enabled;
285 crtc->enabled = drm_helper_crtc_in_use(crtc);
286 if (!crtc->enabled)
287 return true;
289 adjusted_mode = drm_mode_duplicate(dev, mode);
290 if (!adjusted_mode) {
291 crtc->enabled = saved_enabled;
292 return false;
295 saved_mode = crtc->mode;
296 saved_hwmode = crtc->hwmode;
297 saved_x = crtc->x;
298 saved_y = crtc->y;
300 /* Update crtc values up front so the driver can rely on them for mode
301 * setting.
303 crtc->mode = *mode;
304 crtc->x = x;
305 crtc->y = y;
307 /* Pass our mode to the connectors and the CRTC to give them a chance to
308 * adjust it according to limitations or connector properties, and also
309 * a chance to reject the mode entirely.
311 drm_for_each_encoder(encoder, dev) {
313 if (encoder->crtc != crtc)
314 continue;
316 ret = drm_bridge_mode_fixup(encoder->bridge,
317 mode, adjusted_mode);
318 if (!ret) {
319 DRM_DEBUG_KMS("Bridge fixup failed\n");
320 goto done;
323 encoder_funcs = encoder->helper_private;
324 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
325 adjusted_mode))) {
326 DRM_DEBUG_KMS("Encoder fixup failed\n");
327 goto done;
331 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
332 DRM_DEBUG_KMS("CRTC fixup failed\n");
333 goto done;
335 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
337 crtc->hwmode = *adjusted_mode;
339 /* Prepare the encoders and CRTCs before setting the mode. */
340 drm_for_each_encoder(encoder, dev) {
342 if (encoder->crtc != crtc)
343 continue;
345 drm_bridge_disable(encoder->bridge);
347 encoder_funcs = encoder->helper_private;
348 /* Disable the encoders as the first thing we do. */
349 encoder_funcs->prepare(encoder);
351 drm_bridge_post_disable(encoder->bridge);
354 drm_crtc_prepare_encoders(dev);
356 crtc_funcs->prepare(crtc);
358 /* Set up the DPLL and any encoders state that needs to adjust or depend
359 * on the DPLL.
361 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
362 if (!ret)
363 goto done;
365 drm_for_each_encoder(encoder, dev) {
367 if (encoder->crtc != crtc)
368 continue;
370 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
371 encoder->base.id, encoder->name,
372 mode->base.id, mode->name);
373 encoder_funcs = encoder->helper_private;
374 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
376 drm_bridge_mode_set(encoder->bridge, mode, adjusted_mode);
379 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
380 crtc_funcs->commit(crtc);
382 drm_for_each_encoder(encoder, dev) {
384 if (encoder->crtc != crtc)
385 continue;
387 drm_bridge_pre_enable(encoder->bridge);
389 encoder_funcs = encoder->helper_private;
390 encoder_funcs->commit(encoder);
392 drm_bridge_enable(encoder->bridge);
395 /* Calculate and store various constants which
396 * are later needed by vblank and swap-completion
397 * timestamping. They are derived from true hwmode.
399 drm_calc_timestamping_constants(crtc, &crtc->hwmode);
401 /* FIXME: add subpixel order */
402 done:
403 drm_mode_destroy(dev, adjusted_mode);
404 if (!ret) {
405 crtc->enabled = saved_enabled;
406 crtc->mode = saved_mode;
407 crtc->hwmode = saved_hwmode;
408 crtc->x = saved_x;
409 crtc->y = saved_y;
412 return ret;
414 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
416 static void
417 drm_crtc_helper_disable(struct drm_crtc *crtc)
419 struct drm_device *dev = crtc->dev;
420 struct drm_connector *connector;
421 struct drm_encoder *encoder;
423 /* Decouple all encoders and their attached connectors from this crtc */
424 drm_for_each_encoder(encoder, dev) {
425 if (encoder->crtc != crtc)
426 continue;
428 drm_for_each_connector(connector, dev) {
429 if (connector->encoder != encoder)
430 continue;
432 connector->encoder = NULL;
435 * drm_helper_disable_unused_functions() ought to be
436 * doing this, but since we've decoupled the encoder
437 * from the connector above, the required connection
438 * between them is henceforth no longer available.
440 connector->dpms = DRM_MODE_DPMS_OFF;
444 __drm_helper_disable_unused_functions(dev);
448 * drm_crtc_helper_set_config - set a new config from userspace
449 * @set: mode set configuration
451 * Setup a new configuration, provided by the upper layers (either an ioctl call
452 * from userspace or internally e.g. from the fbdev support code) in @set, and
453 * enable it. This is the main helper functions for drivers that implement
454 * kernel mode setting with the crtc helper functions and the assorted
455 * ->prepare(), ->modeset() and ->commit() helper callbacks.
457 * Returns:
458 * Returns 0 on success, negative errno numbers on failure.
460 int drm_crtc_helper_set_config(struct drm_mode_set *set)
462 struct drm_device *dev;
463 struct drm_crtc *new_crtc;
464 struct drm_encoder *save_encoders, *new_encoder, *encoder;
465 bool mode_changed = false; /* if true do a full mode set */
466 bool fb_changed = false; /* if true and !mode_changed just do a flip */
467 struct drm_connector *save_connectors, *connector;
468 int count = 0, ro, fail = 0;
469 const struct drm_crtc_helper_funcs *crtc_funcs;
470 struct drm_mode_set save_set;
471 int ret;
472 int i;
474 DRM_DEBUG_KMS("\n");
476 BUG_ON(!set);
477 BUG_ON(!set->crtc);
478 BUG_ON(!set->crtc->helper_private);
480 /* Enforce sane interface api - has been abused by the fb helper. */
481 BUG_ON(!set->mode && set->fb);
482 BUG_ON(set->fb && set->num_connectors == 0);
484 crtc_funcs = set->crtc->helper_private;
486 if (!set->mode)
487 set->fb = NULL;
489 if (set->fb) {
490 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
491 set->crtc->base.id, set->fb->base.id,
492 (int)set->num_connectors, set->x, set->y);
493 } else {
494 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
495 drm_crtc_helper_disable(set->crtc);
496 return 0;
499 dev = set->crtc->dev;
501 drm_warn_on_modeset_not_all_locked(dev);
504 * Allocate space for the backup of all (non-pointer) encoder and
505 * connector data.
507 save_encoders = kzalloc(dev->mode_config.num_encoder *
508 sizeof(struct drm_encoder), GFP_KERNEL);
509 if (!save_encoders)
510 return -ENOMEM;
512 save_connectors = kzalloc(dev->mode_config.num_connector *
513 sizeof(struct drm_connector), GFP_KERNEL);
514 if (!save_connectors) {
515 kfree(save_encoders);
516 return -ENOMEM;
520 * Copy data. Note that driver private data is not affected.
521 * Should anything bad happen only the expected state is
522 * restored, not the drivers personal bookkeeping.
524 count = 0;
525 drm_for_each_encoder(encoder, dev) {
526 save_encoders[count++] = *encoder;
529 count = 0;
530 drm_for_each_connector(connector, dev) {
531 save_connectors[count++] = *connector;
534 save_set.crtc = set->crtc;
535 save_set.mode = &set->crtc->mode;
536 save_set.x = set->crtc->x;
537 save_set.y = set->crtc->y;
538 save_set.fb = set->crtc->primary->fb;
540 /* We should be able to check here if the fb has the same properties
541 * and then just flip_or_move it */
542 if (set->crtc->primary->fb != set->fb) {
543 /* If we have no fb then treat it as a full mode set */
544 if (set->crtc->primary->fb == NULL) {
545 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
546 mode_changed = true;
547 } else if (set->fb == NULL) {
548 mode_changed = true;
549 } else if (set->fb->pixel_format !=
550 set->crtc->primary->fb->pixel_format) {
551 mode_changed = true;
552 } else
553 fb_changed = true;
556 if (set->x != set->crtc->x || set->y != set->crtc->y)
557 fb_changed = true;
559 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
560 DRM_DEBUG_KMS("modes are different, full mode set\n");
561 drm_mode_debug_printmodeline(&set->crtc->mode);
562 drm_mode_debug_printmodeline(set->mode);
563 mode_changed = true;
566 /* a) traverse passed in connector list and get encoders for them */
567 count = 0;
568 drm_for_each_connector(connector, dev) {
569 const struct drm_connector_helper_funcs *connector_funcs =
570 connector->helper_private;
571 new_encoder = connector->encoder;
572 for (ro = 0; ro < set->num_connectors; ro++) {
573 if (set->connectors[ro] == connector) {
574 new_encoder = connector_funcs->best_encoder(connector);
575 /* if we can't get an encoder for a connector
576 we are setting now - then fail */
577 if (new_encoder == NULL)
578 /* don't break so fail path works correct */
579 fail = 1;
581 if (connector->dpms != DRM_MODE_DPMS_ON) {
582 DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
583 mode_changed = true;
586 break;
590 if (new_encoder != connector->encoder) {
591 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
592 mode_changed = true;
593 /* If the encoder is reused for another connector, then
594 * the appropriate crtc will be set later.
596 if (connector->encoder)
597 connector->encoder->crtc = NULL;
598 connector->encoder = new_encoder;
602 if (fail) {
603 ret = -EINVAL;
604 goto fail;
607 count = 0;
608 drm_for_each_connector(connector, dev) {
609 if (!connector->encoder)
610 continue;
612 if (connector->encoder->crtc == set->crtc)
613 new_crtc = NULL;
614 else
615 new_crtc = connector->encoder->crtc;
617 for (ro = 0; ro < set->num_connectors; ro++) {
618 if (set->connectors[ro] == connector)
619 new_crtc = set->crtc;
622 /* Make sure the new CRTC will work with the encoder */
623 if (new_crtc &&
624 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
625 ret = -EINVAL;
626 goto fail;
628 if (new_crtc != connector->encoder->crtc) {
629 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
630 mode_changed = true;
631 connector->encoder->crtc = new_crtc;
633 if (new_crtc) {
634 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
635 connector->base.id, connector->name,
636 new_crtc->base.id);
637 } else {
638 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
639 connector->base.id, connector->name);
643 /* mode_set_base is not a required function */
644 if (fb_changed && !crtc_funcs->mode_set_base)
645 mode_changed = true;
647 if (mode_changed) {
648 if (drm_helper_crtc_in_use(set->crtc)) {
649 DRM_DEBUG_KMS("attempting to set mode from"
650 " userspace\n");
651 drm_mode_debug_printmodeline(set->mode);
652 set->crtc->primary->fb = set->fb;
653 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
654 set->x, set->y,
655 save_set.fb)) {
656 DRM_ERROR("failed to set mode on [CRTC:%d]\n",
657 set->crtc->base.id);
658 set->crtc->primary->fb = save_set.fb;
659 ret = -EINVAL;
660 goto fail;
662 DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
663 for (i = 0; i < set->num_connectors; i++) {
664 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
665 set->connectors[i]->name);
666 set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
669 __drm_helper_disable_unused_functions(dev);
670 } else if (fb_changed) {
671 set->crtc->x = set->x;
672 set->crtc->y = set->y;
673 set->crtc->primary->fb = set->fb;
674 ret = crtc_funcs->mode_set_base(set->crtc,
675 set->x, set->y, save_set.fb);
676 if (ret != 0) {
677 set->crtc->x = save_set.x;
678 set->crtc->y = save_set.y;
679 set->crtc->primary->fb = save_set.fb;
680 goto fail;
684 kfree(save_connectors);
685 kfree(save_encoders);
686 return 0;
688 fail:
689 /* Restore all previous data. */
690 count = 0;
691 drm_for_each_encoder(encoder, dev) {
692 *encoder = save_encoders[count++];
695 count = 0;
696 drm_for_each_connector(connector, dev) {
697 *connector = save_connectors[count++];
700 /* Try to restore the config */
701 if (mode_changed &&
702 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
703 save_set.y, save_set.fb))
704 DRM_ERROR("failed to restore config after modeset failure\n");
706 kfree(save_connectors);
707 kfree(save_encoders);
708 return ret;
710 EXPORT_SYMBOL(drm_crtc_helper_set_config);
712 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
714 int dpms = DRM_MODE_DPMS_OFF;
715 struct drm_connector *connector;
716 struct drm_device *dev = encoder->dev;
718 drm_for_each_connector(connector, dev)
719 if (connector->encoder == encoder)
720 if (connector->dpms < dpms)
721 dpms = connector->dpms;
722 return dpms;
725 /* Helper which handles bridge ordering around encoder dpms */
726 static void drm_helper_encoder_dpms(struct drm_encoder *encoder, int mode)
728 struct drm_bridge *bridge = encoder->bridge;
729 const struct drm_encoder_helper_funcs *encoder_funcs;
731 if (mode == DRM_MODE_DPMS_ON)
732 drm_bridge_pre_enable(bridge);
733 else
734 drm_bridge_disable(bridge);
736 encoder_funcs = encoder->helper_private;
737 if (encoder_funcs->dpms)
738 encoder_funcs->dpms(encoder, mode);
740 if (mode == DRM_MODE_DPMS_ON)
741 drm_bridge_enable(bridge);
742 else
743 drm_bridge_post_disable(bridge);
746 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
748 int dpms = DRM_MODE_DPMS_OFF;
749 struct drm_connector *connector;
750 struct drm_device *dev = crtc->dev;
752 drm_for_each_connector(connector, dev)
753 if (connector->encoder && connector->encoder->crtc == crtc)
754 if (connector->dpms < dpms)
755 dpms = connector->dpms;
756 return dpms;
760 * drm_helper_connector_dpms() - connector dpms helper implementation
761 * @connector: affected connector
762 * @mode: DPMS mode
764 * This is the main helper function provided by the crtc helper framework for
765 * implementing the DPMS connector attribute. It computes the new desired DPMS
766 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
767 * callback provided by the driver appropriately.
769 * Returns:
770 * Always returns 0.
772 int drm_helper_connector_dpms(struct drm_connector *connector, int mode)
774 struct drm_encoder *encoder = connector->encoder;
775 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
776 int old_dpms, encoder_dpms = DRM_MODE_DPMS_OFF;
778 if (mode == connector->dpms)
779 return 0;
781 old_dpms = connector->dpms;
782 connector->dpms = mode;
784 if (encoder)
785 encoder_dpms = drm_helper_choose_encoder_dpms(encoder);
787 /* from off to on, do crtc then encoder */
788 if (mode < old_dpms) {
789 if (crtc) {
790 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
791 if (crtc_funcs->dpms)
792 (*crtc_funcs->dpms) (crtc,
793 drm_helper_choose_crtc_dpms(crtc));
795 if (encoder)
796 drm_helper_encoder_dpms(encoder, encoder_dpms);
799 /* from on to off, do encoder then crtc */
800 if (mode > old_dpms) {
801 if (encoder)
802 drm_helper_encoder_dpms(encoder, encoder_dpms);
803 if (crtc) {
804 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
805 if (crtc_funcs->dpms)
806 (*crtc_funcs->dpms) (crtc,
807 drm_helper_choose_crtc_dpms(crtc));
811 return 0;
813 EXPORT_SYMBOL(drm_helper_connector_dpms);
816 * drm_helper_mode_fill_fb_struct - fill out framebuffer metadata
817 * @fb: drm_framebuffer object to fill out
818 * @mode_cmd: metadata from the userspace fb creation request
820 * This helper can be used in a drivers fb_create callback to pre-fill the fb's
821 * metadata fields.
823 void drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
824 struct drm_mode_fb_cmd2 *mode_cmd)
826 int i;
828 fb->width = mode_cmd->width;
829 fb->height = mode_cmd->height;
830 for (i = 0; i < 4; i++) {
831 fb->pitches[i] = mode_cmd->pitches[i];
832 fb->offsets[i] = mode_cmd->offsets[i];
833 fb->modifier[i] = mode_cmd->modifier[i];
835 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
836 &fb->bits_per_pixel);
837 fb->pixel_format = mode_cmd->pixel_format;
838 fb->flags = mode_cmd->flags;
840 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
843 * drm_helper_resume_force_mode - force-restore mode setting configuration
844 * @dev: drm_device which should be restored
846 * Drivers which use the mode setting helpers can use this function to
847 * force-restore the mode setting configuration e.g. on resume or when something
848 * else might have trampled over the hw state (like some overzealous old BIOSen
849 * tended to do).
851 * This helper doesn't provide a error return value since restoring the old
852 * config should never fail due to resource allocation issues since the driver
853 * has successfully set the restored configuration already. Hence this should
854 * boil down to the equivalent of a few dpms on calls, which also don't provide
855 * an error code.
857 * Drivers where simply restoring an old configuration again might fail (e.g.
858 * due to slight differences in allocating shared resources when the
859 * configuration is restored in a different order than when userspace set it up)
860 * need to use their own restore logic.
862 void drm_helper_resume_force_mode(struct drm_device *dev)
864 struct drm_crtc *crtc;
865 struct drm_encoder *encoder;
866 const struct drm_crtc_helper_funcs *crtc_funcs;
867 int encoder_dpms;
868 bool ret;
870 drm_modeset_lock_all(dev);
871 drm_for_each_crtc(crtc, dev) {
873 if (!crtc->enabled)
874 continue;
876 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
877 crtc->x, crtc->y, crtc->primary->fb);
879 /* Restoring the old config should never fail! */
880 if (ret == false)
881 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
883 /* Turn off outputs that were already powered off */
884 if (drm_helper_choose_crtc_dpms(crtc)) {
885 drm_for_each_encoder(encoder, dev) {
887 if(encoder->crtc != crtc)
888 continue;
890 encoder_dpms = drm_helper_choose_encoder_dpms(
891 encoder);
893 drm_helper_encoder_dpms(encoder, encoder_dpms);
896 crtc_funcs = crtc->helper_private;
897 if (crtc_funcs->dpms)
898 (*crtc_funcs->dpms) (crtc,
899 drm_helper_choose_crtc_dpms(crtc));
903 /* disable the unused connectors while restoring the modesetting */
904 __drm_helper_disable_unused_functions(dev);
905 drm_modeset_unlock_all(dev);
907 EXPORT_SYMBOL(drm_helper_resume_force_mode);
910 * drm_helper_crtc_mode_set - mode_set implementation for atomic plane helpers
911 * @crtc: DRM CRTC
912 * @mode: DRM display mode which userspace requested
913 * @adjusted_mode: DRM display mode adjusted by ->mode_fixup callbacks
914 * @x: x offset of the CRTC scanout area on the underlying framebuffer
915 * @y: y offset of the CRTC scanout area on the underlying framebuffer
916 * @old_fb: previous framebuffer
918 * This function implements a callback useable as the ->mode_set callback
919 * required by the crtc helpers. Besides the atomic plane helper functions for
920 * the primary plane the driver must also provide the ->mode_set_nofb callback
921 * to set up the crtc.
923 * This is a transitional helper useful for converting drivers to the atomic
924 * interfaces.
926 int drm_helper_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
927 struct drm_display_mode *adjusted_mode, int x, int y,
928 struct drm_framebuffer *old_fb)
930 struct drm_crtc_state *crtc_state;
931 const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
932 int ret;
934 if (crtc->funcs->atomic_duplicate_state)
935 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
936 else {
937 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
938 if (!crtc_state)
939 return -ENOMEM;
940 if (crtc->state)
941 __drm_atomic_helper_crtc_duplicate_state(crtc, crtc_state);
942 else
943 crtc_state->crtc = crtc;
946 crtc_state->planes_changed = true;
947 crtc_state->mode_changed = true;
948 ret = drm_atomic_set_mode_for_crtc(crtc_state, mode);
949 if (ret)
950 goto out;
951 drm_mode_copy(&crtc_state->adjusted_mode, adjusted_mode);
953 if (crtc_funcs->atomic_check) {
954 ret = crtc_funcs->atomic_check(crtc, crtc_state);
955 if (ret)
956 goto out;
959 swap(crtc->state, crtc_state);
961 crtc_funcs->mode_set_nofb(crtc);
963 ret = drm_helper_crtc_mode_set_base(crtc, x, y, old_fb);
965 out:
966 if (crtc->funcs->atomic_destroy_state)
967 crtc->funcs->atomic_destroy_state(crtc, crtc_state);
968 else {
969 __drm_atomic_helper_crtc_destroy_state(crtc, crtc_state);
970 kfree(crtc_state);
973 return ret;
975 EXPORT_SYMBOL(drm_helper_crtc_mode_set);
978 * drm_helper_crtc_mode_set_base - mode_set_base implementation for atomic plane helpers
979 * @crtc: DRM CRTC
980 * @x: x offset of the CRTC scanout area on the underlying framebuffer
981 * @y: y offset of the CRTC scanout area on the underlying framebuffer
982 * @old_fb: previous framebuffer
984 * This function implements a callback useable as the ->mode_set_base used
985 * required by the crtc helpers. The driver must provide the atomic plane helper
986 * functions for the primary plane.
988 * This is a transitional helper useful for converting drivers to the atomic
989 * interfaces.
991 int drm_helper_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
992 struct drm_framebuffer *old_fb)
994 struct drm_plane_state *plane_state;
995 struct drm_plane *plane = crtc->primary;
997 if (plane->funcs->atomic_duplicate_state)
998 plane_state = plane->funcs->atomic_duplicate_state(plane);
999 else if (plane->state)
1000 plane_state = drm_atomic_helper_plane_duplicate_state(plane);
1001 else
1002 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
1003 if (!plane_state)
1004 return -ENOMEM;
1005 plane_state->plane = plane;
1007 plane_state->crtc = crtc;
1008 drm_atomic_set_fb_for_plane(plane_state, crtc->primary->fb);
1009 plane_state->crtc_x = 0;
1010 plane_state->crtc_y = 0;
1011 plane_state->crtc_h = crtc->mode.vdisplay;
1012 plane_state->crtc_w = crtc->mode.hdisplay;
1013 plane_state->src_x = x << 16;
1014 plane_state->src_y = y << 16;
1015 plane_state->src_h = crtc->mode.vdisplay << 16;
1016 plane_state->src_w = crtc->mode.hdisplay << 16;
1018 return drm_plane_helper_commit(plane, plane_state, old_fb);
1020 EXPORT_SYMBOL(drm_helper_crtc_mode_set_base);