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