nouveau_fence.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. if (!USE_SEMA(dev))
  203. return NULL;
  204. sema = kmalloc(sizeof(*sema), GFP_KERNEL);
  205. if (!sema)
  206. goto fail;
  207. spin_lock(&dev_priv->fence.lock);
  208. sema->mem = drm_mm_search_free(&dev_priv->fence.heap, 4, 0, 0);
  209. if (sema->mem)
  210. sema->mem = drm_mm_get_block(sema->mem, 4, 0);
  211. spin_unlock(&dev_priv->fence.lock);
  212. if (!sema->mem)
  213. goto fail;
  214. kref_init(&sema->ref);
  215. sema->dev = dev;
  216. nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 0);
  217. return sema;
  218. fail:
  219. kfree(sema);
  220. return NULL;
  221. }
  222. static void
  223. free_semaphore(struct kref *ref)
  224. {
  225. struct nouveau_semaphore *sema =
  226. container_of(ref, struct nouveau_semaphore, ref);
  227. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  228. spin_lock(&dev_priv->fence.lock);
  229. drm_mm_put_block(sema->mem);
  230. spin_unlock(&dev_priv->fence.lock);
  231. kfree(sema);
  232. }
  233. static void
  234. semaphore_work(void *priv, bool signalled)
  235. {
  236. struct nouveau_semaphore *sema = priv;
  237. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  238. if (unlikely(!signalled))
  239. nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1);
  240. kref_put(&sema->ref, free_semaphore);
  241. }
  242. static int
  243. emit_semaphore(struct nouveau_channel *chan, int method,
  244. struct nouveau_semaphore *sema)
  245. {
  246. struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
  247. struct nouveau_fence *fence;
  248. int ret;
  249. ret = RING_SPACE(chan, dev_priv->card_type >= NV_50 ? 6 : 4);
  250. if (ret)
  251. return ret;
  252. if (dev_priv->card_type >= NV_50) {
  253. BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
  254. OUT_RING(chan, NvSema);
  255. }
  256. BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1);
  257. OUT_RING(chan, sema->mem->start);
  258. BEGIN_RING(chan, NvSubSw, method, 1);
  259. OUT_RING(chan, 1);
  260. /* Delay semaphore destruction until its work is done */
  261. ret = nouveau_fence_new(chan, &fence, true);
  262. if (ret)
  263. return ret;
  264. kref_get(&sema->ref);
  265. nouveau_fence_work(fence, semaphore_work, sema);
  266. nouveau_fence_unref((void *)&fence);
  267. return 0;
  268. }
  269. int
  270. nouveau_fence_sync(struct nouveau_fence *fence,
  271. struct nouveau_channel *wchan)
  272. {
  273. struct nouveau_channel *chan = nouveau_fence_channel(fence);
  274. struct drm_device *dev = wchan->dev;
  275. struct nouveau_semaphore *sema;
  276. int ret;
  277. if (likely(!fence || chan == wchan ||
  278. nouveau_fence_signalled(fence, NULL)))
  279. return 0;
  280. sema = alloc_semaphore(dev);
  281. if (!sema) {
  282. /* Early card or broken userspace, fall back to
  283. * software sync. */
  284. return nouveau_fence_wait(fence, NULL, false, false);
  285. }
  286. /* Signal the semaphore from chan */
  287. ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema);
  288. if (ret)
  289. goto out;
  290. /* Make wchan wait until it gets signalled */
  291. ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema);
  292. out:
  293. kref_put(&sema->ref, free_semaphore);
  294. return ret;
  295. }
  296. int
  297. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  298. {
  299. return 0;
  300. }
  301. int
  302. nouveau_fence_channel_init(struct nouveau_channel *chan)
  303. {
  304. struct drm_device *dev = chan->dev;
  305. struct drm_nouveau_private *dev_priv = dev->dev_private;
  306. struct nouveau_gpuobj *obj = NULL;
  307. int ret;
  308. /* Create an NV_SW object for various sync purposes */
  309. ret = nouveau_gpuobj_sw_new(chan, NV_SW, &obj);
  310. if (ret)
  311. return ret;
  312. ret = nouveau_ramht_insert(chan, NvSw, obj);
  313. nouveau_gpuobj_ref(NULL, &obj);
  314. if (ret)
  315. return ret;
  316. ret = RING_SPACE(chan, 2);
  317. if (ret)
  318. return ret;
  319. BEGIN_RING(chan, NvSubSw, 0, 1);
  320. OUT_RING(chan, NvSw);
  321. /* Create a DMA object for the shared cross-channel sync area. */
  322. if (USE_SEMA(dev)) {
  323. struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node;
  324. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  325. mem->start << PAGE_SHIFT,
  326. mem->size << PAGE_SHIFT,
  327. NV_DMA_ACCESS_RW,
  328. NV_DMA_TARGET_VIDMEM, &obj);
  329. if (ret)
  330. return ret;
  331. ret = nouveau_ramht_insert(chan, NvSema, obj);
  332. nouveau_gpuobj_ref(NULL, &obj);
  333. if (ret)
  334. return ret;
  335. ret = RING_SPACE(chan, 2);
  336. if (ret)
  337. return ret;
  338. BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
  339. OUT_RING(chan, NvSema);
  340. }
  341. FIRE_RING(chan);
  342. INIT_LIST_HEAD(&chan->fence.pending);
  343. spin_lock_init(&chan->fence.lock);
  344. atomic_set(&chan->fence.last_sequence_irq, 0);
  345. return 0;
  346. }
  347. void
  348. nouveau_fence_channel_fini(struct nouveau_channel *chan)
  349. {
  350. struct nouveau_fence *tmp, *fence;
  351. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  352. fence->signalled = true;
  353. list_del(&fence->entry);
  354. if (unlikely(fence->work))
  355. fence->work(fence->priv, false);
  356. kref_put(&fence->refcount, nouveau_fence_del);
  357. }
  358. }
  359. int
  360. nouveau_fence_init(struct drm_device *dev)
  361. {
  362. struct drm_nouveau_private *dev_priv = dev->dev_private;
  363. int ret;
  364. /* Create a shared VRAM heap for cross-channel sync. */
  365. if (USE_SEMA(dev)) {
  366. ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM,
  367. 0, 0, false, true, &dev_priv->fence.bo);
  368. if (ret)
  369. return ret;
  370. ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM);
  371. if (ret)
  372. goto fail;
  373. ret = nouveau_bo_map(dev_priv->fence.bo);
  374. if (ret)
  375. goto fail;
  376. ret = drm_mm_init(&dev_priv->fence.heap, 0,
  377. dev_priv->fence.bo->bo.mem.size);
  378. if (ret)
  379. goto fail;
  380. spin_lock_init(&dev_priv->fence.lock);
  381. }
  382. return 0;
  383. fail:
  384. nouveau_bo_unmap(dev_priv->fence.bo);
  385. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  386. return ret;
  387. }
  388. void
  389. nouveau_fence_fini(struct drm_device *dev)
  390. {
  391. struct drm_nouveau_private *dev_priv = dev->dev_private;
  392. if (USE_SEMA(dev)) {
  393. drm_mm_takedown(&dev_priv->fence.heap);
  394. nouveau_bo_unmap(dev_priv->fence.bo);
  395. nouveau_bo_unpin(dev_priv->fence.bo);
  396. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  397. }
  398. }