drm/i915: don't use HPD_PORT_A as an alias for HPD_NONE
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_hotplug.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <linux/kernel.h>
25
26 #include <drm/drmP.h>
27 #include <drm/i915_drm.h>
28
29 #include "i915_drv.h"
30 #include "intel_drv.h"
31
32 /**
33 * DOC: Hotplug
34 *
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.
38 *
39 * Hotplug in i915 is handled in many different levels of abstraction.
40 *
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.
45 *
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
49 * regular hotplug).
50 *
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.
55 *
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().
59 *
60 * Finally, the userspace is responsible for triggering a modeset upon receiving
61 * the hotplug uevent, disabling or enabling the crtc as needed.
62 *
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.
69 *
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.
77 */
78
79 bool intel_hpd_pin_to_port(enum hpd_pin pin, enum port *port)
80 {
81 switch (pin) {
82 case HPD_PORT_A:
83 *port = PORT_A;
84 return true;
85 case HPD_PORT_B:
86 *port = PORT_B;
87 return true;
88 case HPD_PORT_C:
89 *port = PORT_C;
90 return true;
91 case HPD_PORT_D:
92 *port = PORT_D;
93 return true;
94 default:
95 return false; /* no hpd */
96 }
97 }
98
99 #define HPD_STORM_DETECT_PERIOD 1000
100 #define HPD_STORM_THRESHOLD 5
101 #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
102
103 /**
104 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
105 * @dev_priv: private driver data pointer
106 * @pin: the pin to gather stats on
107 *
108 * Gather stats about HPD irqs from the specified @pin, and detect irq
109 * storms. Only the pin specific stats and state are changed, the caller is
110 * responsible for further action.
111 *
112 * @HPD_STORM_THRESHOLD irqs are allowed within @HPD_STORM_DETECT_PERIOD ms,
113 * otherwise it's considered an irq storm, and the irq state is set to
114 * @HPD_MARK_DISABLED.
115 *
116 * Return true if an irq storm was detected on @pin.
117 */
118 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
119 enum hpd_pin pin)
120 {
121 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
122 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
123 bool storm = false;
124
125 if (!time_in_range(jiffies, start, end)) {
126 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
127 dev_priv->hotplug.stats[pin].count = 0;
128 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
129 } else if (dev_priv->hotplug.stats[pin].count > HPD_STORM_THRESHOLD) {
130 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
131 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
132 storm = true;
133 } else {
134 dev_priv->hotplug.stats[pin].count++;
135 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
136 dev_priv->hotplug.stats[pin].count);
137 }
138
139 return storm;
140 }
141
142 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
143 {
144 struct drm_device *dev = dev_priv->dev;
145 struct drm_mode_config *mode_config = &dev->mode_config;
146 struct intel_connector *intel_connector;
147 struct intel_encoder *intel_encoder;
148 struct drm_connector *connector;
149 enum hpd_pin pin;
150 bool hpd_disabled = false;
151
152 assert_spin_locked(&dev_priv->irq_lock);
153
154 list_for_each_entry(connector, &mode_config->connector_list, head) {
155 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
156 continue;
157
158 intel_connector = to_intel_connector(connector);
159 intel_encoder = intel_connector->encoder;
160 if (!intel_encoder)
161 continue;
162
163 pin = intel_encoder->hpd_pin;
164 if (pin == HPD_NONE ||
165 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
166 continue;
167
168 DRM_INFO("HPD interrupt storm detected on connector %s: "
169 "switching from hotplug detection to polling\n",
170 connector->name);
171
172 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
173 connector->polled = DRM_CONNECTOR_POLL_CONNECT
174 | DRM_CONNECTOR_POLL_DISCONNECT;
175 hpd_disabled = true;
176 }
177
178 /* Enable polling and queue hotplug re-enabling. */
179 if (hpd_disabled) {
180 drm_kms_helper_poll_enable(dev);
181 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
182 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
183 }
184 }
185
186 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
187 {
188 struct drm_i915_private *dev_priv =
189 container_of(work, typeof(*dev_priv),
190 hotplug.reenable_work.work);
191 struct drm_device *dev = dev_priv->dev;
192 struct drm_mode_config *mode_config = &dev->mode_config;
193 int i;
194
195 intel_runtime_pm_get(dev_priv);
196
197 spin_lock_irq(&dev_priv->irq_lock);
198 for_each_hpd_pin(i) {
199 struct drm_connector *connector;
200
201 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
202 continue;
203
204 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
205
206 list_for_each_entry(connector, &mode_config->connector_list, head) {
207 struct intel_connector *intel_connector = to_intel_connector(connector);
208
209 if (intel_connector->encoder->hpd_pin == i) {
210 if (connector->polled != intel_connector->polled)
211 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
212 connector->name);
213 connector->polled = intel_connector->polled;
214 if (!connector->polled)
215 connector->polled = DRM_CONNECTOR_POLL_HPD;
216 }
217 }
218 }
219 if (dev_priv->display.hpd_irq_setup)
220 dev_priv->display.hpd_irq_setup(dev);
221 spin_unlock_irq(&dev_priv->irq_lock);
222
223 intel_runtime_pm_put(dev_priv);
224 }
225
226 static bool intel_hpd_irq_event(struct drm_device *dev,
227 struct drm_connector *connector)
228 {
229 enum drm_connector_status old_status;
230
231 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
232 old_status = connector->status;
233
234 connector->status = connector->funcs->detect(connector, false);
235 if (old_status == connector->status)
236 return false;
237
238 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
239 connector->base.id,
240 connector->name,
241 drm_get_connector_status_name(old_status),
242 drm_get_connector_status_name(connector->status));
243
244 return true;
245 }
246
247 static void i915_digport_work_func(struct work_struct *work)
248 {
249 struct drm_i915_private *dev_priv =
250 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
251 u32 long_port_mask, short_port_mask;
252 struct intel_digital_port *intel_dig_port;
253 int i;
254 u32 old_bits = 0;
255
256 spin_lock_irq(&dev_priv->irq_lock);
257 long_port_mask = dev_priv->hotplug.long_port_mask;
258 dev_priv->hotplug.long_port_mask = 0;
259 short_port_mask = dev_priv->hotplug.short_port_mask;
260 dev_priv->hotplug.short_port_mask = 0;
261 spin_unlock_irq(&dev_priv->irq_lock);
262
263 for (i = 0; i < I915_MAX_PORTS; i++) {
264 bool valid = false;
265 bool long_hpd = false;
266 intel_dig_port = dev_priv->hotplug.irq_port[i];
267 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
268 continue;
269
270 if (long_port_mask & (1 << i)) {
271 valid = true;
272 long_hpd = true;
273 } else if (short_port_mask & (1 << i))
274 valid = true;
275
276 if (valid) {
277 enum irqreturn ret;
278
279 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
280 if (ret == IRQ_NONE) {
281 /* fall back to old school hpd */
282 old_bits |= (1 << intel_dig_port->base.hpd_pin);
283 }
284 }
285 }
286
287 if (old_bits) {
288 spin_lock_irq(&dev_priv->irq_lock);
289 dev_priv->hotplug.event_bits |= old_bits;
290 spin_unlock_irq(&dev_priv->irq_lock);
291 schedule_work(&dev_priv->hotplug.hotplug_work);
292 }
293 }
294
295 /*
296 * Handle hotplug events outside the interrupt handler proper.
297 */
298 static void i915_hotplug_work_func(struct work_struct *work)
299 {
300 struct drm_i915_private *dev_priv =
301 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
302 struct drm_device *dev = dev_priv->dev;
303 struct drm_mode_config *mode_config = &dev->mode_config;
304 struct intel_connector *intel_connector;
305 struct intel_encoder *intel_encoder;
306 struct drm_connector *connector;
307 bool changed = false;
308 u32 hpd_event_bits;
309
310 mutex_lock(&mode_config->mutex);
311 DRM_DEBUG_KMS("running encoder hotplug functions\n");
312
313 spin_lock_irq(&dev_priv->irq_lock);
314
315 hpd_event_bits = dev_priv->hotplug.event_bits;
316 dev_priv->hotplug.event_bits = 0;
317
318 /* Disable hotplug on connectors that hit an irq storm. */
319 intel_hpd_irq_storm_disable(dev_priv);
320
321 spin_unlock_irq(&dev_priv->irq_lock);
322
323 list_for_each_entry(connector, &mode_config->connector_list, head) {
324 intel_connector = to_intel_connector(connector);
325 if (!intel_connector->encoder)
326 continue;
327 intel_encoder = intel_connector->encoder;
328 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
329 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
330 connector->name, intel_encoder->hpd_pin);
331 if (intel_encoder->hot_plug)
332 intel_encoder->hot_plug(intel_encoder);
333 if (intel_hpd_irq_event(dev, connector))
334 changed = true;
335 }
336 }
337 mutex_unlock(&mode_config->mutex);
338
339 if (changed)
340 drm_kms_helper_hotplug_event(dev);
341 }
342
343
344 /**
345 * intel_hpd_irq_handler - main hotplug irq handler
346 * @dev: drm device
347 * @pin_mask: a mask of hpd pins that have triggered the irq
348 * @long_mask: a mask of hpd pins that may be long hpd pulses
349 *
350 * This is the main hotplug irq handler for all platforms. The platform specific
351 * irq handlers call the platform specific hotplug irq handlers, which read and
352 * decode the appropriate registers into bitmasks about hpd pins that have
353 * triggered (@pin_mask), and which of those pins may be long pulses
354 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
355 * is not a digital port.
356 *
357 * Here, we do hotplug irq storm detection and mitigation, and pass further
358 * processing to appropriate bottom halves.
359 */
360 void intel_hpd_irq_handler(struct drm_device *dev,
361 u32 pin_mask, u32 long_mask)
362 {
363 struct drm_i915_private *dev_priv = dev->dev_private;
364 int i;
365 enum port port;
366 bool storm_detected = false;
367 bool queue_dig = false, queue_hp = false;
368 bool is_dig_port;
369
370 if (!pin_mask)
371 return;
372
373 spin_lock(&dev_priv->irq_lock);
374 for_each_hpd_pin(i) {
375 if (!(BIT(i) & pin_mask))
376 continue;
377
378 is_dig_port = intel_hpd_pin_to_port(i, &port) &&
379 dev_priv->hotplug.irq_port[port];
380
381 if (is_dig_port) {
382 bool long_hpd = long_mask & BIT(i);
383
384 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
385 long_hpd ? "long" : "short");
386 /*
387 * For long HPD pulses we want to have the digital queue happen,
388 * but we still want HPD storm detection to function.
389 */
390 queue_dig = true;
391 if (long_hpd) {
392 dev_priv->hotplug.long_port_mask |= (1 << port);
393 } else {
394 /* for short HPD just trigger the digital queue */
395 dev_priv->hotplug.short_port_mask |= (1 << port);
396 continue;
397 }
398 }
399
400 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
401 /*
402 * On GMCH platforms the interrupt mask bits only
403 * prevent irq generation, not the setting of the
404 * hotplug bits itself. So only WARN about unexpected
405 * interrupts on saner platforms.
406 */
407 WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
408 "Received HPD interrupt on pin %d although disabled\n", i);
409 continue;
410 }
411
412 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
413 continue;
414
415 if (!is_dig_port) {
416 dev_priv->hotplug.event_bits |= BIT(i);
417 queue_hp = true;
418 }
419
420 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
421 dev_priv->hotplug.event_bits &= ~BIT(i);
422 storm_detected = true;
423 }
424 }
425
426 if (storm_detected)
427 dev_priv->display.hpd_irq_setup(dev);
428 spin_unlock(&dev_priv->irq_lock);
429
430 /*
431 * Our hotplug handler can grab modeset locks (by calling down into the
432 * fb helpers). Hence it must not be run on our own dev-priv->wq work
433 * queue for otherwise the flush_work in the pageflip code will
434 * deadlock.
435 */
436 if (queue_dig)
437 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
438 if (queue_hp)
439 schedule_work(&dev_priv->hotplug.hotplug_work);
440 }
441
442 /**
443 * intel_hpd_init - initializes and enables hpd support
444 * @dev_priv: i915 device instance
445 *
446 * This function enables the hotplug support. It requires that interrupts have
447 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
448 * poll request can run concurrently to other code, so locking rules must be
449 * obeyed.
450 *
451 * This is a separate step from interrupt enabling to simplify the locking rules
452 * in the driver load and resume code.
453 */
454 void intel_hpd_init(struct drm_i915_private *dev_priv)
455 {
456 struct drm_device *dev = dev_priv->dev;
457 struct drm_mode_config *mode_config = &dev->mode_config;
458 struct drm_connector *connector;
459 int i;
460
461 for_each_hpd_pin(i) {
462 dev_priv->hotplug.stats[i].count = 0;
463 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
464 }
465 list_for_each_entry(connector, &mode_config->connector_list, head) {
466 struct intel_connector *intel_connector = to_intel_connector(connector);
467 connector->polled = intel_connector->polled;
468 if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
469 connector->polled = DRM_CONNECTOR_POLL_HPD;
470 if (intel_connector->mst_port)
471 connector->polled = DRM_CONNECTOR_POLL_HPD;
472 }
473
474 /*
475 * Interrupt setup is already guaranteed to be single-threaded, this is
476 * just to make the assert_spin_locked checks happy.
477 */
478 spin_lock_irq(&dev_priv->irq_lock);
479 if (dev_priv->display.hpd_irq_setup)
480 dev_priv->display.hpd_irq_setup(dev);
481 spin_unlock_irq(&dev_priv->irq_lock);
482 }
483
484 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
485 {
486 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
487 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
488 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
489 intel_hpd_irq_storm_reenable_work);
490 }
491
492 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
493 {
494 spin_lock_irq(&dev_priv->irq_lock);
495
496 dev_priv->hotplug.long_port_mask = 0;
497 dev_priv->hotplug.short_port_mask = 0;
498 dev_priv->hotplug.event_bits = 0;
499
500 spin_unlock_irq(&dev_priv->irq_lock);
501
502 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
503 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
504 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
505 }
This page took 0.041764 seconds and 5 git commands to generate.