r8169: more broken register writes workaround
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpu / drm / drm_crtc_helper.c
blob08173fcdda4d511bb7bdfad6ac512d845f7d832d
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 "drmP.h"
33 #include "drm_crtc.h"
34 #include "drm_crtc_helper.h"
35 #include "drm_fb_helper.h"
37 static void drm_mode_validate_flag(struct drm_connector *connector,
38 int flags)
40 struct drm_display_mode *mode, *t;
42 if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
43 return;
45 list_for_each_entry_safe(mode, t, &connector->modes, head) {
46 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
47 !(flags & DRM_MODE_FLAG_INTERLACE))
48 mode->status = MODE_NO_INTERLACE;
49 if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
50 !(flags & DRM_MODE_FLAG_DBLSCAN))
51 mode->status = MODE_NO_DBLESCAN;
54 return;
57 /**
58 * drm_helper_probe_connector_modes - get complete set of display modes
59 * @dev: DRM device
60 * @maxX: max width for modes
61 * @maxY: max height for modes
63 * LOCKING:
64 * Caller must hold mode config lock.
66 * Based on @dev's mode_config layout, scan all the connectors and try to detect
67 * modes on them. Modes will first be added to the connector's probed_modes
68 * list, then culled (based on validity and the @maxX, @maxY parameters) and
69 * put into the normal modes list.
71 * Intended to be used either at bootup time or when major configuration
72 * changes have occurred.
74 * FIXME: take into account monitor limits
76 * RETURNS:
77 * Number of modes found on @connector.
79 int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
80 uint32_t maxX, uint32_t maxY)
82 struct drm_device *dev = connector->dev;
83 struct drm_display_mode *mode, *t;
84 struct drm_connector_helper_funcs *connector_funcs =
85 connector->helper_private;
86 int count = 0;
87 int mode_flags = 0;
89 DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector));
90 /* set all modes to the unverified state */
91 list_for_each_entry_safe(mode, t, &connector->modes, head)
92 mode->status = MODE_UNVERIFIED;
94 if (connector->force) {
95 if (connector->force == DRM_FORCE_ON)
96 connector->status = connector_status_connected;
97 else
98 connector->status = connector_status_disconnected;
99 if (connector->funcs->force)
100 connector->funcs->force(connector);
101 } else
102 connector->status = connector->funcs->detect(connector);
104 if (connector->status == connector_status_disconnected) {
105 DRM_DEBUG_KMS("%s is disconnected\n",
106 drm_get_connector_name(connector));
107 drm_mode_connector_update_edid_property(connector, NULL);
108 goto prune;
111 count = (*connector_funcs->get_modes)(connector);
112 if (!count) {
113 count = drm_add_modes_noedid(connector, 800, 600);
114 if (!count)
115 return 0;
118 drm_mode_connector_list_update(connector);
120 if (maxX && maxY)
121 drm_mode_validate_size(dev, &connector->modes, maxX,
122 maxY, 0);
124 if (connector->interlace_allowed)
125 mode_flags |= DRM_MODE_FLAG_INTERLACE;
126 if (connector->doublescan_allowed)
127 mode_flags |= DRM_MODE_FLAG_DBLSCAN;
128 drm_mode_validate_flag(connector, mode_flags);
130 list_for_each_entry_safe(mode, t, &connector->modes, head) {
131 if (mode->status == MODE_OK)
132 mode->status = connector_funcs->mode_valid(connector,
133 mode);
136 prune:
137 drm_mode_prune_invalid(dev, &connector->modes, true);
139 if (list_empty(&connector->modes))
140 return 0;
142 drm_mode_sort(&connector->modes);
144 DRM_DEBUG_KMS("Probed modes for %s\n",
145 drm_get_connector_name(connector));
146 list_for_each_entry_safe(mode, t, &connector->modes, head) {
147 mode->vrefresh = drm_mode_vrefresh(mode);
149 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
150 drm_mode_debug_printmodeline(mode);
153 return count;
155 EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
157 int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
158 uint32_t maxY)
160 struct drm_connector *connector;
161 int count = 0;
163 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
164 count += drm_helper_probe_single_connector_modes(connector,
165 maxX, maxY);
168 return count;
170 EXPORT_SYMBOL(drm_helper_probe_connector_modes);
173 * drm_helper_encoder_in_use - check if a given encoder is in use
174 * @encoder: encoder to check
176 * LOCKING:
177 * Caller must hold mode config lock.
179 * Walk @encoders's DRM device's mode_config and see if it's in use.
181 * RETURNS:
182 * True if @encoder is part of the mode_config, false otherwise.
184 bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
186 struct drm_connector *connector;
187 struct drm_device *dev = encoder->dev;
188 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
189 if (connector->encoder == encoder)
190 return true;
191 return false;
193 EXPORT_SYMBOL(drm_helper_encoder_in_use);
196 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
197 * @crtc: CRTC to check
199 * LOCKING:
200 * Caller must hold mode config lock.
202 * Walk @crtc's DRM device's mode_config and see if it's in use.
204 * RETURNS:
205 * True if @crtc is part of the mode_config, false otherwise.
207 bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
209 struct drm_encoder *encoder;
210 struct drm_device *dev = crtc->dev;
211 /* FIXME: Locking around list access? */
212 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
213 if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
214 return true;
215 return false;
217 EXPORT_SYMBOL(drm_helper_crtc_in_use);
220 * drm_disable_unused_functions - disable unused objects
221 * @dev: DRM device
223 * LOCKING:
224 * Caller must hold mode config lock.
226 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
227 * by calling its dpms function, which should power it off.
229 void drm_helper_disable_unused_functions(struct drm_device *dev)
231 struct drm_encoder *encoder;
232 struct drm_connector *connector;
233 struct drm_encoder_helper_funcs *encoder_funcs;
234 struct drm_crtc *crtc;
236 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
237 if (!connector->encoder)
238 continue;
239 if (connector->status == connector_status_disconnected)
240 connector->encoder = NULL;
243 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
244 encoder_funcs = encoder->helper_private;
245 if (!drm_helper_encoder_in_use(encoder)) {
246 if (encoder_funcs->disable)
247 (*encoder_funcs->disable)(encoder);
248 else
249 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
250 /* disconnector encoder from any connector */
251 encoder->crtc = NULL;
255 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
257 crtc->enabled = drm_helper_crtc_in_use(crtc);
258 if (!crtc->enabled) {
259 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
260 crtc->fb = NULL;
264 EXPORT_SYMBOL(drm_helper_disable_unused_functions);
266 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
268 struct drm_display_mode *mode;
270 list_for_each_entry(mode, &connector->modes, head) {
271 if (drm_mode_width(mode) > width ||
272 drm_mode_height(mode) > height)
273 continue;
274 if (mode->type & DRM_MODE_TYPE_PREFERRED)
275 return mode;
277 return NULL;
280 static bool drm_has_cmdline_mode(struct drm_connector *connector)
282 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
283 struct drm_fb_helper_cmdline_mode *cmdline_mode;
285 if (!fb_help_conn)
286 return false;
288 cmdline_mode = &fb_help_conn->cmdline_mode;
289 return cmdline_mode->specified;
292 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
294 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
295 struct drm_fb_helper_cmdline_mode *cmdline_mode;
296 struct drm_display_mode *mode = NULL;
298 if (!fb_help_conn)
299 return mode;
301 cmdline_mode = &fb_help_conn->cmdline_mode;
302 if (cmdline_mode->specified == false)
303 return mode;
305 /* attempt to find a matching mode in the list of modes
306 * we have gotten so far, if not add a CVT mode that conforms
308 if (cmdline_mode->rb || cmdline_mode->margins)
309 goto create_mode;
311 list_for_each_entry(mode, &connector->modes, head) {
312 /* check width/height */
313 if (mode->hdisplay != cmdline_mode->xres ||
314 mode->vdisplay != cmdline_mode->yres)
315 continue;
317 if (cmdline_mode->refresh_specified) {
318 if (mode->vrefresh != cmdline_mode->refresh)
319 continue;
322 if (cmdline_mode->interlace) {
323 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
324 continue;
326 return mode;
329 create_mode:
330 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
331 cmdline_mode->yres,
332 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
333 cmdline_mode->rb, cmdline_mode->interlace,
334 cmdline_mode->margins);
335 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
336 list_add(&mode->head, &connector->modes);
337 return mode;
340 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
342 bool enable;
344 if (strict) {
345 enable = connector->status == connector_status_connected;
346 } else {
347 enable = connector->status != connector_status_disconnected;
349 return enable;
352 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
354 bool any_enabled = false;
355 struct drm_connector *connector;
356 int i = 0;
358 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
359 enabled[i] = drm_connector_enabled(connector, true);
360 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
361 enabled[i] ? "yes" : "no");
362 any_enabled |= enabled[i];
363 i++;
366 if (any_enabled)
367 return;
369 i = 0;
370 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
371 enabled[i] = drm_connector_enabled(connector, false);
372 i++;
376 static bool drm_target_preferred(struct drm_device *dev,
377 struct drm_display_mode **modes,
378 bool *enabled, int width, int height)
380 struct drm_connector *connector;
381 int i = 0;
383 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
385 if (enabled[i] == false) {
386 i++;
387 continue;
390 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
391 connector->base.id);
393 /* got for command line mode first */
394 modes[i] = drm_pick_cmdline_mode(connector, width, height);
395 if (!modes[i]) {
396 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
397 connector->base.id);
398 modes[i] = drm_has_preferred_mode(connector, width, height);
400 /* No preferred modes, pick one off the list */
401 if (!modes[i] && !list_empty(&connector->modes)) {
402 list_for_each_entry(modes[i], &connector->modes, head)
403 break;
405 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
406 "none");
407 i++;
409 return true;
412 static int drm_pick_crtcs(struct drm_device *dev,
413 struct drm_crtc **best_crtcs,
414 struct drm_display_mode **modes,
415 int n, int width, int height)
417 int c, o;
418 struct drm_connector *connector;
419 struct drm_connector_helper_funcs *connector_funcs;
420 struct drm_encoder *encoder;
421 struct drm_crtc *best_crtc;
422 int my_score, best_score, score;
423 struct drm_crtc **crtcs, *crtc;
425 if (n == dev->mode_config.num_connector)
426 return 0;
427 c = 0;
428 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
429 if (c == n)
430 break;
431 c++;
434 best_crtcs[n] = NULL;
435 best_crtc = NULL;
436 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
437 if (modes[n] == NULL)
438 return best_score;
440 crtcs = kmalloc(dev->mode_config.num_connector *
441 sizeof(struct drm_crtc *), GFP_KERNEL);
442 if (!crtcs)
443 return best_score;
445 my_score = 1;
446 if (connector->status == connector_status_connected)
447 my_score++;
448 if (drm_has_cmdline_mode(connector))
449 my_score++;
450 if (drm_has_preferred_mode(connector, width, height))
451 my_score++;
453 connector_funcs = connector->helper_private;
454 encoder = connector_funcs->best_encoder(connector);
455 if (!encoder)
456 goto out;
458 connector->encoder = encoder;
460 /* select a crtc for this connector and then attempt to configure
461 remaining connectors */
462 c = 0;
463 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
465 if ((encoder->possible_crtcs & (1 << c)) == 0) {
466 c++;
467 continue;
470 for (o = 0; o < n; o++)
471 if (best_crtcs[o] == crtc)
472 break;
474 if (o < n) {
475 /* ignore cloning for now */
476 c++;
477 continue;
480 crtcs[n] = crtc;
481 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
482 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
483 width, height);
484 if (score > best_score) {
485 best_crtc = crtc;
486 best_score = score;
487 memcpy(best_crtcs, crtcs,
488 dev->mode_config.num_connector *
489 sizeof(struct drm_crtc *));
491 c++;
493 out:
494 kfree(crtcs);
495 return best_score;
498 static void drm_setup_crtcs(struct drm_device *dev)
500 struct drm_crtc **crtcs;
501 struct drm_display_mode **modes;
502 struct drm_encoder *encoder;
503 struct drm_connector *connector;
504 bool *enabled;
505 int width, height;
506 int i, ret;
508 DRM_DEBUG_KMS("\n");
510 width = dev->mode_config.max_width;
511 height = dev->mode_config.max_height;
513 /* clean out all the encoder/crtc combos */
514 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
515 encoder->crtc = NULL;
518 crtcs = kcalloc(dev->mode_config.num_connector,
519 sizeof(struct drm_crtc *), GFP_KERNEL);
520 modes = kcalloc(dev->mode_config.num_connector,
521 sizeof(struct drm_display_mode *), GFP_KERNEL);
522 enabled = kcalloc(dev->mode_config.num_connector,
523 sizeof(bool), GFP_KERNEL);
525 drm_enable_connectors(dev, enabled);
527 ret = drm_target_preferred(dev, modes, enabled, width, height);
528 if (!ret)
529 DRM_ERROR("Unable to find initial modes\n");
531 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
533 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
535 i = 0;
536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
537 struct drm_display_mode *mode = modes[i];
538 struct drm_crtc *crtc = crtcs[i];
540 if (connector->encoder == NULL) {
541 i++;
542 continue;
545 if (mode && crtc) {
546 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
547 mode->name, crtc->base.id);
548 crtc->desired_mode = mode;
549 connector->encoder->crtc = crtc;
550 } else {
551 connector->encoder->crtc = NULL;
552 connector->encoder = NULL;
554 i++;
557 kfree(crtcs);
558 kfree(modes);
559 kfree(enabled);
563 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
564 * @encoder: encoder to test
565 * @crtc: crtc to test
567 * Return false if @encoder can't be driven by @crtc, true otherwise.
569 static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
570 struct drm_crtc *crtc)
572 struct drm_device *dev;
573 struct drm_crtc *tmp;
574 int crtc_mask = 1;
576 WARN(!crtc, "checking null crtc?");
578 dev = crtc->dev;
580 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
581 if (tmp == crtc)
582 break;
583 crtc_mask <<= 1;
586 if (encoder->possible_crtcs & crtc_mask)
587 return true;
588 return false;
592 * Check the CRTC we're going to map each output to vs. its current
593 * CRTC. If they don't match, we have to disable the output and the CRTC
594 * since the driver will have to re-route things.
596 static void
597 drm_crtc_prepare_encoders(struct drm_device *dev)
599 struct drm_encoder_helper_funcs *encoder_funcs;
600 struct drm_encoder *encoder;
602 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
603 encoder_funcs = encoder->helper_private;
604 /* Disable unused encoders */
605 if (encoder->crtc == NULL)
606 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
607 /* Disable encoders whose CRTC is about to change */
608 if (encoder_funcs->get_crtc &&
609 encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
610 (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
615 * drm_crtc_set_mode - set a mode
616 * @crtc: CRTC to program
617 * @mode: mode to use
618 * @x: width of mode
619 * @y: height of mode
621 * LOCKING:
622 * Caller must hold mode config lock.
624 * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
625 * to fixup or reject the mode prior to trying to set it.
627 * RETURNS:
628 * True if the mode was set successfully, or false otherwise.
630 bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
631 struct drm_display_mode *mode,
632 int x, int y,
633 struct drm_framebuffer *old_fb)
635 struct drm_device *dev = crtc->dev;
636 struct drm_display_mode *adjusted_mode, saved_mode;
637 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
638 struct drm_encoder_helper_funcs *encoder_funcs;
639 int saved_x, saved_y;
640 struct drm_encoder *encoder;
641 bool ret = true;
643 adjusted_mode = drm_mode_duplicate(dev, mode);
645 crtc->enabled = drm_helper_crtc_in_use(crtc);
647 if (!crtc->enabled)
648 return true;
650 saved_mode = crtc->mode;
651 saved_x = crtc->x;
652 saved_y = crtc->y;
654 /* Update crtc values up front so the driver can rely on them for mode
655 * setting.
657 crtc->mode = *mode;
658 crtc->x = x;
659 crtc->y = y;
661 /* Pass our mode to the connectors and the CRTC to give them a chance to
662 * adjust it according to limitations or connector properties, and also
663 * a chance to reject the mode entirely.
665 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
667 if (encoder->crtc != crtc)
668 continue;
669 encoder_funcs = encoder->helper_private;
670 if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
671 adjusted_mode))) {
672 goto done;
676 if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
677 goto done;
680 /* Prepare the encoders and CRTCs before setting the mode. */
681 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
683 if (encoder->crtc != crtc)
684 continue;
685 encoder_funcs = encoder->helper_private;
686 /* Disable the encoders as the first thing we do. */
687 encoder_funcs->prepare(encoder);
690 drm_crtc_prepare_encoders(dev);
692 crtc_funcs->prepare(crtc);
694 /* Set up the DPLL and any encoders state that needs to adjust or depend
695 * on the DPLL.
697 ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
698 if (!ret)
699 goto done;
701 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
703 if (encoder->crtc != crtc)
704 continue;
706 DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
707 mode->name, mode->base.id);
708 encoder_funcs = encoder->helper_private;
709 encoder_funcs->mode_set(encoder, mode, adjusted_mode);
712 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
713 crtc_funcs->commit(crtc);
715 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
717 if (encoder->crtc != crtc)
718 continue;
720 encoder_funcs = encoder->helper_private;
721 encoder_funcs->commit(encoder);
725 /* XXX free adjustedmode */
726 drm_mode_destroy(dev, adjusted_mode);
727 /* FIXME: add subpixel order */
728 done:
729 if (!ret) {
730 crtc->mode = saved_mode;
731 crtc->x = saved_x;
732 crtc->y = saved_y;
735 return ret;
737 EXPORT_SYMBOL(drm_crtc_helper_set_mode);
741 * drm_crtc_helper_set_config - set a new config from userspace
742 * @crtc: CRTC to setup
743 * @crtc_info: user provided configuration
744 * @new_mode: new mode to set
745 * @connector_set: set of connectors for the new config
746 * @fb: new framebuffer
748 * LOCKING:
749 * Caller must hold mode config lock.
751 * Setup a new configuration, provided by the user in @crtc_info, and enable
752 * it.
754 * RETURNS:
755 * Zero. (FIXME)
757 int drm_crtc_helper_set_config(struct drm_mode_set *set)
759 struct drm_device *dev;
760 struct drm_crtc *save_crtcs, *new_crtc, *crtc;
761 struct drm_encoder *save_encoders, *new_encoder, *encoder;
762 struct drm_framebuffer *old_fb = NULL;
763 bool mode_changed = false; /* if true do a full mode set */
764 bool fb_changed = false; /* if true and !mode_changed just do a flip */
765 struct drm_connector *save_connectors, *connector;
766 int count = 0, ro, fail = 0;
767 struct drm_crtc_helper_funcs *crtc_funcs;
768 int ret = 0;
770 DRM_DEBUG_KMS("\n");
772 if (!set)
773 return -EINVAL;
775 if (!set->crtc)
776 return -EINVAL;
778 if (!set->crtc->helper_private)
779 return -EINVAL;
781 crtc_funcs = set->crtc->helper_private;
783 DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:"
784 " %d (x, y) (%i, %i)\n",
785 set->crtc, set->crtc->base.id, set->fb, set->connectors,
786 (int)set->num_connectors, set->x, set->y);
788 dev = set->crtc->dev;
790 /* Allocate space for the backup of all (non-pointer) crtc, encoder and
791 * connector data. */
792 save_crtcs = kzalloc(dev->mode_config.num_crtc *
793 sizeof(struct drm_crtc), GFP_KERNEL);
794 if (!save_crtcs)
795 return -ENOMEM;
797 save_encoders = kzalloc(dev->mode_config.num_encoder *
798 sizeof(struct drm_encoder), GFP_KERNEL);
799 if (!save_encoders) {
800 kfree(save_crtcs);
801 return -ENOMEM;
804 save_connectors = kzalloc(dev->mode_config.num_connector *
805 sizeof(struct drm_connector), GFP_KERNEL);
806 if (!save_connectors) {
807 kfree(save_crtcs);
808 kfree(save_encoders);
809 return -ENOMEM;
812 /* Copy data. Note that driver private data is not affected.
813 * Should anything bad happen only the expected state is
814 * restored, not the drivers personal bookkeeping.
816 count = 0;
817 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
818 save_crtcs[count++] = *crtc;
821 count = 0;
822 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
823 save_encoders[count++] = *encoder;
826 count = 0;
827 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
828 save_connectors[count++] = *connector;
831 /* We should be able to check here if the fb has the same properties
832 * and then just flip_or_move it */
833 if (set->crtc->fb != set->fb) {
834 /* If we have no fb then treat it as a full mode set */
835 if (set->crtc->fb == NULL) {
836 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
837 mode_changed = true;
838 } else if (set->fb == NULL) {
839 mode_changed = true;
840 } else if ((set->fb->bits_per_pixel !=
841 set->crtc->fb->bits_per_pixel) ||
842 set->fb->depth != set->crtc->fb->depth)
843 fb_changed = true;
844 else
845 fb_changed = true;
848 if (set->x != set->crtc->x || set->y != set->crtc->y)
849 fb_changed = true;
851 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
852 DRM_DEBUG_KMS("modes are different, full mode set\n");
853 drm_mode_debug_printmodeline(&set->crtc->mode);
854 drm_mode_debug_printmodeline(set->mode);
855 mode_changed = true;
858 /* a) traverse passed in connector list and get encoders for them */
859 count = 0;
860 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
861 struct drm_connector_helper_funcs *connector_funcs =
862 connector->helper_private;
863 new_encoder = connector->encoder;
864 for (ro = 0; ro < set->num_connectors; ro++) {
865 if (set->connectors[ro] == connector) {
866 new_encoder = connector_funcs->best_encoder(connector);
867 /* if we can't get an encoder for a connector
868 we are setting now - then fail */
869 if (new_encoder == NULL)
870 /* don't break so fail path works correct */
871 fail = 1;
872 break;
876 if (new_encoder != connector->encoder) {
877 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
878 mode_changed = true;
879 /* If the encoder is reused for another connector, then
880 * the appropriate crtc will be set later.
882 if (connector->encoder)
883 connector->encoder->crtc = NULL;
884 connector->encoder = new_encoder;
888 if (fail) {
889 ret = -EINVAL;
890 goto fail;
893 count = 0;
894 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
895 if (!connector->encoder)
896 continue;
898 if (connector->encoder->crtc == set->crtc)
899 new_crtc = NULL;
900 else
901 new_crtc = connector->encoder->crtc;
903 for (ro = 0; ro < set->num_connectors; ro++) {
904 if (set->connectors[ro] == connector)
905 new_crtc = set->crtc;
908 /* Make sure the new CRTC will work with the encoder */
909 if (new_crtc &&
910 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
911 ret = -EINVAL;
912 goto fail;
914 if (new_crtc != connector->encoder->crtc) {
915 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
916 mode_changed = true;
917 connector->encoder->crtc = new_crtc;
919 DRM_DEBUG_KMS("setting connector %d crtc to %p\n",
920 connector->base.id, new_crtc);
923 /* mode_set_base is not a required function */
924 if (fb_changed && !crtc_funcs->mode_set_base)
925 mode_changed = true;
927 if (mode_changed) {
928 old_fb = set->crtc->fb;
929 set->crtc->fb = set->fb;
930 set->crtc->enabled = (set->mode != NULL);
931 if (set->mode != NULL) {
932 DRM_DEBUG_KMS("attempting to set mode from"
933 " userspace\n");
934 drm_mode_debug_printmodeline(set->mode);
935 if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
936 set->x, set->y,
937 old_fb)) {
938 DRM_ERROR("failed to set mode on crtc %p\n",
939 set->crtc);
940 ret = -EINVAL;
941 goto fail;
943 /* TODO are these needed? */
944 set->crtc->desired_x = set->x;
945 set->crtc->desired_y = set->y;
946 set->crtc->desired_mode = set->mode;
948 drm_helper_disable_unused_functions(dev);
949 } else if (fb_changed) {
950 set->crtc->x = set->x;
951 set->crtc->y = set->y;
953 old_fb = set->crtc->fb;
954 if (set->crtc->fb != set->fb)
955 set->crtc->fb = set->fb;
956 ret = crtc_funcs->mode_set_base(set->crtc,
957 set->x, set->y, old_fb);
958 if (ret != 0)
959 goto fail;
962 kfree(save_connectors);
963 kfree(save_encoders);
964 kfree(save_crtcs);
965 return 0;
967 fail:
968 /* Restore all previous data. */
969 count = 0;
970 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
971 *crtc = save_crtcs[count++];
974 count = 0;
975 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
976 *encoder = save_encoders[count++];
979 count = 0;
980 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
981 *connector = save_connectors[count++];
984 kfree(save_connectors);
985 kfree(save_encoders);
986 kfree(save_crtcs);
987 return ret;
989 EXPORT_SYMBOL(drm_crtc_helper_set_config);
991 bool drm_helper_plugged_event(struct drm_device *dev)
993 DRM_DEBUG_KMS("\n");
995 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
996 dev->mode_config.max_height);
998 drm_setup_crtcs(dev);
1000 /* alert the driver fb layer */
1001 dev->mode_config.funcs->fb_changed(dev);
1003 /* FIXME: send hotplug event */
1004 return true;
1007 * drm_initial_config - setup a sane initial connector configuration
1008 * @dev: DRM device
1010 * LOCKING:
1011 * Called at init time, must take mode config lock.
1013 * Scan the CRTCs and connectors and try to put together an initial setup.
1014 * At the moment, this is a cloned configuration across all heads with
1015 * a new framebuffer object as the backing store.
1017 * RETURNS:
1018 * Zero if everything went ok, nonzero otherwise.
1020 bool drm_helper_initial_config(struct drm_device *dev)
1022 int count = 0;
1024 /* disable all the possible outputs/crtcs before entering KMS mode */
1025 drm_helper_disable_unused_functions(dev);
1027 drm_fb_helper_parse_command_line(dev);
1029 count = drm_helper_probe_connector_modes(dev,
1030 dev->mode_config.max_width,
1031 dev->mode_config.max_height);
1034 * we shouldn't end up with no modes here.
1036 WARN(!count, "No connectors reported connected with modes\n");
1038 drm_setup_crtcs(dev);
1040 /* alert the driver fb layer */
1041 dev->mode_config.funcs->fb_changed(dev);
1043 return 0;
1045 EXPORT_SYMBOL(drm_helper_initial_config);
1047 static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1049 int dpms = DRM_MODE_DPMS_OFF;
1050 struct drm_connector *connector;
1051 struct drm_device *dev = encoder->dev;
1053 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1054 if (connector->encoder == encoder)
1055 if (connector->dpms < dpms)
1056 dpms = connector->dpms;
1057 return dpms;
1060 static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1062 int dpms = DRM_MODE_DPMS_OFF;
1063 struct drm_connector *connector;
1064 struct drm_device *dev = crtc->dev;
1066 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1067 if (connector->encoder && connector->encoder->crtc == crtc)
1068 if (connector->dpms < dpms)
1069 dpms = connector->dpms;
1070 return dpms;
1074 * drm_helper_connector_dpms
1075 * @connector affected connector
1076 * @mode DPMS mode
1078 * Calls the low-level connector DPMS function, then
1079 * calls appropriate encoder and crtc DPMS functions as well
1081 void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1083 struct drm_encoder *encoder = connector->encoder;
1084 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1085 int old_dpms;
1087 if (mode == connector->dpms)
1088 return;
1090 old_dpms = connector->dpms;
1091 connector->dpms = mode;
1093 /* from off to on, do crtc then encoder */
1094 if (mode < old_dpms) {
1095 if (crtc) {
1096 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1097 if (crtc_funcs->dpms)
1098 (*crtc_funcs->dpms) (crtc,
1099 drm_helper_choose_crtc_dpms(crtc));
1101 if (encoder) {
1102 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1103 if (encoder_funcs->dpms)
1104 (*encoder_funcs->dpms) (encoder,
1105 drm_helper_choose_encoder_dpms(encoder));
1109 /* from on to off, do encoder then crtc */
1110 if (mode > old_dpms) {
1111 if (encoder) {
1112 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1113 if (encoder_funcs->dpms)
1114 (*encoder_funcs->dpms) (encoder,
1115 drm_helper_choose_encoder_dpms(encoder));
1117 if (crtc) {
1118 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1119 if (crtc_funcs->dpms)
1120 (*crtc_funcs->dpms) (crtc,
1121 drm_helper_choose_crtc_dpms(crtc));
1125 return;
1127 EXPORT_SYMBOL(drm_helper_connector_dpms);
1130 * drm_hotplug_stage_two
1131 * @dev DRM device
1132 * @connector hotpluged connector
1134 * LOCKING.
1135 * Caller must hold mode config lock, function might grab struct lock.
1137 * Stage two of a hotplug.
1139 * RETURNS:
1140 * Zero on success, errno on failure.
1142 int drm_helper_hotplug_stage_two(struct drm_device *dev)
1144 drm_helper_plugged_event(dev);
1146 return 0;
1148 EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1150 int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1151 struct drm_mode_fb_cmd *mode_cmd)
1153 fb->width = mode_cmd->width;
1154 fb->height = mode_cmd->height;
1155 fb->pitch = mode_cmd->pitch;
1156 fb->bits_per_pixel = mode_cmd->bpp;
1157 fb->depth = mode_cmd->depth;
1159 return 0;
1161 EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1163 int drm_helper_resume_force_mode(struct drm_device *dev)
1165 struct drm_crtc *crtc;
1166 int ret;
1168 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1170 if (!crtc->enabled)
1171 continue;
1173 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1174 crtc->x, crtc->y, crtc->fb);
1176 if (ret == false)
1177 DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1179 /* disable the unused connectors while restoring the modesetting */
1180 drm_helper_disable_unused_functions(dev);
1181 return 0;
1183 EXPORT_SYMBOL(drm_helper_resume_force_mode);