nouveau_fence.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <trace/events/dma_fence.h>
  30. #include <nvif/cl826e.h>
  31. #include <nvif/notify.h>
  32. #include <nvif/event.h>
  33. #include "nouveau_drv.h"
  34. #include "nouveau_dma.h"
  35. #include "nouveau_fence.h"
  36. static const struct dma_fence_ops nouveau_fence_ops_uevent;
  37. static const struct dma_fence_ops nouveau_fence_ops_legacy;
  38. static inline struct nouveau_fence *
  39. from_fence(struct dma_fence *fence)
  40. {
  41. return container_of(fence, struct nouveau_fence, base);
  42. }
  43. static inline struct nouveau_fence_chan *
  44. nouveau_fctx(struct nouveau_fence *fence)
  45. {
  46. return container_of(fence->base.lock, struct nouveau_fence_chan, lock);
  47. }
  48. static int
  49. nouveau_fence_signal(struct nouveau_fence *fence)
  50. {
  51. int drop = 0;
  52. dma_fence_signal_locked(&fence->base);
  53. list_del(&fence->head);
  54. rcu_assign_pointer(fence->channel, NULL);
  55. if (test_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags)) {
  56. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  57. if (!--fctx->notify_ref)
  58. drop = 1;
  59. }
  60. dma_fence_put(&fence->base);
  61. return drop;
  62. }
  63. static struct nouveau_fence *
  64. nouveau_local_fence(struct dma_fence *fence, struct nouveau_drm *drm)
  65. {
  66. if (fence->ops != &nouveau_fence_ops_legacy &&
  67. fence->ops != &nouveau_fence_ops_uevent)
  68. return NULL;
  69. if (fence->context < drm->chan.context_base ||
  70. fence->context >= drm->chan.context_base + drm->chan.nr)
  71. return NULL;
  72. return from_fence(fence);
  73. }
  74. void
  75. nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
  76. {
  77. struct nouveau_fence *fence;
  78. spin_lock_irq(&fctx->lock);
  79. while (!list_empty(&fctx->pending)) {
  80. fence = list_entry(fctx->pending.next, typeof(*fence), head);
  81. if (nouveau_fence_signal(fence))
  82. nvif_notify_put(&fctx->notify);
  83. }
  84. spin_unlock_irq(&fctx->lock);
  85. nvif_notify_fini(&fctx->notify);
  86. fctx->dead = 1;
  87. /*
  88. * Ensure that all accesses to fence->channel complete before freeing
  89. * the channel.
  90. */
  91. synchronize_rcu();
  92. }
  93. static void
  94. nouveau_fence_context_put(struct kref *fence_ref)
  95. {
  96. kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref));
  97. }
  98. void
  99. nouveau_fence_context_free(struct nouveau_fence_chan *fctx)
  100. {
  101. kref_put(&fctx->fence_ref, nouveau_fence_context_put);
  102. }
  103. static int
  104. nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
  105. {
  106. struct nouveau_fence *fence;
  107. int drop = 0;
  108. u32 seq = fctx->read(chan);
  109. while (!list_empty(&fctx->pending)) {
  110. fence = list_entry(fctx->pending.next, typeof(*fence), head);
  111. if ((int)(seq - fence->base.seqno) < 0)
  112. break;
  113. drop |= nouveau_fence_signal(fence);
  114. }
  115. return drop;
  116. }
  117. static int
  118. nouveau_fence_wait_uevent_handler(struct nvif_notify *notify)
  119. {
  120. struct nouveau_fence_chan *fctx =
  121. container_of(notify, typeof(*fctx), notify);
  122. unsigned long flags;
  123. int ret = NVIF_NOTIFY_KEEP;
  124. spin_lock_irqsave(&fctx->lock, flags);
  125. if (!list_empty(&fctx->pending)) {
  126. struct nouveau_fence *fence;
  127. struct nouveau_channel *chan;
  128. fence = list_entry(fctx->pending.next, typeof(*fence), head);
  129. chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
  130. if (nouveau_fence_update(chan, fctx))
  131. ret = NVIF_NOTIFY_DROP;
  132. }
  133. spin_unlock_irqrestore(&fctx->lock, flags);
  134. return ret;
  135. }
  136. void
  137. nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx)
  138. {
  139. struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
  140. struct nouveau_cli *cli = (void *)chan->user.client;
  141. int ret;
  142. INIT_LIST_HEAD(&fctx->flip);
  143. INIT_LIST_HEAD(&fctx->pending);
  144. spin_lock_init(&fctx->lock);
  145. fctx->context = chan->drm->chan.context_base + chan->chid;
  146. if (chan == chan->drm->cechan)
  147. strcpy(fctx->name, "copy engine channel");
  148. else if (chan == chan->drm->channel)
  149. strcpy(fctx->name, "generic kernel channel");
  150. else
  151. strcpy(fctx->name, nvxx_client(&cli->base)->name);
  152. kref_init(&fctx->fence_ref);
  153. if (!priv->uevent)
  154. return;
  155. ret = nvif_notify_init(&chan->user, nouveau_fence_wait_uevent_handler,
  156. false, NV826E_V0_NTFY_NON_STALL_INTERRUPT,
  157. &(struct nvif_notify_uevent_req) { },
  158. sizeof(struct nvif_notify_uevent_req),
  159. sizeof(struct nvif_notify_uevent_rep),
  160. &fctx->notify);
  161. WARN_ON(ret);
  162. }
  163. int
  164. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  165. {
  166. struct nouveau_fence_chan *fctx = chan->fence;
  167. struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
  168. int ret;
  169. fence->channel = chan;
  170. fence->timeout = jiffies + (15 * HZ);
  171. if (priv->uevent)
  172. dma_fence_init(&fence->base, &nouveau_fence_ops_uevent,
  173. &fctx->lock, fctx->context, ++fctx->sequence);
  174. else
  175. dma_fence_init(&fence->base, &nouveau_fence_ops_legacy,
  176. &fctx->lock, fctx->context, ++fctx->sequence);
  177. kref_get(&fctx->fence_ref);
  178. trace_dma_fence_emit(&fence->base);
  179. ret = fctx->emit(fence);
  180. if (!ret) {
  181. dma_fence_get(&fence->base);
  182. spin_lock_irq(&fctx->lock);
  183. if (nouveau_fence_update(chan, fctx))
  184. nvif_notify_put(&fctx->notify);
  185. list_add_tail(&fence->head, &fctx->pending);
  186. spin_unlock_irq(&fctx->lock);
  187. }
  188. return ret;
  189. }
  190. bool
  191. nouveau_fence_done(struct nouveau_fence *fence)
  192. {
  193. if (fence->base.ops == &nouveau_fence_ops_legacy ||
  194. fence->base.ops == &nouveau_fence_ops_uevent) {
  195. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  196. struct nouveau_channel *chan;
  197. unsigned long flags;
  198. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  199. return true;
  200. spin_lock_irqsave(&fctx->lock, flags);
  201. chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
  202. if (chan && nouveau_fence_update(chan, fctx))
  203. nvif_notify_put(&fctx->notify);
  204. spin_unlock_irqrestore(&fctx->lock, flags);
  205. }
  206. return dma_fence_is_signaled(&fence->base);
  207. }
  208. static long
  209. nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait)
  210. {
  211. struct nouveau_fence *fence = from_fence(f);
  212. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  213. unsigned long t = jiffies, timeout = t + wait;
  214. while (!nouveau_fence_done(fence)) {
  215. ktime_t kt;
  216. t = jiffies;
  217. if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
  218. __set_current_state(TASK_RUNNING);
  219. return 0;
  220. }
  221. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  222. TASK_UNINTERRUPTIBLE);
  223. kt = sleep_time;
  224. schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
  225. sleep_time *= 2;
  226. if (sleep_time > NSEC_PER_MSEC)
  227. sleep_time = NSEC_PER_MSEC;
  228. if (intr && signal_pending(current))
  229. return -ERESTARTSYS;
  230. }
  231. __set_current_state(TASK_RUNNING);
  232. return timeout - t;
  233. }
  234. static int
  235. nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
  236. {
  237. int ret = 0;
  238. while (!nouveau_fence_done(fence)) {
  239. if (time_after_eq(jiffies, fence->timeout)) {
  240. ret = -EBUSY;
  241. break;
  242. }
  243. __set_current_state(intr ?
  244. TASK_INTERRUPTIBLE :
  245. TASK_UNINTERRUPTIBLE);
  246. if (intr && signal_pending(current)) {
  247. ret = -ERESTARTSYS;
  248. break;
  249. }
  250. }
  251. __set_current_state(TASK_RUNNING);
  252. return ret;
  253. }
  254. int
  255. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  256. {
  257. long ret;
  258. if (!lazy)
  259. return nouveau_fence_wait_busy(fence, intr);
  260. ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ);
  261. if (ret < 0)
  262. return ret;
  263. else if (!ret)
  264. return -EBUSY;
  265. else
  266. return 0;
  267. }
  268. int
  269. nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr)
  270. {
  271. struct nouveau_fence_chan *fctx = chan->fence;
  272. struct dma_fence *fence;
  273. struct reservation_object *resv = nvbo->bo.resv;
  274. struct reservation_object_list *fobj;
  275. struct nouveau_fence *f;
  276. int ret = 0, i;
  277. if (!exclusive) {
  278. ret = reservation_object_reserve_shared(resv);
  279. if (ret)
  280. return ret;
  281. }
  282. fobj = reservation_object_get_list(resv);
  283. fence = reservation_object_get_excl(resv);
  284. if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
  285. struct nouveau_channel *prev = NULL;
  286. bool must_wait = true;
  287. f = nouveau_local_fence(fence, chan->drm);
  288. if (f) {
  289. rcu_read_lock();
  290. prev = rcu_dereference(f->channel);
  291. if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
  292. must_wait = false;
  293. rcu_read_unlock();
  294. }
  295. if (must_wait)
  296. ret = dma_fence_wait(fence, intr);
  297. return ret;
  298. }
  299. if (!exclusive || !fobj)
  300. return ret;
  301. for (i = 0; i < fobj->shared_count && !ret; ++i) {
  302. struct nouveau_channel *prev = NULL;
  303. bool must_wait = true;
  304. fence = rcu_dereference_protected(fobj->shared[i],
  305. reservation_object_held(resv));
  306. f = nouveau_local_fence(fence, chan->drm);
  307. if (f) {
  308. rcu_read_lock();
  309. prev = rcu_dereference(f->channel);
  310. if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
  311. must_wait = false;
  312. rcu_read_unlock();
  313. }
  314. if (must_wait)
  315. ret = dma_fence_wait(fence, intr);
  316. }
  317. return ret;
  318. }
  319. void
  320. nouveau_fence_unref(struct nouveau_fence **pfence)
  321. {
  322. if (*pfence)
  323. dma_fence_put(&(*pfence)->base);
  324. *pfence = NULL;
  325. }
  326. int
  327. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  328. struct nouveau_fence **pfence)
  329. {
  330. struct nouveau_fence *fence;
  331. int ret = 0;
  332. if (unlikely(!chan->fence))
  333. return -ENODEV;
  334. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  335. if (!fence)
  336. return -ENOMEM;
  337. ret = nouveau_fence_emit(fence, chan);
  338. if (ret)
  339. nouveau_fence_unref(&fence);
  340. *pfence = fence;
  341. return ret;
  342. }
  343. static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence)
  344. {
  345. return "nouveau";
  346. }
  347. static const char *nouveau_fence_get_timeline_name(struct dma_fence *f)
  348. {
  349. struct nouveau_fence *fence = from_fence(f);
  350. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  351. return !fctx->dead ? fctx->name : "dead channel";
  352. }
  353. /*
  354. * In an ideal world, read would not assume the channel context is still alive.
  355. * This function may be called from another device, running into free memory as a
  356. * result. The drm node should still be there, so we can derive the index from
  357. * the fence context.
  358. */
  359. static bool nouveau_fence_is_signaled(struct dma_fence *f)
  360. {
  361. struct nouveau_fence *fence = from_fence(f);
  362. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  363. struct nouveau_channel *chan;
  364. bool ret = false;
  365. rcu_read_lock();
  366. chan = rcu_dereference(fence->channel);
  367. if (chan)
  368. ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0;
  369. rcu_read_unlock();
  370. return ret;
  371. }
  372. static bool nouveau_fence_no_signaling(struct dma_fence *f)
  373. {
  374. struct nouveau_fence *fence = from_fence(f);
  375. /*
  376. * caller should have a reference on the fence,
  377. * else fence could get freed here
  378. */
  379. WARN_ON(kref_read(&fence->base.refcount) <= 1);
  380. /*
  381. * This needs uevents to work correctly, but dma_fence_add_callback relies on
  382. * being able to enable signaling. It will still get signaled eventually,
  383. * just not right away.
  384. */
  385. if (nouveau_fence_is_signaled(f)) {
  386. list_del(&fence->head);
  387. dma_fence_put(&fence->base);
  388. return false;
  389. }
  390. return true;
  391. }
  392. static void nouveau_fence_release(struct dma_fence *f)
  393. {
  394. struct nouveau_fence *fence = from_fence(f);
  395. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  396. kref_put(&fctx->fence_ref, nouveau_fence_context_put);
  397. dma_fence_free(&fence->base);
  398. }
  399. static const struct dma_fence_ops nouveau_fence_ops_legacy = {
  400. .get_driver_name = nouveau_fence_get_get_driver_name,
  401. .get_timeline_name = nouveau_fence_get_timeline_name,
  402. .enable_signaling = nouveau_fence_no_signaling,
  403. .signaled = nouveau_fence_is_signaled,
  404. .wait = nouveau_fence_wait_legacy,
  405. .release = nouveau_fence_release
  406. };
  407. static bool nouveau_fence_enable_signaling(struct dma_fence *f)
  408. {
  409. struct nouveau_fence *fence = from_fence(f);
  410. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  411. bool ret;
  412. if (!fctx->notify_ref++)
  413. nvif_notify_get(&fctx->notify);
  414. ret = nouveau_fence_no_signaling(f);
  415. if (ret)
  416. set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
  417. else if (!--fctx->notify_ref)
  418. nvif_notify_put(&fctx->notify);
  419. return ret;
  420. }
  421. static const struct dma_fence_ops nouveau_fence_ops_uevent = {
  422. .get_driver_name = nouveau_fence_get_get_driver_name,
  423. .get_timeline_name = nouveau_fence_get_timeline_name,
  424. .enable_signaling = nouveau_fence_enable_signaling,
  425. .signaled = nouveau_fence_is_signaled,
  426. .wait = dma_fence_default_wait,
  427. .release = nouveau_fence_release
  428. };