nouveau_fence.c 13 KB

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