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