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