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/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 *)nvif_client(chan->object);
  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->object, NULL,
  155. nouveau_fence_wait_uevent_handler, false,
  156. 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 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 fence *fence, struct 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 fence *fence,
  183. void (*func)(void *), void *data)
  184. {
  185. struct nouveau_fence_work *work;
  186. if (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 (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. fence_init(&fence->base, &nouveau_fence_ops_uevent,
  219. &fctx->lock, fctx->context, ++fctx->sequence);
  220. else
  221. fence_init(&fence->base, &nouveau_fence_ops_legacy,
  222. &fctx->lock, fctx->context, ++fctx->sequence);
  223. kref_get(&fctx->fence_ref);
  224. trace_fence_emit(&fence->base);
  225. ret = fctx->emit(fence);
  226. if (!ret) {
  227. 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(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 fence_is_signaled(&fence->base);
  253. }
  254. static long
  255. nouveau_fence_wait_legacy(struct 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 = ktime_set(0, 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 = 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 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 = 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 = fence_wait(fence, intr);
  362. }
  363. return ret;
  364. }
  365. void
  366. nouveau_fence_unref(struct nouveau_fence **pfence)
  367. {
  368. if (*pfence)
  369. 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 fence *fence)
  391. {
  392. return "nouveau";
  393. }
  394. static const char *nouveau_fence_get_timeline_name(struct 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 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 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 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. fence_put(&fence->base);
  435. return false;
  436. }
  437. return true;
  438. }
  439. static void nouveau_fence_release(struct 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. fence_free(&fence->base);
  445. }
  446. static const struct 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 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(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 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 = fence_default_wait,
  474. .release = NULL
  475. };