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