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 a standard 640x480@60Hz monitor
39 static struct drm_display_mode std_mode[] = {
40 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
41 752, 800, 0, 480, 490, 492, 525, 0,
42 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
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,
437 struct drm_device *dev = crtc->dev;
438 struct drm_display_mode *adjusted_mode, saved_mode;
439 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
440 struct drm_encoder_helper_funcs *encoder_funcs;
441 int saved_x, saved_y;
442 struct drm_encoder *encoder;
445 adjusted_mode = drm_mode_duplicate(dev, mode);
447 crtc->enabled = drm_helper_crtc_in_use(crtc);
452 saved_mode = crtc->mode;
456 /* Update crtc values up front so the driver can rely on them for mode
463 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
464 if (saved_x != crtc->x || saved_y != crtc->y) {
465 crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y);
470 /* Pass our mode to the connectors and the CRTC to give them a chance to
471 * adjust it according to limitations or connector properties, and also
472 * a chance to reject the mode entirely.
474 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
476 if (encoder->crtc != crtc)
478 encoder_funcs = encoder->helper_private;
479 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
485 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
489 /* Prepare the encoders and CRTCs before setting the mode. */
490 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
492 if (encoder->crtc != crtc)
494 encoder_funcs = encoder->helper_private;
495 /* Disable the encoders as the first thing we do. */
496 encoder_funcs->prepare(encoder);
499 crtc_funcs->prepare(crtc);
501 /* Set up the DPLL and any encoders state that needs to adjust or depend
504 crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y);
506 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
508 if (encoder->crtc != crtc)
511 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
512 mode->name, mode->base.id);
513 encoder_funcs = encoder->helper_private;
514 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
517 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
518 crtc_funcs->commit(crtc);
520 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
522 if (encoder->crtc != crtc)
525 encoder_funcs = encoder->helper_private;
526 encoder_funcs->commit(encoder);
530 /* XXX free adjustedmode */
531 drm_mode_destroy(dev, adjusted_mode);
532 /* FIXME: add subpixel order */
535 crtc->mode = saved_mode;
542 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
546 * drm_crtc_helper_set_config - set a new config from userspace
547 * @crtc: CRTC to setup
548 * @crtc_info: user provided configuration
549 * @new_mode: new mode to set
550 * @connector_set: set of connectors for the new config
551 * @fb: new framebuffer
554 * Caller must hold mode config lock.
556 * Setup a new configuration, provided by the user in @crtc_info, and enable
562 int drm_crtc_helper_set_config(struct drm_mode_set *set)
564 struct drm_device *dev;
565 struct drm_crtc **save_crtcs, *new_crtc;
566 struct drm_encoder **save_encoders, *new_encoder;
568 bool changed = false;
569 bool flip_or_move = false;
570 struct drm_connector *connector;
571 int count = 0, ro, fail = 0;
572 struct drm_crtc_helper_funcs *crtc_funcs;
583 if (!set->crtc->helper_private)
586 crtc_funcs = set->crtc->helper_private;
588 DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
589 set->crtc, set->crtc->base.id, set->fb, set->connectors,
590 (int)set->num_connectors, set->x, set->y);
592 dev = set->crtc->dev;
594 /* save previous config */
595 save_enabled = set->crtc->enabled;
597 /* this is meant to be num_connector not num_crtc */
598 save_crtcs = kzalloc(dev->mode_config.num_connector *
599 sizeof(struct drm_crtc *), GFP_KERNEL);
603 save_encoders = kzalloc(dev->mode_config.num_connector *
604 sizeof(struct drm_encoders *), GFP_KERNEL);
605 if (!save_encoders) {
610 /* We should be able to check here if the fb has the same properties
611 * and then just flip_or_move it */
612 if (set->crtc->fb != set->fb) {
613 /* if we have no fb then its a change not a flip */
614 if (set->crtc->fb == NULL)
620 if (set->x != set->crtc->x || set->y != set->crtc->y)
623 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
624 DRM_DEBUG("modes are different\n");
625 drm_mode_debug_printmodeline(&set->crtc->mode);
626 drm_mode_debug_printmodeline(set->mode);
630 /* a) traverse passed in connector list and get encoders for them */
632 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
633 struct drm_connector_helper_funcs *connector_funcs =
634 connector->helper_private;
635 save_encoders[count++] = connector->encoder;
636 new_encoder = connector->encoder;
637 for (ro = 0; ro < set->num_connectors; ro++) {
638 if (set->connectors[ro] == connector) {
639 new_encoder = connector_funcs->best_encoder(connector);
640 /* if we can't get an encoder for a connector
641 we are setting now - then fail */
642 if (new_encoder == NULL)
643 /* don't break so fail path works correct */
649 if (new_encoder != connector->encoder) {
651 connector->encoder = new_encoder;
657 goto fail_no_encoder;
661 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
662 if (!connector->encoder)
665 save_crtcs[count++] = connector->encoder->crtc;
667 if (connector->encoder->crtc == set->crtc)
670 new_crtc = connector->encoder->crtc;
672 for (ro = 0; ro < set->num_connectors; ro++) {
673 if (set->connectors[ro] == connector)
674 new_crtc = set->crtc;
676 if (new_crtc != connector->encoder->crtc) {
678 connector->encoder->crtc = new_crtc;
682 /* mode_set_base is not a required function */
683 if (flip_or_move && !crtc_funcs->mode_set_base)
687 set->crtc->fb = set->fb;
688 set->crtc->enabled = (set->mode != NULL);
689 if (set->mode != NULL) {
690 DRM_DEBUG("attempting to set mode from userspace\n");
691 drm_mode_debug_printmodeline(set->mode);
692 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
697 /* TODO are these needed? */
698 set->crtc->desired_x = set->x;
699 set->crtc->desired_y = set->y;
700 set->crtc->desired_mode = set->mode;
702 drm_helper_disable_unused_functions(dev);
703 } else if (flip_or_move) {
704 if (set->crtc->fb != set->fb)
705 set->crtc->fb = set->fb;
706 crtc_funcs->mode_set_base(set->crtc, set->x, set->y);
709 kfree(save_encoders);
714 set->crtc->enabled = save_enabled;
716 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
717 connector->encoder->crtc = save_crtcs[count++];
721 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
722 connector->encoder = save_encoders[count++];
724 kfree(save_encoders);
727 EXPORT_SYMBOL(drm_crtc_helper_set_config);
729 bool drm_helper_plugged_event(struct drm_device *dev)
733 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
734 dev->mode_config.max_height);
736 drm_setup_crtcs(dev);
738 /* alert the driver fb layer */
739 dev->mode_config.funcs->fb_changed(dev);
741 /* FIXME: send hotplug event */
745 * drm_initial_config - setup a sane initial connector configuration
747 * @can_grow: this configuration is growable
750 * Called at init time, must take mode config lock.
752 * Scan the CRTCs and connectors and try to put together an initial setup.
753 * At the moment, this is a cloned configuration across all heads with
754 * a new framebuffer object as the backing store.
757 * Zero if everything went ok, nonzero otherwise.
759 bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
763 drm_helper_plugged_event(dev);
766 EXPORT_SYMBOL(drm_helper_initial_config);
769 * drm_hotplug_stage_two
771 * @connector hotpluged connector
774 * Caller must hold mode config lock, function might grab struct lock.
776 * Stage two of a hotplug.
779 * Zero on success, errno on failure.
781 int drm_helper_hotplug_stage_two(struct drm_device *dev)
783 dev->mode_config.hotplug_counter++;
785 drm_helper_plugged_event(dev);
789 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
791 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
792 struct drm_mode_fb_cmd *mode_cmd)
794 fb->width = mode_cmd->width;
795 fb->height = mode_cmd->height;
796 fb->pitch = mode_cmd->pitch;
797 fb->bits_per_pixel = mode_cmd->bpp;
798 fb->depth = mode_cmd->depth;
802 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
804 int drm_helper_resume_force_mode(struct drm_device *dev)
806 struct drm_crtc *crtc;
809 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
814 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
818 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
822 EXPORT_SYMBOL(drm_helper_resume_force_mode);