Linux Audio

Check our new training course

Linux kernel drivers training

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