vmwgfx_irq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "drmP.h"
  28. #include "vmwgfx_drv.h"
  29. #define VMW_FENCE_WRAP (1 << 24)
  30. irqreturn_t vmw_irq_handler(DRM_IRQ_ARGS)
  31. {
  32. struct drm_device *dev = (struct drm_device *)arg;
  33. struct vmw_private *dev_priv = vmw_priv(dev);
  34. uint32_t status, masked_status;
  35. spin_lock(&dev_priv->irq_lock);
  36. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  37. masked_status = status & dev_priv->irq_mask;
  38. spin_unlock(&dev_priv->irq_lock);
  39. if (likely(status))
  40. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  41. if (!masked_status)
  42. return IRQ_NONE;
  43. if (masked_status & (SVGA_IRQFLAG_ANY_FENCE |
  44. SVGA_IRQFLAG_FENCE_GOAL)) {
  45. vmw_fences_update(dev_priv->fman);
  46. wake_up_all(&dev_priv->fence_queue);
  47. }
  48. if (masked_status & SVGA_IRQFLAG_FIFO_PROGRESS)
  49. wake_up_all(&dev_priv->fifo_queue);
  50. return IRQ_HANDLED;
  51. }
  52. static bool vmw_fifo_idle(struct vmw_private *dev_priv, uint32_t seqno)
  53. {
  54. uint32_t busy;
  55. mutex_lock(&dev_priv->hw_mutex);
  56. busy = vmw_read(dev_priv, SVGA_REG_BUSY);
  57. mutex_unlock(&dev_priv->hw_mutex);
  58. return (busy == 0);
  59. }
  60. void vmw_update_seqno(struct vmw_private *dev_priv,
  61. struct vmw_fifo_state *fifo_state)
  62. {
  63. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  64. uint32_t seqno = ioread32(fifo_mem + SVGA_FIFO_FENCE);
  65. if (dev_priv->last_read_seqno != seqno) {
  66. dev_priv->last_read_seqno = seqno;
  67. vmw_marker_pull(&fifo_state->marker_queue, seqno);
  68. vmw_fences_update(dev_priv->fman);
  69. }
  70. }
  71. bool vmw_seqno_passed(struct vmw_private *dev_priv,
  72. uint32_t seqno)
  73. {
  74. struct vmw_fifo_state *fifo_state;
  75. bool ret;
  76. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  77. return true;
  78. fifo_state = &dev_priv->fifo;
  79. vmw_update_seqno(dev_priv, fifo_state);
  80. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  81. return true;
  82. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE) &&
  83. vmw_fifo_idle(dev_priv, seqno))
  84. return true;
  85. /**
  86. * Then check if the seqno is higher than what we've actually
  87. * emitted. Then the fence is stale and signaled.
  88. */
  89. ret = ((atomic_read(&dev_priv->marker_seq) - seqno)
  90. > VMW_FENCE_WRAP);
  91. return ret;
  92. }
  93. int vmw_fallback_wait(struct vmw_private *dev_priv,
  94. bool lazy,
  95. bool fifo_idle,
  96. uint32_t seqno,
  97. bool interruptible,
  98. unsigned long timeout)
  99. {
  100. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  101. uint32_t count = 0;
  102. uint32_t signal_seq;
  103. int ret;
  104. unsigned long end_jiffies = jiffies + timeout;
  105. bool (*wait_condition)(struct vmw_private *, uint32_t);
  106. DEFINE_WAIT(__wait);
  107. wait_condition = (fifo_idle) ? &vmw_fifo_idle :
  108. &vmw_seqno_passed;
  109. /**
  110. * Block command submission while waiting for idle.
  111. */
  112. if (fifo_idle)
  113. down_read(&fifo_state->rwsem);
  114. signal_seq = atomic_read(&dev_priv->marker_seq);
  115. ret = 0;
  116. for (;;) {
  117. prepare_to_wait(&dev_priv->fence_queue, &__wait,
  118. (interruptible) ?
  119. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  120. if (wait_condition(dev_priv, seqno))
  121. break;
  122. if (time_after_eq(jiffies, end_jiffies)) {
  123. DRM_ERROR("SVGA device lockup.\n");
  124. break;
  125. }
  126. if (lazy)
  127. schedule_timeout(1);
  128. else if ((++count & 0x0F) == 0) {
  129. /**
  130. * FIXME: Use schedule_hr_timeout here for
  131. * newer kernels and lower CPU utilization.
  132. */
  133. __set_current_state(TASK_RUNNING);
  134. schedule();
  135. __set_current_state((interruptible) ?
  136. TASK_INTERRUPTIBLE :
  137. TASK_UNINTERRUPTIBLE);
  138. }
  139. if (interruptible && signal_pending(current)) {
  140. ret = -ERESTARTSYS;
  141. break;
  142. }
  143. }
  144. finish_wait(&dev_priv->fence_queue, &__wait);
  145. if (ret == 0 && fifo_idle) {
  146. __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
  147. iowrite32(signal_seq, fifo_mem + SVGA_FIFO_FENCE);
  148. }
  149. wake_up_all(&dev_priv->fence_queue);
  150. if (fifo_idle)
  151. up_read(&fifo_state->rwsem);
  152. return ret;
  153. }
  154. void vmw_seqno_waiter_add(struct vmw_private *dev_priv)
  155. {
  156. mutex_lock(&dev_priv->hw_mutex);
  157. if (dev_priv->fence_queue_waiters++ == 0) {
  158. unsigned long irq_flags;
  159. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  160. outl(SVGA_IRQFLAG_ANY_FENCE,
  161. dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  162. dev_priv->irq_mask |= SVGA_IRQFLAG_ANY_FENCE;
  163. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  164. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  165. }
  166. mutex_unlock(&dev_priv->hw_mutex);
  167. }
  168. void vmw_seqno_waiter_remove(struct vmw_private *dev_priv)
  169. {
  170. mutex_lock(&dev_priv->hw_mutex);
  171. if (--dev_priv->fence_queue_waiters == 0) {
  172. unsigned long irq_flags;
  173. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  174. dev_priv->irq_mask &= ~SVGA_IRQFLAG_ANY_FENCE;
  175. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  176. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  177. }
  178. mutex_unlock(&dev_priv->hw_mutex);
  179. }
  180. void vmw_goal_waiter_add(struct vmw_private *dev_priv)
  181. {
  182. mutex_lock(&dev_priv->hw_mutex);
  183. if (dev_priv->goal_queue_waiters++ == 0) {
  184. unsigned long irq_flags;
  185. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  186. outl(SVGA_IRQFLAG_FENCE_GOAL,
  187. dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  188. dev_priv->irq_mask |= SVGA_IRQFLAG_FENCE_GOAL;
  189. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  190. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  191. }
  192. mutex_unlock(&dev_priv->hw_mutex);
  193. }
  194. void vmw_goal_waiter_remove(struct vmw_private *dev_priv)
  195. {
  196. mutex_lock(&dev_priv->hw_mutex);
  197. if (--dev_priv->goal_queue_waiters == 0) {
  198. unsigned long irq_flags;
  199. spin_lock_irqsave(&dev_priv->irq_lock, irq_flags);
  200. dev_priv->irq_mask &= ~SVGA_IRQFLAG_FENCE_GOAL;
  201. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  202. spin_unlock_irqrestore(&dev_priv->irq_lock, irq_flags);
  203. }
  204. mutex_unlock(&dev_priv->hw_mutex);
  205. }
  206. int vmw_wait_seqno(struct vmw_private *dev_priv,
  207. bool lazy, uint32_t seqno,
  208. bool interruptible, unsigned long timeout)
  209. {
  210. long ret;
  211. struct vmw_fifo_state *fifo = &dev_priv->fifo;
  212. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  213. return 0;
  214. if (likely(vmw_seqno_passed(dev_priv, seqno)))
  215. return 0;
  216. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  217. if (!(fifo->capabilities & SVGA_FIFO_CAP_FENCE))
  218. return vmw_fallback_wait(dev_priv, lazy, true, seqno,
  219. interruptible, timeout);
  220. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  221. return vmw_fallback_wait(dev_priv, lazy, false, seqno,
  222. interruptible, timeout);
  223. vmw_seqno_waiter_add(dev_priv);
  224. if (interruptible)
  225. ret = wait_event_interruptible_timeout
  226. (dev_priv->fence_queue,
  227. vmw_seqno_passed(dev_priv, seqno),
  228. timeout);
  229. else
  230. ret = wait_event_timeout
  231. (dev_priv->fence_queue,
  232. vmw_seqno_passed(dev_priv, seqno),
  233. timeout);
  234. vmw_seqno_waiter_remove(dev_priv);
  235. if (unlikely(ret == 0))
  236. ret = -EBUSY;
  237. else if (likely(ret > 0))
  238. ret = 0;
  239. return ret;
  240. }
  241. void vmw_irq_preinstall(struct drm_device *dev)
  242. {
  243. struct vmw_private *dev_priv = vmw_priv(dev);
  244. uint32_t status;
  245. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  246. return;
  247. spin_lock_init(&dev_priv->irq_lock);
  248. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  249. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  250. }
  251. int vmw_irq_postinstall(struct drm_device *dev)
  252. {
  253. return 0;
  254. }
  255. void vmw_irq_uninstall(struct drm_device *dev)
  256. {
  257. struct vmw_private *dev_priv = vmw_priv(dev);
  258. uint32_t status;
  259. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  260. return;
  261. mutex_lock(&dev_priv->hw_mutex);
  262. vmw_write(dev_priv, SVGA_REG_IRQMASK, 0);
  263. mutex_unlock(&dev_priv->hw_mutex);
  264. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  265. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  266. }