blob: 7d210097eefa96a2b7e1add2d90668c98fdbaf58 [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
Imre Deakcc24fcd2015-07-21 15:32:45 -070079bool intel_hpd_pin_to_port(enum hpd_pin pin, enum port *port)
Jani Nikula77913b32015-06-18 13:06:16 +030080{
81 switch (pin) {
Imre Deakcc24fcd2015-07-21 15:32:45 -070082 case HPD_PORT_A:
83 *port = PORT_A;
84 return true;
Jani Nikula77913b32015-06-18 13:06:16 +030085 case HPD_PORT_B:
Imre Deakcc24fcd2015-07-21 15:32:45 -070086 *port = PORT_B;
87 return true;
Jani Nikula77913b32015-06-18 13:06:16 +030088 case HPD_PORT_C:
Imre Deakcc24fcd2015-07-21 15:32:45 -070089 *port = PORT_C;
90 return true;
Jani Nikula77913b32015-06-18 13:06:16 +030091 case HPD_PORT_D:
Imre Deakcc24fcd2015-07-21 15:32:45 -070092 *port = PORT_D;
93 return true;
Xiong Zhang26951ca2015-08-17 15:55:50 +080094 case HPD_PORT_E:
95 *port = PORT_E;
96 return true;
Jani Nikula77913b32015-06-18 13:06:16 +030097 default:
Imre Deakcc24fcd2015-07-21 15:32:45 -070098 return false; /* no hpd */
Jani Nikula77913b32015-06-18 13:06:16 +030099 }
100}
101
102#define HPD_STORM_DETECT_PERIOD 1000
Jani Nikula77913b32015-06-18 13:06:16 +0300103#define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000)
104
105/**
106 * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
107 * @dev_priv: private driver data pointer
108 * @pin: the pin to gather stats on
109 *
110 * Gather stats about HPD irqs from the specified @pin, and detect irq
111 * storms. Only the pin specific stats and state are changed, the caller is
112 * responsible for further action.
113 *
Lyude317eaa92017-02-03 21:18:25 -0500114 * The number of irqs that are allowed within @HPD_STORM_DETECT_PERIOD is
115 * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
116 * @HPD_STORM_DEFAULT_THRESHOLD. If this threshold is exceeded, it's
117 * considered an irq storm and the irq state is set to @HPD_MARK_DISABLED.
118 *
119 * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
120 * and should only be adjusted for automated hotplug testing.
Jani Nikula77913b32015-06-18 13:06:16 +0300121 *
122 * Return true if an irq storm was detected on @pin.
123 */
124static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
125 enum hpd_pin pin)
126{
127 unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
128 unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
Lyude317eaa92017-02-03 21:18:25 -0500129 const int threshold = dev_priv->hotplug.hpd_storm_threshold;
Jani Nikula77913b32015-06-18 13:06:16 +0300130 bool storm = false;
131
132 if (!time_in_range(jiffies, start, end)) {
133 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
134 dev_priv->hotplug.stats[pin].count = 0;
135 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
Lyude317eaa92017-02-03 21:18:25 -0500136 } else if (dev_priv->hotplug.stats[pin].count > threshold &&
137 threshold) {
Jani Nikula77913b32015-06-18 13:06:16 +0300138 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
139 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
140 storm = true;
141 } else {
142 dev_priv->hotplug.stats[pin].count++;
143 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
144 dev_priv->hotplug.stats[pin].count);
145 }
146
147 return storm;
148}
149
150static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
151{
Chris Wilson91c8a322016-07-05 10:40:23 +0100152 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300153 struct intel_connector *intel_connector;
154 struct intel_encoder *intel_encoder;
155 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100156 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300157 enum hpd_pin pin;
158 bool hpd_disabled = false;
159
Chris Wilson67520412017-03-02 13:28:01 +0000160 lockdep_assert_held(&dev_priv->irq_lock);
Jani Nikula77913b32015-06-18 13:06:16 +0300161
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100162 drm_connector_list_iter_begin(dev, &conn_iter);
163 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300164 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
165 continue;
166
167 intel_connector = to_intel_connector(connector);
168 intel_encoder = intel_connector->encoder;
169 if (!intel_encoder)
170 continue;
171
172 pin = intel_encoder->hpd_pin;
173 if (pin == HPD_NONE ||
174 dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
175 continue;
176
177 DRM_INFO("HPD interrupt storm detected on connector %s: "
178 "switching from hotplug detection to polling\n",
179 connector->name);
180
181 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
182 connector->polled = DRM_CONNECTOR_POLL_CONNECT
183 | DRM_CONNECTOR_POLL_DISCONNECT;
184 hpd_disabled = true;
185 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100186 drm_connector_list_iter_end(&conn_iter);
Jani Nikula77913b32015-06-18 13:06:16 +0300187
188 /* Enable polling and queue hotplug re-enabling. */
189 if (hpd_disabled) {
Dave Airliec4d79c22017-01-27 12:04:08 +1000190 drm_kms_helper_poll_enable(dev);
Jani Nikula77913b32015-06-18 13:06:16 +0300191 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
192 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
193 }
194}
195
196static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
197{
198 struct drm_i915_private *dev_priv =
199 container_of(work, typeof(*dev_priv),
200 hotplug.reenable_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +0100201 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300202 int i;
203
204 intel_runtime_pm_get(dev_priv);
205
206 spin_lock_irq(&dev_priv->irq_lock);
207 for_each_hpd_pin(i) {
208 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100209 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300210
211 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
212 continue;
213
214 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
215
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100216 drm_connector_list_iter_begin(dev, &conn_iter);
217 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300218 struct intel_connector *intel_connector = to_intel_connector(connector);
219
220 if (intel_connector->encoder->hpd_pin == i) {
221 if (connector->polled != intel_connector->polled)
222 DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
223 connector->name);
224 connector->polled = intel_connector->polled;
225 if (!connector->polled)
226 connector->polled = DRM_CONNECTOR_POLL_HPD;
227 }
228 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100229 drm_connector_list_iter_end(&conn_iter);
Jani Nikula77913b32015-06-18 13:06:16 +0300230 }
Chris Wilson262fd482017-02-15 13:15:47 +0000231 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100232 dev_priv->display.hpd_irq_setup(dev_priv);
Jani Nikula77913b32015-06-18 13:06:16 +0300233 spin_unlock_irq(&dev_priv->irq_lock);
234
235 intel_runtime_pm_put(dev_priv);
236}
237
238static bool intel_hpd_irq_event(struct drm_device *dev,
239 struct drm_connector *connector)
240{
241 enum drm_connector_status old_status;
242
243 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
244 old_status = connector->status;
245
246 connector->status = connector->funcs->detect(connector, false);
247 if (old_status == connector->status)
248 return false;
249
250 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
251 connector->base.id,
252 connector->name,
253 drm_get_connector_status_name(old_status),
254 drm_get_connector_status_name(connector->status));
255
256 return true;
257}
258
259static void i915_digport_work_func(struct work_struct *work)
260{
261 struct drm_i915_private *dev_priv =
262 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
263 u32 long_port_mask, short_port_mask;
264 struct intel_digital_port *intel_dig_port;
265 int i;
266 u32 old_bits = 0;
267
268 spin_lock_irq(&dev_priv->irq_lock);
269 long_port_mask = dev_priv->hotplug.long_port_mask;
270 dev_priv->hotplug.long_port_mask = 0;
271 short_port_mask = dev_priv->hotplug.short_port_mask;
272 dev_priv->hotplug.short_port_mask = 0;
273 spin_unlock_irq(&dev_priv->irq_lock);
274
275 for (i = 0; i < I915_MAX_PORTS; i++) {
276 bool valid = false;
277 bool long_hpd = false;
278 intel_dig_port = dev_priv->hotplug.irq_port[i];
279 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
280 continue;
281
282 if (long_port_mask & (1 << i)) {
283 valid = true;
284 long_hpd = true;
285 } else if (short_port_mask & (1 << i))
286 valid = true;
287
288 if (valid) {
289 enum irqreturn ret;
290
291 ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
292 if (ret == IRQ_NONE) {
293 /* fall back to old school hpd */
294 old_bits |= (1 << intel_dig_port->base.hpd_pin);
295 }
296 }
297 }
298
299 if (old_bits) {
300 spin_lock_irq(&dev_priv->irq_lock);
301 dev_priv->hotplug.event_bits |= old_bits;
302 spin_unlock_irq(&dev_priv->irq_lock);
303 schedule_work(&dev_priv->hotplug.hotplug_work);
304 }
305}
306
307/*
308 * Handle hotplug events outside the interrupt handler proper.
309 */
310static void i915_hotplug_work_func(struct work_struct *work)
311{
312 struct drm_i915_private *dev_priv =
313 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
Chris Wilson91c8a322016-07-05 10:40:23 +0100314 struct drm_device *dev = &dev_priv->drm;
Jani Nikula77913b32015-06-18 13:06:16 +0300315 struct intel_connector *intel_connector;
316 struct intel_encoder *intel_encoder;
317 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100318 struct drm_connector_list_iter conn_iter;
Jani Nikula77913b32015-06-18 13:06:16 +0300319 bool changed = false;
320 u32 hpd_event_bits;
321
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100322 mutex_lock(&dev->mode_config.mutex);
Jani Nikula77913b32015-06-18 13:06:16 +0300323 DRM_DEBUG_KMS("running encoder hotplug functions\n");
324
325 spin_lock_irq(&dev_priv->irq_lock);
326
327 hpd_event_bits = dev_priv->hotplug.event_bits;
328 dev_priv->hotplug.event_bits = 0;
329
330 /* Disable hotplug on connectors that hit an irq storm. */
331 intel_hpd_irq_storm_disable(dev_priv);
332
333 spin_unlock_irq(&dev_priv->irq_lock);
334
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100335 drm_connector_list_iter_begin(dev, &conn_iter);
336 drm_for_each_connector_iter(connector, &conn_iter) {
Jani Nikula77913b32015-06-18 13:06:16 +0300337 intel_connector = to_intel_connector(connector);
338 if (!intel_connector->encoder)
339 continue;
340 intel_encoder = intel_connector->encoder;
341 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
342 DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
343 connector->name, intel_encoder->hpd_pin);
344 if (intel_encoder->hot_plug)
345 intel_encoder->hot_plug(intel_encoder);
346 if (intel_hpd_irq_event(dev, connector))
347 changed = true;
348 }
349 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100350 drm_connector_list_iter_end(&conn_iter);
351 mutex_unlock(&dev->mode_config.mutex);
Jani Nikula77913b32015-06-18 13:06:16 +0300352
353 if (changed)
354 drm_kms_helper_hotplug_event(dev);
355}
356
357
358/**
359 * intel_hpd_irq_handler - main hotplug irq handler
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100360 * @dev_priv: drm_i915_private
Jani Nikula77913b32015-06-18 13:06:16 +0300361 * @pin_mask: a mask of hpd pins that have triggered the irq
362 * @long_mask: a mask of hpd pins that may be long hpd pulses
363 *
364 * This is the main hotplug irq handler for all platforms. The platform specific
365 * irq handlers call the platform specific hotplug irq handlers, which read and
366 * decode the appropriate registers into bitmasks about hpd pins that have
367 * triggered (@pin_mask), and which of those pins may be long pulses
368 * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
369 * is not a digital port.
370 *
371 * Here, we do hotplug irq storm detection and mitigation, and pass further
372 * processing to appropriate bottom halves.
373 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100374void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
Jani Nikula77913b32015-06-18 13:06:16 +0300375 u32 pin_mask, u32 long_mask)
376{
Jani Nikula77913b32015-06-18 13:06:16 +0300377 int i;
378 enum port port;
379 bool storm_detected = false;
380 bool queue_dig = false, queue_hp = false;
381 bool is_dig_port;
382
383 if (!pin_mask)
384 return;
385
386 spin_lock(&dev_priv->irq_lock);
387 for_each_hpd_pin(i) {
388 if (!(BIT(i) & pin_mask))
389 continue;
390
Imre Deakcc24fcd2015-07-21 15:32:45 -0700391 is_dig_port = intel_hpd_pin_to_port(i, &port) &&
392 dev_priv->hotplug.irq_port[port];
Jani Nikula77913b32015-06-18 13:06:16 +0300393
394 if (is_dig_port) {
395 bool long_hpd = long_mask & BIT(i);
396
397 DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
398 long_hpd ? "long" : "short");
399 /*
400 * For long HPD pulses we want to have the digital queue happen,
401 * but we still want HPD storm detection to function.
402 */
403 queue_dig = true;
404 if (long_hpd) {
405 dev_priv->hotplug.long_port_mask |= (1 << port);
406 } else {
407 /* for short HPD just trigger the digital queue */
408 dev_priv->hotplug.short_port_mask |= (1 << port);
409 continue;
410 }
411 }
412
413 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
414 /*
415 * On GMCH platforms the interrupt mask bits only
416 * prevent irq generation, not the setting of the
417 * hotplug bits itself. So only WARN about unexpected
418 * interrupts on saner platforms.
419 */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100420 WARN_ONCE(!HAS_GMCH_DISPLAY(dev_priv),
Jani Nikula77913b32015-06-18 13:06:16 +0300421 "Received HPD interrupt on pin %d although disabled\n", i);
422 continue;
423 }
424
425 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
426 continue;
427
428 if (!is_dig_port) {
429 dev_priv->hotplug.event_bits |= BIT(i);
430 queue_hp = true;
431 }
432
433 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
434 dev_priv->hotplug.event_bits &= ~BIT(i);
435 storm_detected = true;
436 }
437 }
438
Chris Wilson262fd482017-02-15 13:15:47 +0000439 if (storm_detected && dev_priv->display_irqs_enabled)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +0100440 dev_priv->display.hpd_irq_setup(dev_priv);
Jani Nikula77913b32015-06-18 13:06:16 +0300441 spin_unlock(&dev_priv->irq_lock);
442
443 /*
444 * Our hotplug handler can grab modeset locks (by calling down into the
445 * fb helpers). Hence it must not be run on our own dev-priv->wq work
446 * queue for otherwise the flush_work in the pageflip code will
447 * deadlock.
448 */
449 if (queue_dig)
450 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
451 if (queue_hp)
452 schedule_work(&dev_priv->hotplug.hotplug_work);
453}
454
455/**
456 * intel_hpd_init - initializes and enables hpd support
457 * @dev_priv: i915 device instance
458 *
459 * This function enables the hotplug support. It requires that interrupts have
460 * already been enabled with intel_irq_init_hw(). From this point on hotplug and
461 * poll request can run concurrently to other code, so locking rules must be
462 * obeyed.
463 *
464 * This is a separate step from interrupt enabling to simplify the locking rules
465 * in the driver load and resume code.
Lyude19625e82016-06-21 17:03:44 -0400466 *
467 * Also see: intel_hpd_poll_init(), which enables connector polling
Jani Nikula77913b32015-06-18 13:06:16 +0300468 */
469void intel_hpd_init(struct drm_i915_private *dev_priv)
470{
Jani Nikula77913b32015-06-18 13:06:16 +0300471 int i;
472
473 for_each_hpd_pin(i) {
474 dev_priv->hotplug.stats[i].count = 0;
475 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
476 }
Lyude07c51912016-01-07 10:43:28 -0500477
Lyude19625e82016-06-21 17:03:44 -0400478 WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
479 schedule_work(&dev_priv->hotplug.poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300480
481 /*
482 * Interrupt setup is already guaranteed to be single-threaded, this is
483 * just to make the assert_spin_locked checks happy.
484 */
Chris Wilson262fd482017-02-15 13:15:47 +0000485 if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
486 spin_lock_irq(&dev_priv->irq_lock);
487 if (dev_priv->display_irqs_enabled)
488 dev_priv->display.hpd_irq_setup(dev_priv);
489 spin_unlock_irq(&dev_priv->irq_lock);
490 }
Jani Nikula77913b32015-06-18 13:06:16 +0300491}
492
Chris Wilson24808e92016-08-17 12:09:06 +0100493static void i915_hpd_poll_init_work(struct work_struct *work)
494{
Lyude19625e82016-06-21 17:03:44 -0400495 struct drm_i915_private *dev_priv =
496 container_of(work, struct drm_i915_private,
497 hotplug.poll_init_work);
498 struct drm_device *dev = &dev_priv->drm;
Lyude19625e82016-06-21 17:03:44 -0400499 struct drm_connector *connector;
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100500 struct drm_connector_list_iter conn_iter;
Lyude19625e82016-06-21 17:03:44 -0400501 bool enabled;
502
503 mutex_lock(&dev->mode_config.mutex);
504
505 enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
506
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100507 drm_connector_list_iter_begin(dev, &conn_iter);
508 drm_for_each_connector_iter(connector, &conn_iter) {
Lyude19625e82016-06-21 17:03:44 -0400509 struct intel_connector *intel_connector =
510 to_intel_connector(connector);
511 connector->polled = intel_connector->polled;
512
513 /* MST has a dynamic intel_connector->encoder and it's reprobing
514 * is all handled by the MST helpers. */
515 if (intel_connector->mst_port)
516 continue;
517
Tvrtko Ursulin56b857a2016-11-07 09:29:20 +0000518 if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
Lyude19625e82016-06-21 17:03:44 -0400519 intel_connector->encoder->hpd_pin > HPD_NONE) {
520 connector->polled = enabled ?
521 DRM_CONNECTOR_POLL_CONNECT |
522 DRM_CONNECTOR_POLL_DISCONNECT :
523 DRM_CONNECTOR_POLL_HPD;
524 }
525 }
Daniel Vettercc3ca4f2017-03-01 10:52:22 +0100526 drm_connector_list_iter_end(&conn_iter);
Lyude19625e82016-06-21 17:03:44 -0400527
528 if (enabled)
Dave Airliec4d79c22017-01-27 12:04:08 +1000529 drm_kms_helper_poll_enable(dev);
Lyude19625e82016-06-21 17:03:44 -0400530
531 mutex_unlock(&dev->mode_config.mutex);
532
533 /*
534 * We might have missed any hotplugs that happened while we were
535 * in the middle of disabling polling
536 */
537 if (!enabled)
538 drm_helper_hpd_irq_event(dev);
539}
540
541/**
542 * intel_hpd_poll_init - enables/disables polling for connectors with hpd
543 * @dev_priv: i915 device instance
Lyude19625e82016-06-21 17:03:44 -0400544 *
545 * This function enables polling for all connectors, regardless of whether or
546 * not they support hotplug detection. Under certain conditions HPD may not be
547 * functional. On most Intel GPUs, this happens when we enter runtime suspend.
548 * On Valleyview and Cherryview systems, this also happens when we shut off all
549 * of the powerwells.
550 *
551 * Since this function can get called in contexts where we're already holding
552 * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
553 * worker.
554 *
555 * Also see: intel_hpd_init(), which restores hpd handling.
556 */
557void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
558{
559 WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
560
561 /*
562 * We might already be holding dev->mode_config.mutex, so do this in a
563 * seperate worker
564 * As well, there's no issue if we race here since we always reschedule
565 * this worker anyway
566 */
567 schedule_work(&dev_priv->hotplug.poll_init_work);
568}
569
Jani Nikula77913b32015-06-18 13:06:16 +0300570void intel_hpd_init_work(struct drm_i915_private *dev_priv)
571{
572 INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
573 INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
Lyude19625e82016-06-21 17:03:44 -0400574 INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300575 INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
576 intel_hpd_irq_storm_reenable_work);
577}
578
579void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
580{
581 spin_lock_irq(&dev_priv->irq_lock);
582
583 dev_priv->hotplug.long_port_mask = 0;
584 dev_priv->hotplug.short_port_mask = 0;
585 dev_priv->hotplug.event_bits = 0;
586
587 spin_unlock_irq(&dev_priv->irq_lock);
588
589 cancel_work_sync(&dev_priv->hotplug.dig_port_work);
590 cancel_work_sync(&dev_priv->hotplug.hotplug_work);
Lyude19625e82016-06-21 17:03:44 -0400591 cancel_work_sync(&dev_priv->hotplug.poll_init_work);
Jani Nikula77913b32015-06-18 13:06:16 +0300592 cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
593}
Lyudeb236d7c82016-06-21 17:03:43 -0400594
595bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
596{
597 bool ret = false;
598
599 if (pin == HPD_NONE)
600 return false;
601
602 spin_lock_irq(&dev_priv->irq_lock);
603 if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
604 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
605 ret = true;
606 }
607 spin_unlock_irq(&dev_priv->irq_lock);
608
609 return ret;
610}
611
612void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
613{
614 if (pin == HPD_NONE)
615 return;
616
617 spin_lock_irq(&dev_priv->irq_lock);
618 dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
619 spin_unlock_irq(&dev_priv->irq_lock);
620}