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