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.
79 bool intel_hpd_pin_to_port(enum hpd_pin pin, enum port *port)
98 return false; /* no hpd */
102 #define HPD_STORM_DETECT_PERIOD 1000
103 #define HPD_STORM_THRESHOLD 5
104 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
107 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
108 * @dev_priv: private driver data pointer
109 * @pin: the pin to gather stats on
111 * Gather stats about HPD irqs from the specified @pin, and detect irq
112 * storms. Only the pin specific stats and state are changed, the caller is
113 * responsible for further action.
115 * @HPD_STORM_THRESHOLD irqs are allowed within @HPD_STORM_DETECT_PERIOD ms,
116 * otherwise it's considered an irq storm, and the irq state is set to
117 * @HPD_MARK_DISABLED.
119 * Return true if an irq storm was detected on @pin.
121 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
124 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
125 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
128 if (!time_in_range(jiffies, start, end)) {
129 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
130 dev_priv->hotplug.stats[pin].count = 0;
131 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
132 } else if (dev_priv->hotplug.stats[pin].count > HPD_STORM_THRESHOLD) {
133 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
134 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
137 dev_priv->hotplug.stats[pin].count++;
138 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
139 dev_priv->hotplug.stats[pin].count);
145 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
147 struct drm_device *dev = dev_priv->dev;
148 struct drm_mode_config *mode_config = &dev->mode_config;
149 struct intel_connector *intel_connector;
150 struct intel_encoder *intel_encoder;
151 struct drm_connector *connector;
153 bool hpd_disabled = false;
155 assert_spin_locked(&dev_priv->irq_lock);
157 list_for_each_entry(connector, &mode_config->connector_list, head) {
158 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
161 intel_connector = to_intel_connector(connector);
162 intel_encoder = intel_connector->encoder;
166 pin = intel_encoder->hpd_pin;
167 if (pin == HPD_NONE ||
168 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
171 DRM_INFO("HPD interrupt storm detected on connector %s: "
172 "switching from hotplug detection to polling\n",
175 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
176 connector->polled = DRM_CONNECTOR_POLL_CONNECT
177 | DRM_CONNECTOR_POLL_DISCONNECT;
181 /* Enable polling and queue hotplug re-enabling. */
183 drm_kms_helper_poll_enable_locked(dev);
184 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
185 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
189 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
191 struct drm_i915_private *dev_priv =
192 container_of(work, typeof(*dev_priv),
193 hotplug.reenable_work.work);
194 struct drm_device *dev = dev_priv->dev;
195 struct drm_mode_config *mode_config = &dev->mode_config;
198 intel_runtime_pm_get(dev_priv);
200 spin_lock_irq(&dev_priv->irq_lock);
201 for_each_hpd_pin(i) {
202 struct drm_connector *connector;
204 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
207 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
209 list_for_each_entry(connector, &mode_config->connector_list, head) {
210 struct intel_connector *intel_connector = to_intel_connector(connector);
212 if (intel_connector->encoder->hpd_pin == i) {
213 if (connector->polled != intel_connector->polled)
214 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
216 connector->polled = intel_connector->polled;
217 if (!connector->polled)
218 connector->polled = DRM_CONNECTOR_POLL_HPD;
222 if (dev_priv->display.hpd_irq_setup)
223 dev_priv->display.hpd_irq_setup(dev_priv);
224 spin_unlock_irq(&dev_priv->irq_lock);
226 intel_runtime_pm_put(dev_priv);
229 static bool intel_hpd_irq_event(struct drm_device *dev,
230 struct drm_connector *connector)
232 enum drm_connector_status old_status;
234 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
235 old_status = connector->status;
237 connector->status = connector->funcs->detect(connector, false);
238 if (old_status == connector->status)
241 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
244 drm_get_connector_status_name(old_status),
245 drm_get_connector_status_name(connector->status));
250 static void i915_digport_work_func(struct work_struct *work)
252 struct drm_i915_private *dev_priv =
253 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
254 u32 long_port_mask, short_port_mask;
255 struct intel_digital_port *intel_dig_port;
259 spin_lock_irq(&dev_priv->irq_lock);
260 long_port_mask = dev_priv->hotplug.long_port_mask;
261 dev_priv->hotplug.long_port_mask = 0;
262 short_port_mask = dev_priv->hotplug.short_port_mask;
263 dev_priv->hotplug.short_port_mask = 0;
264 spin_unlock_irq(&dev_priv->irq_lock);
266 for (i = 0; i < I915_MAX_PORTS; i++) {
268 bool long_hpd = false;
269 intel_dig_port = dev_priv->hotplug.irq_port[i];
270 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
273 if (long_port_mask & (1 << i)) {
276 } else if (short_port_mask & (1 << i))
282 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
283 if (ret == IRQ_NONE) {
284 /* fall back to old school hpd */
285 old_bits |= (1 << intel_dig_port->base.hpd_pin);
291 spin_lock_irq(&dev_priv->irq_lock);
292 dev_priv->hotplug.event_bits |= old_bits;
293 spin_unlock_irq(&dev_priv->irq_lock);
294 schedule_work(&dev_priv->hotplug.hotplug_work);
299 * Handle hotplug events outside the interrupt handler proper.
301 static void i915_hotplug_work_func(struct work_struct *work)
303 struct drm_i915_private *dev_priv =
304 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
305 struct drm_device *dev = dev_priv->dev;
306 struct drm_mode_config *mode_config = &dev->mode_config;
307 struct intel_connector *intel_connector;
308 struct intel_encoder *intel_encoder;
309 struct drm_connector *connector;
310 bool changed = false;
313 mutex_lock(&mode_config->mutex);
314 DRM_DEBUG_KMS("running encoder hotplug functions\n");
316 spin_lock_irq(&dev_priv->irq_lock);
318 hpd_event_bits = dev_priv->hotplug.event_bits;
319 dev_priv->hotplug.event_bits = 0;
321 /* Disable hotplug on connectors that hit an irq storm. */
322 intel_hpd_irq_storm_disable(dev_priv);
324 spin_unlock_irq(&dev_priv->irq_lock);
326 list_for_each_entry(connector, &mode_config->connector_list, head) {
327 intel_connector = to_intel_connector(connector);
328 if (!intel_connector->encoder)
330 intel_encoder = intel_connector->encoder;
331 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
332 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
333 connector->name, intel_encoder->hpd_pin);
334 if (intel_encoder->hot_plug)
335 intel_encoder->hot_plug(intel_encoder);
336 if (intel_hpd_irq_event(dev, connector))
340 mutex_unlock(&mode_config->mutex);
343 drm_kms_helper_hotplug_event(dev);
348 * intel_hpd_irq_handler - main hotplug irq handler
349 * @dev_priv: drm_i915_private
350 * @pin_mask: a mask of hpd pins that have triggered the irq
351 * @long_mask: a mask of hpd pins that may be long hpd pulses
353 * This is the main hotplug irq handler for all platforms. The platform specific
354 * irq handlers call the platform specific hotplug irq handlers, which read and
355 * decode the appropriate registers into bitmasks about hpd pins that have
356 * triggered (@pin_mask), and which of those pins may be long pulses
357 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
358 * is not a digital port.
360 * Here, we do hotplug irq storm detection and mitigation, and pass further
361 * processing to appropriate bottom halves.
363 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
364 u32 pin_mask, u32 long_mask)
368 bool storm_detected = false;
369 bool queue_dig = false, queue_hp = false;
375 spin_lock(&dev_priv->irq_lock);
376 for_each_hpd_pin(i) {
377 if (!(BIT(i) & pin_mask))
380 is_dig_port = intel_hpd_pin_to_port(i, &port) &&
381 dev_priv->hotplug.irq_port[port];
384 bool long_hpd = long_mask & BIT(i);
386 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
387 long_hpd ? "long" : "short");
389 * For long HPD pulses we want to have the digital queue happen,
390 * but we still want HPD storm detection to function.
394 dev_priv->hotplug.long_port_mask |= (1 << port);
396 /* for short HPD just trigger the digital queue */
397 dev_priv->hotplug.short_port_mask |= (1 << port);
402 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
404 * On GMCH platforms the interrupt mask bits only
405 * prevent irq generation, not the setting of the
406 * hotplug bits itself. So only WARN about unexpected
407 * interrupts on saner platforms.
409 WARN_ONCE(!HAS_GMCH_DISPLAY(dev_priv),
410 "Received HPD interrupt on pin %d although disabled\n", i);
414 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
418 dev_priv->hotplug.event_bits |= BIT(i);
422 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
423 dev_priv->hotplug.event_bits &= ~BIT(i);
424 storm_detected = true;
429 dev_priv->display.hpd_irq_setup(dev_priv);
430 spin_unlock(&dev_priv->irq_lock);
433 * Our hotplug handler can grab modeset locks (by calling down into the
434 * fb helpers). Hence it must not be run on our own dev-priv->wq work
435 * queue for otherwise the flush_work in the pageflip code will
439 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
441 schedule_work(&dev_priv->hotplug.hotplug_work);
445 * intel_hpd_init - initializes and enables hpd support
446 * @dev_priv: i915 device instance
448 * This function enables the hotplug support. It requires that interrupts have
449 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
450 * poll request can run concurrently to other code, so locking rules must be
453 * This is a separate step from interrupt enabling to simplify the locking rules
454 * in the driver load and resume code.
456 void intel_hpd_init(struct drm_i915_private *dev_priv)
458 struct drm_device *dev = dev_priv->dev;
459 struct drm_mode_config *mode_config = &dev->mode_config;
460 struct drm_connector *connector;
463 for_each_hpd_pin(i) {
464 dev_priv->hotplug.stats[i].count = 0;
465 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
467 list_for_each_entry(connector, &mode_config->connector_list, head) {
468 struct intel_connector *intel_connector = to_intel_connector(connector);
469 connector->polled = intel_connector->polled;
471 /* MST has a dynamic intel_connector->encoder and it's reprobing
472 * is all handled by the MST helpers. */
473 if (intel_connector->mst_port)
476 if (!connector->polled && I915_HAS_HOTPLUG(dev) &&
477 intel_connector->encoder->hpd_pin > HPD_NONE)
478 connector->polled = DRM_CONNECTOR_POLL_HPD;
482 * Interrupt setup is already guaranteed to be single-threaded, this is
483 * just to make the assert_spin_locked checks happy.
485 spin_lock_irq(&dev_priv->irq_lock);
486 if (dev_priv->display.hpd_irq_setup)
487 dev_priv->display.hpd_irq_setup(dev_priv);
488 spin_unlock_irq(&dev_priv->irq_lock);
491 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
493 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
494 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
495 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
496 intel_hpd_irq_storm_reenable_work);
499 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
501 spin_lock_irq(&dev_priv->irq_lock);
503 dev_priv->hotplug.long_port_mask = 0;
504 dev_priv->hotplug.short_port_mask = 0;
505 dev_priv->hotplug.event_bits = 0;
507 spin_unlock_irq(&dev_priv->irq_lock);
509 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
510 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
511 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);