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