intel_hotplug.c 16 KB

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