nouveau_fence.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. struct nouveau_fence_priv *priv = (void*)drm->fence;
  66. if (fence->ops != &nouveau_fence_ops_legacy &&
  67. fence->ops != &nouveau_fence_ops_uevent)
  68. return NULL;
  69. if (fence->context < priv->context_base ||
  70. fence->context >= priv->context_base + priv->contexts)
  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(fence->channel, 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 = priv->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, G82_CHANNEL_DMA_V0_NTFY_UEVENT,
  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. struct nouveau_fence_work {
  164. struct work_struct work;
  165. struct dma_fence_cb cb;
  166. void (*func)(void *);
  167. void *data;
  168. };
  169. static void
  170. nouveau_fence_work_handler(struct work_struct *kwork)
  171. {
  172. struct nouveau_fence_work *work = container_of(kwork, typeof(*work), work);
  173. work->func(work->data);
  174. kfree(work);
  175. }
  176. static void nouveau_fence_work_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  177. {
  178. struct nouveau_fence_work *work = container_of(cb, typeof(*work), cb);
  179. schedule_work(&work->work);
  180. }
  181. void
  182. nouveau_fence_work(struct dma_fence *fence,
  183. void (*func)(void *), void *data)
  184. {
  185. struct nouveau_fence_work *work;
  186. if (dma_fence_is_signaled(fence))
  187. goto err;
  188. work = kmalloc(sizeof(*work), GFP_KERNEL);
  189. if (!work) {
  190. /*
  191. * this might not be a nouveau fence any more,
  192. * so force a lazy wait here
  193. */
  194. WARN_ON(nouveau_fence_wait((struct nouveau_fence *)fence,
  195. true, false));
  196. goto err;
  197. }
  198. INIT_WORK(&work->work, nouveau_fence_work_handler);
  199. work->func = func;
  200. work->data = data;
  201. if (dma_fence_add_callback(fence, &work->cb, nouveau_fence_work_cb) < 0)
  202. goto err_free;
  203. return;
  204. err_free:
  205. kfree(work);
  206. err:
  207. func(data);
  208. }
  209. int
  210. nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
  211. {
  212. struct nouveau_fence_chan *fctx = chan->fence;
  213. struct nouveau_fence_priv *priv = (void*)chan->drm->fence;
  214. int ret;
  215. fence->channel = chan;
  216. fence->timeout = jiffies + (15 * HZ);
  217. if (priv->uevent)
  218. dma_fence_init(&fence->base, &nouveau_fence_ops_uevent,
  219. &fctx->lock, fctx->context, ++fctx->sequence);
  220. else
  221. dma_fence_init(&fence->base, &nouveau_fence_ops_legacy,
  222. &fctx->lock, fctx->context, ++fctx->sequence);
  223. kref_get(&fctx->fence_ref);
  224. trace_dma_fence_emit(&fence->base);
  225. ret = fctx->emit(fence);
  226. if (!ret) {
  227. dma_fence_get(&fence->base);
  228. spin_lock_irq(&fctx->lock);
  229. if (nouveau_fence_update(chan, fctx))
  230. nvif_notify_put(&fctx->notify);
  231. list_add_tail(&fence->head, &fctx->pending);
  232. spin_unlock_irq(&fctx->lock);
  233. }
  234. return ret;
  235. }
  236. bool
  237. nouveau_fence_done(struct nouveau_fence *fence)
  238. {
  239. if (fence->base.ops == &nouveau_fence_ops_legacy ||
  240. fence->base.ops == &nouveau_fence_ops_uevent) {
  241. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  242. struct nouveau_channel *chan;
  243. unsigned long flags;
  244. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
  245. return true;
  246. spin_lock_irqsave(&fctx->lock, flags);
  247. chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
  248. if (chan && nouveau_fence_update(chan, fctx))
  249. nvif_notify_put(&fctx->notify);
  250. spin_unlock_irqrestore(&fctx->lock, flags);
  251. }
  252. return dma_fence_is_signaled(&fence->base);
  253. }
  254. static long
  255. nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait)
  256. {
  257. struct nouveau_fence *fence = from_fence(f);
  258. unsigned long sleep_time = NSEC_PER_MSEC / 1000;
  259. unsigned long t = jiffies, timeout = t + wait;
  260. while (!nouveau_fence_done(fence)) {
  261. ktime_t kt;
  262. t = jiffies;
  263. if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) {
  264. __set_current_state(TASK_RUNNING);
  265. return 0;
  266. }
  267. __set_current_state(intr ? TASK_INTERRUPTIBLE :
  268. TASK_UNINTERRUPTIBLE);
  269. kt = sleep_time;
  270. schedule_hrtimeout(&kt, HRTIMER_MODE_REL);
  271. sleep_time *= 2;
  272. if (sleep_time > NSEC_PER_MSEC)
  273. sleep_time = NSEC_PER_MSEC;
  274. if (intr && signal_pending(current))
  275. return -ERESTARTSYS;
  276. }
  277. __set_current_state(TASK_RUNNING);
  278. return timeout - t;
  279. }
  280. static int
  281. nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr)
  282. {
  283. int ret = 0;
  284. while (!nouveau_fence_done(fence)) {
  285. if (time_after_eq(jiffies, fence->timeout)) {
  286. ret = -EBUSY;
  287. break;
  288. }
  289. __set_current_state(intr ?
  290. TASK_INTERRUPTIBLE :
  291. TASK_UNINTERRUPTIBLE);
  292. if (intr && signal_pending(current)) {
  293. ret = -ERESTARTSYS;
  294. break;
  295. }
  296. }
  297. __set_current_state(TASK_RUNNING);
  298. return ret;
  299. }
  300. int
  301. nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
  302. {
  303. long ret;
  304. if (!lazy)
  305. return nouveau_fence_wait_busy(fence, intr);
  306. ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ);
  307. if (ret < 0)
  308. return ret;
  309. else if (!ret)
  310. return -EBUSY;
  311. else
  312. return 0;
  313. }
  314. int
  315. nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool exclusive, bool intr)
  316. {
  317. struct nouveau_fence_chan *fctx = chan->fence;
  318. struct dma_fence *fence;
  319. struct reservation_object *resv = nvbo->bo.resv;
  320. struct reservation_object_list *fobj;
  321. struct nouveau_fence *f;
  322. int ret = 0, i;
  323. if (!exclusive) {
  324. ret = reservation_object_reserve_shared(resv);
  325. if (ret)
  326. return ret;
  327. }
  328. fobj = reservation_object_get_list(resv);
  329. fence = reservation_object_get_excl(resv);
  330. if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
  331. struct nouveau_channel *prev = NULL;
  332. bool must_wait = true;
  333. f = nouveau_local_fence(fence, chan->drm);
  334. if (f) {
  335. rcu_read_lock();
  336. prev = rcu_dereference(f->channel);
  337. if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
  338. must_wait = false;
  339. rcu_read_unlock();
  340. }
  341. if (must_wait)
  342. ret = dma_fence_wait(fence, intr);
  343. return ret;
  344. }
  345. if (!exclusive || !fobj)
  346. return ret;
  347. for (i = 0; i < fobj->shared_count && !ret; ++i) {
  348. struct nouveau_channel *prev = NULL;
  349. bool must_wait = true;
  350. fence = rcu_dereference_protected(fobj->shared[i],
  351. reservation_object_held(resv));
  352. f = nouveau_local_fence(fence, chan->drm);
  353. if (f) {
  354. rcu_read_lock();
  355. prev = rcu_dereference(f->channel);
  356. if (prev && (prev == chan || fctx->sync(f, prev, chan) == 0))
  357. must_wait = false;
  358. rcu_read_unlock();
  359. }
  360. if (must_wait)
  361. ret = dma_fence_wait(fence, intr);
  362. }
  363. return ret;
  364. }
  365. void
  366. nouveau_fence_unref(struct nouveau_fence **pfence)
  367. {
  368. if (*pfence)
  369. dma_fence_put(&(*pfence)->base);
  370. *pfence = NULL;
  371. }
  372. int
  373. nouveau_fence_new(struct nouveau_channel *chan, bool sysmem,
  374. struct nouveau_fence **pfence)
  375. {
  376. struct nouveau_fence *fence;
  377. int ret = 0;
  378. if (unlikely(!chan->fence))
  379. return -ENODEV;
  380. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  381. if (!fence)
  382. return -ENOMEM;
  383. fence->sysmem = sysmem;
  384. ret = nouveau_fence_emit(fence, chan);
  385. if (ret)
  386. nouveau_fence_unref(&fence);
  387. *pfence = fence;
  388. return ret;
  389. }
  390. static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence)
  391. {
  392. return "nouveau";
  393. }
  394. static const char *nouveau_fence_get_timeline_name(struct dma_fence *f)
  395. {
  396. struct nouveau_fence *fence = from_fence(f);
  397. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  398. return !fctx->dead ? fctx->name : "dead channel";
  399. }
  400. /*
  401. * In an ideal world, read would not assume the channel context is still alive.
  402. * This function may be called from another device, running into free memory as a
  403. * result. The drm node should still be there, so we can derive the index from
  404. * the fence context.
  405. */
  406. static bool nouveau_fence_is_signaled(struct dma_fence *f)
  407. {
  408. struct nouveau_fence *fence = from_fence(f);
  409. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  410. struct nouveau_channel *chan;
  411. bool ret = false;
  412. rcu_read_lock();
  413. chan = rcu_dereference(fence->channel);
  414. if (chan)
  415. ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0;
  416. rcu_read_unlock();
  417. return ret;
  418. }
  419. static bool nouveau_fence_no_signaling(struct dma_fence *f)
  420. {
  421. struct nouveau_fence *fence = from_fence(f);
  422. /*
  423. * caller should have a reference on the fence,
  424. * else fence could get freed here
  425. */
  426. WARN_ON(atomic_read(&fence->base.refcount.refcount) <= 1);
  427. /*
  428. * This needs uevents to work correctly, but dma_fence_add_callback relies on
  429. * being able to enable signaling. It will still get signaled eventually,
  430. * just not right away.
  431. */
  432. if (nouveau_fence_is_signaled(f)) {
  433. list_del(&fence->head);
  434. dma_fence_put(&fence->base);
  435. return false;
  436. }
  437. return true;
  438. }
  439. static void nouveau_fence_release(struct dma_fence *f)
  440. {
  441. struct nouveau_fence *fence = from_fence(f);
  442. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  443. kref_put(&fctx->fence_ref, nouveau_fence_context_put);
  444. dma_fence_free(&fence->base);
  445. }
  446. static const struct dma_fence_ops nouveau_fence_ops_legacy = {
  447. .get_driver_name = nouveau_fence_get_get_driver_name,
  448. .get_timeline_name = nouveau_fence_get_timeline_name,
  449. .enable_signaling = nouveau_fence_no_signaling,
  450. .signaled = nouveau_fence_is_signaled,
  451. .wait = nouveau_fence_wait_legacy,
  452. .release = nouveau_fence_release
  453. };
  454. static bool nouveau_fence_enable_signaling(struct dma_fence *f)
  455. {
  456. struct nouveau_fence *fence = from_fence(f);
  457. struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
  458. bool ret;
  459. if (!fctx->notify_ref++)
  460. nvif_notify_get(&fctx->notify);
  461. ret = nouveau_fence_no_signaling(f);
  462. if (ret)
  463. set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
  464. else if (!--fctx->notify_ref)
  465. nvif_notify_put(&fctx->notify);
  466. return ret;
  467. }
  468. static const struct dma_fence_ops nouveau_fence_ops_uevent = {
  469. .get_driver_name = nouveau_fence_get_get_driver_name,
  470. .get_timeline_name = nouveau_fence_get_timeline_name,
  471. .enable_signaling = nouveau_fence_enable_signaling,
  472. .signaled = nouveau_fence_is_signaled,
  473. .wait = dma_fence_default_wait,
  474. .release = nouveau_fence_release
  475. };