1 1.6 riastrad /* $NetBSD: drm_probe_helper.c,v 1.6 2021/12/18 23:44:57 riastradh Exp $ */ 2 1.3 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright (c) 2006-2008 Intel Corporation 5 1.1 riastrad * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie> 6 1.1 riastrad * 7 1.1 riastrad * DRM core CRTC related functions 8 1.1 riastrad * 9 1.1 riastrad * Permission to use, copy, modify, distribute, and sell this software and its 10 1.1 riastrad * documentation for any purpose is hereby granted without fee, provided that 11 1.1 riastrad * the above copyright notice appear in all copies and that both that copyright 12 1.1 riastrad * notice and this permission notice appear in supporting documentation, and 13 1.1 riastrad * that the name of the copyright holders not be used in advertising or 14 1.1 riastrad * publicity pertaining to distribution of the software without specific, 15 1.1 riastrad * written prior permission. The copyright holders make no representations 16 1.1 riastrad * about the suitability of this software for any purpose. It is provided "as 17 1.1 riastrad * is" without express or implied warranty. 18 1.1 riastrad * 19 1.1 riastrad * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 20 1.1 riastrad * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 21 1.1 riastrad * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 22 1.1 riastrad * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 23 1.1 riastrad * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 24 1.1 riastrad * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 25 1.1 riastrad * OF THIS SOFTWARE. 26 1.1 riastrad * 27 1.1 riastrad * Authors: 28 1.1 riastrad * Keith Packard 29 1.1 riastrad * Eric Anholt <eric (at) anholt.net> 30 1.1 riastrad * Dave Airlie <airlied (at) linux.ie> 31 1.1 riastrad * Jesse Barnes <jesse.barnes (at) intel.com> 32 1.1 riastrad */ 33 1.1 riastrad 34 1.3 riastrad #include <sys/cdefs.h> 35 1.6 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_probe_helper.c,v 1.6 2021/12/18 23:44:57 riastradh Exp $"); 36 1.3 riastrad 37 1.1 riastrad #include <linux/export.h> 38 1.1 riastrad #include <linux/moduleparam.h> 39 1.1 riastrad 40 1.6 riastrad #include <drm/drm_bridge.h> 41 1.6 riastrad #include <drm/drm_client.h> 42 1.1 riastrad #include <drm/drm_crtc.h> 43 1.6 riastrad #include <drm/drm_edid.h> 44 1.6 riastrad #include <drm/drm_fb_helper.h> 45 1.1 riastrad #include <drm/drm_fourcc.h> 46 1.6 riastrad #include <drm/drm_modeset_helper_vtables.h> 47 1.6 riastrad #include <drm/drm_print.h> 48 1.6 riastrad #include <drm/drm_probe_helper.h> 49 1.6 riastrad #include <drm/drm_sysfs.h> 50 1.6 riastrad 51 1.6 riastrad #include "drm_crtc_helper_internal.h" 52 1.1 riastrad 53 1.1 riastrad /** 54 1.1 riastrad * DOC: output probing helper overview 55 1.1 riastrad * 56 1.1 riastrad * This library provides some helper code for output probing. It provides an 57 1.6 riastrad * implementation of the core &drm_connector_funcs.fill_modes interface with 58 1.6 riastrad * drm_helper_probe_single_connector_modes(). 59 1.1 riastrad * 60 1.1 riastrad * It also provides support for polling connectors with a work item and for 61 1.1 riastrad * generic hotplug interrupt handling where the driver doesn't or cannot keep 62 1.1 riastrad * track of a per-connector hpd interrupt. 63 1.1 riastrad * 64 1.1 riastrad * This helper library can be used independently of the modeset helper library. 65 1.1 riastrad * Drivers can also overwrite different parts e.g. use their own hotplug 66 1.1 riastrad * handling code to avoid probing unrelated outputs. 67 1.6 riastrad * 68 1.6 riastrad * The probe helpers share the function table structures with other display 69 1.6 riastrad * helper libraries. See &struct drm_connector_helper_funcs for the details. 70 1.1 riastrad */ 71 1.1 riastrad 72 1.1 riastrad static bool drm_kms_helper_poll = true; 73 1.1 riastrad module_param_named(poll, drm_kms_helper_poll, bool, 0600); 74 1.1 riastrad 75 1.3 riastrad static enum drm_mode_status 76 1.3 riastrad drm_mode_validate_flag(const struct drm_display_mode *mode, 77 1.3 riastrad int flags) 78 1.3 riastrad { 79 1.3 riastrad if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && 80 1.3 riastrad !(flags & DRM_MODE_FLAG_INTERLACE)) 81 1.3 riastrad return MODE_NO_INTERLACE; 82 1.3 riastrad 83 1.3 riastrad if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && 84 1.3 riastrad !(flags & DRM_MODE_FLAG_DBLSCAN)) 85 1.3 riastrad return MODE_NO_DBLESCAN; 86 1.3 riastrad 87 1.3 riastrad if ((mode->flags & DRM_MODE_FLAG_3D_MASK) && 88 1.3 riastrad !(flags & DRM_MODE_FLAG_3D_MASK)) 89 1.3 riastrad return MODE_NO_STEREO; 90 1.3 riastrad 91 1.3 riastrad return MODE_OK; 92 1.3 riastrad } 93 1.3 riastrad 94 1.6 riastrad static enum drm_mode_status 95 1.6 riastrad drm_mode_validate_pipeline(struct drm_display_mode *mode, 96 1.6 riastrad struct drm_connector *connector) 97 1.6 riastrad { 98 1.6 riastrad struct drm_device *dev = connector->dev; 99 1.6 riastrad enum drm_mode_status ret = MODE_OK; 100 1.6 riastrad struct drm_encoder *encoder; 101 1.6 riastrad 102 1.6 riastrad /* Step 1: Validate against connector */ 103 1.6 riastrad ret = drm_connector_mode_valid(connector, mode); 104 1.6 riastrad if (ret != MODE_OK) 105 1.6 riastrad return ret; 106 1.6 riastrad 107 1.6 riastrad /* Step 2: Validate against encoders and crtcs */ 108 1.6 riastrad drm_connector_for_each_possible_encoder(connector, encoder) { 109 1.6 riastrad struct drm_bridge *bridge; 110 1.6 riastrad struct drm_crtc *crtc; 111 1.6 riastrad 112 1.6 riastrad ret = drm_encoder_mode_valid(encoder, mode); 113 1.6 riastrad if (ret != MODE_OK) { 114 1.6 riastrad /* No point in continuing for crtc check as this encoder 115 1.6 riastrad * will not accept the mode anyway. If all encoders 116 1.6 riastrad * reject the mode then, at exit, ret will not be 117 1.6 riastrad * MODE_OK. */ 118 1.6 riastrad continue; 119 1.6 riastrad } 120 1.6 riastrad 121 1.6 riastrad bridge = drm_bridge_chain_get_first_bridge(encoder); 122 1.6 riastrad ret = drm_bridge_chain_mode_valid(bridge, mode); 123 1.6 riastrad if (ret != MODE_OK) { 124 1.6 riastrad /* There is also no point in continuing for crtc check 125 1.6 riastrad * here. */ 126 1.6 riastrad continue; 127 1.6 riastrad } 128 1.6 riastrad 129 1.6 riastrad drm_for_each_crtc(crtc, dev) { 130 1.6 riastrad if (!drm_encoder_crtc_ok(encoder, crtc)) 131 1.6 riastrad continue; 132 1.6 riastrad 133 1.6 riastrad ret = drm_crtc_mode_valid(crtc, mode); 134 1.6 riastrad if (ret == MODE_OK) { 135 1.6 riastrad /* If we get to this point there is at least 136 1.6 riastrad * one combination of encoder+crtc that works 137 1.6 riastrad * for this mode. Lets return now. */ 138 1.6 riastrad return ret; 139 1.6 riastrad } 140 1.6 riastrad } 141 1.6 riastrad } 142 1.6 riastrad 143 1.6 riastrad return ret; 144 1.6 riastrad } 145 1.6 riastrad 146 1.3 riastrad static int drm_helper_probe_add_cmdline_mode(struct drm_connector *connector) 147 1.1 riastrad { 148 1.6 riastrad struct drm_cmdline_mode *cmdline_mode; 149 1.1 riastrad struct drm_display_mode *mode; 150 1.1 riastrad 151 1.6 riastrad cmdline_mode = &connector->cmdline_mode; 152 1.6 riastrad if (!cmdline_mode->specified) 153 1.3 riastrad return 0; 154 1.1 riastrad 155 1.6 riastrad /* Only add a GTF mode if we find no matching probed modes */ 156 1.6 riastrad list_for_each_entry(mode, &connector->probed_modes, head) { 157 1.6 riastrad if (mode->hdisplay != cmdline_mode->xres || 158 1.6 riastrad mode->vdisplay != cmdline_mode->yres) 159 1.6 riastrad continue; 160 1.6 riastrad 161 1.6 riastrad if (cmdline_mode->refresh_specified) { 162 1.6 riastrad /* The probed mode's vrefresh is set until later */ 163 1.6 riastrad if (drm_mode_vrefresh(mode) != cmdline_mode->refresh) 164 1.6 riastrad continue; 165 1.6 riastrad } 166 1.6 riastrad 167 1.6 riastrad return 0; 168 1.6 riastrad } 169 1.6 riastrad 170 1.3 riastrad mode = drm_mode_create_from_cmdline_mode(connector->dev, 171 1.6 riastrad cmdline_mode); 172 1.3 riastrad if (mode == NULL) 173 1.3 riastrad return 0; 174 1.1 riastrad 175 1.3 riastrad drm_mode_probed_add(connector, mode); 176 1.3 riastrad return 1; 177 1.1 riastrad } 178 1.1 riastrad 179 1.6 riastrad enum drm_mode_status drm_crtc_mode_valid(struct drm_crtc *crtc, 180 1.6 riastrad const struct drm_display_mode *mode) 181 1.6 riastrad { 182 1.6 riastrad const struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 183 1.6 riastrad 184 1.6 riastrad if (!crtc_funcs || !crtc_funcs->mode_valid) 185 1.6 riastrad return MODE_OK; 186 1.6 riastrad 187 1.6 riastrad return crtc_funcs->mode_valid(crtc, mode); 188 1.6 riastrad } 189 1.6 riastrad 190 1.6 riastrad enum drm_mode_status drm_encoder_mode_valid(struct drm_encoder *encoder, 191 1.6 riastrad const struct drm_display_mode *mode) 192 1.6 riastrad { 193 1.6 riastrad const struct drm_encoder_helper_funcs *encoder_funcs = 194 1.6 riastrad encoder->helper_private; 195 1.6 riastrad 196 1.6 riastrad if (!encoder_funcs || !encoder_funcs->mode_valid) 197 1.6 riastrad return MODE_OK; 198 1.6 riastrad 199 1.6 riastrad return encoder_funcs->mode_valid(encoder, mode); 200 1.6 riastrad } 201 1.6 riastrad 202 1.6 riastrad enum drm_mode_status drm_connector_mode_valid(struct drm_connector *connector, 203 1.6 riastrad struct drm_display_mode *mode) 204 1.6 riastrad { 205 1.6 riastrad const struct drm_connector_helper_funcs *connector_funcs = 206 1.6 riastrad connector->helper_private; 207 1.6 riastrad 208 1.6 riastrad if (!connector_funcs || !connector_funcs->mode_valid) 209 1.6 riastrad return MODE_OK; 210 1.6 riastrad 211 1.6 riastrad return connector_funcs->mode_valid(connector, mode); 212 1.6 riastrad } 213 1.6 riastrad 214 1.3 riastrad #define DRM_OUTPUT_POLL_PERIOD (10*HZ) 215 1.1 riastrad /** 216 1.6 riastrad * drm_kms_helper_poll_enable - re-enable output polling. 217 1.3 riastrad * @dev: drm_device 218 1.1 riastrad * 219 1.6 riastrad * This function re-enables the output polling work, after it has been 220 1.6 riastrad * temporarily disabled using drm_kms_helper_poll_disable(), for example over 221 1.6 riastrad * suspend/resume. 222 1.1 riastrad * 223 1.6 riastrad * Drivers can call this helper from their device resume implementation. It is 224 1.6 riastrad * not an error to call this even when output polling isn't enabled. 225 1.6 riastrad * 226 1.6 riastrad * Note that calls to enable and disable polling must be strictly ordered, which 227 1.6 riastrad * is automatically the case when they're only call from suspend/resume 228 1.6 riastrad * callbacks. 229 1.1 riastrad */ 230 1.6 riastrad void drm_kms_helper_poll_enable(struct drm_device *dev) 231 1.3 riastrad { 232 1.3 riastrad bool poll = false; 233 1.3 riastrad struct drm_connector *connector; 234 1.6 riastrad struct drm_connector_list_iter conn_iter; 235 1.6 riastrad unsigned long delay = DRM_OUTPUT_POLL_PERIOD; 236 1.3 riastrad 237 1.3 riastrad if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) 238 1.3 riastrad return; 239 1.3 riastrad 240 1.6 riastrad drm_connector_list_iter_begin(dev, &conn_iter); 241 1.6 riastrad drm_for_each_connector_iter(connector, &conn_iter) { 242 1.3 riastrad if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 243 1.3 riastrad DRM_CONNECTOR_POLL_DISCONNECT)) 244 1.3 riastrad poll = true; 245 1.3 riastrad } 246 1.6 riastrad drm_connector_list_iter_end(&conn_iter); 247 1.6 riastrad 248 1.6 riastrad if (dev->mode_config.delayed_event) { 249 1.6 riastrad /* 250 1.6 riastrad * FIXME: 251 1.6 riastrad * 252 1.6 riastrad * Use short (1s) delay to handle the initial delayed event. 253 1.6 riastrad * This delay should not be needed, but Optimus/nouveau will 254 1.6 riastrad * fail in a mysterious way if the delayed event is handled as 255 1.6 riastrad * soon as possible like it is done in 256 1.6 riastrad * drm_helper_probe_single_connector_modes() in case the poll 257 1.6 riastrad * was enabled before. 258 1.6 riastrad */ 259 1.6 riastrad poll = true; 260 1.6 riastrad delay = HZ; 261 1.6 riastrad } 262 1.3 riastrad 263 1.3 riastrad if (poll) 264 1.6 riastrad schedule_delayed_work(&dev->mode_config.output_poll_work, delay); 265 1.3 riastrad } 266 1.6 riastrad EXPORT_SYMBOL(drm_kms_helper_poll_enable); 267 1.6 riastrad 268 1.6 riastrad static enum drm_connector_status 269 1.6 riastrad drm_helper_probe_detect_ctx(struct drm_connector *connector, bool force) 270 1.6 riastrad { 271 1.6 riastrad const struct drm_connector_helper_funcs *funcs = connector->helper_private; 272 1.6 riastrad struct drm_modeset_acquire_ctx ctx; 273 1.6 riastrad int ret; 274 1.6 riastrad 275 1.6 riastrad drm_modeset_acquire_init(&ctx, 0); 276 1.6 riastrad 277 1.6 riastrad retry: 278 1.6 riastrad ret = drm_modeset_lock(&connector->dev->mode_config.connection_mutex, &ctx); 279 1.6 riastrad if (!ret) { 280 1.6 riastrad if (funcs->detect_ctx) 281 1.6 riastrad ret = funcs->detect_ctx(connector, &ctx, force); 282 1.6 riastrad else if (connector->funcs->detect) 283 1.6 riastrad ret = connector->funcs->detect(connector, force); 284 1.6 riastrad else 285 1.6 riastrad ret = connector_status_connected; 286 1.6 riastrad } 287 1.6 riastrad 288 1.6 riastrad if (ret == -EDEADLK) { 289 1.6 riastrad drm_modeset_backoff(&ctx); 290 1.6 riastrad goto retry; 291 1.6 riastrad } 292 1.3 riastrad 293 1.6 riastrad if (WARN_ON(ret < 0)) 294 1.6 riastrad ret = connector_status_unknown; 295 1.3 riastrad 296 1.6 riastrad drm_modeset_drop_locks(&ctx); 297 1.6 riastrad drm_modeset_acquire_fini(&ctx); 298 1.6 riastrad 299 1.6 riastrad return ret; 300 1.6 riastrad } 301 1.6 riastrad 302 1.6 riastrad /** 303 1.6 riastrad * drm_helper_probe_detect - probe connector status 304 1.6 riastrad * @connector: connector to probe 305 1.6 riastrad * @ctx: acquire_ctx, or NULL to let this function handle locking. 306 1.6 riastrad * @force: Whether destructive probe operations should be performed. 307 1.6 riastrad * 308 1.6 riastrad * This function calls the detect callbacks of the connector. 309 1.6 riastrad * This function returns &drm_connector_status, or 310 1.6 riastrad * if @ctx is set, it might also return -EDEADLK. 311 1.6 riastrad */ 312 1.6 riastrad int 313 1.6 riastrad drm_helper_probe_detect(struct drm_connector *connector, 314 1.6 riastrad struct drm_modeset_acquire_ctx *ctx, 315 1.6 riastrad bool force) 316 1.6 riastrad { 317 1.6 riastrad const struct drm_connector_helper_funcs *funcs = connector->helper_private; 318 1.6 riastrad struct drm_device *dev = connector->dev; 319 1.6 riastrad int ret; 320 1.6 riastrad 321 1.6 riastrad if (!ctx) 322 1.6 riastrad return drm_helper_probe_detect_ctx(connector, force); 323 1.6 riastrad 324 1.6 riastrad ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx); 325 1.6 riastrad if (ret) 326 1.6 riastrad return ret; 327 1.6 riastrad 328 1.6 riastrad if (funcs->detect_ctx) 329 1.6 riastrad return funcs->detect_ctx(connector, ctx, force); 330 1.6 riastrad else if (connector->funcs->detect) 331 1.6 riastrad return connector->funcs->detect(connector, force); 332 1.6 riastrad else 333 1.6 riastrad return connector_status_connected; 334 1.6 riastrad } 335 1.6 riastrad EXPORT_SYMBOL(drm_helper_probe_detect); 336 1.6 riastrad 337 1.6 riastrad /** 338 1.6 riastrad * drm_helper_probe_single_connector_modes - get complete set of display modes 339 1.6 riastrad * @connector: connector to probe 340 1.6 riastrad * @maxX: max width for modes 341 1.6 riastrad * @maxY: max height for modes 342 1.6 riastrad * 343 1.6 riastrad * Based on the helper callbacks implemented by @connector in struct 344 1.6 riastrad * &drm_connector_helper_funcs try to detect all valid modes. Modes will first 345 1.6 riastrad * be added to the connector's probed_modes list, then culled (based on validity 346 1.6 riastrad * and the @maxX, @maxY parameters) and put into the normal modes list. 347 1.6 riastrad * 348 1.6 riastrad * Intended to be used as a generic implementation of the 349 1.6 riastrad * &drm_connector_funcs.fill_modes() vfunc for drivers that use the CRTC helpers 350 1.6 riastrad * for output mode filtering and detection. 351 1.6 riastrad * 352 1.6 riastrad * The basic procedure is as follows 353 1.6 riastrad * 354 1.6 riastrad * 1. All modes currently on the connector's modes list are marked as stale 355 1.6 riastrad * 356 1.6 riastrad * 2. New modes are added to the connector's probed_modes list with 357 1.6 riastrad * drm_mode_probed_add(). New modes start their life with status as OK. 358 1.6 riastrad * Modes are added from a single source using the following priority order. 359 1.6 riastrad * 360 1.6 riastrad * - &drm_connector_helper_funcs.get_modes vfunc 361 1.6 riastrad * - if the connector status is connector_status_connected, standard 362 1.6 riastrad * VESA DMT modes up to 1024x768 are automatically added 363 1.6 riastrad * (drm_add_modes_noedid()) 364 1.6 riastrad * 365 1.6 riastrad * Finally modes specified via the kernel command line (video=...) are 366 1.6 riastrad * added in addition to what the earlier probes produced 367 1.6 riastrad * (drm_helper_probe_add_cmdline_mode()). These modes are generated 368 1.6 riastrad * using the VESA GTF/CVT formulas. 369 1.6 riastrad * 370 1.6 riastrad * 3. Modes are moved from the probed_modes list to the modes list. Potential 371 1.6 riastrad * duplicates are merged together (see drm_connector_list_update()). 372 1.6 riastrad * After this step the probed_modes list will be empty again. 373 1.6 riastrad * 374 1.6 riastrad * 4. Any non-stale mode on the modes list then undergoes validation 375 1.6 riastrad * 376 1.6 riastrad * - drm_mode_validate_basic() performs basic sanity checks 377 1.6 riastrad * - drm_mode_validate_size() filters out modes larger than @maxX and @maxY 378 1.6 riastrad * (if specified) 379 1.6 riastrad * - drm_mode_validate_flag() checks the modes against basic connector 380 1.6 riastrad * capabilities (interlace_allowed,doublescan_allowed,stereo_allowed) 381 1.6 riastrad * - the optional &drm_connector_helper_funcs.mode_valid helper can perform 382 1.6 riastrad * driver and/or sink specific checks 383 1.6 riastrad * - the optional &drm_crtc_helper_funcs.mode_valid, 384 1.6 riastrad * &drm_bridge_funcs.mode_valid and &drm_encoder_helper_funcs.mode_valid 385 1.6 riastrad * helpers can perform driver and/or source specific checks which are also 386 1.6 riastrad * enforced by the modeset/atomic helpers 387 1.6 riastrad * 388 1.6 riastrad * 5. Any mode whose status is not OK is pruned from the connector's modes list, 389 1.6 riastrad * accompanied by a debug message indicating the reason for the mode's 390 1.6 riastrad * rejection (see drm_mode_prune_invalid()). 391 1.6 riastrad * 392 1.6 riastrad * Returns: 393 1.6 riastrad * The number of modes found on @connector. 394 1.6 riastrad */ 395 1.6 riastrad int drm_helper_probe_single_connector_modes(struct drm_connector *connector, 396 1.6 riastrad uint32_t maxX, uint32_t maxY) 397 1.1 riastrad { 398 1.1 riastrad struct drm_device *dev = connector->dev; 399 1.1 riastrad struct drm_display_mode *mode; 400 1.3 riastrad const struct drm_connector_helper_funcs *connector_funcs = 401 1.1 riastrad connector->helper_private; 402 1.6 riastrad int count = 0, ret; 403 1.1 riastrad int mode_flags = 0; 404 1.1 riastrad bool verbose_prune = true; 405 1.3 riastrad enum drm_connector_status old_status; 406 1.6 riastrad struct drm_modeset_acquire_ctx ctx; 407 1.1 riastrad 408 1.1 riastrad WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); 409 1.1 riastrad 410 1.6 riastrad drm_modeset_acquire_init(&ctx, 0); 411 1.6 riastrad 412 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, 413 1.3 riastrad connector->name); 414 1.6 riastrad 415 1.6 riastrad retry: 416 1.6 riastrad ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx); 417 1.6 riastrad if (ret == -EDEADLK) { 418 1.6 riastrad drm_modeset_backoff(&ctx); 419 1.6 riastrad goto retry; 420 1.6 riastrad } else 421 1.6 riastrad WARN_ON(ret < 0); 422 1.6 riastrad 423 1.6 riastrad /* set all old modes to the stale state */ 424 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) 425 1.6 riastrad mode->status = MODE_STALE; 426 1.6 riastrad 427 1.6 riastrad old_status = connector->status; 428 1.1 riastrad 429 1.1 riastrad if (connector->force) { 430 1.3 riastrad if (connector->force == DRM_FORCE_ON || 431 1.3 riastrad connector->force == DRM_FORCE_ON_DIGITAL) 432 1.1 riastrad connector->status = connector_status_connected; 433 1.1 riastrad else 434 1.1 riastrad connector->status = connector_status_disconnected; 435 1.1 riastrad if (connector->funcs->force) 436 1.1 riastrad connector->funcs->force(connector); 437 1.1 riastrad } else { 438 1.6 riastrad ret = drm_helper_probe_detect(connector, &ctx, true); 439 1.6 riastrad 440 1.6 riastrad if (ret == -EDEADLK) { 441 1.6 riastrad drm_modeset_backoff(&ctx); 442 1.6 riastrad goto retry; 443 1.6 riastrad } else if (WARN(ret < 0, "Invalid return value %i for connector detection\n", ret)) 444 1.6 riastrad ret = connector_status_unknown; 445 1.6 riastrad 446 1.6 riastrad connector->status = ret; 447 1.6 riastrad } 448 1.3 riastrad 449 1.6 riastrad /* 450 1.6 riastrad * Normally either the driver's hpd code or the poll loop should 451 1.6 riastrad * pick up any changes and fire the hotplug event. But if 452 1.6 riastrad * userspace sneaks in a probe, we might miss a change. Hence 453 1.6 riastrad * check here, and if anything changed start the hotplug code. 454 1.6 riastrad */ 455 1.6 riastrad if (old_status != connector->status) { 456 1.6 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", 457 1.6 riastrad connector->base.id, 458 1.6 riastrad connector->name, 459 1.6 riastrad drm_get_connector_status_name(old_status), 460 1.6 riastrad drm_get_connector_status_name(connector->status)); 461 1.3 riastrad 462 1.3 riastrad /* 463 1.6 riastrad * The hotplug event code might call into the fb 464 1.6 riastrad * helpers, and so expects that we do not hold any 465 1.6 riastrad * locks. Fire up the poll struct instead, it will 466 1.6 riastrad * disable itself again. 467 1.3 riastrad */ 468 1.6 riastrad dev->mode_config.delayed_event = true; 469 1.6 riastrad if (dev->mode_config.poll_enabled) 470 1.6 riastrad schedule_delayed_work(&dev->mode_config.output_poll_work, 471 1.6 riastrad 0); 472 1.1 riastrad } 473 1.1 riastrad 474 1.1 riastrad /* Re-enable polling in case the global poll config changed. */ 475 1.1 riastrad if (drm_kms_helper_poll != dev->mode_config.poll_running) 476 1.6 riastrad drm_kms_helper_poll_enable(dev); 477 1.1 riastrad 478 1.1 riastrad dev->mode_config.poll_running = drm_kms_helper_poll; 479 1.1 riastrad 480 1.1 riastrad if (connector->status == connector_status_disconnected) { 481 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", 482 1.3 riastrad connector->base.id, connector->name); 483 1.6 riastrad drm_connector_update_edid_property(connector, NULL); 484 1.1 riastrad verbose_prune = false; 485 1.1 riastrad goto prune; 486 1.1 riastrad } 487 1.1 riastrad 488 1.6 riastrad count = (*connector_funcs->get_modes)(connector); 489 1.6 riastrad 490 1.6 riastrad /* 491 1.6 riastrad * Fallback for when DDC probe failed in drm_get_edid() and thus skipped 492 1.6 riastrad * override/firmware EDID. 493 1.6 riastrad */ 494 1.6 riastrad if (count == 0 && connector->status == connector_status_connected) 495 1.6 riastrad count = drm_add_override_edid_modes(connector); 496 1.1 riastrad 497 1.1 riastrad if (count == 0 && connector->status == connector_status_connected) 498 1.1 riastrad count = drm_add_modes_noedid(connector, 1024, 768); 499 1.3 riastrad count += drm_helper_probe_add_cmdline_mode(connector); 500 1.1 riastrad if (count == 0) 501 1.1 riastrad goto prune; 502 1.1 riastrad 503 1.6 riastrad drm_connector_list_update(connector); 504 1.1 riastrad 505 1.1 riastrad if (connector->interlace_allowed) 506 1.1 riastrad mode_flags |= DRM_MODE_FLAG_INTERLACE; 507 1.1 riastrad if (connector->doublescan_allowed) 508 1.1 riastrad mode_flags |= DRM_MODE_FLAG_DBLSCAN; 509 1.1 riastrad if (connector->stereo_allowed) 510 1.1 riastrad mode_flags |= DRM_MODE_FLAG_3D_MASK; 511 1.1 riastrad 512 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) { 513 1.1 riastrad if (mode->status == MODE_OK) 514 1.6 riastrad mode->status = drm_mode_validate_driver(dev, mode); 515 1.3 riastrad 516 1.3 riastrad if (mode->status == MODE_OK) 517 1.3 riastrad mode->status = drm_mode_validate_size(mode, maxX, maxY); 518 1.3 riastrad 519 1.3 riastrad if (mode->status == MODE_OK) 520 1.3 riastrad mode->status = drm_mode_validate_flag(mode, mode_flags); 521 1.3 riastrad 522 1.6 riastrad if (mode->status == MODE_OK) 523 1.6 riastrad mode->status = drm_mode_validate_pipeline(mode, 524 1.6 riastrad connector); 525 1.6 riastrad 526 1.6 riastrad if (mode->status == MODE_OK) 527 1.6 riastrad mode->status = drm_mode_validate_ycbcr420(mode, 528 1.6 riastrad connector); 529 1.1 riastrad } 530 1.1 riastrad 531 1.1 riastrad prune: 532 1.1 riastrad drm_mode_prune_invalid(dev, &connector->modes, verbose_prune); 533 1.1 riastrad 534 1.6 riastrad drm_modeset_drop_locks(&ctx); 535 1.6 riastrad drm_modeset_acquire_fini(&ctx); 536 1.6 riastrad 537 1.1 riastrad if (list_empty(&connector->modes)) 538 1.1 riastrad return 0; 539 1.1 riastrad 540 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) 541 1.1 riastrad mode->vrefresh = drm_mode_vrefresh(mode); 542 1.1 riastrad 543 1.1 riastrad drm_mode_sort(&connector->modes); 544 1.1 riastrad 545 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, 546 1.3 riastrad connector->name); 547 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) { 548 1.1 riastrad drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 549 1.1 riastrad drm_mode_debug_printmodeline(mode); 550 1.1 riastrad } 551 1.1 riastrad 552 1.1 riastrad return count; 553 1.1 riastrad } 554 1.1 riastrad EXPORT_SYMBOL(drm_helper_probe_single_connector_modes); 555 1.1 riastrad 556 1.1 riastrad /** 557 1.1 riastrad * drm_kms_helper_hotplug_event - fire off KMS hotplug events 558 1.1 riastrad * @dev: drm_device whose connector state changed 559 1.1 riastrad * 560 1.1 riastrad * This function fires off the uevent for userspace and also calls the 561 1.1 riastrad * output_poll_changed function, which is most commonly used to inform the fbdev 562 1.1 riastrad * emulation code and allow it to update the fbcon output configuration. 563 1.1 riastrad * 564 1.1 riastrad * Drivers should call this from their hotplug handling code when a change is 565 1.1 riastrad * detected. Note that this function does not do any output detection of its 566 1.1 riastrad * own, like drm_helper_hpd_irq_event() does - this is assumed to be done by the 567 1.1 riastrad * driver already. 568 1.1 riastrad * 569 1.1 riastrad * This function must be called from process context with no mode 570 1.1 riastrad * setting locks held. 571 1.1 riastrad */ 572 1.1 riastrad void drm_kms_helper_hotplug_event(struct drm_device *dev) 573 1.1 riastrad { 574 1.5 maya #ifdef __NetBSD__ 575 1.5 maya sysmon_pswitch_event(&dev->sc_monitor_hotplug, PSWITCH_EVENT_PRESSED); 576 1.5 maya #endif 577 1.1 riastrad /* send a uevent + call fbdev */ 578 1.1 riastrad drm_sysfs_hotplug_event(dev); 579 1.1 riastrad if (dev->mode_config.funcs->output_poll_changed) 580 1.1 riastrad dev->mode_config.funcs->output_poll_changed(dev); 581 1.6 riastrad 582 1.6 riastrad drm_client_dev_hotplug(dev); 583 1.1 riastrad } 584 1.1 riastrad EXPORT_SYMBOL(drm_kms_helper_hotplug_event); 585 1.1 riastrad 586 1.1 riastrad static void output_poll_execute(struct work_struct *work) 587 1.1 riastrad { 588 1.1 riastrad struct delayed_work *delayed_work = to_delayed_work(work); 589 1.1 riastrad struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work); 590 1.1 riastrad struct drm_connector *connector; 591 1.6 riastrad struct drm_connector_list_iter conn_iter; 592 1.1 riastrad enum drm_connector_status old_status; 593 1.3 riastrad bool repoll = false, changed; 594 1.3 riastrad 595 1.6 riastrad if (!dev->mode_config.poll_enabled) 596 1.6 riastrad return; 597 1.6 riastrad 598 1.3 riastrad /* Pick up any changes detected by the probe functions. */ 599 1.3 riastrad changed = dev->mode_config.delayed_event; 600 1.3 riastrad dev->mode_config.delayed_event = false; 601 1.1 riastrad 602 1.1 riastrad if (!drm_kms_helper_poll) 603 1.3 riastrad goto out; 604 1.1 riastrad 605 1.6 riastrad if (!mutex_trylock(&dev->mode_config.mutex)) { 606 1.6 riastrad repoll = true; 607 1.6 riastrad goto out; 608 1.6 riastrad } 609 1.1 riastrad 610 1.6 riastrad drm_connector_list_iter_begin(dev, &conn_iter); 611 1.6 riastrad drm_for_each_connector_iter(connector, &conn_iter) { 612 1.1 riastrad /* Ignore forced connectors. */ 613 1.1 riastrad if (connector->force) 614 1.1 riastrad continue; 615 1.1 riastrad 616 1.1 riastrad /* Ignore HPD capable connectors and connectors where we don't 617 1.1 riastrad * want any hotplug detection at all for polling. */ 618 1.1 riastrad if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD) 619 1.1 riastrad continue; 620 1.1 riastrad 621 1.1 riastrad old_status = connector->status; 622 1.1 riastrad /* if we are connected and don't want to poll for disconnect 623 1.1 riastrad skip it */ 624 1.1 riastrad if (old_status == connector_status_connected && 625 1.1 riastrad !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT)) 626 1.1 riastrad continue; 627 1.1 riastrad 628 1.3 riastrad repoll = true; 629 1.3 riastrad 630 1.6 riastrad connector->status = drm_helper_probe_detect(connector, NULL, false); 631 1.1 riastrad if (old_status != connector->status) { 632 1.1 riastrad const char *old, *new; 633 1.1 riastrad 634 1.3 riastrad /* 635 1.3 riastrad * The poll work sets force=false when calling detect so 636 1.3 riastrad * that drivers can avoid to do disruptive tests (e.g. 637 1.3 riastrad * when load detect cycles could cause flickering on 638 1.3 riastrad * other, running displays). This bears the risk that we 639 1.3 riastrad * flip-flop between unknown here in the poll work and 640 1.3 riastrad * the real state when userspace forces a full detect 641 1.3 riastrad * call after receiving a hotplug event due to this 642 1.3 riastrad * change. 643 1.3 riastrad * 644 1.3 riastrad * Hence clamp an unknown detect status to the old 645 1.3 riastrad * value. 646 1.3 riastrad */ 647 1.3 riastrad if (connector->status == connector_status_unknown) { 648 1.3 riastrad connector->status = old_status; 649 1.3 riastrad continue; 650 1.3 riastrad } 651 1.3 riastrad 652 1.1 riastrad old = drm_get_connector_status_name(old_status); 653 1.1 riastrad new = drm_get_connector_status_name(connector->status); 654 1.1 riastrad 655 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s] " 656 1.1 riastrad "status updated from %s to %s\n", 657 1.1 riastrad connector->base.id, 658 1.3 riastrad connector->name, 659 1.1 riastrad old, new); 660 1.1 riastrad 661 1.1 riastrad changed = true; 662 1.1 riastrad } 663 1.1 riastrad } 664 1.6 riastrad drm_connector_list_iter_end(&conn_iter); 665 1.1 riastrad 666 1.1 riastrad mutex_unlock(&dev->mode_config.mutex); 667 1.1 riastrad 668 1.3 riastrad out: 669 1.1 riastrad if (changed) 670 1.1 riastrad drm_kms_helper_hotplug_event(dev); 671 1.1 riastrad 672 1.1 riastrad if (repoll) 673 1.1 riastrad schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD); 674 1.1 riastrad } 675 1.1 riastrad 676 1.1 riastrad /** 677 1.3 riastrad * drm_kms_helper_is_poll_worker - is %current task an output poll worker? 678 1.3 riastrad * 679 1.3 riastrad * Determine if %current task is an output poll worker. This can be used 680 1.3 riastrad * to select distinct code paths for output polling versus other contexts. 681 1.3 riastrad * 682 1.3 riastrad * One use case is to avoid a deadlock between the output poll worker and 683 1.3 riastrad * the autosuspend worker wherein the latter waits for polling to finish 684 1.3 riastrad * upon calling drm_kms_helper_poll_disable(), while the former waits for 685 1.3 riastrad * runtime suspend to finish upon calling pm_runtime_get_sync() in a 686 1.3 riastrad * connector ->detect hook. 687 1.3 riastrad */ 688 1.3 riastrad bool drm_kms_helper_is_poll_worker(void) 689 1.3 riastrad { 690 1.3 riastrad struct work_struct *work = current_work(); 691 1.3 riastrad 692 1.3 riastrad return work && work->func == output_poll_execute; 693 1.3 riastrad } 694 1.3 riastrad EXPORT_SYMBOL(drm_kms_helper_is_poll_worker); 695 1.3 riastrad 696 1.3 riastrad /** 697 1.1 riastrad * drm_kms_helper_poll_disable - disable output polling 698 1.1 riastrad * @dev: drm_device 699 1.1 riastrad * 700 1.1 riastrad * This function disables the output polling work. 701 1.1 riastrad * 702 1.1 riastrad * Drivers can call this helper from their device suspend implementation. It is 703 1.6 riastrad * not an error to call this even when output polling isn't enabled or already 704 1.6 riastrad * disabled. Polling is re-enabled by calling drm_kms_helper_poll_enable(). 705 1.6 riastrad * 706 1.6 riastrad * Note that calls to enable and disable polling must be strictly ordered, which 707 1.6 riastrad * is automatically the case when they're only call from suspend/resume 708 1.6 riastrad * callbacks. 709 1.1 riastrad */ 710 1.1 riastrad void drm_kms_helper_poll_disable(struct drm_device *dev) 711 1.1 riastrad { 712 1.1 riastrad if (!dev->mode_config.poll_enabled) 713 1.1 riastrad return; 714 1.1 riastrad cancel_delayed_work_sync(&dev->mode_config.output_poll_work); 715 1.1 riastrad } 716 1.1 riastrad EXPORT_SYMBOL(drm_kms_helper_poll_disable); 717 1.1 riastrad 718 1.1 riastrad /** 719 1.1 riastrad * drm_kms_helper_poll_init - initialize and enable output polling 720 1.1 riastrad * @dev: drm_device 721 1.1 riastrad * 722 1.1 riastrad * This function intializes and then also enables output polling support for 723 1.1 riastrad * @dev. Drivers which do not have reliable hotplug support in hardware can use 724 1.1 riastrad * this helper infrastructure to regularly poll such connectors for changes in 725 1.1 riastrad * their connection state. 726 1.1 riastrad * 727 1.1 riastrad * Drivers can control which connectors are polled by setting the 728 1.1 riastrad * DRM_CONNECTOR_POLL_CONNECT and DRM_CONNECTOR_POLL_DISCONNECT flags. On 729 1.1 riastrad * connectors where probing live outputs can result in visual distortion drivers 730 1.1 riastrad * should not set the DRM_CONNECTOR_POLL_DISCONNECT flag to avoid this. 731 1.1 riastrad * Connectors which have no flag or only DRM_CONNECTOR_POLL_HPD set are 732 1.1 riastrad * completely ignored by the polling logic. 733 1.1 riastrad * 734 1.1 riastrad * Note that a connector can be both polled and probed from the hotplug handler, 735 1.1 riastrad * in case the hotplug interrupt is known to be unreliable. 736 1.1 riastrad */ 737 1.1 riastrad void drm_kms_helper_poll_init(struct drm_device *dev) 738 1.1 riastrad { 739 1.1 riastrad INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute); 740 1.1 riastrad dev->mode_config.poll_enabled = true; 741 1.1 riastrad 742 1.1 riastrad drm_kms_helper_poll_enable(dev); 743 1.1 riastrad } 744 1.1 riastrad EXPORT_SYMBOL(drm_kms_helper_poll_init); 745 1.1 riastrad 746 1.1 riastrad /** 747 1.1 riastrad * drm_kms_helper_poll_fini - disable output polling and clean it up 748 1.1 riastrad * @dev: drm_device 749 1.1 riastrad */ 750 1.1 riastrad void drm_kms_helper_poll_fini(struct drm_device *dev) 751 1.1 riastrad { 752 1.6 riastrad if (!dev->mode_config.poll_enabled) 753 1.6 riastrad return; 754 1.6 riastrad 755 1.6 riastrad dev->mode_config.poll_enabled = false; 756 1.6 riastrad cancel_delayed_work_sync(&dev->mode_config.output_poll_work); 757 1.1 riastrad } 758 1.1 riastrad EXPORT_SYMBOL(drm_kms_helper_poll_fini); 759 1.1 riastrad 760 1.1 riastrad /** 761 1.1 riastrad * drm_helper_hpd_irq_event - hotplug processing 762 1.1 riastrad * @dev: drm_device 763 1.1 riastrad * 764 1.1 riastrad * Drivers can use this helper function to run a detect cycle on all connectors 765 1.1 riastrad * which have the DRM_CONNECTOR_POLL_HPD flag set in their &polled member. All 766 1.1 riastrad * other connectors are ignored, which is useful to avoid reprobing fixed 767 1.1 riastrad * panels. 768 1.1 riastrad * 769 1.1 riastrad * This helper function is useful for drivers which can't or don't track hotplug 770 1.1 riastrad * interrupts for each connector. 771 1.1 riastrad * 772 1.1 riastrad * Drivers which support hotplug interrupts for each connector individually and 773 1.1 riastrad * which have a more fine-grained detect logic should bypass this code and 774 1.1 riastrad * directly call drm_kms_helper_hotplug_event() in case the connector state 775 1.1 riastrad * changed. 776 1.1 riastrad * 777 1.1 riastrad * This function must be called from process context with no mode 778 1.1 riastrad * setting locks held. 779 1.1 riastrad * 780 1.1 riastrad * Note that a connector can be both polled and probed from the hotplug handler, 781 1.1 riastrad * in case the hotplug interrupt is known to be unreliable. 782 1.1 riastrad */ 783 1.1 riastrad bool drm_helper_hpd_irq_event(struct drm_device *dev) 784 1.1 riastrad { 785 1.1 riastrad struct drm_connector *connector; 786 1.6 riastrad struct drm_connector_list_iter conn_iter; 787 1.1 riastrad enum drm_connector_status old_status; 788 1.1 riastrad bool changed = false; 789 1.1 riastrad 790 1.1 riastrad if (!dev->mode_config.poll_enabled) 791 1.1 riastrad return false; 792 1.1 riastrad 793 1.1 riastrad mutex_lock(&dev->mode_config.mutex); 794 1.6 riastrad drm_connector_list_iter_begin(dev, &conn_iter); 795 1.6 riastrad drm_for_each_connector_iter(connector, &conn_iter) { 796 1.1 riastrad /* Only handle HPD capable connectors. */ 797 1.1 riastrad if (!(connector->polled & DRM_CONNECTOR_POLL_HPD)) 798 1.1 riastrad continue; 799 1.1 riastrad 800 1.1 riastrad old_status = connector->status; 801 1.1 riastrad 802 1.6 riastrad connector->status = drm_helper_probe_detect(connector, NULL, false); 803 1.1 riastrad DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", 804 1.1 riastrad connector->base.id, 805 1.3 riastrad connector->name, 806 1.1 riastrad drm_get_connector_status_name(old_status), 807 1.1 riastrad drm_get_connector_status_name(connector->status)); 808 1.1 riastrad if (old_status != connector->status) 809 1.1 riastrad changed = true; 810 1.1 riastrad } 811 1.6 riastrad drm_connector_list_iter_end(&conn_iter); 812 1.1 riastrad mutex_unlock(&dev->mode_config.mutex); 813 1.1 riastrad 814 1.1 riastrad if (changed) 815 1.1 riastrad drm_kms_helper_hotplug_event(dev); 816 1.1 riastrad 817 1.1 riastrad return changed; 818 1.1 riastrad } 819 1.1 riastrad EXPORT_SYMBOL(drm_helper_hpd_irq_event); 820