1 /* (c) 2006-2007 Intel Corporation
2 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * DRM core CRTC related functions
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 * Eric Anholt <eric@anholt.net>
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
33 #include "drm_crtc_helper.h"
36 * Detailed mode info for a standard 640x480@60Hz monitor
38 static struct drm_display_mode std_mode[] = {
39 { DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 25200, 640, 656,
40 752, 800, 0, 480, 490, 492, 525, 0,
41 V_NHSYNC | V_NVSYNC) }, /* 640x480@60Hz */
45 * drm_helper_probe_connector_modes - get complete set of display modes
47 * @maxX: max width for modes
48 * @maxY: max height for modes
51 * Caller must hold mode config lock.
53 * Based on @dev's mode_config layout, scan all the connectors and try to detect
54 * modes on them. Modes will first be added to the connector's probed_modes
55 * list, then culled (based on validity and the @maxX, @maxY parameters) and
56 * put into the normal modes list.
58 * Intended to be used either at bootup time or when major configuration
59 * changes have occurred.
61 * FIXME: take into account monitor limits
63 void drm_helper_probe_single_connector_modes(struct drm_connector *connector, uint32_t maxX, uint32_t maxY)
65 struct drm_device *dev = connector->dev;
66 struct drm_display_mode *mode, *t;
67 struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
70 DRM_DEBUG("%s\n", drm_get_connector_name(connector));
71 /* set all modes to the unverified state */
72 list_for_each_entry_safe(mode, t, &connector->modes, head)
73 mode->status = MODE_UNVERIFIED;
75 connector->status = (*connector->funcs->detect)(connector);
77 if (connector->status == connector_status_disconnected) {
78 DRM_DEBUG("%s is disconnected\n", drm_get_connector_name(connector));
79 /* TODO set EDID to NULL */
83 ret = (*connector_funcs->get_modes)(connector);
86 drm_mode_connector_list_update(connector);
90 drm_mode_validate_size(dev, &connector->modes, maxX,
92 list_for_each_entry_safe(mode, t, &connector->modes, head) {
93 if (mode->status == MODE_OK)
94 mode->status = (*connector_funcs->mode_valid)(connector,mode);
98 drm_mode_prune_invalid(dev, &connector->modes, TRUE);
100 if (list_empty(&connector->modes)) {
101 struct drm_display_mode *stdmode;
103 DRM_DEBUG("No valid modes on %s\n", drm_get_connector_name(connector));
105 /* Should we do this here ???
106 * When no valid EDID modes are available we end up
107 * here and bailed in the past, now we add a standard
108 * 640x480@60Hz mode and carry on.
110 stdmode = drm_mode_duplicate(dev, &std_mode[0]);
111 drm_mode_probed_add(connector, stdmode);
112 drm_mode_list_concat(&connector->probed_modes,
115 DRM_DEBUG("Adding standard 640x480 @ 60Hz to %s\n",
116 drm_get_connector_name(connector));
119 drm_mode_sort(&connector->modes);
121 DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
122 list_for_each_entry_safe(mode, t, &connector->modes, head) {
123 mode->vrefresh = drm_mode_vrefresh(mode);
125 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
126 drm_mode_debug_printmodeline(mode);
129 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
131 void drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX, uint32_t maxY)
133 struct drm_connector *connector;
135 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
136 drm_helper_probe_single_connector_modes(connector, maxX, maxY);
139 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
143 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
144 * @crtc: CRTC to check
147 * Caller must hold mode config lock.
149 * Walk @crtc's DRM device's mode_config and see if it's in use.
152 * True if @crtc is part of the mode_config, false otherwise.
154 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
156 struct drm_encoder *encoder;
157 struct drm_device *dev = crtc->dev;
158 /* FIXME: Locking around list access? */
159 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
160 if (encoder->crtc == crtc)
164 EXPORT_SYMBOL(drm_helper_crtc_in_use);
167 * drm_disable_unused_functions - disable unused objects
171 * Caller must hold mode config lock.
173 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
174 * by calling its dpms function, which should power it off.
176 void drm_helper_disable_unused_functions(struct drm_device *dev)
178 struct drm_encoder *encoder;
179 struct drm_encoder_helper_funcs *encoder_funcs;
180 struct drm_crtc *crtc;
182 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
183 encoder_funcs = encoder->helper_private;
185 (*encoder_funcs->dpms)(encoder, DPMSModeOff);
188 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
189 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
190 crtc->enabled = drm_helper_crtc_in_use(crtc);
191 if (!crtc->enabled) {
192 crtc_funcs->dpms(crtc, DPMSModeOff);
197 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
200 * drm_pick_crtcs - pick crtcs for connector devices
204 * Caller must hold mode config lock.
206 static void drm_pick_crtcs (struct drm_device *dev)
209 struct drm_connector *connector, *connector_equal;
210 struct drm_encoder *encoder, *encoder_equal;
211 struct drm_crtc *crtc;
212 struct drm_display_mode *des_mode = NULL, *modes, *modes_equal;
213 struct drm_connector_helper_funcs *connector_funcs;
217 /* clean out all the encoder/crtc combos */
218 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
219 encoder->crtc = NULL;
222 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
223 connector_funcs = connector->helper_private;
224 connector->encoder = NULL;
226 /* Don't hook up connectors that are disconnected ??
228 * This is debateable. Do we want fixed /dev/fbX or
229 * dynamic on hotplug (need mode code for that though) ?
231 * If we don't hook up connectors now, then we only create
232 * /dev/fbX for the connector that's enabled, that's good as
233 * the users console will be on that connector.
235 * If we do hook up connectors that are disconnected now, then
236 * the user may end up having to muck about with the fbcon
237 * map flags to assign his console to the enabled connector. Ugh.
239 if (connector->status != connector_status_connected)
242 if (list_empty(&connector->modes))
247 list_for_each_entry(des_mode, &connector->modes, head) {
248 if (des_mode->type & DRM_MODE_TYPE_PREFERRED) {
254 /* No preferred mode, let's just select the first available */
257 list_for_each_entry(des_mode, &connector->modes, head) {
262 encoder = connector_funcs->best_encoder(connector);
266 connector->encoder = encoder;
269 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
273 if ((encoder->possible_crtcs & (1 << c)) == 0)
276 list_for_each_entry(encoder_equal, &dev->mode_config.encoder_list, head) {
277 if (encoder->base.id == encoder_equal->base.id)
280 /* Find out if crtc has been assigned before */
281 if (encoder_equal->crtc == crtc)
285 #if 1 /* continue for now */
291 list_for_each_entry(connector_equal, &dev->mode_config.connector_list, head) {
293 if (connector->base.id == connector_equal->base.id)
296 encoder_equal = connector_equal->encoder;
301 list_for_each_entry(modes, &connector->modes, head) {
302 list_for_each_entry(modes_equal, &connector_equal->modes, head) {
303 if (drm_mode_equal (modes, modes_equal)) {
304 if ((encoder->possible_clones & encoder_equal->possible_clones) && (connector_equal->encoder->crtc == crtc)) {
305 printk("Cloning %s (0x%x) to %s (0x%x)\n",drm_get_connector_name(connector),encoder->possible_clones,drm_get_connector_name(connector_equal),encoder_equal->possible_clones);
316 /* crtc has been assigned skip it */
320 /* Found a CRTC to attach to, do it ! */
321 encoder->crtc = crtc;
322 encoder->crtc->desired_mode = des_mode;
323 connector->initial_x = 0;
324 connector->initial_y = 0;
325 DRM_DEBUG("Desired mode for CRTC %d is 0x%x:%s\n",c,des_mode->base.id, des_mode->name);
330 EXPORT_SYMBOL(drm_pick_crtcs);
333 * drm_crtc_set_mode - set a mode
334 * @crtc: CRTC to program
340 * Caller must hold mode config lock.
342 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
343 * to fixup or reject the mode prior to trying to set it.
346 * True if the mode was set successfully, or false otherwise.
348 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
351 struct drm_device *dev = crtc->dev;
352 struct drm_display_mode *adjusted_mode, saved_mode;
353 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
354 struct drm_encoder_helper_funcs *encoder_funcs;
355 int saved_x, saved_y;
356 struct drm_encoder *encoder;
359 adjusted_mode = drm_mode_duplicate(dev, mode);
361 crtc->enabled = drm_helper_crtc_in_use(crtc);
366 saved_mode = crtc->mode;
370 /* Update crtc values up front so the driver can rely on them for mode
377 if (drm_mode_equal(&saved_mode, &crtc->mode)) {
378 if (saved_x != crtc->x || saved_y != crtc->y) {
379 crtc_funcs->mode_set_base(crtc, crtc->x, crtc->y);
384 /* Pass our mode to the connectors and the CRTC to give them a chance to
385 * adjust it according to limitations or connector properties, and also
386 * a chance to reject the mode entirely.
388 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
390 if (encoder->crtc != crtc)
392 encoder_funcs = encoder->helper_private;
393 if (!(ret = encoder_funcs->mode_fixup(encoder, mode, adjusted_mode))) {
398 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
402 /* Prepare the encoders and CRTCs before setting the mode. */
403 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
405 if (encoder->crtc != crtc)
407 encoder_funcs = encoder->helper_private;
408 /* Disable the encoders as the first thing we do. */
409 encoder_funcs->prepare(encoder);
412 crtc_funcs->prepare(crtc);
414 /* Set up the DPLL and any encoders state that needs to adjust or depend
417 crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y);
419 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
421 if (encoder->crtc != crtc)
424 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder), mode->name, mode->base.id);
425 encoder_funcs = encoder->helper_private;
426 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
429 /* Now, enable the clocks, plane, pipe, and connectors that we set up. */
430 crtc_funcs->commit(crtc);
432 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
434 if (encoder->crtc != crtc)
437 encoder_funcs = encoder->helper_private;
438 encoder_funcs->commit(encoder);
442 /* XXX free adjustedmode */
443 drm_mode_destroy(dev, adjusted_mode);
445 // if (scrn->pScreen)
446 // drm_crtc_set_screen_sub_pixel_order(dev);
450 crtc->mode = saved_mode;
457 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
461 * drm_crtc_helper_set_config - set a new config from userspace
462 * @crtc: CRTC to setup
463 * @crtc_info: user provided configuration
464 * @new_mode: new mode to set
465 * @connector_set: set of connectors for the new config
466 * @fb: new framebuffer
469 * Caller must hold mode config lock.
471 * Setup a new configuration, provided by the user in @crtc_info, and enable
477 int drm_crtc_helper_set_config(struct drm_mode_set *set)
479 struct drm_device *dev;
480 struct drm_crtc **save_crtcs, *new_crtc;
481 struct drm_encoder **save_encoders, *new_encoder;
483 bool changed = false;
484 bool flip_or_move = false;
485 struct drm_connector *connector;
486 int count = 0, ro, fail = 0;
487 struct drm_crtc_helper_funcs *crtc_funcs;
498 if (!set->crtc->helper_private)
501 crtc_funcs = set->crtc->helper_private;
503 DRM_DEBUG("crtc: %p fb: %p connectors: %p num_connectors: %i (x, y) (%i, %i)\n", set->crtc, set->fb, set->connectors, set->num_connectors, set->x, set->y);
505 dev = set->crtc->dev;
507 /* save previous config */
508 save_enabled = set->crtc->enabled;
510 /* this is meant to be num_connector not num_crtc */
511 save_crtcs = kzalloc(dev->mode_config.num_connector * sizeof(struct drm_crtc *), GFP_KERNEL);
515 save_encoders = kzalloc(dev->mode_config.num_connector * sizeof(struct drm_encoders *), GFP_KERNEL);
516 if (!save_encoders) {
521 /* We should be able to check here if the fb has the same properties
522 * and then just flip_or_move it */
523 if (set->crtc->fb != set->fb) {
524 /* if we have no fb then its a change not a flip */
525 if (set->crtc->fb == NULL)
531 if (set->x != set->crtc->x || set->y != set->crtc->y)
534 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
535 DRM_DEBUG("modes are different\n");
536 drm_mode_debug_printmodeline(&set->crtc->mode);
537 drm_mode_debug_printmodeline(set->mode);
541 /* a) traverse passed in connector list and get encoders for them */
543 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
544 struct drm_connector_helper_funcs *connector_funcs = connector->helper_private;
545 save_encoders[count++] = connector->encoder;
546 new_encoder = connector->encoder;
547 for (ro = 0; ro < set->num_connectors; ro++) {
548 if (set->connectors[ro] == connector) {
549 new_encoder = connector_funcs->best_encoder(connector);
550 /* if we can't get an encoder for a connector
551 we are setting now - then fail */
552 if (new_encoder == NULL)
553 /* don't break so fail path works correct */
559 if (new_encoder != connector->encoder) {
561 connector->encoder = new_encoder;
567 goto fail_no_encoder;
570 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
571 if (!connector->encoder)
574 save_crtcs[count++] = connector->encoder->crtc;
576 if (connector->encoder->crtc == set->crtc)
579 new_crtc = connector->encoder->crtc;
581 for (ro = 0; ro < set->num_connectors; ro++) {
582 if (set->connectors[ro] == connector)
583 new_crtc = set->crtc;
585 if (new_crtc != connector->encoder->crtc) {
587 connector->encoder->crtc = new_crtc;
591 /* mode_set_base is not a required function */
592 if (flip_or_move && !crtc_funcs->mode_set_base)
596 set->crtc->fb = set->fb;
597 set->crtc->enabled = (set->mode != NULL);
598 if (set->mode != NULL) {
599 DRM_DEBUG("attempting to set mode from userspace\n");
600 drm_mode_debug_printmodeline(set->mode);
601 if (!drm_crtc_helper_set_mode(set->crtc, set->mode, set->x,
606 /* TODO are these needed? */
607 set->crtc->desired_x = set->x;
608 set->crtc->desired_y = set->y;
609 set->crtc->desired_mode = set->mode;
611 drm_helper_disable_unused_functions(dev);
612 } else if (flip_or_move) {
613 if (set->crtc->fb != set->fb)
614 set->crtc->fb = set->fb;
615 crtc_funcs->mode_set_base(set->crtc, set->x, set->y);
618 kfree(save_encoders);
623 set->crtc->enabled = save_enabled;
625 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
626 connector->encoder->crtc = save_crtcs[count++];
630 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
631 connector->encoder = save_encoders[count++];
633 kfree(save_encoders);
638 EXPORT_SYMBOL(drm_crtc_helper_set_config);
640 bool drm_helper_plugged_event(struct drm_device *dev)
644 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width, dev->mode_config.max_height);
648 /* alert the driver fb layer */
649 dev->mode_config.funcs->fb_changed(dev);
651 drm_helper_disable_unused_functions(dev);
653 drm_sysfs_hotplug_event(dev);
657 * drm_initial_config - setup a sane initial connector configuration
659 * @can_grow: this configuration is growable
662 * Called at init time, must take mode config lock.
664 * Scan the CRTCs and connectors and try to put together an initial setup.
665 * At the moment, this is a cloned configuration across all heads with
666 * a new framebuffer object as the backing store.
669 * Zero if everything went ok, nonzero otherwise.
671 bool drm_helper_initial_config(struct drm_device *dev, bool can_grow)
675 drm_helper_plugged_event(dev);
678 EXPORT_SYMBOL(drm_helper_initial_config);
681 * drm_hotplug_stage_two
683 * @connector hotpluged connector
686 * Caller must hold mode config lock, function might grab struct lock.
688 * Stage two of a hotplug.
691 * Zero on success, errno on failure.
693 int drm_helper_hotplug_stage_two(struct drm_device *dev)
695 dev->mode_config.hotplug_counter++;
697 drm_helper_plugged_event(dev);
701 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
703 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
704 struct drm_mode_fb_cmd *mode_cmd)
706 fb->width = mode_cmd->width;
707 fb->height = mode_cmd->height;
708 fb->pitch = mode_cmd->pitch;
709 fb->bits_per_pixel = mode_cmd->bpp;
710 fb->depth = mode_cmd->depth;
711 fb->mm_handle = mode_cmd->handle;
715 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
718 * drm_get_buffer_object - find the buffer object for a given handle
720 * @bo: pointer to caller's buffer_object pointer
721 * @handle: handle to lookup
724 * Must take @dev's struct_mutex to protect buffer object lookup.
726 * Given @handle, lookup the buffer object in @dev and put it in the caller's
730 * Zero on success, -EINVAL if the handle couldn't be found.
732 int drm_get_buffer_object(struct drm_device *dev, struct drm_buffer_object **bo, unsigned long handle)
734 struct drm_user_object *uo;
735 struct drm_hash_item *hash;
740 mutex_lock(&dev->struct_mutex);
741 ret = drm_ht_find_item(&dev->object_hash, handle, &hash);
743 DRM_ERROR("Couldn't find handle.\n");
748 uo = drm_hash_entry(hash, struct drm_user_object, hash);
749 if (uo->type != drm_buffer_type) {
754 *bo = drm_user_object_entry(uo, struct drm_buffer_object, base);
757 mutex_unlock(&dev->struct_mutex);
760 EXPORT_SYMBOL(drm_get_buffer_object);