blob: 43aa92beff2a691b8fe9ea0e00e1721680c73d2a [file] [log] [blame]
Jani Nikula77913b32015-06-18 13:06:16 +03001/*
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
Jani Nikula856974a2015-07-02 16:05:28 +030032/**
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.
Thulasimani,Sivakumarfeecb692015-07-10 12:30:43 +053069 *
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.
Jani Nikula856974a2015-07-02 16:05:28 +030077 */
78
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070079/**
80 * intel_hpd_port - return port hard associated with certain pin.
Rodrigo Vivicf539022018-01-29 15:22:21 -080081 * @dev_priv: private driver data pointer
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070082 * @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 */
Rodrigo Vivicf539022018-01-29 15:22:21 -080087enum port intel_hpd_pin_to_port(struct drm_i915_private *dev_priv,
88 enum hpd_pin pin)
Jani Nikula77913b32015-06-18 13:06:16 +030089{
90 switch (pin) {
Imre Deakcc24fcd2015-07-21 15:32:45 -070091 case HPD_PORT_A:
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070092 return PORT_A;
Jani Nikula77913b32015-06-18 13:06:16 +030093 case HPD_PORT_B:
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070094 return PORT_B;
Jani Nikula77913b32015-06-18 13:06:16 +030095 case HPD_PORT_C:
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070096 return PORT_C;
Jani Nikula77913b32015-06-18 13:06:16 +030097 case HPD_PORT_D:
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -070098 return PORT_D;
Xiong Zhang26951ca2015-08-17 15:55:50 +080099 case HPD_PORT_E:
Rodrigo Vivicf539022018-01-29 15:22:21 -0800100 if (IS_CNL_WITH_PORT_F(dev_priv))
101 return PORT_F;
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -0700102 return PORT_E;
Dhinakaran Pandiyan96ae4832018-03-23 10:24:17 -0700103 case HPD_PORT_F:
104 return PORT_F;
Jani Nikula77913b32015-06-18 13:06:16 +0300105 default:
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -0700106 return PORT_NONE; /* no port for this pin */
Jani Nikula77913b32015-06-18 13:06:16 +0300107 }
108}
109
Rodrigo Vivif761bef22017-08-11 11:26:50 -0700110/**
Rodrigo Vivicf539022018-01-29 15:22:21 -0800111 * intel_hpd_pin_default - return default pin associated with certain port.
112 * @dev_priv: private driver data pointer
Rodrigo Vivif761bef22017-08-11 11:26:50 -0700113 * @port: the hpd port to get associated pin
114 *
Rodrigo Vivicf539022018-01-29 15:22:21 -0800115 * It is only valid and used by digital port encoder.
116 *
Rodrigo Vivif761bef22017-08-11 11:26:50 -0700117 * Return pin that is associatade with @port and HDP_NONE if no pin is
118 * hard associated with that @port.
119 */
Rodrigo Vivicf539022018-01-29 15:22:21 -0800120enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
121 enum port port)
Rodrigo Vivif761bef22017-08-11 11:26:50 -0700122{
123 switch (port) {
124 case PORT_A:
125 return HPD_PORT_A;
126 case PORT_B:
127 return HPD_PORT_B;
128 case PORT_C:
129 return HPD_PORT_C;
130 case PORT_D:
131 return HPD_PORT_D;
132 case PORT_E:
133 return HPD_PORT_E;
Rodrigo Vivicf539022018-01-29 15:22:21 -0800134 case PORT_F:
135 if (IS_CNL_WITH_PORT_F(dev_priv))
136 return HPD_PORT_E;
Dhinakaran Pandiyan96ae4832018-03-23 10:24:17 -0700137 return HPD_PORT_F;
Rodrigo Vivif761bef22017-08-11 11:26:50 -0700138 default:
139 MISSING_CASE(port);
140 return HPD_NONE;
141 }
142}
143
Jani Nikula77913b32015-06-18 13:06:16 +0300144#define HPD_STORM_DETECT_PERIOD 1000
Jani Nikula77913b32015-06-18 13:06:16 +0300145#define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
146
147/**
148 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
149 * @dev_priv: private driver data pointer
150 * @pin: the pin to gather stats on
151 *
152 * Gather stats about HPD irqs from the specified @pin, and detect irq
153 * storms. Only the pin specific stats and state are changed, the caller is
154 * responsible for further action.
155 *
Lyude317eaa92017-02-03 21:18:25 -0500156 * The number of irqs that are allowed within @HPD_STORM_DETECT_PERIOD is
157 * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
158 * @HPD_STORM_DEFAULT_THRESHOLD. If this threshold is exceeded, it's
159 * considered an irq storm and the irq state is set to @HPD_MARK_DISABLED.
160 *
161 * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
162 * and should only be adjusted for automated hotplug testing.
Jani Nikula77913b32015-06-18 13:06:16 +0300163 *
164 * Return true if an irq storm was detected on @pin.
165 */
166static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
167 enum hpd_pin pin)
168{
169 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
170 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
Lyude317eaa92017-02-03 21:18:25 -0500171 const int threshold = dev_priv->hotplug.hpd_storm_threshold;
Jani Nikula77913b32015-06-18 13:06:16 +0300172 bool storm = false;
173
174 if (!time_in_range(jiffies, start, end)) {
175 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
176 dev_priv->hotplug.stats[pin].count = 0;
177 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
Lyude317eaa92017-02-03 21:18:25 -0500178 } else if (dev_priv->hotplug.stats[pin].count > threshold &&
179 threshold) {
Jani Nikula77913b32015-06-18 13:06:16 +0300180 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
181 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
182 storm = true;
183 } else {
184 dev_priv->hotplug.stats[pin].count++;
185 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
186 dev_priv->hotplug.stats[pin].count);
187 }
188
189 return storm;
190}
191
192static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
193{
Chris Wilson91c8a322016-07-05 10:40:23 +0100194 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300195 struct intel_connector *intel_connector;
196 struct intel_encoder *intel_encoder;
197 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100198 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300199 enum hpd_pin pin;
200 bool hpd_disabled = false;
201
Chris Wilson67520412017-03-02 13:28:01 +0000202 lockdep_assert_held(&dev_priv->irq_lock);
Jani Nikula77913b32015-06-18 13:06:16 +0300203
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100204 drm_connector_list_iter_begin(dev, &conn_iter);
205 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300206 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
207 continue;
208
209 intel_connector = to_intel_connector(connector);
210 intel_encoder = intel_connector->encoder;
211 if (!intel_encoder)
212 continue;
213
214 pin = intel_encoder->hpd_pin;
215 if (pin == HPD_NONE ||
216 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
217 continue;
218
219 DRM_INFO("HPD interrupt storm detected on connector %s: "
220 "switching from hotplug detection to polling\n",
221 connector->name);
222
223 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
224 connector->polled = DRM_CONNECTOR_POLL_CONNECT
225 | DRM_CONNECTOR_POLL_DISCONNECT;
226 hpd_disabled = true;
227 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100228 drm_connector_list_iter_end(&conn_iter);
Jani Nikula77913b32015-06-18 13:06:16 +0300229
230 /* Enable polling and queue hotplug re-enabling. */
231 if (hpd_disabled) {
Dave Airliec4d79c22017-01-27 12:04:08 +1000232 drm_kms_helper_poll_enable(dev);
Jani Nikula77913b32015-06-18 13:06:16 +0300233 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
234 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
235 }
236}
237
238static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
239{
240 struct drm_i915_private *dev_priv =
241 container_of(work, typeof(*dev_priv),
242 hotplug.reenable_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +0100243 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300244 int i;
245
246 intel_runtime_pm_get(dev_priv);
247
248 spin_lock_irq(&dev_priv->irq_lock);
249 for_each_hpd_pin(i) {
250 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100251 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300252
253 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
254 continue;
255
256 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
257
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100258 drm_connector_list_iter_begin(dev, &conn_iter);
259 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300260 struct intel_connector *intel_connector = to_intel_connector(connector);
261
262 if (intel_connector->encoder->hpd_pin == i) {
263 if (connector->polled != intel_connector->polled)
264 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
265 connector->name);
266 connector->polled = intel_connector->polled;
267 if (!connector->polled)
268 connector->polled = DRM_CONNECTOR_POLL_HPD;
269 }
270 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100271 drm_connector_list_iter_end(&conn_iter);
Jani Nikula77913b32015-06-18 13:06:16 +0300272 }
Chris Wilson262fd482017-02-15 13:15:47 +0000273 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100274 dev_priv->display.hpd_irq_setup(dev_priv);
Jani Nikula77913b32015-06-18 13:06:16 +0300275 spin_unlock_irq(&dev_priv->irq_lock);
276
277 intel_runtime_pm_put(dev_priv);
278}
279
Ville Syrjälädba14b22018-01-17 21:21:46 +0200280bool intel_encoder_hotplug(struct intel_encoder *encoder,
281 struct intel_connector *connector)
Jani Nikula77913b32015-06-18 13:06:16 +0300282{
Ville Syrjälädba14b22018-01-17 21:21:46 +0200283 struct drm_device *dev = connector->base.dev;
Jani Nikula77913b32015-06-18 13:06:16 +0300284 enum drm_connector_status old_status;
285
286 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
Ville Syrjälädba14b22018-01-17 21:21:46 +0200287 old_status = connector->base.status;
Jani Nikula77913b32015-06-18 13:06:16 +0300288
Ville Syrjälädba14b22018-01-17 21:21:46 +0200289 connector->base.status =
290 drm_helper_probe_detect(&connector->base, NULL, false);
Maarten Lankhorst6c5ed5a2017-04-06 20:55:20 +0200291
Ville Syrjälädba14b22018-01-17 21:21:46 +0200292 if (old_status == connector->base.status)
Jani Nikula77913b32015-06-18 13:06:16 +0300293 return false;
294
295 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
Ville Syrjälädba14b22018-01-17 21:21:46 +0200296 connector->base.base.id,
297 connector->base.name,
Jani Nikula77913b32015-06-18 13:06:16 +0300298 drm_get_connector_status_name(old_status),
Ville Syrjälädba14b22018-01-17 21:21:46 +0200299 drm_get_connector_status_name(connector->base.status));
Jani Nikula77913b32015-06-18 13:06:16 +0300300
301 return true;
302}
303
304static void i915_digport_work_func(struct work_struct *work)
305{
306 struct drm_i915_private *dev_priv =
307 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
308 u32 long_port_mask, short_port_mask;
309 struct intel_digital_port *intel_dig_port;
310 int i;
311 u32 old_bits = 0;
312
313 spin_lock_irq(&dev_priv->irq_lock);
314 long_port_mask = dev_priv->hotplug.long_port_mask;
315 dev_priv->hotplug.long_port_mask = 0;
316 short_port_mask = dev_priv->hotplug.short_port_mask;
317 dev_priv->hotplug.short_port_mask = 0;
318 spin_unlock_irq(&dev_priv->irq_lock);
319
320 for (i = 0; i < I915_MAX_PORTS; i++) {
321 bool valid = false;
322 bool long_hpd = false;
323 intel_dig_port = dev_priv->hotplug.irq_port[i];
324 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
325 continue;
326
327 if (long_port_mask & (1 << i)) {
328 valid = true;
329 long_hpd = true;
330 } else if (short_port_mask & (1 << i))
331 valid = true;
332
333 if (valid) {
334 enum irqreturn ret;
335
336 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
337 if (ret == IRQ_NONE) {
338 /* fall back to old school hpd */
339 old_bits |= (1 << intel_dig_port->base.hpd_pin);
340 }
341 }
342 }
343
344 if (old_bits) {
345 spin_lock_irq(&dev_priv->irq_lock);
346 dev_priv->hotplug.event_bits |= old_bits;
347 spin_unlock_irq(&dev_priv->irq_lock);
348 schedule_work(&dev_priv->hotplug.hotplug_work);
349 }
350}
351
352/*
353 * Handle hotplug events outside the interrupt handler proper.
354 */
355static void i915_hotplug_work_func(struct work_struct *work)
356{
357 struct drm_i915_private *dev_priv =
358 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
Chris Wilson91c8a322016-07-05 10:40:23 +0100359 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300360 struct intel_connector *intel_connector;
361 struct intel_encoder *intel_encoder;
362 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100363 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300364 bool changed = false;
365 u32 hpd_event_bits;
366
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100367 mutex_lock(&dev->mode_config.mutex);
Jani Nikula77913b32015-06-18 13:06:16 +0300368 DRM_DEBUG_KMS("running encoder hotplug functions\n");
369
370 spin_lock_irq(&dev_priv->irq_lock);
371
372 hpd_event_bits = dev_priv->hotplug.event_bits;
373 dev_priv->hotplug.event_bits = 0;
374
375 /* Disable hotplug on connectors that hit an irq storm. */
376 intel_hpd_irq_storm_disable(dev_priv);
377
378 spin_unlock_irq(&dev_priv->irq_lock);
379
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100380 drm_connector_list_iter_begin(dev, &conn_iter);
381 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300382 intel_connector = to_intel_connector(connector);
383 if (!intel_connector->encoder)
384 continue;
385 intel_encoder = intel_connector->encoder;
386 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
387 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
388 connector->name, intel_encoder->hpd_pin);
Ville Syrjälädba14b22018-01-17 21:21:46 +0200389
390 changed |= intel_encoder->hotplug(intel_encoder,
391 intel_connector);
Jani Nikula77913b32015-06-18 13:06:16 +0300392 }
393 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100394 drm_connector_list_iter_end(&conn_iter);
395 mutex_unlock(&dev->mode_config.mutex);
Jani Nikula77913b32015-06-18 13:06:16 +0300396
397 if (changed)
398 drm_kms_helper_hotplug_event(dev);
399}
400
401
402/**
403 * intel_hpd_irq_handler - main hotplug irq handler
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100404 * @dev_priv: drm_i915_private
Jani Nikula77913b32015-06-18 13:06:16 +0300405 * @pin_mask: a mask of hpd pins that have triggered the irq
406 * @long_mask: a mask of hpd pins that may be long hpd pulses
407 *
408 * This is the main hotplug irq handler for all platforms. The platform specific
409 * irq handlers call the platform specific hotplug irq handlers, which read and
410 * decode the appropriate registers into bitmasks about hpd pins that have
411 * triggered (@pin_mask), and which of those pins may be long pulses
412 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
413 * is not a digital port.
414 *
415 * Here, we do hotplug irq storm detection and mitigation, and pass further
416 * processing to appropriate bottom halves.
417 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100418void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
Jani Nikula77913b32015-06-18 13:06:16 +0300419 u32 pin_mask, u32 long_mask)
420{
Jani Nikula77913b32015-06-18 13:06:16 +0300421 int i;
422 enum port port;
423 bool storm_detected = false;
424 bool queue_dig = false, queue_hp = false;
425 bool is_dig_port;
426
427 if (!pin_mask)
428 return;
429
430 spin_lock(&dev_priv->irq_lock);
431 for_each_hpd_pin(i) {
432 if (!(BIT(i) & pin_mask))
433 continue;
434
Rodrigo Vivicf539022018-01-29 15:22:21 -0800435 port = intel_hpd_pin_to_port(dev_priv, i);
Rodrigo Vivi256cfdde2017-08-11 11:26:49 -0700436 is_dig_port = port != PORT_NONE &&
437 dev_priv->hotplug.irq_port[port];
Jani Nikula77913b32015-06-18 13:06:16 +0300438
439 if (is_dig_port) {
440 bool long_hpd = long_mask & BIT(i);
441
442 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
443 long_hpd ? "long" : "short");
444 /*
445 * For long HPD pulses we want to have the digital queue happen,
446 * but we still want HPD storm detection to function.
447 */
448 queue_dig = true;
449 if (long_hpd) {
450 dev_priv->hotplug.long_port_mask |= (1 << port);
451 } else {
452 /* for short HPD just trigger the digital queue */
453 dev_priv->hotplug.short_port_mask |= (1 << port);
454 continue;
455 }
456 }
457
458 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
459 /*
460 * On GMCH platforms the interrupt mask bits only
461 * prevent irq generation, not the setting of the
462 * hotplug bits itself. So only WARN about unexpected
463 * interrupts on saner platforms.
464 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100465 WARN_ONCE(!HAS_GMCH_DISPLAY(dev_priv),
Jani Nikula77913b32015-06-18 13:06:16 +0300466 "Received HPD interrupt on pin %d although disabled\n", i);
467 continue;
468 }
469
470 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
471 continue;
472
473 if (!is_dig_port) {
474 dev_priv->hotplug.event_bits |= BIT(i);
475 queue_hp = true;
476 }
477
478 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
479 dev_priv->hotplug.event_bits &= ~BIT(i);
480 storm_detected = true;
481 }
482 }
483
Chris Wilson262fd482017-02-15 13:15:47 +0000484 if (storm_detected && dev_priv->display_irqs_enabled)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100485 dev_priv->display.hpd_irq_setup(dev_priv);
Jani Nikula77913b32015-06-18 13:06:16 +0300486 spin_unlock(&dev_priv->irq_lock);
487
488 /*
489 * Our hotplug handler can grab modeset locks (by calling down into the
490 * fb helpers). Hence it must not be run on our own dev-priv->wq work
491 * queue for otherwise the flush_work in the pageflip code will
492 * deadlock.
493 */
494 if (queue_dig)
495 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
496 if (queue_hp)
497 schedule_work(&dev_priv->hotplug.hotplug_work);
498}
499
500/**
501 * intel_hpd_init - initializes and enables hpd support
502 * @dev_priv: i915 device instance
503 *
504 * This function enables the hotplug support. It requires that interrupts have
505 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
506 * poll request can run concurrently to other code, so locking rules must be
507 * obeyed.
508 *
509 * This is a separate step from interrupt enabling to simplify the locking rules
510 * in the driver load and resume code.
Lyude19625e82016-06-21 17:03:44 -0400511 *
512 * Also see: intel_hpd_poll_init(), which enables connector polling
Jani Nikula77913b32015-06-18 13:06:16 +0300513 */
514void intel_hpd_init(struct drm_i915_private *dev_priv)
515{
Jani Nikula77913b32015-06-18 13:06:16 +0300516 int i;
517
518 for_each_hpd_pin(i) {
519 dev_priv->hotplug.stats[i].count = 0;
520 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
521 }
Lyude07c51912016-01-07 10:43:28 -0500522
Lyude19625e82016-06-21 17:03:44 -0400523 WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
524 schedule_work(&dev_priv->hotplug.poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300525
526 /*
527 * Interrupt setup is already guaranteed to be single-threaded, this is
528 * just to make the assert_spin_locked checks happy.
529 */
Chris Wilson262fd482017-02-15 13:15:47 +0000530 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
531 spin_lock_irq(&dev_priv->irq_lock);
532 if (dev_priv->display_irqs_enabled)
533 dev_priv->display.hpd_irq_setup(dev_priv);
534 spin_unlock_irq(&dev_priv->irq_lock);
535 }
Jani Nikula77913b32015-06-18 13:06:16 +0300536}
537
Chris Wilson24808e92016-08-17 12:09:06 +0100538static void i915_hpd_poll_init_work(struct work_struct *work)
539{
Lyude19625e82016-06-21 17:03:44 -0400540 struct drm_i915_private *dev_priv =
541 container_of(work, struct drm_i915_private,
542 hotplug.poll_init_work);
543 struct drm_device *dev = &dev_priv->drm;
Lyude19625e82016-06-21 17:03:44 -0400544 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100545 struct drm_connector_list_iter conn_iter;
Lyude19625e82016-06-21 17:03:44 -0400546 bool enabled;
547
548 mutex_lock(&dev->mode_config.mutex);
549
550 enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
551
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100552 drm_connector_list_iter_begin(dev, &conn_iter);
553 drm_for_each_connector_iter(connector, &conn_iter) {
Lyude19625e82016-06-21 17:03:44 -0400554 struct intel_connector *intel_connector =
555 to_intel_connector(connector);
556 connector->polled = intel_connector->polled;
557
558 /* MST has a dynamic intel_connector->encoder and it's reprobing
559 * is all handled by the MST helpers. */
560 if (intel_connector->mst_port)
561 continue;
562
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +0000563 if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
Lyude19625e82016-06-21 17:03:44 -0400564 intel_connector->encoder->hpd_pin > HPD_NONE) {
565 connector->polled = enabled ?
566 DRM_CONNECTOR_POLL_CONNECT |
567 DRM_CONNECTOR_POLL_DISCONNECT :
568 DRM_CONNECTOR_POLL_HPD;
569 }
570 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100571 drm_connector_list_iter_end(&conn_iter);
Lyude19625e82016-06-21 17:03:44 -0400572
573 if (enabled)
Dave Airliec4d79c22017-01-27 12:04:08 +1000574 drm_kms_helper_poll_enable(dev);
Lyude19625e82016-06-21 17:03:44 -0400575
576 mutex_unlock(&dev->mode_config.mutex);
577
578 /*
579 * We might have missed any hotplugs that happened while we were
580 * in the middle of disabling polling
581 */
582 if (!enabled)
583 drm_helper_hpd_irq_event(dev);
584}
585
586/**
587 * intel_hpd_poll_init - enables/disables polling for connectors with hpd
588 * @dev_priv: i915 device instance
Lyude19625e82016-06-21 17:03:44 -0400589 *
590 * This function enables polling for all connectors, regardless of whether or
591 * not they support hotplug detection. Under certain conditions HPD may not be
592 * functional. On most Intel GPUs, this happens when we enter runtime suspend.
593 * On Valleyview and Cherryview systems, this also happens when we shut off all
594 * of the powerwells.
595 *
596 * Since this function can get called in contexts where we're already holding
597 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
598 * worker.
599 *
600 * Also see: intel_hpd_init(), which restores hpd handling.
601 */
602void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
603{
604 WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
605
606 /*
607 * We might already be holding dev->mode_config.mutex, so do this in a
608 * seperate worker
609 * As well, there's no issue if we race here since we always reschedule
610 * this worker anyway
611 */
612 schedule_work(&dev_priv->hotplug.poll_init_work);
613}
614
Jani Nikula77913b32015-06-18 13:06:16 +0300615void intel_hpd_init_work(struct drm_i915_private *dev_priv)
616{
617 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
618 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
Lyude19625e82016-06-21 17:03:44 -0400619 INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300620 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
621 intel_hpd_irq_storm_reenable_work);
622}
623
624void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
625{
626 spin_lock_irq(&dev_priv->irq_lock);
627
628 dev_priv->hotplug.long_port_mask = 0;
629 dev_priv->hotplug.short_port_mask = 0;
630 dev_priv->hotplug.event_bits = 0;
631
632 spin_unlock_irq(&dev_priv->irq_lock);
633
634 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
635 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
Lyude19625e82016-06-21 17:03:44 -0400636 cancel_work_sync(&dev_priv->hotplug.poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300637 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
638}
Lyudeb236d7c82016-06-21 17:03:43 -0400639
640bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
641{
642 bool ret = false;
643
644 if (pin == HPD_NONE)
645 return false;
646
647 spin_lock_irq(&dev_priv->irq_lock);
648 if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
649 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
650 ret = true;
651 }
652 spin_unlock_irq(&dev_priv->irq_lock);
653
654 return ret;
655}
656
657void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
658{
659 if (pin == HPD_NONE)
660 return;
661
662 spin_lock_irq(&dev_priv->irq_lock);
663 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
664 spin_unlock_irq(&dev_priv->irq_lock);
665}