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"
32 enum port intel_hpd_pin_to_port(enum hpd_pin pin)
42 return PORT_A; /* no hpd */
46 #define HPD_STORM_DETECT_PERIOD 1000
47 #define HPD_STORM_THRESHOLD 5
48 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
51 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
52 * @dev_priv: private driver data pointer
53 * @pin: the pin to gather stats on
55 * Gather stats about HPD irqs from the specified @pin, and detect irq
56 * storms. Only the pin specific stats and state are changed, the caller is
57 * responsible for further action.
59 * @HPD_STORM_THRESHOLD irqs are allowed within @HPD_STORM_DETECT_PERIOD ms,
60 * otherwise it's considered an irq storm, and the irq state is set to
63 * Return true if an irq storm was detected on @pin.
65 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
68 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
69 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
72 if (!time_in_range(jiffies, start, end)) {
73 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
74 dev_priv->hotplug.stats[pin].count = 0;
75 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
76 } else if (dev_priv->hotplug.stats[pin].count > HPD_STORM_THRESHOLD) {
77 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
78 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
81 dev_priv->hotplug.stats[pin].count++;
82 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
83 dev_priv->hotplug.stats[pin].count);
89 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
91 struct drm_device *dev = dev_priv->dev;
92 struct drm_mode_config *mode_config = &dev->mode_config;
93 struct intel_connector *intel_connector;
94 struct intel_encoder *intel_encoder;
95 struct drm_connector *connector;
97 bool hpd_disabled = false;
99 assert_spin_locked(&dev_priv->irq_lock);
101 list_for_each_entry(connector, &mode_config->connector_list, head) {
102 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
105 intel_connector = to_intel_connector(connector);
106 intel_encoder = intel_connector->encoder;
110 pin = intel_encoder->hpd_pin;
111 if (pin == HPD_NONE ||
112 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
115 DRM_INFO("HPD interrupt storm detected on connector %s: "
116 "switching from hotplug detection to polling\n",
119 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
120 connector->polled = DRM_CONNECTOR_POLL_CONNECT
121 | DRM_CONNECTOR_POLL_DISCONNECT;
125 /* Enable polling and queue hotplug re-enabling. */
127 drm_kms_helper_poll_enable(dev);
128 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
129 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
133 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
135 struct drm_i915_private *dev_priv =
136 container_of(work, typeof(*dev_priv),
137 hotplug.reenable_work.work);
138 struct drm_device *dev = dev_priv->dev;
139 struct drm_mode_config *mode_config = &dev->mode_config;
142 intel_runtime_pm_get(dev_priv);
144 spin_lock_irq(&dev_priv->irq_lock);
145 for_each_hpd_pin(i) {
146 struct drm_connector *connector;
148 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
151 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
153 list_for_each_entry(connector, &mode_config->connector_list, head) {
154 struct intel_connector *intel_connector = to_intel_connector(connector);
156 if (intel_connector->encoder->hpd_pin == i) {
157 if (connector->polled != intel_connector->polled)
158 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
160 connector->polled = intel_connector->polled;
161 if (!connector->polled)
162 connector->polled = DRM_CONNECTOR_POLL_HPD;
166 if (dev_priv->display.hpd_irq_setup)
167 dev_priv->display.hpd_irq_setup(dev);
168 spin_unlock_irq(&dev_priv->irq_lock);
170 intel_runtime_pm_put(dev_priv);
173 static bool intel_hpd_irq_event(struct drm_device *dev,
174 struct drm_connector *connector)
176 enum drm_connector_status old_status;
178 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
179 old_status = connector->status;
181 connector->status = connector->funcs->detect(connector, false);
182 if (old_status == connector->status)
185 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
188 drm_get_connector_status_name(old_status),
189 drm_get_connector_status_name(connector->status));
194 static void i915_digport_work_func(struct work_struct *work)
196 struct drm_i915_private *dev_priv =
197 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
198 u32 long_port_mask, short_port_mask;
199 struct intel_digital_port *intel_dig_port;
203 spin_lock_irq(&dev_priv->irq_lock);
204 long_port_mask = dev_priv->hotplug.long_port_mask;
205 dev_priv->hotplug.long_port_mask = 0;
206 short_port_mask = dev_priv->hotplug.short_port_mask;
207 dev_priv->hotplug.short_port_mask = 0;
208 spin_unlock_irq(&dev_priv->irq_lock);
210 for (i = 0; i < I915_MAX_PORTS; i++) {
212 bool long_hpd = false;
213 intel_dig_port = dev_priv->hotplug.irq_port[i];
214 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
217 if (long_port_mask & (1 << i)) {
220 } else if (short_port_mask & (1 << i))
226 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
227 if (ret == IRQ_NONE) {
228 /* fall back to old school hpd */
229 old_bits |= (1 << intel_dig_port->base.hpd_pin);
235 spin_lock_irq(&dev_priv->irq_lock);
236 dev_priv->hotplug.event_bits |= old_bits;
237 spin_unlock_irq(&dev_priv->irq_lock);
238 schedule_work(&dev_priv->hotplug.hotplug_work);
243 * Handle hotplug events outside the interrupt handler proper.
245 static void i915_hotplug_work_func(struct work_struct *work)
247 struct drm_i915_private *dev_priv =
248 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
249 struct drm_device *dev = dev_priv->dev;
250 struct drm_mode_config *mode_config = &dev->mode_config;
251 struct intel_connector *intel_connector;
252 struct intel_encoder *intel_encoder;
253 struct drm_connector *connector;
254 bool changed = false;
257 mutex_lock(&mode_config->mutex);
258 DRM_DEBUG_KMS("running encoder hotplug functions\n");
260 spin_lock_irq(&dev_priv->irq_lock);
262 hpd_event_bits = dev_priv->hotplug.event_bits;
263 dev_priv->hotplug.event_bits = 0;
265 /* Disable hotplug on connectors that hit an irq storm. */
266 intel_hpd_irq_storm_disable(dev_priv);
268 spin_unlock_irq(&dev_priv->irq_lock);
270 list_for_each_entry(connector, &mode_config->connector_list, head) {
271 intel_connector = to_intel_connector(connector);
272 if (!intel_connector->encoder)
274 intel_encoder = intel_connector->encoder;
275 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
276 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
277 connector->name, intel_encoder->hpd_pin);
278 if (intel_encoder->hot_plug)
279 intel_encoder->hot_plug(intel_encoder);
280 if (intel_hpd_irq_event(dev, connector))
284 mutex_unlock(&mode_config->mutex);
287 drm_kms_helper_hotplug_event(dev);
292 * intel_hpd_irq_handler - main hotplug irq handler
294 * @pin_mask: a mask of hpd pins that have triggered the irq
295 * @long_mask: a mask of hpd pins that may be long hpd pulses
297 * This is the main hotplug irq handler for all platforms. The platform specific
298 * irq handlers call the platform specific hotplug irq handlers, which read and
299 * decode the appropriate registers into bitmasks about hpd pins that have
300 * triggered (@pin_mask), and which of those pins may be long pulses
301 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
302 * is not a digital port.
304 * Here, we do hotplug irq storm detection and mitigation, and pass further
305 * processing to appropriate bottom halves.
307 void intel_hpd_irq_handler(struct drm_device *dev,
308 u32 pin_mask, u32 long_mask)
310 struct drm_i915_private *dev_priv = dev->dev_private;
313 bool storm_detected = false;
314 bool queue_dig = false, queue_hp = false;
320 spin_lock(&dev_priv->irq_lock);
321 for_each_hpd_pin(i) {
322 if (!(BIT(i) & pin_mask))
325 port = intel_hpd_pin_to_port(i);
326 is_dig_port = port && dev_priv->hotplug.irq_port[port];
329 bool long_hpd = long_mask & BIT(i);
331 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
332 long_hpd ? "long" : "short");
334 * For long HPD pulses we want to have the digital queue happen,
335 * but we still want HPD storm detection to function.
339 dev_priv->hotplug.long_port_mask |= (1 << port);
341 /* for short HPD just trigger the digital queue */
342 dev_priv->hotplug.short_port_mask |= (1 << port);
347 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
349 * On GMCH platforms the interrupt mask bits only
350 * prevent irq generation, not the setting of the
351 * hotplug bits itself. So only WARN about unexpected
352 * interrupts on saner platforms.
354 WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
355 "Received HPD interrupt on pin %d although disabled\n", i);
359 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
363 dev_priv->hotplug.event_bits |= BIT(i);
367 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
368 dev_priv->hotplug.event_bits &= ~BIT(i);
369 storm_detected = true;
374 dev_priv->display.hpd_irq_setup(dev);
375 spin_unlock(&dev_priv->irq_lock);
378 * Our hotplug handler can grab modeset locks (by calling down into the
379 * fb helpers). Hence it must not be run on our own dev-priv->wq work
380 * queue for otherwise the flush_work in the pageflip code will
384 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
386 schedule_work(&dev_priv->hotplug.hotplug_work);
390 * intel_hpd_init - initializes and enables hpd support
391 * @dev_priv: i915 device instance
393 * This function enables the hotplug support. It requires that interrupts have
394 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
395 * poll request can run concurrently to other code, so locking rules must be
398 * This is a separate step from interrupt enabling to simplify the locking rules
399 * in the driver load and resume code.
401 void intel_hpd_init(struct drm_i915_private *dev_priv)
403 struct drm_device *dev = dev_priv->dev;
404 struct drm_mode_config *mode_config = &dev->mode_config;
405 struct drm_connector *connector;
408 for_each_hpd_pin(i) {
409 dev_priv->hotplug.stats[i].count = 0;
410 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
412 list_for_each_entry(connector, &mode_config->connector_list, head) {
413 struct intel_connector *intel_connector = to_intel_connector(connector);
414 connector->polled = intel_connector->polled;
415 if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
416 connector->polled = DRM_CONNECTOR_POLL_HPD;
417 if (intel_connector->mst_port)
418 connector->polled = DRM_CONNECTOR_POLL_HPD;
422 * Interrupt setup is already guaranteed to be single-threaded, this is
423 * just to make the assert_spin_locked checks happy.
425 spin_lock_irq(&dev_priv->irq_lock);
426 if (dev_priv->display.hpd_irq_setup)
427 dev_priv->display.hpd_irq_setup(dev);
428 spin_unlock_irq(&dev_priv->irq_lock);
431 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
433 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
434 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
435 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
436 intel_hpd_irq_storm_reenable_work);
439 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
441 spin_lock_irq(&dev_priv->irq_lock);
443 dev_priv->hotplug.long_port_mask = 0;
444 dev_priv->hotplug.short_port_mask = 0;
445 dev_priv->hotplug.event_bits = 0;
447 spin_unlock_irq(&dev_priv->irq_lock);
449 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
450 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
451 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);