omap_irq.c 8.3 KB

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