vmwgfx_irq.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 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 <drm/drmP.h>
  28. #include "vmwgfx_drv.h"
  29. #define VMW_FENCE_WRAP (1 << 24)
  30. irqreturn_t vmw_irq_handler(int irq, void *arg)
  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. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  36. masked_status = status & READ_ONCE(dev_priv->irq_mask);
  37. if (likely(status))
  38. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  39. if (!status)
  40. return IRQ_NONE;
  41. if (masked_status & (SVGA_IRQFLAG_ANY_FENCE |
  42. SVGA_IRQFLAG_FENCE_GOAL)) {
  43. vmw_fences_update(dev_priv->fman);
  44. wake_up_all(&dev_priv->fence_queue);
  45. }
  46. if (masked_status & SVGA_IRQFLAG_FIFO_PROGRESS)
  47. wake_up_all(&dev_priv->fifo_queue);
  48. if (masked_status & (SVGA_IRQFLAG_COMMAND_BUFFER |
  49. SVGA_IRQFLAG_ERROR))
  50. vmw_cmdbuf_tasklet_schedule(dev_priv->cman);
  51. return IRQ_HANDLED;
  52. }
  53. static bool vmw_fifo_idle(struct vmw_private *dev_priv, uint32_t seqno)
  54. {
  55. return (vmw_read(dev_priv, SVGA_REG_BUSY) == 0);
  56. }
  57. void vmw_update_seqno(struct vmw_private *dev_priv,
  58. struct vmw_fifo_state *fifo_state)
  59. {
  60. u32 *fifo_mem = dev_priv->mmio_virt;
  61. uint32_t seqno = vmw_mmio_read(fifo_mem + SVGA_FIFO_FENCE);
  62. if (dev_priv->last_read_seqno != seqno) {
  63. dev_priv->last_read_seqno = seqno;
  64. vmw_marker_pull(&fifo_state->marker_queue, seqno);
  65. vmw_fences_update(dev_priv->fman);
  66. }
  67. }
  68. bool vmw_seqno_passed(struct vmw_private *dev_priv,
  69. uint32_t seqno)
  70. {
  71. struct vmw_fifo_state *fifo_state;
  72. bool ret;
  73. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  74. return true;
  75. fifo_state = &dev_priv->fifo;
  76. vmw_update_seqno(dev_priv, fifo_state);
  77. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  78. return true;
  79. if (!(fifo_state->capabilities & SVGA_FIFO_CAP_FENCE) &&
  80. vmw_fifo_idle(dev_priv, seqno))
  81. return true;
  82. /**
  83. * Then check if the seqno is higher than what we've actually
  84. * emitted. Then the fence is stale and signaled.
  85. */
  86. ret = ((atomic_read(&dev_priv->marker_seq) - seqno)
  87. > VMW_FENCE_WRAP);
  88. return ret;
  89. }
  90. int vmw_fallback_wait(struct vmw_private *dev_priv,
  91. bool lazy,
  92. bool fifo_idle,
  93. uint32_t seqno,
  94. bool interruptible,
  95. unsigned long timeout)
  96. {
  97. struct vmw_fifo_state *fifo_state = &dev_priv->fifo;
  98. uint32_t count = 0;
  99. uint32_t signal_seq;
  100. int ret;
  101. unsigned long end_jiffies = jiffies + timeout;
  102. bool (*wait_condition)(struct vmw_private *, uint32_t);
  103. DEFINE_WAIT(__wait);
  104. wait_condition = (fifo_idle) ? &vmw_fifo_idle :
  105. &vmw_seqno_passed;
  106. /**
  107. * Block command submission while waiting for idle.
  108. */
  109. if (fifo_idle) {
  110. down_read(&fifo_state->rwsem);
  111. if (dev_priv->cman) {
  112. ret = vmw_cmdbuf_idle(dev_priv->cman, interruptible,
  113. 10*HZ);
  114. if (ret)
  115. goto out_err;
  116. }
  117. }
  118. signal_seq = atomic_read(&dev_priv->marker_seq);
  119. ret = 0;
  120. for (;;) {
  121. prepare_to_wait(&dev_priv->fence_queue, &__wait,
  122. (interruptible) ?
  123. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  124. if (wait_condition(dev_priv, seqno))
  125. break;
  126. if (time_after_eq(jiffies, end_jiffies)) {
  127. DRM_ERROR("SVGA device lockup.\n");
  128. break;
  129. }
  130. if (lazy)
  131. schedule_timeout(1);
  132. else if ((++count & 0x0F) == 0) {
  133. /**
  134. * FIXME: Use schedule_hr_timeout here for
  135. * newer kernels and lower CPU utilization.
  136. */
  137. __set_current_state(TASK_RUNNING);
  138. schedule();
  139. __set_current_state((interruptible) ?
  140. TASK_INTERRUPTIBLE :
  141. TASK_UNINTERRUPTIBLE);
  142. }
  143. if (interruptible && signal_pending(current)) {
  144. ret = -ERESTARTSYS;
  145. break;
  146. }
  147. }
  148. finish_wait(&dev_priv->fence_queue, &__wait);
  149. if (ret == 0 && fifo_idle) {
  150. u32 *fifo_mem = dev_priv->mmio_virt;
  151. vmw_mmio_write(signal_seq, fifo_mem + SVGA_FIFO_FENCE);
  152. }
  153. wake_up_all(&dev_priv->fence_queue);
  154. out_err:
  155. if (fifo_idle)
  156. up_read(&fifo_state->rwsem);
  157. return ret;
  158. }
  159. void vmw_generic_waiter_add(struct vmw_private *dev_priv,
  160. u32 flag, int *waiter_count)
  161. {
  162. spin_lock_bh(&dev_priv->waiter_lock);
  163. if ((*waiter_count)++ == 0) {
  164. outl(flag, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  165. dev_priv->irq_mask |= flag;
  166. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  167. }
  168. spin_unlock_bh(&dev_priv->waiter_lock);
  169. }
  170. void vmw_generic_waiter_remove(struct vmw_private *dev_priv,
  171. u32 flag, int *waiter_count)
  172. {
  173. spin_lock_bh(&dev_priv->waiter_lock);
  174. if (--(*waiter_count) == 0) {
  175. dev_priv->irq_mask &= ~flag;
  176. vmw_write(dev_priv, SVGA_REG_IRQMASK, dev_priv->irq_mask);
  177. }
  178. spin_unlock_bh(&dev_priv->waiter_lock);
  179. }
  180. void vmw_seqno_waiter_add(struct vmw_private *dev_priv)
  181. {
  182. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ANY_FENCE,
  183. &dev_priv->fence_queue_waiters);
  184. }
  185. void vmw_seqno_waiter_remove(struct vmw_private *dev_priv)
  186. {
  187. vmw_generic_waiter_remove(dev_priv, SVGA_IRQFLAG_ANY_FENCE,
  188. &dev_priv->fence_queue_waiters);
  189. }
  190. void vmw_goal_waiter_add(struct vmw_private *dev_priv)
  191. {
  192. vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_FENCE_GOAL,
  193. &dev_priv->goal_queue_waiters);
  194. }
  195. void vmw_goal_waiter_remove(struct vmw_private *dev_priv)
  196. {
  197. vmw_generic_waiter_remove(dev_priv, SVGA_IRQFLAG_FENCE_GOAL,
  198. &dev_priv->goal_queue_waiters);
  199. }
  200. int vmw_wait_seqno(struct vmw_private *dev_priv,
  201. bool lazy, uint32_t seqno,
  202. bool interruptible, unsigned long timeout)
  203. {
  204. long ret;
  205. struct vmw_fifo_state *fifo = &dev_priv->fifo;
  206. if (likely(dev_priv->last_read_seqno - seqno < VMW_FENCE_WRAP))
  207. return 0;
  208. if (likely(vmw_seqno_passed(dev_priv, seqno)))
  209. return 0;
  210. vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
  211. if (!(fifo->capabilities & SVGA_FIFO_CAP_FENCE))
  212. return vmw_fallback_wait(dev_priv, lazy, true, seqno,
  213. interruptible, timeout);
  214. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  215. return vmw_fallback_wait(dev_priv, lazy, false, seqno,
  216. interruptible, timeout);
  217. vmw_seqno_waiter_add(dev_priv);
  218. if (interruptible)
  219. ret = wait_event_interruptible_timeout
  220. (dev_priv->fence_queue,
  221. vmw_seqno_passed(dev_priv, seqno),
  222. timeout);
  223. else
  224. ret = wait_event_timeout
  225. (dev_priv->fence_queue,
  226. vmw_seqno_passed(dev_priv, seqno),
  227. timeout);
  228. vmw_seqno_waiter_remove(dev_priv);
  229. if (unlikely(ret == 0))
  230. ret = -EBUSY;
  231. else if (likely(ret > 0))
  232. ret = 0;
  233. return ret;
  234. }
  235. void vmw_irq_preinstall(struct drm_device *dev)
  236. {
  237. struct vmw_private *dev_priv = vmw_priv(dev);
  238. uint32_t status;
  239. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  240. return;
  241. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  242. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  243. }
  244. int vmw_irq_postinstall(struct drm_device *dev)
  245. {
  246. return 0;
  247. }
  248. void vmw_irq_uninstall(struct drm_device *dev)
  249. {
  250. struct vmw_private *dev_priv = vmw_priv(dev);
  251. uint32_t status;
  252. if (!(dev_priv->capabilities & SVGA_CAP_IRQMASK))
  253. return;
  254. vmw_write(dev_priv, SVGA_REG_IRQMASK, 0);
  255. status = inl(dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  256. outl(status, dev_priv->io_start + VMWGFX_IRQSTATUS_PORT);
  257. }