xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_hotplug.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2015 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20df0566a6SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21df0566a6SJani Nikula  * IN THE SOFTWARE.
22df0566a6SJani Nikula  */
23df0566a6SJani Nikula 
24df0566a6SJani Nikula #include <linux/kernel.h>
25df0566a6SJani Nikula 
26df0566a6SJani Nikula #include "i915_drv.h"
277e97596cSJani Nikula #include "i915_irq.h"
281d455f8dSJani Nikula #include "intel_display_types.h"
29df0566a6SJani Nikula #include "intel_hotplug.h"
30da38ba98SJani Nikula #include "intel_hotplug_irq.h"
31df0566a6SJani Nikula 
32df0566a6SJani Nikula /**
33df0566a6SJani Nikula  * DOC: Hotplug
34df0566a6SJani Nikula  *
35df0566a6SJani Nikula  * Simply put, hotplug occurs when a display is connected to or disconnected
36df0566a6SJani Nikula  * from the system. However, there may be adapters and docking stations and
37df0566a6SJani Nikula  * Display Port short pulses and MST devices involved, complicating matters.
38df0566a6SJani Nikula  *
39df0566a6SJani Nikula  * Hotplug in i915 is handled in many different levels of abstraction.
40df0566a6SJani Nikula  *
41df0566a6SJani Nikula  * The platform dependent interrupt handling code in i915_irq.c enables,
42df0566a6SJani Nikula  * disables, and does preliminary handling of the interrupts. The interrupt
43df0566a6SJani Nikula  * handlers gather the hotplug detect (HPD) information from relevant registers
44df0566a6SJani Nikula  * into a platform independent mask of hotplug pins that have fired.
45df0566a6SJani Nikula  *
46df0566a6SJani Nikula  * The platform independent interrupt handler intel_hpd_irq_handler() in
47df0566a6SJani Nikula  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
48df0566a6SJani Nikula  * further processing to appropriate bottom halves (Display Port specific and
49df0566a6SJani Nikula  * regular hotplug).
50df0566a6SJani Nikula  *
51df0566a6SJani Nikula  * The Display Port work function i915_digport_work_func() calls into
52df0566a6SJani Nikula  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
53df0566a6SJani Nikula  * pulses, with failures and non-MST long pulses triggering regular hotplug
54df0566a6SJani Nikula  * processing on the connector.
55df0566a6SJani Nikula  *
56df0566a6SJani Nikula  * The regular hotplug work function i915_hotplug_work_func() calls connector
57df0566a6SJani Nikula  * detect hooks, and, if connector status changes, triggers sending of hotplug
58df0566a6SJani Nikula  * uevent to userspace via drm_kms_helper_hotplug_event().
59df0566a6SJani Nikula  *
60df0566a6SJani Nikula  * Finally, the userspace is responsible for triggering a modeset upon receiving
61df0566a6SJani Nikula  * the hotplug uevent, disabling or enabling the crtc as needed.
62df0566a6SJani Nikula  *
63df0566a6SJani Nikula  * The hotplug interrupt storm detection and mitigation code keeps track of the
64df0566a6SJani Nikula  * number of interrupts per hotplug pin per a period of time, and if the number
65df0566a6SJani Nikula  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
66df0566a6SJani Nikula  * while before being re-enabled. The intention is to mitigate issues raising
67df0566a6SJani Nikula  * from broken hardware triggering massive amounts of interrupts and grinding
68df0566a6SJani Nikula  * the system to a halt.
69df0566a6SJani Nikula  *
70df0566a6SJani Nikula  * Current implementation expects that hotplug interrupt storm will not be
71df0566a6SJani Nikula  * seen when display port sink is connected, hence on platforms whose DP
72df0566a6SJani Nikula  * callback is handled by i915_digport_work_func reenabling of hpd is not
73df0566a6SJani Nikula  * performed (it was never expected to be disabled in the first place ;) )
74df0566a6SJani Nikula  * this is specific to DP sinks handled by this routine and any other display
75df0566a6SJani Nikula  * such as HDMI or DVI enabled on the same port will have proper logic since
76df0566a6SJani Nikula  * it will use i915_hotplug_work_func where this logic is handled.
77df0566a6SJani Nikula  */
78df0566a6SJani Nikula 
79df0566a6SJani Nikula /**
80df0566a6SJani Nikula  * intel_hpd_pin_default - return default pin associated with certain port.
81df0566a6SJani Nikula  * @dev_priv: private driver data pointer
82df0566a6SJani Nikula  * @port: the hpd port to get associated pin
83df0566a6SJani Nikula  *
84df0566a6SJani Nikula  * It is only valid and used by digital port encoder.
85df0566a6SJani Nikula  *
86da51e4baSVille Syrjälä  * Return pin that is associatade with @port.
87df0566a6SJani Nikula  */
intel_hpd_pin_default(struct drm_i915_private * dev_priv,enum port port)88df0566a6SJani Nikula enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
89df0566a6SJani Nikula 				   enum port port)
90df0566a6SJani Nikula {
912cf12207SMatt Roper 	return HPD_PORT_A + port - PORT_A;
92df0566a6SJani Nikula }
93df0566a6SJani Nikula 
94dd890d42SJani Nikula /* Threshold == 5 for long IRQs, 50 for short */
95dd890d42SJani Nikula #define HPD_STORM_DEFAULT_THRESHOLD	50
96dd890d42SJani Nikula 
97df0566a6SJani Nikula #define HPD_STORM_DETECT_PERIOD		1000
98df0566a6SJani Nikula #define HPD_STORM_REENABLE_DELAY	(2 * 60 * 1000)
993944709dSImre Deak #define HPD_RETRY_DELAY			1000
100df0566a6SJani Nikula 
101012ee690SVille Syrjälä static enum hpd_pin
intel_connector_hpd_pin(struct intel_connector * connector)102012ee690SVille Syrjälä intel_connector_hpd_pin(struct intel_connector *connector)
103012ee690SVille Syrjälä {
104012ee690SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(connector);
105012ee690SVille Syrjälä 
106012ee690SVille Syrjälä 	/*
107012ee690SVille Syrjälä 	 * MST connectors get their encoder attached dynamically
108012ee690SVille Syrjälä 	 * so need to make sure we have an encoder here. But since
109012ee690SVille Syrjälä 	 * MST encoders have their hpd_pin set to HPD_NONE we don't
110012ee690SVille Syrjälä 	 * have to special case them beyond that.
111012ee690SVille Syrjälä 	 */
112012ee690SVille Syrjälä 	return encoder ? encoder->hpd_pin : HPD_NONE;
113012ee690SVille Syrjälä }
114012ee690SVille Syrjälä 
115df0566a6SJani Nikula /**
116df0566a6SJani Nikula  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
117df0566a6SJani Nikula  * @dev_priv: private driver data pointer
118df0566a6SJani Nikula  * @pin: the pin to gather stats on
119df0566a6SJani Nikula  * @long_hpd: whether the HPD IRQ was long or short
120df0566a6SJani Nikula  *
121df0566a6SJani Nikula  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
122df0566a6SJani Nikula  * storms. Only the pin specific stats and state are changed, the caller is
123df0566a6SJani Nikula  * responsible for further action.
124df0566a6SJani Nikula  *
125df0566a6SJani Nikula  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
1265a4dd6f0SJani Nikula  * stored in @dev_priv->display.hotplug.hpd_storm_threshold which defaults to
127df0566a6SJani Nikula  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
128df0566a6SJani Nikula  * short IRQs count as +1. If this threshold is exceeded, it's considered an
129df0566a6SJani Nikula  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
130df0566a6SJani Nikula  *
131df0566a6SJani Nikula  * By default, most systems will only count long IRQs towards
1325a4dd6f0SJani Nikula  * &dev_priv->display.hotplug.hpd_storm_threshold. However, some older systems also
133df0566a6SJani Nikula  * suffer from short IRQ storms and must also track these. Because short IRQ
134df0566a6SJani Nikula  * storms are naturally caused by sideband interactions with DP MST devices,
135df0566a6SJani Nikula  * short IRQ detection is only enabled for systems without DP MST support.
136df0566a6SJani Nikula  * Systems which are new enough to support DP MST are far less likely to
137df0566a6SJani Nikula  * suffer from IRQ storms at all, so this is fine.
138df0566a6SJani Nikula  *
139df0566a6SJani Nikula  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
140df0566a6SJani Nikula  * and should only be adjusted for automated hotplug testing.
141df0566a6SJani Nikula  *
142df0566a6SJani Nikula  * Return true if an IRQ storm was detected on @pin.
143df0566a6SJani Nikula  */
intel_hpd_irq_storm_detect(struct drm_i915_private * dev_priv,enum hpd_pin pin,bool long_hpd)144df0566a6SJani Nikula static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
145df0566a6SJani Nikula 				       enum hpd_pin pin, bool long_hpd)
146df0566a6SJani Nikula {
1475a4dd6f0SJani Nikula 	struct intel_hotplug *hpd = &dev_priv->display.hotplug;
148df0566a6SJani Nikula 	unsigned long start = hpd->stats[pin].last_jiffies;
149df0566a6SJani Nikula 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
150df0566a6SJani Nikula 	const int increment = long_hpd ? 10 : 1;
151df0566a6SJani Nikula 	const int threshold = hpd->hpd_storm_threshold;
152df0566a6SJani Nikula 	bool storm = false;
153df0566a6SJani Nikula 
154df0566a6SJani Nikula 	if (!threshold ||
1555a4dd6f0SJani Nikula 	    (!long_hpd && !dev_priv->display.hotplug.hpd_short_storm_enabled))
156df0566a6SJani Nikula 		return false;
157df0566a6SJani Nikula 
158df0566a6SJani Nikula 	if (!time_in_range(jiffies, start, end)) {
159df0566a6SJani Nikula 		hpd->stats[pin].last_jiffies = jiffies;
160df0566a6SJani Nikula 		hpd->stats[pin].count = 0;
161df0566a6SJani Nikula 	}
162df0566a6SJani Nikula 
163df0566a6SJani Nikula 	hpd->stats[pin].count += increment;
164df0566a6SJani Nikula 	if (hpd->stats[pin].count > threshold) {
165df0566a6SJani Nikula 		hpd->stats[pin].state = HPD_MARK_DISABLED;
1661084f5c8SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1671084f5c8SWambui Karuga 			    "HPD interrupt storm detected on PIN %d\n", pin);
168df0566a6SJani Nikula 		storm = true;
169df0566a6SJani Nikula 	} else {
1701084f5c8SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1711084f5c8SWambui Karuga 			    "Received HPD interrupt on PIN %d - cnt: %d\n",
1721084f5c8SWambui Karuga 			      pin,
173df0566a6SJani Nikula 			      hpd->stats[pin].count);
174df0566a6SJani Nikula 	}
175df0566a6SJani Nikula 
176df0566a6SJani Nikula 	return storm;
177df0566a6SJani Nikula }
178df0566a6SJani Nikula 
179df0566a6SJani Nikula static void
intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private * dev_priv)180df0566a6SJani Nikula intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
181df0566a6SJani Nikula {
182df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
18370a221b2SVille Syrjälä 	struct intel_connector *connector;
184df0566a6SJani Nikula 	bool hpd_disabled = false;
185df0566a6SJani Nikula 
186df0566a6SJani Nikula 	lockdep_assert_held(&dev_priv->irq_lock);
187df0566a6SJani Nikula 
1883703060dSAndrzej Hajda 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
18970a221b2SVille Syrjälä 	for_each_intel_connector_iter(connector, &conn_iter) {
19070a221b2SVille Syrjälä 		enum hpd_pin pin;
19170a221b2SVille Syrjälä 
19270a221b2SVille Syrjälä 		if (connector->base.polled != DRM_CONNECTOR_POLL_HPD)
193df0566a6SJani Nikula 			continue;
194df0566a6SJani Nikula 
195012ee690SVille Syrjälä 		pin = intel_connector_hpd_pin(connector);
196df0566a6SJani Nikula 		if (pin == HPD_NONE ||
1975a4dd6f0SJani Nikula 		    dev_priv->display.hotplug.stats[pin].state != HPD_MARK_DISABLED)
198df0566a6SJani Nikula 			continue;
199df0566a6SJani Nikula 
2001084f5c8SWambui Karuga 		drm_info(&dev_priv->drm,
2011084f5c8SWambui Karuga 			 "HPD interrupt storm detected on connector %s: "
202df0566a6SJani Nikula 			 "switching from hotplug detection to polling\n",
20370a221b2SVille Syrjälä 			 connector->base.name);
204df0566a6SJani Nikula 
2055a4dd6f0SJani Nikula 		dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
20670a221b2SVille Syrjälä 		connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
20770a221b2SVille Syrjälä 			DRM_CONNECTOR_POLL_DISCONNECT;
208df0566a6SJani Nikula 		hpd_disabled = true;
209df0566a6SJani Nikula 	}
210df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
211df0566a6SJani Nikula 
212df0566a6SJani Nikula 	/* Enable polling and queue hotplug re-enabling. */
213df0566a6SJani Nikula 	if (hpd_disabled) {
214*cfd48ad8SImre Deak 		drm_kms_helper_poll_reschedule(&dev_priv->drm);
215848a4e5cSLuca Coelho 		mod_delayed_work(dev_priv->unordered_wq,
216848a4e5cSLuca Coelho 				 &dev_priv->display.hotplug.reenable_work,
217df0566a6SJani Nikula 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
218df0566a6SJani Nikula 	}
219df0566a6SJani Nikula }
220df0566a6SJani Nikula 
intel_hpd_irq_storm_reenable_work(struct work_struct * work)221df0566a6SJani Nikula static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
222df0566a6SJani Nikula {
223df0566a6SJani Nikula 	struct drm_i915_private *dev_priv =
224df0566a6SJani Nikula 		container_of(work, typeof(*dev_priv),
2255a4dd6f0SJani Nikula 			     display.hotplug.reenable_work.work);
22632e3ae50SVille Syrjälä 	struct drm_connector_list_iter conn_iter;
22732e3ae50SVille Syrjälä 	struct intel_connector *connector;
228df0566a6SJani Nikula 	intel_wakeref_t wakeref;
229df0566a6SJani Nikula 	enum hpd_pin pin;
230df0566a6SJani Nikula 
231df0566a6SJani Nikula 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
232df0566a6SJani Nikula 
233df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
234df0566a6SJani Nikula 
2353703060dSAndrzej Hajda 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
23670a221b2SVille Syrjälä 	for_each_intel_connector_iter(connector, &conn_iter) {
23732e3ae50SVille Syrjälä 		pin = intel_connector_hpd_pin(connector);
23832e3ae50SVille Syrjälä 		if (pin == HPD_NONE ||
2395a4dd6f0SJani Nikula 		    dev_priv->display.hotplug.stats[pin].state != HPD_DISABLED)
24032e3ae50SVille Syrjälä 			continue;
24132e3ae50SVille Syrjälä 
24270a221b2SVille Syrjälä 		if (connector->base.polled != connector->polled)
2431084f5c8SWambui Karuga 			drm_dbg(&dev_priv->drm,
2441084f5c8SWambui Karuga 				"Reenabling HPD on connector %s\n",
24570a221b2SVille Syrjälä 				connector->base.name);
24670a221b2SVille Syrjälä 		connector->base.polled = connector->polled;
247df0566a6SJani Nikula 	}
248df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
24932e3ae50SVille Syrjälä 
25032e3ae50SVille Syrjälä 	for_each_hpd_pin(pin) {
2515a4dd6f0SJani Nikula 		if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED)
2525a4dd6f0SJani Nikula 			dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
253df0566a6SJani Nikula 	}
25432e3ae50SVille Syrjälä 
2555f51e78fSVille Syrjälä 	intel_hpd_irq_setup(dev_priv);
25632e3ae50SVille Syrjälä 
257df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
258df0566a6SJani Nikula 
259df0566a6SJani Nikula 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
260df0566a6SJani Nikula }
261df0566a6SJani Nikula 
2623944709dSImre Deak enum intel_hotplug_state
intel_encoder_hotplug(struct intel_encoder * encoder,struct intel_connector * connector)2633944709dSImre Deak intel_encoder_hotplug(struct intel_encoder *encoder,
2648c8919c7SImre Deak 		      struct intel_connector *connector)
265df0566a6SJani Nikula {
266df0566a6SJani Nikula 	struct drm_device *dev = connector->base.dev;
267df0566a6SJani Nikula 	enum drm_connector_status old_status;
26835205ee9SStanislav Lisovskiy 	u64 old_epoch_counter;
26935205ee9SStanislav Lisovskiy 	bool ret = false;
270df0566a6SJani Nikula 
271f4224a4cSPankaj Bharadiya 	drm_WARN_ON(dev, !mutex_is_locked(&dev->mode_config.mutex));
272df0566a6SJani Nikula 	old_status = connector->base.status;
273c3bad0c7SImre Deak 	old_epoch_counter = connector->base.epoch_counter;
274df0566a6SJani Nikula 
275df0566a6SJani Nikula 	connector->base.status =
276df0566a6SJani Nikula 		drm_helper_probe_detect(&connector->base, NULL, false);
277df0566a6SJani Nikula 
27835205ee9SStanislav Lisovskiy 	if (old_epoch_counter != connector->base.epoch_counter)
27935205ee9SStanislav Lisovskiy 		ret = true;
280df0566a6SJani Nikula 
28135205ee9SStanislav Lisovskiy 	if (ret) {
2820bd6c4a1SJani Nikula 		drm_dbg_kms(dev, "[CONNECTOR:%d:%s] status updated from %s to %s (epoch counter %llu->%llu)\n",
283df0566a6SJani Nikula 			    connector->base.base.id,
284df0566a6SJani Nikula 			    connector->base.name,
285df0566a6SJani Nikula 			    drm_get_connector_status_name(old_status),
28635205ee9SStanislav Lisovskiy 			    drm_get_connector_status_name(connector->base.status),
287c3bad0c7SImre Deak 			    old_epoch_counter,
28835205ee9SStanislav Lisovskiy 			    connector->base.epoch_counter);
2893944709dSImre Deak 		return INTEL_HOTPLUG_CHANGED;
290df0566a6SJani Nikula 	}
29135205ee9SStanislav Lisovskiy 	return INTEL_HOTPLUG_UNCHANGED;
29235205ee9SStanislav Lisovskiy }
293df0566a6SJani Nikula 
intel_encoder_has_hpd_pulse(struct intel_encoder * encoder)294df0566a6SJani Nikula static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
295df0566a6SJani Nikula {
296df0566a6SJani Nikula 	return intel_encoder_is_dig_port(encoder) &&
297b7d02c3aSVille Syrjälä 		enc_to_dig_port(encoder)->hpd_pulse != NULL;
298df0566a6SJani Nikula }
299df0566a6SJani Nikula 
i915_digport_work_func(struct work_struct * work)300df0566a6SJani Nikula static void i915_digport_work_func(struct work_struct *work)
301df0566a6SJani Nikula {
302df0566a6SJani Nikula 	struct drm_i915_private *dev_priv =
3035a4dd6f0SJani Nikula 		container_of(work, struct drm_i915_private, display.hotplug.dig_port_work);
304df0566a6SJani Nikula 	u32 long_port_mask, short_port_mask;
305df0566a6SJani Nikula 	struct intel_encoder *encoder;
306df0566a6SJani Nikula 	u32 old_bits = 0;
307df0566a6SJani Nikula 
308df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
3095a4dd6f0SJani Nikula 	long_port_mask = dev_priv->display.hotplug.long_port_mask;
3105a4dd6f0SJani Nikula 	dev_priv->display.hotplug.long_port_mask = 0;
3115a4dd6f0SJani Nikula 	short_port_mask = dev_priv->display.hotplug.short_port_mask;
3125a4dd6f0SJani Nikula 	dev_priv->display.hotplug.short_port_mask = 0;
313df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
314df0566a6SJani Nikula 
315df0566a6SJani Nikula 	for_each_intel_encoder(&dev_priv->drm, encoder) {
316df0566a6SJani Nikula 		struct intel_digital_port *dig_port;
317df0566a6SJani Nikula 		enum port port = encoder->port;
318df0566a6SJani Nikula 		bool long_hpd, short_hpd;
319df0566a6SJani Nikula 		enum irqreturn ret;
320df0566a6SJani Nikula 
321df0566a6SJani Nikula 		if (!intel_encoder_has_hpd_pulse(encoder))
322df0566a6SJani Nikula 			continue;
323df0566a6SJani Nikula 
324df0566a6SJani Nikula 		long_hpd = long_port_mask & BIT(port);
325df0566a6SJani Nikula 		short_hpd = short_port_mask & BIT(port);
326df0566a6SJani Nikula 
327df0566a6SJani Nikula 		if (!long_hpd && !short_hpd)
328df0566a6SJani Nikula 			continue;
329df0566a6SJani Nikula 
330b7d02c3aSVille Syrjälä 		dig_port = enc_to_dig_port(encoder);
331df0566a6SJani Nikula 
332df0566a6SJani Nikula 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
333df0566a6SJani Nikula 		if (ret == IRQ_NONE) {
334df0566a6SJani Nikula 			/* fall back to old school hpd */
335df0566a6SJani Nikula 			old_bits |= BIT(encoder->hpd_pin);
336df0566a6SJani Nikula 		}
337df0566a6SJani Nikula 	}
338df0566a6SJani Nikula 
339df0566a6SJani Nikula 	if (old_bits) {
340df0566a6SJani Nikula 		spin_lock_irq(&dev_priv->irq_lock);
3415a4dd6f0SJani Nikula 		dev_priv->display.hotplug.event_bits |= old_bits;
342df0566a6SJani Nikula 		spin_unlock_irq(&dev_priv->irq_lock);
343848a4e5cSLuca Coelho 		queue_delayed_work(dev_priv->unordered_wq,
344848a4e5cSLuca Coelho 				   &dev_priv->display.hotplug.hotplug_work, 0);
345df0566a6SJani Nikula 	}
346df0566a6SJani Nikula }
347df0566a6SJani Nikula 
348471bdd0dSImre Deak /**
349471bdd0dSImre Deak  * intel_hpd_trigger_irq - trigger an hpd irq event for a port
350471bdd0dSImre Deak  * @dig_port: digital port
351471bdd0dSImre Deak  *
352471bdd0dSImre Deak  * Trigger an HPD interrupt event for the given port, emulating a short pulse
353471bdd0dSImre Deak  * generated by the sink, and schedule the dig port work to handle it.
354471bdd0dSImre Deak  */
intel_hpd_trigger_irq(struct intel_digital_port * dig_port)355471bdd0dSImre Deak void intel_hpd_trigger_irq(struct intel_digital_port *dig_port)
356471bdd0dSImre Deak {
357471bdd0dSImre Deak 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
358471bdd0dSImre Deak 
359471bdd0dSImre Deak 	spin_lock_irq(&i915->irq_lock);
3605a4dd6f0SJani Nikula 	i915->display.hotplug.short_port_mask |= BIT(dig_port->base.port);
361471bdd0dSImre Deak 	spin_unlock_irq(&i915->irq_lock);
362471bdd0dSImre Deak 
3635a4dd6f0SJani Nikula 	queue_work(i915->display.hotplug.dp_wq, &i915->display.hotplug.dig_port_work);
364471bdd0dSImre Deak }
365471bdd0dSImre Deak 
366df0566a6SJani Nikula /*
367df0566a6SJani Nikula  * Handle hotplug events outside the interrupt handler proper.
368df0566a6SJani Nikula  */
i915_hotplug_work_func(struct work_struct * work)369df0566a6SJani Nikula static void i915_hotplug_work_func(struct work_struct *work)
370df0566a6SJani Nikula {
371df0566a6SJani Nikula 	struct drm_i915_private *dev_priv =
3723944709dSImre Deak 		container_of(work, struct drm_i915_private,
3735a4dd6f0SJani Nikula 			     display.hotplug.hotplug_work.work);
374df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
37570a221b2SVille Syrjälä 	struct intel_connector *connector;
3763944709dSImre Deak 	u32 changed = 0, retry = 0;
377df0566a6SJani Nikula 	u32 hpd_event_bits;
3783944709dSImre Deak 	u32 hpd_retry_bits;
379a811c2e4SSimon Ser 	struct drm_connector *first_changed_connector = NULL;
380a811c2e4SSimon Ser 	int changed_connectors = 0;
381df0566a6SJani Nikula 
3823703060dSAndrzej Hajda 	mutex_lock(&dev_priv->drm.mode_config.mutex);
3831084f5c8SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "running encoder hotplug functions\n");
384df0566a6SJani Nikula 
385df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
386df0566a6SJani Nikula 
3875a4dd6f0SJani Nikula 	hpd_event_bits = dev_priv->display.hotplug.event_bits;
3885a4dd6f0SJani Nikula 	dev_priv->display.hotplug.event_bits = 0;
3895a4dd6f0SJani Nikula 	hpd_retry_bits = dev_priv->display.hotplug.retry_bits;
3905a4dd6f0SJani Nikula 	dev_priv->display.hotplug.retry_bits = 0;
391df0566a6SJani Nikula 
392df0566a6SJani Nikula 	/* Enable polling for connectors which had HPD IRQ storms */
393df0566a6SJani Nikula 	intel_hpd_irq_storm_switch_to_polling(dev_priv);
394df0566a6SJani Nikula 
395df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
396df0566a6SJani Nikula 
397b0fbef65SVinod Govindapillai 	/* Skip calling encode hotplug handlers if ignore long HPD set*/
398b0fbef65SVinod Govindapillai 	if (dev_priv->display.hotplug.ignore_long_hpd) {
399b0fbef65SVinod Govindapillai 		drm_dbg_kms(&dev_priv->drm, "Ignore HPD flag on - skip encoder hotplug handlers\n");
400b0fbef65SVinod Govindapillai 		mutex_unlock(&dev_priv->drm.mode_config.mutex);
401b0fbef65SVinod Govindapillai 		return;
402b0fbef65SVinod Govindapillai 	}
403b0fbef65SVinod Govindapillai 
4043703060dSAndrzej Hajda 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
40570a221b2SVille Syrjälä 	for_each_intel_connector_iter(connector, &conn_iter) {
406012ee690SVille Syrjälä 		enum hpd_pin pin;
4073944709dSImre Deak 		u32 hpd_bit;
4083944709dSImre Deak 
409012ee690SVille Syrjälä 		pin = intel_connector_hpd_pin(connector);
410012ee690SVille Syrjälä 		if (pin == HPD_NONE)
411df0566a6SJani Nikula 			continue;
41270a221b2SVille Syrjälä 
413012ee690SVille Syrjälä 		hpd_bit = BIT(pin);
4143944709dSImre Deak 		if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
415012ee690SVille Syrjälä 			struct intel_encoder *encoder =
416012ee690SVille Syrjälä 				intel_attached_encoder(connector);
417012ee690SVille Syrjälä 
4188c8919c7SImre Deak 			if (hpd_event_bits & hpd_bit)
4198c8919c7SImre Deak 				connector->hotplug_retries = 0;
4208c8919c7SImre Deak 			else
4218c8919c7SImre Deak 				connector->hotplug_retries++;
422df0566a6SJani Nikula 
4238c8919c7SImre Deak 			drm_dbg_kms(&dev_priv->drm,
4248c8919c7SImre Deak 				    "Connector %s (pin %i) received hotplug event. (retry %d)\n",
4258c8919c7SImre Deak 				    connector->base.name, pin,
4268c8919c7SImre Deak 				    connector->hotplug_retries);
4278c8919c7SImre Deak 
4288c8919c7SImre Deak 			switch (encoder->hotplug(encoder, connector)) {
4293944709dSImre Deak 			case INTEL_HOTPLUG_UNCHANGED:
4303944709dSImre Deak 				break;
4313944709dSImre Deak 			case INTEL_HOTPLUG_CHANGED:
4323944709dSImre Deak 				changed |= hpd_bit;
433a811c2e4SSimon Ser 				changed_connectors++;
434a811c2e4SSimon Ser 				if (!first_changed_connector) {
435a811c2e4SSimon Ser 					drm_connector_get(&connector->base);
436a811c2e4SSimon Ser 					first_changed_connector = &connector->base;
437a811c2e4SSimon Ser 				}
4383944709dSImre Deak 				break;
4393944709dSImre Deak 			case INTEL_HOTPLUG_RETRY:
4403944709dSImre Deak 				retry |= hpd_bit;
4413944709dSImre Deak 				break;
4423944709dSImre Deak 			}
443df0566a6SJani Nikula 		}
444df0566a6SJani Nikula 	}
445df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
4463703060dSAndrzej Hajda 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
447df0566a6SJani Nikula 
448a811c2e4SSimon Ser 	if (changed_connectors == 1)
449a811c2e4SSimon Ser 		drm_kms_helper_connector_hotplug_event(first_changed_connector);
450a811c2e4SSimon Ser 	else if (changed_connectors > 0)
4513703060dSAndrzej Hajda 		drm_kms_helper_hotplug_event(&dev_priv->drm);
4523944709dSImre Deak 
453a811c2e4SSimon Ser 	if (first_changed_connector)
454a811c2e4SSimon Ser 		drm_connector_put(first_changed_connector);
455a811c2e4SSimon Ser 
4563944709dSImre Deak 	/* Remove shared HPD pins that have changed */
4573944709dSImre Deak 	retry &= ~changed;
4583944709dSImre Deak 	if (retry) {
4593944709dSImre Deak 		spin_lock_irq(&dev_priv->irq_lock);
4605a4dd6f0SJani Nikula 		dev_priv->display.hotplug.retry_bits |= retry;
4613944709dSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
4623944709dSImre Deak 
463848a4e5cSLuca Coelho 		mod_delayed_work(dev_priv->unordered_wq,
464848a4e5cSLuca Coelho 				 &dev_priv->display.hotplug.hotplug_work,
4653944709dSImre Deak 				 msecs_to_jiffies(HPD_RETRY_DELAY));
4663944709dSImre Deak 	}
467df0566a6SJani Nikula }
468df0566a6SJani Nikula 
469df0566a6SJani Nikula 
470df0566a6SJani Nikula /**
471df0566a6SJani Nikula  * intel_hpd_irq_handler - main hotplug irq handler
472df0566a6SJani Nikula  * @dev_priv: drm_i915_private
473df0566a6SJani Nikula  * @pin_mask: a mask of hpd pins that have triggered the irq
474df0566a6SJani Nikula  * @long_mask: a mask of hpd pins that may be long hpd pulses
475df0566a6SJani Nikula  *
476df0566a6SJani Nikula  * This is the main hotplug irq handler for all platforms. The platform specific
477df0566a6SJani Nikula  * irq handlers call the platform specific hotplug irq handlers, which read and
478df0566a6SJani Nikula  * decode the appropriate registers into bitmasks about hpd pins that have
479df0566a6SJani Nikula  * triggered (@pin_mask), and which of those pins may be long pulses
480df0566a6SJani Nikula  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
481df0566a6SJani Nikula  * is not a digital port.
482df0566a6SJani Nikula  *
483df0566a6SJani Nikula  * Here, we do hotplug irq storm detection and mitigation, and pass further
484df0566a6SJani Nikula  * processing to appropriate bottom halves.
485df0566a6SJani Nikula  */
intel_hpd_irq_handler(struct drm_i915_private * dev_priv,u32 pin_mask,u32 long_mask)486df0566a6SJani Nikula void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
487df0566a6SJani Nikula 			   u32 pin_mask, u32 long_mask)
488df0566a6SJani Nikula {
489df0566a6SJani Nikula 	struct intel_encoder *encoder;
490df0566a6SJani Nikula 	bool storm_detected = false;
491df0566a6SJani Nikula 	bool queue_dig = false, queue_hp = false;
492df0566a6SJani Nikula 	u32 long_hpd_pulse_mask = 0;
493df0566a6SJani Nikula 	u32 short_hpd_pulse_mask = 0;
494df0566a6SJani Nikula 	enum hpd_pin pin;
495df0566a6SJani Nikula 
496df0566a6SJani Nikula 	if (!pin_mask)
497df0566a6SJani Nikula 		return;
498df0566a6SJani Nikula 
499df0566a6SJani Nikula 	spin_lock(&dev_priv->irq_lock);
500df0566a6SJani Nikula 
501df0566a6SJani Nikula 	/*
502df0566a6SJani Nikula 	 * Determine whether ->hpd_pulse() exists for each pin, and
503df0566a6SJani Nikula 	 * whether we have a short or a long pulse. This is needed
504df0566a6SJani Nikula 	 * as each pin may have up to two encoders (HDMI and DP) and
505df0566a6SJani Nikula 	 * only the one of them (DP) will have ->hpd_pulse().
506df0566a6SJani Nikula 	 */
507df0566a6SJani Nikula 	for_each_intel_encoder(&dev_priv->drm, encoder) {
508df0566a6SJani Nikula 		enum port port = encoder->port;
509df0566a6SJani Nikula 		bool long_hpd;
510df0566a6SJani Nikula 
511df0566a6SJani Nikula 		pin = encoder->hpd_pin;
512df0566a6SJani Nikula 		if (!(BIT(pin) & pin_mask))
513df0566a6SJani Nikula 			continue;
514df0566a6SJani Nikula 
515e198eea9SVille Syrjälä 		if (!intel_encoder_has_hpd_pulse(encoder))
516df0566a6SJani Nikula 			continue;
517df0566a6SJani Nikula 
518df0566a6SJani Nikula 		long_hpd = long_mask & BIT(pin);
519df0566a6SJani Nikula 
5201084f5c8SWambui Karuga 		drm_dbg(&dev_priv->drm,
5211084f5c8SWambui Karuga 			"digital hpd on [ENCODER:%d:%s] - %s\n",
52266a990ddSVille Syrjälä 			encoder->base.base.id, encoder->base.name,
523df0566a6SJani Nikula 			long_hpd ? "long" : "short");
524df0566a6SJani Nikula 		queue_dig = true;
525df0566a6SJani Nikula 
526df0566a6SJani Nikula 		if (long_hpd) {
527df0566a6SJani Nikula 			long_hpd_pulse_mask |= BIT(pin);
5285a4dd6f0SJani Nikula 			dev_priv->display.hotplug.long_port_mask |= BIT(port);
529df0566a6SJani Nikula 		} else {
530df0566a6SJani Nikula 			short_hpd_pulse_mask |= BIT(pin);
5315a4dd6f0SJani Nikula 			dev_priv->display.hotplug.short_port_mask |= BIT(port);
532df0566a6SJani Nikula 		}
533df0566a6SJani Nikula 	}
534df0566a6SJani Nikula 
535df0566a6SJani Nikula 	/* Now process each pin just once */
536df0566a6SJani Nikula 	for_each_hpd_pin(pin) {
537df0566a6SJani Nikula 		bool long_hpd;
538df0566a6SJani Nikula 
539df0566a6SJani Nikula 		if (!(BIT(pin) & pin_mask))
540df0566a6SJani Nikula 			continue;
541df0566a6SJani Nikula 
5425a4dd6f0SJani Nikula 		if (dev_priv->display.hotplug.stats[pin].state == HPD_DISABLED) {
543df0566a6SJani Nikula 			/*
544df0566a6SJani Nikula 			 * On GMCH platforms the interrupt mask bits only
545df0566a6SJani Nikula 			 * prevent irq generation, not the setting of the
546df0566a6SJani Nikula 			 * hotplug bits itself. So only WARN about unexpected
547df0566a6SJani Nikula 			 * interrupts on saner platforms.
548df0566a6SJani Nikula 			 */
549f4224a4cSPankaj Bharadiya 			drm_WARN_ONCE(&dev_priv->drm, !HAS_GMCH(dev_priv),
550f4224a4cSPankaj Bharadiya 				      "Received HPD interrupt on pin %d although disabled\n",
551f4224a4cSPankaj Bharadiya 				      pin);
552df0566a6SJani Nikula 			continue;
553df0566a6SJani Nikula 		}
554df0566a6SJani Nikula 
5555a4dd6f0SJani Nikula 		if (dev_priv->display.hotplug.stats[pin].state != HPD_ENABLED)
556df0566a6SJani Nikula 			continue;
557df0566a6SJani Nikula 
558df0566a6SJani Nikula 		/*
559df0566a6SJani Nikula 		 * Delegate to ->hpd_pulse() if one of the encoders for this
560df0566a6SJani Nikula 		 * pin has it, otherwise let the hotplug_work deal with this
561df0566a6SJani Nikula 		 * pin directly.
562df0566a6SJani Nikula 		 */
563df0566a6SJani Nikula 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
564df0566a6SJani Nikula 			long_hpd = long_hpd_pulse_mask & BIT(pin);
565df0566a6SJani Nikula 		} else {
5665a4dd6f0SJani Nikula 			dev_priv->display.hotplug.event_bits |= BIT(pin);
567df0566a6SJani Nikula 			long_hpd = true;
568df0566a6SJani Nikula 			queue_hp = true;
569df0566a6SJani Nikula 		}
570df0566a6SJani Nikula 
571df0566a6SJani Nikula 		if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
5725a4dd6f0SJani Nikula 			dev_priv->display.hotplug.event_bits &= ~BIT(pin);
573df0566a6SJani Nikula 			storm_detected = true;
574df0566a6SJani Nikula 			queue_hp = true;
575df0566a6SJani Nikula 		}
576df0566a6SJani Nikula 	}
577df0566a6SJani Nikula 
578df0566a6SJani Nikula 	/*
579df0566a6SJani Nikula 	 * Disable any IRQs that storms were detected on. Polling enablement
580df0566a6SJani Nikula 	 * happens later in our hotplug work.
581df0566a6SJani Nikula 	 */
5825f51e78fSVille Syrjälä 	if (storm_detected)
5835f51e78fSVille Syrjälä 		intel_hpd_irq_setup(dev_priv);
584df0566a6SJani Nikula 	spin_unlock(&dev_priv->irq_lock);
585df0566a6SJani Nikula 
586df0566a6SJani Nikula 	/*
587df0566a6SJani Nikula 	 * Our hotplug handler can grab modeset locks (by calling down into the
588df0566a6SJani Nikula 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
589df0566a6SJani Nikula 	 * queue for otherwise the flush_work in the pageflip code will
590df0566a6SJani Nikula 	 * deadlock.
591df0566a6SJani Nikula 	 */
592df0566a6SJani Nikula 	if (queue_dig)
5935a4dd6f0SJani Nikula 		queue_work(dev_priv->display.hotplug.dp_wq, &dev_priv->display.hotplug.dig_port_work);
594df0566a6SJani Nikula 	if (queue_hp)
595848a4e5cSLuca Coelho 		queue_delayed_work(dev_priv->unordered_wq,
596848a4e5cSLuca Coelho 				   &dev_priv->display.hotplug.hotplug_work, 0);
597df0566a6SJani Nikula }
598df0566a6SJani Nikula 
599df0566a6SJani Nikula /**
600df0566a6SJani Nikula  * intel_hpd_init - initializes and enables hpd support
601df0566a6SJani Nikula  * @dev_priv: i915 device instance
602df0566a6SJani Nikula  *
603df0566a6SJani Nikula  * This function enables the hotplug support. It requires that interrupts have
604df0566a6SJani Nikula  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
605df0566a6SJani Nikula  * poll request can run concurrently to other code, so locking rules must be
606df0566a6SJani Nikula  * obeyed.
607df0566a6SJani Nikula  *
608df0566a6SJani Nikula  * This is a separate step from interrupt enabling to simplify the locking rules
609df0566a6SJani Nikula  * in the driver load and resume code.
610df0566a6SJani Nikula  *
6114c8d4651SVille Syrjälä  * Also see: intel_hpd_poll_enable() and intel_hpd_poll_disable().
612df0566a6SJani Nikula  */
intel_hpd_init(struct drm_i915_private * dev_priv)613df0566a6SJani Nikula void intel_hpd_init(struct drm_i915_private *dev_priv)
614df0566a6SJani Nikula {
615df0566a6SJani Nikula 	int i;
616df0566a6SJani Nikula 
6175df7bd13SJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
6185df7bd13SJosé Roberto de Souza 		return;
6195df7bd13SJosé Roberto de Souza 
620df0566a6SJani Nikula 	for_each_hpd_pin(i) {
6215a4dd6f0SJani Nikula 		dev_priv->display.hotplug.stats[i].count = 0;
6225a4dd6f0SJani Nikula 		dev_priv->display.hotplug.stats[i].state = HPD_ENABLED;
623df0566a6SJani Nikula 	}
624df0566a6SJani Nikula 
625df0566a6SJani Nikula 	/*
626df0566a6SJani Nikula 	 * Interrupt setup is already guaranteed to be single-threaded, this is
627df0566a6SJani Nikula 	 * just to make the assert_spin_locked checks happy.
628df0566a6SJani Nikula 	 */
629df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
6305f51e78fSVille Syrjälä 	intel_hpd_irq_setup(dev_priv);
631df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
632df0566a6SJani Nikula }
633df0566a6SJani Nikula 
i915_hpd_poll_init_work(struct work_struct * work)634df0566a6SJani Nikula static void i915_hpd_poll_init_work(struct work_struct *work)
635df0566a6SJani Nikula {
636df0566a6SJani Nikula 	struct drm_i915_private *dev_priv =
637df0566a6SJani Nikula 		container_of(work, struct drm_i915_private,
6385a4dd6f0SJani Nikula 			     display.hotplug.poll_init_work);
639df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
64070a221b2SVille Syrjälä 	struct intel_connector *connector;
641df0566a6SJani Nikula 	bool enabled;
642df0566a6SJani Nikula 
6433703060dSAndrzej Hajda 	mutex_lock(&dev_priv->drm.mode_config.mutex);
644df0566a6SJani Nikula 
6455a4dd6f0SJani Nikula 	enabled = READ_ONCE(dev_priv->display.hotplug.poll_enabled);
646df0566a6SJani Nikula 
6473703060dSAndrzej Hajda 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
64870a221b2SVille Syrjälä 	for_each_intel_connector_iter(connector, &conn_iter) {
6495fb908ebSVille Syrjälä 		enum hpd_pin pin;
6505fb908ebSVille Syrjälä 
6515fb908ebSVille Syrjälä 		pin = intel_connector_hpd_pin(connector);
6525fb908ebSVille Syrjälä 		if (pin == HPD_NONE)
6535fb908ebSVille Syrjälä 			continue;
654012ee690SVille Syrjälä 
65570a221b2SVille Syrjälä 		connector->base.polled = connector->polled;
656df0566a6SJani Nikula 
6575fb908ebSVille Syrjälä 		if (enabled && connector->base.polled == DRM_CONNECTOR_POLL_HPD)
6585fb908ebSVille Syrjälä 			connector->base.polled = DRM_CONNECTOR_POLL_CONNECT |
6595fb908ebSVille Syrjälä 				DRM_CONNECTOR_POLL_DISCONNECT;
660df0566a6SJani Nikula 	}
661df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
662df0566a6SJani Nikula 
663df0566a6SJani Nikula 	if (enabled)
664*cfd48ad8SImre Deak 		drm_kms_helper_poll_reschedule(&dev_priv->drm);
665df0566a6SJani Nikula 
6663703060dSAndrzej Hajda 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
667df0566a6SJani Nikula 
668df0566a6SJani Nikula 	/*
669df0566a6SJani Nikula 	 * We might have missed any hotplugs that happened while we were
670df0566a6SJani Nikula 	 * in the middle of disabling polling
671df0566a6SJani Nikula 	 */
672df0566a6SJani Nikula 	if (!enabled)
6733703060dSAndrzej Hajda 		drm_helper_hpd_irq_event(&dev_priv->drm);
674df0566a6SJani Nikula }
675df0566a6SJani Nikula 
676df0566a6SJani Nikula /**
6774c8d4651SVille Syrjälä  * intel_hpd_poll_enable - enable polling for connectors with hpd
678df0566a6SJani Nikula  * @dev_priv: i915 device instance
679df0566a6SJani Nikula  *
6804c8d4651SVille Syrjälä  * This function enables polling for all connectors which support HPD.
6814c8d4651SVille Syrjälä  * Under certain conditions HPD may not be functional. On most Intel GPUs,
6824c8d4651SVille Syrjälä  * this happens when we enter runtime suspend.
683df0566a6SJani Nikula  * On Valleyview and Cherryview systems, this also happens when we shut off all
684df0566a6SJani Nikula  * of the powerwells.
685df0566a6SJani Nikula  *
686df0566a6SJani Nikula  * Since this function can get called in contexts where we're already holding
687df0566a6SJani Nikula  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
688df0566a6SJani Nikula  * worker.
689df0566a6SJani Nikula  *
6904c8d4651SVille Syrjälä  * Also see: intel_hpd_init() and intel_hpd_poll_disable().
691df0566a6SJani Nikula  */
intel_hpd_poll_enable(struct drm_i915_private * dev_priv)6924c8d4651SVille Syrjälä void intel_hpd_poll_enable(struct drm_i915_private *dev_priv)
693df0566a6SJani Nikula {
69404770b08SJouni Högander 	if (!HAS_DISPLAY(dev_priv) ||
69504770b08SJouni Högander 	    !INTEL_DISPLAY_ENABLED(dev_priv))
6965df7bd13SJosé Roberto de Souza 		return;
6975df7bd13SJosé Roberto de Souza 
6985a4dd6f0SJani Nikula 	WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, true);
699df0566a6SJani Nikula 
700df0566a6SJani Nikula 	/*
701df0566a6SJani Nikula 	 * We might already be holding dev->mode_config.mutex, so do this in a
702df0566a6SJani Nikula 	 * seperate worker
703df0566a6SJani Nikula 	 * As well, there's no issue if we race here since we always reschedule
704df0566a6SJani Nikula 	 * this worker anyway
705df0566a6SJani Nikula 	 */
706848a4e5cSLuca Coelho 	queue_work(dev_priv->unordered_wq,
707848a4e5cSLuca Coelho 		   &dev_priv->display.hotplug.poll_init_work);
708df0566a6SJani Nikula }
709df0566a6SJani Nikula 
7104c8d4651SVille Syrjälä /**
7114c8d4651SVille Syrjälä  * intel_hpd_poll_disable - disable polling for connectors with hpd
7124c8d4651SVille Syrjälä  * @dev_priv: i915 device instance
7134c8d4651SVille Syrjälä  *
7144c8d4651SVille Syrjälä  * This function disables polling for all connectors which support HPD.
7154c8d4651SVille Syrjälä  * Under certain conditions HPD may not be functional. On most Intel GPUs,
7164c8d4651SVille Syrjälä  * this happens when we enter runtime suspend.
7174c8d4651SVille Syrjälä  * On Valleyview and Cherryview systems, this also happens when we shut off all
7184c8d4651SVille Syrjälä  * of the powerwells.
7194c8d4651SVille Syrjälä  *
7204c8d4651SVille Syrjälä  * Since this function can get called in contexts where we're already holding
7214c8d4651SVille Syrjälä  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
7224c8d4651SVille Syrjälä  * worker.
7234c8d4651SVille Syrjälä  *
7244c8d4651SVille Syrjälä  * Also used during driver init to initialize connector->polled
7254c8d4651SVille Syrjälä  * appropriately for all connectors.
7264c8d4651SVille Syrjälä  *
7274c8d4651SVille Syrjälä  * Also see: intel_hpd_init() and intel_hpd_poll_enable().
7284c8d4651SVille Syrjälä  */
intel_hpd_poll_disable(struct drm_i915_private * dev_priv)7294c8d4651SVille Syrjälä void intel_hpd_poll_disable(struct drm_i915_private *dev_priv)
7304c8d4651SVille Syrjälä {
7315df7bd13SJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
7325df7bd13SJosé Roberto de Souza 		return;
7335df7bd13SJosé Roberto de Souza 
7345a4dd6f0SJani Nikula 	WRITE_ONCE(dev_priv->display.hotplug.poll_enabled, false);
735848a4e5cSLuca Coelho 	queue_work(dev_priv->unordered_wq,
736848a4e5cSLuca Coelho 		   &dev_priv->display.hotplug.poll_init_work);
7374c8d4651SVille Syrjälä }
7384c8d4651SVille Syrjälä 
intel_hpd_init_early(struct drm_i915_private * i915)739dd890d42SJani Nikula void intel_hpd_init_early(struct drm_i915_private *i915)
740df0566a6SJani Nikula {
741dd890d42SJani Nikula 	INIT_DELAYED_WORK(&i915->display.hotplug.hotplug_work,
7423944709dSImre Deak 			  i915_hotplug_work_func);
743dd890d42SJani Nikula 	INIT_WORK(&i915->display.hotplug.dig_port_work, i915_digport_work_func);
744dd890d42SJani Nikula 	INIT_WORK(&i915->display.hotplug.poll_init_work, i915_hpd_poll_init_work);
745dd890d42SJani Nikula 	INIT_DELAYED_WORK(&i915->display.hotplug.reenable_work,
746df0566a6SJani Nikula 			  intel_hpd_irq_storm_reenable_work);
747dd890d42SJani Nikula 
748dd890d42SJani Nikula 	i915->display.hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
749dd890d42SJani Nikula 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
750dd890d42SJani Nikula 	 * detection, as short HPD storms will occur as a natural part of
751dd890d42SJani Nikula 	 * sideband messaging with MST.
752dd890d42SJani Nikula 	 * On older platforms however, IRQ storms can occur with both long and
753dd890d42SJani Nikula 	 * short pulses, as seen on some G4x systems.
754dd890d42SJani Nikula 	 */
755dd890d42SJani Nikula 	i915->display.hotplug.hpd_short_storm_enabled = !HAS_DP_MST(i915);
756df0566a6SJani Nikula }
757df0566a6SJani Nikula 
intel_hpd_cancel_work(struct drm_i915_private * dev_priv)758df0566a6SJani Nikula void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
759df0566a6SJani Nikula {
7605df7bd13SJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
7615df7bd13SJosé Roberto de Souza 		return;
7625df7bd13SJosé Roberto de Souza 
763df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
764df0566a6SJani Nikula 
7655a4dd6f0SJani Nikula 	dev_priv->display.hotplug.long_port_mask = 0;
7665a4dd6f0SJani Nikula 	dev_priv->display.hotplug.short_port_mask = 0;
7675a4dd6f0SJani Nikula 	dev_priv->display.hotplug.event_bits = 0;
7685a4dd6f0SJani Nikula 	dev_priv->display.hotplug.retry_bits = 0;
769df0566a6SJani Nikula 
770df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
771df0566a6SJani Nikula 
7725a4dd6f0SJani Nikula 	cancel_work_sync(&dev_priv->display.hotplug.dig_port_work);
7735a4dd6f0SJani Nikula 	cancel_delayed_work_sync(&dev_priv->display.hotplug.hotplug_work);
7745a4dd6f0SJani Nikula 	cancel_work_sync(&dev_priv->display.hotplug.poll_init_work);
7755a4dd6f0SJani Nikula 	cancel_delayed_work_sync(&dev_priv->display.hotplug.reenable_work);
776df0566a6SJani Nikula }
777df0566a6SJani Nikula 
intel_hpd_disable(struct drm_i915_private * dev_priv,enum hpd_pin pin)778df0566a6SJani Nikula bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
779df0566a6SJani Nikula {
780df0566a6SJani Nikula 	bool ret = false;
781df0566a6SJani Nikula 
782df0566a6SJani Nikula 	if (pin == HPD_NONE)
783df0566a6SJani Nikula 		return false;
784df0566a6SJani Nikula 
785df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
7865a4dd6f0SJani Nikula 	if (dev_priv->display.hotplug.stats[pin].state == HPD_ENABLED) {
7875a4dd6f0SJani Nikula 		dev_priv->display.hotplug.stats[pin].state = HPD_DISABLED;
788df0566a6SJani Nikula 		ret = true;
789df0566a6SJani Nikula 	}
790df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
791df0566a6SJani Nikula 
792df0566a6SJani Nikula 	return ret;
793df0566a6SJani Nikula }
794df0566a6SJani Nikula 
intel_hpd_enable(struct drm_i915_private * dev_priv,enum hpd_pin pin)795df0566a6SJani Nikula void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
796df0566a6SJani Nikula {
797df0566a6SJani Nikula 	if (pin == HPD_NONE)
798df0566a6SJani Nikula 		return;
799df0566a6SJani Nikula 
800df0566a6SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
8015a4dd6f0SJani Nikula 	dev_priv->display.hotplug.stats[pin].state = HPD_ENABLED;
802df0566a6SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
803df0566a6SJani Nikula }
8041bed8b07SJani Nikula 
i915_hpd_storm_ctl_show(struct seq_file * m,void * data)8051bed8b07SJani Nikula static int i915_hpd_storm_ctl_show(struct seq_file *m, void *data)
8061bed8b07SJani Nikula {
8071bed8b07SJani Nikula 	struct drm_i915_private *dev_priv = m->private;
8081bed8b07SJani Nikula 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
8091bed8b07SJani Nikula 
8101bed8b07SJani Nikula 	/* Synchronize with everything first in case there's been an HPD
8111bed8b07SJani Nikula 	 * storm, but we haven't finished handling it in the kernel yet
8121bed8b07SJani Nikula 	 */
8131bed8b07SJani Nikula 	intel_synchronize_irq(dev_priv);
8141bed8b07SJani Nikula 	flush_work(&dev_priv->display.hotplug.dig_port_work);
8151bed8b07SJani Nikula 	flush_delayed_work(&dev_priv->display.hotplug.hotplug_work);
8161bed8b07SJani Nikula 
8171bed8b07SJani Nikula 	seq_printf(m, "Threshold: %d\n", hotplug->hpd_storm_threshold);
8181bed8b07SJani Nikula 	seq_printf(m, "Detected: %s\n",
8191bed8b07SJani Nikula 		   str_yes_no(delayed_work_pending(&hotplug->reenable_work)));
8201bed8b07SJani Nikula 
8211bed8b07SJani Nikula 	return 0;
8221bed8b07SJani Nikula }
8231bed8b07SJani Nikula 
i915_hpd_storm_ctl_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)8241bed8b07SJani Nikula static ssize_t i915_hpd_storm_ctl_write(struct file *file,
8251bed8b07SJani Nikula 					const char __user *ubuf, size_t len,
8261bed8b07SJani Nikula 					loff_t *offp)
8271bed8b07SJani Nikula {
8281bed8b07SJani Nikula 	struct seq_file *m = file->private_data;
8291bed8b07SJani Nikula 	struct drm_i915_private *dev_priv = m->private;
8301bed8b07SJani Nikula 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
8311bed8b07SJani Nikula 	unsigned int new_threshold;
8321bed8b07SJani Nikula 	int i;
8331bed8b07SJani Nikula 	char *newline;
8341bed8b07SJani Nikula 	char tmp[16];
8351bed8b07SJani Nikula 
8361bed8b07SJani Nikula 	if (len >= sizeof(tmp))
8371bed8b07SJani Nikula 		return -EINVAL;
8381bed8b07SJani Nikula 
8391bed8b07SJani Nikula 	if (copy_from_user(tmp, ubuf, len))
8401bed8b07SJani Nikula 		return -EFAULT;
8411bed8b07SJani Nikula 
8421bed8b07SJani Nikula 	tmp[len] = '\0';
8431bed8b07SJani Nikula 
8441bed8b07SJani Nikula 	/* Strip newline, if any */
8451bed8b07SJani Nikula 	newline = strchr(tmp, '\n');
8461bed8b07SJani Nikula 	if (newline)
8471bed8b07SJani Nikula 		*newline = '\0';
8481bed8b07SJani Nikula 
8491bed8b07SJani Nikula 	if (strcmp(tmp, "reset") == 0)
8501bed8b07SJani Nikula 		new_threshold = HPD_STORM_DEFAULT_THRESHOLD;
8511bed8b07SJani Nikula 	else if (kstrtouint(tmp, 10, &new_threshold) != 0)
8521bed8b07SJani Nikula 		return -EINVAL;
8531bed8b07SJani Nikula 
8541bed8b07SJani Nikula 	if (new_threshold > 0)
8551bed8b07SJani Nikula 		drm_dbg_kms(&dev_priv->drm,
8561bed8b07SJani Nikula 			    "Setting HPD storm detection threshold to %d\n",
8571bed8b07SJani Nikula 			    new_threshold);
8581bed8b07SJani Nikula 	else
8591bed8b07SJani Nikula 		drm_dbg_kms(&dev_priv->drm, "Disabling HPD storm detection\n");
8601bed8b07SJani Nikula 
8611bed8b07SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
8621bed8b07SJani Nikula 	hotplug->hpd_storm_threshold = new_threshold;
8631bed8b07SJani Nikula 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
8641bed8b07SJani Nikula 	for_each_hpd_pin(i)
8651bed8b07SJani Nikula 		hotplug->stats[i].count = 0;
8661bed8b07SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
8671bed8b07SJani Nikula 
8681bed8b07SJani Nikula 	/* Re-enable hpd immediately if we were in an irq storm */
8691bed8b07SJani Nikula 	flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
8701bed8b07SJani Nikula 
8711bed8b07SJani Nikula 	return len;
8721bed8b07SJani Nikula }
8731bed8b07SJani Nikula 
i915_hpd_storm_ctl_open(struct inode * inode,struct file * file)8741bed8b07SJani Nikula static int i915_hpd_storm_ctl_open(struct inode *inode, struct file *file)
8751bed8b07SJani Nikula {
8761bed8b07SJani Nikula 	return single_open(file, i915_hpd_storm_ctl_show, inode->i_private);
8771bed8b07SJani Nikula }
8781bed8b07SJani Nikula 
8791bed8b07SJani Nikula static const struct file_operations i915_hpd_storm_ctl_fops = {
8801bed8b07SJani Nikula 	.owner = THIS_MODULE,
8811bed8b07SJani Nikula 	.open = i915_hpd_storm_ctl_open,
8821bed8b07SJani Nikula 	.read = seq_read,
8831bed8b07SJani Nikula 	.llseek = seq_lseek,
8841bed8b07SJani Nikula 	.release = single_release,
8851bed8b07SJani Nikula 	.write = i915_hpd_storm_ctl_write
8861bed8b07SJani Nikula };
8871bed8b07SJani Nikula 
i915_hpd_short_storm_ctl_show(struct seq_file * m,void * data)8881bed8b07SJani Nikula static int i915_hpd_short_storm_ctl_show(struct seq_file *m, void *data)
8891bed8b07SJani Nikula {
8901bed8b07SJani Nikula 	struct drm_i915_private *dev_priv = m->private;
8911bed8b07SJani Nikula 
8921bed8b07SJani Nikula 	seq_printf(m, "Enabled: %s\n",
8931bed8b07SJani Nikula 		   str_yes_no(dev_priv->display.hotplug.hpd_short_storm_enabled));
8941bed8b07SJani Nikula 
8951bed8b07SJani Nikula 	return 0;
8961bed8b07SJani Nikula }
8971bed8b07SJani Nikula 
8981bed8b07SJani Nikula static int
i915_hpd_short_storm_ctl_open(struct inode * inode,struct file * file)8991bed8b07SJani Nikula i915_hpd_short_storm_ctl_open(struct inode *inode, struct file *file)
9001bed8b07SJani Nikula {
9011bed8b07SJani Nikula 	return single_open(file, i915_hpd_short_storm_ctl_show,
9021bed8b07SJani Nikula 			   inode->i_private);
9031bed8b07SJani Nikula }
9041bed8b07SJani Nikula 
i915_hpd_short_storm_ctl_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)9051bed8b07SJani Nikula static ssize_t i915_hpd_short_storm_ctl_write(struct file *file,
9061bed8b07SJani Nikula 					      const char __user *ubuf,
9071bed8b07SJani Nikula 					      size_t len, loff_t *offp)
9081bed8b07SJani Nikula {
9091bed8b07SJani Nikula 	struct seq_file *m = file->private_data;
9101bed8b07SJani Nikula 	struct drm_i915_private *dev_priv = m->private;
9111bed8b07SJani Nikula 	struct intel_hotplug *hotplug = &dev_priv->display.hotplug;
9121bed8b07SJani Nikula 	char *newline;
9131bed8b07SJani Nikula 	char tmp[16];
9141bed8b07SJani Nikula 	int i;
9151bed8b07SJani Nikula 	bool new_state;
9161bed8b07SJani Nikula 
9171bed8b07SJani Nikula 	if (len >= sizeof(tmp))
9181bed8b07SJani Nikula 		return -EINVAL;
9191bed8b07SJani Nikula 
9201bed8b07SJani Nikula 	if (copy_from_user(tmp, ubuf, len))
9211bed8b07SJani Nikula 		return -EFAULT;
9221bed8b07SJani Nikula 
9231bed8b07SJani Nikula 	tmp[len] = '\0';
9241bed8b07SJani Nikula 
9251bed8b07SJani Nikula 	/* Strip newline, if any */
9261bed8b07SJani Nikula 	newline = strchr(tmp, '\n');
9271bed8b07SJani Nikula 	if (newline)
9281bed8b07SJani Nikula 		*newline = '\0';
9291bed8b07SJani Nikula 
9301bed8b07SJani Nikula 	/* Reset to the "default" state for this system */
9311bed8b07SJani Nikula 	if (strcmp(tmp, "reset") == 0)
9321bed8b07SJani Nikula 		new_state = !HAS_DP_MST(dev_priv);
9331bed8b07SJani Nikula 	else if (kstrtobool(tmp, &new_state) != 0)
9341bed8b07SJani Nikula 		return -EINVAL;
9351bed8b07SJani Nikula 
9361bed8b07SJani Nikula 	drm_dbg_kms(&dev_priv->drm, "%sabling HPD short storm detection\n",
9371bed8b07SJani Nikula 		    new_state ? "En" : "Dis");
9381bed8b07SJani Nikula 
9391bed8b07SJani Nikula 	spin_lock_irq(&dev_priv->irq_lock);
9401bed8b07SJani Nikula 	hotplug->hpd_short_storm_enabled = new_state;
9411bed8b07SJani Nikula 	/* Reset the HPD storm stats so we don't accidentally trigger a storm */
9421bed8b07SJani Nikula 	for_each_hpd_pin(i)
9431bed8b07SJani Nikula 		hotplug->stats[i].count = 0;
9441bed8b07SJani Nikula 	spin_unlock_irq(&dev_priv->irq_lock);
9451bed8b07SJani Nikula 
9461bed8b07SJani Nikula 	/* Re-enable hpd immediately if we were in an irq storm */
9471bed8b07SJani Nikula 	flush_delayed_work(&dev_priv->display.hotplug.reenable_work);
9481bed8b07SJani Nikula 
9491bed8b07SJani Nikula 	return len;
9501bed8b07SJani Nikula }
9511bed8b07SJani Nikula 
9521bed8b07SJani Nikula static const struct file_operations i915_hpd_short_storm_ctl_fops = {
9531bed8b07SJani Nikula 	.owner = THIS_MODULE,
9541bed8b07SJani Nikula 	.open = i915_hpd_short_storm_ctl_open,
9551bed8b07SJani Nikula 	.read = seq_read,
9561bed8b07SJani Nikula 	.llseek = seq_lseek,
9571bed8b07SJani Nikula 	.release = single_release,
9581bed8b07SJani Nikula 	.write = i915_hpd_short_storm_ctl_write,
9591bed8b07SJani Nikula };
9601bed8b07SJani Nikula 
intel_hpd_debugfs_register(struct drm_i915_private * i915)9611bed8b07SJani Nikula void intel_hpd_debugfs_register(struct drm_i915_private *i915)
9621bed8b07SJani Nikula {
9631bed8b07SJani Nikula 	struct drm_minor *minor = i915->drm.primary;
9641bed8b07SJani Nikula 
9651bed8b07SJani Nikula 	debugfs_create_file("i915_hpd_storm_ctl", 0644, minor->debugfs_root,
9661bed8b07SJani Nikula 			    i915, &i915_hpd_storm_ctl_fops);
9671bed8b07SJani Nikula 	debugfs_create_file("i915_hpd_short_storm_ctl", 0644, minor->debugfs_root,
9681bed8b07SJani Nikula 			    i915, &i915_hpd_short_storm_ctl_fops);
969b0fbef65SVinod Govindapillai 	debugfs_create_bool("i915_ignore_long_hpd", 0644, minor->debugfs_root,
970b0fbef65SVinod Govindapillai 			    &i915->display.hotplug.ignore_long_hpd);
9711bed8b07SJani Nikula }
972