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/drmP.h>
27 #include <drm/i915_drm.h>
28 
29 #include "i915_drv.h"
30 #include "intel_drv.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  */
intel_hpd_pin_default(struct drm_i915_private * dev_priv,enum port port)89 enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
90 				   enum port port)
91 {
92 	switch (port) {
93 	case PORT_A:
94 		return HPD_PORT_A;
95 	case PORT_B:
96 		return HPD_PORT_B;
97 	case PORT_C:
98 		return HPD_PORT_C;
99 	case PORT_D:
100 		return HPD_PORT_D;
101 	case PORT_E:
102 		return HPD_PORT_E;
103 	case PORT_F:
104 		if (IS_CNL_WITH_PORT_F(dev_priv))
105 			return HPD_PORT_E;
106 		return HPD_PORT_F;
107 	default:
108 		MISSING_CASE(port);
109 		return HPD_NONE;
110 	}
111 }
112 
113 #define HPD_STORM_DETECT_PERIOD		1000
114 #define HPD_STORM_REENABLE_DELAY	(2 * 60 * 1000)
115 
116 /**
117  * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
118  * @dev_priv: private driver data pointer
119  * @pin: the pin to gather stats on
120  *
121  * Gather stats about HPD irqs from the specified @pin, and detect irq
122  * storms. Only the pin specific stats and state are changed, the caller is
123  * responsible for further action.
124  *
125  * The number of irqs that are allowed within @HPD_STORM_DETECT_PERIOD is
126  * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
127  * @HPD_STORM_DEFAULT_THRESHOLD. If this threshold is exceeded, it's
128  * considered an irq storm and the irq state is set to @HPD_MARK_DISABLED.
129  *
130  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
131  * and should only be adjusted for automated hotplug testing.
132  *
133  * Return true if an irq storm was detected on @pin.
134  */
intel_hpd_irq_storm_detect(struct drm_i915_private * dev_priv,enum hpd_pin pin)135 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
136 				       enum hpd_pin pin)
137 {
138 	unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
139 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
140 	const int threshold = dev_priv->hotplug.hpd_storm_threshold;
141 	bool storm = false;
142 
143 	if (!time_in_range(jiffies, start, end)) {
144 		dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
145 		dev_priv->hotplug.stats[pin].count = 0;
146 		DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
147 	} else if (dev_priv->hotplug.stats[pin].count > threshold &&
148 		   threshold) {
149 		dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
150 		DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
151 		storm = true;
152 	} else {
153 		dev_priv->hotplug.stats[pin].count++;
154 		DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
155 			      dev_priv->hotplug.stats[pin].count);
156 	}
157 
158 	return storm;
159 }
160 
intel_hpd_irq_storm_disable(struct drm_i915_private * dev_priv)161 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
162 {
163 	struct drm_device *dev = &dev_priv->drm;
164 	struct intel_connector *intel_connector;
165 	struct intel_encoder *intel_encoder;
166 	struct drm_connector *connector;
167 	struct drm_connector_list_iter conn_iter;
168 	enum hpd_pin pin;
169 	bool hpd_disabled = false;
170 
171 	lockdep_assert_held(&dev_priv->irq_lock);
172 
173 	drm_connector_list_iter_begin(dev, &conn_iter);
174 	drm_for_each_connector_iter(connector, &conn_iter) {
175 		if (connector->polled != DRM_CONNECTOR_POLL_HPD)
176 			continue;
177 
178 		intel_connector = to_intel_connector(connector);
179 		intel_encoder = intel_connector->encoder;
180 		if (!intel_encoder)
181 			continue;
182 
183 		pin = intel_encoder->hpd_pin;
184 		if (pin == HPD_NONE ||
185 		    dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
186 			continue;
187 
188 		DRM_INFO("HPD interrupt storm detected on connector %s: "
189 			 "switching from hotplug detection to polling\n",
190 			 connector->name);
191 
192 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
193 		connector->polled = DRM_CONNECTOR_POLL_CONNECT
194 			| DRM_CONNECTOR_POLL_DISCONNECT;
195 		hpd_disabled = true;
196 	}
197 	drm_connector_list_iter_end(&conn_iter);
198 
199 	/* Enable polling and queue hotplug re-enabling. */
200 	if (hpd_disabled) {
201 		drm_kms_helper_poll_enable(dev);
202 		mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
203 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
204 	}
205 }
206 
intel_hpd_irq_storm_reenable_work(struct work_struct * work)207 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
208 {
209 	struct drm_i915_private *dev_priv =
210 		container_of(work, typeof(*dev_priv),
211 			     hotplug.reenable_work.work);
212 	struct drm_device *dev = &dev_priv->drm;
213 	enum hpd_pin pin;
214 
215 	intel_runtime_pm_get(dev_priv);
216 
217 	spin_lock_irq(&dev_priv->irq_lock);
218 	for_each_hpd_pin(pin) {
219 		struct drm_connector *connector;
220 		struct drm_connector_list_iter conn_iter;
221 
222 		if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
223 			continue;
224 
225 		dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
226 
227 		drm_connector_list_iter_begin(dev, &conn_iter);
228 		drm_for_each_connector_iter(connector, &conn_iter) {
229 			struct intel_connector *intel_connector = to_intel_connector(connector);
230 
231 			if (intel_connector->encoder->hpd_pin == pin) {
232 				if (connector->polled != intel_connector->polled)
233 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
234 							 connector->name);
235 				connector->polled = intel_connector->polled;
236 				if (!connector->polled)
237 					connector->polled = DRM_CONNECTOR_POLL_HPD;
238 			}
239 		}
240 		drm_connector_list_iter_end(&conn_iter);
241 	}
242 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
243 		dev_priv->display.hpd_irq_setup(dev_priv);
244 	spin_unlock_irq(&dev_priv->irq_lock);
245 
246 	intel_runtime_pm_put(dev_priv);
247 }
248 
intel_encoder_hotplug(struct intel_encoder * encoder,struct intel_connector * connector)249 bool intel_encoder_hotplug(struct intel_encoder *encoder,
250 			   struct intel_connector *connector)
251 {
252 	struct drm_device *dev = connector->base.dev;
253 	enum drm_connector_status old_status;
254 
255 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
256 	old_status = connector->base.status;
257 
258 	connector->base.status =
259 		drm_helper_probe_detect(&connector->base, NULL, false);
260 
261 	if (old_status == connector->base.status)
262 		return false;
263 
264 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
265 		      connector->base.base.id,
266 		      connector->base.name,
267 		      drm_get_connector_status_name(old_status),
268 		      drm_get_connector_status_name(connector->base.status));
269 
270 	return true;
271 }
272 
intel_encoder_has_hpd_pulse(struct intel_encoder * encoder)273 static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
274 {
275 	return intel_encoder_is_dig_port(encoder) &&
276 		enc_to_dig_port(&encoder->base)->hpd_pulse != NULL;
277 }
278 
i915_digport_work_func(struct work_struct * work)279 static void i915_digport_work_func(struct work_struct *work)
280 {
281 	struct drm_i915_private *dev_priv =
282 		container_of(work, struct drm_i915_private, hotplug.dig_port_work);
283 	u32 long_port_mask, short_port_mask;
284 	struct intel_encoder *encoder;
285 	u32 old_bits = 0;
286 
287 	spin_lock_irq(&dev_priv->irq_lock);
288 	long_port_mask = dev_priv->hotplug.long_port_mask;
289 	dev_priv->hotplug.long_port_mask = 0;
290 	short_port_mask = dev_priv->hotplug.short_port_mask;
291 	dev_priv->hotplug.short_port_mask = 0;
292 	spin_unlock_irq(&dev_priv->irq_lock);
293 
294 	for_each_intel_encoder(&dev_priv->drm, encoder) {
295 		struct intel_digital_port *dig_port;
296 		enum port port = encoder->port;
297 		bool long_hpd, short_hpd;
298 		enum irqreturn ret;
299 
300 		if (!intel_encoder_has_hpd_pulse(encoder))
301 			continue;
302 
303 		long_hpd = long_port_mask & BIT(port);
304 		short_hpd = short_port_mask & BIT(port);
305 
306 		if (!long_hpd && !short_hpd)
307 			continue;
308 
309 		dig_port = enc_to_dig_port(&encoder->base);
310 
311 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
312 		if (ret == IRQ_NONE) {
313 			/* fall back to old school hpd */
314 			old_bits |= BIT(encoder->hpd_pin);
315 		}
316 	}
317 
318 	if (old_bits) {
319 		spin_lock_irq(&dev_priv->irq_lock);
320 		dev_priv->hotplug.event_bits |= old_bits;
321 		spin_unlock_irq(&dev_priv->irq_lock);
322 		schedule_work(&dev_priv->hotplug.hotplug_work);
323 	}
324 }
325 
326 /*
327  * Handle hotplug events outside the interrupt handler proper.
328  */
i915_hotplug_work_func(struct work_struct * work)329 static void i915_hotplug_work_func(struct work_struct *work)
330 {
331 	struct drm_i915_private *dev_priv =
332 		container_of(work, struct drm_i915_private, hotplug.hotplug_work);
333 	struct drm_device *dev = &dev_priv->drm;
334 	struct intel_connector *intel_connector;
335 	struct intel_encoder *intel_encoder;
336 	struct drm_connector *connector;
337 	struct drm_connector_list_iter conn_iter;
338 	bool changed = false;
339 	u32 hpd_event_bits;
340 
341 	mutex_lock(&dev->mode_config.mutex);
342 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
343 
344 	spin_lock_irq(&dev_priv->irq_lock);
345 
346 	hpd_event_bits = dev_priv->hotplug.event_bits;
347 	dev_priv->hotplug.event_bits = 0;
348 
349 	/* Disable hotplug on connectors that hit an irq storm. */
350 	intel_hpd_irq_storm_disable(dev_priv);
351 
352 	spin_unlock_irq(&dev_priv->irq_lock);
353 
354 	drm_connector_list_iter_begin(dev, &conn_iter);
355 	drm_for_each_connector_iter(connector, &conn_iter) {
356 		intel_connector = to_intel_connector(connector);
357 		if (!intel_connector->encoder)
358 			continue;
359 		intel_encoder = intel_connector->encoder;
360 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
361 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
362 				      connector->name, intel_encoder->hpd_pin);
363 
364 			changed |= intel_encoder->hotplug(intel_encoder,
365 							  intel_connector);
366 		}
367 	}
368 	drm_connector_list_iter_end(&conn_iter);
369 	mutex_unlock(&dev->mode_config.mutex);
370 
371 	if (changed)
372 		drm_kms_helper_hotplug_event(dev);
373 }
374 
375 
376 /**
377  * intel_hpd_irq_handler - main hotplug irq handler
378  * @dev_priv: drm_i915_private
379  * @pin_mask: a mask of hpd pins that have triggered the irq
380  * @long_mask: a mask of hpd pins that may be long hpd pulses
381  *
382  * This is the main hotplug irq handler for all platforms. The platform specific
383  * irq handlers call the platform specific hotplug irq handlers, which read and
384  * decode the appropriate registers into bitmasks about hpd pins that have
385  * triggered (@pin_mask), and which of those pins may be long pulses
386  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
387  * is not a digital port.
388  *
389  * Here, we do hotplug irq storm detection and mitigation, and pass further
390  * processing to appropriate bottom halves.
391  */
intel_hpd_irq_handler(struct drm_i915_private * dev_priv,u32 pin_mask,u32 long_mask)392 void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
393 			   u32 pin_mask, u32 long_mask)
394 {
395 	struct intel_encoder *encoder;
396 	bool storm_detected = false;
397 	bool queue_dig = false, queue_hp = false;
398 
399 	if (!pin_mask)
400 		return;
401 
402 	spin_lock(&dev_priv->irq_lock);
403 	for_each_intel_encoder(&dev_priv->drm, encoder) {
404 		enum hpd_pin pin = encoder->hpd_pin;
405 		bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
406 
407 		if (!(BIT(pin) & pin_mask))
408 			continue;
409 
410 		if (has_hpd_pulse) {
411 			bool long_hpd = long_mask & BIT(pin);
412 			enum port port = encoder->port;
413 
414 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
415 					 long_hpd ? "long" : "short");
416 			/*
417 			 * For long HPD pulses we want to have the digital queue happen,
418 			 * but we still want HPD storm detection to function.
419 			 */
420 			queue_dig = true;
421 			if (long_hpd) {
422 				dev_priv->hotplug.long_port_mask |= (1 << port);
423 			} else {
424 				/* for short HPD just trigger the digital queue */
425 				dev_priv->hotplug.short_port_mask |= (1 << port);
426 				continue;
427 			}
428 		}
429 
430 		if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
431 			/*
432 			 * On GMCH platforms the interrupt mask bits only
433 			 * prevent irq generation, not the setting of the
434 			 * hotplug bits itself. So only WARN about unexpected
435 			 * interrupts on saner platforms.
436 			 */
437 			WARN_ONCE(!HAS_GMCH_DISPLAY(dev_priv),
438 				  "Received HPD interrupt on pin %d although disabled\n", pin);
439 			continue;
440 		}
441 
442 		if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
443 			continue;
444 
445 		if (!has_hpd_pulse) {
446 			dev_priv->hotplug.event_bits |= BIT(pin);
447 			queue_hp = true;
448 		}
449 
450 		if (intel_hpd_irq_storm_detect(dev_priv, pin)) {
451 			dev_priv->hotplug.event_bits &= ~BIT(pin);
452 			storm_detected = true;
453 		}
454 	}
455 
456 	if (storm_detected && dev_priv->display_irqs_enabled)
457 		dev_priv->display.hpd_irq_setup(dev_priv);
458 	spin_unlock(&dev_priv->irq_lock);
459 
460 	/*
461 	 * Our hotplug handler can grab modeset locks (by calling down into the
462 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
463 	 * queue for otherwise the flush_work in the pageflip code will
464 	 * deadlock.
465 	 */
466 	if (queue_dig)
467 		queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
468 	if (queue_hp)
469 		schedule_work(&dev_priv->hotplug.hotplug_work);
470 }
471 
472 /**
473  * intel_hpd_init - initializes and enables hpd support
474  * @dev_priv: i915 device instance
475  *
476  * This function enables the hotplug support. It requires that interrupts have
477  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
478  * poll request can run concurrently to other code, so locking rules must be
479  * obeyed.
480  *
481  * This is a separate step from interrupt enabling to simplify the locking rules
482  * in the driver load and resume code.
483  *
484  * Also see: intel_hpd_poll_init(), which enables connector polling
485  */
intel_hpd_init(struct drm_i915_private * dev_priv)486 void intel_hpd_init(struct drm_i915_private *dev_priv)
487 {
488 	int i;
489 
490 	for_each_hpd_pin(i) {
491 		dev_priv->hotplug.stats[i].count = 0;
492 		dev_priv->hotplug.stats[i].state = HPD_ENABLED;
493 	}
494 
495 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
496 	schedule_work(&dev_priv->hotplug.poll_init_work);
497 
498 	/*
499 	 * Interrupt setup is already guaranteed to be single-threaded, this is
500 	 * just to make the assert_spin_locked checks happy.
501 	 */
502 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
503 		spin_lock_irq(&dev_priv->irq_lock);
504 		if (dev_priv->display_irqs_enabled)
505 			dev_priv->display.hpd_irq_setup(dev_priv);
506 		spin_unlock_irq(&dev_priv->irq_lock);
507 	}
508 }
509 
i915_hpd_poll_init_work(struct work_struct * work)510 static void i915_hpd_poll_init_work(struct work_struct *work)
511 {
512 	struct drm_i915_private *dev_priv =
513 		container_of(work, struct drm_i915_private,
514 			     hotplug.poll_init_work);
515 	struct drm_device *dev = &dev_priv->drm;
516 	struct drm_connector *connector;
517 	struct drm_connector_list_iter conn_iter;
518 	bool enabled;
519 
520 	mutex_lock(&dev->mode_config.mutex);
521 
522 	enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
523 
524 	drm_connector_list_iter_begin(dev, &conn_iter);
525 	drm_for_each_connector_iter(connector, &conn_iter) {
526 		struct intel_connector *intel_connector =
527 			to_intel_connector(connector);
528 		connector->polled = intel_connector->polled;
529 
530 		/* MST has a dynamic intel_connector->encoder and it's reprobing
531 		 * is all handled by the MST helpers. */
532 		if (intel_connector->mst_port)
533 			continue;
534 
535 		if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
536 		    intel_connector->encoder->hpd_pin > HPD_NONE) {
537 			connector->polled = enabled ?
538 				DRM_CONNECTOR_POLL_CONNECT |
539 				DRM_CONNECTOR_POLL_DISCONNECT :
540 				DRM_CONNECTOR_POLL_HPD;
541 		}
542 	}
543 	drm_connector_list_iter_end(&conn_iter);
544 
545 	if (enabled)
546 		drm_kms_helper_poll_enable(dev);
547 
548 	mutex_unlock(&dev->mode_config.mutex);
549 
550 	/*
551 	 * We might have missed any hotplugs that happened while we were
552 	 * in the middle of disabling polling
553 	 */
554 	if (!enabled)
555 		drm_helper_hpd_irq_event(dev);
556 }
557 
558 /**
559  * intel_hpd_poll_init - enables/disables polling for connectors with hpd
560  * @dev_priv: i915 device instance
561  *
562  * This function enables polling for all connectors, regardless of whether or
563  * not they support hotplug detection. Under certain conditions HPD may not be
564  * functional. On most Intel GPUs, this happens when we enter runtime suspend.
565  * On Valleyview and Cherryview systems, this also happens when we shut off all
566  * of the powerwells.
567  *
568  * Since this function can get called in contexts where we're already holding
569  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
570  * worker.
571  *
572  * Also see: intel_hpd_init(), which restores hpd handling.
573  */
intel_hpd_poll_init(struct drm_i915_private * dev_priv)574 void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
575 {
576 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
577 
578 	/*
579 	 * We might already be holding dev->mode_config.mutex, so do this in a
580 	 * seperate worker
581 	 * As well, there's no issue if we race here since we always reschedule
582 	 * this worker anyway
583 	 */
584 	schedule_work(&dev_priv->hotplug.poll_init_work);
585 }
586 
intel_hpd_init_work(struct drm_i915_private * dev_priv)587 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
588 {
589 	INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
590 	INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
591 	INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
592 	INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
593 			  intel_hpd_irq_storm_reenable_work);
594 }
595 
intel_hpd_cancel_work(struct drm_i915_private * dev_priv)596 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
597 {
598 	spin_lock_irq(&dev_priv->irq_lock);
599 
600 	dev_priv->hotplug.long_port_mask = 0;
601 	dev_priv->hotplug.short_port_mask = 0;
602 	dev_priv->hotplug.event_bits = 0;
603 
604 	spin_unlock_irq(&dev_priv->irq_lock);
605 
606 	cancel_work_sync(&dev_priv->hotplug.dig_port_work);
607 	cancel_work_sync(&dev_priv->hotplug.hotplug_work);
608 	cancel_work_sync(&dev_priv->hotplug.poll_init_work);
609 	cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
610 }
611 
intel_hpd_disable(struct drm_i915_private * dev_priv,enum hpd_pin pin)612 bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
613 {
614 	bool ret = false;
615 
616 	if (pin == HPD_NONE)
617 		return false;
618 
619 	spin_lock_irq(&dev_priv->irq_lock);
620 	if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
621 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
622 		ret = true;
623 	}
624 	spin_unlock_irq(&dev_priv->irq_lock);
625 
626 	return ret;
627 }
628 
intel_hpd_enable(struct drm_i915_private * dev_priv,enum hpd_pin pin)629 void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
630 {
631 	if (pin == HPD_NONE)
632 		return;
633 
634 	spin_lock_irq(&dev_priv->irq_lock);
635 	dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
636 	spin_unlock_irq(&dev_priv->irq_lock);
637 }
638