Linux Audio

Check our new training course

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