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/debugfs.h>
25 #include <linux/kernel.h>
27 #include <drm/drm_probe_helper.h>
31 #include "intel_display_power.h"
32 #include "intel_display_types.h"
33 #include "intel_hotplug.h"
34 #include "intel_hotplug_irq.h"
39 * Simply put, hotplug occurs when a display is connected to or disconnected
40 * from the system. However, there may be adapters and docking stations and
41 * Display Port short pulses and MST devices involved, complicating matters.
43 * Hotplug in i915 is handled in many different levels of abstraction.
45 * The platform dependent interrupt handling code in i915_irq.c enables,
46 * disables, and does preliminary handling of the interrupts. The interrupt
47 * handlers gather the hotplug detect (HPD) information from relevant registers
48 * into a platform independent mask of hotplug pins that have fired.
50 * The platform independent interrupt handler intel_hpd_irq_handler() in
51 * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
52 * further processing to appropriate bottom halves (Display Port specific and
55 * The Display Port work function i915_digport_work_func() calls into
56 * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
57 * pulses, with failures and non-MST long pulses triggering regular hotplug
58 * processing on the connector.
60 * The regular hotplug work function i915_hotplug_work_func() calls connector
61 * detect hooks, and, if connector status changes, triggers sending of hotplug
62 * uevent to userspace via drm_kms_helper_hotplug_event().
64 * Finally, the userspace is responsible for triggering a modeset upon receiving
65 * the hotplug uevent, disabling or enabling the crtc as needed.
67 * The hotplug interrupt storm detection and mitigation code keeps track of the
68 * number of interrupts per hotplug pin per a period of time, and if the number
69 * of interrupts exceeds a certain threshold, the interrupt is disabled for a
70 * while before being re-enabled. The intention is to mitigate issues raising
71 * from broken hardware triggering massive amounts of interrupts and grinding
72 * the system to a halt.
74 * Current implementation expects that hotplug interrupt storm will not be
75 * seen when display port sink is connected, hence on platforms whose DP
76 * callback is handled by i915_digport_work_func reenabling of hpd is not
77 * performed (it was never expected to be disabled in the first place ;) )
78 * this is specific to DP sinks handled by this routine and any other display
79 * such as HDMI or DVI enabled on the same port will have proper logic since
80 * it will use i915_hotplug_work_func where this logic is handled.
84 * intel_hpd_pin_default - return default pin associated with certain port.
85 * @dev_priv: private driver data pointer
86 * @port: the hpd port to get associated pin
88 * It is only valid and used by digital port encoder.
90 * Return pin that is associatade with @port.
92 enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
95 return HPD_PORT_A + port - PORT_A;
98 /* Threshold == 5 for long IRQs, 50 for short */
99 #define HPD_STORM_DEFAULT_THRESHOLD 50
101 #define HPD_STORM_DETECT_PERIOD 1000
102 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
103 #define HPD_RETRY_DELAY 1000
106 intel_connector_hpd_pin(struct intel_connector *connector)
108 struct intel_encoder *encoder = intel_attached_encoder(connector);
111 * MST connectors get their encoder attached dynamically
112 * so need to make sure we have an encoder here. But since
113 * MST encoders have their hpd_pin set to HPD_NONE we don't
114 * have to special case them beyond that.
116 return encoder ? encoder->hpd_pin : HPD_NONE;
120 * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
121 * @dev_priv: private driver data pointer
122 * @pin: the pin to gather stats on
123 * @long_hpd: whether the HPD IRQ was long or short
125 * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
126 * storms. Only the pin specific stats and state are changed, the caller is
127 * responsible for further action.
129 * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
130 * stored in @dev_priv->display.hotplug.hpd_storm_threshold which defaults to
131 * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
132 * short IRQs count as +1. If this threshold is exceeded, it's considered an
133 * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
135 * By default, most systems will only count long IRQs towards
136 * &dev_priv->display.hotplug.hpd_storm_threshold. However, some older systems also
137 * suffer from short IRQ storms and must also track these. Because short IRQ
138 * storms are naturally caused by sideband interactions with DP MST devices,
139 * short IRQ detection is only enabled for systems without DP MST support.
140 * Systems which are new enough to support DP MST are far less likely to
141 * suffer from IRQ storms at all, so this is fine.
143 * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
144 * and should only be adjusted for automated hotplug testing.
146 * Return true if an IRQ storm was detected on @pin.
148 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
149 enum hpd_pin pin, bool long_hpd)
151 struct intel_hotplug *hpd = &dev_priv->display.hotplug;
152 unsigned long start = hpd->stats[pin].last_jiffies;
153 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
154 const int increment = long_hpd ? 10 : 1;
155 const int threshold = hpd->hpd_storm_threshold;
159 (!long_hpd && !dev_priv->display.hotplug.hpd_short_storm_enabled))
162 if (!time_in_range(jiffies, start, end)) {
163 hpd->stats[pin].last_jiffies = jiffies;
164 hpd->stats[pin].count = 0;
167 hpd->stats[pin].count += increment;
168 if (hpd->stats[pin].count > threshold) {
169 hpd->stats[pin].state = HPD_MARK_DISABLED;
170 drm_dbg_kms(&dev_priv->drm,
171 "HPD interrupt storm detected on PIN %d\n", pin);
174 drm_dbg_kms(&dev_priv->drm,
175 "Received HPD interrupt on PIN %d - cnt: %d\n",
177 hpd->stats[pin].count);
183 static bool detection_work_enabled(struct drm_i915_private *i915)
185 lockdep_assert_held(&i915->irq_lock);
187 return i915->display.hotplug.detection_work_enabled;
191 mod_delayed_detection_work(struct drm_i915_private *i915, struct delayed_work *work, int delay)
193 lockdep_assert_held(&i915->irq_lock);
195 if (!detection_work_enabled(i915))
198 return mod_delayed_work(i915->unordered_wq, work, delay);
202 queue_delayed_detection_work(struct drm_i915_private *i915, struct delayed_work *work, int delay)
204 lockdep_assert_held(&i915->irq_lock);
206 if (!detection_work_enabled(i915))
209 return queue_delayed_work(i915->unordered_wq, work, delay);
213 queue_detection_work(struct drm_i915_private *i915, struct work_struct *work)
215 lockdep_assert_held(&i915->irq_lock);
217 if (!detection_work_enabled(i915))
220 return queue_work(i915->unordered_wq, work);
224 intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
226 struct drm_connector_list_iter conn_iter;
227 struct intel_connector *connector;
228 bool hpd_disabled = false;
230 lockdep_assert_held(&dev_priv->irq_lock);
232 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
233 for_each_intel_connector_iter(connector, &conn_iter) {
236 if (connector->base.polled != DRM_CONNECTOR_POLL_HPD)
239 pin = intel_connector_hpd_pin(connector);
240 if (pin == HPD_NONE ||
241 dev_priv->display.hotplug.stats[pin].state != HPD_MARK_DISABLED)
244 drm_info(&dev_priv->drm,
245 "HPD interrupt storm detected on connector %s: "
246 "switching from hotplug detection to polling\n",
247 connector->base.name);
249 dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
250 connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
251 DRM_CONNECTOR_POLL_DISCONNECT;
254 drm_connector_list_iter_end(&conn_iter);
256 /* Enable polling and queue hotplug re-enabling. */
258 drm_kms_helper_poll_reschedule(&dev_priv->drm);
259 mod_delayed_detection_work(dev_priv,
260 &dev_priv->display.hotplug.reenable_work,
261 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
265 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
267 struct drm_i915_private *dev_priv =
268 container_of(work, typeof(*dev_priv),
269 display.hotplug.reenable_work.work);
270 struct drm_connector_list_iter conn_iter;
271 struct intel_connector *connector;
272 intel_wakeref_t wakeref;
275 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
277 spin_lock_irq(&dev_priv->irq_lock);
279 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
280 for_each_intel_connector_iter(connector, &conn_iter) {
281 pin = intel_connector_hpd_pin(connector);
282 if (pin == HPD_NONE ||
283 dev_priv->display.hotplug.stats[pin].state != HPD_DISABLED)
286 if (connector->base.polled != connector->polled)
287 drm_dbg(&dev_priv->drm,
288 "Reenabling HPD on connector %s\n",
289 connector->base.name);
290 connector->base.polled = connector->polled;
292 drm_connector_list_iter_end(&conn_iter);
294 for_each_hpd_pin(pin) {
295 if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED)
296 dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
299 intel_hpd_irq_setup(dev_priv);
301 spin_unlock_irq(&dev_priv->irq_lock);
303 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
306 static enum intel_hotplug_state
307 intel_hotplug_detect_connector(struct intel_connector *connector)
309 struct drm_device *dev = connector->base.dev;
310 enum drm_connector_status old_status;
311 u64 old_epoch_counter;
315 drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
316 old_status = connector->base.status;
317 old_epoch_counter = connector->base.epoch_counter;
319 status = drm_helper_probe_detect(&connector->base, NULL, false);
320 if (!connector->base.force)
321 connector->base.status = status;
323 if (old_epoch_counter != connector->base.epoch_counter)
327 drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s (epoch counter %llu->%llu)\n",
328 connector->base.base.id,
329 connector->base.name,
330 drm_get_connector_status_name(old_status),
331 drm_get_connector_status_name(connector->base.status),
333 connector->base.epoch_counter);
334 return INTEL_HOTPLUG_CHANGED;
336 return INTEL_HOTPLUG_UNCHANGED;
339 enum intel_hotplug_state
340 intel_encoder_hotplug(struct intel_encoder *encoder,
341 struct intel_connector *connector)
343 return intel_hotplug_detect_connector(connector);
346 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
348 return intel_encoder_is_dig_port(encoder) &&
349 enc_to_dig_port(encoder)->hpd_pulse != NULL;
352 static void i915_digport_work_func(struct work_struct *work)
354 struct drm_i915_private *dev_priv =
355 container_of(work, struct drm_i915_private, display.hotplug.dig_port_work);
356 u32 long_port_mask, short_port_mask;
357 struct intel_encoder *encoder;
360 spin_lock_irq(&dev_priv->irq_lock);
361 long_port_mask = dev_priv->display.hotplug.long_port_mask;
362 dev_priv->display.hotplug.long_port_mask = 0;
363 short_port_mask = dev_priv->display.hotplug.short_port_mask;
364 dev_priv->display.hotplug.short_port_mask = 0;
365 spin_unlock_irq(&dev_priv->irq_lock);
367 for_each_intel_encoder(&dev_priv->drm, encoder) {
368 struct intel_digital_port *dig_port;
369 enum port port = encoder->port;
370 bool long_hpd, short_hpd;
373 if (!intel_encoder_has_hpd_pulse(encoder))
376 long_hpd = long_port_mask & BIT(port);
377 short_hpd = short_port_mask & BIT(port);
379 if (!long_hpd && !short_hpd)
382 dig_port = enc_to_dig_port(encoder);
384 ret = dig_port->hpd_pulse(dig_port, long_hpd);
385 if (ret == IRQ_NONE) {
386 /* fall back to old school hpd */
387 old_bits |= BIT(encoder->hpd_pin);
392 spin_lock_irq(&dev_priv->irq_lock);
393 dev_priv->display.hotplug.event_bits |= old_bits;
394 queue_delayed_detection_work(dev_priv,
395 &dev_priv->display.hotplug.hotplug_work, 0);
396 spin_unlock_irq(&dev_priv->irq_lock);
401 * intel_hpd_trigger_irq - trigger an hpd irq event for a port
402 * @dig_port: digital port
404 * Trigger an HPD interrupt event for the given port, emulating a short pulse
405 * generated by the sink, and schedule the dig port work to handle it.
407 void intel_hpd_trigger_irq(struct intel_digital_port *dig_port)
409 struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
411 spin_lock_irq(&i915->irq_lock);
412 i915->display.hotplug.short_port_mask |= BIT(dig_port->base.port);
413 spin_unlock_irq(&i915->irq_lock);
415 queue_work(i915->display.hotplug.dp_wq, &i915->display.hotplug.dig_port_work);
419 * Handle hotplug events outside the interrupt handler proper.
421 static void i915_hotplug_work_func(struct work_struct *work)
423 struct drm_i915_private *dev_priv =
424 container_of(work, struct drm_i915_private,
425 display.hotplug.hotplug_work.work);
426 struct drm_connector_list_iter conn_iter;
427 struct intel_connector *connector;
428 u32 changed = 0, retry = 0;
431 struct drm_connector *first_changed_connector = NULL;
432 int changed_connectors = 0;
434 mutex_lock(&dev_priv->drm.mode_config.mutex);
435 drm_dbg_kms(&dev_priv->drm, "running encoder hotplug functions\n");
437 spin_lock_irq(&dev_priv->irq_lock);
439 hpd_event_bits = dev_priv->display.hotplug.event_bits;
440 dev_priv->display.hotplug.event_bits = 0;
441 hpd_retry_bits = dev_priv->display.hotplug.retry_bits;
442 dev_priv->display.hotplug.retry_bits = 0;
444 /* Enable polling for connectors which had HPD IRQ storms */
445 intel_hpd_irq_storm_switch_to_polling(dev_priv);
447 spin_unlock_irq(&dev_priv->irq_lock);
449 /* Skip calling encode hotplug handlers if ignore long HPD set*/
450 if (dev_priv->display.hotplug.ignore_long_hpd) {
451 drm_dbg_kms(&dev_priv->drm, "Ignore HPD flag on - skip encoder hotplug handlers\n");
452 mutex_unlock(&dev_priv->drm.mode_config.mutex);
456 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
457 for_each_intel_connector_iter(connector, &conn_iter) {
461 pin = intel_connector_hpd_pin(connector);
466 if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
467 struct intel_encoder *encoder =
468 intel_attached_encoder(connector);
470 if (hpd_event_bits & hpd_bit)
471 connector->hotplug_retries = 0;
473 connector->hotplug_retries++;
475 drm_dbg_kms(&dev_priv->drm,
476 "Connector %s (pin %i) received hotplug event. (retry %d)\n",
477 connector->base.name, pin,
478 connector->hotplug_retries);
480 switch (encoder->hotplug(encoder, connector)) {
481 case INTEL_HOTPLUG_UNCHANGED:
483 case INTEL_HOTPLUG_CHANGED:
485 changed_connectors++;
486 if (!first_changed_connector) {
487 drm_connector_get(&connector->base);
488 first_changed_connector = &connector->base;
491 case INTEL_HOTPLUG_RETRY:
497 drm_connector_list_iter_end(&conn_iter);
498 mutex_unlock(&dev_priv->drm.mode_config.mutex);
500 if (changed_connectors == 1)
501 drm_kms_helper_connector_hotplug_event(first_changed_connector);
502 else if (changed_connectors > 0)
503 drm_kms_helper_hotplug_event(&dev_priv->drm);
505 if (first_changed_connector)
506 drm_connector_put(first_changed_connector);
508 /* Remove shared HPD pins that have changed */
511 spin_lock_irq(&dev_priv->irq_lock);
512 dev_priv->display.hotplug.retry_bits |= retry;
514 mod_delayed_detection_work(dev_priv,
515 &dev_priv->display.hotplug.hotplug_work,
516 msecs_to_jiffies(HPD_RETRY_DELAY));
517 spin_unlock_irq(&dev_priv->irq_lock);
523 * intel_hpd_irq_handler - main hotplug irq handler
524 * @dev_priv: drm_i915_private
525 * @pin_mask: a mask of hpd pins that have triggered the irq
526 * @long_mask: a mask of hpd pins that may be long hpd pulses
528 * This is the main hotplug irq handler for all platforms. The platform specific
529 * irq handlers call the platform specific hotplug irq handlers, which read and
530 * decode the appropriate registers into bitmasks about hpd pins that have
531 * triggered (@pin_mask), and which of those pins may be long pulses
532 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
533 * is not a digital port.
535 * Here, we do hotplug irq storm detection and mitigation, and pass further
536 * processing to appropriate bottom halves.
538 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
539 u32 pin_mask, u32 long_mask)
541 struct intel_encoder *encoder;
542 bool storm_detected = false;
543 bool queue_dig = false, queue_hp = false;
544 u32 long_hpd_pulse_mask = 0;
545 u32 short_hpd_pulse_mask = 0;
551 spin_lock(&dev_priv->irq_lock);
554 * Determine whether ->hpd_pulse() exists for each pin, and
555 * whether we have a short or a long pulse. This is needed
556 * as each pin may have up to two encoders (HDMI and DP) and
557 * only the one of them (DP) will have ->hpd_pulse().
559 for_each_intel_encoder(&dev_priv->drm, encoder) {
560 enum port port = encoder->port;
563 pin = encoder->hpd_pin;
564 if (!(BIT(pin) & pin_mask))
567 if (!intel_encoder_has_hpd_pulse(encoder))
570 long_hpd = long_mask & BIT(pin);
572 drm_dbg(&dev_priv->drm,
573 "digital hpd on [ENCODER:%d:%s] - %s\n",
574 encoder->base.base.id, encoder->base.name,
575 long_hpd ? "long" : "short");
579 long_hpd_pulse_mask |= BIT(pin);
580 dev_priv->display.hotplug.long_port_mask |= BIT(port);
582 short_hpd_pulse_mask |= BIT(pin);
583 dev_priv->display.hotplug.short_port_mask |= BIT(port);
587 /* Now process each pin just once */
588 for_each_hpd_pin(pin) {
591 if (!(BIT(pin) & pin_mask))
594 if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED) {
596 * On GMCH platforms the interrupt mask bits only
597 * prevent irq generation, not the setting of the
598 * hotplug bits itself. So only WARN about unexpected
599 * interrupts on saner platforms.
601 drm_WARN_ONCE(&dev_priv->drm, !HAS_GMCH(dev_priv),
602 "Received HPD interrupt on pin %d although disabled\n",
607 if (dev_priv->display.hotplug.stats[pin].state != HPD_ENABLED)
611 * Delegate to ->hpd_pulse() if one of the encoders for this
612 * pin has it, otherwise let the hotplug_work deal with this
615 if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
616 long_hpd = long_hpd_pulse_mask & BIT(pin);
618 dev_priv->display.hotplug.event_bits |= BIT(pin);
623 if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
624 dev_priv->display.hotplug.event_bits &= ~BIT(pin);
625 storm_detected = true;
631 * Disable any IRQs that storms were detected on. Polling enablement
632 * happens later in our hotplug work.
635 intel_hpd_irq_setup(dev_priv);
638 * Our hotplug handler can grab modeset locks (by calling down into the
639 * fb helpers). Hence it must not be run on our own dev-priv->wq work
640 * queue for otherwise the flush_work in the pageflip code will
644 queue_work(dev_priv->display.hotplug.dp_wq, &dev_priv->display.hotplug.dig_port_work);
646 queue_delayed_detection_work(dev_priv,
647 &dev_priv->display.hotplug.hotplug_work, 0);
649 spin_unlock(&dev_priv->irq_lock);
653 * intel_hpd_init - initializes and enables hpd support
654 * @dev_priv: i915 device instance
656 * This function enables the hotplug support. It requires that interrupts have
657 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
658 * poll request can run concurrently to other code, so locking rules must be
661 * This is a separate step from interrupt enabling to simplify the locking rules
662 * in the driver load and resume code.
664 * Also see: intel_hpd_poll_enable() and intel_hpd_poll_disable().
666 void intel_hpd_init(struct drm_i915_private *dev_priv)
670 if (!HAS_DISPLAY(dev_priv))
673 for_each_hpd_pin(i) {
674 dev_priv->display.hotplug.stats[i].count = 0;
675 dev_priv->display.hotplug.stats[i].state = HPD_ENABLED;
679 * Interrupt setup is already guaranteed to be single-threaded, this is
680 * just to make the assert_spin_locked checks happy.
682 spin_lock_irq(&dev_priv->irq_lock);
683 intel_hpd_irq_setup(dev_priv);
684 spin_unlock_irq(&dev_priv->irq_lock);
687 static void i915_hpd_poll_detect_connectors(struct drm_i915_private *i915)
689 struct drm_connector_list_iter conn_iter;
690 struct intel_connector *connector;
691 struct intel_connector *first_changed_connector = NULL;
694 mutex_lock(&i915->drm.mode_config.mutex);
696 if (!i915->drm.mode_config.poll_enabled)
699 drm_connector_list_iter_begin(&i915->drm, &conn_iter);
700 for_each_intel_connector_iter(connector, &conn_iter) {
701 if (!(connector->base.polled & DRM_CONNECTOR_POLL_HPD))
704 if (intel_hotplug_detect_connector(connector) != INTEL_HOTPLUG_CHANGED)
710 drm_connector_get(&connector->base);
711 first_changed_connector = connector;
714 drm_connector_list_iter_end(&conn_iter);
717 mutex_unlock(&i915->drm.mode_config.mutex);
723 drm_kms_helper_connector_hotplug_event(&first_changed_connector->base);
725 drm_kms_helper_hotplug_event(&i915->drm);
727 drm_connector_put(&first_changed_connector->base);
730 static void i915_hpd_poll_init_work(struct work_struct *work)
732 struct drm_i915_private *dev_priv =
733 container_of(work, struct drm_i915_private,
734 display.hotplug.poll_init_work);
735 struct drm_connector_list_iter conn_iter;
736 struct intel_connector *connector;
737 intel_wakeref_t wakeref;
740 mutex_lock(&dev_priv->drm.mode_config.mutex);
742 enabled = READ_ONCE(dev_priv->display.hotplug.poll_enabled);
744 * Prevent taking a power reference from this sequence of
745 * i915_hpd_poll_init_work() -> drm_helper_hpd_irq_event() ->
746 * connector detect which would requeue i915_hpd_poll_init_work()
747 * and so risk an endless loop of this same sequence.
750 wakeref = intel_display_power_get(dev_priv,
751 POWER_DOMAIN_DISPLAY_CORE);
752 drm_WARN_ON(&dev_priv->drm,
753 READ_ONCE(dev_priv->display.hotplug.poll_enabled));
754 cancel_work(&dev_priv->display.hotplug.poll_init_work);
757 spin_lock_irq(&dev_priv->irq_lock);
759 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
760 for_each_intel_connector_iter(connector, &conn_iter) {
763 pin = intel_connector_hpd_pin(connector);
767 if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED)
770 connector->base.polled = connector->polled;
772 if (enabled && connector->base.polled == DRM_CONNECTOR_POLL_HPD)
773 connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
774 DRM_CONNECTOR_POLL_DISCONNECT;
776 drm_connector_list_iter_end(&conn_iter);
778 spin_unlock_irq(&dev_priv->irq_lock);
781 drm_kms_helper_poll_reschedule(&dev_priv->drm);
783 mutex_unlock(&dev_priv->drm.mode_config.mutex);
786 * We might have missed any hotplugs that happened while we were
787 * in the middle of disabling polling
790 i915_hpd_poll_detect_connectors(dev_priv);
792 intel_display_power_put(dev_priv,
793 POWER_DOMAIN_DISPLAY_CORE,
799 * intel_hpd_poll_enable - enable polling for connectors with hpd
800 * @dev_priv: i915 device instance
802 * This function enables polling for all connectors which support HPD.
803 * Under certain conditions HPD may not be functional. On most Intel GPUs,
804 * this happens when we enter runtime suspend.
805 * On Valleyview and Cherryview systems, this also happens when we shut off all
808 * Since this function can get called in contexts where we're already holding
809 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
812 * Also see: intel_hpd_init() and intel_hpd_poll_disable().
814 void intel_hpd_poll_enable(struct drm_i915_private *dev_priv)
816 struct intel_display *display = &dev_priv->display;
818 if (!HAS_DISPLAY(dev_priv) ||
819 !intel_display_device_enabled(display))
822 WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, true);
825 * We might already be holding dev->mode_config.mutex, so do this in a
827 * As well, there's no issue if we race here since we always reschedule
830 spin_lock_irq(&dev_priv->irq_lock);
831 queue_detection_work(dev_priv,
832 &dev_priv->display.hotplug.poll_init_work);
833 spin_unlock_irq(&dev_priv->irq_lock);
837 * intel_hpd_poll_disable - disable polling for connectors with hpd
838 * @dev_priv: i915 device instance
840 * This function disables polling for all connectors which support HPD.
841 * Under certain conditions HPD may not be functional. On most Intel GPUs,
842 * this happens when we enter runtime suspend.
843 * On Valleyview and Cherryview systems, this also happens when we shut off all
846 * Since this function can get called in contexts where we're already holding
847 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
850 * Also used during driver init to initialize connector->polled
851 * appropriately for all connectors.
853 * Also see: intel_hpd_init() and intel_hpd_poll_enable().
855 void intel_hpd_poll_disable(struct drm_i915_private *dev_priv)
857 if (!HAS_DISPLAY(dev_priv))
860 WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, false);
862 spin_lock_irq(&dev_priv->irq_lock);
863 queue_detection_work(dev_priv,
864 &dev_priv->display.hotplug.poll_init_work);
865 spin_unlock_irq(&dev_priv->irq_lock);
868 void intel_hpd_init_early(struct drm_i915_private *i915)
870 INIT_DELAYED_WORK(&i915->display.hotplug.hotplug_work,
871 i915_hotplug_work_func);
872 INIT_WORK(&i915->display.hotplug.dig_port_work, i915_digport_work_func);
873 INIT_WORK(&i915->display.hotplug.poll_init_work, i915_hpd_poll_init_work);
874 INIT_DELAYED_WORK(&i915->display.hotplug.reenable_work,
875 intel_hpd_irq_storm_reenable_work);
877 i915->display.hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
878 /* If we have MST support, we want to avoid doing short HPD IRQ storm
879 * detection, as short HPD storms will occur as a natural part of
880 * sideband messaging with MST.
881 * On older platforms however, IRQ storms can occur with both long and
882 * short pulses, as seen on some G4x systems.
884 i915->display.hotplug.hpd_short_storm_enabled = !HAS_DP_MST(i915);
887 static bool cancel_all_detection_work(struct drm_i915_private *i915)
889 bool was_pending = false;
891 if (cancel_delayed_work_sync(&i915->display.hotplug.hotplug_work))
893 if (cancel_work_sync(&i915->display.hotplug.poll_init_work))
895 if (cancel_delayed_work_sync(&i915->display.hotplug.reenable_work))
901 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
903 if (!HAS_DISPLAY(dev_priv))
906 spin_lock_irq(&dev_priv->irq_lock);
908 dev_priv->display.hotplug.long_port_mask = 0;
909 dev_priv->display.hotplug.short_port_mask = 0;
910 dev_priv->display.hotplug.event_bits = 0;
911 dev_priv->display.hotplug.retry_bits = 0;
913 spin_unlock_irq(&dev_priv->irq_lock);
915 cancel_work_sync(&dev_priv->display.hotplug.dig_port_work);
918 * All other work triggered by hotplug events should be canceled by
921 if (cancel_all_detection_work(dev_priv))
922 drm_dbg_kms(&dev_priv->drm, "Hotplug detection work still active\n");
925 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
932 spin_lock_irq(&dev_priv->irq_lock);
933 if (dev_priv->display.hotplug.stats[pin].state == HPD_ENABLED) {
934 dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
937 spin_unlock_irq(&dev_priv->irq_lock);
942 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
947 spin_lock_irq(&dev_priv->irq_lock);
948 dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
949 spin_unlock_irq(&dev_priv->irq_lock);
952 static void queue_work_for_missed_irqs(struct drm_i915_private *i915)
954 bool queue_work = false;
957 lockdep_assert_held(&i915->irq_lock);
959 if (i915->display.hotplug.event_bits ||
960 i915->display.hotplug.retry_bits)
963 for_each_hpd_pin(pin) {
964 switch (i915->display.hotplug.stats[pin].state) {
965 case HPD_MARK_DISABLED:
971 MISSING_CASE(i915->display.hotplug.stats[pin].state);
976 queue_delayed_detection_work(i915, &i915->display.hotplug.hotplug_work, 0);
979 void intel_hpd_enable_detection_work(struct drm_i915_private *i915)
981 spin_lock_irq(&i915->irq_lock);
982 i915->display.hotplug.detection_work_enabled = true;
983 queue_work_for_missed_irqs(i915);
984 spin_unlock_irq(&i915->irq_lock);
987 void intel_hpd_disable_detection_work(struct drm_i915_private *i915)
989 spin_lock_irq(&i915->irq_lock);
990 i915->display.hotplug.detection_work_enabled = false;
991 spin_unlock_irq(&i915->irq_lock);
993 cancel_all_detection_work(i915);
996 bool intel_hpd_schedule_detection(struct drm_i915_private *i915)
1001 spin_lock_irqsave(&i915->irq_lock, flags);
1002 ret = queue_delayed_detection_work(i915, &i915->display.hotplug.hotplug_work, 0);
1003 spin_unlock_irqrestore(&i915->irq_lock, flags);
1008 static int i915_hpd_storm_ctl_show(struct seq_file *m, void *data)
1010 struct drm_i915_private *dev_priv = m->private;
1011 struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
1013 /* Synchronize with everything first in case there's been an HPD
1014 * storm, but we haven't finished handling it in the kernel yet
1016 intel_synchronize_irq(dev_priv);
1017 flush_work(&dev_priv->display.hotplug.dig_port_work);
1018 flush_delayed_work(&dev_priv->display.hotplug.hotplug_work);
1020 seq_printf(m, "Threshold: %d\n", hotplug->hpd_storm_threshold);
1021 seq_printf(m, "Detected: %s\n",
1022 str_yes_no(delayed_work_pending(&hotplug->reenable_work)));
1027 static ssize_t i915_hpd_storm_ctl_write(struct file *file,
1028 const char __user *ubuf, size_t len,
1031 struct seq_file *m = file->private_data;
1032 struct drm_i915_private *dev_priv = m->private;
1033 struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
1034 unsigned int new_threshold;
1039 if (len >= sizeof(tmp))
1042 if (copy_from_user(tmp, ubuf, len))
1047 /* Strip newline, if any */
1048 newline = strchr(tmp, '\n');
1052 if (strcmp(tmp, "reset") == 0)
1053 new_threshold = HPD_STORM_DEFAULT_THRESHOLD;
1054 else if (kstrtouint(tmp, 10, &new_threshold) != 0)
1057 if (new_threshold > 0)
1058 drm_dbg_kms(&dev_priv->drm,
1059 "Setting HPD storm detection threshold to %d\n",
1062 drm_dbg_kms(&dev_priv->drm, "Disabling HPD storm detection\n");
1064 spin_lock_irq(&dev_priv->irq_lock);
1065 hotplug->hpd_storm_threshold = new_threshold;
1066 /* Reset the HPD storm stats so we don't accidentally trigger a storm */
1068 hotplug->stats[i].count = 0;
1069 spin_unlock_irq(&dev_priv->irq_lock);
1071 /* Re-enable hpd immediately if we were in an irq storm */
1072 flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
1077 static int i915_hpd_storm_ctl_open(struct inode *inode, struct file *file)
1079 return single_open(file, i915_hpd_storm_ctl_show, inode->i_private);
1082 static const struct file_operations i915_hpd_storm_ctl_fops = {
1083 .owner = THIS_MODULE,
1084 .open = i915_hpd_storm_ctl_open,
1086 .llseek = seq_lseek,
1087 .release = single_release,
1088 .write = i915_hpd_storm_ctl_write
1091 static int i915_hpd_short_storm_ctl_show(struct seq_file *m, void *data)
1093 struct drm_i915_private *dev_priv = m->private;
1095 seq_printf(m, "Enabled: %s\n",
1096 str_yes_no(dev_priv->display.hotplug.hpd_short_storm_enabled));
1102 i915_hpd_short_storm_ctl_open(struct inode *inode, struct file *file)
1104 return single_open(file, i915_hpd_short_storm_ctl_show,
1108 static ssize_t i915_hpd_short_storm_ctl_write(struct file *file,
1109 const char __user *ubuf,
1110 size_t len, loff_t *offp)
1112 struct seq_file *m = file->private_data;
1113 struct drm_i915_private *dev_priv = m->private;
1114 struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
1120 if (len >= sizeof(tmp))
1123 if (copy_from_user(tmp, ubuf, len))
1128 /* Strip newline, if any */
1129 newline = strchr(tmp, '\n');
1133 /* Reset to the "default" state for this system */
1134 if (strcmp(tmp, "reset") == 0)
1135 new_state = !HAS_DP_MST(dev_priv);
1136 else if (kstrtobool(tmp, &new_state) != 0)
1139 drm_dbg_kms(&dev_priv->drm, "%sabling HPD short storm detection\n",
1140 new_state ? "En" : "Dis");
1142 spin_lock_irq(&dev_priv->irq_lock);
1143 hotplug->hpd_short_storm_enabled = new_state;
1144 /* Reset the HPD storm stats so we don't accidentally trigger a storm */
1146 hotplug->stats[i].count = 0;
1147 spin_unlock_irq(&dev_priv->irq_lock);
1149 /* Re-enable hpd immediately if we were in an irq storm */
1150 flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
1155 static const struct file_operations i915_hpd_short_storm_ctl_fops = {
1156 .owner = THIS_MODULE,
1157 .open = i915_hpd_short_storm_ctl_open,
1159 .llseek = seq_lseek,
1160 .release = single_release,
1161 .write = i915_hpd_short_storm_ctl_write,
1164 void intel_hpd_debugfs_register(struct drm_i915_private *i915)
1166 struct drm_minor *minor = i915->drm.primary;
1168 debugfs_create_file("i915_hpd_storm_ctl", 0644, minor->debugfs_root,
1169 i915, &i915_hpd_storm_ctl_fops);
1170 debugfs_create_file("i915_hpd_short_storm_ctl", 0644, minor->debugfs_root,
1171 i915, &i915_hpd_short_storm_ctl_fops);
1172 debugfs_create_bool("i915_ignore_long_hpd", 0644, minor->debugfs_root,
1173 &i915->display.hotplug.ignore_long_hpd);