1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/kernel.h>
25
26#include <drm/i915_drm.h>
27
28#include "i915_drv.h"
29#include "intel_drv.h"
30#include "intel_hotplug.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
90 enum port port)
91{
92 switch (port) {
93 case PORT_A:
94 return HPD_PORT_A;
95 case PORT_B:
96 return HPD_PORT_B;
97 case PORT_C:
98 return HPD_PORT_C;
99 case PORT_D:
100 return HPD_PORT_D;
101 case PORT_E:
102 return HPD_PORT_E;
103 case PORT_F:
104 if (IS_CNL_WITH_PORT_F(dev_priv))
105 return HPD_PORT_E;
106 return HPD_PORT_F;
107 default:
108 MISSING_CASE(port);
109 return HPD_NONE;
110 }
111}
112
113#define HPD_STORM_DETECT_PERIOD 1000
114#define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
146 enum hpd_pin pin, bool long_hpd)
147{
148 struct i915_hotplug *hpd = &dev_priv->hotplug;
149 unsigned long start = hpd->stats[pin].last_jiffies;
150 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
151 const int increment = long_hpd ? 10 : 1;
152 const int threshold = hpd->hpd_storm_threshold;
153 bool storm = false;
154
155 if (!threshold ||
156 (!long_hpd && !dev_priv->hotplug.hpd_short_storm_enabled))
157 return false;
158
159 if (!time_in_range(jiffies, start, end)) {
160 hpd->stats[pin].last_jiffies = jiffies;
161 hpd->stats[pin].count = 0;
162 }
163
164 hpd->stats[pin].count += increment;
165 if (hpd->stats[pin].count > threshold) {
166 hpd->stats[pin].state = HPD_MARK_DISABLED;
167 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
168 storm = true;
169 } else {
170 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
171 hpd->stats[pin].count);
172 }
173
174 return storm;
175}
176
177static void
178intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
179{
180 struct drm_device *dev = &dev_priv->drm;
181 struct intel_connector *intel_connector;
182 struct intel_encoder *intel_encoder;
183 struct drm_connector *connector;
184 struct drm_connector_list_iter conn_iter;
185 enum hpd_pin pin;
186 bool hpd_disabled = false;
187
188 lockdep_assert_held(&dev_priv->irq_lock);
189
190 drm_connector_list_iter_begin(dev, &conn_iter);
191 drm_for_each_connector_iter(connector, &conn_iter) {
192 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
193 continue;
194
195 intel_connector = to_intel_connector(connector);
196 intel_encoder = intel_connector->encoder;
197 if (!intel_encoder)
198 continue;
199
200 pin = intel_encoder->hpd_pin;
201 if (pin == HPD_NONE ||
202 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
203 continue;
204
205 DRM_INFO("HPD interrupt storm detected on connector %s: "
206 "switching from hotplug detection to polling\n",
207 connector->name);
208
209 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
210 connector->polled = DRM_CONNECTOR_POLL_CONNECT
211 | DRM_CONNECTOR_POLL_DISCONNECT;
212 hpd_disabled = true;
213 }
214 drm_connector_list_iter_end(&conn_iter);
215
216
217 if (hpd_disabled) {
218 drm_kms_helper_poll_enable(dev);
219 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
220 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
221 }
222}
223
224static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
225{
226 struct drm_i915_private *dev_priv =
227 container_of(work, typeof(*dev_priv),
228 hotplug.reenable_work.work);
229 struct drm_device *dev = &dev_priv->drm;
230 intel_wakeref_t wakeref;
231 enum hpd_pin pin;
232
233 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
234
235 spin_lock_irq(&dev_priv->irq_lock);
236 for_each_hpd_pin(pin) {
237 struct drm_connector *connector;
238 struct drm_connector_list_iter conn_iter;
239
240 if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
241 continue;
242
243 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
244
245 drm_connector_list_iter_begin(dev, &conn_iter);
246 drm_for_each_connector_iter(connector, &conn_iter) {
247 struct intel_connector *intel_connector = to_intel_connector(connector);
248
249
250 if (!intel_connector->mst_port &&
251 intel_connector->encoder->hpd_pin == pin) {
252 if (connector->polled != intel_connector->polled)
253 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
254 connector->name);
255 connector->polled = intel_connector->polled;
256 if (!connector->polled)
257 connector->polled = DRM_CONNECTOR_POLL_HPD;
258 }
259 }
260 drm_connector_list_iter_end(&conn_iter);
261 }
262 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
263 dev_priv->display.hpd_irq_setup(dev_priv);
264 spin_unlock_irq(&dev_priv->irq_lock);
265
266 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
267}
268
269bool intel_encoder_hotplug(struct intel_encoder *encoder,
270 struct intel_connector *connector)
271{
272 struct drm_device *dev = connector->base.dev;
273 enum drm_connector_status old_status;
274
275 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
276 old_status = connector->base.status;
277
278 connector->base.status =
279 drm_helper_probe_detect(&connector->base, NULL, false);
280
281 if (old_status == connector->base.status)
282 return false;
283
284 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
285 connector->base.base.id,
286 connector->base.name,
287 drm_get_connector_status_name(old_status),
288 drm_get_connector_status_name(connector->base.status));
289
290 return true;
291}
292
293static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
294{
295 return intel_encoder_is_dig_port(encoder) &&
296 enc_to_dig_port(&encoder->base)->hpd_pulse != NULL;
297}
298
299static void i915_digport_work_func(struct work_struct *work)
300{
301 struct drm_i915_private *dev_priv =
302 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
303 u32 long_port_mask, short_port_mask;
304 struct intel_encoder *encoder;
305 u32 old_bits = 0;
306
307 spin_lock_irq(&dev_priv->irq_lock);
308 long_port_mask = dev_priv->hotplug.long_port_mask;
309 dev_priv->hotplug.long_port_mask = 0;
310 short_port_mask = dev_priv->hotplug.short_port_mask;
311 dev_priv->hotplug.short_port_mask = 0;
312 spin_unlock_irq(&dev_priv->irq_lock);
313
314 for_each_intel_encoder(&dev_priv->drm, encoder) {
315 struct intel_digital_port *dig_port;
316 enum port port = encoder->port;
317 bool long_hpd, short_hpd;
318 enum irqreturn ret;
319
320 if (!intel_encoder_has_hpd_pulse(encoder))
321 continue;
322
323 long_hpd = long_port_mask & BIT(port);
324 short_hpd = short_port_mask & BIT(port);
325
326 if (!long_hpd && !short_hpd)
327 continue;
328
329 dig_port = enc_to_dig_port(&encoder->base);
330
331 ret = dig_port->hpd_pulse(dig_port, long_hpd);
332 if (ret == IRQ_NONE) {
333
334 old_bits |= BIT(encoder->hpd_pin);
335 }
336 }
337
338 if (old_bits) {
339 spin_lock_irq(&dev_priv->irq_lock);
340 dev_priv->hotplug.event_bits |= old_bits;
341 spin_unlock_irq(&dev_priv->irq_lock);
342 schedule_work(&dev_priv->hotplug.hotplug_work);
343 }
344}
345
346
347
348
349static void i915_hotplug_work_func(struct work_struct *work)
350{
351 struct drm_i915_private *dev_priv =
352 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
353 struct drm_device *dev = &dev_priv->drm;
354 struct intel_connector *intel_connector;
355 struct intel_encoder *intel_encoder;
356 struct drm_connector *connector;
357 struct drm_connector_list_iter conn_iter;
358 bool changed = false;
359 u32 hpd_event_bits;
360
361 mutex_lock(&dev->mode_config.mutex);
362 DRM_DEBUG_KMS("running encoder hotplug functions\n");
363
364 spin_lock_irq(&dev_priv->irq_lock);
365
366 hpd_event_bits = dev_priv->hotplug.event_bits;
367 dev_priv->hotplug.event_bits = 0;
368
369
370 intel_hpd_irq_storm_switch_to_polling(dev_priv);
371
372 spin_unlock_irq(&dev_priv->irq_lock);
373
374 drm_connector_list_iter_begin(dev, &conn_iter);
375 drm_for_each_connector_iter(connector, &conn_iter) {
376 intel_connector = to_intel_connector(connector);
377 if (!intel_connector->encoder)
378 continue;
379 intel_encoder = intel_connector->encoder;
380 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
381 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
382 connector->name, intel_encoder->hpd_pin);
383
384 changed |= intel_encoder->hotplug(intel_encoder,
385 intel_connector);
386 }
387 }
388 drm_connector_list_iter_end(&conn_iter);
389 mutex_unlock(&dev->mode_config.mutex);
390
391 if (changed)
392 drm_kms_helper_hotplug_event(dev);
393}
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
413 u32 pin_mask, u32 long_mask)
414{
415 struct intel_encoder *encoder;
416 bool storm_detected = false;
417 bool queue_dig = false, queue_hp = false;
418 u32 long_hpd_pulse_mask = 0;
419 u32 short_hpd_pulse_mask = 0;
420 enum hpd_pin pin;
421
422 if (!pin_mask)
423 return;
424
425 spin_lock(&dev_priv->irq_lock);
426
427
428
429
430
431
432
433 for_each_intel_encoder(&dev_priv->drm, encoder) {
434 bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
435 enum port port = encoder->port;
436 bool long_hpd;
437
438 pin = encoder->hpd_pin;
439 if (!(BIT(pin) & pin_mask))
440 continue;
441
442 if (!has_hpd_pulse)
443 continue;
444
445 long_hpd = long_mask & BIT(pin);
446
447 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
448 long_hpd ? "long" : "short");
449 queue_dig = true;
450
451 if (long_hpd) {
452 long_hpd_pulse_mask |= BIT(pin);
453 dev_priv->hotplug.long_port_mask |= BIT(port);
454 } else {
455 short_hpd_pulse_mask |= BIT(pin);
456 dev_priv->hotplug.short_port_mask |= BIT(port);
457 }
458 }
459
460
461 for_each_hpd_pin(pin) {
462 bool long_hpd;
463
464 if (!(BIT(pin) & pin_mask))
465 continue;
466
467 if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
468
469
470
471
472
473
474 WARN_ONCE(!HAS_GMCH(dev_priv),
475 "Received HPD interrupt on pin %d although disabled\n", pin);
476 continue;
477 }
478
479 if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
480 continue;
481
482
483
484
485
486
487 if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
488 long_hpd = long_hpd_pulse_mask & BIT(pin);
489 } else {
490 dev_priv->hotplug.event_bits |= BIT(pin);
491 long_hpd = true;
492 queue_hp = true;
493 }
494
495 if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
496 dev_priv->hotplug.event_bits &= ~BIT(pin);
497 storm_detected = true;
498 queue_hp = true;
499 }
500 }
501
502
503
504
505
506 if (storm_detected && dev_priv->display_irqs_enabled)
507 dev_priv->display.hpd_irq_setup(dev_priv);
508 spin_unlock(&dev_priv->irq_lock);
509
510
511
512
513
514
515
516 if (queue_dig)
517 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
518 if (queue_hp)
519 schedule_work(&dev_priv->hotplug.hotplug_work);
520}
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536void intel_hpd_init(struct drm_i915_private *dev_priv)
537{
538 int i;
539
540 for_each_hpd_pin(i) {
541 dev_priv->hotplug.stats[i].count = 0;
542 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
543 }
544
545 WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
546 schedule_work(&dev_priv->hotplug.poll_init_work);
547
548
549
550
551
552 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
553 spin_lock_irq(&dev_priv->irq_lock);
554 if (dev_priv->display_irqs_enabled)
555 dev_priv->display.hpd_irq_setup(dev_priv);
556 spin_unlock_irq(&dev_priv->irq_lock);
557 }
558}
559
560static void i915_hpd_poll_init_work(struct work_struct *work)
561{
562 struct drm_i915_private *dev_priv =
563 container_of(work, struct drm_i915_private,
564 hotplug.poll_init_work);
565 struct drm_device *dev = &dev_priv->drm;
566 struct drm_connector *connector;
567 struct drm_connector_list_iter conn_iter;
568 bool enabled;
569
570 mutex_lock(&dev->mode_config.mutex);
571
572 enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
573
574 drm_connector_list_iter_begin(dev, &conn_iter);
575 drm_for_each_connector_iter(connector, &conn_iter) {
576 struct intel_connector *intel_connector =
577 to_intel_connector(connector);
578 connector->polled = intel_connector->polled;
579
580
581
582 if (intel_connector->mst_port)
583 continue;
584
585 if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
586 intel_connector->encoder->hpd_pin > HPD_NONE) {
587 connector->polled = enabled ?
588 DRM_CONNECTOR_POLL_CONNECT |
589 DRM_CONNECTOR_POLL_DISCONNECT :
590 DRM_CONNECTOR_POLL_HPD;
591 }
592 }
593 drm_connector_list_iter_end(&conn_iter);
594
595 if (enabled)
596 drm_kms_helper_poll_enable(dev);
597
598 mutex_unlock(&dev->mode_config.mutex);
599
600
601
602
603
604 if (!enabled)
605 drm_helper_hpd_irq_event(dev);
606}
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
625{
626 WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
627
628
629
630
631
632
633
634 schedule_work(&dev_priv->hotplug.poll_init_work);
635}
636
637void intel_hpd_init_work(struct drm_i915_private *dev_priv)
638{
639 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
640 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
641 INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
642 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
643 intel_hpd_irq_storm_reenable_work);
644}
645
646void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
647{
648 spin_lock_irq(&dev_priv->irq_lock);
649
650 dev_priv->hotplug.long_port_mask = 0;
651 dev_priv->hotplug.short_port_mask = 0;
652 dev_priv->hotplug.event_bits = 0;
653
654 spin_unlock_irq(&dev_priv->irq_lock);
655
656 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
657 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
658 cancel_work_sync(&dev_priv->hotplug.poll_init_work);
659 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
660}
661
662bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
663{
664 bool ret = false;
665
666 if (pin == HPD_NONE)
667 return false;
668
669 spin_lock_irq(&dev_priv->irq_lock);
670 if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
671 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
672 ret = true;
673 }
674 spin_unlock_irq(&dev_priv->irq_lock);
675
676 return ret;
677}
678
679void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
680{
681 if (pin == HPD_NONE)
682 return;
683
684 spin_lock_irq(&dev_priv->irq_lock);
685 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
686 spin_unlock_irq(&dev_priv->irq_lock);
687}
688