omap_irq.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  3. * Author: Rob Clark <rob.clark@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "omap_drv.h"
  18. struct omap_irq_wait {
  19. struct list_head node;
  20. wait_queue_head_t wq;
  21. uint32_t irqmask;
  22. int count;
  23. };
  24. /* call with wait_lock and dispc runtime held */
  25. static void omap_irq_update(struct drm_device *dev)
  26. {
  27. struct omap_drm_private *priv = dev->dev_private;
  28. struct omap_irq_wait *wait;
  29. uint32_t irqmask = priv->irq_mask;
  30. assert_spin_locked(&priv->wait_lock);
  31. list_for_each_entry(wait, &priv->wait_list, node)
  32. irqmask |= wait->irqmask;
  33. DBG("irqmask=%08x", irqmask);
  34. priv->dispc_ops->write_irqenable(irqmask);
  35. }
  36. static void omap_irq_wait_handler(struct omap_irq_wait *wait)
  37. {
  38. wait->count--;
  39. wake_up(&wait->wq);
  40. }
  41. struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
  42. uint32_t irqmask, int count)
  43. {
  44. struct omap_drm_private *priv = dev->dev_private;
  45. struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  46. unsigned long flags;
  47. init_waitqueue_head(&wait->wq);
  48. wait->irqmask = irqmask;
  49. wait->count = count;
  50. spin_lock_irqsave(&priv->wait_lock, flags);
  51. list_add(&wait->node, &priv->wait_list);
  52. omap_irq_update(dev);
  53. spin_unlock_irqrestore(&priv->wait_lock, flags);
  54. return wait;
  55. }
  56. int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
  57. unsigned long timeout)
  58. {
  59. struct omap_drm_private *priv = dev->dev_private;
  60. unsigned long flags;
  61. int ret;
  62. ret = wait_event_timeout(wait->wq, (wait->count <= 0), timeout);
  63. spin_lock_irqsave(&priv->wait_lock, flags);
  64. list_del(&wait->node);
  65. omap_irq_update(dev);
  66. spin_unlock_irqrestore(&priv->wait_lock, flags);
  67. kfree(wait);
  68. return ret == 0 ? -1 : 0;
  69. }
  70. /**
  71. * enable_vblank - enable vblank interrupt events
  72. * @dev: DRM device
  73. * @pipe: which irq to enable
  74. *
  75. * Enable vblank interrupts for @crtc. If the device doesn't have
  76. * a hardware vblank counter, this routine should be a no-op, since
  77. * interrupts will have to stay on to keep the count accurate.
  78. *
  79. * RETURNS
  80. * Zero on success, appropriate errno if the given @crtc's vblank
  81. * interrupt cannot be enabled.
  82. */
  83. int omap_irq_enable_vblank(struct drm_crtc *crtc)
  84. {
  85. struct drm_device *dev = crtc->dev;
  86. struct omap_drm_private *priv = dev->dev_private;
  87. unsigned long flags;
  88. enum omap_channel channel = omap_crtc_channel(crtc);
  89. DBG("dev=%p, crtc=%u", dev, channel);
  90. spin_lock_irqsave(&priv->wait_lock, flags);
  91. priv->irq_mask |= priv->dispc_ops->mgr_get_vsync_irq(channel);
  92. omap_irq_update(dev);
  93. spin_unlock_irqrestore(&priv->wait_lock, flags);
  94. return 0;
  95. }
  96. /**
  97. * disable_vblank - disable vblank interrupt events
  98. * @dev: DRM device
  99. * @pipe: which irq to enable
  100. *
  101. * Disable vblank interrupts for @crtc. If the device doesn't have
  102. * a hardware vblank counter, this routine should be a no-op, since
  103. * interrupts will have to stay on to keep the count accurate.
  104. */
  105. void omap_irq_disable_vblank(struct drm_crtc *crtc)
  106. {
  107. struct drm_device *dev = crtc->dev;
  108. struct omap_drm_private *priv = dev->dev_private;
  109. unsigned long flags;
  110. enum omap_channel channel = omap_crtc_channel(crtc);
  111. DBG("dev=%p, crtc=%u", dev, channel);
  112. spin_lock_irqsave(&priv->wait_lock, flags);
  113. priv->irq_mask &= ~priv->dispc_ops->mgr_get_vsync_irq(channel);
  114. omap_irq_update(dev);
  115. spin_unlock_irqrestore(&priv->wait_lock, flags);
  116. }
  117. static void omap_irq_fifo_underflow(struct omap_drm_private *priv,
  118. u32 irqstatus)
  119. {
  120. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  121. DEFAULT_RATELIMIT_BURST);
  122. static const struct {
  123. const char *name;
  124. u32 mask;
  125. } sources[] = {
  126. { "gfx", DISPC_IRQ_GFX_FIFO_UNDERFLOW },
  127. { "vid1", DISPC_IRQ_VID1_FIFO_UNDERFLOW },
  128. { "vid2", DISPC_IRQ_VID2_FIFO_UNDERFLOW },
  129. { "vid3", DISPC_IRQ_VID3_FIFO_UNDERFLOW },
  130. };
  131. const u32 mask = DISPC_IRQ_GFX_FIFO_UNDERFLOW
  132. | DISPC_IRQ_VID1_FIFO_UNDERFLOW
  133. | DISPC_IRQ_VID2_FIFO_UNDERFLOW
  134. | DISPC_IRQ_VID3_FIFO_UNDERFLOW;
  135. unsigned int i;
  136. spin_lock(&priv->wait_lock);
  137. irqstatus &= priv->irq_mask & mask;
  138. spin_unlock(&priv->wait_lock);
  139. if (!irqstatus)
  140. return;
  141. if (!__ratelimit(&_rs))
  142. return;
  143. DRM_ERROR("FIFO underflow on ");
  144. for (i = 0; i < ARRAY_SIZE(sources); ++i) {
  145. if (sources[i].mask & irqstatus)
  146. pr_cont("%s ", sources[i].name);
  147. }
  148. pr_cont("(0x%08x)\n", irqstatus);
  149. }
  150. static void omap_irq_ocp_error_handler(struct drm_device *dev,
  151. u32 irqstatus)
  152. {
  153. if (!(irqstatus & DISPC_IRQ_OCP_ERR))
  154. return;
  155. dev_err_ratelimited(dev->dev, "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 = priv->dispc_ops->read_irqstatus();
  166. priv->dispc_ops->clear_irqstatus(irqstatus);
  167. priv->dispc_ops->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 & priv->dispc_ops->mgr_get_vsync_irq(channel)) {
  173. drm_handle_vblank(dev, id);
  174. omap_crtc_vblank_irq(crtc);
  175. }
  176. if (irqstatus & priv->dispc_ops->mgr_get_sync_lost_irq(channel))
  177. omap_crtc_error_irq(crtc, irqstatus);
  178. }
  179. omap_irq_ocp_error_handler(dev, 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 = priv->dispc_ops->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 |= priv->dispc_ops->mgr_get_sync_lost_irq(i);
  219. priv->dispc_ops->runtime_get();
  220. priv->dispc_ops->clear_irqstatus(0xffffffff);
  221. priv->dispc_ops->runtime_put();
  222. ret = priv->dispc_ops->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. struct omap_drm_private *priv = dev->dev_private;
  231. if (!dev->irq_enabled)
  232. return;
  233. dev->irq_enabled = false;
  234. priv->dispc_ops->free_irq(dev);
  235. }