omap_irq.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_irq.c
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. * Author: Rob Clark <rob.clark@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "omap_drv.h"
  20. struct omap_irq_wait {
  21. struct list_head node;
  22. wait_queue_head_t wq;
  23. uint32_t irqmask;
  24. int count;
  25. };
  26. /* call with wait_lock and dispc runtime held */
  27. static void omap_irq_update(struct drm_device *dev)
  28. {
  29. struct omap_drm_private *priv = dev->dev_private;
  30. struct omap_irq_wait *wait;
  31. uint32_t irqmask = priv->irq_mask;
  32. assert_spin_locked(&priv->wait_lock);
  33. list_for_each_entry(wait, &priv->wait_list, node)
  34. irqmask |= wait->irqmask;
  35. DBG("irqmask=%08x", irqmask);
  36. dispc_write_irqenable(irqmask);
  37. dispc_read_irqenable(); /* flush posted write */
  38. }
  39. static void omap_irq_wait_handler(struct omap_irq_wait *wait)
  40. {
  41. wait->count--;
  42. wake_up(&wait->wq);
  43. }
  44. struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
  45. uint32_t irqmask, int count)
  46. {
  47. struct omap_drm_private *priv = dev->dev_private;
  48. struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  49. unsigned long flags;
  50. init_waitqueue_head(&wait->wq);
  51. wait->irqmask = irqmask;
  52. wait->count = count;
  53. spin_lock_irqsave(&priv->wait_lock, flags);
  54. list_add(&wait->node, &priv->wait_list);
  55. omap_irq_update(dev);
  56. spin_unlock_irqrestore(&priv->wait_lock, flags);
  57. return wait;
  58. }
  59. int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
  60. unsigned long timeout)
  61. {
  62. struct omap_drm_private *priv = dev->dev_private;
  63. unsigned long flags;
  64. int ret;
  65. ret = wait_event_timeout(wait->wq, (wait->count <= 0), timeout);
  66. spin_lock_irqsave(&priv->wait_lock, flags);
  67. list_del(&wait->node);
  68. omap_irq_update(dev);
  69. spin_unlock_irqrestore(&priv->wait_lock, flags);
  70. kfree(wait);
  71. return ret == 0 ? -1 : 0;
  72. }
  73. /**
  74. * enable_vblank - enable vblank interrupt events
  75. * @dev: DRM device
  76. * @pipe: which irq to enable
  77. *
  78. * Enable vblank interrupts for @crtc. If the device doesn't have
  79. * a hardware vblank counter, this routine should be a no-op, since
  80. * interrupts will have to stay on to keep the count accurate.
  81. *
  82. * RETURNS
  83. * Zero on success, appropriate errno if the given @crtc's vblank
  84. * interrupt cannot be enabled.
  85. */
  86. int omap_irq_enable_vblank(struct drm_device *dev, unsigned int pipe)
  87. {
  88. struct omap_drm_private *priv = dev->dev_private;
  89. struct drm_crtc *crtc = priv->crtcs[pipe];
  90. unsigned long flags;
  91. DBG("dev=%p, crtc=%u", dev, pipe);
  92. spin_lock_irqsave(&priv->wait_lock, flags);
  93. priv->irq_mask |= dispc_mgr_get_vsync_irq(omap_crtc_channel(crtc));
  94. omap_irq_update(dev);
  95. spin_unlock_irqrestore(&priv->wait_lock, flags);
  96. return 0;
  97. }
  98. /**
  99. * disable_vblank - disable vblank interrupt events
  100. * @dev: DRM device
  101. * @pipe: which irq to enable
  102. *
  103. * Disable vblank interrupts for @crtc. If the device doesn't have
  104. * a hardware vblank counter, this routine should be a no-op, since
  105. * interrupts will have to stay on to keep the count accurate.
  106. */
  107. void omap_irq_disable_vblank(struct drm_device *dev, unsigned int pipe)
  108. {
  109. struct omap_drm_private *priv = dev->dev_private;
  110. struct drm_crtc *crtc = priv->crtcs[pipe];
  111. unsigned long flags;
  112. DBG("dev=%p, crtc=%u", dev, pipe);
  113. spin_lock_irqsave(&priv->wait_lock, flags);
  114. priv->irq_mask &= ~dispc_mgr_get_vsync_irq(omap_crtc_channel(crtc));
  115. omap_irq_update(dev);
  116. spin_unlock_irqrestore(&priv->wait_lock, flags);
  117. }
  118. static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
  119. u32 irqstatus)
  120. {
  121. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  122. DEFAULT_RATELIMIT_BURST);
  123. static const struct {
  124. const char *name;
  125. u32 mask;
  126. } sources[] = {
  127. { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
  128. { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
  129. { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
  130. { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
  131. };
  132. const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
  133. | DISPC_IRQ_VID1_FIFO_UNDERFLOW
  134. | DISPC_IRQ_VID2_FIFO_UNDERFLOW
  135. | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
  136. unsigned int i;
  137. spin_lock(&priv->wait_lock);
  138. irqstatus &= priv->irq_mask & mask;
  139. spin_unlock(&priv->wait_lock);
  140. if (!irqstatus)
  141. return;
  142. if (!__ratelimit(&_rs))
  143. return;
  144. DRM_ERROR("FIFO underflow on ");
  145. for (i = 0; i < ARRAY_SIZE(sources); ++i) {
  146. if (sources[i].mask & irqstatus)
  147. pr_cont("%s ", sources[i].name);
  148. }
  149. pr_cont("(0x%08x)\n", irqstatus);
  150. }
  151. static void omap_irq_ocp_error_handler(u32 irqstatus)
  152. {
  153. if (!(irqstatus & DISPC_IRQ_OCP_ERR))
  154. return;
  155. DRM_ERROR("OCP error\n");
  156. }
  157. static irqreturn_t omap_irq_handler(int irq, void *arg)
  158. {
  159. struct drm_device *dev = (struct drm_device *) arg;
  160. struct omap_drm_private *priv = dev->dev_private;
  161. struct omap_irq_wait *wait, *n;
  162. unsigned long flags;
  163. unsigned int id;
  164. u32 irqstatus;
  165. irqstatus = dispc_read_irqstatus();
  166. dispc_clear_irqstatus(irqstatus);
  167. dispc_read_irqstatus(); /* flush posted write */
  168. VERB("irqs: %08x", irqstatus);
  169. for (id = 0; id < priv->num_crtcs; id++) {
  170. struct drm_crtc *crtc = priv->crtcs[id];
  171. enum omap_channel channel = omap_crtc_channel(crtc);
  172. if (irqstatus & dispc_mgr_get_vsync_irq(channel)) {
  173. drm_handle_vblank(dev, id);
  174. omap_crtc_vblank_irq(crtc);
  175. }
  176. if (irqstatus & dispc_mgr_get_sync_lost_irq(channel))
  177. omap_crtc_error_irq(crtc, irqstatus);
  178. }
  179. omap_irq_ocp_error_handler(irqstatus);
  180. omap_irq_fifo_underflow(priv, irqstatus);
  181. spin_lock_irqsave(&priv->wait_lock, flags);
  182. list_for_each_entry_safe(wait, n, &priv->wait_list, node) {
  183. if (wait->irqmask & irqstatus)
  184. omap_irq_wait_handler(wait);
  185. }
  186. spin_unlock_irqrestore(&priv->wait_lock, flags);
  187. return IRQ_HANDLED;
  188. }
  189. static const u32 omap_underflow_irqs[] = {
  190. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  191. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  192. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  193. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  194. };
  195. /*
  196. * We need a special version, instead of just using drm_irq_install(),
  197. * because we need to register the irq via omapdss. Once omapdss and
  198. * omapdrm are merged together we can assign the dispc hwmod data to
  199. * ourselves and drop these and just use drm_irq_{install,uninstall}()
  200. */
  201. int omap_drm_irq_install(struct drm_device *dev)
  202. {
  203. struct omap_drm_private *priv = dev->dev_private;
  204. unsigned int num_mgrs = dss_feat_get_num_mgrs();
  205. unsigned int max_planes;
  206. unsigned int i;
  207. int ret;
  208. spin_lock_init(&priv->wait_lock);
  209. INIT_LIST_HEAD(&priv->wait_list);
  210. priv->irq_mask = DISPC_IRQ_OCP_ERR;
  211. max_planes = min(ARRAY_SIZE(priv->planes),
  212. ARRAY_SIZE(omap_underflow_irqs));
  213. for (i = 0; i < max_planes; ++i) {
  214. if (priv->planes[i])
  215. priv->irq_mask |= omap_underflow_irqs[i];
  216. }
  217. for (i = 0; i < num_mgrs; ++i)
  218. priv->irq_mask |= dispc_mgr_get_sync_lost_irq(i);
  219. dispc_runtime_get();
  220. dispc_clear_irqstatus(0xffffffff);
  221. dispc_runtime_put();
  222. ret = dispc_request_irq(omap_irq_handler, dev);
  223. if (ret < 0)
  224. return ret;
  225. dev->irq_enabled = true;
  226. return 0;
  227. }
  228. void omap_drm_irq_uninstall(struct drm_device *dev)
  229. {
  230. unsigned long irqflags;
  231. int i;
  232. if (!dev->irq_enabled)
  233. return;
  234. dev->irq_enabled = false;
  235. /* Wake up any waiters so they don't hang. */
  236. if (dev->num_crtcs) {
  237. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  238. for (i = 0; i < dev->num_crtcs; i++) {
  239. wake_up(&dev->vblank[i].queue);
  240. dev->vblank[i].enabled = false;
  241. dev->vblank[i].last =
  242. dev->driver->get_vblank_counter(dev, i);
  243. }
  244. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  245. }
  246. dispc_free_irq(dev);
  247. }