xref: /linux/drivers/gpu/drm/i915/display/intel_hotplug.c (revision a4871e6201c46c8e1d04308265b4b4c5753c8209)
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/debugfs.h>
25 #include <linux/kernel.h>
26 
27 #include <drm/drm_probe_helper.h>
28 
29 #include "i915_drv.h"
30 #include "i915_irq.h"
31 #include "intel_connector.h"
32 #include "intel_display_power.h"
33 #include "intel_display_rpm.h"
34 #include "intel_display_types.h"
35 #include "intel_hdcp.h"
36 #include "intel_hotplug.h"
37 #include "intel_hotplug_irq.h"
38 
39 /**
40  * DOC: Hotplug
41  *
42  * Simply put, hotplug occurs when a display is connected to or disconnected
43  * from the system. However, there may be adapters and docking stations and
44  * Display Port short pulses and MST devices involved, complicating matters.
45  *
46  * Hotplug in i915 is handled in many different levels of abstraction.
47  *
48  * The platform dependent interrupt handling code in i915_irq.c enables,
49  * disables, and does preliminary handling of the interrupts. The interrupt
50  * handlers gather the hotplug detect (HPD) information from relevant registers
51  * into a platform independent mask of hotplug pins that have fired.
52  *
53  * The platform independent interrupt handler intel_hpd_irq_handler() in
54  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
55  * further processing to appropriate bottom halves (Display Port specific and
56  * regular hotplug).
57  *
58  * The Display Port work function i915_digport_work_func() calls into
59  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
60  * pulses, with failures and non-MST long pulses triggering regular hotplug
61  * processing on the connector.
62  *
63  * The regular hotplug work function i915_hotplug_work_func() calls connector
64  * detect hooks, and, if connector status changes, triggers sending of hotplug
65  * uevent to userspace via drm_kms_helper_hotplug_event().
66  *
67  * Finally, the userspace is responsible for triggering a modeset upon receiving
68  * the hotplug uevent, disabling or enabling the crtc as needed.
69  *
70  * The hotplug interrupt storm detection and mitigation code keeps track of the
71  * number of interrupts per hotplug pin per a period of time, and if the number
72  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
73  * while before being re-enabled. The intention is to mitigate issues raising
74  * from broken hardware triggering massive amounts of interrupts and grinding
75  * the system to a halt.
76  *
77  * Current implementation expects that hotplug interrupt storm will not be
78  * seen when display port sink is connected, hence on platforms whose DP
79  * callback is handled by i915_digport_work_func reenabling of hpd is not
80  * performed (it was never expected to be disabled in the first place ;) )
81  * this is specific to DP sinks handled by this routine and any other display
82  * such as HDMI or DVI enabled on the same port will have proper logic since
83  * it will use i915_hotplug_work_func where this logic is handled.
84  */
85 
86 /**
87  * intel_hpd_pin_default - return default pin associated with certain port.
88  * @port: the hpd port to get associated pin
89  *
90  * It is only valid and used by digital port encoder.
91  *
92  * Return pin that is associatade with @port.
93  */
94 enum hpd_pin intel_hpd_pin_default(enum port port)
95 {
96 	return HPD_PORT_A + port - PORT_A;
97 }
98 
99 /* Threshold == 5 for long IRQs, 50 for short */
100 #define HPD_STORM_DEFAULT_THRESHOLD	50
101 
102 #define HPD_STORM_DETECT_PERIOD		1000
103 #define HPD_STORM_REENABLE_DELAY	(2 * 60 * 1000)
104 #define HPD_RETRY_DELAY			1000
105 
106 static enum hpd_pin
107 intel_connector_hpd_pin(struct intel_connector *connector)
108 {
109 	struct intel_encoder *encoder = intel_attached_encoder(connector);
110 
111 	/*
112 	 * MST connectors get their encoder attached dynamically
113 	 * so need to make sure we have an encoder here. But since
114 	 * MST encoders have their hpd_pin set to HPD_NONE we don't
115 	 * have to special case them beyond that.
116 	 */
117 	return encoder ? encoder->hpd_pin : HPD_NONE;
118 }
119 
120 /**
121  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
122  * @display: display device
123  * @pin: the pin to gather stats on
124  * @long_hpd: whether the HPD IRQ was long or short
125  *
126  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
127  * storms. Only the pin specific stats and state are changed, the caller is
128  * responsible for further action.
129  *
130  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
131  * stored in @display->hotplug.hpd_storm_threshold which defaults to
132  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
133  * short IRQs count as +1. If this threshold is exceeded, it's considered an
134  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
135  *
136  * By default, most systems will only count long IRQs towards
137  * &display->hotplug.hpd_storm_threshold. However, some older systems also
138  * suffer from short IRQ storms and must also track these. Because short IRQ
139  * storms are naturally caused by sideband interactions with DP MST devices,
140  * short IRQ detection is only enabled for systems without DP MST support.
141  * Systems which are new enough to support DP MST are far less likely to
142  * suffer from IRQ storms at all, so this is fine.
143  *
144  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
145  * and should only be adjusted for automated hotplug testing.
146  *
147  * Return true if an IRQ storm was detected on @pin.
148  */
149 static bool intel_hpd_irq_storm_detect(struct intel_display *display,
150 				       enum hpd_pin pin, bool long_hpd)
151 {
152 	struct intel_hotplug *hpd = &display->hotplug;
153 	unsigned long start = hpd->stats[pin].last_jiffies;
154 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
155 	const int increment = long_hpd ? 10 : 1;
156 	const int threshold = hpd->hpd_storm_threshold;
157 	bool storm = false;
158 
159 	if (!threshold ||
160 	    (!long_hpd && !display->hotplug.hpd_short_storm_enabled))
161 		return false;
162 
163 	if (!time_in_range(jiffies, start, end)) {
164 		hpd->stats[pin].last_jiffies = jiffies;
165 		hpd->stats[pin].count = 0;
166 	}
167 
168 	hpd->stats[pin].count += increment;
169 	if (hpd->stats[pin].count > threshold) {
170 		hpd->stats[pin].state = HPD_MARK_DISABLED;
171 		drm_dbg_kms(display->drm,
172 			    "HPD interrupt storm detected on PIN %d\n", pin);
173 		storm = true;
174 	} else {
175 		drm_dbg_kms(display->drm,
176 			    "Received HPD interrupt on PIN %d - cnt: %d\n",
177 			      pin,
178 			      hpd->stats[pin].count);
179 	}
180 
181 	return storm;
182 }
183 
184 static bool detection_work_enabled(struct intel_display *display)
185 {
186 	struct drm_i915_private *i915 = to_i915(display->drm);
187 
188 	lockdep_assert_held(&i915->irq_lock);
189 
190 	return display->hotplug.detection_work_enabled;
191 }
192 
193 static bool
194 mod_delayed_detection_work(struct intel_display *display, struct delayed_work *work, int delay)
195 {
196 	struct drm_i915_private *i915 = to_i915(display->drm);
197 
198 	lockdep_assert_held(&i915->irq_lock);
199 
200 	if (!detection_work_enabled(display))
201 		return false;
202 
203 	return mod_delayed_work(i915->unordered_wq, work, delay);
204 }
205 
206 static bool
207 queue_delayed_detection_work(struct intel_display *display, struct delayed_work *work, int delay)
208 {
209 	struct drm_i915_private *i915 = to_i915(display->drm);
210 
211 	lockdep_assert_held(&i915->irq_lock);
212 
213 	if (!detection_work_enabled(display))
214 		return false;
215 
216 	return queue_delayed_work(i915->unordered_wq, work, delay);
217 }
218 
219 static bool
220 queue_detection_work(struct intel_display *display, struct work_struct *work)
221 {
222 	struct drm_i915_private *i915 = to_i915(display->drm);
223 
224 	lockdep_assert_held(&i915->irq_lock);
225 
226 	if (!detection_work_enabled(display))
227 		return false;
228 
229 	return queue_work(i915->unordered_wq, work);
230 }
231 
232 static void
233 intel_hpd_irq_storm_switch_to_polling(struct intel_display *display)
234 {
235 	struct drm_i915_private *dev_priv = to_i915(display->drm);
236 	struct drm_connector_list_iter conn_iter;
237 	struct intel_connector *connector;
238 	bool hpd_disabled = false;
239 
240 	lockdep_assert_held(&dev_priv->irq_lock);
241 
242 	drm_connector_list_iter_begin(display->drm, &conn_iter);
243 	for_each_intel_connector_iter(connector, &conn_iter) {
244 		enum hpd_pin pin;
245 
246 		if (connector->base.polled != DRM_CONNECTOR_POLL_HPD)
247 			continue;
248 
249 		pin = intel_connector_hpd_pin(connector);
250 		if (pin == HPD_NONE ||
251 		    display->hotplug.stats[pin].state != HPD_MARK_DISABLED)
252 			continue;
253 
254 		drm_info(display->drm,
255 			 "HPD interrupt storm detected on connector %s: "
256 			 "switching from hotplug detection to polling\n",
257 			 connector->base.name);
258 
259 		display->hotplug.stats[pin].state = HPD_DISABLED;
260 		connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
261 			DRM_CONNECTOR_POLL_DISCONNECT;
262 		hpd_disabled = true;
263 	}
264 	drm_connector_list_iter_end(&conn_iter);
265 
266 	/* Enable polling and queue hotplug re-enabling. */
267 	if (hpd_disabled) {
268 		drm_kms_helper_poll_reschedule(display->drm);
269 		mod_delayed_detection_work(display,
270 					   &display->hotplug.reenable_work,
271 					   msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
272 	}
273 }
274 
275 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
276 {
277 	struct intel_display *display =
278 		container_of(work, typeof(*display), hotplug.reenable_work.work);
279 	struct drm_i915_private *dev_priv = to_i915(display->drm);
280 	struct drm_connector_list_iter conn_iter;
281 	struct intel_connector *connector;
282 	struct ref_tracker *wakeref;
283 	enum hpd_pin pin;
284 
285 	wakeref = intel_display_rpm_get(display);
286 
287 	spin_lock_irq(&dev_priv->irq_lock);
288 
289 	drm_connector_list_iter_begin(display->drm, &conn_iter);
290 	for_each_intel_connector_iter(connector, &conn_iter) {
291 		pin = intel_connector_hpd_pin(connector);
292 		if (pin == HPD_NONE ||
293 		    display->hotplug.stats[pin].state != HPD_DISABLED)
294 			continue;
295 
296 		if (connector->base.polled != connector->polled)
297 			drm_dbg(display->drm,
298 				"Reenabling HPD on connector %s\n",
299 				connector->base.name);
300 		connector->base.polled = connector->polled;
301 	}
302 	drm_connector_list_iter_end(&conn_iter);
303 
304 	for_each_hpd_pin(pin) {
305 		if (display->hotplug.stats[pin].state == HPD_DISABLED)
306 			display->hotplug.stats[pin].state = HPD_ENABLED;
307 	}
308 
309 	intel_hpd_irq_setup(display);
310 
311 	spin_unlock_irq(&dev_priv->irq_lock);
312 
313 	intel_display_rpm_put(display, wakeref);
314 }
315 
316 static enum intel_hotplug_state
317 intel_hotplug_detect_connector(struct intel_connector *connector)
318 {
319 	struct drm_device *dev = connector->base.dev;
320 	enum drm_connector_status old_status;
321 	u64 old_epoch_counter;
322 	int status;
323 	bool ret = false;
324 
325 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
326 	old_status = connector->base.status;
327 	old_epoch_counter = connector->base.epoch_counter;
328 
329 	status = drm_helper_probe_detect(&connector->base, NULL, false);
330 	if (!connector->base.force)
331 		connector->base.status = status;
332 
333 	if (old_epoch_counter != connector->base.epoch_counter)
334 		ret = true;
335 
336 	if (ret) {
337 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s (epoch counter %llu->%llu)\n",
338 			    connector->base.base.id,
339 			    connector->base.name,
340 			    drm_get_connector_status_name(old_status),
341 			    drm_get_connector_status_name(connector->base.status),
342 			    old_epoch_counter,
343 			    connector->base.epoch_counter);
344 		return INTEL_HOTPLUG_CHANGED;
345 	}
346 	return INTEL_HOTPLUG_UNCHANGED;
347 }
348 
349 enum intel_hotplug_state
350 intel_encoder_hotplug(struct intel_encoder *encoder,
351 		      struct intel_connector *connector)
352 {
353 	return intel_hotplug_detect_connector(connector);
354 }
355 
356 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
357 {
358 	return intel_encoder_is_dig_port(encoder) &&
359 		enc_to_dig_port(encoder)->hpd_pulse != NULL;
360 }
361 
362 static bool hpd_pin_has_pulse(struct intel_display *display, enum hpd_pin pin)
363 {
364 	struct intel_encoder *encoder;
365 
366 	for_each_intel_encoder(display->drm, encoder) {
367 		if (encoder->hpd_pin != pin)
368 			continue;
369 
370 		if (intel_encoder_has_hpd_pulse(encoder))
371 			return true;
372 	}
373 
374 	return false;
375 }
376 
377 static bool hpd_pin_is_blocked(struct intel_display *display, enum hpd_pin pin)
378 {
379 	struct drm_i915_private *i915 = to_i915(display->drm);
380 
381 	lockdep_assert_held(&i915->irq_lock);
382 
383 	return display->hotplug.stats[pin].blocked_count;
384 }
385 
386 static u32 get_blocked_hpd_pin_mask(struct intel_display *display)
387 {
388 	enum hpd_pin pin;
389 	u32 hpd_pin_mask = 0;
390 
391 	for_each_hpd_pin(pin) {
392 		if (hpd_pin_is_blocked(display, pin))
393 			hpd_pin_mask |= BIT(pin);
394 	}
395 
396 	return hpd_pin_mask;
397 }
398 
399 static void i915_digport_work_func(struct work_struct *work)
400 {
401 	struct intel_display *display =
402 		container_of(work, struct intel_display, hotplug.dig_port_work);
403 	struct drm_i915_private *dev_priv = to_i915(display->drm);
404 	struct intel_hotplug *hotplug = &display->hotplug;
405 	u32 long_hpd_pin_mask, short_hpd_pin_mask;
406 	struct intel_encoder *encoder;
407 	u32 blocked_hpd_pin_mask;
408 	u32 old_bits = 0;
409 
410 	spin_lock_irq(&dev_priv->irq_lock);
411 
412 	blocked_hpd_pin_mask = get_blocked_hpd_pin_mask(display);
413 	long_hpd_pin_mask = hotplug->long_hpd_pin_mask & ~blocked_hpd_pin_mask;
414 	hotplug->long_hpd_pin_mask &= ~long_hpd_pin_mask;
415 	short_hpd_pin_mask = hotplug->short_hpd_pin_mask & ~blocked_hpd_pin_mask;
416 	hotplug->short_hpd_pin_mask &= ~short_hpd_pin_mask;
417 
418 	spin_unlock_irq(&dev_priv->irq_lock);
419 
420 	for_each_intel_encoder(display->drm, encoder) {
421 		struct intel_digital_port *dig_port;
422 		enum hpd_pin pin = encoder->hpd_pin;
423 		bool long_hpd, short_hpd;
424 		enum irqreturn ret;
425 
426 		if (!intel_encoder_has_hpd_pulse(encoder))
427 			continue;
428 
429 		long_hpd = long_hpd_pin_mask & BIT(pin);
430 		short_hpd = short_hpd_pin_mask & BIT(pin);
431 
432 		if (!long_hpd && !short_hpd)
433 			continue;
434 
435 		dig_port = enc_to_dig_port(encoder);
436 
437 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
438 		if (ret == IRQ_NONE) {
439 			/* fall back to old school hpd */
440 			old_bits |= BIT(pin);
441 		}
442 	}
443 
444 	if (old_bits) {
445 		spin_lock_irq(&dev_priv->irq_lock);
446 		display->hotplug.event_bits |= old_bits;
447 		queue_delayed_detection_work(display,
448 					     &display->hotplug.hotplug_work, 0);
449 		spin_unlock_irq(&dev_priv->irq_lock);
450 	}
451 }
452 
453 /**
454  * intel_hpd_trigger_irq - trigger an hpd irq event for a port
455  * @dig_port: digital port
456  *
457  * Trigger an HPD interrupt event for the given port, emulating a short pulse
458  * generated by the sink, and schedule the dig port work to handle it.
459  */
460 void intel_hpd_trigger_irq(struct intel_digital_port *dig_port)
461 {
462 	struct intel_display *display = to_intel_display(dig_port);
463 	struct drm_i915_private *i915 = to_i915(display->drm);
464 	struct intel_hotplug *hotplug = &display->hotplug;
465 	struct intel_encoder *encoder = &dig_port->base;
466 
467 	spin_lock_irq(&i915->irq_lock);
468 
469 	hotplug->short_hpd_pin_mask |= BIT(encoder->hpd_pin);
470 	if (!hpd_pin_is_blocked(display, encoder->hpd_pin))
471 		queue_work(hotplug->dp_wq, &hotplug->dig_port_work);
472 
473 	spin_unlock_irq(&i915->irq_lock);
474 }
475 
476 /*
477  * Handle hotplug events outside the interrupt handler proper.
478  */
479 static void i915_hotplug_work_func(struct work_struct *work)
480 {
481 	struct intel_display *display =
482 		container_of(work, struct intel_display, hotplug.hotplug_work.work);
483 	struct drm_i915_private *dev_priv = to_i915(display->drm);
484 	struct intel_hotplug *hotplug = &display->hotplug;
485 	struct drm_connector_list_iter conn_iter;
486 	struct intel_connector *connector;
487 	u32 changed = 0, retry = 0;
488 	u32 hpd_event_bits;
489 	u32 hpd_retry_bits;
490 	struct drm_connector *first_changed_connector = NULL;
491 	int changed_connectors = 0;
492 	u32 blocked_hpd_pin_mask;
493 
494 	mutex_lock(&display->drm->mode_config.mutex);
495 	drm_dbg_kms(display->drm, "running encoder hotplug functions\n");
496 
497 	spin_lock_irq(&dev_priv->irq_lock);
498 
499 	blocked_hpd_pin_mask = get_blocked_hpd_pin_mask(display);
500 	hpd_event_bits = hotplug->event_bits & ~blocked_hpd_pin_mask;
501 	hotplug->event_bits &= ~hpd_event_bits;
502 	hpd_retry_bits = hotplug->retry_bits & ~blocked_hpd_pin_mask;
503 	hotplug->retry_bits &= ~hpd_retry_bits;
504 
505 	/* Enable polling for connectors which had HPD IRQ storms */
506 	intel_hpd_irq_storm_switch_to_polling(display);
507 
508 	spin_unlock_irq(&dev_priv->irq_lock);
509 
510 	/* Skip calling encode hotplug handlers if ignore long HPD set*/
511 	if (display->hotplug.ignore_long_hpd) {
512 		drm_dbg_kms(display->drm, "Ignore HPD flag on - skip encoder hotplug handlers\n");
513 		mutex_unlock(&display->drm->mode_config.mutex);
514 		return;
515 	}
516 
517 	drm_connector_list_iter_begin(display->drm, &conn_iter);
518 	for_each_intel_connector_iter(connector, &conn_iter) {
519 		enum hpd_pin pin;
520 		u32 hpd_bit;
521 
522 		pin = intel_connector_hpd_pin(connector);
523 		if (pin == HPD_NONE)
524 			continue;
525 
526 		hpd_bit = BIT(pin);
527 		if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
528 			struct intel_encoder *encoder =
529 				intel_attached_encoder(connector);
530 
531 			if (hpd_event_bits & hpd_bit)
532 				connector->hotplug_retries = 0;
533 			else
534 				connector->hotplug_retries++;
535 
536 			drm_dbg_kms(display->drm,
537 				    "Connector %s (pin %i) received hotplug event. (retry %d)\n",
538 				    connector->base.name, pin,
539 				    connector->hotplug_retries);
540 
541 			switch (encoder->hotplug(encoder, connector)) {
542 			case INTEL_HOTPLUG_UNCHANGED:
543 				break;
544 			case INTEL_HOTPLUG_CHANGED:
545 				changed |= hpd_bit;
546 				changed_connectors++;
547 				if (!first_changed_connector) {
548 					drm_connector_get(&connector->base);
549 					first_changed_connector = &connector->base;
550 				}
551 				break;
552 			case INTEL_HOTPLUG_RETRY:
553 				retry |= hpd_bit;
554 				break;
555 			}
556 		}
557 	}
558 	drm_connector_list_iter_end(&conn_iter);
559 	mutex_unlock(&display->drm->mode_config.mutex);
560 
561 	if (changed_connectors == 1)
562 		drm_kms_helper_connector_hotplug_event(first_changed_connector);
563 	else if (changed_connectors > 0)
564 		drm_kms_helper_hotplug_event(display->drm);
565 
566 	if (first_changed_connector)
567 		drm_connector_put(first_changed_connector);
568 
569 	/* Remove shared HPD pins that have changed */
570 	retry &= ~changed;
571 	if (retry) {
572 		spin_lock_irq(&dev_priv->irq_lock);
573 		display->hotplug.retry_bits |= retry;
574 
575 		mod_delayed_detection_work(display,
576 					   &display->hotplug.hotplug_work,
577 					   msecs_to_jiffies(HPD_RETRY_DELAY));
578 		spin_unlock_irq(&dev_priv->irq_lock);
579 	}
580 }
581 
582 
583 /**
584  * intel_hpd_irq_handler - main hotplug irq handler
585  * @display: display device
586  * @pin_mask: a mask of hpd pins that have triggered the irq
587  * @long_mask: a mask of hpd pins that may be long hpd pulses
588  *
589  * This is the main hotplug irq handler for all platforms. The platform specific
590  * irq handlers call the platform specific hotplug irq handlers, which read and
591  * decode the appropriate registers into bitmasks about hpd pins that have
592  * triggered (@pin_mask), and which of those pins may be long pulses
593  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
594  * is not a digital port.
595  *
596  * Here, we do hotplug irq storm detection and mitigation, and pass further
597  * processing to appropriate bottom halves.
598  */
599 void intel_hpd_irq_handler(struct intel_display *display,
600 			   u32 pin_mask, u32 long_mask)
601 {
602 	struct drm_i915_private *dev_priv = to_i915(display->drm);
603 	struct intel_encoder *encoder;
604 	bool storm_detected = false;
605 	bool queue_dig = false, queue_hp = false;
606 	u32 long_hpd_pulse_mask = 0;
607 	u32 short_hpd_pulse_mask = 0;
608 	enum hpd_pin pin;
609 
610 	if (!pin_mask)
611 		return;
612 
613 	spin_lock(&dev_priv->irq_lock);
614 
615 	/*
616 	 * Determine whether ->hpd_pulse() exists for each pin, and
617 	 * whether we have a short or a long pulse. This is needed
618 	 * as each pin may have up to two encoders (HDMI and DP) and
619 	 * only the one of them (DP) will have ->hpd_pulse().
620 	 */
621 	for_each_intel_encoder(display->drm, encoder) {
622 		bool long_hpd;
623 
624 		pin = encoder->hpd_pin;
625 		if (!(BIT(pin) & pin_mask))
626 			continue;
627 
628 		if (!intel_encoder_has_hpd_pulse(encoder))
629 			continue;
630 
631 		long_hpd = long_mask & BIT(pin);
632 
633 		drm_dbg(display->drm,
634 			"digital hpd on [ENCODER:%d:%s] - %s\n",
635 			encoder->base.base.id, encoder->base.name,
636 			long_hpd ? "long" : "short");
637 
638 		if (!hpd_pin_is_blocked(display, pin))
639 			queue_dig = true;
640 
641 		if (long_hpd) {
642 			long_hpd_pulse_mask |= BIT(pin);
643 			display->hotplug.long_hpd_pin_mask |= BIT(pin);
644 		} else {
645 			short_hpd_pulse_mask |= BIT(pin);
646 			display->hotplug.short_hpd_pin_mask |= BIT(pin);
647 		}
648 	}
649 
650 	/* Now process each pin just once */
651 	for_each_hpd_pin(pin) {
652 		bool long_hpd;
653 
654 		if (!(BIT(pin) & pin_mask))
655 			continue;
656 
657 		if (display->hotplug.stats[pin].state == HPD_DISABLED) {
658 			/*
659 			 * On GMCH platforms the interrupt mask bits only
660 			 * prevent irq generation, not the setting of the
661 			 * hotplug bits itself. So only WARN about unexpected
662 			 * interrupts on saner platforms.
663 			 */
664 			drm_WARN_ONCE(display->drm, !HAS_GMCH(display),
665 				      "Received HPD interrupt on pin %d although disabled\n",
666 				      pin);
667 			continue;
668 		}
669 
670 		if (display->hotplug.stats[pin].state != HPD_ENABLED)
671 			continue;
672 
673 		/*
674 		 * Delegate to ->hpd_pulse() if one of the encoders for this
675 		 * pin has it, otherwise let the hotplug_work deal with this
676 		 * pin directly.
677 		 */
678 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
679 			long_hpd = long_hpd_pulse_mask & BIT(pin);
680 		} else {
681 			display->hotplug.event_bits |= BIT(pin);
682 			long_hpd = true;
683 
684 			if (!hpd_pin_is_blocked(display, pin))
685 				queue_hp = true;
686 		}
687 
688 		if (intel_hpd_irq_storm_detect(display, pin, long_hpd)) {
689 			display->hotplug.event_bits &= ~BIT(pin);
690 			storm_detected = true;
691 			queue_hp = true;
692 		}
693 	}
694 
695 	/*
696 	 * Disable any IRQs that storms were detected on. Polling enablement
697 	 * happens later in our hotplug work.
698 	 */
699 	if (storm_detected)
700 		intel_hpd_irq_setup(display);
701 
702 	/*
703 	 * Our hotplug handler can grab modeset locks (by calling down into the
704 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
705 	 * queue for otherwise the flush_work in the pageflip code will
706 	 * deadlock.
707 	 */
708 	if (queue_dig)
709 		queue_work(display->hotplug.dp_wq, &display->hotplug.dig_port_work);
710 	if (queue_hp)
711 		queue_delayed_detection_work(display,
712 					     &display->hotplug.hotplug_work, 0);
713 
714 	spin_unlock(&dev_priv->irq_lock);
715 }
716 
717 /**
718  * intel_hpd_init - initializes and enables hpd support
719  * @display: display device instance
720  *
721  * This function enables the hotplug support. It requires that interrupts have
722  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
723  * poll request can run concurrently to other code, so locking rules must be
724  * obeyed.
725  *
726  * This is a separate step from interrupt enabling to simplify the locking rules
727  * in the driver load and resume code.
728  *
729  * Also see: intel_hpd_poll_enable() and intel_hpd_poll_disable().
730  */
731 void intel_hpd_init(struct intel_display *display)
732 {
733 	struct drm_i915_private *dev_priv = to_i915(display->drm);
734 	int i;
735 
736 	if (!HAS_DISPLAY(display))
737 		return;
738 
739 	for_each_hpd_pin(i) {
740 		display->hotplug.stats[i].count = 0;
741 		display->hotplug.stats[i].state = HPD_ENABLED;
742 	}
743 
744 	/*
745 	 * Interrupt setup is already guaranteed to be single-threaded, this is
746 	 * just to make the assert_spin_locked checks happy.
747 	 */
748 	spin_lock_irq(&dev_priv->irq_lock);
749 	intel_hpd_irq_setup(display);
750 	spin_unlock_irq(&dev_priv->irq_lock);
751 }
752 
753 static void i915_hpd_poll_detect_connectors(struct intel_display *display)
754 {
755 	struct drm_connector_list_iter conn_iter;
756 	struct intel_connector *connector;
757 	struct intel_connector *first_changed_connector = NULL;
758 	int changed = 0;
759 
760 	mutex_lock(&display->drm->mode_config.mutex);
761 
762 	if (!display->drm->mode_config.poll_enabled)
763 		goto out;
764 
765 	drm_connector_list_iter_begin(display->drm, &conn_iter);
766 	for_each_intel_connector_iter(connector, &conn_iter) {
767 		if (!(connector->base.polled & DRM_CONNECTOR_POLL_HPD))
768 			continue;
769 
770 		if (intel_hotplug_detect_connector(connector) != INTEL_HOTPLUG_CHANGED)
771 			continue;
772 
773 		changed++;
774 
775 		if (changed == 1) {
776 			drm_connector_get(&connector->base);
777 			first_changed_connector = connector;
778 		}
779 	}
780 	drm_connector_list_iter_end(&conn_iter);
781 
782 out:
783 	mutex_unlock(&display->drm->mode_config.mutex);
784 
785 	if (!changed)
786 		return;
787 
788 	if (changed == 1)
789 		drm_kms_helper_connector_hotplug_event(&first_changed_connector->base);
790 	else
791 		drm_kms_helper_hotplug_event(display->drm);
792 
793 	drm_connector_put(&first_changed_connector->base);
794 }
795 
796 static void i915_hpd_poll_init_work(struct work_struct *work)
797 {
798 	struct intel_display *display =
799 		container_of(work, typeof(*display), hotplug.poll_init_work);
800 	struct drm_i915_private *dev_priv = to_i915(display->drm);
801 	struct drm_connector_list_iter conn_iter;
802 	struct intel_connector *connector;
803 	intel_wakeref_t wakeref;
804 	bool enabled;
805 
806 	mutex_lock(&display->drm->mode_config.mutex);
807 
808 	enabled = READ_ONCE(display->hotplug.poll_enabled);
809 	/*
810 	 * Prevent taking a power reference from this sequence of
811 	 * i915_hpd_poll_init_work() -> drm_helper_hpd_irq_event() ->
812 	 * connector detect which would requeue i915_hpd_poll_init_work()
813 	 * and so risk an endless loop of this same sequence.
814 	 */
815 	if (!enabled) {
816 		wakeref = intel_display_power_get(display,
817 						  POWER_DOMAIN_DISPLAY_CORE);
818 		drm_WARN_ON(display->drm,
819 			    READ_ONCE(display->hotplug.poll_enabled));
820 		cancel_work(&display->hotplug.poll_init_work);
821 	}
822 
823 	spin_lock_irq(&dev_priv->irq_lock);
824 
825 	drm_connector_list_iter_begin(display->drm, &conn_iter);
826 	for_each_intel_connector_iter(connector, &conn_iter) {
827 		enum hpd_pin pin;
828 
829 		pin = intel_connector_hpd_pin(connector);
830 		if (pin == HPD_NONE)
831 			continue;
832 
833 		if (display->hotplug.stats[pin].state == HPD_DISABLED)
834 			continue;
835 
836 		connector->base.polled = connector->polled;
837 
838 		if (enabled && connector->base.polled == DRM_CONNECTOR_POLL_HPD)
839 			connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
840 				DRM_CONNECTOR_POLL_DISCONNECT;
841 	}
842 	drm_connector_list_iter_end(&conn_iter);
843 
844 	spin_unlock_irq(&dev_priv->irq_lock);
845 
846 	if (enabled)
847 		drm_kms_helper_poll_reschedule(display->drm);
848 
849 	mutex_unlock(&display->drm->mode_config.mutex);
850 
851 	/*
852 	 * We might have missed any hotplugs that happened while we were
853 	 * in the middle of disabling polling
854 	 */
855 	if (!enabled) {
856 		i915_hpd_poll_detect_connectors(display);
857 
858 		intel_display_power_put(display,
859 					POWER_DOMAIN_DISPLAY_CORE,
860 					wakeref);
861 	}
862 }
863 
864 /**
865  * intel_hpd_poll_enable - enable polling for connectors with hpd
866  * @display: display device instance
867  *
868  * This function enables polling for all connectors which support HPD.
869  * Under certain conditions HPD may not be functional. On most Intel GPUs,
870  * this happens when we enter runtime suspend.
871  * On Valleyview and Cherryview systems, this also happens when we shut off all
872  * of the powerwells.
873  *
874  * Since this function can get called in contexts where we're already holding
875  * dev->mode_config.mutex, we do the actual hotplug enabling in a separate
876  * worker.
877  *
878  * Also see: intel_hpd_init() and intel_hpd_poll_disable().
879  */
880 void intel_hpd_poll_enable(struct intel_display *display)
881 {
882 	struct drm_i915_private *dev_priv = to_i915(display->drm);
883 
884 	if (!HAS_DISPLAY(display) || !intel_display_device_enabled(display))
885 		return;
886 
887 	WRITE_ONCE(display->hotplug.poll_enabled, true);
888 
889 	/*
890 	 * We might already be holding dev->mode_config.mutex, so do this in a
891 	 * separate worker
892 	 * As well, there's no issue if we race here since we always reschedule
893 	 * this worker anyway
894 	 */
895 	spin_lock_irq(&dev_priv->irq_lock);
896 	queue_detection_work(display,
897 			     &display->hotplug.poll_init_work);
898 	spin_unlock_irq(&dev_priv->irq_lock);
899 }
900 
901 /**
902  * intel_hpd_poll_disable - disable polling for connectors with hpd
903  * @display: display device instance
904  *
905  * This function disables polling for all connectors which support HPD.
906  * Under certain conditions HPD may not be functional. On most Intel GPUs,
907  * this happens when we enter runtime suspend.
908  * On Valleyview and Cherryview systems, this also happens when we shut off all
909  * of the powerwells.
910  *
911  * Since this function can get called in contexts where we're already holding
912  * dev->mode_config.mutex, we do the actual hotplug enabling in a separate
913  * worker.
914  *
915  * Also used during driver init to initialize connector->polled
916  * appropriately for all connectors.
917  *
918  * Also see: intel_hpd_init() and intel_hpd_poll_enable().
919  */
920 void intel_hpd_poll_disable(struct intel_display *display)
921 {
922 	struct drm_i915_private *dev_priv = to_i915(display->drm);
923 
924 	if (!HAS_DISPLAY(display))
925 		return;
926 
927 	WRITE_ONCE(display->hotplug.poll_enabled, false);
928 
929 	spin_lock_irq(&dev_priv->irq_lock);
930 	queue_detection_work(display,
931 			     &display->hotplug.poll_init_work);
932 	spin_unlock_irq(&dev_priv->irq_lock);
933 }
934 
935 void intel_hpd_poll_fini(struct intel_display *display)
936 {
937 	struct intel_connector *connector;
938 	struct drm_connector_list_iter conn_iter;
939 
940 	/* Kill all the work that may have been queued by hpd. */
941 	drm_connector_list_iter_begin(display->drm, &conn_iter);
942 	for_each_intel_connector_iter(connector, &conn_iter) {
943 		intel_connector_cancel_modeset_retry_work(connector);
944 		intel_hdcp_cancel_works(connector);
945 	}
946 	drm_connector_list_iter_end(&conn_iter);
947 }
948 
949 void intel_hpd_init_early(struct intel_display *display)
950 {
951 	INIT_DELAYED_WORK(&display->hotplug.hotplug_work,
952 			  i915_hotplug_work_func);
953 	INIT_WORK(&display->hotplug.dig_port_work, i915_digport_work_func);
954 	INIT_WORK(&display->hotplug.poll_init_work, i915_hpd_poll_init_work);
955 	INIT_DELAYED_WORK(&display->hotplug.reenable_work,
956 			  intel_hpd_irq_storm_reenable_work);
957 
958 	display->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
959 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
960 	 * detection, as short HPD storms will occur as a natural part of
961 	 * sideband messaging with MST.
962 	 * On older platforms however, IRQ storms can occur with both long and
963 	 * short pulses, as seen on some G4x systems.
964 	 */
965 	display->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(display);
966 }
967 
968 static bool cancel_all_detection_work(struct intel_display *display)
969 {
970 	bool was_pending = false;
971 
972 	if (cancel_delayed_work_sync(&display->hotplug.hotplug_work))
973 		was_pending = true;
974 	if (cancel_work_sync(&display->hotplug.poll_init_work))
975 		was_pending = true;
976 	if (cancel_delayed_work_sync(&display->hotplug.reenable_work))
977 		was_pending = true;
978 
979 	return was_pending;
980 }
981 
982 void intel_hpd_cancel_work(struct intel_display *display)
983 {
984 	struct drm_i915_private *dev_priv = to_i915(display->drm);
985 
986 	if (!HAS_DISPLAY(display))
987 		return;
988 
989 	spin_lock_irq(&dev_priv->irq_lock);
990 
991 	drm_WARN_ON(display->drm, get_blocked_hpd_pin_mask(display));
992 
993 	display->hotplug.long_hpd_pin_mask = 0;
994 	display->hotplug.short_hpd_pin_mask = 0;
995 	display->hotplug.event_bits = 0;
996 	display->hotplug.retry_bits = 0;
997 
998 	spin_unlock_irq(&dev_priv->irq_lock);
999 
1000 	cancel_work_sync(&display->hotplug.dig_port_work);
1001 
1002 	/*
1003 	 * All other work triggered by hotplug events should be canceled by
1004 	 * now.
1005 	 */
1006 	if (cancel_all_detection_work(display))
1007 		drm_dbg_kms(display->drm, "Hotplug detection work still active\n");
1008 }
1009 
1010 static void queue_work_for_missed_irqs(struct intel_display *display)
1011 {
1012 	struct drm_i915_private *i915 = to_i915(display->drm);
1013 	struct intel_hotplug *hotplug = &display->hotplug;
1014 	bool queue_hp_work = false;
1015 	u32 blocked_hpd_pin_mask;
1016 	enum hpd_pin pin;
1017 
1018 	lockdep_assert_held(&i915->irq_lock);
1019 
1020 	blocked_hpd_pin_mask = get_blocked_hpd_pin_mask(display);
1021 	if ((hotplug->event_bits | hotplug->retry_bits) & ~blocked_hpd_pin_mask)
1022 		queue_hp_work = true;
1023 
1024 	for_each_hpd_pin(pin) {
1025 		switch (display->hotplug.stats[pin].state) {
1026 		case HPD_MARK_DISABLED:
1027 			queue_hp_work = true;
1028 			break;
1029 		case HPD_DISABLED:
1030 		case HPD_ENABLED:
1031 			break;
1032 		default:
1033 			MISSING_CASE(display->hotplug.stats[pin].state);
1034 		}
1035 	}
1036 
1037 	if ((hotplug->long_hpd_pin_mask | hotplug->short_hpd_pin_mask) & ~blocked_hpd_pin_mask)
1038 		queue_work(hotplug->dp_wq, &hotplug->dig_port_work);
1039 
1040 	if (queue_hp_work)
1041 		queue_delayed_detection_work(display, &display->hotplug.hotplug_work, 0);
1042 }
1043 
1044 static bool block_hpd_pin(struct intel_display *display, enum hpd_pin pin)
1045 {
1046 	struct drm_i915_private *i915 = to_i915(display->drm);
1047 	struct intel_hotplug *hotplug = &display->hotplug;
1048 
1049 	lockdep_assert_held(&i915->irq_lock);
1050 
1051 	hotplug->stats[pin].blocked_count++;
1052 
1053 	return hotplug->stats[pin].blocked_count == 1;
1054 }
1055 
1056 static bool unblock_hpd_pin(struct intel_display *display, enum hpd_pin pin)
1057 {
1058 	struct drm_i915_private *i915 = to_i915(display->drm);
1059 	struct intel_hotplug *hotplug = &display->hotplug;
1060 
1061 	lockdep_assert_held(&i915->irq_lock);
1062 
1063 	if (drm_WARN_ON(display->drm, hotplug->stats[pin].blocked_count == 0))
1064 		return true;
1065 
1066 	hotplug->stats[pin].blocked_count--;
1067 
1068 	return hotplug->stats[pin].blocked_count == 0;
1069 }
1070 
1071 /**
1072  * intel_hpd_block - Block handling of HPD IRQs on an HPD pin
1073  * @encoder: Encoder to block the HPD handling for
1074  *
1075  * Blocks the handling of HPD IRQs on the HPD pin of @encoder.
1076  *
1077  * On return:
1078  *
1079  * - It's guaranteed that the blocked encoders' HPD pulse handler
1080  *   (via intel_digital_port::hpd_pulse()) is not running.
1081  * - The hotplug event handling (via intel_encoder::hotplug()) of an
1082  *   HPD IRQ pending at the time this function is called may be still
1083  *   running.
1084  * - Detection on the encoder's connector (via
1085  *   drm_connector_helper_funcs::detect_ctx(),
1086  *   drm_connector_funcs::detect()) remains allowed, for instance as part of
1087  *   userspace connector probing, or DRM core's connector polling.
1088  *
1089  * The call must be followed by calling intel_hpd_unblock(), or
1090  * intel_hpd_clear_and_unblock().
1091  *
1092  * Note that the handling of HPD IRQs for another encoder using the same HPD
1093  * pin as that of @encoder will be also blocked.
1094  */
1095 void intel_hpd_block(struct intel_encoder *encoder)
1096 {
1097 	struct intel_display *display = to_intel_display(encoder);
1098 	struct drm_i915_private *i915 = to_i915(display->drm);
1099 	struct intel_hotplug *hotplug = &display->hotplug;
1100 	bool do_flush = false;
1101 
1102 	if (encoder->hpd_pin == HPD_NONE)
1103 		return;
1104 
1105 	spin_lock_irq(&i915->irq_lock);
1106 
1107 	if (block_hpd_pin(display, encoder->hpd_pin))
1108 		do_flush = true;
1109 
1110 	spin_unlock_irq(&i915->irq_lock);
1111 
1112 	if (do_flush && hpd_pin_has_pulse(display, encoder->hpd_pin))
1113 		flush_work(&hotplug->dig_port_work);
1114 }
1115 
1116 /**
1117  * intel_hpd_unblock - Unblock handling of HPD IRQs on an HPD pin
1118  * @encoder: Encoder to unblock the HPD handling for
1119  *
1120  * Unblock the handling of HPD IRQs on the HPD pin of @encoder, which was
1121  * previously blocked by intel_hpd_block(). Any HPD IRQ raised on the
1122  * HPD pin while it was blocked will be handled for @encoder and for any
1123  * other encoder sharing the same HPD pin.
1124  */
1125 void intel_hpd_unblock(struct intel_encoder *encoder)
1126 {
1127 	struct intel_display *display = to_intel_display(encoder);
1128 	struct drm_i915_private *i915 = to_i915(display->drm);
1129 
1130 	if (encoder->hpd_pin == HPD_NONE)
1131 		return;
1132 
1133 	spin_lock_irq(&i915->irq_lock);
1134 
1135 	if (unblock_hpd_pin(display, encoder->hpd_pin))
1136 		queue_work_for_missed_irqs(display);
1137 
1138 	spin_unlock_irq(&i915->irq_lock);
1139 }
1140 
1141 /**
1142  * intel_hpd_clear_and_unblock - Unblock handling of new HPD IRQs on an HPD pin
1143  * @encoder: Encoder to unblock the HPD handling for
1144  *
1145  * Unblock the handling of HPD IRQs on the HPD pin of @encoder, which was
1146  * previously blocked by intel_hpd_block(). Any HPD IRQ raised on the
1147  * HPD pin while it was blocked will be cleared, handling only new IRQs.
1148  */
1149 void intel_hpd_clear_and_unblock(struct intel_encoder *encoder)
1150 {
1151 	struct intel_display *display = to_intel_display(encoder);
1152 	struct drm_i915_private *i915 = to_i915(display->drm);
1153 	struct intel_hotplug *hotplug = &display->hotplug;
1154 	enum hpd_pin pin = encoder->hpd_pin;
1155 
1156 	if (pin == HPD_NONE)
1157 		return;
1158 
1159 	spin_lock_irq(&i915->irq_lock);
1160 
1161 	if (unblock_hpd_pin(display, pin)) {
1162 		hotplug->event_bits &= ~BIT(pin);
1163 		hotplug->retry_bits &= ~BIT(pin);
1164 		hotplug->short_hpd_pin_mask &= ~BIT(pin);
1165 		hotplug->long_hpd_pin_mask &= ~BIT(pin);
1166 	}
1167 
1168 	spin_unlock_irq(&i915->irq_lock);
1169 }
1170 
1171 void intel_hpd_enable_detection_work(struct intel_display *display)
1172 {
1173 	struct drm_i915_private *i915 = to_i915(display->drm);
1174 
1175 	spin_lock_irq(&i915->irq_lock);
1176 	display->hotplug.detection_work_enabled = true;
1177 	queue_work_for_missed_irqs(display);
1178 	spin_unlock_irq(&i915->irq_lock);
1179 }
1180 
1181 void intel_hpd_disable_detection_work(struct intel_display *display)
1182 {
1183 	struct drm_i915_private *i915 = to_i915(display->drm);
1184 
1185 	spin_lock_irq(&i915->irq_lock);
1186 	display->hotplug.detection_work_enabled = false;
1187 	spin_unlock_irq(&i915->irq_lock);
1188 
1189 	cancel_all_detection_work(display);
1190 }
1191 
1192 bool intel_hpd_schedule_detection(struct intel_display *display)
1193 {
1194 	struct drm_i915_private *i915 = to_i915(display->drm);
1195 	unsigned long flags;
1196 	bool ret;
1197 
1198 	spin_lock_irqsave(&i915->irq_lock, flags);
1199 	ret = queue_delayed_detection_work(display, &display->hotplug.hotplug_work, 0);
1200 	spin_unlock_irqrestore(&i915->irq_lock, flags);
1201 
1202 	return ret;
1203 }
1204 
1205 static int i915_hpd_storm_ctl_show(struct seq_file *m, void *data)
1206 {
1207 	struct intel_display *display = m->private;
1208 	struct drm_i915_private *dev_priv = to_i915(display->drm);
1209 	struct intel_hotplug *hotplug = &display->hotplug;
1210 
1211 	/* Synchronize with everything first in case there's been an HPD
1212 	 * storm, but we haven't finished handling it in the kernel yet
1213 	 */
1214 	intel_synchronize_irq(dev_priv);
1215 	flush_work(&display->hotplug.dig_port_work);
1216 	flush_delayed_work(&display->hotplug.hotplug_work);
1217 
1218 	seq_printf(m, "Threshold: %d\n", hotplug->hpd_storm_threshold);
1219 	seq_printf(m, "Detected: %s\n",
1220 		   str_yes_no(delayed_work_pending(&hotplug->reenable_work)));
1221 
1222 	return 0;
1223 }
1224 
1225 static ssize_t i915_hpd_storm_ctl_write(struct file *file,
1226 					const char __user *ubuf, size_t len,
1227 					loff_t *offp)
1228 {
1229 	struct seq_file *m = file->private_data;
1230 	struct intel_display *display = m->private;
1231 	struct drm_i915_private *dev_priv = to_i915(display->drm);
1232 	struct intel_hotplug *hotplug = &display->hotplug;
1233 	unsigned int new_threshold;
1234 	int i;
1235 	char *newline;
1236 	char tmp[16];
1237 
1238 	if (len >= sizeof(tmp))
1239 		return -EINVAL;
1240 
1241 	if (copy_from_user(tmp, ubuf, len))
1242 		return -EFAULT;
1243 
1244 	tmp[len] = '\0';
1245 
1246 	/* Strip newline, if any */
1247 	newline = strchr(tmp, '\n');
1248 	if (newline)
1249 		*newline = '\0';
1250 
1251 	if (strcmp(tmp, "reset") == 0)
1252 		new_threshold = HPD_STORM_DEFAULT_THRESHOLD;
1253 	else if (kstrtouint(tmp, 10, &new_threshold) != 0)
1254 		return -EINVAL;
1255 
1256 	if (new_threshold > 0)
1257 		drm_dbg_kms(display->drm,
1258 			    "Setting HPD storm detection threshold to %d\n",
1259 			    new_threshold);
1260 	else
1261 		drm_dbg_kms(display->drm, "Disabling HPD storm detection\n");
1262 
1263 	spin_lock_irq(&dev_priv->irq_lock);
1264 	hotplug->hpd_storm_threshold = new_threshold;
1265 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
1266 	for_each_hpd_pin(i)
1267 		hotplug->stats[i].count = 0;
1268 	spin_unlock_irq(&dev_priv->irq_lock);
1269 
1270 	/* Re-enable hpd immediately if we were in an irq storm */
1271 	flush_delayed_work(&display->hotplug.reenable_work);
1272 
1273 	return len;
1274 }
1275 
1276 static int i915_hpd_storm_ctl_open(struct inode *inode, struct file *file)
1277 {
1278 	return single_open(file, i915_hpd_storm_ctl_show, inode->i_private);
1279 }
1280 
1281 static const struct file_operations i915_hpd_storm_ctl_fops = {
1282 	.owner = THIS_MODULE,
1283 	.open = i915_hpd_storm_ctl_open,
1284 	.read = seq_read,
1285 	.llseek = seq_lseek,
1286 	.release = single_release,
1287 	.write = i915_hpd_storm_ctl_write
1288 };
1289 
1290 static int i915_hpd_short_storm_ctl_show(struct seq_file *m, void *data)
1291 {
1292 	struct intel_display *display = m->private;
1293 
1294 	seq_printf(m, "Enabled: %s\n",
1295 		   str_yes_no(display->hotplug.hpd_short_storm_enabled));
1296 
1297 	return 0;
1298 }
1299 
1300 static int
1301 i915_hpd_short_storm_ctl_open(struct inode *inode, struct file *file)
1302 {
1303 	return single_open(file, i915_hpd_short_storm_ctl_show,
1304 			   inode->i_private);
1305 }
1306 
1307 static ssize_t i915_hpd_short_storm_ctl_write(struct file *file,
1308 					      const char __user *ubuf,
1309 					      size_t len, loff_t *offp)
1310 {
1311 	struct seq_file *m = file->private_data;
1312 	struct intel_display *display = m->private;
1313 	struct drm_i915_private *dev_priv = to_i915(display->drm);
1314 	struct intel_hotplug *hotplug = &display->hotplug;
1315 	char *newline;
1316 	char tmp[16];
1317 	int i;
1318 	bool new_state;
1319 
1320 	if (len >= sizeof(tmp))
1321 		return -EINVAL;
1322 
1323 	if (copy_from_user(tmp, ubuf, len))
1324 		return -EFAULT;
1325 
1326 	tmp[len] = '\0';
1327 
1328 	/* Strip newline, if any */
1329 	newline = strchr(tmp, '\n');
1330 	if (newline)
1331 		*newline = '\0';
1332 
1333 	/* Reset to the "default" state for this system */
1334 	if (strcmp(tmp, "reset") == 0)
1335 		new_state = !HAS_DP_MST(display);
1336 	else if (kstrtobool(tmp, &new_state) != 0)
1337 		return -EINVAL;
1338 
1339 	drm_dbg_kms(display->drm, "%sabling HPD short storm detection\n",
1340 		    new_state ? "En" : "Dis");
1341 
1342 	spin_lock_irq(&dev_priv->irq_lock);
1343 	hotplug->hpd_short_storm_enabled = new_state;
1344 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
1345 	for_each_hpd_pin(i)
1346 		hotplug->stats[i].count = 0;
1347 	spin_unlock_irq(&dev_priv->irq_lock);
1348 
1349 	/* Re-enable hpd immediately if we were in an irq storm */
1350 	flush_delayed_work(&display->hotplug.reenable_work);
1351 
1352 	return len;
1353 }
1354 
1355 static const struct file_operations i915_hpd_short_storm_ctl_fops = {
1356 	.owner = THIS_MODULE,
1357 	.open = i915_hpd_short_storm_ctl_open,
1358 	.read = seq_read,
1359 	.llseek = seq_lseek,
1360 	.release = single_release,
1361 	.write = i915_hpd_short_storm_ctl_write,
1362 };
1363 
1364 void intel_hpd_debugfs_register(struct intel_display *display)
1365 {
1366 	struct drm_minor *minor = display->drm->primary;
1367 
1368 	debugfs_create_file("i915_hpd_storm_ctl", 0644, minor->debugfs_root,
1369 			    display, &i915_hpd_storm_ctl_fops);
1370 	debugfs_create_file("i915_hpd_short_storm_ctl", 0644, minor->debugfs_root,
1371 			    display, &i915_hpd_short_storm_ctl_fops);
1372 	debugfs_create_bool("i915_ignore_long_hpd", 0644, minor->debugfs_root,
1373 			    &display->hotplug.ignore_long_hpd);
1374 }
1375