Jani Nikula | 77913b3 | 2015-06-18 13:06:16 +0300 | [diff] [blame] | 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 | enum port intel_hpd_pin_to_port(enum hpd_pin pin) |
| 33 | { |
| 34 | switch (pin) { |
| 35 | case HPD_PORT_B: |
| 36 | return PORT_B; |
| 37 | case HPD_PORT_C: |
| 38 | return PORT_C; |
| 39 | case HPD_PORT_D: |
| 40 | return PORT_D; |
| 41 | default: |
| 42 | return PORT_A; /* no hpd */ |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #define HPD_STORM_DETECT_PERIOD 1000 |
| 47 | #define HPD_STORM_THRESHOLD 5 |
| 48 | #define HPD_STORM_REENABLE_DELAY (2 * 60 * 1000) |
| 49 | |
| 50 | /** |
| 51 | * intel_hpd_irq_storm_detect - gather stats and detect HPD irq storm on a pin |
| 52 | * @dev_priv: private driver data pointer |
| 53 | * @pin: the pin to gather stats on |
| 54 | * |
| 55 | * Gather stats about HPD irqs from the specified @pin, and detect irq |
| 56 | * storms. Only the pin specific stats and state are changed, the caller is |
| 57 | * responsible for further action. |
| 58 | * |
| 59 | * @HPD_STORM_THRESHOLD irqs are allowed within @HPD_STORM_DETECT_PERIOD ms, |
| 60 | * otherwise it's considered an irq storm, and the irq state is set to |
| 61 | * @HPD_MARK_DISABLED. |
| 62 | * |
| 63 | * Return true if an irq storm was detected on @pin. |
| 64 | */ |
| 65 | static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv, |
| 66 | enum hpd_pin pin) |
| 67 | { |
| 68 | unsigned long start = dev_priv->hotplug.stats[pin].last_jiffies; |
| 69 | unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD); |
| 70 | bool storm = false; |
| 71 | |
| 72 | if (!time_in_range(jiffies, start, end)) { |
| 73 | dev_priv->hotplug.stats[pin].last_jiffies = jiffies; |
| 74 | dev_priv->hotplug.stats[pin].count = 0; |
| 75 | DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", pin); |
| 76 | } else if (dev_priv->hotplug.stats[pin].count > HPD_STORM_THRESHOLD) { |
| 77 | dev_priv->hotplug.stats[pin].state = HPD_MARK_DISABLED; |
| 78 | DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin); |
| 79 | storm = true; |
| 80 | } else { |
| 81 | dev_priv->hotplug.stats[pin].count++; |
| 82 | DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin, |
| 83 | dev_priv->hotplug.stats[pin].count); |
| 84 | } |
| 85 | |
| 86 | return storm; |
| 87 | } |
| 88 | |
| 89 | static void intel_hpd_irq_storm_disable(struct drm_i915_private *dev_priv) |
| 90 | { |
| 91 | struct drm_device *dev = dev_priv->dev; |
| 92 | struct drm_mode_config *mode_config = &dev->mode_config; |
| 93 | struct intel_connector *intel_connector; |
| 94 | struct intel_encoder *intel_encoder; |
| 95 | struct drm_connector *connector; |
| 96 | enum hpd_pin pin; |
| 97 | bool hpd_disabled = false; |
| 98 | |
| 99 | assert_spin_locked(&dev_priv->irq_lock); |
| 100 | |
| 101 | list_for_each_entry(connector, &mode_config->connector_list, head) { |
| 102 | if (connector->polled != DRM_CONNECTOR_POLL_HPD) |
| 103 | continue; |
| 104 | |
| 105 | intel_connector = to_intel_connector(connector); |
| 106 | intel_encoder = intel_connector->encoder; |
| 107 | if (!intel_encoder) |
| 108 | continue; |
| 109 | |
| 110 | pin = intel_encoder->hpd_pin; |
| 111 | if (pin == HPD_NONE || |
| 112 | dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED) |
| 113 | continue; |
| 114 | |
| 115 | DRM_INFO("HPD interrupt storm detected on connector %s: " |
| 116 | "switching from hotplug detection to polling\n", |
| 117 | connector->name); |
| 118 | |
| 119 | dev_priv->hotplug.stats[pin].state = HPD_DISABLED; |
| 120 | connector->polled = DRM_CONNECTOR_POLL_CONNECT |
| 121 | | DRM_CONNECTOR_POLL_DISCONNECT; |
| 122 | hpd_disabled = true; |
| 123 | } |
| 124 | |
| 125 | /* Enable polling and queue hotplug re-enabling. */ |
| 126 | if (hpd_disabled) { |
| 127 | drm_kms_helper_poll_enable(dev); |
| 128 | mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work, |
| 129 | msecs_to_jiffies(HPD_STORM_REENABLE_DELAY)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void intel_hpd_irq_storm_reenable_work(struct work_struct *work) |
| 134 | { |
| 135 | struct drm_i915_private *dev_priv = |
| 136 | container_of(work, typeof(*dev_priv), |
| 137 | hotplug.reenable_work.work); |
| 138 | struct drm_device *dev = dev_priv->dev; |
| 139 | struct drm_mode_config *mode_config = &dev->mode_config; |
| 140 | int i; |
| 141 | |
| 142 | intel_runtime_pm_get(dev_priv); |
| 143 | |
| 144 | spin_lock_irq(&dev_priv->irq_lock); |
| 145 | for_each_hpd_pin(i) { |
| 146 | struct drm_connector *connector; |
| 147 | |
| 148 | if (dev_priv->hotplug.stats[i].state != HPD_DISABLED) |
| 149 | continue; |
| 150 | |
| 151 | dev_priv->hotplug.stats[i].state = HPD_ENABLED; |
| 152 | |
| 153 | list_for_each_entry(connector, &mode_config->connector_list, head) { |
| 154 | struct intel_connector *intel_connector = to_intel_connector(connector); |
| 155 | |
| 156 | if (intel_connector->encoder->hpd_pin == i) { |
| 157 | if (connector->polled != intel_connector->polled) |
| 158 | DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n", |
| 159 | connector->name); |
| 160 | connector->polled = intel_connector->polled; |
| 161 | if (!connector->polled) |
| 162 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | if (dev_priv->display.hpd_irq_setup) |
| 167 | dev_priv->display.hpd_irq_setup(dev); |
| 168 | spin_unlock_irq(&dev_priv->irq_lock); |
| 169 | |
| 170 | intel_runtime_pm_put(dev_priv); |
| 171 | } |
| 172 | |
| 173 | static bool intel_hpd_irq_event(struct drm_device *dev, |
| 174 | struct drm_connector *connector) |
| 175 | { |
| 176 | enum drm_connector_status old_status; |
| 177 | |
| 178 | WARN_ON(!mutex_is_locked(&dev->mode_config.mutex)); |
| 179 | old_status = connector->status; |
| 180 | |
| 181 | connector->status = connector->funcs->detect(connector, false); |
| 182 | if (old_status == connector->status) |
| 183 | return false; |
| 184 | |
| 185 | DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n", |
| 186 | connector->base.id, |
| 187 | connector->name, |
| 188 | drm_get_connector_status_name(old_status), |
| 189 | drm_get_connector_status_name(connector->status)); |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | static void i915_digport_work_func(struct work_struct *work) |
| 195 | { |
| 196 | struct drm_i915_private *dev_priv = |
| 197 | container_of(work, struct drm_i915_private, hotplug.dig_port_work); |
| 198 | u32 long_port_mask, short_port_mask; |
| 199 | struct intel_digital_port *intel_dig_port; |
| 200 | int i; |
| 201 | u32 old_bits = 0; |
| 202 | |
| 203 | spin_lock_irq(&dev_priv->irq_lock); |
| 204 | long_port_mask = dev_priv->hotplug.long_port_mask; |
| 205 | dev_priv->hotplug.long_port_mask = 0; |
| 206 | short_port_mask = dev_priv->hotplug.short_port_mask; |
| 207 | dev_priv->hotplug.short_port_mask = 0; |
| 208 | spin_unlock_irq(&dev_priv->irq_lock); |
| 209 | |
| 210 | for (i = 0; i < I915_MAX_PORTS; i++) { |
| 211 | bool valid = false; |
| 212 | bool long_hpd = false; |
| 213 | intel_dig_port = dev_priv->hotplug.irq_port[i]; |
| 214 | if (!intel_dig_port || !intel_dig_port->hpd_pulse) |
| 215 | continue; |
| 216 | |
| 217 | if (long_port_mask & (1 << i)) { |
| 218 | valid = true; |
| 219 | long_hpd = true; |
| 220 | } else if (short_port_mask & (1 << i)) |
| 221 | valid = true; |
| 222 | |
| 223 | if (valid) { |
| 224 | enum irqreturn ret; |
| 225 | |
| 226 | ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd); |
| 227 | if (ret == IRQ_NONE) { |
| 228 | /* fall back to old school hpd */ |
| 229 | old_bits |= (1 << intel_dig_port->base.hpd_pin); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (old_bits) { |
| 235 | spin_lock_irq(&dev_priv->irq_lock); |
| 236 | dev_priv->hotplug.event_bits |= old_bits; |
| 237 | spin_unlock_irq(&dev_priv->irq_lock); |
| 238 | schedule_work(&dev_priv->hotplug.hotplug_work); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Handle hotplug events outside the interrupt handler proper. |
| 244 | */ |
| 245 | static void i915_hotplug_work_func(struct work_struct *work) |
| 246 | { |
| 247 | struct drm_i915_private *dev_priv = |
| 248 | container_of(work, struct drm_i915_private, hotplug.hotplug_work); |
| 249 | struct drm_device *dev = dev_priv->dev; |
| 250 | struct drm_mode_config *mode_config = &dev->mode_config; |
| 251 | struct intel_connector *intel_connector; |
| 252 | struct intel_encoder *intel_encoder; |
| 253 | struct drm_connector *connector; |
| 254 | bool changed = false; |
| 255 | u32 hpd_event_bits; |
| 256 | |
| 257 | mutex_lock(&mode_config->mutex); |
| 258 | DRM_DEBUG_KMS("running encoder hotplug functions\n"); |
| 259 | |
| 260 | spin_lock_irq(&dev_priv->irq_lock); |
| 261 | |
| 262 | hpd_event_bits = dev_priv->hotplug.event_bits; |
| 263 | dev_priv->hotplug.event_bits = 0; |
| 264 | |
| 265 | /* Disable hotplug on connectors that hit an irq storm. */ |
| 266 | intel_hpd_irq_storm_disable(dev_priv); |
| 267 | |
| 268 | spin_unlock_irq(&dev_priv->irq_lock); |
| 269 | |
| 270 | list_for_each_entry(connector, &mode_config->connector_list, head) { |
| 271 | intel_connector = to_intel_connector(connector); |
| 272 | if (!intel_connector->encoder) |
| 273 | continue; |
| 274 | intel_encoder = intel_connector->encoder; |
| 275 | if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) { |
| 276 | DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n", |
| 277 | connector->name, intel_encoder->hpd_pin); |
| 278 | if (intel_encoder->hot_plug) |
| 279 | intel_encoder->hot_plug(intel_encoder); |
| 280 | if (intel_hpd_irq_event(dev, connector)) |
| 281 | changed = true; |
| 282 | } |
| 283 | } |
| 284 | mutex_unlock(&mode_config->mutex); |
| 285 | |
| 286 | if (changed) |
| 287 | drm_kms_helper_hotplug_event(dev); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * intel_hpd_irq_handler - main hotplug irq handler |
| 293 | * @dev: drm device |
| 294 | * @pin_mask: a mask of hpd pins that have triggered the irq |
| 295 | * @long_mask: a mask of hpd pins that may be long hpd pulses |
| 296 | * |
| 297 | * This is the main hotplug irq handler for all platforms. The platform specific |
| 298 | * irq handlers call the platform specific hotplug irq handlers, which read and |
| 299 | * decode the appropriate registers into bitmasks about hpd pins that have |
| 300 | * triggered (@pin_mask), and which of those pins may be long pulses |
| 301 | * (@long_mask). The @long_mask is ignored if the port corresponding to the pin |
| 302 | * is not a digital port. |
| 303 | * |
| 304 | * Here, we do hotplug irq storm detection and mitigation, and pass further |
| 305 | * processing to appropriate bottom halves. |
| 306 | */ |
| 307 | void intel_hpd_irq_handler(struct drm_device *dev, |
| 308 | u32 pin_mask, u32 long_mask) |
| 309 | { |
| 310 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 311 | int i; |
| 312 | enum port port; |
| 313 | bool storm_detected = false; |
| 314 | bool queue_dig = false, queue_hp = false; |
| 315 | bool is_dig_port; |
| 316 | |
| 317 | if (!pin_mask) |
| 318 | return; |
| 319 | |
| 320 | spin_lock(&dev_priv->irq_lock); |
| 321 | for_each_hpd_pin(i) { |
| 322 | if (!(BIT(i) & pin_mask)) |
| 323 | continue; |
| 324 | |
| 325 | port = intel_hpd_pin_to_port(i); |
| 326 | is_dig_port = port && dev_priv->hotplug.irq_port[port]; |
| 327 | |
| 328 | if (is_dig_port) { |
| 329 | bool long_hpd = long_mask & BIT(i); |
| 330 | |
| 331 | DRM_DEBUG_DRIVER("digital hpd port %c - %s\n", port_name(port), |
| 332 | long_hpd ? "long" : "short"); |
| 333 | /* |
| 334 | * For long HPD pulses we want to have the digital queue happen, |
| 335 | * but we still want HPD storm detection to function. |
| 336 | */ |
| 337 | queue_dig = true; |
| 338 | if (long_hpd) { |
| 339 | dev_priv->hotplug.long_port_mask |= (1 << port); |
| 340 | } else { |
| 341 | /* for short HPD just trigger the digital queue */ |
| 342 | dev_priv->hotplug.short_port_mask |= (1 << port); |
| 343 | continue; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (dev_priv->hotplug.stats[i].state == HPD_DISABLED) { |
| 348 | /* |
| 349 | * On GMCH platforms the interrupt mask bits only |
| 350 | * prevent irq generation, not the setting of the |
| 351 | * hotplug bits itself. So only WARN about unexpected |
| 352 | * interrupts on saner platforms. |
| 353 | */ |
| 354 | WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev), |
| 355 | "Received HPD interrupt on pin %d although disabled\n", i); |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | if (dev_priv->hotplug.stats[i].state != HPD_ENABLED) |
| 360 | continue; |
| 361 | |
| 362 | if (!is_dig_port) { |
| 363 | dev_priv->hotplug.event_bits |= BIT(i); |
| 364 | queue_hp = true; |
| 365 | } |
| 366 | |
| 367 | if (intel_hpd_irq_storm_detect(dev_priv, i)) { |
| 368 | dev_priv->hotplug.event_bits &= ~BIT(i); |
| 369 | storm_detected = true; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (storm_detected) |
| 374 | dev_priv->display.hpd_irq_setup(dev); |
| 375 | spin_unlock(&dev_priv->irq_lock); |
| 376 | |
| 377 | /* |
| 378 | * Our hotplug handler can grab modeset locks (by calling down into the |
| 379 | * fb helpers). Hence it must not be run on our own dev-priv->wq work |
| 380 | * queue for otherwise the flush_work in the pageflip code will |
| 381 | * deadlock. |
| 382 | */ |
| 383 | if (queue_dig) |
| 384 | queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work); |
| 385 | if (queue_hp) |
| 386 | schedule_work(&dev_priv->hotplug.hotplug_work); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * intel_hpd_init - initializes and enables hpd support |
| 391 | * @dev_priv: i915 device instance |
| 392 | * |
| 393 | * This function enables the hotplug support. It requires that interrupts have |
| 394 | * already been enabled with intel_irq_init_hw(). From this point on hotplug and |
| 395 | * poll request can run concurrently to other code, so locking rules must be |
| 396 | * obeyed. |
| 397 | * |
| 398 | * This is a separate step from interrupt enabling to simplify the locking rules |
| 399 | * in the driver load and resume code. |
| 400 | */ |
| 401 | void intel_hpd_init(struct drm_i915_private *dev_priv) |
| 402 | { |
| 403 | struct drm_device *dev = dev_priv->dev; |
| 404 | struct drm_mode_config *mode_config = &dev->mode_config; |
| 405 | struct drm_connector *connector; |
| 406 | int i; |
| 407 | |
| 408 | for_each_hpd_pin(i) { |
| 409 | dev_priv->hotplug.stats[i].count = 0; |
| 410 | dev_priv->hotplug.stats[i].state = HPD_ENABLED; |
| 411 | } |
| 412 | list_for_each_entry(connector, &mode_config->connector_list, head) { |
| 413 | struct intel_connector *intel_connector = to_intel_connector(connector); |
| 414 | connector->polled = intel_connector->polled; |
| 415 | if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE) |
| 416 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
| 417 | if (intel_connector->mst_port) |
| 418 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Interrupt setup is already guaranteed to be single-threaded, this is |
| 423 | * just to make the assert_spin_locked checks happy. |
| 424 | */ |
| 425 | spin_lock_irq(&dev_priv->irq_lock); |
| 426 | if (dev_priv->display.hpd_irq_setup) |
| 427 | dev_priv->display.hpd_irq_setup(dev); |
| 428 | spin_unlock_irq(&dev_priv->irq_lock); |
| 429 | } |
| 430 | |
| 431 | void intel_hpd_init_work(struct drm_i915_private *dev_priv) |
| 432 | { |
| 433 | INIT_WORK(&dev_priv->hotplug.hotplug_work, i915_hotplug_work_func); |
| 434 | INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func); |
| 435 | INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work, |
| 436 | intel_hpd_irq_storm_reenable_work); |
| 437 | } |
| 438 | |
| 439 | void intel_hpd_cancel_work(struct drm_i915_private *dev_priv) |
| 440 | { |
| 441 | spin_lock_irq(&dev_priv->irq_lock); |
| 442 | |
| 443 | dev_priv->hotplug.long_port_mask = 0; |
| 444 | dev_priv->hotplug.short_port_mask = 0; |
| 445 | dev_priv->hotplug.event_bits = 0; |
| 446 | |
| 447 | spin_unlock_irq(&dev_priv->irq_lock); |
| 448 | |
| 449 | cancel_work_sync(&dev_priv->hotplug.dig_port_work); |
| 450 | cancel_work_sync(&dev_priv->hotplug.hotplug_work); |
| 451 | cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work); |
| 452 | } |