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