omap_irq.c 7.7 KB

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