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