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
27 * Eric Anholt <eric@anholt.net>
28 * Dave Airlie <airlied@linux.ie>
29 * Jesse Barnes <jesse.barnes@intel.com>
34 #include "drm_crtc_helper.h"
37 * Detailed mode info for 800x600@60Hz
39 static struct drm_display_mode std_mode[] = {
40 { DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
41 968, 1056, 0, 600, 601, 605, 628, 0,
42 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
46 * drm_helper_probe_connector_modes - get complete set of display modes
48 * @maxX: max width for modes
49 * @maxY: max height for modes
52 * Caller must hold mode config lock.
54 * Based on @dev's mode_config layout, scan all the connectors and try to detect
55 * modes on them. Modes will first be added to the connector's probed_modes
56 * list, then culled (based on validity and the @maxX, @maxY parameters) and
57 * put into the normal modes list.
59 * Intended to be used either at bootup time or when major configuration
60 * changes have occurred.
62 * FIXME: take into account monitor limits
64 void drm_helper_probe_single_connector_modes(struct drm_connector *connector,
65 uint32_t maxX, uint32_t maxY)
67 struct drm_device *dev = connector->dev;
68 struct drm_display_mode *mode, *t;
69 struct drm_connector_helper_funcs *connector_funcs =
70 connector->helper_private;
73 DRM_DEBUG("%s\n", drm_get_connector_name(connector));
74 /* set all modes to the unverified state */
75 list_for_each_entry_safe(mode, t, &connector->modes, head)
76 mode->status = MODE_UNVERIFIED;
78 connector->status = connector->funcs->detect(connector);
80 if (connector->status == connector_status_disconnected) {
81 DRM_DEBUG("%s is disconnected\n",
82 drm_get_connector_name(connector));
83 /* TODO set EDID to NULL */
87 ret = (*connector_funcs->get_modes)(connector);
90 drm_mode_connector_list_update(connector);
94 drm_mode_validate_size(dev, &connector->modes, maxX,
96 list_for_each_entry_safe(mode, t, &connector->modes, head) {
97 if (mode->status == MODE_OK)
98 mode->status = connector_funcs->mode_valid(connector,
103 drm_mode_prune_invalid(dev, &connector->modes, true);
105 if (list_empty(&connector->modes)) {
106 struct drm_display_mode *stdmode;
108 DRM_DEBUG("No valid modes on %s\n",
109 drm_get_connector_name(connector));
111 /* Should we do this here ???
112 * When no valid EDID modes are available we end up
113 * here and bailed in the past, now we add a standard
114 * 640x480@60Hz mode and carry on.
116 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
117 drm_mode_probed_add(connector, stdmode);
118 drm_mode_list_concat(&connector->probed_modes,
121 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
122 drm_get_connector_name(connector));
125 drm_mode_sort(&connector->modes);
127 DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
128 list_for_each_entry_safe(mode, t, &connector->modes, head) {
129 mode->vrefresh = drm_mode_vrefresh(mode);
131 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
132 drm_mode_debug_printmodeline(mode);
135 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
137 void drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
140 struct drm_connector *connector;
142 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
143 drm_helper_probe_single_connector_modes(connector, maxX, maxY);
146 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
150 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
151 * @crtc: CRTC to check
154 * Caller must hold mode config lock.
156 * Walk @crtc's DRM device's mode_config and see if it's in use.
159 * True if @crtc is part of the mode_config, false otherwise.
161 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
163 struct drm_encoder *encoder;
164 struct drm_device *dev = crtc->dev;
165 /* FIXME: Locking around list access? */
166 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
167 if (encoder->crtc == crtc)
171 EXPORT_SYMBOL(drm_helper_crtc_in_use);
174 * drm_disable_unused_functions - disable unused objects
178 * Caller must hold mode config lock.
180 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
181 * by calling its dpms function, which should power it off.
183 void drm_helper_disable_unused_functions(struct drm_device *dev)
185 struct drm_encoder *encoder;
186 struct drm_encoder_helper_funcs *encoder_funcs;
187 struct drm_crtc *crtc;
189 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
190 encoder_funcs = encoder->helper_private;
192 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
195 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
196 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
197 crtc->enabled = drm_helper_crtc_in_use(crtc);
198 if (!crtc->enabled) {
199 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
204 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
206 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
208 struct drm_display_mode *mode;
210 list_for_each_entry(mode, &connector->modes, head) {
211 if (drm_mode_width(mode) > width ||
212 drm_mode_height(mode) > height)
214 if (mode->type & DRM_MODE_TYPE_PREFERRED)
220 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
225 enable = connector->status == connector_status_connected;
227 enable = connector->status != connector_status_disconnected;
232 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
234 bool any_enabled = false;
235 struct drm_connector *connector;
238 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
239 enabled[i] = drm_connector_enabled(connector, true);
240 any_enabled |= enabled[i];
248 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
249 enabled[i] = drm_connector_enabled(connector, false);
254 static bool drm_target_preferred(struct drm_device *dev,
255 struct drm_display_mode **modes,
256 bool *enabled, int width, int height)
258 struct drm_connector *connector;
261 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
263 if (enabled[i] == false) {
268 modes[i] = drm_has_preferred_mode(connector, width, height);
270 list_for_each_entry(modes[i], &connector->modes, head)
278 static int drm_pick_crtcs(struct drm_device *dev,
279 struct drm_crtc **best_crtcs,
280 struct drm_display_mode **modes,
281 int n, int width, int height)
284 struct drm_connector *connector;
285 struct drm_connector_helper_funcs *connector_funcs;
286 struct drm_encoder *encoder;
287 struct drm_crtc *best_crtc;
288 int my_score, best_score, score;
289 struct drm_crtc **crtcs, *crtc;
291 if (n == dev->mode_config.num_connector)
294 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
300 best_crtcs[n] = NULL;
302 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
303 if (modes[n] == NULL)
306 crtcs = kmalloc(dev->mode_config.num_connector *
307 sizeof(struct drm_crtc *), GFP_KERNEL);
312 if (connector->status == connector_status_connected)
314 if (drm_has_preferred_mode(connector, width, height))
317 connector_funcs = connector->helper_private;
318 encoder = connector_funcs->best_encoder(connector);
322 connector->encoder = encoder;
324 /* select a crtc for this connector and then attempt to configure
325 remaining connectors */
327 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
329 if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
334 for (o = 0; o < n; o++)
335 if (best_crtcs[o] == crtc)
339 /* ignore cloning for now */
345 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
346 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
348 if (score > best_score) {
351 memcpy(best_crtcs, crtcs,
352 dev->mode_config.num_connector *
353 sizeof(struct drm_crtc *));
362 static void drm_setup_crtcs(struct drm_device *dev)
364 struct drm_crtc **crtcs;
365 struct drm_display_mode **modes;
366 struct drm_encoder *encoder;
367 struct drm_connector *connector;
372 width = dev->mode_config.max_width;
373 height = dev->mode_config.max_height;
375 /* clean out all the encoder/crtc combos */
376 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
377 encoder->crtc = NULL;
380 crtcs = kcalloc(dev->mode_config.num_connector,
381 sizeof(struct drm_crtc *), GFP_KERNEL);
382 modes = kcalloc(dev->mode_config.num_connector,
383 sizeof(struct drm_display_mode *), GFP_KERNEL);
384 enabled = kcalloc(dev->mode_config.num_connector,
385 sizeof(bool), GFP_KERNEL);
387 drm_enable_connectors(dev, enabled);
389 ret = drm_target_preferred(dev, modes, enabled, width, height);
391 DRM_ERROR("Unable to find initial modes\n");
393 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
396 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
397 struct drm_display_mode *mode = modes[i];
398 struct drm_crtc *crtc = crtcs[i];
400 if (connector->encoder == NULL) {
406 crtc->desired_mode = mode;
407 connector->encoder->crtc = crtc;
409 connector->encoder->crtc = NULL;
418 * drm_crtc_set_mode - set a mode
419 * @crtc: CRTC to program
425 * Caller must hold mode config lock.
427 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
428 * to fixup or reject the mode prior to trying to set it.
431 * True if the mode was set successfully, or false otherwise.
433 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
434 struct drm_display_mode *mode,
436 struct drm_framebuffer *old_fb)
438 struct drm_device *dev = crtc->dev;
439 struct drm_display_mode *adjusted_mode, saved_mode;
440 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
441 struct drm_encoder_helper_funcs *encoder_funcs;
442 int saved_x, saved_y;
443 struct drm_encoder *encoder;
446 adjusted_mode = drm_mode_duplicate(dev, mode);
448 crtc->enabled = drm_helper_crtc_in_use(crtc);
453 saved_mode = crtc->mode;
457 /* Update crtc values up front so the driver can rely on them for mode
464 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
465 if (saved_x != crtc->x || saved_y != crtc->y) {
466 crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y,
472 /* Pass our mode to the connectors and the CRTC to give them a chance to
473 * adjust it according to limitations or connector properties, and also
474 * a chance to reject the mode entirely.
476 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
478 if (encoder->crtc != crtc)
480 encoder_funcs = encoder->helper_private;
481 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
487 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
491 /* Prepare the encoders and CRTCs before setting the mode. */
492 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
494 if (encoder->crtc != crtc)
496 encoder_funcs = encoder->helper_private;
497 /* Disable the encoders as the first thing we do. */
498 encoder_funcs->prepare(encoder);
501 crtc_funcs->prepare(crtc);
503 /* Set up the DPLL and any encoders state that needs to adjust or depend
506 crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
508 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
510 if (encoder->crtc != crtc)
513 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
514 mode->name, mode->base.id);
515 encoder_funcs = encoder->helper_private;
516 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
519 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
520 crtc_funcs->commit(crtc);
522 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
524 if (encoder->crtc != crtc)
527 encoder_funcs = encoder->helper_private;
528 encoder_funcs->commit(encoder);
532 /* XXX free adjustedmode */
533 drm_mode_destroy(dev, adjusted_mode);
534 /* FIXME: add subpixel order */
537 crtc->mode = saved_mode;
544 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
548 * drm_crtc_helper_set_config - set a new config from userspace
549 * @crtc: CRTC to setup
550 * @crtc_info: user provided configuration
551 * @new_mode: new mode to set
552 * @connector_set: set of connectors for the new config
553 * @fb: new framebuffer
556 * Caller must hold mode config lock.
558 * Setup a new configuration, provided by the user in @crtc_info, and enable
564 int drm_crtc_helper_set_config(struct drm_mode_set *set)
566 struct drm_device *dev;
567 struct drm_crtc **save_crtcs, *new_crtc;
568 struct drm_encoder **save_encoders, *new_encoder;
569 struct drm_framebuffer *old_fb;
571 bool changed = false;
572 bool flip_or_move = false;
573 struct drm_connector *connector;
574 int count = 0, ro, fail = 0;
575 struct drm_crtc_helper_funcs *crtc_funcs;
586 if (!set->crtc->helper_private)
589 crtc_funcs = set->crtc->helper_private;
591 DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
592 set->crtc, set->crtc->base.id, set->fb, set->connectors,
593 (int)set->num_connectors, set->x, set->y);
595 dev = set->crtc->dev;
597 /* save previous config */
598 save_enabled = set->crtc->enabled;
600 /* this is meant to be num_connector not num_crtc */
601 save_crtcs = kzalloc(dev->mode_config.num_connector *
602 sizeof(struct drm_crtc *), GFP_KERNEL);
606 save_encoders = kzalloc(dev->mode_config.num_connector *
607 sizeof(struct drm_encoders *), GFP_KERNEL);
608 if (!save_encoders) {
613 /* We should be able to check here if the fb has the same properties
614 * and then just flip_or_move it */
615 if (set->crtc->fb != set->fb) {
616 /* if we have no fb then its a change not a flip */
617 if (set->crtc->fb == NULL)
623 if (set->x != set->crtc->x || set->y != set->crtc->y)
626 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
627 DRM_DEBUG("modes are different\n");
628 drm_mode_debug_printmodeline(&set->crtc->mode);
629 drm_mode_debug_printmodeline(set->mode);
633 /* a) traverse passed in connector list and get encoders for them */
635 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
636 struct drm_connector_helper_funcs *connector_funcs =
637 connector->helper_private;
638 save_encoders[count++] = connector->encoder;
639 new_encoder = connector->encoder;
640 for (ro = 0; ro < set->num_connectors; ro++) {
641 if (set->connectors[ro] == connector) {
642 new_encoder = connector_funcs->best_encoder(connector);
643 /* if we can't get an encoder for a connector
644 we are setting now - then fail */
645 if (new_encoder == NULL)
646 /* don't break so fail path works correct */
652 if (new_encoder != connector->encoder) {
654 connector->encoder = new_encoder;
660 goto fail_no_encoder;
664 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
665 if (!connector->encoder)
668 save_crtcs[count++] = connector->encoder->crtc;
670 if (connector->encoder->crtc == set->crtc)
673 new_crtc = connector->encoder->crtc;
675 for (ro = 0; ro < set->num_connectors; ro++) {
676 if (set->connectors[ro] == connector)
677 new_crtc = set->crtc;
679 if (new_crtc != connector->encoder->crtc) {
681 connector->encoder->crtc = new_crtc;
685 /* mode_set_base is not a required function */
686 if (flip_or_move && !crtc_funcs->mode_set_base)
690 old_fb = set->crtc->fb;
691 set->crtc->fb = set->fb;
692 set->crtc->enabled = (set->mode != NULL);
693 if (set->mode != NULL) {
694 DRM_DEBUG("attempting to set mode from userspace\n");
695 drm_mode_debug_printmodeline(set->mode);
696 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
702 /* TODO are these needed? */
703 set->crtc->desired_x = set->x;
704 set->crtc->desired_y = set->y;
705 set->crtc->desired_mode = set->mode;
707 drm_helper_disable_unused_functions(dev);
708 } else if (flip_or_move) {
709 old_fb = set->crtc->fb;
710 if (set->crtc->fb != set->fb)
711 set->crtc->fb = set->fb;
712 crtc_funcs->mode_set_base(set->crtc, set->x, set->y, old_fb);
715 kfree(save_encoders);
720 set->crtc->enabled = save_enabled;
722 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
723 connector->encoder->crtc = save_crtcs[count++];
727 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
728 connector->encoder = save_encoders[count++];
730 kfree(save_encoders);
733 EXPORT_SYMBOL(drm_crtc_helper_set_config);
735 bool drm_helper_plugged_event(struct drm_device *dev)
739 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
740 dev->mode_config.max_height);
742 drm_setup_crtcs(dev);
744 /* alert the driver fb layer */
745 dev->mode_config.funcs->fb_changed(dev);
747 /* FIXME: send hotplug event */
751 * drm_initial_config - setup a sane initial connector configuration
753 * @can_grow: this configuration is growable
756 * Called at init time, must take mode config lock.
758 * Scan the CRTCs and connectors and try to put together an initial setup.
759 * At the moment, this is a cloned configuration across all heads with
760 * a new framebuffer object as the backing store.
763 * Zero if everything went ok, nonzero otherwise.
765 bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
769 drm_helper_plugged_event(dev);
772 EXPORT_SYMBOL(drm_helper_initial_config);
775 * drm_hotplug_stage_two
777 * @connector hotpluged connector
780 * Caller must hold mode config lock, function might grab struct lock.
782 * Stage two of a hotplug.
785 * Zero on success, errno on failure.
787 int drm_helper_hotplug_stage_two(struct drm_device *dev)
789 drm_helper_plugged_event(dev);
793 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
795 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
796 struct drm_mode_fb_cmd *mode_cmd)
798 fb->width = mode_cmd->width;
799 fb->height = mode_cmd->height;
800 fb->pitch = mode_cmd->pitch;
801 fb->bits_per_pixel = mode_cmd->bpp;
802 fb->depth = mode_cmd->depth;
806 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
808 int drm_helper_resume_force_mode(struct drm_device *dev)
810 struct drm_crtc *crtc;
813 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
819 crtc->x, crtc->y, crtc->fb);
822 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
826 EXPORT_SYMBOL(drm_helper_resume_force_mode);