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 "i915_drv.h"
27 #include "i915_irq.h"
28 #include "intel_display_types.h"
29 #include "intel_hotplug.h"
30 
31 /**
32  * DOC: Hotplug
33  *
34  * Simply put, hotplug occurs when a display is connected to or disconnected
35  * from the system. However, there may be adapters and docking stations and
36  * Display Port short pulses and MST devices involved, complicating matters.
37  *
38  * Hotplug in i915 is handled in many different levels of abstraction.
39  *
40  * The platform dependent interrupt handling code in i915_irq.c enables,
41  * disables, and does preliminary handling of the interrupts. The interrupt
42  * handlers gather the hotplug detect (HPD) information from relevant registers
43  * into a platform independent mask of hotplug pins that have fired.
44  *
45  * The platform independent interrupt handler intel_hpd_irq_handler() in
46  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
47  * further processing to appropriate bottom halves (Display Port specific and
48  * regular hotplug).
49  *
50  * The Display Port work function i915_digport_work_func() calls into
51  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
52  * pulses, with failures and non-MST long pulses triggering regular hotplug
53  * processing on the connector.
54  *
55  * The regular hotplug work function i915_hotplug_work_func() calls connector
56  * detect hooks, and, if connector status changes, triggers sending of hotplug
57  * uevent to userspace via drm_kms_helper_hotplug_event().
58  *
59  * Finally, the userspace is responsible for triggering a modeset upon receiving
60  * the hotplug uevent, disabling or enabling the crtc as needed.
61  *
62  * The hotplug interrupt storm detection and mitigation code keeps track of the
63  * number of interrupts per hotplug pin per a period of time, and if the number
64  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
65  * while before being re-enabled. The intention is to mitigate issues raising
66  * from broken hardware triggering massive amounts of interrupts and grinding
67  * the system to a halt.
68  *
69  * Current implementation expects that hotplug interrupt storm will not be
70  * seen when display port sink is connected, hence on platforms whose DP
71  * callback is handled by i915_digport_work_func reenabling of hpd is not
72  * performed (it was never expected to be disabled in the first place ;) )
73  * this is specific to DP sinks handled by this routine and any other display
74  * such as HDMI or DVI enabled on the same port will have proper logic since
75  * it will use i915_hotplug_work_func where this logic is handled.
76  */
77 
78 /**
79  * intel_hpd_pin_default - return default pin associated with certain port.
80  * @dev_priv: private driver data pointer
81  * @port: the hpd port to get associated pin
82  *
83  * It is only valid and used by digital port encoder.
84  *
85  * Return pin that is associatade with @port.
86  */
87 enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
88 				   enum port port)
89 {
90 	return HPD_PORT_A + port - PORT_A;
91 }
92 
93 /* Threshold == 5 for long IRQs, 50 for short */
94 #define HPD_STORM_DEFAULT_THRESHOLD	50
95 
96 #define HPD_STORM_DETECT_PERIOD		1000
97 #define HPD_STORM_REENABLE_DELAY	(2 * 60 * 1000)
98 #define HPD_RETRY_DELAY			1000
99 
100 static enum hpd_pin
101 intel_connector_hpd_pin(struct intel_connector *connector)
102 {
103 	struct intel_encoder *encoder = intel_attached_encoder(connector);
104 
105 	/*
106 	 * MST connectors get their encoder attached dynamically
107 	 * so need to make sure we have an encoder here. But since
108 	 * MST encoders have their hpd_pin set to HPD_NONE we don't
109 	 * have to special case them beyond that.
110 	 */
111 	return encoder ? encoder->hpd_pin : HPD_NONE;
112 }
113 
114 /**
115  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
116  * @dev_priv: private driver data pointer
117  * @pin: the pin to gather stats on
118  * @long_hpd: whether the HPD IRQ was long or short
119  *
120  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
121  * storms. Only the pin specific stats and state are changed, the caller is
122  * responsible for further action.
123  *
124  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
125  * stored in @dev_priv->display.hotplug.hpd_storm_threshold which defaults to
126  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
127  * short IRQs count as +1. If this threshold is exceeded, it's considered an
128  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
129  *
130  * By default, most systems will only count long IRQs towards
131  * &dev_priv->display.hotplug.hpd_storm_threshold. However, some older systems also
132  * suffer from short IRQ storms and must also track these. Because short IRQ
133  * storms are naturally caused by sideband interactions with DP MST devices,
134  * short IRQ detection is only enabled for systems without DP MST support.
135  * Systems which are new enough to support DP MST are far less likely to
136  * suffer from IRQ storms at all, so this is fine.
137  *
138  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
139  * and should only be adjusted for automated hotplug testing.
140  *
141  * Return true if an IRQ storm was detected on @pin.
142  */
143 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
144 				       enum hpd_pin pin, bool long_hpd)
145 {
146 	struct intel_hotplug *hpd = &dev_priv->display.hotplug;
147 	unsigned long start = hpd->stats[pin].last_jiffies;
148 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
149 	const int increment = long_hpd ? 10 : 1;
150 	const int threshold = hpd->hpd_storm_threshold;
151 	bool storm = false;
152 
153 	if (!threshold ||
154 	    (!long_hpd && !dev_priv->display.hotplug.hpd_short_storm_enabled))
155 		return false;
156 
157 	if (!time_in_range(jiffies, start, end)) {
158 		hpd->stats[pin].last_jiffies = jiffies;
159 		hpd->stats[pin].count = 0;
160 	}
161 
162 	hpd->stats[pin].count += increment;
163 	if (hpd->stats[pin].count > threshold) {
164 		hpd->stats[pin].state = HPD_MARK_DISABLED;
165 		drm_dbg_kms(&dev_priv->drm,
166 			    "HPD interrupt storm detected on PIN %d\n", pin);
167 		storm = true;
168 	} else {
169 		drm_dbg_kms(&dev_priv->drm,
170 			    "Received HPD interrupt on PIN %d - cnt: %d\n",
171 			      pin,
172 			      hpd->stats[pin].count);
173 	}
174 
175 	return storm;
176 }
177 
178 static void
179 intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
180 {
181 	struct drm_connector_list_iter conn_iter;
182 	struct intel_connector *connector;
183 	bool hpd_disabled = false;
184 
185 	lockdep_assert_held(&dev_priv->irq_lock);
186 
187 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
188 	for_each_intel_connector_iter(connector, &conn_iter) {
189 		enum hpd_pin pin;
190 
191 		if (connector->base.polled != DRM_CONNECTOR_POLL_HPD)
192 			continue;
193 
194 		pin = intel_connector_hpd_pin(connector);
195 		if (pin == HPD_NONE ||
196 		    dev_priv->display.hotplug.stats[pin].state != HPD_MARK_DISABLED)
197 			continue;
198 
199 		drm_info(&dev_priv->drm,
200 			 "HPD interrupt storm detected on connector %s: "
201 			 "switching from hotplug detection to polling\n",
202 			 connector->base.name);
203 
204 		dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
205 		connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
206 			DRM_CONNECTOR_POLL_DISCONNECT;
207 		hpd_disabled = true;
208 	}
209 	drm_connector_list_iter_end(&conn_iter);
210 
211 	/* Enable polling and queue hotplug re-enabling. */
212 	if (hpd_disabled) {
213 		drm_kms_helper_poll_enable(&dev_priv->drm);
214 		mod_delayed_work(system_wq, &dev_priv->display.hotplug.reenable_work,
215 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
216 	}
217 }
218 
219 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
220 {
221 	struct drm_i915_private *dev_priv =
222 		container_of(work, typeof(*dev_priv),
223 			     display.hotplug.reenable_work.work);
224 	struct drm_connector_list_iter conn_iter;
225 	struct intel_connector *connector;
226 	intel_wakeref_t wakeref;
227 	enum hpd_pin pin;
228 
229 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
230 
231 	spin_lock_irq(&dev_priv->irq_lock);
232 
233 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
234 	for_each_intel_connector_iter(connector, &conn_iter) {
235 		pin = intel_connector_hpd_pin(connector);
236 		if (pin == HPD_NONE ||
237 		    dev_priv->display.hotplug.stats[pin].state != HPD_DISABLED)
238 			continue;
239 
240 		if (connector->base.polled != connector->polled)
241 			drm_dbg(&dev_priv->drm,
242 				"Reenabling HPD on connector %s\n",
243 				connector->base.name);
244 		connector->base.polled = connector->polled;
245 	}
246 	drm_connector_list_iter_end(&conn_iter);
247 
248 	for_each_hpd_pin(pin) {
249 		if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED)
250 			dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
251 	}
252 
253 	intel_hpd_irq_setup(dev_priv);
254 
255 	spin_unlock_irq(&dev_priv->irq_lock);
256 
257 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
258 }
259 
260 enum intel_hotplug_state
261 intel_encoder_hotplug(struct intel_encoder *encoder,
262 		      struct intel_connector *connector)
263 {
264 	struct drm_device *dev = connector->base.dev;
265 	enum drm_connector_status old_status;
266 	u64 old_epoch_counter;
267 	bool ret = false;
268 
269 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
270 	old_status = connector->base.status;
271 	old_epoch_counter = connector->base.epoch_counter;
272 
273 	connector->base.status =
274 		drm_helper_probe_detect(&connector->base, NULL, false);
275 
276 	if (old_epoch_counter != connector->base.epoch_counter)
277 		ret = true;
278 
279 	if (ret) {
280 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s (epoch counter %llu->%llu)\n",
281 			    connector->base.base.id,
282 			    connector->base.name,
283 			    drm_get_connector_status_name(old_status),
284 			    drm_get_connector_status_name(connector->base.status),
285 			    old_epoch_counter,
286 			    connector->base.epoch_counter);
287 		return INTEL_HOTPLUG_CHANGED;
288 	}
289 	return INTEL_HOTPLUG_UNCHANGED;
290 }
291 
292 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
293 {
294 	return intel_encoder_is_dig_port(encoder) &&
295 		enc_to_dig_port(encoder)->hpd_pulse != NULL;
296 }
297 
298 static void i915_digport_work_func(struct work_struct *work)
299 {
300 	struct drm_i915_private *dev_priv =
301 		container_of(work, struct drm_i915_private, display.hotplug.dig_port_work);
302 	u32 long_port_mask, short_port_mask;
303 	struct intel_encoder *encoder;
304 	u32 old_bits = 0;
305 
306 	spin_lock_irq(&dev_priv->irq_lock);
307 	long_port_mask = dev_priv->display.hotplug.long_port_mask;
308 	dev_priv->display.hotplug.long_port_mask = 0;
309 	short_port_mask = dev_priv->display.hotplug.short_port_mask;
310 	dev_priv->display.hotplug.short_port_mask = 0;
311 	spin_unlock_irq(&dev_priv->irq_lock);
312 
313 	for_each_intel_encoder(&dev_priv->drm, encoder) {
314 		struct intel_digital_port *dig_port;
315 		enum port port = encoder->port;
316 		bool long_hpd, short_hpd;
317 		enum irqreturn ret;
318 
319 		if (!intel_encoder_has_hpd_pulse(encoder))
320 			continue;
321 
322 		long_hpd = long_port_mask & BIT(port);
323 		short_hpd = short_port_mask & BIT(port);
324 
325 		if (!long_hpd && !short_hpd)
326 			continue;
327 
328 		dig_port = enc_to_dig_port(encoder);
329 
330 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
331 		if (ret == IRQ_NONE) {
332 			/* fall back to old school hpd */
333 			old_bits |= BIT(encoder->hpd_pin);
334 		}
335 	}
336 
337 	if (old_bits) {
338 		spin_lock_irq(&dev_priv->irq_lock);
339 		dev_priv->display.hotplug.event_bits |= old_bits;
340 		spin_unlock_irq(&dev_priv->irq_lock);
341 		queue_delayed_work(system_wq, &dev_priv->display.hotplug.hotplug_work, 0);
342 	}
343 }
344 
345 /**
346  * intel_hpd_trigger_irq - trigger an hpd irq event for a port
347  * @dig_port: digital port
348  *
349  * Trigger an HPD interrupt event for the given port, emulating a short pulse
350  * generated by the sink, and schedule the dig port work to handle it.
351  */
352 void intel_hpd_trigger_irq(struct intel_digital_port *dig_port)
353 {
354 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
355 
356 	spin_lock_irq(&i915->irq_lock);
357 	i915->display.hotplug.short_port_mask |= BIT(dig_port->base.port);
358 	spin_unlock_irq(&i915->irq_lock);
359 
360 	queue_work(i915->display.hotplug.dp_wq, &i915->display.hotplug.dig_port_work);
361 }
362 
363 /*
364  * Handle hotplug events outside the interrupt handler proper.
365  */
366 static void i915_hotplug_work_func(struct work_struct *work)
367 {
368 	struct drm_i915_private *dev_priv =
369 		container_of(work, struct drm_i915_private,
370 			     display.hotplug.hotplug_work.work);
371 	struct drm_connector_list_iter conn_iter;
372 	struct intel_connector *connector;
373 	u32 changed = 0, retry = 0;
374 	u32 hpd_event_bits;
375 	u32 hpd_retry_bits;
376 
377 	mutex_lock(&dev_priv->drm.mode_config.mutex);
378 	drm_dbg_kms(&dev_priv->drm, "running encoder hotplug functions\n");
379 
380 	spin_lock_irq(&dev_priv->irq_lock);
381 
382 	hpd_event_bits = dev_priv->display.hotplug.event_bits;
383 	dev_priv->display.hotplug.event_bits = 0;
384 	hpd_retry_bits = dev_priv->display.hotplug.retry_bits;
385 	dev_priv->display.hotplug.retry_bits = 0;
386 
387 	/* Enable polling for connectors which had HPD IRQ storms */
388 	intel_hpd_irq_storm_switch_to_polling(dev_priv);
389 
390 	spin_unlock_irq(&dev_priv->irq_lock);
391 
392 	/* Skip calling encode hotplug handlers if ignore long HPD set*/
393 	if (dev_priv->display.hotplug.ignore_long_hpd) {
394 		drm_dbg_kms(&dev_priv->drm, "Ignore HPD flag on - skip encoder hotplug handlers\n");
395 		mutex_unlock(&dev_priv->drm.mode_config.mutex);
396 		return;
397 	}
398 
399 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
400 	for_each_intel_connector_iter(connector, &conn_iter) {
401 		enum hpd_pin pin;
402 		u32 hpd_bit;
403 
404 		pin = intel_connector_hpd_pin(connector);
405 		if (pin == HPD_NONE)
406 			continue;
407 
408 		hpd_bit = BIT(pin);
409 		if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
410 			struct intel_encoder *encoder =
411 				intel_attached_encoder(connector);
412 
413 			if (hpd_event_bits & hpd_bit)
414 				connector->hotplug_retries = 0;
415 			else
416 				connector->hotplug_retries++;
417 
418 			drm_dbg_kms(&dev_priv->drm,
419 				    "Connector %s (pin %i) received hotplug event. (retry %d)\n",
420 				    connector->base.name, pin,
421 				    connector->hotplug_retries);
422 
423 			switch (encoder->hotplug(encoder, connector)) {
424 			case INTEL_HOTPLUG_UNCHANGED:
425 				break;
426 			case INTEL_HOTPLUG_CHANGED:
427 				changed |= hpd_bit;
428 				break;
429 			case INTEL_HOTPLUG_RETRY:
430 				retry |= hpd_bit;
431 				break;
432 			}
433 		}
434 	}
435 	drm_connector_list_iter_end(&conn_iter);
436 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
437 
438 	if (changed)
439 		drm_kms_helper_hotplug_event(&dev_priv->drm);
440 
441 	/* Remove shared HPD pins that have changed */
442 	retry &= ~changed;
443 	if (retry) {
444 		spin_lock_irq(&dev_priv->irq_lock);
445 		dev_priv->display.hotplug.retry_bits |= retry;
446 		spin_unlock_irq(&dev_priv->irq_lock);
447 
448 		mod_delayed_work(system_wq, &dev_priv->display.hotplug.hotplug_work,
449 				 msecs_to_jiffies(HPD_RETRY_DELAY));
450 	}
451 }
452 
453 
454 /**
455  * intel_hpd_irq_handler - main hotplug irq handler
456  * @dev_priv: drm_i915_private
457  * @pin_mask: a mask of hpd pins that have triggered the irq
458  * @long_mask: a mask of hpd pins that may be long hpd pulses
459  *
460  * This is the main hotplug irq handler for all platforms. The platform specific
461  * irq handlers call the platform specific hotplug irq handlers, which read and
462  * decode the appropriate registers into bitmasks about hpd pins that have
463  * triggered (@pin_mask), and which of those pins may be long pulses
464  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
465  * is not a digital port.
466  *
467  * Here, we do hotplug irq storm detection and mitigation, and pass further
468  * processing to appropriate bottom halves.
469  */
470 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
471 			   u32 pin_mask, u32 long_mask)
472 {
473 	struct intel_encoder *encoder;
474 	bool storm_detected = false;
475 	bool queue_dig = false, queue_hp = false;
476 	u32 long_hpd_pulse_mask = 0;
477 	u32 short_hpd_pulse_mask = 0;
478 	enum hpd_pin pin;
479 
480 	if (!pin_mask)
481 		return;
482 
483 	spin_lock(&dev_priv->irq_lock);
484 
485 	/*
486 	 * Determine whether ->hpd_pulse() exists for each pin, and
487 	 * whether we have a short or a long pulse. This is needed
488 	 * as each pin may have up to two encoders (HDMI and DP) and
489 	 * only the one of them (DP) will have ->hpd_pulse().
490 	 */
491 	for_each_intel_encoder(&dev_priv->drm, encoder) {
492 		enum port port = encoder->port;
493 		bool long_hpd;
494 
495 		pin = encoder->hpd_pin;
496 		if (!(BIT(pin) & pin_mask))
497 			continue;
498 
499 		if (!intel_encoder_has_hpd_pulse(encoder))
500 			continue;
501 
502 		long_hpd = long_mask & BIT(pin);
503 
504 		drm_dbg(&dev_priv->drm,
505 			"digital hpd on [ENCODER:%d:%s] - %s\n",
506 			encoder->base.base.id, encoder->base.name,
507 			long_hpd ? "long" : "short");
508 		queue_dig = true;
509 
510 		if (long_hpd) {
511 			long_hpd_pulse_mask |= BIT(pin);
512 			dev_priv->display.hotplug.long_port_mask |= BIT(port);
513 		} else {
514 			short_hpd_pulse_mask |= BIT(pin);
515 			dev_priv->display.hotplug.short_port_mask |= BIT(port);
516 		}
517 	}
518 
519 	/* Now process each pin just once */
520 	for_each_hpd_pin(pin) {
521 		bool long_hpd;
522 
523 		if (!(BIT(pin) & pin_mask))
524 			continue;
525 
526 		if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED) {
527 			/*
528 			 * On GMCH platforms the interrupt mask bits only
529 			 * prevent irq generation, not the setting of the
530 			 * hotplug bits itself. So only WARN about unexpected
531 			 * interrupts on saner platforms.
532 			 */
533 			drm_WARN_ONCE(&dev_priv->drm, !HAS_GMCH(dev_priv),
534 				      "Received HPD interrupt on pin %d although disabled\n",
535 				      pin);
536 			continue;
537 		}
538 
539 		if (dev_priv->display.hotplug.stats[pin].state != HPD_ENABLED)
540 			continue;
541 
542 		/*
543 		 * Delegate to ->hpd_pulse() if one of the encoders for this
544 		 * pin has it, otherwise let the hotplug_work deal with this
545 		 * pin directly.
546 		 */
547 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
548 			long_hpd = long_hpd_pulse_mask & BIT(pin);
549 		} else {
550 			dev_priv->display.hotplug.event_bits |= BIT(pin);
551 			long_hpd = true;
552 			queue_hp = true;
553 		}
554 
555 		if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
556 			dev_priv->display.hotplug.event_bits &= ~BIT(pin);
557 			storm_detected = true;
558 			queue_hp = true;
559 		}
560 	}
561 
562 	/*
563 	 * Disable any IRQs that storms were detected on. Polling enablement
564 	 * happens later in our hotplug work.
565 	 */
566 	if (storm_detected)
567 		intel_hpd_irq_setup(dev_priv);
568 	spin_unlock(&dev_priv->irq_lock);
569 
570 	/*
571 	 * Our hotplug handler can grab modeset locks (by calling down into the
572 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
573 	 * queue for otherwise the flush_work in the pageflip code will
574 	 * deadlock.
575 	 */
576 	if (queue_dig)
577 		queue_work(dev_priv->display.hotplug.dp_wq, &dev_priv->display.hotplug.dig_port_work);
578 	if (queue_hp)
579 		queue_delayed_work(system_wq, &dev_priv->display.hotplug.hotplug_work, 0);
580 }
581 
582 /**
583  * intel_hpd_init - initializes and enables hpd support
584  * @dev_priv: i915 device instance
585  *
586  * This function enables the hotplug support. It requires that interrupts have
587  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
588  * poll request can run concurrently to other code, so locking rules must be
589  * obeyed.
590  *
591  * This is a separate step from interrupt enabling to simplify the locking rules
592  * in the driver load and resume code.
593  *
594  * Also see: intel_hpd_poll_enable() and intel_hpd_poll_disable().
595  */
596 void intel_hpd_init(struct drm_i915_private *dev_priv)
597 {
598 	int i;
599 
600 	if (!HAS_DISPLAY(dev_priv))
601 		return;
602 
603 	for_each_hpd_pin(i) {
604 		dev_priv->display.hotplug.stats[i].count = 0;
605 		dev_priv->display.hotplug.stats[i].state = HPD_ENABLED;
606 	}
607 
608 	/*
609 	 * Interrupt setup is already guaranteed to be single-threaded, this is
610 	 * just to make the assert_spin_locked checks happy.
611 	 */
612 	spin_lock_irq(&dev_priv->irq_lock);
613 	intel_hpd_irq_setup(dev_priv);
614 	spin_unlock_irq(&dev_priv->irq_lock);
615 }
616 
617 static void i915_hpd_poll_init_work(struct work_struct *work)
618 {
619 	struct drm_i915_private *dev_priv =
620 		container_of(work, struct drm_i915_private,
621 			     display.hotplug.poll_init_work);
622 	struct drm_connector_list_iter conn_iter;
623 	struct intel_connector *connector;
624 	bool enabled;
625 
626 	mutex_lock(&dev_priv->drm.mode_config.mutex);
627 
628 	enabled = READ_ONCE(dev_priv->display.hotplug.poll_enabled);
629 
630 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
631 	for_each_intel_connector_iter(connector, &conn_iter) {
632 		enum hpd_pin pin;
633 
634 		pin = intel_connector_hpd_pin(connector);
635 		if (pin == HPD_NONE)
636 			continue;
637 
638 		connector->base.polled = connector->polled;
639 
640 		if (enabled && connector->base.polled == DRM_CONNECTOR_POLL_HPD)
641 			connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
642 				DRM_CONNECTOR_POLL_DISCONNECT;
643 	}
644 	drm_connector_list_iter_end(&conn_iter);
645 
646 	if (enabled)
647 		drm_kms_helper_poll_enable(&dev_priv->drm);
648 
649 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
650 
651 	/*
652 	 * We might have missed any hotplugs that happened while we were
653 	 * in the middle of disabling polling
654 	 */
655 	if (!enabled)
656 		drm_helper_hpd_irq_event(&dev_priv->drm);
657 }
658 
659 /**
660  * intel_hpd_poll_enable - enable polling for connectors with hpd
661  * @dev_priv: i915 device instance
662  *
663  * This function enables polling for all connectors which support HPD.
664  * Under certain conditions HPD may not be functional. On most Intel GPUs,
665  * this happens when we enter runtime suspend.
666  * On Valleyview and Cherryview systems, this also happens when we shut off all
667  * of the powerwells.
668  *
669  * Since this function can get called in contexts where we're already holding
670  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
671  * worker.
672  *
673  * Also see: intel_hpd_init() and intel_hpd_poll_disable().
674  */
675 void intel_hpd_poll_enable(struct drm_i915_private *dev_priv)
676 {
677 	if (!HAS_DISPLAY(dev_priv) ||
678 	    !INTEL_DISPLAY_ENABLED(dev_priv))
679 		return;
680 
681 	WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, true);
682 
683 	/*
684 	 * We might already be holding dev->mode_config.mutex, so do this in a
685 	 * seperate worker
686 	 * As well, there's no issue if we race here since we always reschedule
687 	 * this worker anyway
688 	 */
689 	schedule_work(&dev_priv->display.hotplug.poll_init_work);
690 }
691 
692 /**
693  * intel_hpd_poll_disable - disable polling for connectors with hpd
694  * @dev_priv: i915 device instance
695  *
696  * This function disables polling for all connectors which support HPD.
697  * Under certain conditions HPD may not be functional. On most Intel GPUs,
698  * this happens when we enter runtime suspend.
699  * On Valleyview and Cherryview systems, this also happens when we shut off all
700  * of the powerwells.
701  *
702  * Since this function can get called in contexts where we're already holding
703  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
704  * worker.
705  *
706  * Also used during driver init to initialize connector->polled
707  * appropriately for all connectors.
708  *
709  * Also see: intel_hpd_init() and intel_hpd_poll_enable().
710  */
711 void intel_hpd_poll_disable(struct drm_i915_private *dev_priv)
712 {
713 	if (!HAS_DISPLAY(dev_priv))
714 		return;
715 
716 	WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, false);
717 	schedule_work(&dev_priv->display.hotplug.poll_init_work);
718 }
719 
720 void intel_hpd_init_early(struct drm_i915_private *i915)
721 {
722 	INIT_DELAYED_WORK(&i915->display.hotplug.hotplug_work,
723 			  i915_hotplug_work_func);
724 	INIT_WORK(&i915->display.hotplug.dig_port_work, i915_digport_work_func);
725 	INIT_WORK(&i915->display.hotplug.poll_init_work, i915_hpd_poll_init_work);
726 	INIT_DELAYED_WORK(&i915->display.hotplug.reenable_work,
727 			  intel_hpd_irq_storm_reenable_work);
728 
729 	i915->display.hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
730 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
731 	 * detection, as short HPD storms will occur as a natural part of
732 	 * sideband messaging with MST.
733 	 * On older platforms however, IRQ storms can occur with both long and
734 	 * short pulses, as seen on some G4x systems.
735 	 */
736 	i915->display.hotplug.hpd_short_storm_enabled = !HAS_DP_MST(i915);
737 }
738 
739 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
740 {
741 	if (!HAS_DISPLAY(dev_priv))
742 		return;
743 
744 	spin_lock_irq(&dev_priv->irq_lock);
745 
746 	dev_priv->display.hotplug.long_port_mask = 0;
747 	dev_priv->display.hotplug.short_port_mask = 0;
748 	dev_priv->display.hotplug.event_bits = 0;
749 	dev_priv->display.hotplug.retry_bits = 0;
750 
751 	spin_unlock_irq(&dev_priv->irq_lock);
752 
753 	cancel_work_sync(&dev_priv->display.hotplug.dig_port_work);
754 	cancel_delayed_work_sync(&dev_priv->display.hotplug.hotplug_work);
755 	cancel_work_sync(&dev_priv->display.hotplug.poll_init_work);
756 	cancel_delayed_work_sync(&dev_priv->display.hotplug.reenable_work);
757 }
758 
759 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
760 {
761 	bool ret = false;
762 
763 	if (pin == HPD_NONE)
764 		return false;
765 
766 	spin_lock_irq(&dev_priv->irq_lock);
767 	if (dev_priv->display.hotplug.stats[pin].state == HPD_ENABLED) {
768 		dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
769 		ret = true;
770 	}
771 	spin_unlock_irq(&dev_priv->irq_lock);
772 
773 	return ret;
774 }
775 
776 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
777 {
778 	if (pin == HPD_NONE)
779 		return;
780 
781 	spin_lock_irq(&dev_priv->irq_lock);
782 	dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
783 	spin_unlock_irq(&dev_priv->irq_lock);
784 }
785 
786 static int i915_hpd_storm_ctl_show(struct seq_file *m, void *data)
787 {
788 	struct drm_i915_private *dev_priv = m->private;
789 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
790 
791 	/* Synchronize with everything first in case there's been an HPD
792 	 * storm, but we haven't finished handling it in the kernel yet
793 	 */
794 	intel_synchronize_irq(dev_priv);
795 	flush_work(&dev_priv->display.hotplug.dig_port_work);
796 	flush_delayed_work(&dev_priv->display.hotplug.hotplug_work);
797 
798 	seq_printf(m, "Threshold: %d\n", hotplug->hpd_storm_threshold);
799 	seq_printf(m, "Detected: %s\n",
800 		   str_yes_no(delayed_work_pending(&hotplug->reenable_work)));
801 
802 	return 0;
803 }
804 
805 static ssize_t i915_hpd_storm_ctl_write(struct file *file,
806 					const char __user *ubuf, size_t len,
807 					loff_t *offp)
808 {
809 	struct seq_file *m = file->private_data;
810 	struct drm_i915_private *dev_priv = m->private;
811 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
812 	unsigned int new_threshold;
813 	int i;
814 	char *newline;
815 	char tmp[16];
816 
817 	if (len >= sizeof(tmp))
818 		return -EINVAL;
819 
820 	if (copy_from_user(tmp, ubuf, len))
821 		return -EFAULT;
822 
823 	tmp[len] = '\0';
824 
825 	/* Strip newline, if any */
826 	newline = strchr(tmp, '\n');
827 	if (newline)
828 		*newline = '\0';
829 
830 	if (strcmp(tmp, "reset") == 0)
831 		new_threshold = HPD_STORM_DEFAULT_THRESHOLD;
832 	else if (kstrtouint(tmp, 10, &new_threshold) != 0)
833 		return -EINVAL;
834 
835 	if (new_threshold > 0)
836 		drm_dbg_kms(&dev_priv->drm,
837 			    "Setting HPD storm detection threshold to %d\n",
838 			    new_threshold);
839 	else
840 		drm_dbg_kms(&dev_priv->drm, "Disabling HPD storm detection\n");
841 
842 	spin_lock_irq(&dev_priv->irq_lock);
843 	hotplug->hpd_storm_threshold = new_threshold;
844 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
845 	for_each_hpd_pin(i)
846 		hotplug->stats[i].count = 0;
847 	spin_unlock_irq(&dev_priv->irq_lock);
848 
849 	/* Re-enable hpd immediately if we were in an irq storm */
850 	flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
851 
852 	return len;
853 }
854 
855 static int i915_hpd_storm_ctl_open(struct inode *inode, struct file *file)
856 {
857 	return single_open(file, i915_hpd_storm_ctl_show, inode->i_private);
858 }
859 
860 static const struct file_operations i915_hpd_storm_ctl_fops = {
861 	.owner = THIS_MODULE,
862 	.open = i915_hpd_storm_ctl_open,
863 	.read = seq_read,
864 	.llseek = seq_lseek,
865 	.release = single_release,
866 	.write = i915_hpd_storm_ctl_write
867 };
868 
869 static int i915_hpd_short_storm_ctl_show(struct seq_file *m, void *data)
870 {
871 	struct drm_i915_private *dev_priv = m->private;
872 
873 	seq_printf(m, "Enabled: %s\n",
874 		   str_yes_no(dev_priv->display.hotplug.hpd_short_storm_enabled));
875 
876 	return 0;
877 }
878 
879 static int
880 i915_hpd_short_storm_ctl_open(struct inode *inode, struct file *file)
881 {
882 	return single_open(file, i915_hpd_short_storm_ctl_show,
883 			   inode->i_private);
884 }
885 
886 static ssize_t i915_hpd_short_storm_ctl_write(struct file *file,
887 					      const char __user *ubuf,
888 					      size_t len, loff_t *offp)
889 {
890 	struct seq_file *m = file->private_data;
891 	struct drm_i915_private *dev_priv = m->private;
892 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
893 	char *newline;
894 	char tmp[16];
895 	int i;
896 	bool new_state;
897 
898 	if (len >= sizeof(tmp))
899 		return -EINVAL;
900 
901 	if (copy_from_user(tmp, ubuf, len))
902 		return -EFAULT;
903 
904 	tmp[len] = '\0';
905 
906 	/* Strip newline, if any */
907 	newline = strchr(tmp, '\n');
908 	if (newline)
909 		*newline = '\0';
910 
911 	/* Reset to the "default" state for this system */
912 	if (strcmp(tmp, "reset") == 0)
913 		new_state = !HAS_DP_MST(dev_priv);
914 	else if (kstrtobool(tmp, &new_state) != 0)
915 		return -EINVAL;
916 
917 	drm_dbg_kms(&dev_priv->drm, "%sabling HPD short storm detection\n",
918 		    new_state ? "En" : "Dis");
919 
920 	spin_lock_irq(&dev_priv->irq_lock);
921 	hotplug->hpd_short_storm_enabled = new_state;
922 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
923 	for_each_hpd_pin(i)
924 		hotplug->stats[i].count = 0;
925 	spin_unlock_irq(&dev_priv->irq_lock);
926 
927 	/* Re-enable hpd immediately if we were in an irq storm */
928 	flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
929 
930 	return len;
931 }
932 
933 static const struct file_operations i915_hpd_short_storm_ctl_fops = {
934 	.owner = THIS_MODULE,
935 	.open = i915_hpd_short_storm_ctl_open,
936 	.read = seq_read,
937 	.llseek = seq_lseek,
938 	.release = single_release,
939 	.write = i915_hpd_short_storm_ctl_write,
940 };
941 
942 void intel_hpd_debugfs_register(struct drm_i915_private *i915)
943 {
944 	struct drm_minor *minor = i915->drm.primary;
945 
946 	debugfs_create_file("i915_hpd_storm_ctl", 0644, minor->debugfs_root,
947 			    i915, &i915_hpd_storm_ctl_fops);
948 	debugfs_create_file("i915_hpd_short_storm_ctl", 0644, minor->debugfs_root,
949 			    i915, &i915_hpd_short_storm_ctl_fops);
950 	debugfs_create_bool("i915_ignore_long_hpd", 0644, minor->debugfs_root,
951 			    &i915->display.hotplug.ignore_long_hpd);
952 }
953