Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006-2008 Intel Corporation |
| 3 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> |
| 4 | * |
| 5 | * DRM core CRTC related functions |
| 6 | * |
| 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. |
| 16 | * |
| 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. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Keith Packard |
| 27 | * Eric Anholt <eric@anholt.net> |
| 28 | * Dave Airlie <airlied@linux.ie> |
| 29 | * Jesse Barnes <jesse.barnes@intel.com> |
| 30 | */ |
| 31 | |
| 32 | #include "drmP.h" |
| 33 | #include "drm_crtc.h" |
| 34 | #include "drm_crtc_helper.h" |
| 35 | |
| 36 | /* |
Dave Airlie | aa91c66 | 2008-12-08 14:55:27 +1000 | [diff] [blame] | 37 | * Detailed mode info for 800x600@60Hz |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 38 | */ |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 39 | static struct drm_display_mode std_modes[] = { |
Dave Airlie | aa91c66 | 2008-12-08 14:55:27 +1000 | [diff] [blame] | 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) }, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 45 | static void drm_mode_validate_flag(struct drm_connector *connector, |
| 46 | int flags) |
| 47 | { |
| 48 | struct drm_display_mode *mode, *t; |
| 49 | |
| 50 | if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE)) |
| 51 | return; |
| 52 | |
| 53 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 54 | if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && |
| 55 | !(flags & DRM_MODE_FLAG_INTERLACE)) |
| 56 | mode->status = MODE_NO_INTERLACE; |
| 57 | if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && |
| 58 | !(flags & DRM_MODE_FLAG_DBLSCAN)) |
| 59 | mode->status = MODE_NO_DBLESCAN; |
| 60 | } |
| 61 | |
| 62 | return; |
| 63 | } |
| 64 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 65 | /** |
| 66 | * drm_helper_probe_connector_modes - get complete set of display modes |
| 67 | * @dev: DRM device |
| 68 | * @maxX: max width for modes |
| 69 | * @maxY: max height for modes |
| 70 | * |
| 71 | * LOCKING: |
| 72 | * Caller must hold mode config lock. |
| 73 | * |
| 74 | * Based on @dev's mode_config layout, scan all the connectors and try to detect |
| 75 | * modes on them. Modes will first be added to the connector's probed_modes |
| 76 | * list, then culled (based on validity and the @maxX, @maxY parameters) and |
| 77 | * put into the normal modes list. |
| 78 | * |
| 79 | * Intended to be used either at bootup time or when major configuration |
| 80 | * changes have occurred. |
| 81 | * |
| 82 | * FIXME: take into account monitor limits |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 83 | * |
| 84 | * RETURNS: |
| 85 | * Number of modes found on @connector. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 86 | */ |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 87 | int drm_helper_probe_single_connector_modes(struct drm_connector *connector, |
| 88 | uint32_t maxX, uint32_t maxY) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 89 | { |
| 90 | struct drm_device *dev = connector->dev; |
| 91 | struct drm_display_mode *mode, *t; |
| 92 | struct drm_connector_helper_funcs *connector_funcs = |
| 93 | connector->helper_private; |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 94 | int count = 0; |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 95 | int mode_flags = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 96 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 97 | DRM_DEBUG_KMS("%s\n", drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 98 | /* set all modes to the unverified state */ |
| 99 | list_for_each_entry_safe(mode, t, &connector->modes, head) |
| 100 | mode->status = MODE_UNVERIFIED; |
| 101 | |
| 102 | connector->status = connector->funcs->detect(connector); |
| 103 | |
| 104 | if (connector->status == connector_status_disconnected) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 105 | DRM_DEBUG_KMS("%s is disconnected\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 106 | drm_get_connector_name(connector)); |
| 107 | /* TODO set EDID to NULL */ |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 108 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 111 | count = (*connector_funcs->get_modes)(connector); |
| 112 | if (!count) |
| 113 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 114 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 115 | drm_mode_connector_list_update(connector); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 116 | |
| 117 | if (maxX && maxY) |
| 118 | drm_mode_validate_size(dev, &connector->modes, maxX, |
| 119 | maxY, 0); |
yakui_zhao | 6714977 | 2009-04-02 11:52:12 +0800 | [diff] [blame] | 120 | |
| 121 | if (connector->interlace_allowed) |
| 122 | mode_flags |= DRM_MODE_FLAG_INTERLACE; |
| 123 | if (connector->doublescan_allowed) |
| 124 | mode_flags |= DRM_MODE_FLAG_DBLSCAN; |
| 125 | drm_mode_validate_flag(connector, mode_flags); |
| 126 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 127 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 128 | if (mode->status == MODE_OK) |
| 129 | mode->status = connector_funcs->mode_valid(connector, |
| 130 | mode); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | drm_mode_prune_invalid(dev, &connector->modes, true); |
| 135 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 136 | if (list_empty(&connector->modes)) |
| 137 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 138 | |
| 139 | drm_mode_sort(&connector->modes); |
| 140 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 141 | DRM_DEBUG_KMS("Probed modes for %s\n", |
| 142 | drm_get_connector_name(connector)); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 143 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 144 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 145 | |
| 146 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
| 147 | drm_mode_debug_printmodeline(mode); |
| 148 | } |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 149 | |
| 150 | return count; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 151 | } |
| 152 | EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); |
| 153 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 154 | int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX, |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 155 | uint32_t maxY) |
| 156 | { |
| 157 | struct drm_connector *connector; |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 158 | int count = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 159 | |
| 160 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 161 | count += drm_helper_probe_single_connector_modes(connector, |
| 162 | maxX, maxY); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 163 | } |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 164 | |
| 165 | return count; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 166 | } |
| 167 | EXPORT_SYMBOL(drm_helper_probe_connector_modes); |
| 168 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 169 | static void drm_helper_add_std_modes(struct drm_device *dev, |
| 170 | struct drm_connector *connector) |
| 171 | { |
| 172 | struct drm_display_mode *mode, *t; |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < ARRAY_SIZE(std_modes); i++) { |
| 176 | struct drm_display_mode *stdmode; |
| 177 | |
| 178 | /* |
| 179 | * When no valid EDID modes are available we end up |
| 180 | * here and bailed in the past, now we add some standard |
| 181 | * modes and move on. |
| 182 | */ |
| 183 | stdmode = drm_mode_duplicate(dev, &std_modes[i]); |
| 184 | drm_mode_probed_add(connector, stdmode); |
| 185 | drm_mode_list_concat(&connector->probed_modes, |
| 186 | &connector->modes); |
| 187 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 188 | DRM_DEBUG_KMS("Adding mode %s to %s\n", stdmode->name, |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 189 | drm_get_connector_name(connector)); |
| 190 | } |
| 191 | drm_mode_sort(&connector->modes); |
| 192 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 193 | DRM_DEBUG_KMS("Added std modes on %s\n", |
| 194 | drm_get_connector_name(connector)); |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 195 | list_for_each_entry_safe(mode, t, &connector->modes, head) { |
| 196 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 197 | |
| 198 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); |
| 199 | drm_mode_debug_printmodeline(mode); |
| 200 | } |
| 201 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 204 | * drm_helper_encoder_in_use - check if a given encoder is in use |
| 205 | * @encoder: encoder to check |
| 206 | * |
| 207 | * LOCKING: |
| 208 | * Caller must hold mode config lock. |
| 209 | * |
| 210 | * Walk @encoders's DRM device's mode_config and see if it's in use. |
| 211 | * |
| 212 | * RETURNS: |
| 213 | * True if @encoder is part of the mode_config, false otherwise. |
| 214 | */ |
| 215 | bool drm_helper_encoder_in_use(struct drm_encoder *encoder) |
| 216 | { |
| 217 | struct drm_connector *connector; |
| 218 | struct drm_device *dev = encoder->dev; |
| 219 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 220 | if (connector->encoder == encoder) |
| 221 | return true; |
| 222 | return false; |
| 223 | } |
| 224 | EXPORT_SYMBOL(drm_helper_encoder_in_use); |
| 225 | |
| 226 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 227 | * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config |
| 228 | * @crtc: CRTC to check |
| 229 | * |
| 230 | * LOCKING: |
| 231 | * Caller must hold mode config lock. |
| 232 | * |
| 233 | * Walk @crtc's DRM device's mode_config and see if it's in use. |
| 234 | * |
| 235 | * RETURNS: |
| 236 | * True if @crtc is part of the mode_config, false otherwise. |
| 237 | */ |
| 238 | bool drm_helper_crtc_in_use(struct drm_crtc *crtc) |
| 239 | { |
| 240 | struct drm_encoder *encoder; |
| 241 | struct drm_device *dev = crtc->dev; |
| 242 | /* FIXME: Locking around list access? */ |
| 243 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 244 | if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 245 | return true; |
| 246 | return false; |
| 247 | } |
| 248 | EXPORT_SYMBOL(drm_helper_crtc_in_use); |
| 249 | |
| 250 | /** |
| 251 | * drm_disable_unused_functions - disable unused objects |
| 252 | * @dev: DRM device |
| 253 | * |
| 254 | * LOCKING: |
| 255 | * Caller must hold mode config lock. |
| 256 | * |
| 257 | * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled |
| 258 | * by calling its dpms function, which should power it off. |
| 259 | */ |
| 260 | void drm_helper_disable_unused_functions(struct drm_device *dev) |
| 261 | { |
| 262 | struct drm_encoder *encoder; |
| 263 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 264 | struct drm_crtc *crtc; |
| 265 | |
| 266 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 267 | encoder_funcs = encoder->helper_private; |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 268 | if (!drm_helper_encoder_in_use(encoder)) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 269 | (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); |
| 270 | } |
| 271 | |
| 272 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 273 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 274 | crtc->enabled = drm_helper_crtc_in_use(crtc); |
| 275 | if (!crtc->enabled) { |
| 276 | crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF); |
| 277 | crtc->fb = NULL; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | EXPORT_SYMBOL(drm_helper_disable_unused_functions); |
| 282 | |
| 283 | static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height) |
| 284 | { |
| 285 | struct drm_display_mode *mode; |
| 286 | |
| 287 | list_for_each_entry(mode, &connector->modes, head) { |
| 288 | if (drm_mode_width(mode) > width || |
| 289 | drm_mode_height(mode) > height) |
| 290 | continue; |
| 291 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 292 | return mode; |
| 293 | } |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) |
| 298 | { |
| 299 | bool enable; |
| 300 | |
| 301 | if (strict) { |
| 302 | enable = connector->status == connector_status_connected; |
| 303 | } else { |
| 304 | enable = connector->status != connector_status_disconnected; |
| 305 | } |
| 306 | return enable; |
| 307 | } |
| 308 | |
| 309 | static void drm_enable_connectors(struct drm_device *dev, bool *enabled) |
| 310 | { |
| 311 | bool any_enabled = false; |
| 312 | struct drm_connector *connector; |
| 313 | int i = 0; |
| 314 | |
| 315 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 316 | enabled[i] = drm_connector_enabled(connector, true); |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 317 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 318 | enabled[i] ? "yes" : "no"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 319 | any_enabled |= enabled[i]; |
| 320 | i++; |
| 321 | } |
| 322 | |
| 323 | if (any_enabled) |
| 324 | return; |
| 325 | |
| 326 | i = 0; |
| 327 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 328 | enabled[i] = drm_connector_enabled(connector, false); |
| 329 | i++; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static bool drm_target_preferred(struct drm_device *dev, |
| 334 | struct drm_display_mode **modes, |
| 335 | bool *enabled, int width, int height) |
| 336 | { |
| 337 | struct drm_connector *connector; |
| 338 | int i = 0; |
| 339 | |
| 340 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 341 | |
| 342 | if (enabled[i] == false) { |
| 343 | i++; |
| 344 | continue; |
| 345 | } |
| 346 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 347 | DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 348 | connector->base.id); |
| 349 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 350 | modes[i] = drm_has_preferred_mode(connector, width, height); |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 351 | /* No preferred modes, pick one off the list */ |
| 352 | if (!modes[i] && !list_empty(&connector->modes)) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 353 | list_for_each_entry(modes[i], &connector->modes, head) |
| 354 | break; |
| 355 | } |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 356 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 357 | "none"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 358 | i++; |
| 359 | } |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | static int drm_pick_crtcs(struct drm_device *dev, |
| 364 | struct drm_crtc **best_crtcs, |
| 365 | struct drm_display_mode **modes, |
| 366 | int n, int width, int height) |
| 367 | { |
| 368 | int c, o; |
| 369 | struct drm_connector *connector; |
| 370 | struct drm_connector_helper_funcs *connector_funcs; |
| 371 | struct drm_encoder *encoder; |
| 372 | struct drm_crtc *best_crtc; |
| 373 | int my_score, best_score, score; |
| 374 | struct drm_crtc **crtcs, *crtc; |
| 375 | |
| 376 | if (n == dev->mode_config.num_connector) |
| 377 | return 0; |
| 378 | c = 0; |
| 379 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 380 | if (c == n) |
| 381 | break; |
| 382 | c++; |
| 383 | } |
| 384 | |
| 385 | best_crtcs[n] = NULL; |
| 386 | best_crtc = NULL; |
| 387 | best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height); |
| 388 | if (modes[n] == NULL) |
| 389 | return best_score; |
| 390 | |
| 391 | crtcs = kmalloc(dev->mode_config.num_connector * |
| 392 | sizeof(struct drm_crtc *), GFP_KERNEL); |
| 393 | if (!crtcs) |
| 394 | return best_score; |
| 395 | |
| 396 | my_score = 1; |
| 397 | if (connector->status == connector_status_connected) |
| 398 | my_score++; |
| 399 | if (drm_has_preferred_mode(connector, width, height)) |
| 400 | my_score++; |
| 401 | |
| 402 | connector_funcs = connector->helper_private; |
| 403 | encoder = connector_funcs->best_encoder(connector); |
| 404 | if (!encoder) |
| 405 | goto out; |
| 406 | |
| 407 | connector->encoder = encoder; |
| 408 | |
| 409 | /* select a crtc for this connector and then attempt to configure |
| 410 | remaining connectors */ |
| 411 | c = 0; |
| 412 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 413 | |
| 414 | if ((connector->encoder->possible_crtcs & (1 << c)) == 0) { |
| 415 | c++; |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | for (o = 0; o < n; o++) |
| 420 | if (best_crtcs[o] == crtc) |
| 421 | break; |
| 422 | |
| 423 | if (o < n) { |
| 424 | /* ignore cloning for now */ |
| 425 | c++; |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | crtcs[n] = crtc; |
| 430 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *)); |
| 431 | score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1, |
| 432 | width, height); |
| 433 | if (score > best_score) { |
| 434 | best_crtc = crtc; |
| 435 | best_score = score; |
| 436 | memcpy(best_crtcs, crtcs, |
| 437 | dev->mode_config.num_connector * |
| 438 | sizeof(struct drm_crtc *)); |
| 439 | } |
| 440 | c++; |
| 441 | } |
| 442 | out: |
| 443 | kfree(crtcs); |
| 444 | return best_score; |
| 445 | } |
| 446 | |
| 447 | static void drm_setup_crtcs(struct drm_device *dev) |
| 448 | { |
| 449 | struct drm_crtc **crtcs; |
| 450 | struct drm_display_mode **modes; |
| 451 | struct drm_encoder *encoder; |
| 452 | struct drm_connector *connector; |
| 453 | bool *enabled; |
| 454 | int width, height; |
| 455 | int i, ret; |
| 456 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 457 | DRM_DEBUG_KMS("\n"); |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 458 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 459 | width = dev->mode_config.max_width; |
| 460 | height = dev->mode_config.max_height; |
| 461 | |
| 462 | /* clean out all the encoder/crtc combos */ |
| 463 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 464 | encoder->crtc = NULL; |
| 465 | } |
| 466 | |
| 467 | crtcs = kcalloc(dev->mode_config.num_connector, |
| 468 | sizeof(struct drm_crtc *), GFP_KERNEL); |
| 469 | modes = kcalloc(dev->mode_config.num_connector, |
| 470 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
| 471 | enabled = kcalloc(dev->mode_config.num_connector, |
| 472 | sizeof(bool), GFP_KERNEL); |
| 473 | |
| 474 | drm_enable_connectors(dev, enabled); |
| 475 | |
| 476 | ret = drm_target_preferred(dev, modes, enabled, width, height); |
| 477 | if (!ret) |
| 478 | DRM_ERROR("Unable to find initial modes\n"); |
| 479 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 480 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height); |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 481 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 482 | drm_pick_crtcs(dev, crtcs, modes, 0, width, height); |
| 483 | |
| 484 | i = 0; |
| 485 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 486 | struct drm_display_mode *mode = modes[i]; |
| 487 | struct drm_crtc *crtc = crtcs[i]; |
| 488 | |
| 489 | if (connector->encoder == NULL) { |
| 490 | i++; |
| 491 | continue; |
| 492 | } |
| 493 | |
| 494 | if (mode && crtc) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 495 | DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 496 | mode->name, crtc->base.id); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 497 | crtc->desired_mode = mode; |
| 498 | connector->encoder->crtc = crtc; |
| 499 | } else |
| 500 | connector->encoder->crtc = NULL; |
| 501 | i++; |
| 502 | } |
| 503 | |
| 504 | kfree(crtcs); |
| 505 | kfree(modes); |
| 506 | kfree(enabled); |
| 507 | } |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * drm_encoder_crtc_ok - can a given crtc drive a given encoder? |
| 511 | * @encoder: encoder to test |
| 512 | * @crtc: crtc to test |
| 513 | * |
| 514 | * Return false if @encoder can't be driven by @crtc, true otherwise. |
| 515 | */ |
| 516 | static bool drm_encoder_crtc_ok(struct drm_encoder *encoder, |
| 517 | struct drm_crtc *crtc) |
| 518 | { |
| 519 | struct drm_device *dev; |
| 520 | struct drm_crtc *tmp; |
| 521 | int crtc_mask = 1; |
| 522 | |
| 523 | WARN(!crtc, "checking null crtc?"); |
| 524 | |
| 525 | dev = crtc->dev; |
| 526 | |
| 527 | list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) { |
| 528 | if (tmp == crtc) |
| 529 | break; |
| 530 | crtc_mask <<= 1; |
| 531 | } |
| 532 | |
| 533 | if (encoder->possible_crtcs & crtc_mask) |
| 534 | return true; |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Check the CRTC we're going to map each output to vs. its current |
| 540 | * CRTC. If they don't match, we have to disable the output and the CRTC |
| 541 | * since the driver will have to re-route things. |
| 542 | */ |
| 543 | static void |
| 544 | drm_crtc_prepare_encoders(struct drm_device *dev) |
| 545 | { |
| 546 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 547 | struct drm_encoder *encoder; |
| 548 | |
| 549 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 550 | encoder_funcs = encoder->helper_private; |
| 551 | /* Disable unused encoders */ |
| 552 | if (encoder->crtc == NULL) |
| 553 | (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); |
| 554 | /* Disable encoders whose CRTC is about to change */ |
| 555 | if (encoder_funcs->get_crtc && |
| 556 | encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) |
| 557 | (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); |
| 558 | } |
| 559 | } |
| 560 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 561 | /** |
| 562 | * drm_crtc_set_mode - set a mode |
| 563 | * @crtc: CRTC to program |
| 564 | * @mode: mode to use |
| 565 | * @x: width of mode |
| 566 | * @y: height of mode |
| 567 | * |
| 568 | * LOCKING: |
| 569 | * Caller must hold mode config lock. |
| 570 | * |
| 571 | * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance |
| 572 | * to fixup or reject the mode prior to trying to set it. |
| 573 | * |
| 574 | * RETURNS: |
| 575 | * True if the mode was set successfully, or false otherwise. |
| 576 | */ |
| 577 | bool drm_crtc_helper_set_mode(struct drm_crtc *crtc, |
| 578 | struct drm_display_mode *mode, |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 579 | int x, int y, |
| 580 | struct drm_framebuffer *old_fb) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 581 | { |
| 582 | struct drm_device *dev = crtc->dev; |
| 583 | struct drm_display_mode *adjusted_mode, saved_mode; |
| 584 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 585 | struct drm_encoder_helper_funcs *encoder_funcs; |
| 586 | int saved_x, saved_y; |
| 587 | struct drm_encoder *encoder; |
| 588 | bool ret = true; |
| 589 | |
| 590 | adjusted_mode = drm_mode_duplicate(dev, mode); |
| 591 | |
| 592 | crtc->enabled = drm_helper_crtc_in_use(crtc); |
| 593 | |
| 594 | if (!crtc->enabled) |
| 595 | return true; |
| 596 | |
| 597 | saved_mode = crtc->mode; |
| 598 | saved_x = crtc->x; |
| 599 | saved_y = crtc->y; |
| 600 | |
| 601 | /* Update crtc values up front so the driver can rely on them for mode |
| 602 | * setting. |
| 603 | */ |
| 604 | crtc->mode = *mode; |
| 605 | crtc->x = x; |
| 606 | crtc->y = y; |
| 607 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 608 | /* Pass our mode to the connectors and the CRTC to give them a chance to |
| 609 | * adjust it according to limitations or connector properties, and also |
| 610 | * a chance to reject the mode entirely. |
| 611 | */ |
| 612 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 613 | |
| 614 | if (encoder->crtc != crtc) |
| 615 | continue; |
| 616 | encoder_funcs = encoder->helper_private; |
| 617 | if (!(ret = encoder_funcs->mode_fixup(encoder, mode, |
| 618 | adjusted_mode))) { |
| 619 | goto done; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { |
| 624 | goto done; |
| 625 | } |
| 626 | |
| 627 | /* Prepare the encoders and CRTCs before setting the mode. */ |
| 628 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 629 | |
| 630 | if (encoder->crtc != crtc) |
| 631 | continue; |
| 632 | encoder_funcs = encoder->helper_private; |
| 633 | /* Disable the encoders as the first thing we do. */ |
| 634 | encoder_funcs->prepare(encoder); |
| 635 | } |
| 636 | |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 637 | drm_crtc_prepare_encoders(dev); |
| 638 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 639 | crtc_funcs->prepare(crtc); |
| 640 | |
| 641 | /* Set up the DPLL and any encoders state that needs to adjust or depend |
| 642 | * on the DPLL. |
| 643 | */ |
Chris Wilson | 5c3b82e | 2009-02-11 13:25:09 +0000 | [diff] [blame] | 644 | ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); |
| 645 | if (!ret) |
| 646 | goto done; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 647 | |
| 648 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 649 | |
| 650 | if (encoder->crtc != crtc) |
| 651 | continue; |
| 652 | |
| 653 | DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder), |
| 654 | mode->name, mode->base.id); |
| 655 | encoder_funcs = encoder->helper_private; |
| 656 | encoder_funcs->mode_set(encoder, mode, adjusted_mode); |
| 657 | } |
| 658 | |
| 659 | /* Now enable the clocks, plane, pipe, and connectors that we set up. */ |
| 660 | crtc_funcs->commit(crtc); |
| 661 | |
| 662 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 663 | |
| 664 | if (encoder->crtc != crtc) |
| 665 | continue; |
| 666 | |
| 667 | encoder_funcs = encoder->helper_private; |
| 668 | encoder_funcs->commit(encoder); |
| 669 | |
| 670 | } |
| 671 | |
| 672 | /* XXX free adjustedmode */ |
| 673 | drm_mode_destroy(dev, adjusted_mode); |
| 674 | /* FIXME: add subpixel order */ |
| 675 | done: |
| 676 | if (!ret) { |
| 677 | crtc->mode = saved_mode; |
| 678 | crtc->x = saved_x; |
| 679 | crtc->y = saved_y; |
| 680 | } |
| 681 | |
| 682 | return ret; |
| 683 | } |
| 684 | EXPORT_SYMBOL(drm_crtc_helper_set_mode); |
| 685 | |
| 686 | |
| 687 | /** |
| 688 | * drm_crtc_helper_set_config - set a new config from userspace |
| 689 | * @crtc: CRTC to setup |
| 690 | * @crtc_info: user provided configuration |
| 691 | * @new_mode: new mode to set |
| 692 | * @connector_set: set of connectors for the new config |
| 693 | * @fb: new framebuffer |
| 694 | * |
| 695 | * LOCKING: |
| 696 | * Caller must hold mode config lock. |
| 697 | * |
| 698 | * Setup a new configuration, provided by the user in @crtc_info, and enable |
| 699 | * it. |
| 700 | * |
| 701 | * RETURNS: |
| 702 | * Zero. (FIXME) |
| 703 | */ |
| 704 | int drm_crtc_helper_set_config(struct drm_mode_set *set) |
| 705 | { |
| 706 | struct drm_device *dev; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 707 | struct drm_crtc *save_crtcs, *new_crtc, *crtc; |
| 708 | struct drm_encoder *save_encoders, *new_encoder, *encoder; |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 709 | struct drm_framebuffer *old_fb = NULL; |
Jakob Bornecrantz | 4cb72b1 | 2009-08-03 13:43:59 +0100 | [diff] [blame] | 710 | bool mode_changed = false; /* if true do a full mode set */ |
| 711 | bool fb_changed = false; /* if true and !mode_changed just do a flip */ |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 712 | struct drm_connector *save_connectors, *connector; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 713 | int count = 0, ro, fail = 0; |
| 714 | struct drm_crtc_helper_funcs *crtc_funcs; |
| 715 | int ret = 0; |
| 716 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 717 | DRM_DEBUG_KMS("\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 718 | |
| 719 | if (!set) |
| 720 | return -EINVAL; |
| 721 | |
| 722 | if (!set->crtc) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | if (!set->crtc->helper_private) |
| 726 | return -EINVAL; |
| 727 | |
| 728 | crtc_funcs = set->crtc->helper_private; |
| 729 | |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 730 | DRM_DEBUG_KMS("crtc: %p %d fb: %p connectors: %p num_connectors:" |
| 731 | " %d (x, y) (%i, %i)\n", |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 732 | set->crtc, set->crtc->base.id, set->fb, set->connectors, |
| 733 | (int)set->num_connectors, set->x, set->y); |
| 734 | |
| 735 | dev = set->crtc->dev; |
| 736 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 737 | /* Allocate space for the backup of all (non-pointer) crtc, encoder and |
| 738 | * connector data. */ |
| 739 | save_crtcs = kzalloc(dev->mode_config.num_crtc * |
| 740 | sizeof(struct drm_crtc), GFP_KERNEL); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 741 | if (!save_crtcs) |
| 742 | return -ENOMEM; |
| 743 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 744 | save_encoders = kzalloc(dev->mode_config.num_encoder * |
| 745 | sizeof(struct drm_encoder), GFP_KERNEL); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 746 | if (!save_encoders) { |
| 747 | kfree(save_crtcs); |
| 748 | return -ENOMEM; |
| 749 | } |
| 750 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 751 | save_connectors = kzalloc(dev->mode_config.num_connector * |
| 752 | sizeof(struct drm_connector), GFP_KERNEL); |
| 753 | if (!save_connectors) { |
| 754 | kfree(save_crtcs); |
| 755 | kfree(save_encoders); |
| 756 | return -ENOMEM; |
| 757 | } |
| 758 | |
| 759 | /* Copy data. Note that driver private data is not affected. |
| 760 | * Should anything bad happen only the expected state is |
| 761 | * restored, not the drivers personal bookkeeping. |
| 762 | */ |
| 763 | count = 0; |
| 764 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 765 | save_crtcs[count++] = *crtc; |
| 766 | } |
| 767 | |
| 768 | count = 0; |
| 769 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 770 | save_encoders[count++] = *encoder; |
| 771 | } |
| 772 | |
| 773 | count = 0; |
| 774 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 775 | save_connectors[count++] = *connector; |
| 776 | } |
| 777 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 778 | /* We should be able to check here if the fb has the same properties |
| 779 | * and then just flip_or_move it */ |
| 780 | if (set->crtc->fb != set->fb) { |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 781 | /* If we have no fb then treat it as a full mode set */ |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 782 | if (set->crtc->fb == NULL) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 783 | DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 784 | mode_changed = true; |
Jakob Bornecrantz | 4cb72b1 | 2009-08-03 13:43:59 +0100 | [diff] [blame] | 785 | } else if (set->fb == NULL) { |
| 786 | mode_changed = true; |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 787 | } else if ((set->fb->bits_per_pixel != |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 788 | set->crtc->fb->bits_per_pixel) || |
| 789 | set->fb->depth != set->crtc->fb->depth) |
| 790 | fb_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 791 | else |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 792 | fb_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | if (set->x != set->crtc->x || set->y != set->crtc->y) |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 796 | fb_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 797 | |
| 798 | if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 799 | DRM_DEBUG_KMS("modes are different, full mode set\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 800 | drm_mode_debug_printmodeline(&set->crtc->mode); |
| 801 | drm_mode_debug_printmodeline(set->mode); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 802 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* a) traverse passed in connector list and get encoders for them */ |
| 806 | count = 0; |
| 807 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 808 | struct drm_connector_helper_funcs *connector_funcs = |
| 809 | connector->helper_private; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 810 | new_encoder = connector->encoder; |
| 811 | for (ro = 0; ro < set->num_connectors; ro++) { |
| 812 | if (set->connectors[ro] == connector) { |
| 813 | new_encoder = connector_funcs->best_encoder(connector); |
| 814 | /* if we can't get an encoder for a connector |
| 815 | we are setting now - then fail */ |
| 816 | if (new_encoder == NULL) |
| 817 | /* don't break so fail path works correct */ |
| 818 | fail = 1; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (new_encoder != connector->encoder) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 824 | DRM_DEBUG_KMS("encoder changed, full mode switch\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 825 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 826 | connector->encoder = new_encoder; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (fail) { |
| 831 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 832 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | count = 0; |
| 836 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 837 | if (!connector->encoder) |
| 838 | continue; |
| 839 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 840 | if (connector->encoder->crtc == set->crtc) |
| 841 | new_crtc = NULL; |
| 842 | else |
| 843 | new_crtc = connector->encoder->crtc; |
| 844 | |
| 845 | for (ro = 0; ro < set->num_connectors; ro++) { |
| 846 | if (set->connectors[ro] == connector) |
| 847 | new_crtc = set->crtc; |
| 848 | } |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 849 | |
| 850 | /* Make sure the new CRTC will work with the encoder */ |
| 851 | if (new_crtc && |
| 852 | !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { |
| 853 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 854 | goto fail; |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 855 | } |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 856 | if (new_crtc != connector->encoder->crtc) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 857 | DRM_DEBUG_KMS("crtc changed, full mode switch\n"); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 858 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 859 | connector->encoder->crtc = new_crtc; |
| 860 | } |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 861 | DRM_DEBUG_KMS("setting connector %d crtc to %p\n", |
Jesse Barnes | 7bec756 | 2009-02-23 16:09:34 -0800 | [diff] [blame] | 862 | connector->base.id, new_crtc); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* mode_set_base is not a required function */ |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 866 | if (fb_changed && !crtc_funcs->mode_set_base) |
| 867 | mode_changed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 868 | |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 869 | if (mode_changed) { |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 870 | old_fb = set->crtc->fb; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 871 | set->crtc->fb = set->fb; |
| 872 | set->crtc->enabled = (set->mode != NULL); |
| 873 | if (set->mode != NULL) { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 874 | DRM_DEBUG_KMS("attempting to set mode from" |
| 875 | " userspace\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 876 | drm_mode_debug_printmodeline(set->mode); |
| 877 | if (!drm_crtc_helper_set_mode(set->crtc, set->mode, |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 878 | set->x, set->y, |
| 879 | old_fb)) { |
Chris Wilson | 5c3b82e | 2009-02-11 13:25:09 +0000 | [diff] [blame] | 880 | DRM_ERROR("failed to set mode on crtc %p\n", |
| 881 | set->crtc); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 882 | ret = -EINVAL; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 883 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 884 | } |
| 885 | /* TODO are these needed? */ |
| 886 | set->crtc->desired_x = set->x; |
| 887 | set->crtc->desired_y = set->y; |
| 888 | set->crtc->desired_mode = set->mode; |
| 889 | } |
| 890 | drm_helper_disable_unused_functions(dev); |
Jesse Barnes | 712531b | 2009-01-09 13:56:14 -0800 | [diff] [blame] | 891 | } else if (fb_changed) { |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 892 | old_fb = set->crtc->fb; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 893 | if (set->crtc->fb != set->fb) |
| 894 | set->crtc->fb = set->fb; |
Chris Wilson | 5c3b82e | 2009-02-11 13:25:09 +0000 | [diff] [blame] | 895 | ret = crtc_funcs->mode_set_base(set->crtc, |
| 896 | set->x, set->y, old_fb); |
| 897 | if (ret != 0) |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 898 | goto fail; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 901 | kfree(save_connectors); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 902 | kfree(save_encoders); |
| 903 | kfree(save_crtcs); |
| 904 | return 0; |
| 905 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 906 | fail: |
| 907 | /* Restore all previous data. */ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 908 | count = 0; |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 909 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 910 | *crtc = save_crtcs[count++]; |
| 911 | } |
Chris Wilson | e62fb64 | 2009-02-11 16:39:21 +0000 | [diff] [blame] | 912 | |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 913 | count = 0; |
| 914 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 915 | *encoder = save_encoders[count++]; |
Chris Wilson | e62fb64 | 2009-02-11 16:39:21 +0000 | [diff] [blame] | 916 | } |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 917 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 918 | count = 0; |
| 919 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 920 | *connector = save_connectors[count++]; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 921 | } |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 922 | |
| 923 | kfree(save_connectors); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 924 | kfree(save_encoders); |
Maarten Maathuis | e67aae7 | 2009-08-27 10:18:29 +0200 | [diff] [blame^] | 925 | kfree(save_crtcs); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 926 | return ret; |
| 927 | } |
| 928 | EXPORT_SYMBOL(drm_crtc_helper_set_config); |
| 929 | |
| 930 | bool drm_helper_plugged_event(struct drm_device *dev) |
| 931 | { |
Zhao Yakui | 58367ed | 2009-07-20 13:48:07 +0800 | [diff] [blame] | 932 | DRM_DEBUG_KMS("\n"); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 933 | |
| 934 | drm_helper_probe_connector_modes(dev, dev->mode_config.max_width, |
| 935 | dev->mode_config.max_height); |
| 936 | |
| 937 | drm_setup_crtcs(dev); |
| 938 | |
| 939 | /* alert the driver fb layer */ |
| 940 | dev->mode_config.funcs->fb_changed(dev); |
| 941 | |
| 942 | /* FIXME: send hotplug event */ |
| 943 | return true; |
| 944 | } |
| 945 | /** |
| 946 | * drm_initial_config - setup a sane initial connector configuration |
| 947 | * @dev: DRM device |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 948 | * |
| 949 | * LOCKING: |
| 950 | * Called at init time, must take mode config lock. |
| 951 | * |
| 952 | * Scan the CRTCs and connectors and try to put together an initial setup. |
| 953 | * At the moment, this is a cloned configuration across all heads with |
| 954 | * a new framebuffer object as the backing store. |
| 955 | * |
| 956 | * RETURNS: |
| 957 | * Zero if everything went ok, nonzero otherwise. |
| 958 | */ |
Jesse Barnes | 7a1fb5d | 2009-03-27 13:05:19 -0700 | [diff] [blame] | 959 | bool drm_helper_initial_config(struct drm_device *dev) |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 960 | { |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 961 | struct drm_connector *connector; |
| 962 | int count = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 963 | |
Jesse Barnes | 40a518d | 2009-01-12 12:05:32 -0800 | [diff] [blame] | 964 | count = drm_helper_probe_connector_modes(dev, |
| 965 | dev->mode_config.max_width, |
| 966 | dev->mode_config.max_height); |
| 967 | |
| 968 | /* |
| 969 | * None of the available connectors had any modes, so add some |
| 970 | * and try to light them up anyway |
| 971 | */ |
| 972 | if (!count) { |
| 973 | DRM_ERROR("connectors have no modes, using standard modes\n"); |
| 974 | list_for_each_entry(connector, |
| 975 | &dev->mode_config.connector_list, |
| 976 | head) |
| 977 | drm_helper_add_std_modes(dev, connector); |
| 978 | } |
| 979 | |
| 980 | drm_setup_crtcs(dev); |
| 981 | |
| 982 | /* alert the driver fb layer */ |
| 983 | dev->mode_config.funcs->fb_changed(dev); |
| 984 | |
| 985 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 986 | } |
| 987 | EXPORT_SYMBOL(drm_helper_initial_config); |
| 988 | |
Keith Packard | c9fb15f | 2009-05-30 20:42:28 -0700 | [diff] [blame] | 989 | static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) |
| 990 | { |
| 991 | int dpms = DRM_MODE_DPMS_OFF; |
| 992 | struct drm_connector *connector; |
| 993 | struct drm_device *dev = encoder->dev; |
| 994 | |
| 995 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 996 | if (connector->encoder == encoder) |
| 997 | if (connector->dpms < dpms) |
| 998 | dpms = connector->dpms; |
| 999 | return dpms; |
| 1000 | } |
| 1001 | |
| 1002 | static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) |
| 1003 | { |
| 1004 | int dpms = DRM_MODE_DPMS_OFF; |
| 1005 | struct drm_connector *connector; |
| 1006 | struct drm_device *dev = crtc->dev; |
| 1007 | |
| 1008 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) |
| 1009 | if (connector->encoder && connector->encoder->crtc == crtc) |
| 1010 | if (connector->dpms < dpms) |
| 1011 | dpms = connector->dpms; |
| 1012 | return dpms; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * drm_helper_connector_dpms |
| 1017 | * @connector affected connector |
| 1018 | * @mode DPMS mode |
| 1019 | * |
| 1020 | * Calls the low-level connector DPMS function, then |
| 1021 | * calls appropriate encoder and crtc DPMS functions as well |
| 1022 | */ |
| 1023 | void drm_helper_connector_dpms(struct drm_connector *connector, int mode) |
| 1024 | { |
| 1025 | struct drm_encoder *encoder = connector->encoder; |
| 1026 | struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; |
| 1027 | int old_dpms; |
| 1028 | |
| 1029 | if (mode == connector->dpms) |
| 1030 | return; |
| 1031 | |
| 1032 | old_dpms = connector->dpms; |
| 1033 | connector->dpms = mode; |
| 1034 | |
| 1035 | /* from off to on, do crtc then encoder */ |
| 1036 | if (mode < old_dpms) { |
| 1037 | if (crtc) { |
| 1038 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 1039 | if (crtc_funcs->dpms) |
| 1040 | (*crtc_funcs->dpms) (crtc, |
| 1041 | drm_helper_choose_crtc_dpms(crtc)); |
| 1042 | } |
| 1043 | if (encoder) { |
| 1044 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; |
| 1045 | if (encoder_funcs->dpms) |
| 1046 | (*encoder_funcs->dpms) (encoder, |
| 1047 | drm_helper_choose_encoder_dpms(encoder)); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /* from on to off, do encoder then crtc */ |
| 1052 | if (mode > old_dpms) { |
| 1053 | if (encoder) { |
| 1054 | struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; |
| 1055 | if (encoder_funcs->dpms) |
| 1056 | (*encoder_funcs->dpms) (encoder, |
| 1057 | drm_helper_choose_encoder_dpms(encoder)); |
| 1058 | } |
| 1059 | if (crtc) { |
| 1060 | struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; |
| 1061 | if (crtc_funcs->dpms) |
| 1062 | (*crtc_funcs->dpms) (crtc, |
| 1063 | drm_helper_choose_crtc_dpms(crtc)); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | return; |
| 1068 | } |
| 1069 | EXPORT_SYMBOL(drm_helper_connector_dpms); |
| 1070 | |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1071 | /** |
| 1072 | * drm_hotplug_stage_two |
| 1073 | * @dev DRM device |
| 1074 | * @connector hotpluged connector |
| 1075 | * |
| 1076 | * LOCKING. |
| 1077 | * Caller must hold mode config lock, function might grab struct lock. |
| 1078 | * |
| 1079 | * Stage two of a hotplug. |
| 1080 | * |
| 1081 | * RETURNS: |
| 1082 | * Zero on success, errno on failure. |
| 1083 | */ |
| 1084 | int drm_helper_hotplug_stage_two(struct drm_device *dev) |
| 1085 | { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1086 | drm_helper_plugged_event(dev); |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | EXPORT_SYMBOL(drm_helper_hotplug_stage_two); |
| 1091 | |
| 1092 | int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, |
| 1093 | struct drm_mode_fb_cmd *mode_cmd) |
| 1094 | { |
| 1095 | fb->width = mode_cmd->width; |
| 1096 | fb->height = mode_cmd->height; |
| 1097 | fb->pitch = mode_cmd->pitch; |
| 1098 | fb->bits_per_pixel = mode_cmd->bpp; |
| 1099 | fb->depth = mode_cmd->depth; |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct); |
| 1104 | |
| 1105 | int drm_helper_resume_force_mode(struct drm_device *dev) |
| 1106 | { |
| 1107 | struct drm_crtc *crtc; |
| 1108 | int ret; |
| 1109 | |
| 1110 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 1111 | |
| 1112 | if (!crtc->enabled) |
| 1113 | continue; |
| 1114 | |
Kristian Høgsberg | 3c4fdcf | 2008-12-17 22:14:46 -0500 | [diff] [blame] | 1115 | ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, |
| 1116 | crtc->x, crtc->y, crtc->fb); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1117 | |
| 1118 | if (ret == false) |
| 1119 | DRM_ERROR("failed to set mode on crtc %p\n", crtc); |
| 1120 | } |
Zhao Yakui | af4fcb5 | 2009-07-08 14:13:13 +0800 | [diff] [blame] | 1121 | /* disable the unused connectors while restoring the modesetting */ |
| 1122 | drm_helper_disable_unused_functions(dev); |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | EXPORT_SYMBOL(drm_helper_resume_force_mode); |