1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <linux/kernel.h>
25 
26 #include <drm/i915_drm.h>
27 
28 #include "i915_drv.h"
29 #include "intel_drv.h"
30 #include "intel_hotplug.h"
31 
32 /**
33  * DOC: Hotplug
34  *
35  * Simply put, hotplug occurs when a display is connected to or disconnected
36  * from the system. However, there may be adapters and docking stations and
37  * Display Port short pulses and MST devices involved, complicating matters.
38  *
39  * Hotplug in i915 is handled in many different levels of abstraction.
40  *
41  * The platform dependent interrupt handling code in i915_irq.c enables,
42  * disables, and does preliminary handling of the interrupts. The interrupt
43  * handlers gather the hotplug detect (HPD) information from relevant registers
44  * into a platform independent mask of hotplug pins that have fired.
45  *
46  * The platform independent interrupt handler intel_hpd_irq_handler() in
47  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
48  * further processing to appropriate bottom halves (Display Port specific and
49  * regular hotplug).
50  *
51  * The Display Port work function i915_digport_work_func() calls into
52  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
53  * pulses, with failures and non-MST long pulses triggering regular hotplug
54  * processing on the connector.
55  *
56  * The regular hotplug work function i915_hotplug_work_func() calls connector
57  * detect hooks, and, if connector status changes, triggers sending of hotplug
58  * uevent to userspace via drm_kms_helper_hotplug_event().
59  *
60  * Finally, the userspace is responsible for triggering a modeset upon receiving
61  * the hotplug uevent, disabling or enabling the crtc as needed.
62  *
63  * The hotplug interrupt storm detection and mitigation code keeps track of the
64  * number of interrupts per hotplug pin per a period of time, and if the number
65  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
66  * while before being re-enabled. The intention is to mitigate issues raising
67  * from broken hardware triggering massive amounts of interrupts and grinding
68  * the system to a halt.
69  *
70  * Current implementation expects that hotplug interrupt storm will not be
71  * seen when display port sink is connected, hence on platforms whose DP
72  * callback is handled by i915_digport_work_func reenabling of hpd is not
73  * performed (it was never expected to be disabled in the first place ;) )
74  * this is specific to DP sinks handled by this routine and any other display
75  * such as HDMI or DVI enabled on the same port will have proper logic since
76  * it will use i915_hotplug_work_func where this logic is handled.
77  */
78 
79 /**
80  * intel_hpd_pin_default - return default pin associated with certain port.
81  * @dev_priv: private driver data pointer
82  * @port: the hpd port to get associated pin
83  *
84  * It is only valid and used by digital port encoder.
85  *
86  * Return pin that is associatade with @port and HDP_NONE if no pin is
87  * hard associated with that @port.
88  */
89 enum 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 #define HPD_RETRY_DELAY			1000
116 
117 /**
118  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
119  * @dev_priv: private driver data pointer
120  * @pin: the pin to gather stats on
121  * @long_hpd: whether the HPD IRQ was long or short
122  *
123  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
124  * storms. Only the pin specific stats and state are changed, the caller is
125  * responsible for further action.
126  *
127  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
128  * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
129  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
130  * short IRQs count as +1. If this threshold is exceeded, it's considered an
131  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
132  *
133  * By default, most systems will only count long IRQs towards
134  * &dev_priv->hotplug.hpd_storm_threshold. However, some older systems also
135  * suffer from short IRQ storms and must also track these. Because short IRQ
136  * storms are naturally caused by sideband interactions with DP MST devices,
137  * short IRQ detection is only enabled for systems without DP MST support.
138  * Systems which are new enough to support DP MST are far less likely to
139  * suffer from IRQ storms at all, so this is fine.
140  *
141  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
142  * and should only be adjusted for automated hotplug testing.
143  *
144  * Return true if an IRQ storm was detected on @pin.
145  */
146 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
147 				       enum hpd_pin pin, bool long_hpd)
148 {
149 	struct i915_hotplug *hpd = &dev_priv->hotplug;
150 	unsigned long start = hpd->stats[pin].last_jiffies;
151 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
152 	const int increment = long_hpd ? 10 : 1;
153 	const int threshold = hpd->hpd_storm_threshold;
154 	bool storm = false;
155 
156 	if (!threshold ||
157 	    (!long_hpd && !dev_priv->hotplug.hpd_short_storm_enabled))
158 		return false;
159 
160 	if (!time_in_range(jiffies, start, end)) {
161 		hpd->stats[pin].last_jiffies = jiffies;
162 		hpd->stats[pin].count = 0;
163 	}
164 
165 	hpd->stats[pin].count += increment;
166 	if (hpd->stats[pin].count > threshold) {
167 		hpd->stats[pin].state = HPD_MARK_DISABLED;
168 		DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
169 		storm = true;
170 	} else {
171 		DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", 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_device *dev = &dev_priv->drm;
182 	struct intel_connector *intel_connector;
183 	struct intel_encoder *intel_encoder;
184 	struct drm_connector *connector;
185 	struct drm_connector_list_iter conn_iter;
186 	enum hpd_pin pin;
187 	bool hpd_disabled = false;
188 
189 	lockdep_assert_held(&dev_priv->irq_lock);
190 
191 	drm_connector_list_iter_begin(dev, &conn_iter);
192 	drm_for_each_connector_iter(connector, &conn_iter) {
193 		if (connector->polled != DRM_CONNECTOR_POLL_HPD)
194 			continue;
195 
196 		intel_connector = to_intel_connector(connector);
197 		intel_encoder = intel_connector->encoder;
198 		if (!intel_encoder)
199 			continue;
200 
201 		pin = intel_encoder->hpd_pin;
202 		if (pin == HPD_NONE ||
203 		    dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
204 			continue;
205 
206 		DRM_INFO("HPD interrupt storm detected on connector %s: "
207 			 "switching from hotplug detection to polling\n",
208 			 connector->name);
209 
210 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
211 		connector->polled = DRM_CONNECTOR_POLL_CONNECT
212 			| DRM_CONNECTOR_POLL_DISCONNECT;
213 		hpd_disabled = true;
214 	}
215 	drm_connector_list_iter_end(&conn_iter);
216 
217 	/* Enable polling and queue hotplug re-enabling. */
218 	if (hpd_disabled) {
219 		drm_kms_helper_poll_enable(dev);
220 		mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
221 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
222 	}
223 }
224 
225 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
226 {
227 	struct drm_i915_private *dev_priv =
228 		container_of(work, typeof(*dev_priv),
229 			     hotplug.reenable_work.work);
230 	struct drm_device *dev = &dev_priv->drm;
231 	intel_wakeref_t wakeref;
232 	enum hpd_pin pin;
233 
234 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
235 
236 	spin_lock_irq(&dev_priv->irq_lock);
237 	for_each_hpd_pin(pin) {
238 		struct drm_connector *connector;
239 		struct drm_connector_list_iter conn_iter;
240 
241 		if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
242 			continue;
243 
244 		dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
245 
246 		drm_connector_list_iter_begin(dev, &conn_iter);
247 		drm_for_each_connector_iter(connector, &conn_iter) {
248 			struct intel_connector *intel_connector = to_intel_connector(connector);
249 
250 			/* Don't check MST ports, they don't have pins */
251 			if (!intel_connector->mst_port &&
252 			    intel_connector->encoder->hpd_pin == pin) {
253 				if (connector->polled != intel_connector->polled)
254 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
255 							 connector->name);
256 				connector->polled = intel_connector->polled;
257 				if (!connector->polled)
258 					connector->polled = DRM_CONNECTOR_POLL_HPD;
259 			}
260 		}
261 		drm_connector_list_iter_end(&conn_iter);
262 	}
263 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
264 		dev_priv->display.hpd_irq_setup(dev_priv);
265 	spin_unlock_irq(&dev_priv->irq_lock);
266 
267 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
268 }
269 
270 enum intel_hotplug_state
271 intel_encoder_hotplug(struct intel_encoder *encoder,
272 		      struct intel_connector *connector,
273 		      bool irq_received)
274 {
275 	struct drm_device *dev = connector->base.dev;
276 	enum drm_connector_status old_status;
277 
278 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
279 	old_status = connector->base.status;
280 
281 	connector->base.status =
282 		drm_helper_probe_detect(&connector->base, NULL, false);
283 
284 	if (old_status == connector->base.status)
285 		return INTEL_HOTPLUG_UNCHANGED;
286 
287 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
288 		      connector->base.base.id,
289 		      connector->base.name,
290 		      drm_get_connector_status_name(old_status),
291 		      drm_get_connector_status_name(connector->base.status));
292 
293 	return INTEL_HOTPLUG_CHANGED;
294 }
295 
296 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
297 {
298 	return intel_encoder_is_dig_port(encoder) &&
299 		enc_to_dig_port(&encoder->base)->hpd_pulse != NULL;
300 }
301 
302 static void i915_digport_work_func(struct work_struct *work)
303 {
304 	struct drm_i915_private *dev_priv =
305 		container_of(work, struct drm_i915_private, hotplug.dig_port_work);
306 	u32 long_port_mask, short_port_mask;
307 	struct intel_encoder *encoder;
308 	u32 old_bits = 0;
309 
310 	spin_lock_irq(&dev_priv->irq_lock);
311 	long_port_mask = dev_priv->hotplug.long_port_mask;
312 	dev_priv->hotplug.long_port_mask = 0;
313 	short_port_mask = dev_priv->hotplug.short_port_mask;
314 	dev_priv->hotplug.short_port_mask = 0;
315 	spin_unlock_irq(&dev_priv->irq_lock);
316 
317 	for_each_intel_encoder(&dev_priv->drm, encoder) {
318 		struct intel_digital_port *dig_port;
319 		enum port port = encoder->port;
320 		bool long_hpd, short_hpd;
321 		enum irqreturn ret;
322 
323 		if (!intel_encoder_has_hpd_pulse(encoder))
324 			continue;
325 
326 		long_hpd = long_port_mask & BIT(port);
327 		short_hpd = short_port_mask & BIT(port);
328 
329 		if (!long_hpd && !short_hpd)
330 			continue;
331 
332 		dig_port = enc_to_dig_port(&encoder->base);
333 
334 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
335 		if (ret == IRQ_NONE) {
336 			/* fall back to old school hpd */
337 			old_bits |= BIT(encoder->hpd_pin);
338 		}
339 	}
340 
341 	if (old_bits) {
342 		spin_lock_irq(&dev_priv->irq_lock);
343 		dev_priv->hotplug.event_bits |= old_bits;
344 		spin_unlock_irq(&dev_priv->irq_lock);
345 		queue_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work, 0);
346 	}
347 }
348 
349 /*
350  * Handle hotplug events outside the interrupt handler proper.
351  */
352 static void i915_hotplug_work_func(struct work_struct *work)
353 {
354 	struct drm_i915_private *dev_priv =
355 		container_of(work, struct drm_i915_private,
356 			     hotplug.hotplug_work.work);
357 	struct drm_device *dev = &dev_priv->drm;
358 	struct intel_connector *intel_connector;
359 	struct intel_encoder *intel_encoder;
360 	struct drm_connector *connector;
361 	struct drm_connector_list_iter conn_iter;
362 	u32 changed = 0, retry = 0;
363 	u32 hpd_event_bits;
364 	u32 hpd_retry_bits;
365 
366 	mutex_lock(&dev->mode_config.mutex);
367 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
368 
369 	spin_lock_irq(&dev_priv->irq_lock);
370 
371 	hpd_event_bits = dev_priv->hotplug.event_bits;
372 	dev_priv->hotplug.event_bits = 0;
373 	hpd_retry_bits = dev_priv->hotplug.retry_bits;
374 	dev_priv->hotplug.retry_bits = 0;
375 
376 	/* Enable polling for connectors which had HPD IRQ storms */
377 	intel_hpd_irq_storm_switch_to_polling(dev_priv);
378 
379 	spin_unlock_irq(&dev_priv->irq_lock);
380 
381 	drm_connector_list_iter_begin(dev, &conn_iter);
382 	drm_for_each_connector_iter(connector, &conn_iter) {
383 		u32 hpd_bit;
384 
385 		intel_connector = to_intel_connector(connector);
386 		if (!intel_connector->encoder)
387 			continue;
388 		intel_encoder = intel_connector->encoder;
389 		hpd_bit = BIT(intel_encoder->hpd_pin);
390 		if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
391 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
392 				      connector->name, intel_encoder->hpd_pin);
393 
394 			switch (intel_encoder->hotplug(intel_encoder,
395 						       intel_connector,
396 						       hpd_event_bits & hpd_bit)) {
397 			case INTEL_HOTPLUG_UNCHANGED:
398 				break;
399 			case INTEL_HOTPLUG_CHANGED:
400 				changed |= hpd_bit;
401 				break;
402 			case INTEL_HOTPLUG_RETRY:
403 				retry |= hpd_bit;
404 				break;
405 			}
406 		}
407 	}
408 	drm_connector_list_iter_end(&conn_iter);
409 	mutex_unlock(&dev->mode_config.mutex);
410 
411 	if (changed)
412 		drm_kms_helper_hotplug_event(dev);
413 
414 	/* Remove shared HPD pins that have changed */
415 	retry &= ~changed;
416 	if (retry) {
417 		spin_lock_irq(&dev_priv->irq_lock);
418 		dev_priv->hotplug.retry_bits |= retry;
419 		spin_unlock_irq(&dev_priv->irq_lock);
420 
421 		mod_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work,
422 				 msecs_to_jiffies(HPD_RETRY_DELAY));
423 	}
424 }
425 
426 
427 /**
428  * intel_hpd_irq_handler - main hotplug irq handler
429  * @dev_priv: drm_i915_private
430  * @pin_mask: a mask of hpd pins that have triggered the irq
431  * @long_mask: a mask of hpd pins that may be long hpd pulses
432  *
433  * This is the main hotplug irq handler for all platforms. The platform specific
434  * irq handlers call the platform specific hotplug irq handlers, which read and
435  * decode the appropriate registers into bitmasks about hpd pins that have
436  * triggered (@pin_mask), and which of those pins may be long pulses
437  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
438  * is not a digital port.
439  *
440  * Here, we do hotplug irq storm detection and mitigation, and pass further
441  * processing to appropriate bottom halves.
442  */
443 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
444 			   u32 pin_mask, u32 long_mask)
445 {
446 	struct intel_encoder *encoder;
447 	bool storm_detected = false;
448 	bool queue_dig = false, queue_hp = false;
449 	u32 long_hpd_pulse_mask = 0;
450 	u32 short_hpd_pulse_mask = 0;
451 	enum hpd_pin pin;
452 
453 	if (!pin_mask)
454 		return;
455 
456 	spin_lock(&dev_priv->irq_lock);
457 
458 	/*
459 	 * Determine whether ->hpd_pulse() exists for each pin, and
460 	 * whether we have a short or a long pulse. This is needed
461 	 * as each pin may have up to two encoders (HDMI and DP) and
462 	 * only the one of them (DP) will have ->hpd_pulse().
463 	 */
464 	for_each_intel_encoder(&dev_priv->drm, encoder) {
465 		bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
466 		enum port port = encoder->port;
467 		bool long_hpd;
468 
469 		pin = encoder->hpd_pin;
470 		if (!(BIT(pin) & pin_mask))
471 			continue;
472 
473 		if (!has_hpd_pulse)
474 			continue;
475 
476 		long_hpd = long_mask & BIT(pin);
477 
478 		DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
479 				 long_hpd ? "long" : "short");
480 		queue_dig = true;
481 
482 		if (long_hpd) {
483 			long_hpd_pulse_mask |= BIT(pin);
484 			dev_priv->hotplug.long_port_mask |= BIT(port);
485 		} else {
486 			short_hpd_pulse_mask |= BIT(pin);
487 			dev_priv->hotplug.short_port_mask |= BIT(port);
488 		}
489 	}
490 
491 	/* Now process each pin just once */
492 	for_each_hpd_pin(pin) {
493 		bool long_hpd;
494 
495 		if (!(BIT(pin) & pin_mask))
496 			continue;
497 
498 		if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
499 			/*
500 			 * On GMCH platforms the interrupt mask bits only
501 			 * prevent irq generation, not the setting of the
502 			 * hotplug bits itself. So only WARN about unexpected
503 			 * interrupts on saner platforms.
504 			 */
505 			WARN_ONCE(!HAS_GMCH(dev_priv),
506 				  "Received HPD interrupt on pin %d although disabled\n", pin);
507 			continue;
508 		}
509 
510 		if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
511 			continue;
512 
513 		/*
514 		 * Delegate to ->hpd_pulse() if one of the encoders for this
515 		 * pin has it, otherwise let the hotplug_work deal with this
516 		 * pin directly.
517 		 */
518 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
519 			long_hpd = long_hpd_pulse_mask & BIT(pin);
520 		} else {
521 			dev_priv->hotplug.event_bits |= BIT(pin);
522 			long_hpd = true;
523 			queue_hp = true;
524 		}
525 
526 		if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
527 			dev_priv->hotplug.event_bits &= ~BIT(pin);
528 			storm_detected = true;
529 			queue_hp = true;
530 		}
531 	}
532 
533 	/*
534 	 * Disable any IRQs that storms were detected on. Polling enablement
535 	 * happens later in our hotplug work.
536 	 */
537 	if (storm_detected && dev_priv->display_irqs_enabled)
538 		dev_priv->display.hpd_irq_setup(dev_priv);
539 	spin_unlock(&dev_priv->irq_lock);
540 
541 	/*
542 	 * Our hotplug handler can grab modeset locks (by calling down into the
543 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
544 	 * queue for otherwise the flush_work in the pageflip code will
545 	 * deadlock.
546 	 */
547 	if (queue_dig)
548 		queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
549 	if (queue_hp)
550 		queue_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work, 0);
551 }
552 
553 /**
554  * intel_hpd_init - initializes and enables hpd support
555  * @dev_priv: i915 device instance
556  *
557  * This function enables the hotplug support. It requires that interrupts have
558  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
559  * poll request can run concurrently to other code, so locking rules must be
560  * obeyed.
561  *
562  * This is a separate step from interrupt enabling to simplify the locking rules
563  * in the driver load and resume code.
564  *
565  * Also see: intel_hpd_poll_init(), which enables connector polling
566  */
567 void intel_hpd_init(struct drm_i915_private *dev_priv)
568 {
569 	int i;
570 
571 	for_each_hpd_pin(i) {
572 		dev_priv->hotplug.stats[i].count = 0;
573 		dev_priv->hotplug.stats[i].state = HPD_ENABLED;
574 	}
575 
576 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
577 	schedule_work(&dev_priv->hotplug.poll_init_work);
578 
579 	/*
580 	 * Interrupt setup is already guaranteed to be single-threaded, this is
581 	 * just to make the assert_spin_locked checks happy.
582 	 */
583 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
584 		spin_lock_irq(&dev_priv->irq_lock);
585 		if (dev_priv->display_irqs_enabled)
586 			dev_priv->display.hpd_irq_setup(dev_priv);
587 		spin_unlock_irq(&dev_priv->irq_lock);
588 	}
589 }
590 
591 static void i915_hpd_poll_init_work(struct work_struct *work)
592 {
593 	struct drm_i915_private *dev_priv =
594 		container_of(work, struct drm_i915_private,
595 			     hotplug.poll_init_work);
596 	struct drm_device *dev = &dev_priv->drm;
597 	struct drm_connector *connector;
598 	struct drm_connector_list_iter conn_iter;
599 	bool enabled;
600 
601 	mutex_lock(&dev->mode_config.mutex);
602 
603 	enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
604 
605 	drm_connector_list_iter_begin(dev, &conn_iter);
606 	drm_for_each_connector_iter(connector, &conn_iter) {
607 		struct intel_connector *intel_connector =
608 			to_intel_connector(connector);
609 		connector->polled = intel_connector->polled;
610 
611 		/* MST has a dynamic intel_connector->encoder and it's reprobing
612 		 * is all handled by the MST helpers. */
613 		if (intel_connector->mst_port)
614 			continue;
615 
616 		if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
617 		    intel_connector->encoder->hpd_pin > HPD_NONE) {
618 			connector->polled = enabled ?
619 				DRM_CONNECTOR_POLL_CONNECT |
620 				DRM_CONNECTOR_POLL_DISCONNECT :
621 				DRM_CONNECTOR_POLL_HPD;
622 		}
623 	}
624 	drm_connector_list_iter_end(&conn_iter);
625 
626 	if (enabled)
627 		drm_kms_helper_poll_enable(dev);
628 
629 	mutex_unlock(&dev->mode_config.mutex);
630 
631 	/*
632 	 * We might have missed any hotplugs that happened while we were
633 	 * in the middle of disabling polling
634 	 */
635 	if (!enabled)
636 		drm_helper_hpd_irq_event(dev);
637 }
638 
639 /**
640  * intel_hpd_poll_init - enables/disables polling for connectors with hpd
641  * @dev_priv: i915 device instance
642  *
643  * This function enables polling for all connectors, regardless of whether or
644  * not they support hotplug detection. Under certain conditions HPD may not be
645  * functional. On most Intel GPUs, this happens when we enter runtime suspend.
646  * On Valleyview and Cherryview systems, this also happens when we shut off all
647  * of the powerwells.
648  *
649  * Since this function can get called in contexts where we're already holding
650  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
651  * worker.
652  *
653  * Also see: intel_hpd_init(), which restores hpd handling.
654  */
655 void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
656 {
657 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
658 
659 	/*
660 	 * We might already be holding dev->mode_config.mutex, so do this in a
661 	 * seperate worker
662 	 * As well, there's no issue if we race here since we always reschedule
663 	 * this worker anyway
664 	 */
665 	schedule_work(&dev_priv->hotplug.poll_init_work);
666 }
667 
668 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
669 {
670 	INIT_DELAYED_WORK(&dev_priv->hotplug.hotplug_work,
671 			  i915_hotplug_work_func);
672 	INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
673 	INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
674 	INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
675 			  intel_hpd_irq_storm_reenable_work);
676 }
677 
678 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
679 {
680 	spin_lock_irq(&dev_priv->irq_lock);
681 
682 	dev_priv->hotplug.long_port_mask = 0;
683 	dev_priv->hotplug.short_port_mask = 0;
684 	dev_priv->hotplug.event_bits = 0;
685 	dev_priv->hotplug.retry_bits = 0;
686 
687 	spin_unlock_irq(&dev_priv->irq_lock);
688 
689 	cancel_work_sync(&dev_priv->hotplug.dig_port_work);
690 	cancel_delayed_work_sync(&dev_priv->hotplug.hotplug_work);
691 	cancel_work_sync(&dev_priv->hotplug.poll_init_work);
692 	cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
693 }
694 
695 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
696 {
697 	bool ret = false;
698 
699 	if (pin == HPD_NONE)
700 		return false;
701 
702 	spin_lock_irq(&dev_priv->irq_lock);
703 	if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
704 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
705 		ret = true;
706 	}
707 	spin_unlock_irq(&dev_priv->irq_lock);
708 
709 	return ret;
710 }
711 
712 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
713 {
714 	if (pin == HPD_NONE)
715 		return;
716 
717 	spin_lock_irq(&dev_priv->irq_lock);
718 	dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
719 	spin_unlock_irq(&dev_priv->irq_lock);
720 }
721