drm/i915/hotplug: document the hotplug handling in the driver
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / i915 / intel_hotplug.c
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <linux/kernel.h>
25
26 #include <drm/drmP.h>
27 #include <drm/i915_drm.h>
28
29 #include "i915_drv.h"
30 #include "intel_drv.h"
31
32 /**
33  * DOC: Hotplug
34  *
35  * Simply put, hotplug occurs when a display is connected to or disconnected
36  * from the system. However, there may be adapters and docking stations and
37  * Display Port short pulses and MST devices involved, complicating matters.
38  *
39  * Hotplug in i915 is handled in many different levels of abstraction.
40  *
41  * The platform dependent interrupt handling code in i915_irq.c enables,
42  * disables, and does preliminary handling of the interrupts. The interrupt
43  * handlers gather the hotplug detect (HPD) information from relevant registers
44  * into a platform independent mask of hotplug pins that have fired.
45  *
46  * The platform independent interrupt handler intel_hpd_irq_handler() in
47  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
48  * further processing to appropriate bottom halves (Display Port specific and
49  * regular hotplug).
50  *
51  * The Display Port work function i915_digport_work_func() calls into
52  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
53  * pulses, with failures and non-MST long pulses triggering regular hotplug
54  * processing on the connector.
55  *
56  * The regular hotplug work function i915_hotplug_work_func() calls connector
57  * detect hooks, and, if connector status changes, triggers sending of hotplug
58  * uevent to userspace via drm_kms_helper_hotplug_event().
59  *
60  * Finally, the userspace is responsible for triggering a modeset upon receiving
61  * the hotplug uevent, disabling or enabling the crtc as needed.
62  *
63  * The hotplug interrupt storm detection and mitigation code keeps track of the
64  * number of interrupts per hotplug pin per a period of time, and if the number
65  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
66  * while before being re-enabled. The intention is to mitigate issues raising
67  * from broken hardware triggering massive amounts of interrupts and grinding
68  * the system to a halt.
69  */
70
71 enum port intel_hpd_pin_to_port(enum hpd_pin pin)
72 {
73         switch (pin) {
74         case HPD_PORT_B:
75                 return PORT_B;
76         case HPD_PORT_C:
77                 return PORT_C;
78         case HPD_PORT_D:
79                 return PORT_D;
80         default:
81                 return PORT_A; /* no hpd */
82         }
83 }
84
85 #define HPD_STORM_DETECT_PERIOD         1000
86 #define HPD_STORM_THRESHOLD             5
87 #define HPD_STORM_REENABLE_DELAY        (2 * 60 * 1000)
88
89 /**
90  * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin
91  * @dev_priv: private driver data pointer
92  * @pin: the pin to gather stats on
93  *
94  * Gather stats about HPD irqs from the specified @pin, and detect irq
95  * storms. Only the pin specific stats and state are changed, the caller is
96  * responsible for further action.
97  *
98  * @HPD_STORM_THRESHOLD irqs are allowed within @HPD_STORM_DETECT_PERIOD ms,
99  * otherwise it's considered an irq storm, and the irq state is set to
100  * @HPD_MARK_DISABLED.
101  *
102  * Return true if an irq storm was detected on @pin.
103  */
104 static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
105                                        enum hpd_pin pin)
106 {
107         unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies;
108         unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
109         bool storm = false;
110
111         if (!time_in_range(jiffies, start, end)) {
112                 dev_priv->hotplug.stats[pin].last_jiffies = jiffies;
113                 dev_priv->hotplug.stats[pin].count = 0;
114                 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin);
115         } else if (dev_priv->hotplug.stats[pin].count > HPD_STORM_THRESHOLD) {
116                 dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED;
117                 DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
118                 storm = true;
119         } else {
120                 dev_priv->hotplug.stats[pin].count++;
121                 DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
122                               dev_priv->hotplug.stats[pin].count);
123         }
124
125         return storm;
126 }
127
128 static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv)
129 {
130         struct drm_device *dev = dev_priv->dev;
131         struct drm_mode_config *mode_config = &dev->mode_config;
132         struct intel_connector *intel_connector;
133         struct intel_encoder *intel_encoder;
134         struct drm_connector *connector;
135         enum hpd_pin pin;
136         bool hpd_disabled = false;
137
138         assert_spin_locked(&dev_priv->irq_lock);
139
140         list_for_each_entry(connector, &mode_config->connector_list, head) {
141                 if (connector->polled != DRM_CONNECTOR_POLL_HPD)
142                         continue;
143
144                 intel_connector = to_intel_connector(connector);
145                 intel_encoder = intel_connector->encoder;
146                 if (!intel_encoder)
147                         continue;
148
149                 pin = intel_encoder->hpd_pin;
150                 if (pin == HPD_NONE ||
151                     dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
152                         continue;
153
154                 DRM_INFO("HPD interrupt storm detected on connector %s: "
155                          "switching from hotplug detection to polling\n",
156                          connector->name);
157
158                 dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
159                 connector->polled = DRM_CONNECTOR_POLL_CONNECT
160                         | DRM_CONNECTOR_POLL_DISCONNECT;
161                 hpd_disabled = true;
162         }
163
164         /* Enable polling and queue hotplug re-enabling. */
165         if (hpd_disabled) {
166                 drm_kms_helper_poll_enable(dev);
167                 mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
168                                  msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
169         }
170 }
171
172 static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
173 {
174         struct drm_i915_private *dev_priv =
175                 container_of(work, typeof(*dev_priv),
176                              hotplug.reenable_work.work);
177         struct drm_device *dev = dev_priv->dev;
178         struct drm_mode_config *mode_config = &dev->mode_config;
179         int i;
180
181         intel_runtime_pm_get(dev_priv);
182
183         spin_lock_irq(&dev_priv->irq_lock);
184         for_each_hpd_pin(i) {
185                 struct drm_connector *connector;
186
187                 if (dev_priv->hotplug.stats[i].state != HPD_DISABLED)
188                         continue;
189
190                 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
191
192                 list_for_each_entry(connector, &mode_config->connector_list, head) {
193                         struct intel_connector *intel_connector = to_intel_connector(connector);
194
195                         if (intel_connector->encoder->hpd_pin == i) {
196                                 if (connector->polled != intel_connector->polled)
197                                         DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
198                                                          connector->name);
199                                 connector->polled = intel_connector->polled;
200                                 if (!connector->polled)
201                                         connector->polled = DRM_CONNECTOR_POLL_HPD;
202                         }
203                 }
204         }
205         if (dev_priv->display.hpd_irq_setup)
206                 dev_priv->display.hpd_irq_setup(dev);
207         spin_unlock_irq(&dev_priv->irq_lock);
208
209         intel_runtime_pm_put(dev_priv);
210 }
211
212 static bool intel_hpd_irq_event(struct drm_device *dev,
213                                 struct drm_connector *connector)
214 {
215         enum drm_connector_status old_status;
216
217         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
218         old_status = connector->status;
219
220         connector->status = connector->funcs->detect(connector, false);
221         if (old_status == connector->status)
222                 return false;
223
224         DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
225                       connector->base.id,
226                       connector->name,
227                       drm_get_connector_status_name(old_status),
228                       drm_get_connector_status_name(connector->status));
229
230         return true;
231 }
232
233 static void i915_digport_work_func(struct work_struct *work)
234 {
235         struct drm_i915_private *dev_priv =
236                 container_of(work, struct drm_i915_private, hotplug.dig_port_work);
237         u32 long_port_mask, short_port_mask;
238         struct intel_digital_port *intel_dig_port;
239         int i;
240         u32 old_bits = 0;
241
242         spin_lock_irq(&dev_priv->irq_lock);
243         long_port_mask = dev_priv->hotplug.long_port_mask;
244         dev_priv->hotplug.long_port_mask = 0;
245         short_port_mask = dev_priv->hotplug.short_port_mask;
246         dev_priv->hotplug.short_port_mask = 0;
247         spin_unlock_irq(&dev_priv->irq_lock);
248
249         for (i = 0; i < I915_MAX_PORTS; i++) {
250                 bool valid = false;
251                 bool long_hpd = false;
252                 intel_dig_port = dev_priv->hotplug.irq_port[i];
253                 if (!intel_dig_port || !intel_dig_port->hpd_pulse)
254                         continue;
255
256                 if (long_port_mask & (1 << i))  {
257                         valid = true;
258                         long_hpd = true;
259                 } else if (short_port_mask & (1 << i))
260                         valid = true;
261
262                 if (valid) {
263                         enum irqreturn ret;
264
265                         ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
266                         if (ret == IRQ_NONE) {
267                                 /* fall back to old school hpd */
268                                 old_bits |= (1 << intel_dig_port->base.hpd_pin);
269                         }
270                 }
271         }
272
273         if (old_bits) {
274                 spin_lock_irq(&dev_priv->irq_lock);
275                 dev_priv->hotplug.event_bits |= old_bits;
276                 spin_unlock_irq(&dev_priv->irq_lock);
277                 schedule_work(&dev_priv->hotplug.hotplug_work);
278         }
279 }
280
281 /*
282  * Handle hotplug events outside the interrupt handler proper.
283  */
284 static void i915_hotplug_work_func(struct work_struct *work)
285 {
286         struct drm_i915_private *dev_priv =
287                 container_of(work, struct drm_i915_private, hotplug.hotplug_work);
288         struct drm_device *dev = dev_priv->dev;
289         struct drm_mode_config *mode_config = &dev->mode_config;
290         struct intel_connector *intel_connector;
291         struct intel_encoder *intel_encoder;
292         struct drm_connector *connector;
293         bool changed = false;
294         u32 hpd_event_bits;
295
296         mutex_lock(&mode_config->mutex);
297         DRM_DEBUG_KMS("running encoder hotplug functions\n");
298
299         spin_lock_irq(&dev_priv->irq_lock);
300
301         hpd_event_bits = dev_priv->hotplug.event_bits;
302         dev_priv->hotplug.event_bits = 0;
303
304         /* Disable hotplug on connectors that hit an irq storm. */
305         intel_hpd_irq_storm_disable(dev_priv);
306
307         spin_unlock_irq(&dev_priv->irq_lock);
308
309         list_for_each_entry(connector, &mode_config->connector_list, head) {
310                 intel_connector = to_intel_connector(connector);
311                 if (!intel_connector->encoder)
312                         continue;
313                 intel_encoder = intel_connector->encoder;
314                 if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
315                         DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
316                                       connector->name, intel_encoder->hpd_pin);
317                         if (intel_encoder->hot_plug)
318                                 intel_encoder->hot_plug(intel_encoder);
319                         if (intel_hpd_irq_event(dev, connector))
320                                 changed = true;
321                 }
322         }
323         mutex_unlock(&mode_config->mutex);
324
325         if (changed)
326                 drm_kms_helper_hotplug_event(dev);
327 }
328
329
330 /**
331  * intel_hpd_irq_handler - main hotplug irq handler
332  * @dev: drm device
333  * @pin_mask: a mask of hpd pins that have triggered the irq
334  * @long_mask: a mask of hpd pins that may be long hpd pulses
335  *
336  * This is the main hotplug irq handler for all platforms. The platform specific
337  * irq handlers call the platform specific hotplug irq handlers, which read and
338  * decode the appropriate registers into bitmasks about hpd pins that have
339  * triggered (@pin_mask), and which of those pins may be long pulses
340  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
341  * is not a digital port.
342  *
343  * Here, we do hotplug irq storm detection and mitigation, and pass further
344  * processing to appropriate bottom halves.
345  */
346 void intel_hpd_irq_handler(struct drm_device *dev,
347                            u32 pin_mask, u32 long_mask)
348 {
349         struct drm_i915_private *dev_priv = dev->dev_private;
350         int i;
351         enum port port;
352         bool storm_detected = false;
353         bool queue_dig = false, queue_hp = false;
354         bool is_dig_port;
355
356         if (!pin_mask)
357                 return;
358
359         spin_lock(&dev_priv->irq_lock);
360         for_each_hpd_pin(i) {
361                 if (!(BIT(i) & pin_mask))
362                         continue;
363
364                 port = intel_hpd_pin_to_port(i);
365                 is_dig_port = port && dev_priv->hotplug.irq_port[port];
366
367                 if (is_dig_port) {
368                         bool long_hpd = long_mask & BIT(i);
369
370                         DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port),
371                                          long_hpd ? "long" : "short");
372                         /*
373                          * For long HPD pulses we want to have the digital queue happen,
374                          * but we still want HPD storm detection to function.
375                          */
376                         queue_dig = true;
377                         if (long_hpd) {
378                                 dev_priv->hotplug.long_port_mask |= (1 << port);
379                         } else {
380                                 /* for short HPD just trigger the digital queue */
381                                 dev_priv->hotplug.short_port_mask |= (1 << port);
382                                 continue;
383                         }
384                 }
385
386                 if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) {
387                         /*
388                          * On GMCH platforms the interrupt mask bits only
389                          * prevent irq generation, not the setting of the
390                          * hotplug bits itself. So only WARN about unexpected
391                          * interrupts on saner platforms.
392                          */
393                         WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
394                                   "Received HPD interrupt on pin %d although disabled\n", i);
395                         continue;
396                 }
397
398                 if (dev_priv->hotplug.stats[i].state != HPD_ENABLED)
399                         continue;
400
401                 if (!is_dig_port) {
402                         dev_priv->hotplug.event_bits |= BIT(i);
403                         queue_hp = true;
404                 }
405
406                 if (intel_hpd_irq_storm_detect(dev_priv, i)) {
407                         dev_priv->hotplug.event_bits &= ~BIT(i);
408                         storm_detected = true;
409                 }
410         }
411
412         if (storm_detected)
413                 dev_priv->display.hpd_irq_setup(dev);
414         spin_unlock(&dev_priv->irq_lock);
415
416         /*
417          * Our hotplug handler can grab modeset locks (by calling down into the
418          * fb helpers). Hence it must not be run on our own dev-priv->wq work
419          * queue for otherwise the flush_work in the pageflip code will
420          * deadlock.
421          */
422         if (queue_dig)
423                 queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
424         if (queue_hp)
425                 schedule_work(&dev_priv->hotplug.hotplug_work);
426 }
427
428 /**
429  * intel_hpd_init - initializes and enables hpd support
430  * @dev_priv: i915 device instance
431  *
432  * This function enables the hotplug support. It requires that interrupts have
433  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
434  * poll request can run concurrently to other code, so locking rules must be
435  * obeyed.
436  *
437  * This is a separate step from interrupt enabling to simplify the locking rules
438  * in the driver load and resume code.
439  */
440 void intel_hpd_init(struct drm_i915_private *dev_priv)
441 {
442         struct drm_device *dev = dev_priv->dev;
443         struct drm_mode_config *mode_config = &dev->mode_config;
444         struct drm_connector *connector;
445         int i;
446
447         for_each_hpd_pin(i) {
448                 dev_priv->hotplug.stats[i].count = 0;
449                 dev_priv->hotplug.stats[i].state = HPD_ENABLED;
450         }
451         list_for_each_entry(connector, &mode_config->connector_list, head) {
452                 struct intel_connector *intel_connector = to_intel_connector(connector);
453                 connector->polled = intel_connector->polled;
454                 if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
455                         connector->polled = DRM_CONNECTOR_POLL_HPD;
456                 if (intel_connector->mst_port)
457                         connector->polled = DRM_CONNECTOR_POLL_HPD;
458         }
459
460         /*
461          * Interrupt setup is already guaranteed to be single-threaded, this is
462          * just to make the assert_spin_locked checks happy.
463          */
464         spin_lock_irq(&dev_priv->irq_lock);
465         if (dev_priv->display.hpd_irq_setup)
466                 dev_priv->display.hpd_irq_setup(dev);
467         spin_unlock_irq(&dev_priv->irq_lock);
468 }
469
470 void intel_hpd_init_work(struct drm_i915_private *dev_priv)
471 {
472         INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func);
473         INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
474         INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
475                           intel_hpd_irq_storm_reenable_work);
476 }
477
478 void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
479 {
480         spin_lock_irq(&dev_priv->irq_lock);
481
482         dev_priv->hotplug.long_port_mask = 0;
483         dev_priv->hotplug.short_port_mask = 0;
484         dev_priv->hotplug.event_bits = 0;
485
486         spin_unlock_irq(&dev_priv->irq_lock);
487
488         cancel_work_sync(&dev_priv->hotplug.dig_port_work);
489         cancel_work_sync(&dev_priv->hotplug.hotplug_work);
490         cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
491 }