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>
27 #include <drm/i915_drm.h>
30 #include "intel_drv.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_port - return port hard associated with certain pin.
81 * @dev_priv: private driver data pointer
82 * @pin: the hpd pin to get associated port
84 * Return port that is associatade with @pin and PORT_NONE if no port is
85 * hard associated with that @pin.
87 enum port intel_hpd_pin_to_port(struct drm_i915_private *dev_priv,
100 if (IS_CNL_WITH_PORT_F(dev_priv))
106 return PORT_NONE; /* no port for this pin */
111 * intel_hpd_pin_default - return default pin associated with certain port.
112 * @dev_priv: private driver data pointer
113 * @port: the hpd port to get associated pin
115 * It is only valid and used by digital port encoder.
117 * Return pin that is associatade with @port and HDP_NONE if no pin is
118 * hard associated with that @port.
120 enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
135 if (IS_CNL_WITH_PORT_F(dev_priv))
144 #define HPD_STORM_DETECT_PERIOD 1000
145 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
148 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
149 * @dev_priv: private driver data pointer
150 * @pin: the pin to gather stats on
152 * Gather stats about HPD irqs from the specified @pin, and detect irq
153 * storms. Only the pin specific stats and state are changed, the caller is
154 * responsible for further action.
156 * The number of irqs that are allowed within @HPD_STORM_DETECT_PERIOD is
157 * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
158 * @HPD_STORM_DEFAULT_THRESHOLD. If this threshold is exceeded, it's
159 * considered an irq storm and the irq state is set to @HPD_MARK_DISABLED.
161 * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
162 * and should only be adjusted for automated hotplug testing.
164 * Return true if an irq storm was detected on @pin.
166 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
169 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
170 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
171 const int threshold = dev_priv->hotplug.hpd_storm_threshold;
174 if (!time_in_range(jiffies, start, end)) {
175 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
176 dev_priv->hotplug.stats[pin].count = 0;
177 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
178 } else if (dev_priv->hotplug.stats[pin].count > threshold &&
180 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
181 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
184 dev_priv->hotplug.stats[pin].count++;
185 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
186 dev_priv->hotplug.stats[pin].count);
192 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
194 struct drm_device *dev = &dev_priv->drm;
195 struct intel_connector *intel_connector;
196 struct intel_encoder *intel_encoder;
197 struct drm_connector *connector;
198 struct drm_connector_list_iter conn_iter;
200 bool hpd_disabled = false;
202 lockdep_assert_held(&dev_priv->irq_lock);
204 drm_connector_list_iter_begin(dev, &conn_iter);
205 drm_for_each_connector_iter(connector, &conn_iter) {
206 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
209 intel_connector = to_intel_connector(connector);
210 intel_encoder = intel_connector->encoder;
214 pin = intel_encoder->hpd_pin;
215 if (pin == HPD_NONE ||
216 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
219 DRM_INFO("HPD interrupt storm detected on connector %s: "
220 "switching from hotplug detection to polling\n",
223 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
224 connector->polled = DRM_CONNECTOR_POLL_CONNECT
225 | DRM_CONNECTOR_POLL_DISCONNECT;
228 drm_connector_list_iter_end(&conn_iter);
230 /* Enable polling and queue hotplug re-enabling. */
232 drm_kms_helper_poll_enable(dev);
233 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
234 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
238 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
240 struct drm_i915_private *dev_priv =
241 container_of(work, typeof(*dev_priv),
242 hotplug.reenable_work.work);
243 struct drm_device *dev = &dev_priv->drm;
246 intel_runtime_pm_get(dev_priv);
248 spin_lock_irq(&dev_priv->irq_lock);
249 for_each_hpd_pin(i) {
250 struct drm_connector *connector;
251 struct drm_connector_list_iter conn_iter;
253 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
256 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
258 drm_connector_list_iter_begin(dev, &conn_iter);
259 drm_for_each_connector_iter(connector, &conn_iter) {
260 struct intel_connector *intel_connector = to_intel_connector(connector);
262 if (intel_connector->encoder->hpd_pin == i) {
263 if (connector->polled != intel_connector->polled)
264 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
266 connector->polled = intel_connector->polled;
267 if (!connector->polled)
268 connector->polled = DRM_CONNECTOR_POLL_HPD;
271 drm_connector_list_iter_end(&conn_iter);
273 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
274 dev_priv->display.hpd_irq_setup(dev_priv);
275 spin_unlock_irq(&dev_priv->irq_lock);
277 intel_runtime_pm_put(dev_priv);
280 bool intel_encoder_hotplug(struct intel_encoder *encoder,
281 struct intel_connector *connector)
283 struct drm_device *dev = connector->base.dev;
284 enum drm_connector_status old_status;
286 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
287 old_status = connector->base.status;
289 connector->base.status =
290 drm_helper_probe_detect(&connector->base, NULL, false);
292 if (old_status == connector->base.status)
295 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
296 connector->base.base.id,
297 connector->base.name,
298 drm_get_connector_status_name(old_status),
299 drm_get_connector_status_name(connector->base.status));
304 static void i915_digport_work_func(struct work_struct *work)
306 struct drm_i915_private *dev_priv =
307 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
308 u32 long_port_mask, short_port_mask;
309 struct intel_digital_port *intel_dig_port;
313 spin_lock_irq(&dev_priv->irq_lock);
314 long_port_mask = dev_priv->hotplug.long_port_mask;
315 dev_priv->hotplug.long_port_mask = 0;
316 short_port_mask = dev_priv->hotplug.short_port_mask;
317 dev_priv->hotplug.short_port_mask = 0;
318 spin_unlock_irq(&dev_priv->irq_lock);
320 for (i = 0; i < I915_MAX_PORTS; i++) {
322 bool long_hpd = false;
323 intel_dig_port = dev_priv->hotplug.irq_port[i];
324 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
327 if (long_port_mask & (1 << i)) {
330 } else if (short_port_mask & (1 << i))
336 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
337 if (ret == IRQ_NONE) {
338 /* fall back to old school hpd */
339 old_bits |= (1 << intel_dig_port->base.hpd_pin);
345 spin_lock_irq(&dev_priv->irq_lock);
346 dev_priv->hotplug.event_bits |= old_bits;
347 spin_unlock_irq(&dev_priv->irq_lock);
348 schedule_work(&dev_priv->hotplug.hotplug_work);
353 * Handle hotplug events outside the interrupt handler proper.
355 static void i915_hotplug_work_func(struct work_struct *work)
357 struct drm_i915_private *dev_priv =
358 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
359 struct drm_device *dev = &dev_priv->drm;
360 struct intel_connector *intel_connector;
361 struct intel_encoder *intel_encoder;
362 struct drm_connector *connector;
363 struct drm_connector_list_iter conn_iter;
364 bool changed = false;
367 mutex_lock(&dev->mode_config.mutex);
368 DRM_DEBUG_KMS("running encoder hotplug functions\n");
370 spin_lock_irq(&dev_priv->irq_lock);
372 hpd_event_bits = dev_priv->hotplug.event_bits;
373 dev_priv->hotplug.event_bits = 0;
375 /* Disable hotplug on connectors that hit an irq storm. */
376 intel_hpd_irq_storm_disable(dev_priv);
378 spin_unlock_irq(&dev_priv->irq_lock);
380 drm_connector_list_iter_begin(dev, &conn_iter);
381 drm_for_each_connector_iter(connector, &conn_iter) {
382 intel_connector = to_intel_connector(connector);
383 if (!intel_connector->encoder)
385 intel_encoder = intel_connector->encoder;
386 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
387 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
388 connector->name, intel_encoder->hpd_pin);
390 changed |= intel_encoder->hotplug(intel_encoder,
394 drm_connector_list_iter_end(&conn_iter);
395 mutex_unlock(&dev->mode_config.mutex);
398 drm_kms_helper_hotplug_event(dev);
403 * intel_hpd_irq_handler - main hotplug irq handler
404 * @dev_priv: drm_i915_private
405 * @pin_mask: a mask of hpd pins that have triggered the irq
406 * @long_mask: a mask of hpd pins that may be long hpd pulses
408 * This is the main hotplug irq handler for all platforms. The platform specific
409 * irq handlers call the platform specific hotplug irq handlers, which read and
410 * decode the appropriate registers into bitmasks about hpd pins that have
411 * triggered (@pin_mask), and which of those pins may be long pulses
412 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
413 * is not a digital port.
415 * Here, we do hotplug irq storm detection and mitigation, and pass further
416 * processing to appropriate bottom halves.
418 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
419 u32 pin_mask, u32 long_mask)
423 bool storm_detected = false;
424 bool queue_dig = false, queue_hp = false;
430 spin_lock(&dev_priv->irq_lock);
431 for_each_hpd_pin(i) {
432 if (!(BIT(i) & pin_mask))
435 port = intel_hpd_pin_to_port(dev_priv, i);
436 is_dig_port = port != PORT_NONE &&
437 dev_priv->hotplug.irq_port[port];
440 bool long_hpd = long_mask & BIT(i);
442 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
443 long_hpd ? "long" : "short");
445 * For long HPD pulses we want to have the digital queue happen,
446 * but we still want HPD storm detection to function.
450 dev_priv->hotplug.long_port_mask |= (1 << port);
452 /* for short HPD just trigger the digital queue */
453 dev_priv->hotplug.short_port_mask |= (1 << port);
458 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
460 * On GMCH platforms the interrupt mask bits only
461 * prevent irq generation, not the setting of the
462 * hotplug bits itself. So only WARN about unexpected
463 * interrupts on saner platforms.
465 WARN_ONCE(!HAS_GMCH_DISPLAY(dev_priv),
466 "Received HPD interrupt on pin %d although disabled\n", i);
470 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
474 dev_priv->hotplug.event_bits |= BIT(i);
478 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
479 dev_priv->hotplug.event_bits &= ~BIT(i);
480 storm_detected = true;
484 if (storm_detected && dev_priv->display_irqs_enabled)
485 dev_priv->display.hpd_irq_setup(dev_priv);
486 spin_unlock(&dev_priv->irq_lock);
489 * Our hotplug handler can grab modeset locks (by calling down into the
490 * fb helpers). Hence it must not be run on our own dev-priv->wq work
491 * queue for otherwise the flush_work in the pageflip code will
495 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
497 schedule_work(&dev_priv->hotplug.hotplug_work);
501 * intel_hpd_init - initializes and enables hpd support
502 * @dev_priv: i915 device instance
504 * This function enables the hotplug support. It requires that interrupts have
505 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
506 * poll request can run concurrently to other code, so locking rules must be
509 * This is a separate step from interrupt enabling to simplify the locking rules
510 * in the driver load and resume code.
512 * Also see: intel_hpd_poll_init(), which enables connector polling
514 void intel_hpd_init(struct drm_i915_private *dev_priv)
518 for_each_hpd_pin(i) {
519 dev_priv->hotplug.stats[i].count = 0;
520 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
523 WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
524 schedule_work(&dev_priv->hotplug.poll_init_work);
527 * Interrupt setup is already guaranteed to be single-threaded, this is
528 * just to make the assert_spin_locked checks happy.
530 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
531 spin_lock_irq(&dev_priv->irq_lock);
532 if (dev_priv->display_irqs_enabled)
533 dev_priv->display.hpd_irq_setup(dev_priv);
534 spin_unlock_irq(&dev_priv->irq_lock);
538 static void i915_hpd_poll_init_work(struct work_struct *work)
540 struct drm_i915_private *dev_priv =
541 container_of(work, struct drm_i915_private,
542 hotplug.poll_init_work);
543 struct drm_device *dev = &dev_priv->drm;
544 struct drm_connector *connector;
545 struct drm_connector_list_iter conn_iter;
548 mutex_lock(&dev->mode_config.mutex);
550 enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
552 drm_connector_list_iter_begin(dev, &conn_iter);
553 drm_for_each_connector_iter(connector, &conn_iter) {
554 struct intel_connector *intel_connector =
555 to_intel_connector(connector);
556 connector->polled = intel_connector->polled;
558 /* MST has a dynamic intel_connector->encoder and it's reprobing
559 * is all handled by the MST helpers. */
560 if (intel_connector->mst_port)
563 if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
564 intel_connector->encoder->hpd_pin > HPD_NONE) {
565 connector->polled = enabled ?
566 DRM_CONNECTOR_POLL_CONNECT |
567 DRM_CONNECTOR_POLL_DISCONNECT :
568 DRM_CONNECTOR_POLL_HPD;
571 drm_connector_list_iter_end(&conn_iter);
574 drm_kms_helper_poll_enable(dev);
576 mutex_unlock(&dev->mode_config.mutex);
579 * We might have missed any hotplugs that happened while we were
580 * in the middle of disabling polling
583 drm_helper_hpd_irq_event(dev);
587 * intel_hpd_poll_init - enables/disables polling for connectors with hpd
588 * @dev_priv: i915 device instance
590 * This function enables polling for all connectors, regardless of whether or
591 * not they support hotplug detection. Under certain conditions HPD may not be
592 * functional. On most Intel GPUs, this happens when we enter runtime suspend.
593 * On Valleyview and Cherryview systems, this also happens when we shut off all
596 * Since this function can get called in contexts where we're already holding
597 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
600 * Also see: intel_hpd_init(), which restores hpd handling.
602 void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
604 WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
607 * We might already be holding dev->mode_config.mutex, so do this in a
609 * As well, there's no issue if we race here since we always reschedule
612 schedule_work(&dev_priv->hotplug.poll_init_work);
615 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
617 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
618 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
619 INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
620 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
621 intel_hpd_irq_storm_reenable_work);
624 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
626 spin_lock_irq(&dev_priv->irq_lock);
628 dev_priv->hotplug.long_port_mask = 0;
629 dev_priv->hotplug.short_port_mask = 0;
630 dev_priv->hotplug.event_bits = 0;
632 spin_unlock_irq(&dev_priv->irq_lock);
634 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
635 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
636 cancel_work_sync(&dev_priv->hotplug.poll_init_work);
637 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
640 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
647 spin_lock_irq(&dev_priv->irq_lock);
648 if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
649 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
652 spin_unlock_irq(&dev_priv->irq_lock);
657 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
662 spin_lock_irq(&dev_priv->irq_lock);
663 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
664 spin_unlock_irq(&dev_priv->irq_lock);