nouveau_fence.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. struct nouveau_fence_uevent {
  144. struct nouveau_eventh handler;
  145. struct nouveau_fence_priv *priv;
  146. };
  147. static int
  148. nouveau_fence_wait_uevent_handler(struct nouveau_eventh *event, int index)
  149. {
  150. struct nouveau_fence_uevent *uevent =
  151. container_of(event, struct nouveau_fence_uevent, handler);
  152. wake_up_all(&uevent->priv->waiting);
  153. return NVKM_EVENT_KEEP;
  154. }
  155. static int
  156. nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
  157. {
  158. struct nouveau_channel *chan = fence->channel;
  159. struct nouveau_fifo *pfifo = nouveau_fifo(chan->drm->device);
  160. struct nouveau_fence_priv *priv = chan->drm->fence;
  161. struct nouveau_fence_uevent uevent = {
  162. .handler.func = nouveau_fence_wait_uevent_handler,
  163. .priv = priv,
  164. };
  165. int ret = 0;
  166. nouveau_event_get(pfifo->uevent, 0, &uevent.handler);
  167. if (fence->timeout) {
  168. unsigned long timeout = fence->timeout - jiffies;
  169. if (time_before(jiffies, fence->timeout)) {
  170. if (intr) {
  171. ret = wait_event_interruptible_timeout(
  172. priv->waiting,
  173. nouveau_fence_done(fence),
  174. timeout);
  175. } else {
  176. ret = wait_event_timeout(priv->waiting,
  177. nouveau_fence_done(fence),
  178. timeout);
  179. }
  180. }
  181. if (ret >= 0) {
  182. fence->timeout = jiffies + ret;
  183. if (time_after_eq(jiffies, fence->timeout))
  184. ret = -EBUSY;
  185. }
  186. } else {
  187. if (intr) {
  188. ret = wait_event_interruptible(priv->waiting,
  189. nouveau_fence_done(fence));
  190. } else {
  191. wait_event(priv->waiting, nouveau_fence_done(fence));
  192. }
  193. }
  194. nouveau_event_put(pfifo->uevent, 0, &uevent.handler);
  195. if (unlikely(ret < 0))
  196. return ret;
  197. return 0;
  198. }
  199. int
  200. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  201. {
  202. struct nouveau_channel *chan = fence->channel;
  203. struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
  204. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  205. ktime_t t;
  206. int ret = 0;
  207. while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
  208. ret = nouveau_fence_wait_uevent(fence, intr);
  209. if (ret < 0)
  210. return ret;
  211. }
  212. while (!nouveau_fence_done(fence)) {
  213. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  214. ret = -EBUSY;
  215. break;
  216. }
  217. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  218. TASK_UNINTERRUPTIBLE);
  219. if (lazy) {
  220. t = ktime_set(0, sleep_time);
  221. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  222. sleep_time *= 2;
  223. if (sleep_time > NSEC_PER_MSEC)
  224. sleep_time = NSEC_PER_MSEC;
  225. }
  226. if (intr && signal_pending(current)) {
  227. ret = -ERESTARTSYS;
  228. break;
  229. }
  230. }
  231. __set_current_state(TASK_RUNNING);
  232. return ret;
  233. }
  234. int
  235. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  236. {
  237. struct nouveau_fence_chan *fctx = chan->fence;
  238. struct nouveau_channel *prev;
  239. int ret = 0;
  240. prev = fence ? fence->channel : NULL;
  241. if (prev) {
  242. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  243. ret = fctx->sync(fence, prev, chan);
  244. if (unlikely(ret))
  245. ret = nouveau_fence_wait(fence, true, false);
  246. }
  247. }
  248. return ret;
  249. }
  250. static void
  251. nouveau_fence_del(struct kref *kref)
  252. {
  253. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  254. kfree(fence);
  255. }
  256. void
  257. nouveau_fence_unref(struct nouveau_fence **pfence)
  258. {
  259. if (*pfence)
  260. kref_put(&(*pfence)->kref, nouveau_fence_del);
  261. *pfence = NULL;
  262. }
  263. struct nouveau_fence *
  264. nouveau_fence_ref(struct nouveau_fence *fence)
  265. {
  266. kref_get(&fence->kref);
  267. return fence;
  268. }
  269. int
  270. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  271. struct nouveau_fence **pfence)
  272. {
  273. struct nouveau_fence *fence;
  274. int ret = 0;
  275. if (unlikely(!chan->fence))
  276. return -ENODEV;
  277. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  278. if (!fence)
  279. return -ENOMEM;
  280. INIT_LIST_HEAD(&fence->work);
  281. fence->sysmem = sysmem;
  282. kref_init(&fence->kref);
  283. ret = nouveau_fence_emit(fence, chan);
  284. if (ret)
  285. nouveau_fence_unref(&fence);
  286. *pfence = fence;
  287. return ret;
  288. }