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