omap_irq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. static void omap_irq_error_handler(struct omap_drm_irq *irq,
  22. uint32_t irqstatus)
  23. {
  24. DRM_ERROR("errors: %08x\n", irqstatus);
  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_drm_irq *irq;
  31. uint32_t irqmask = priv->vblank_mask;
  32. BUG_ON(!spin_is_locked(&list_lock));
  33. list_for_each_entry(irq, &priv->irq_list, node)
  34. irqmask |= irq->irqmask;
  35. DBG("irqmask=%08x", irqmask);
  36. dispc_write_irqenable(irqmask);
  37. dispc_read_irqenable(); /* flush posted write */
  38. }
  39. void omap_irq_register(struct drm_device *dev, struct omap_drm_irq *irq)
  40. {
  41. struct omap_drm_private *priv = dev->dev_private;
  42. unsigned long flags;
  43. dispc_runtime_get();
  44. spin_lock_irqsave(&list_lock, flags);
  45. if (!WARN_ON(irq->registered)) {
  46. irq->registered = true;
  47. list_add(&irq->node, &priv->irq_list);
  48. omap_irq_update(dev);
  49. }
  50. spin_unlock_irqrestore(&list_lock, flags);
  51. dispc_runtime_put();
  52. }
  53. void omap_irq_unregister(struct drm_device *dev, struct omap_drm_irq *irq)
  54. {
  55. unsigned long flags;
  56. dispc_runtime_get();
  57. spin_lock_irqsave(&list_lock, flags);
  58. if (!WARN_ON(!irq->registered)) {
  59. irq->registered = false;
  60. list_del(&irq->node);
  61. omap_irq_update(dev);
  62. }
  63. spin_unlock_irqrestore(&list_lock, flags);
  64. dispc_runtime_put();
  65. }
  66. struct omap_irq_wait {
  67. struct omap_drm_irq irq;
  68. int count;
  69. };
  70. static DECLARE_WAIT_QUEUE_HEAD(wait_event);
  71. static void wait_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  72. {
  73. struct omap_irq_wait *wait =
  74. container_of(irq, struct omap_irq_wait, irq);
  75. wait->count--;
  76. wake_up_all(&wait_event);
  77. }
  78. struct omap_irq_wait * omap_irq_wait_init(struct drm_device *dev,
  79. uint32_t irqmask, int count)
  80. {
  81. struct omap_irq_wait *wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  82. wait->irq.irq = wait_irq;
  83. wait->irq.irqmask = irqmask;
  84. wait->count = count;
  85. omap_irq_register(dev, &wait->irq);
  86. return wait;
  87. }
  88. int omap_irq_wait(struct drm_device *dev, struct omap_irq_wait *wait,
  89. unsigned long timeout)
  90. {
  91. int ret = wait_event_timeout(wait_event, (wait->count <= 0), timeout);
  92. omap_irq_unregister(dev, &wait->irq);
  93. kfree(wait);
  94. if (ret == 0)
  95. return -1;
  96. return 0;
  97. }
  98. /**
  99. * enable_vblank - enable vblank interrupt events
  100. * @dev: DRM device
  101. * @crtc: which irq to enable
  102. *
  103. * Enable 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. * RETURNS
  108. * Zero on success, appropriate errno if the given @crtc's vblank
  109. * interrupt cannot be enabled.
  110. */
  111. int omap_irq_enable_vblank(struct drm_device *dev, int crtc_id)
  112. {
  113. struct omap_drm_private *priv = dev->dev_private;
  114. struct drm_crtc *crtc = priv->crtcs[crtc_id];
  115. unsigned long flags;
  116. DBG("dev=%p, crtc=%d", dev, crtc_id);
  117. dispc_runtime_get();
  118. spin_lock_irqsave(&list_lock, flags);
  119. priv->vblank_mask |= pipe2vbl(crtc);
  120. omap_irq_update(dev);
  121. spin_unlock_irqrestore(&list_lock, flags);
  122. dispc_runtime_put();
  123. return 0;
  124. }
  125. /**
  126. * disable_vblank - disable vblank interrupt events
  127. * @dev: DRM device
  128. * @crtc: which irq to enable
  129. *
  130. * Disable vblank interrupts for @crtc. If the device doesn't have
  131. * a hardware vblank counter, this routine should be a no-op, since
  132. * interrupts will have to stay on to keep the count accurate.
  133. */
  134. void omap_irq_disable_vblank(struct drm_device *dev, int crtc_id)
  135. {
  136. struct omap_drm_private *priv = dev->dev_private;
  137. struct drm_crtc *crtc = priv->crtcs[crtc_id];
  138. unsigned long flags;
  139. DBG("dev=%p, crtc=%d", dev, crtc_id);
  140. dispc_runtime_get();
  141. spin_lock_irqsave(&list_lock, flags);
  142. priv->vblank_mask &= ~pipe2vbl(crtc);
  143. omap_irq_update(dev);
  144. spin_unlock_irqrestore(&list_lock, flags);
  145. dispc_runtime_put();
  146. }
  147. irqreturn_t omap_irq_handler(DRM_IRQ_ARGS)
  148. {
  149. struct drm_device *dev = (struct drm_device *) arg;
  150. struct omap_drm_private *priv = dev->dev_private;
  151. struct omap_drm_irq *handler, *n;
  152. unsigned long flags;
  153. unsigned int id;
  154. u32 irqstatus;
  155. irqstatus = dispc_read_irqstatus();
  156. dispc_clear_irqstatus(irqstatus);
  157. dispc_read_irqstatus(); /* flush posted write */
  158. VERB("irqs: %08x", irqstatus);
  159. for (id = 0; id < priv->num_crtcs; id++) {
  160. struct drm_crtc *crtc = priv->crtcs[id];
  161. if (irqstatus & pipe2vbl(crtc))
  162. drm_handle_vblank(dev, id);
  163. }
  164. spin_lock_irqsave(&list_lock, flags);
  165. list_for_each_entry_safe(handler, n, &priv->irq_list, node) {
  166. if (handler->irqmask & irqstatus) {
  167. spin_unlock_irqrestore(&list_lock, flags);
  168. handler->irq(handler, handler->irqmask & irqstatus);
  169. spin_lock_irqsave(&list_lock, flags);
  170. }
  171. }
  172. spin_unlock_irqrestore(&list_lock, flags);
  173. return IRQ_HANDLED;
  174. }
  175. void omap_irq_preinstall(struct drm_device *dev)
  176. {
  177. DBG("dev=%p", dev);
  178. dispc_runtime_get();
  179. dispc_clear_irqstatus(0xffffffff);
  180. dispc_runtime_put();
  181. }
  182. int omap_irq_postinstall(struct drm_device *dev)
  183. {
  184. struct omap_drm_private *priv = dev->dev_private;
  185. struct omap_drm_irq *error_handler = &priv->error_handler;
  186. DBG("dev=%p", dev);
  187. INIT_LIST_HEAD(&priv->irq_list);
  188. error_handler->irq = omap_irq_error_handler;
  189. error_handler->irqmask = DISPC_IRQ_OCP_ERR;
  190. /* for now ignore DISPC_IRQ_SYNC_LOST_DIGIT.. really I think
  191. * we just need to ignore it while enabling tv-out
  192. */
  193. error_handler->irqmask &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  194. omap_irq_register(dev, error_handler);
  195. return 0;
  196. }
  197. void omap_irq_uninstall(struct drm_device *dev)
  198. {
  199. DBG("dev=%p", dev);
  200. // TODO prolly need to call drm_irq_uninstall() somewhere too
  201. }
  202. /*
  203. * We need a special version, instead of just using drm_irq_install(),
  204. * because we need to register the irq via omapdss. Once omapdss and
  205. * omapdrm are merged together we can assign the dispc hwmod data to
  206. * ourselves and drop these and just use drm_irq_{install,uninstall}()
  207. */
  208. int omap_drm_irq_install(struct drm_device *dev)
  209. {
  210. int ret;
  211. mutex_lock(&dev->struct_mutex);
  212. if (dev->irq_enabled) {
  213. mutex_unlock(&dev->struct_mutex);
  214. return -EBUSY;
  215. }
  216. dev->irq_enabled = 1;
  217. mutex_unlock(&dev->struct_mutex);
  218. /* Before installing handler */
  219. if (dev->driver->irq_preinstall)
  220. dev->driver->irq_preinstall(dev);
  221. ret = dispc_request_irq(dev->driver->irq_handler, dev);
  222. if (ret < 0) {
  223. mutex_lock(&dev->struct_mutex);
  224. dev->irq_enabled = 0;
  225. mutex_unlock(&dev->struct_mutex);
  226. return ret;
  227. }
  228. /* After installing handler */
  229. if (dev->driver->irq_postinstall)
  230. ret = dev->driver->irq_postinstall(dev);
  231. if (ret < 0) {
  232. mutex_lock(&dev->struct_mutex);
  233. dev->irq_enabled = 0;
  234. mutex_unlock(&dev->struct_mutex);
  235. dispc_free_irq(dev);
  236. }
  237. return ret;
  238. }
  239. int omap_drm_irq_uninstall(struct drm_device *dev)
  240. {
  241. unsigned long irqflags;
  242. int irq_enabled, i;
  243. mutex_lock(&dev->struct_mutex);
  244. irq_enabled = dev->irq_enabled;
  245. dev->irq_enabled = 0;
  246. mutex_unlock(&dev->struct_mutex);
  247. /*
  248. * Wake up any waiters so they don't hang.
  249. */
  250. if (dev->num_crtcs) {
  251. spin_lock_irqsave(&dev->vbl_lock, irqflags);
  252. for (i = 0; i < dev->num_crtcs; i++) {
  253. DRM_WAKEUP(&dev->vbl_queue[i]);
  254. dev->vblank_enabled[i] = 0;
  255. dev->last_vblank[i] =
  256. dev->driver->get_vblank_counter(dev, i);
  257. }
  258. spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
  259. }
  260. if (!irq_enabled)
  261. return -EINVAL;
  262. if (dev->driver->irq_uninstall)
  263. dev->driver->irq_uninstall(dev);
  264. dispc_free_irq(dev);
  265. return 0;
  266. }