nouveau_fence.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <drm/drmP.h>
  27. #include <linux/ktime.h>
  28. #include <linux/hrtimer.h>
  29. #include "nouveau_drm.h"
  30. #include "nouveau_dma.h"
  31. #include "nouveau_fence.h"
  32. #include <engine/fifo.h>
  33. struct fence_work {
  34. struct work_struct base;
  35. struct list_head head;
  36. void (*func)(void *);
  37. void *data;
  38. };
  39. static void
  40. nouveau_fence_signal(struct nouveau_fence *fence)
  41. {
  42. struct fence_work *work, *temp;
  43. list_for_each_entry_safe(work, temp, &fence->work, head) {
  44. schedule_work(&work->base);
  45. list_del(&work->head);
  46. }
  47. fence->channel = NULL;
  48. list_del(&fence->head);
  49. }
  50. void
  51. nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
  52. {
  53. struct nouveau_fence *fence, *fnext;
  54. spin_lock(&fctx->lock);
  55. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  56. nouveau_fence_signal(fence);
  57. }
  58. spin_unlock(&fctx->lock);
  59. }
  60. void
  61. nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
  62. {
  63. INIT_LIST_HEAD(&fctx->flip);
  64. INIT_LIST_HEAD(&fctx->pending);
  65. spin_lock_init(&fctx->lock);
  66. }
  67. static void
  68. nouveau_fence_work_handler(struct work_struct *kwork)
  69. {
  70. struct fence_work *work = container_of(kwork, typeof(*work), base);
  71. work->func(work->data);
  72. kfree(work);
  73. }
  74. void
  75. nouveau_fence_work(struct nouveau_fence *fence,
  76. void (*func)(void *), void *data)
  77. {
  78. struct nouveau_channel *chan = fence->channel;
  79. struct nouveau_fence_chan *fctx;
  80. struct fence_work *work = NULL;
  81. if (nouveau_fence_done(fence)) {
  82. func(data);
  83. return;
  84. }
  85. fctx = chan->fence;
  86. work = kmalloc(sizeof(*work), GFP_KERNEL);
  87. if (!work) {
  88. WARN_ON(nouveau_fence_wait(fence, false, false));
  89. func(data);
  90. return;
  91. }
  92. spin_lock(&fctx->lock);
  93. if (!fence->channel) {
  94. spin_unlock(&fctx->lock);
  95. kfree(work);
  96. func(data);
  97. return;
  98. }
  99. INIT_WORK(&work->base, nouveau_fence_work_handler);
  100. work->func = func;
  101. work->data = data;
  102. list_add(&work->head, &fence->work);
  103. spin_unlock(&fctx->lock);
  104. }
  105. static void
  106. nouveau_fence_update(struct nouveau_channel *chan)
  107. {
  108. struct nouveau_fence_chan *fctx = chan->fence;
  109. struct nouveau_fence *fence, *fnext;
  110. spin_lock(&fctx->lock);
  111. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  112. if (fctx->read(chan) < fence->sequence)
  113. break;
  114. nouveau_fence_signal(fence);
  115. nouveau_fence_unref(&fence);
  116. }
  117. spin_unlock(&fctx->lock);
  118. }
  119. int
  120. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  121. {
  122. struct nouveau_fence_chan *fctx = chan->fence;
  123. int ret;
  124. fence->channel = chan;
  125. fence->timeout = jiffies + (15 * DRM_HZ);
  126. fence->sequence = ++fctx->sequence;
  127. ret = fctx->emit(fence);
  128. if (!ret) {
  129. kref_get(&fence->kref);
  130. spin_lock(&fctx->lock);
  131. list_add_tail(&fence->head, &fctx->pending);
  132. spin_unlock(&fctx->lock);
  133. }
  134. return ret;
  135. }
  136. bool
  137. nouveau_fence_done(struct nouveau_fence *fence)
  138. {
  139. if (fence->channel)
  140. nouveau_fence_update(fence->channel);
  141. return !fence->channel;
  142. }
  143. static int
  144. nouveau_fence_wait_uevent_handler(void *data, int index)
  145. {
  146. struct nouveau_fence_priv *priv = data;
  147. wake_up_all(&priv->waiting);
  148. return NVKM_EVENT_KEEP;
  149. }
  150. static int
  151. nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
  152. {
  153. struct nouveau_channel *chan = fence->channel;
  154. struct nouveau_fifo *pfifo = nouveau_fifo(chan->drm->device);
  155. struct nouveau_fence_priv *priv = chan->drm->fence;
  156. struct nouveau_eventh *handler;
  157. int ret = 0;
  158. ret = nouveau_event_new(pfifo->uevent, 0,
  159. nouveau_fence_wait_uevent_handler,
  160. priv, &handler);
  161. if (ret)
  162. return ret;
  163. nouveau_event_get(handler);
  164. if (fence->timeout) {
  165. unsigned long timeout = fence->timeout - jiffies;
  166. if (time_before(jiffies, fence->timeout)) {
  167. if (intr) {
  168. ret = wait_event_interruptible_timeout(
  169. priv->waiting,
  170. nouveau_fence_done(fence),
  171. timeout);
  172. } else {
  173. ret = wait_event_timeout(priv->waiting,
  174. nouveau_fence_done(fence),
  175. timeout);
  176. }
  177. }
  178. if (ret >= 0) {
  179. fence->timeout = jiffies + ret;
  180. if (time_after_eq(jiffies, fence->timeout))
  181. ret = -EBUSY;
  182. }
  183. } else {
  184. if (intr) {
  185. ret = wait_event_interruptible(priv->waiting,
  186. nouveau_fence_done(fence));
  187. } else {
  188. wait_event(priv->waiting, nouveau_fence_done(fence));
  189. }
  190. }
  191. nouveau_event_ref(NULL, &handler);
  192. if (unlikely(ret < 0))
  193. return ret;
  194. return 0;
  195. }
  196. int
  197. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  198. {
  199. struct nouveau_channel *chan = fence->channel;
  200. struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
  201. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  202. ktime_t t;
  203. int ret = 0;
  204. while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
  205. ret = nouveau_fence_wait_uevent(fence, intr);
  206. if (ret < 0)
  207. return ret;
  208. }
  209. while (!nouveau_fence_done(fence)) {
  210. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  211. ret = -EBUSY;
  212. break;
  213. }
  214. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  215. TASK_UNINTERRUPTIBLE);
  216. if (lazy) {
  217. t = ktime_set(0, sleep_time);
  218. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  219. sleep_time *= 2;
  220. if (sleep_time > NSEC_PER_MSEC)
  221. sleep_time = NSEC_PER_MSEC;
  222. }
  223. if (intr && signal_pending(current)) {
  224. ret = -ERESTARTSYS;
  225. break;
  226. }
  227. }
  228. __set_current_state(TASK_RUNNING);
  229. return ret;
  230. }
  231. int
  232. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  233. {
  234. struct nouveau_fence_chan *fctx = chan->fence;
  235. struct nouveau_channel *prev;
  236. int ret = 0;
  237. prev = fence ? fence->channel : NULL;
  238. if (prev) {
  239. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  240. ret = fctx->sync(fence, prev, chan);
  241. if (unlikely(ret))
  242. ret = nouveau_fence_wait(fence, true, false);
  243. }
  244. }
  245. return ret;
  246. }
  247. static void
  248. nouveau_fence_del(struct kref *kref)
  249. {
  250. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  251. kfree(fence);
  252. }
  253. void
  254. nouveau_fence_unref(struct nouveau_fence **pfence)
  255. {
  256. if (*pfence)
  257. kref_put(&(*pfence)->kref, nouveau_fence_del);
  258. *pfence = NULL;
  259. }
  260. struct nouveau_fence *
  261. nouveau_fence_ref(struct nouveau_fence *fence)
  262. {
  263. if (fence)
  264. kref_get(&fence->kref);
  265. return fence;
  266. }
  267. int
  268. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  269. struct nouveau_fence **pfence)
  270. {
  271. struct nouveau_fence *fence;
  272. int ret = 0;
  273. if (unlikely(!chan->fence))
  274. return -ENODEV;
  275. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  276. if (!fence)
  277. return -ENOMEM;
  278. INIT_LIST_HEAD(&fence->work);
  279. fence->sysmem = sysmem;
  280. kref_init(&fence->kref);
  281. ret = nouveau_fence_emit(fence, chan);
  282. if (ret)
  283. nouveau_fence_unref(&fence);
  284. *pfence = fence;
  285. return ret;
  286. }