nouveau_fence.c 12 KB

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