nouveau_fence.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <nvif/notify.h>
  30. #include <nvif/event.h>
  31. #include "nouveau_drm.h"
  32. #include "nouveau_dma.h"
  33. #include "nouveau_fence.h"
  34. struct fence_work {
  35. struct work_struct base;
  36. struct list_head head;
  37. void (*func)(void *);
  38. void *data;
  39. };
  40. static void
  41. nouveau_fence_signal(struct nouveau_fence *fence)
  42. {
  43. struct fence_work *work, *temp;
  44. list_for_each_entry_safe(work, temp, &fence->work, head) {
  45. schedule_work(&work->base);
  46. list_del(&work->head);
  47. }
  48. fence->channel = NULL;
  49. list_del(&fence->head);
  50. }
  51. void
  52. nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
  53. {
  54. struct nouveau_fence *fence, *fnext;
  55. spin_lock(&fctx->lock);
  56. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  57. nouveau_fence_signal(fence);
  58. }
  59. spin_unlock(&fctx->lock);
  60. }
  61. void
  62. nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
  63. {
  64. INIT_LIST_HEAD(&fctx->flip);
  65. INIT_LIST_HEAD(&fctx->pending);
  66. spin_lock_init(&fctx->lock);
  67. }
  68. static void
  69. nouveau_fence_work_handler(struct work_struct *kwork)
  70. {
  71. struct fence_work *work = container_of(kwork, typeof(*work), base);
  72. work->func(work->data);
  73. kfree(work);
  74. }
  75. void
  76. nouveau_fence_work(struct nouveau_fence *fence,
  77. void (*func)(void *), void *data)
  78. {
  79. struct nouveau_channel *chan = fence->channel;
  80. struct nouveau_fence_chan *fctx;
  81. struct fence_work *work = NULL;
  82. if (nouveau_fence_done(fence)) {
  83. func(data);
  84. return;
  85. }
  86. fctx = chan->fence;
  87. work = kmalloc(sizeof(*work), GFP_KERNEL);
  88. if (!work) {
  89. WARN_ON(nouveau_fence_wait(fence, false, false));
  90. func(data);
  91. return;
  92. }
  93. spin_lock(&fctx->lock);
  94. if (!fence->channel) {
  95. spin_unlock(&fctx->lock);
  96. kfree(work);
  97. func(data);
  98. return;
  99. }
  100. INIT_WORK(&work->base, nouveau_fence_work_handler);
  101. work->func = func;
  102. work->data = data;
  103. list_add(&work->head, &fence->work);
  104. spin_unlock(&fctx->lock);
  105. }
  106. static void
  107. nouveau_fence_update(struct nouveau_channel *chan)
  108. {
  109. struct nouveau_fence_chan *fctx = chan->fence;
  110. struct nouveau_fence *fence, *fnext;
  111. spin_lock(&fctx->lock);
  112. list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
  113. if (fctx->read(chan) < fence->sequence)
  114. break;
  115. nouveau_fence_signal(fence);
  116. nouveau_fence_unref(&fence);
  117. }
  118. spin_unlock(&fctx->lock);
  119. }
  120. int
  121. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  122. {
  123. struct nouveau_fence_chan *fctx = chan->fence;
  124. int ret;
  125. fence->channel = chan;
  126. fence->timeout = jiffies + (15 * HZ);
  127. fence->sequence = ++fctx->sequence;
  128. ret = fctx->emit(fence);
  129. if (!ret) {
  130. kref_get(&fence->kref);
  131. spin_lock(&fctx->lock);
  132. list_add_tail(&fence->head, &fctx->pending);
  133. spin_unlock(&fctx->lock);
  134. }
  135. return ret;
  136. }
  137. bool
  138. nouveau_fence_done(struct nouveau_fence *fence)
  139. {
  140. if (fence->channel)
  141. nouveau_fence_update(fence->channel);
  142. return !fence->channel;
  143. }
  144. struct nouveau_fence_wait {
  145. struct nouveau_fence_priv *priv;
  146. struct nvif_notify notify;
  147. };
  148. static int
  149. nouveau_fence_wait_uevent_handler(struct nvif_notify *notify)
  150. {
  151. struct nouveau_fence_wait *wait =
  152. container_of(notify, typeof(*wait), notify);
  153. wake_up_all(&wait->priv->waiting);
  154. return NVIF_NOTIFY_KEEP;
  155. }
  156. static int
  157. nouveau_fence_wait_uevent(struct nouveau_fence *fence, bool intr)
  158. {
  159. struct nouveau_channel *chan = fence->channel;
  160. struct nouveau_fence_priv *priv = chan->drm->fence;
  161. struct nouveau_fence_wait wait = { .priv = priv };
  162. int ret = 0;
  163. ret = nvif_notify_init(chan->object, NULL,
  164. nouveau_fence_wait_uevent_handler, false,
  165. G82_CHANNEL_DMA_V0_NTFY_UEVENT,
  166. &(struct nvif_notify_uevent_req) {
  167. },
  168. sizeof(struct nvif_notify_uevent_req),
  169. sizeof(struct nvif_notify_uevent_rep),
  170. &wait.notify);
  171. if (ret)
  172. return ret;
  173. nvif_notify_get(&wait.notify);
  174. if (fence->timeout) {
  175. unsigned long timeout = fence->timeout - jiffies;
  176. if (time_before(jiffies, fence->timeout)) {
  177. if (intr) {
  178. ret = wait_event_interruptible_timeout(
  179. priv->waiting,
  180. nouveau_fence_done(fence),
  181. timeout);
  182. } else {
  183. ret = wait_event_timeout(priv->waiting,
  184. nouveau_fence_done(fence),
  185. timeout);
  186. }
  187. }
  188. if (ret >= 0) {
  189. fence->timeout = jiffies + ret;
  190. if (time_after_eq(jiffies, fence->timeout))
  191. ret = -EBUSY;
  192. }
  193. } else {
  194. if (intr) {
  195. ret = wait_event_interruptible(priv->waiting,
  196. nouveau_fence_done(fence));
  197. } else {
  198. wait_event(priv->waiting, nouveau_fence_done(fence));
  199. }
  200. }
  201. nvif_notify_fini(&wait.notify);
  202. if (unlikely(ret < 0))
  203. return ret;
  204. return 0;
  205. }
  206. int
  207. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  208. {
  209. struct nouveau_channel *chan = fence->channel;
  210. struct nouveau_fence_priv *priv = chan ? chan->drm->fence : NULL;
  211. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  212. ktime_t t;
  213. int ret = 0;
  214. while (priv && priv->uevent && lazy && !nouveau_fence_done(fence)) {
  215. ret = nouveau_fence_wait_uevent(fence, intr);
  216. if (ret < 0)
  217. return ret;
  218. }
  219. while (!nouveau_fence_done(fence)) {
  220. if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
  221. ret = -EBUSY;
  222. break;
  223. }
  224. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  225. TASK_UNINTERRUPTIBLE);
  226. if (lazy) {
  227. t = ktime_set(0, sleep_time);
  228. schedule_hrtimeout(&t, HRTIMER_MODE_REL);
  229. sleep_time *= 2;
  230. if (sleep_time > NSEC_PER_MSEC)
  231. sleep_time = NSEC_PER_MSEC;
  232. }
  233. if (intr && signal_pending(current)) {
  234. ret = -ERESTARTSYS;
  235. break;
  236. }
  237. }
  238. __set_current_state(TASK_RUNNING);
  239. return ret;
  240. }
  241. int
  242. nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
  243. {
  244. struct nouveau_fence_chan *fctx = chan->fence;
  245. struct nouveau_channel *prev;
  246. int ret = 0;
  247. prev = fence ? fence->channel : NULL;
  248. if (prev) {
  249. if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
  250. ret = fctx->sync(fence, prev, chan);
  251. if (unlikely(ret))
  252. ret = nouveau_fence_wait(fence, true, false);
  253. }
  254. }
  255. return ret;
  256. }
  257. static void
  258. nouveau_fence_del(struct kref *kref)
  259. {
  260. struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
  261. kfree(fence);
  262. }
  263. void
  264. nouveau_fence_unref(struct nouveau_fence **pfence)
  265. {
  266. if (*pfence)
  267. kref_put(&(*pfence)->kref, nouveau_fence_del);
  268. *pfence = NULL;
  269. }
  270. struct nouveau_fence *
  271. nouveau_fence_ref(struct nouveau_fence *fence)
  272. {
  273. if (fence)
  274. kref_get(&fence->kref);
  275. return fence;
  276. }
  277. int
  278. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  279. struct nouveau_fence **pfence)
  280. {
  281. struct nouveau_fence *fence;
  282. int ret = 0;
  283. if (unlikely(!chan->fence))
  284. return -ENODEV;
  285. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  286. if (!fence)
  287. return -ENOMEM;
  288. INIT_LIST_HEAD(&fence->work);
  289. fence->sysmem = sysmem;
  290. kref_init(&fence->kref);
  291. ret = nouveau_fence_emit(fence, chan);
  292. if (ret)
  293. nouveau_fence_unref(&fence);
  294. *pfence = fence;
  295. return ret;
  296. }