nouveau_fence.c 14 KB

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