nouveau_fence.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 "drmP.h"
  27. #include "drm.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_ramht.h"
  30. #include "nouveau_dma.h"
  31. #define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10)
  32. #define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17)
  33. struct nouveau_fence {
  34. struct nouveau_channel *channel;
  35. struct kref refcount;
  36. struct list_head entry;
  37. uint32_t sequence;
  38. bool signalled;
  39. void (*work)(void *priv, bool signalled);
  40. void *priv;
  41. };
  42. struct nouveau_semaphore {
  43. struct kref ref;
  44. struct drm_device *dev;
  45. struct drm_mm_node *mem;
  46. };
  47. static inline struct nouveau_fence *
  48. nouveau_fence(void *sync_obj)
  49. {
  50. return (struct nouveau_fence *)sync_obj;
  51. }
  52. static void
  53. nouveau_fence_del(struct kref *ref)
  54. {
  55. struct nouveau_fence *fence =
  56. container_of(ref, struct nouveau_fence, refcount);
  57. nouveau_channel_ref(NULL, &fence->channel);
  58. kfree(fence);
  59. }
  60. void
  61. nouveau_fence_update(struct nouveau_channel *chan)
  62. {
  63. struct drm_device *dev = chan->dev;
  64. struct nouveau_fence *tmp, *fence;
  65. uint32_t sequence;
  66. spin_lock(&chan->fence.lock);
  67. if (USE_REFCNT(dev))
  68. sequence = nvchan_rd32(chan, 0x48);
  69. else
  70. sequence = atomic_read(&chan->fence.last_sequence_irq);
  71. if (chan->fence.sequence_ack == sequence)
  72. goto out;
  73. chan->fence.sequence_ack = sequence;
  74. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  75. sequence = fence->sequence;
  76. fence->signalled = true;
  77. list_del(&fence->entry);
  78. if (unlikely(fence->work))
  79. fence->work(fence->priv, true);
  80. kref_put(&fence->refcount, nouveau_fence_del);
  81. if (sequence == chan->fence.sequence_ack)
  82. break;
  83. }
  84. out:
  85. spin_unlock(&chan->fence.lock);
  86. }
  87. int
  88. nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
  89. bool emit)
  90. {
  91. struct nouveau_fence *fence;
  92. int ret = 0;
  93. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  94. if (!fence)
  95. return -ENOMEM;
  96. kref_init(&fence->refcount);
  97. nouveau_channel_ref(chan, &fence->channel);
  98. if (emit)
  99. ret = nouveau_fence_emit(fence);
  100. if (ret)
  101. nouveau_fence_unref(&fence);
  102. *pfence = fence;
  103. return ret;
  104. }
  105. struct nouveau_channel *
  106. nouveau_fence_channel(struct nouveau_fence *fence)
  107. {
  108. return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
  109. }
  110. int
  111. nouveau_fence_emit(struct nouveau_fence *fence)
  112. {
  113. struct nouveau_channel *chan = fence->channel;
  114. struct drm_device *dev = chan->dev;
  115. int ret;
  116. ret = RING_SPACE(chan, 2);
  117. if (ret)
  118. return ret;
  119. if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
  120. nouveau_fence_update(chan);
  121. BUG_ON(chan->fence.sequence ==
  122. chan->fence.sequence_ack - 1);
  123. }
  124. fence->sequence = ++chan->fence.sequence;
  125. kref_get(&fence->refcount);
  126. spin_lock(&chan->fence.lock);
  127. list_add_tail(&fence->entry, &chan->fence.pending);
  128. spin_unlock(&chan->fence.lock);
  129. BEGIN_RING(chan, NvSubSw, USE_REFCNT(dev) ? 0x0050 : 0x0150, 1);
  130. OUT_RING(chan, fence->sequence);
  131. FIRE_RING(chan);
  132. return 0;
  133. }
  134. void
  135. nouveau_fence_work(struct nouveau_fence *fence,
  136. void (*work)(void *priv, bool signalled),
  137. void *priv)
  138. {
  139. BUG_ON(fence->work);
  140. spin_lock(&fence->channel->fence.lock);
  141. if (fence->signalled) {
  142. work(priv, true);
  143. } else {
  144. fence->work = work;
  145. fence->priv = priv;
  146. }
  147. spin_unlock(&fence->channel->fence.lock);
  148. }
  149. void
  150. __nouveau_fence_unref(void **sync_obj)
  151. {
  152. struct nouveau_fence *fence = nouveau_fence(*sync_obj);
  153. if (fence)
  154. kref_put(&fence->refcount, nouveau_fence_del);
  155. *sync_obj = NULL;
  156. }
  157. void *
  158. __nouveau_fence_ref(void *sync_obj)
  159. {
  160. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  161. kref_get(&fence->refcount);
  162. return sync_obj;
  163. }
  164. bool
  165. __nouveau_fence_signalled(void *sync_obj, void *sync_arg)
  166. {
  167. struct nouveau_fence *fence = nouveau_fence(sync_obj);
  168. struct nouveau_channel *chan = fence->channel;
  169. if (fence->signalled)
  170. return true;
  171. nouveau_fence_update(chan);
  172. return fence->signalled;
  173. }
  174. int
  175. __nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
  176. {
  177. unsigned long timeout = jiffies + (3 * DRM_HZ);
  178. int ret = 0;
  179. while (1) {
  180. if (__nouveau_fence_signalled(sync_obj, sync_arg))
  181. break;
  182. if (time_after_eq(jiffies, timeout)) {
  183. ret = -EBUSY;
  184. break;
  185. }
  186. __set_current_state(intr ? TASK_INTERRUPTIBLE
  187. : TASK_UNINTERRUPTIBLE);
  188. if (lazy)
  189. schedule_timeout(1);
  190. if (intr && signal_pending(current)) {
  191. ret = -ERESTARTSYS;
  192. break;
  193. }
  194. }
  195. __set_current_state(TASK_RUNNING);
  196. return ret;
  197. }
  198. static struct nouveau_semaphore *
  199. alloc_semaphore(struct drm_device *dev)
  200. {
  201. struct drm_nouveau_private *dev_priv = dev->dev_private;
  202. struct nouveau_semaphore *sema;
  203. int ret;
  204. if (!USE_SEMA(dev))
  205. return NULL;
  206. sema = kmalloc(sizeof(*sema), GFP_KERNEL);
  207. if (!sema)
  208. goto fail;
  209. ret = drm_mm_pre_get(&dev_priv->fence.heap);
  210. if (ret)
  211. goto fail;
  212. spin_lock(&dev_priv->fence.lock);
  213. sema->mem = drm_mm_search_free(&dev_priv->fence.heap, 4, 0, 0);
  214. if (sema->mem)
  215. sema->mem = drm_mm_get_block_atomic(sema->mem, 4, 0);
  216. spin_unlock(&dev_priv->fence.lock);
  217. if (!sema->mem)
  218. goto fail;
  219. kref_init(&sema->ref);
  220. sema->dev = dev;
  221. nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 0);
  222. return sema;
  223. fail:
  224. kfree(sema);
  225. return NULL;
  226. }
  227. static void
  228. free_semaphore(struct kref *ref)
  229. {
  230. struct nouveau_semaphore *sema =
  231. container_of(ref, struct nouveau_semaphore, ref);
  232. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  233. spin_lock(&dev_priv->fence.lock);
  234. drm_mm_put_block(sema->mem);
  235. spin_unlock(&dev_priv->fence.lock);
  236. kfree(sema);
  237. }
  238. static void
  239. semaphore_work(void *priv, bool signalled)
  240. {
  241. struct nouveau_semaphore *sema = priv;
  242. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  243. if (unlikely(!signalled))
  244. nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1);
  245. kref_put(&sema->ref, free_semaphore);
  246. }
  247. static int
  248. emit_semaphore(struct nouveau_channel *chan, int method,
  249. struct nouveau_semaphore *sema)
  250. {
  251. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  252. struct nouveau_fence *fence;
  253. bool smart = (dev_priv->card_type >= NV_50);
  254. int ret;
  255. ret = RING_SPACE(chan, smart ? 8 : 4);
  256. if (ret)
  257. return ret;
  258. if (smart) {
  259. BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
  260. OUT_RING(chan, NvSema);
  261. }
  262. BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1);
  263. OUT_RING(chan, sema->mem->start);
  264. if (smart && method == NV_SW_SEMAPHORE_ACQUIRE) {
  265. /*
  266. * NV50 tries to be too smart and context-switch
  267. * between semaphores instead of doing a "first come,
  268. * first served" strategy like previous cards
  269. * do.
  270. *
  271. * That's bad because the ACQUIRE latency can get as
  272. * large as the PFIFO context time slice in the
  273. * typical DRI2 case where you have several
  274. * outstanding semaphores at the same moment.
  275. *
  276. * If we're going to ACQUIRE, force the card to
  277. * context switch before, just in case the matching
  278. * RELEASE is already scheduled to be executed in
  279. * another channel.
  280. */
  281. BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
  282. OUT_RING(chan, 0);
  283. }
  284. BEGIN_RING(chan, NvSubSw, method, 1);
  285. OUT_RING(chan, 1);
  286. if (smart && method == NV_SW_SEMAPHORE_RELEASE) {
  287. /*
  288. * Force the card to context switch, there may be
  289. * another channel waiting for the semaphore we just
  290. * released.
  291. */
  292. BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
  293. OUT_RING(chan, 0);
  294. }
  295. /* Delay semaphore destruction until its work is done */
  296. ret = nouveau_fence_new(chan, &fence, true);
  297. if (ret)
  298. return ret;
  299. kref_get(&sema->ref);
  300. nouveau_fence_work(fence, semaphore_work, sema);
  301. nouveau_fence_unref(&fence);
  302. return 0;
  303. }
  304. int
  305. nouveau_fence_sync(struct nouveau_fence *fence,
  306. struct nouveau_channel *wchan)
  307. {
  308. struct nouveau_channel *chan = nouveau_fence_channel(fence);
  309. struct drm_device *dev = wchan->dev;
  310. struct nouveau_semaphore *sema;
  311. int ret = 0;
  312. if (likely(!chan || chan == wchan ||
  313. nouveau_fence_signalled(fence)))
  314. goto out;
  315. sema = alloc_semaphore(dev);
  316. if (!sema) {
  317. /* Early card or broken userspace, fall back to
  318. * software sync. */
  319. ret = nouveau_fence_wait(fence, true, false);
  320. goto out;
  321. }
  322. /* try to take chan's mutex, if we can't take it right away
  323. * we have to fallback to software sync to prevent locking
  324. * order issues
  325. */
  326. if (!mutex_trylock(&chan->mutex)) {
  327. ret = nouveau_fence_wait(fence, true, false);
  328. goto out_unref;
  329. }
  330. /* Make wchan wait until it gets signalled */
  331. ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema);
  332. if (ret)
  333. goto out_unlock;
  334. /* Signal the semaphore from chan */
  335. ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema);
  336. out_unlock:
  337. mutex_unlock(&chan->mutex);
  338. out_unref:
  339. kref_put(&sema->ref, free_semaphore);
  340. out:
  341. if (chan)
  342. nouveau_channel_put_unlocked(&chan);
  343. return ret;
  344. }
  345. int
  346. __nouveau_fence_flush(void *sync_obj, void *sync_arg)
  347. {
  348. return 0;
  349. }
  350. int
  351. nouveau_fence_channel_init(struct nouveau_channel *chan)
  352. {
  353. struct drm_device *dev = chan->dev;
  354. struct drm_nouveau_private *dev_priv = dev->dev_private;
  355. struct nouveau_gpuobj *obj = NULL;
  356. int ret;
  357. /* Create an NV_SW object for various sync purposes */
  358. ret = nouveau_gpuobj_gr_new(chan, NV_SW, &obj);
  359. if (ret)
  360. return ret;
  361. ret = nouveau_ramht_insert(chan, NvSw, obj);
  362. nouveau_gpuobj_ref(NULL, &obj);
  363. if (ret)
  364. return ret;
  365. ret = RING_SPACE(chan, 2);
  366. if (ret)
  367. return ret;
  368. BEGIN_RING(chan, NvSubSw, 0, 1);
  369. OUT_RING(chan, NvSw);
  370. /* Create a DMA object for the shared cross-channel sync area. */
  371. if (USE_SEMA(dev)) {
  372. struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node;
  373. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  374. mem->start << PAGE_SHIFT,
  375. mem->size << PAGE_SHIFT,
  376. NV_MEM_ACCESS_RW,
  377. NV_MEM_TARGET_VRAM, &obj);
  378. if (ret)
  379. return ret;
  380. ret = nouveau_ramht_insert(chan, NvSema, obj);
  381. nouveau_gpuobj_ref(NULL, &obj);
  382. if (ret)
  383. return ret;
  384. ret = RING_SPACE(chan, 2);
  385. if (ret)
  386. return ret;
  387. BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
  388. OUT_RING(chan, NvSema);
  389. }
  390. FIRE_RING(chan);
  391. INIT_LIST_HEAD(&chan->fence.pending);
  392. spin_lock_init(&chan->fence.lock);
  393. atomic_set(&chan->fence.last_sequence_irq, 0);
  394. return 0;
  395. }
  396. void
  397. nouveau_fence_channel_fini(struct nouveau_channel *chan)
  398. {
  399. struct nouveau_fence *tmp, *fence;
  400. spin_lock(&chan->fence.lock);
  401. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  402. fence->signalled = true;
  403. list_del(&fence->entry);
  404. if (unlikely(fence->work))
  405. fence->work(fence->priv, false);
  406. kref_put(&fence->refcount, nouveau_fence_del);
  407. }
  408. spin_unlock(&chan->fence.lock);
  409. }
  410. int
  411. nouveau_fence_init(struct drm_device *dev)
  412. {
  413. struct drm_nouveau_private *dev_priv = dev->dev_private;
  414. int ret;
  415. /* Create a shared VRAM heap for cross-channel sync. */
  416. if (USE_SEMA(dev)) {
  417. ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM,
  418. 0, 0, false, true, &dev_priv->fence.bo);
  419. if (ret)
  420. return ret;
  421. ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM);
  422. if (ret)
  423. goto fail;
  424. ret = nouveau_bo_map(dev_priv->fence.bo);
  425. if (ret)
  426. goto fail;
  427. ret = drm_mm_init(&dev_priv->fence.heap, 0,
  428. dev_priv->fence.bo->bo.mem.size);
  429. if (ret)
  430. goto fail;
  431. spin_lock_init(&dev_priv->fence.lock);
  432. }
  433. return 0;
  434. fail:
  435. nouveau_bo_unmap(dev_priv->fence.bo);
  436. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  437. return ret;
  438. }
  439. void
  440. nouveau_fence_fini(struct drm_device *dev)
  441. {
  442. struct drm_nouveau_private *dev_priv = dev->dev_private;
  443. if (USE_SEMA(dev)) {
  444. drm_mm_takedown(&dev_priv->fence.heap);
  445. nouveau_bo_unmap(dev_priv->fence.bo);
  446. nouveau_bo_unpin(dev_priv->fence.bo);
  447. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  448. }
  449. }