omap_irq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. priv->dispc_ops->write_irqenable(irqmask);
  37. }
  38. static void omap_irq_wait_handler(struct omap_irq_wait *wait)
  39. {
  40. wait->count--;
  41. wake_up(&wait->wq);
  42. }
  43. struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
  44. uint32_t irqmask, int count)
  45. {
  46. struct omap_drm_private *priv = dev->dev_private;
  47. struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  48. unsigned long flags;
  49. init_waitqueue_head(&wait->wq);
  50. wait->irqmask = irqmask;
  51. wait->count = count;
  52. spin_lock_irqsave(&priv->wait_lock, flags);
  53. list_add(&wait->node, &priv->wait_list);
  54. omap_irq_update(dev);
  55. spin_unlock_irqrestore(&priv->wait_lock, flags);
  56. return wait;
  57. }
  58. int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
  59. unsigned long timeout)
  60. {
  61. struct omap_drm_private *priv = dev->dev_private;
  62. unsigned long flags;
  63. int ret;
  64. ret = wait_event_timeout(wait->wq, (wait->count <= 0), timeout);
  65. spin_lock_irqsave(&priv->wait_lock, flags);
  66. list_del(&wait->node);
  67. omap_irq_update(dev);
  68. spin_unlock_irqrestore(&priv->wait_lock, flags);
  69. kfree(wait);
  70. return ret == 0 ? -1 : 0;
  71. }
  72. /**
  73. * enable_vblank - enable vblank interrupt events
  74. * @dev: DRM device
  75. * @pipe: which irq to enable
  76. *
  77. * Enable vblank interrupts for @crtc. If the device doesn't have
  78. * a hardware vblank counter, this routine should be a no-op, since
  79. * interrupts will have to stay on to keep the count accurate.
  80. *
  81. * RETURNS
  82. * Zero on success, appropriate errno if the given @crtc's vblank
  83. * interrupt cannot be enabled.
  84. */
  85. int omap_irq_enable_vblank(struct drm_crtc *crtc)
  86. {
  87. struct drm_device *dev = crtc->dev;
  88. struct omap_drm_private *priv = dev->dev_private;
  89. unsigned long flags;
  90. enum omap_channel channel = omap_crtc_channel(crtc);
  91. DBG("dev=%p, crtc=%u", dev, channel);
  92. spin_lock_irqsave(&priv->wait_lock, flags);
  93. priv->irq_mask |= priv->dispc_ops->mgr_get_vsync_irq(channel);
  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_crtc *crtc)
  108. {
  109. struct drm_device *dev = crtc->dev;
  110. struct omap_drm_private *priv = dev->dev_private;
  111. unsigned long flags;
  112. enum omap_channel channel = omap_crtc_channel(crtc);
  113. DBG("dev=%p, crtc=%u", dev, channel);
  114. spin_lock_irqsave(&priv->wait_lock, flags);
  115. priv->irq_mask &= ~priv->dispc_ops->mgr_get_vsync_irq(channel);
  116. omap_irq_update(dev);
  117. spin_unlock_irqrestore(&priv->wait_lock, flags);
  118. }
  119. static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
  120. u32 irqstatus)
  121. {
  122. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  123. DEFAULT_RATELIMIT_BURST);
  124. static const struct {
  125. const char *name;
  126. u32 mask;
  127. } sources[] = {
  128. { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
  129. { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
  130. { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
  131. { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
  132. };
  133. const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
  134. | DISPC_IRQ_VID1_FIFO_UNDERFLOW
  135. | DISPC_IRQ_VID2_FIFO_UNDERFLOW
  136. | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
  137. unsigned int i;
  138. spin_lock(&priv->wait_lock);
  139. irqstatus &= priv->irq_mask & mask;
  140. spin_unlock(&priv->wait_lock);
  141. if (!irqstatus)
  142. return;
  143. if (!__ratelimit(&_rs))
  144. return;
  145. DRM_ERROR("FIFO underflow on ");
  146. for (i = 0; i < ARRAY_SIZE(sources); ++i) {
  147. if (sources[i].mask & irqstatus)
  148. pr_cont("%s ", sources[i].name);
  149. }
  150. pr_cont("(0x%08x)\n", irqstatus);
  151. }
  152. static void omap_irq_ocp_error_handler(struct drm_device *dev,
  153. u32 irqstatus)
  154. {
  155. if (!(irqstatus & DISPC_IRQ_OCP_ERR))
  156. return;
  157. dev_err_ratelimited(dev->dev, "OCP error\n");
  158. }
  159. static irqreturn_t omap_irq_handler(int irq, void *arg)
  160. {
  161. struct drm_device *dev = (struct drm_device *) arg;
  162. struct omap_drm_private *priv = dev->dev_private;
  163. struct omap_irq_wait *wait, *n;
  164. unsigned long flags;
  165. unsigned int id;
  166. u32 irqstatus;
  167. irqstatus = priv->dispc_ops->read_irqstatus();
  168. priv->dispc_ops->clear_irqstatus(irqstatus);
  169. priv->dispc_ops->read_irqstatus(); /* flush posted write */
  170. VERB("irqs: %08x", irqstatus);
  171. for (id = 0; id < priv->num_crtcs; id++) {
  172. struct drm_crtc *crtc = priv->crtcs[id];
  173. enum omap_channel channel = omap_crtc_channel(crtc);
  174. if (irqstatus & priv->dispc_ops->mgr_get_vsync_irq(channel)) {
  175. drm_handle_vblank(dev, id);
  176. omap_crtc_vblank_irq(crtc);
  177. }
  178. if (irqstatus & priv->dispc_ops->mgr_get_sync_lost_irq(channel))
  179. omap_crtc_error_irq(crtc, irqstatus);
  180. }
  181. omap_irq_ocp_error_handler(dev, irqstatus);
  182. omap_irq_fifo_underflow(priv, irqstatus);
  183. spin_lock_irqsave(&priv->wait_lock, flags);
  184. list_for_each_entry_safe(wait, n, &priv->wait_list, node) {
  185. if (wait->irqmask & irqstatus)
  186. omap_irq_wait_handler(wait);
  187. }
  188. spin_unlock_irqrestore(&priv->wait_lock, flags);
  189. return IRQ_HANDLED;
  190. }
  191. static const u32 omap_underflow_irqs[] = {
  192. [OMAP_DSS_GFX] = DISPC_IRQ_GFX_FIFO_UNDERFLOW,
  193. [OMAP_DSS_VIDEO1] = DISPC_IRQ_VID1_FIFO_UNDERFLOW,
  194. [OMAP_DSS_VIDEO2] = DISPC_IRQ_VID2_FIFO_UNDERFLOW,
  195. [OMAP_DSS_VIDEO3] = DISPC_IRQ_VID3_FIFO_UNDERFLOW,
  196. };
  197. /*
  198. * We need a special version, instead of just using drm_irq_install(),
  199. * because we need to register the irq via omapdss. Once omapdss and
  200. * omapdrm are merged together we can assign the dispc hwmod data to
  201. * ourselves and drop these and just use drm_irq_{install,uninstall}()
  202. */
  203. int omap_drm_irq_install(struct drm_device *dev)
  204. {
  205. struct omap_drm_private *priv = dev->dev_private;
  206. unsigned int num_mgrs = priv->dispc_ops->get_num_mgrs();
  207. unsigned int max_planes;
  208. unsigned int i;
  209. int ret;
  210. spin_lock_init(&priv->wait_lock);
  211. INIT_LIST_HEAD(&priv->wait_list);
  212. priv->irq_mask = DISPC_IRQ_OCP_ERR;
  213. max_planes = min(ARRAY_SIZE(priv->planes),
  214. ARRAY_SIZE(omap_underflow_irqs));
  215. for (i = 0; i < max_planes; ++i) {
  216. if (priv->planes[i])
  217. priv->irq_mask |= omap_underflow_irqs[i];
  218. }
  219. for (i = 0; i < num_mgrs; ++i)
  220. priv->irq_mask |= priv->dispc_ops->mgr_get_sync_lost_irq(i);
  221. priv->dispc_ops->runtime_get();
  222. priv->dispc_ops->clear_irqstatus(0xffffffff);
  223. priv->dispc_ops->runtime_put();
  224. ret = priv->dispc_ops->request_irq(omap_irq_handler, dev);
  225. if (ret < 0)
  226. return ret;
  227. dev->irq_enabled = true;
  228. return 0;
  229. }
  230. void omap_drm_irq_uninstall(struct drm_device *dev)
  231. {
  232. struct omap_drm_private *priv = dev->dev_private;
  233. if (!dev->irq_enabled)
  234. return;
  235. dev->irq_enabled = false;
  236. priv->dispc_ops->free_irq(dev);
  237. }