2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include <linux/kernel.h>
26 #include <drm/i915_drm.h>
29 #include "intel_drv.h"
30 #include "intel_hotplug.h"
35 * Simply put, hotplug occurs when a display is connected to or disconnected
36 * from the system. However, there may be adapters and docking stations and
37 * Display Port short pulses and MST devices involved, complicating matters.
39 * Hotplug in i915 is handled in many different levels of abstraction.
41 * The platform dependent interrupt handling code in i915_irq.c enables,
42 * disables, and does preliminary handling of the interrupts. The interrupt
43 * handlers gather the hotplug detect (HPD) information from relevant registers
44 * into a platform independent mask of hotplug pins that have fired.
46 * The platform independent interrupt handler intel_hpd_irq_handler() in
47 * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
48 * further processing to appropriate bottom halves (Display Port specific and
51 * The Display Port work function i915_digport_work_func() calls into
52 * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
53 * pulses, with failures and non-MST long pulses triggering regular hotplug
54 * processing on the connector.
56 * The regular hotplug work function i915_hotplug_work_func() calls connector
57 * detect hooks, and, if connector status changes, triggers sending of hotplug
58 * uevent to userspace via drm_kms_helper_hotplug_event().
60 * Finally, the userspace is responsible for triggering a modeset upon receiving
61 * the hotplug uevent, disabling or enabling the crtc as needed.
63 * The hotplug interrupt storm detection and mitigation code keeps track of the
64 * number of interrupts per hotplug pin per a period of time, and if the number
65 * of interrupts exceeds a certain threshold, the interrupt is disabled for a
66 * while before being re-enabled. The intention is to mitigate issues raising
67 * from broken hardware triggering massive amounts of interrupts and grinding
68 * the system to a halt.
70 * Current implementation expects that hotplug interrupt storm will not be
71 * seen when display port sink is connected, hence on platforms whose DP
72 * callback is handled by i915_digport_work_func reenabling of hpd is not
73 * performed (it was never expected to be disabled in the first place ;) )
74 * this is specific to DP sinks handled by this routine and any other display
75 * such as HDMI or DVI enabled on the same port will have proper logic since
76 * it will use i915_hotplug_work_func where this logic is handled.
80 * intel_hpd_pin_default - return default pin associated with certain port.
81 * @dev_priv: private driver data pointer
82 * @port: the hpd port to get associated pin
84 * It is only valid and used by digital port encoder.
86 * Return pin that is associatade with @port and HDP_NONE if no pin is
87 * hard associated with that @port.
89 enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
104 if (IS_CNL_WITH_PORT_F(dev_priv))
113 #define HPD_STORM_DETECT_PERIOD 1000
114 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
117 * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
118 * @dev_priv: private driver data pointer
119 * @pin: the pin to gather stats on
120 * @long_hpd: whether the HPD IRQ was long or short
122 * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
123 * storms. Only the pin specific stats and state are changed, the caller is
124 * responsible for further action.
126 * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
127 * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
128 * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
129 * short IRQs count as +1. If this threshold is exceeded, it's considered an
130 * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
132 * By default, most systems will only count long IRQs towards
133 * &dev_priv->hotplug.hpd_storm_threshold. However, some older systems also
134 * suffer from short IRQ storms and must also track these. Because short IRQ
135 * storms are naturally caused by sideband interactions with DP MST devices,
136 * short IRQ detection is only enabled for systems without DP MST support.
137 * Systems which are new enough to support DP MST are far less likely to
138 * suffer from IRQ storms at all, so this is fine.
140 * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
141 * and should only be adjusted for automated hotplug testing.
143 * Return true if an IRQ storm was detected on @pin.
145 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
146 enum hpd_pin pin, bool long_hpd)
148 struct i915_hotplug *hpd = &dev_priv->hotplug;
149 unsigned long start = hpd->stats[pin].last_jiffies;
150 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
151 const int increment = long_hpd ? 10 : 1;
152 const int threshold = hpd->hpd_storm_threshold;
156 (!long_hpd && !dev_priv->hotplug.hpd_short_storm_enabled))
159 if (!time_in_range(jiffies, start, end)) {
160 hpd->stats[pin].last_jiffies = jiffies;
161 hpd->stats[pin].count = 0;
164 hpd->stats[pin].count += increment;
165 if (hpd->stats[pin].count > threshold) {
166 hpd->stats[pin].state = HPD_MARK_DISABLED;
167 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
170 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
171 hpd->stats[pin].count);
178 intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
180 struct drm_device *dev = &dev_priv->drm;
181 struct intel_connector *intel_connector;
182 struct intel_encoder *intel_encoder;
183 struct drm_connector *connector;
184 struct drm_connector_list_iter conn_iter;
186 bool hpd_disabled = false;
188 lockdep_assert_held(&dev_priv->irq_lock);
190 drm_connector_list_iter_begin(dev, &conn_iter);
191 drm_for_each_connector_iter(connector, &conn_iter) {
192 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
195 intel_connector = to_intel_connector(connector);
196 intel_encoder = intel_connector->encoder;
200 pin = intel_encoder->hpd_pin;
201 if (pin == HPD_NONE ||
202 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
205 DRM_INFO("HPD interrupt storm detected on connector %s: "
206 "switching from hotplug detection to polling\n",
209 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
210 connector->polled = DRM_CONNECTOR_POLL_CONNECT
211 | DRM_CONNECTOR_POLL_DISCONNECT;
214 drm_connector_list_iter_end(&conn_iter);
216 /* Enable polling and queue hotplug re-enabling. */
218 drm_kms_helper_poll_enable(dev);
219 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
220 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
224 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
226 struct drm_i915_private *dev_priv =
227 container_of(work, typeof(*dev_priv),
228 hotplug.reenable_work.work);
229 struct drm_device *dev = &dev_priv->drm;
230 intel_wakeref_t wakeref;
233 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
235 spin_lock_irq(&dev_priv->irq_lock);
236 for_each_hpd_pin(pin) {
237 struct drm_connector *connector;
238 struct drm_connector_list_iter conn_iter;
240 if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
243 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
245 drm_connector_list_iter_begin(dev, &conn_iter);
246 drm_for_each_connector_iter(connector, &conn_iter) {
247 struct intel_connector *intel_connector = to_intel_connector(connector);
249 /* Don't check MST ports, they don't have pins */
250 if (!intel_connector->mst_port &&
251 intel_connector->encoder->hpd_pin == pin) {
252 if (connector->polled != intel_connector->polled)
253 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
255 connector->polled = intel_connector->polled;
256 if (!connector->polled)
257 connector->polled = DRM_CONNECTOR_POLL_HPD;
260 drm_connector_list_iter_end(&conn_iter);
262 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
263 dev_priv->display.hpd_irq_setup(dev_priv);
264 spin_unlock_irq(&dev_priv->irq_lock);
266 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
269 bool intel_encoder_hotplug(struct intel_encoder *encoder,
270 struct intel_connector *connector)
272 struct drm_device *dev = connector->base.dev;
273 enum drm_connector_status old_status;
275 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
276 old_status = connector->base.status;
278 connector->base.status =
279 drm_helper_probe_detect(&connector->base, NULL, false);
281 if (old_status == connector->base.status)
284 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
285 connector->base.base.id,
286 connector->base.name,
287 drm_get_connector_status_name(old_status),
288 drm_get_connector_status_name(connector->base.status));
293 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
295 return intel_encoder_is_dig_port(encoder) &&
296 enc_to_dig_port(&encoder->base)->hpd_pulse != NULL;
299 static void i915_digport_work_func(struct work_struct *work)
301 struct drm_i915_private *dev_priv =
302 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
303 u32 long_port_mask, short_port_mask;
304 struct intel_encoder *encoder;
307 spin_lock_irq(&dev_priv->irq_lock);
308 long_port_mask = dev_priv->hotplug.long_port_mask;
309 dev_priv->hotplug.long_port_mask = 0;
310 short_port_mask = dev_priv->hotplug.short_port_mask;
311 dev_priv->hotplug.short_port_mask = 0;
312 spin_unlock_irq(&dev_priv->irq_lock);
314 for_each_intel_encoder(&dev_priv->drm, encoder) {
315 struct intel_digital_port *dig_port;
316 enum port port = encoder->port;
317 bool long_hpd, short_hpd;
320 if (!intel_encoder_has_hpd_pulse(encoder))
323 long_hpd = long_port_mask & BIT(port);
324 short_hpd = short_port_mask & BIT(port);
326 if (!long_hpd && !short_hpd)
329 dig_port = enc_to_dig_port(&encoder->base);
331 ret = dig_port->hpd_pulse(dig_port, long_hpd);
332 if (ret == IRQ_NONE) {
333 /* fall back to old school hpd */
334 old_bits |= BIT(encoder->hpd_pin);
339 spin_lock_irq(&dev_priv->irq_lock);
340 dev_priv->hotplug.event_bits |= old_bits;
341 spin_unlock_irq(&dev_priv->irq_lock);
342 schedule_work(&dev_priv->hotplug.hotplug_work);
347 * Handle hotplug events outside the interrupt handler proper.
349 static void i915_hotplug_work_func(struct work_struct *work)
351 struct drm_i915_private *dev_priv =
352 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
353 struct drm_device *dev = &dev_priv->drm;
354 struct intel_connector *intel_connector;
355 struct intel_encoder *intel_encoder;
356 struct drm_connector *connector;
357 struct drm_connector_list_iter conn_iter;
358 bool changed = false;
361 mutex_lock(&dev->mode_config.mutex);
362 DRM_DEBUG_KMS("running encoder hotplug functions\n");
364 spin_lock_irq(&dev_priv->irq_lock);
366 hpd_event_bits = dev_priv->hotplug.event_bits;
367 dev_priv->hotplug.event_bits = 0;
369 /* Enable polling for connectors which had HPD IRQ storms */
370 intel_hpd_irq_storm_switch_to_polling(dev_priv);
372 spin_unlock_irq(&dev_priv->irq_lock);
374 drm_connector_list_iter_begin(dev, &conn_iter);
375 drm_for_each_connector_iter(connector, &conn_iter) {
376 intel_connector = to_intel_connector(connector);
377 if (!intel_connector->encoder)
379 intel_encoder = intel_connector->encoder;
380 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
381 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
382 connector->name, intel_encoder->hpd_pin);
384 changed |= intel_encoder->hotplug(intel_encoder,
388 drm_connector_list_iter_end(&conn_iter);
389 mutex_unlock(&dev->mode_config.mutex);
392 drm_kms_helper_hotplug_event(dev);
397 * intel_hpd_irq_handler - main hotplug irq handler
398 * @dev_priv: drm_i915_private
399 * @pin_mask: a mask of hpd pins that have triggered the irq
400 * @long_mask: a mask of hpd pins that may be long hpd pulses
402 * This is the main hotplug irq handler for all platforms. The platform specific
403 * irq handlers call the platform specific hotplug irq handlers, which read and
404 * decode the appropriate registers into bitmasks about hpd pins that have
405 * triggered (@pin_mask), and which of those pins may be long pulses
406 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
407 * is not a digital port.
409 * Here, we do hotplug irq storm detection and mitigation, and pass further
410 * processing to appropriate bottom halves.
412 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
413 u32 pin_mask, u32 long_mask)
415 struct intel_encoder *encoder;
416 bool storm_detected = false;
417 bool queue_dig = false, queue_hp = false;
418 u32 long_hpd_pulse_mask = 0;
419 u32 short_hpd_pulse_mask = 0;
425 spin_lock(&dev_priv->irq_lock);
428 * Determine whether ->hpd_pulse() exists for each pin, and
429 * whether we have a short or a long pulse. This is needed
430 * as each pin may have up to two encoders (HDMI and DP) and
431 * only the one of them (DP) will have ->hpd_pulse().
433 for_each_intel_encoder(&dev_priv->drm, encoder) {
434 bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
435 enum port port = encoder->port;
438 pin = encoder->hpd_pin;
439 if (!(BIT(pin) & pin_mask))
445 long_hpd = long_mask & BIT(pin);
447 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
448 long_hpd ? "long" : "short");
452 long_hpd_pulse_mask |= BIT(pin);
453 dev_priv->hotplug.long_port_mask |= BIT(port);
455 short_hpd_pulse_mask |= BIT(pin);
456 dev_priv->hotplug.short_port_mask |= BIT(port);
460 /* Now process each pin just once */
461 for_each_hpd_pin(pin) {
464 if (!(BIT(pin) & pin_mask))
467 if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
469 * On GMCH platforms the interrupt mask bits only
470 * prevent irq generation, not the setting of the
471 * hotplug bits itself. So only WARN about unexpected
472 * interrupts on saner platforms.
474 WARN_ONCE(!HAS_GMCH(dev_priv),
475 "Received HPD interrupt on pin %d although disabled\n", pin);
479 if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
483 * Delegate to ->hpd_pulse() if one of the encoders for this
484 * pin has it, otherwise let the hotplug_work deal with this
487 if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
488 long_hpd = long_hpd_pulse_mask & BIT(pin);
490 dev_priv->hotplug.event_bits |= BIT(pin);
495 if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
496 dev_priv->hotplug.event_bits &= ~BIT(pin);
497 storm_detected = true;
503 * Disable any IRQs that storms were detected on. Polling enablement
504 * happens later in our hotplug work.
506 if (storm_detected && dev_priv->display_irqs_enabled)
507 dev_priv->display.hpd_irq_setup(dev_priv);
508 spin_unlock(&dev_priv->irq_lock);
511 * Our hotplug handler can grab modeset locks (by calling down into the
512 * fb helpers). Hence it must not be run on our own dev-priv->wq work
513 * queue for otherwise the flush_work in the pageflip code will
517 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
519 schedule_work(&dev_priv->hotplug.hotplug_work);
523 * intel_hpd_init - initializes and enables hpd support
524 * @dev_priv: i915 device instance
526 * This function enables the hotplug support. It requires that interrupts have
527 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
528 * poll request can run concurrently to other code, so locking rules must be
531 * This is a separate step from interrupt enabling to simplify the locking rules
532 * in the driver load and resume code.
534 * Also see: intel_hpd_poll_init(), which enables connector polling
536 void intel_hpd_init(struct drm_i915_private *dev_priv)
540 for_each_hpd_pin(i) {
541 dev_priv->hotplug.stats[i].count = 0;
542 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
545 WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
546 schedule_work(&dev_priv->hotplug.poll_init_work);
549 * Interrupt setup is already guaranteed to be single-threaded, this is
550 * just to make the assert_spin_locked checks happy.
552 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
553 spin_lock_irq(&dev_priv->irq_lock);
554 if (dev_priv->display_irqs_enabled)
555 dev_priv->display.hpd_irq_setup(dev_priv);
556 spin_unlock_irq(&dev_priv->irq_lock);
560 static void i915_hpd_poll_init_work(struct work_struct *work)
562 struct drm_i915_private *dev_priv =
563 container_of(work, struct drm_i915_private,
564 hotplug.poll_init_work);
565 struct drm_device *dev = &dev_priv->drm;
566 struct drm_connector *connector;
567 struct drm_connector_list_iter conn_iter;
570 mutex_lock(&dev->mode_config.mutex);
572 enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
574 drm_connector_list_iter_begin(dev, &conn_iter);
575 drm_for_each_connector_iter(connector, &conn_iter) {
576 struct intel_connector *intel_connector =
577 to_intel_connector(connector);
578 connector->polled = intel_connector->polled;
580 /* MST has a dynamic intel_connector->encoder and it's reprobing
581 * is all handled by the MST helpers. */
582 if (intel_connector->mst_port)
585 if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
586 intel_connector->encoder->hpd_pin > HPD_NONE) {
587 connector->polled = enabled ?
588 DRM_CONNECTOR_POLL_CONNECT |
589 DRM_CONNECTOR_POLL_DISCONNECT :
590 DRM_CONNECTOR_POLL_HPD;
593 drm_connector_list_iter_end(&conn_iter);
596 drm_kms_helper_poll_enable(dev);
598 mutex_unlock(&dev->mode_config.mutex);
601 * We might have missed any hotplugs that happened while we were
602 * in the middle of disabling polling
605 drm_helper_hpd_irq_event(dev);
609 * intel_hpd_poll_init - enables/disables polling for connectors with hpd
610 * @dev_priv: i915 device instance
612 * This function enables polling for all connectors, regardless of whether or
613 * not they support hotplug detection. Under certain conditions HPD may not be
614 * functional. On most Intel GPUs, this happens when we enter runtime suspend.
615 * On Valleyview and Cherryview systems, this also happens when we shut off all
618 * Since this function can get called in contexts where we're already holding
619 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
622 * Also see: intel_hpd_init(), which restores hpd handling.
624 void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
626 WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
629 * We might already be holding dev->mode_config.mutex, so do this in a
631 * As well, there's no issue if we race here since we always reschedule
634 schedule_work(&dev_priv->hotplug.poll_init_work);
637 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
639 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
640 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
641 INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
642 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
643 intel_hpd_irq_storm_reenable_work);
646 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
648 spin_lock_irq(&dev_priv->irq_lock);
650 dev_priv->hotplug.long_port_mask = 0;
651 dev_priv->hotplug.short_port_mask = 0;
652 dev_priv->hotplug.event_bits = 0;
654 spin_unlock_irq(&dev_priv->irq_lock);
656 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
657 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
658 cancel_work_sync(&dev_priv->hotplug.poll_init_work);
659 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
662 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
669 spin_lock_irq(&dev_priv->irq_lock);
670 if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
671 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
674 spin_unlock_irq(&dev_priv->irq_lock);
679 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
684 spin_lock_irq(&dev_priv->irq_lock);
685 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
686 spin_unlock_irq(&dev_priv->irq_lock);