nouveau_fence.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. bool smart = (dev_priv->card_type >= NV_50);
  249. int ret;
  250. ret = RING_SPACE(chan, smart ? 8 : 4);
  251. if (ret)
  252. return ret;
  253. if (smart) {
  254. BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
  255. OUT_RING(chan, NvSema);
  256. }
  257. BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1);
  258. OUT_RING(chan, sema->mem->start);
  259. if (smart && method == NV_SW_SEMAPHORE_ACQUIRE) {
  260. /*
  261. * NV50 tries to be too smart and context-switch
  262. * between semaphores instead of doing a "first come,
  263. * first served" strategy like previous cards
  264. * do.
  265. *
  266. * That's bad because the ACQUIRE latency can get as
  267. * large as the PFIFO context time slice in the
  268. * typical DRI2 case where you have several
  269. * outstanding semaphores at the same moment.
  270. *
  271. * If we're going to ACQUIRE, force the card to
  272. * context switch before, just in case the matching
  273. * RELEASE is already scheduled to be executed in
  274. * another channel.
  275. */
  276. BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
  277. OUT_RING(chan, 0);
  278. }
  279. BEGIN_RING(chan, NvSubSw, method, 1);
  280. OUT_RING(chan, 1);
  281. if (smart && method == NV_SW_SEMAPHORE_RELEASE) {
  282. /*
  283. * Force the card to context switch, there may be
  284. * another channel waiting for the semaphore we just
  285. * released.
  286. */
  287. BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
  288. OUT_RING(chan, 0);
  289. }
  290. /* Delay semaphore destruction until its work is done */
  291. ret = nouveau_fence_new(chan, &fence, true);
  292. if (ret)
  293. return ret;
  294. kref_get(&sema->ref);
  295. nouveau_fence_work(fence, semaphore_work, sema);
  296. nouveau_fence_unref((void *)&fence);
  297. return 0;
  298. }
  299. int
  300. nouveau_fence_sync(struct nouveau_fence *fence,
  301. struct nouveau_channel *wchan)
  302. {
  303. struct nouveau_channel *chan = nouveau_fence_channel(fence);
  304. struct drm_device *dev = wchan->dev;
  305. struct nouveau_semaphore *sema;
  306. int ret;
  307. if (likely(!fence || chan == wchan ||
  308. nouveau_fence_signalled(fence, NULL)))
  309. return 0;
  310. sema = alloc_semaphore(dev);
  311. if (!sema) {
  312. /* Early card or broken userspace, fall back to
  313. * software sync. */
  314. return nouveau_fence_wait(fence, NULL, false, false);
  315. }
  316. /* Make wchan wait until it gets signalled */
  317. ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema);
  318. if (ret)
  319. goto out;
  320. /* Signal the semaphore from chan */
  321. ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema);
  322. out:
  323. kref_put(&sema->ref, free_semaphore);
  324. return ret;
  325. }
  326. int
  327. nouveau_fence_flush(void *sync_obj, void *sync_arg)
  328. {
  329. return 0;
  330. }
  331. int
  332. nouveau_fence_channel_init(struct nouveau_channel *chan)
  333. {
  334. struct drm_device *dev = chan->dev;
  335. struct drm_nouveau_private *dev_priv = dev->dev_private;
  336. struct nouveau_gpuobj *obj = NULL;
  337. int ret;
  338. /* Create an NV_SW object for various sync purposes */
  339. ret = nouveau_gpuobj_sw_new(chan, NV_SW, &obj);
  340. if (ret)
  341. return ret;
  342. ret = nouveau_ramht_insert(chan, NvSw, obj);
  343. nouveau_gpuobj_ref(NULL, &obj);
  344. if (ret)
  345. return ret;
  346. ret = RING_SPACE(chan, 2);
  347. if (ret)
  348. return ret;
  349. BEGIN_RING(chan, NvSubSw, 0, 1);
  350. OUT_RING(chan, NvSw);
  351. /* Create a DMA object for the shared cross-channel sync area. */
  352. if (USE_SEMA(dev)) {
  353. struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node;
  354. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  355. mem->start << PAGE_SHIFT,
  356. mem->size << PAGE_SHIFT,
  357. NV_DMA_ACCESS_RW,
  358. NV_DMA_TARGET_VIDMEM, &obj);
  359. if (ret)
  360. return ret;
  361. ret = nouveau_ramht_insert(chan, NvSema, 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, NV_SW_DMA_SEMAPHORE, 1);
  369. OUT_RING(chan, NvSema);
  370. }
  371. FIRE_RING(chan);
  372. INIT_LIST_HEAD(&chan->fence.pending);
  373. spin_lock_init(&chan->fence.lock);
  374. atomic_set(&chan->fence.last_sequence_irq, 0);
  375. return 0;
  376. }
  377. void
  378. nouveau_fence_channel_fini(struct nouveau_channel *chan)
  379. {
  380. struct nouveau_fence *tmp, *fence;
  381. list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
  382. fence->signalled = true;
  383. list_del(&fence->entry);
  384. if (unlikely(fence->work))
  385. fence->work(fence->priv, false);
  386. kref_put(&fence->refcount, nouveau_fence_del);
  387. }
  388. }
  389. int
  390. nouveau_fence_init(struct drm_device *dev)
  391. {
  392. struct drm_nouveau_private *dev_priv = dev->dev_private;
  393. int ret;
  394. /* Create a shared VRAM heap for cross-channel sync. */
  395. if (USE_SEMA(dev)) {
  396. ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM,
  397. 0, 0, false, true, &dev_priv->fence.bo);
  398. if (ret)
  399. return ret;
  400. ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM);
  401. if (ret)
  402. goto fail;
  403. ret = nouveau_bo_map(dev_priv->fence.bo);
  404. if (ret)
  405. goto fail;
  406. ret = drm_mm_init(&dev_priv->fence.heap, 0,
  407. dev_priv->fence.bo->bo.mem.size);
  408. if (ret)
  409. goto fail;
  410. spin_lock_init(&dev_priv->fence.lock);
  411. }
  412. return 0;
  413. fail:
  414. nouveau_bo_unmap(dev_priv->fence.bo);
  415. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  416. return ret;
  417. }
  418. void
  419. nouveau_fence_fini(struct drm_device *dev)
  420. {
  421. struct drm_nouveau_private *dev_priv = dev->dev_private;
  422. if (USE_SEMA(dev)) {
  423. drm_mm_takedown(&dev_priv->fence.heap);
  424. nouveau_bo_unmap(dev_priv->fence.bo);
  425. nouveau_bo_unpin(dev_priv->fence.bo);
  426. nouveau_bo_ref(NULL, &dev_priv->fence.bo);
  427. }
  428. }