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 
116 /**
117  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
118  * @dev_priv: private driver data pointer
119  * @pin: the pin to gather stats on
120  * @long_hpd: whether the HPD IRQ was long or short
121  *
122  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
123  * storms. Only the pin specific stats and state are changed, the caller is
124  * responsible for further action.
125  *
126  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
127  * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
128  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
129  * short IRQs count as +1. If this threshold is exceeded, it's considered an
130  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
131  *
132  * By default, most systems will only count long IRQs towards
133  * &dev_priv->hotplug.hpd_storm_threshold. However, some older systems also
134  * suffer from short IRQ storms and must also track these. Because short IRQ
135  * storms are naturally caused by sideband interactions with DP MST devices,
136  * short IRQ detection is only enabled for systems without DP MST support.
137  * Systems which are new enough to support DP MST are far less likely to
138  * suffer from IRQ storms at all, so this is fine.
139  *
140  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
141  * and should only be adjusted for automated hotplug testing.
142  *
143  * Return true if an IRQ storm was detected on @pin.
144  */
145 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
146 				       enum hpd_pin pin, bool long_hpd)
147 {
148 	struct i915_hotplug *hpd = &dev_priv->hotplug;
149 	unsigned long start = hpd->stats[pin].last_jiffies;
150 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
151 	const int increment = long_hpd ? 10 : 1;
152 	const int threshold = hpd->hpd_storm_threshold;
153 	bool storm = false;
154 
155 	if (!threshold ||
156 	    (!long_hpd && !dev_priv->hotplug.hpd_short_storm_enabled))
157 		return false;
158 
159 	if (!time_in_range(jiffies, start, end)) {
160 		hpd->stats[pin].last_jiffies = jiffies;
161 		hpd->stats[pin].count = 0;
162 	}
163 
164 	hpd->stats[pin].count += increment;
165 	if (hpd->stats[pin].count > threshold) {
166 		hpd->stats[pin].state = HPD_MARK_DISABLED;
167 		DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
168 		storm = true;
169 	} else {
170 		DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
171 			      hpd->stats[pin].count);
172 	}
173 
174 	return storm;
175 }
176 
177 static void
178 intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
179 {
180 	struct drm_device *dev = &dev_priv->drm;
181 	struct intel_connector *intel_connector;
182 	struct intel_encoder *intel_encoder;
183 	struct drm_connector *connector;
184 	struct drm_connector_list_iter conn_iter;
185 	enum hpd_pin pin;
186 	bool hpd_disabled = false;
187 
188 	lockdep_assert_held(&dev_priv->irq_lock);
189 
190 	drm_connector_list_iter_begin(dev, &conn_iter);
191 	drm_for_each_connector_iter(connector, &conn_iter) {
192 		if (connector->polled != DRM_CONNECTOR_POLL_HPD)
193 			continue;
194 
195 		intel_connector = to_intel_connector(connector);
196 		intel_encoder = intel_connector->encoder;
197 		if (!intel_encoder)
198 			continue;
199 
200 		pin = intel_encoder->hpd_pin;
201 		if (pin == HPD_NONE ||
202 		    dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
203 			continue;
204 
205 		DRM_INFO("HPD interrupt storm detected on connector %s: "
206 			 "switching from hotplug detection to polling\n",
207 			 connector->name);
208 
209 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
210 		connector->polled = DRM_CONNECTOR_POLL_CONNECT
211 			| DRM_CONNECTOR_POLL_DISCONNECT;
212 		hpd_disabled = true;
213 	}
214 	drm_connector_list_iter_end(&conn_iter);
215 
216 	/* Enable polling and queue hotplug re-enabling. */
217 	if (hpd_disabled) {
218 		drm_kms_helper_poll_enable(dev);
219 		mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
220 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
221 	}
222 }
223 
224 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
225 {
226 	struct drm_i915_private *dev_priv =
227 		container_of(work, typeof(*dev_priv),
228 			     hotplug.reenable_work.work);
229 	struct drm_device *dev = &dev_priv->drm;
230 	intel_wakeref_t wakeref;
231 	enum hpd_pin pin;
232 
233 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
234 
235 	spin_lock_irq(&dev_priv->irq_lock);
236 	for_each_hpd_pin(pin) {
237 		struct drm_connector *connector;
238 		struct drm_connector_list_iter conn_iter;
239 
240 		if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
241 			continue;
242 
243 		dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
244 
245 		drm_connector_list_iter_begin(dev, &conn_iter);
246 		drm_for_each_connector_iter(connector, &conn_iter) {
247 			struct intel_connector *intel_connector = to_intel_connector(connector);
248 
249 			/* Don't check MST ports, they don't have pins */
250 			if (!intel_connector->mst_port &&
251 			    intel_connector->encoder->hpd_pin == pin) {
252 				if (connector->polled != intel_connector->polled)
253 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
254 							 connector->name);
255 				connector->polled = intel_connector->polled;
256 				if (!connector->polled)
257 					connector->polled = DRM_CONNECTOR_POLL_HPD;
258 			}
259 		}
260 		drm_connector_list_iter_end(&conn_iter);
261 	}
262 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
263 		dev_priv->display.hpd_irq_setup(dev_priv);
264 	spin_unlock_irq(&dev_priv->irq_lock);
265 
266 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
267 }
268 
269 bool intel_encoder_hotplug(struct intel_encoder *encoder,
270 			   struct intel_connector *connector)
271 {
272 	struct drm_device *dev = connector->base.dev;
273 	enum drm_connector_status old_status;
274 
275 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
276 	old_status = connector->base.status;
277 
278 	connector->base.status =
279 		drm_helper_probe_detect(&connector->base, NULL, false);
280 
281 	if (old_status == connector->base.status)
282 		return false;
283 
284 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
285 		      connector->base.base.id,
286 		      connector->base.name,
287 		      drm_get_connector_status_name(old_status),
288 		      drm_get_connector_status_name(connector->base.status));
289 
290 	return true;
291 }
292 
293 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
294 {
295 	return intel_encoder_is_dig_port(encoder) &&
296 		enc_to_dig_port(&encoder->base)->hpd_pulse != NULL;
297 }
298 
299 static void i915_digport_work_func(struct work_struct *work)
300 {
301 	struct drm_i915_private *dev_priv =
302 		container_of(work, struct drm_i915_private, hotplug.dig_port_work);
303 	u32 long_port_mask, short_port_mask;
304 	struct intel_encoder *encoder;
305 	u32 old_bits = 0;
306 
307 	spin_lock_irq(&dev_priv->irq_lock);
308 	long_port_mask = dev_priv->hotplug.long_port_mask;
309 	dev_priv->hotplug.long_port_mask = 0;
310 	short_port_mask = dev_priv->hotplug.short_port_mask;
311 	dev_priv->hotplug.short_port_mask = 0;
312 	spin_unlock_irq(&dev_priv->irq_lock);
313 
314 	for_each_intel_encoder(&dev_priv->drm, encoder) {
315 		struct intel_digital_port *dig_port;
316 		enum port port = encoder->port;
317 		bool long_hpd, short_hpd;
318 		enum irqreturn ret;
319 
320 		if (!intel_encoder_has_hpd_pulse(encoder))
321 			continue;
322 
323 		long_hpd = long_port_mask & BIT(port);
324 		short_hpd = short_port_mask & BIT(port);
325 
326 		if (!long_hpd && !short_hpd)
327 			continue;
328 
329 		dig_port = enc_to_dig_port(&encoder->base);
330 
331 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
332 		if (ret == IRQ_NONE) {
333 			/* fall back to old school hpd */
334 			old_bits |= BIT(encoder->hpd_pin);
335 		}
336 	}
337 
338 	if (old_bits) {
339 		spin_lock_irq(&dev_priv->irq_lock);
340 		dev_priv->hotplug.event_bits |= old_bits;
341 		spin_unlock_irq(&dev_priv->irq_lock);
342 		schedule_work(&dev_priv->hotplug.hotplug_work);
343 	}
344 }
345 
346 /*
347  * Handle hotplug events outside the interrupt handler proper.
348  */
349 static void i915_hotplug_work_func(struct work_struct *work)
350 {
351 	struct drm_i915_private *dev_priv =
352 		container_of(work, struct drm_i915_private, hotplug.hotplug_work);
353 	struct drm_device *dev = &dev_priv->drm;
354 	struct intel_connector *intel_connector;
355 	struct intel_encoder *intel_encoder;
356 	struct drm_connector *connector;
357 	struct drm_connector_list_iter conn_iter;
358 	bool changed = false;
359 	u32 hpd_event_bits;
360 
361 	mutex_lock(&dev->mode_config.mutex);
362 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
363 
364 	spin_lock_irq(&dev_priv->irq_lock);
365 
366 	hpd_event_bits = dev_priv->hotplug.event_bits;
367 	dev_priv->hotplug.event_bits = 0;
368 
369 	/* Enable polling for connectors which had HPD IRQ storms */
370 	intel_hpd_irq_storm_switch_to_polling(dev_priv);
371 
372 	spin_unlock_irq(&dev_priv->irq_lock);
373 
374 	drm_connector_list_iter_begin(dev, &conn_iter);
375 	drm_for_each_connector_iter(connector, &conn_iter) {
376 		intel_connector = to_intel_connector(connector);
377 		if (!intel_connector->encoder)
378 			continue;
379 		intel_encoder = intel_connector->encoder;
380 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
381 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
382 				      connector->name, intel_encoder->hpd_pin);
383 
384 			changed |= intel_encoder->hotplug(intel_encoder,
385 							  intel_connector);
386 		}
387 	}
388 	drm_connector_list_iter_end(&conn_iter);
389 	mutex_unlock(&dev->mode_config.mutex);
390 
391 	if (changed)
392 		drm_kms_helper_hotplug_event(dev);
393 }
394 
395 
396 /**
397  * intel_hpd_irq_handler - main hotplug irq handler
398  * @dev_priv: drm_i915_private
399  * @pin_mask: a mask of hpd pins that have triggered the irq
400  * @long_mask: a mask of hpd pins that may be long hpd pulses
401  *
402  * This is the main hotplug irq handler for all platforms. The platform specific
403  * irq handlers call the platform specific hotplug irq handlers, which read and
404  * decode the appropriate registers into bitmasks about hpd pins that have
405  * triggered (@pin_mask), and which of those pins may be long pulses
406  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
407  * is not a digital port.
408  *
409  * Here, we do hotplug irq storm detection and mitigation, and pass further
410  * processing to appropriate bottom halves.
411  */
412 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
413 			   u32 pin_mask, u32 long_mask)
414 {
415 	struct intel_encoder *encoder;
416 	bool storm_detected = false;
417 	bool queue_dig = false, queue_hp = false;
418 	u32 long_hpd_pulse_mask = 0;
419 	u32 short_hpd_pulse_mask = 0;
420 	enum hpd_pin pin;
421 
422 	if (!pin_mask)
423 		return;
424 
425 	spin_lock(&dev_priv->irq_lock);
426 
427 	/*
428 	 * Determine whether ->hpd_pulse() exists for each pin, and
429 	 * whether we have a short or a long pulse. This is needed
430 	 * as each pin may have up to two encoders (HDMI and DP) and
431 	 * only the one of them (DP) will have ->hpd_pulse().
432 	 */
433 	for_each_intel_encoder(&dev_priv->drm, encoder) {
434 		bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
435 		enum port port = encoder->port;
436 		bool long_hpd;
437 
438 		pin = encoder->hpd_pin;
439 		if (!(BIT(pin) & pin_mask))
440 			continue;
441 
442 		if (!has_hpd_pulse)
443 			continue;
444 
445 		long_hpd = long_mask & BIT(pin);
446 
447 		DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
448 				 long_hpd ? "long" : "short");
449 		queue_dig = true;
450 
451 		if (long_hpd) {
452 			long_hpd_pulse_mask |= BIT(pin);
453 			dev_priv->hotplug.long_port_mask |= BIT(port);
454 		} else {
455 			short_hpd_pulse_mask |= BIT(pin);
456 			dev_priv->hotplug.short_port_mask |= BIT(port);
457 		}
458 	}
459 
460 	/* Now process each pin just once */
461 	for_each_hpd_pin(pin) {
462 		bool long_hpd;
463 
464 		if (!(BIT(pin) & pin_mask))
465 			continue;
466 
467 		if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
468 			/*
469 			 * On GMCH platforms the interrupt mask bits only
470 			 * prevent irq generation, not the setting of the
471 			 * hotplug bits itself. So only WARN about unexpected
472 			 * interrupts on saner platforms.
473 			 */
474 			WARN_ONCE(!HAS_GMCH(dev_priv),
475 				  "Received HPD interrupt on pin %d although disabled\n", pin);
476 			continue;
477 		}
478 
479 		if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
480 			continue;
481 
482 		/*
483 		 * Delegate to ->hpd_pulse() if one of the encoders for this
484 		 * pin has it, otherwise let the hotplug_work deal with this
485 		 * pin directly.
486 		 */
487 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
488 			long_hpd = long_hpd_pulse_mask & BIT(pin);
489 		} else {
490 			dev_priv->hotplug.event_bits |= BIT(pin);
491 			long_hpd = true;
492 			queue_hp = true;
493 		}
494 
495 		if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
496 			dev_priv->hotplug.event_bits &= ~BIT(pin);
497 			storm_detected = true;
498 			queue_hp = true;
499 		}
500 	}
501 
502 	/*
503 	 * Disable any IRQs that storms were detected on. Polling enablement
504 	 * happens later in our hotplug work.
505 	 */
506 	if (storm_detected && dev_priv->display_irqs_enabled)
507 		dev_priv->display.hpd_irq_setup(dev_priv);
508 	spin_unlock(&dev_priv->irq_lock);
509 
510 	/*
511 	 * Our hotplug handler can grab modeset locks (by calling down into the
512 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
513 	 * queue for otherwise the flush_work in the pageflip code will
514 	 * deadlock.
515 	 */
516 	if (queue_dig)
517 		queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
518 	if (queue_hp)
519 		schedule_work(&dev_priv->hotplug.hotplug_work);
520 }
521 
522 /**
523  * intel_hpd_init - initializes and enables hpd support
524  * @dev_priv: i915 device instance
525  *
526  * This function enables the hotplug support. It requires that interrupts have
527  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
528  * poll request can run concurrently to other code, so locking rules must be
529  * obeyed.
530  *
531  * This is a separate step from interrupt enabling to simplify the locking rules
532  * in the driver load and resume code.
533  *
534  * Also see: intel_hpd_poll_init(), which enables connector polling
535  */
536 void intel_hpd_init(struct drm_i915_private *dev_priv)
537 {
538 	int i;
539 
540 	for_each_hpd_pin(i) {
541 		dev_priv->hotplug.stats[i].count = 0;
542 		dev_priv->hotplug.stats[i].state = HPD_ENABLED;
543 	}
544 
545 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
546 	schedule_work(&dev_priv->hotplug.poll_init_work);
547 
548 	/*
549 	 * Interrupt setup is already guaranteed to be single-threaded, this is
550 	 * just to make the assert_spin_locked checks happy.
551 	 */
552 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
553 		spin_lock_irq(&dev_priv->irq_lock);
554 		if (dev_priv->display_irqs_enabled)
555 			dev_priv->display.hpd_irq_setup(dev_priv);
556 		spin_unlock_irq(&dev_priv->irq_lock);
557 	}
558 }
559 
560 static void i915_hpd_poll_init_work(struct work_struct *work)
561 {
562 	struct drm_i915_private *dev_priv =
563 		container_of(work, struct drm_i915_private,
564 			     hotplug.poll_init_work);
565 	struct drm_device *dev = &dev_priv->drm;
566 	struct drm_connector *connector;
567 	struct drm_connector_list_iter conn_iter;
568 	bool enabled;
569 
570 	mutex_lock(&dev->mode_config.mutex);
571 
572 	enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
573 
574 	drm_connector_list_iter_begin(dev, &conn_iter);
575 	drm_for_each_connector_iter(connector, &conn_iter) {
576 		struct intel_connector *intel_connector =
577 			to_intel_connector(connector);
578 		connector->polled = intel_connector->polled;
579 
580 		/* MST has a dynamic intel_connector->encoder and it's reprobing
581 		 * is all handled by the MST helpers. */
582 		if (intel_connector->mst_port)
583 			continue;
584 
585 		if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
586 		    intel_connector->encoder->hpd_pin > HPD_NONE) {
587 			connector->polled = enabled ?
588 				DRM_CONNECTOR_POLL_CONNECT |
589 				DRM_CONNECTOR_POLL_DISCONNECT :
590 				DRM_CONNECTOR_POLL_HPD;
591 		}
592 	}
593 	drm_connector_list_iter_end(&conn_iter);
594 
595 	if (enabled)
596 		drm_kms_helper_poll_enable(dev);
597 
598 	mutex_unlock(&dev->mode_config.mutex);
599 
600 	/*
601 	 * We might have missed any hotplugs that happened while we were
602 	 * in the middle of disabling polling
603 	 */
604 	if (!enabled)
605 		drm_helper_hpd_irq_event(dev);
606 }
607 
608 /**
609  * intel_hpd_poll_init - enables/disables polling for connectors with hpd
610  * @dev_priv: i915 device instance
611  *
612  * This function enables polling for all connectors, regardless of whether or
613  * not they support hotplug detection. Under certain conditions HPD may not be
614  * functional. On most Intel GPUs, this happens when we enter runtime suspend.
615  * On Valleyview and Cherryview systems, this also happens when we shut off all
616  * of the powerwells.
617  *
618  * Since this function can get called in contexts where we're already holding
619  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
620  * worker.
621  *
622  * Also see: intel_hpd_init(), which restores hpd handling.
623  */
624 void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
625 {
626 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
627 
628 	/*
629 	 * We might already be holding dev->mode_config.mutex, so do this in a
630 	 * seperate worker
631 	 * As well, there's no issue if we race here since we always reschedule
632 	 * this worker anyway
633 	 */
634 	schedule_work(&dev_priv->hotplug.poll_init_work);
635 }
636 
637 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
638 {
639 	INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
640 	INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
641 	INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
642 	INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
643 			  intel_hpd_irq_storm_reenable_work);
644 }
645 
646 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
647 {
648 	spin_lock_irq(&dev_priv->irq_lock);
649 
650 	dev_priv->hotplug.long_port_mask = 0;
651 	dev_priv->hotplug.short_port_mask = 0;
652 	dev_priv->hotplug.event_bits = 0;
653 
654 	spin_unlock_irq(&dev_priv->irq_lock);
655 
656 	cancel_work_sync(&dev_priv->hotplug.dig_port_work);
657 	cancel_work_sync(&dev_priv->hotplug.hotplug_work);
658 	cancel_work_sync(&dev_priv->hotplug.poll_init_work);
659 	cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
660 }
661 
662 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
663 {
664 	bool ret = false;
665 
666 	if (pin == HPD_NONE)
667 		return false;
668 
669 	spin_lock_irq(&dev_priv->irq_lock);
670 	if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
671 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
672 		ret = true;
673 	}
674 	spin_unlock_irq(&dev_priv->irq_lock);
675 
676 	return ret;
677 }
678 
679 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
680 {
681 	if (pin == HPD_NONE)
682 		return;
683 
684 	spin_lock_irq(&dev_priv->irq_lock);
685 	dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
686 	spin_unlock_irq(&dev_priv->irq_lock);
687 }
688